Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR MIT) |
| 2 | /* |
| 3 | * Microsemi Ocelot Switch driver |
| 4 | * |
| 5 | * Copyright (c) 2017 Microsemi Corporation |
| 6 | */ |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 7 | #include <linux/dsa/ocelot.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 8 | #include <linux/if_bridge.h> |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 9 | #include <linux/ptp_classify.h> |
Vladimir Oltean | 2096805 | 2020-09-30 01:27:26 +0300 | [diff] [blame] | 10 | #include <soc/mscc/ocelot_vcap.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 11 | #include "ocelot.h" |
Vladimir Oltean | 3c83654 | 2020-06-20 18:43:45 +0300 | [diff] [blame] | 12 | #include "ocelot_vcap.h" |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 13 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 14 | #define TABLE_UPDATE_SLEEP_US 10 |
| 15 | #define TABLE_UPDATE_TIMEOUT_US 100000 |
| 16 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 17 | struct ocelot_mact_entry { |
| 18 | u8 mac[ETH_ALEN]; |
| 19 | u16 vid; |
| 20 | enum macaccess_entry_type type; |
| 21 | }; |
| 22 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 23 | /* Caller must hold &ocelot->mact_lock */ |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 24 | static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) |
| 25 | { |
| 26 | return ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 27 | } |
| 28 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 29 | /* Caller must hold &ocelot->mact_lock */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 30 | static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) |
| 31 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 32 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 33 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 34 | return readx_poll_timeout(ocelot_mact_read_macaccess, |
| 35 | ocelot, val, |
| 36 | (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == |
| 37 | MACACCESS_CMD_IDLE, |
| 38 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 41 | /* Caller must hold &ocelot->mact_lock */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 42 | static void ocelot_mact_select(struct ocelot *ocelot, |
| 43 | const unsigned char mac[ETH_ALEN], |
| 44 | unsigned int vid) |
| 45 | { |
| 46 | u32 macl = 0, mach = 0; |
| 47 | |
| 48 | /* Set the MAC address to handle and the vlan associated in a format |
| 49 | * understood by the hardware. |
| 50 | */ |
| 51 | mach |= vid << 16; |
| 52 | mach |= mac[0] << 8; |
| 53 | mach |= mac[1] << 0; |
| 54 | macl |= mac[2] << 24; |
| 55 | macl |= mac[3] << 16; |
| 56 | macl |= mac[4] << 8; |
| 57 | macl |= mac[5] << 0; |
| 58 | |
| 59 | ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); |
| 60 | ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); |
| 61 | |
| 62 | } |
| 63 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 64 | static int __ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 65 | const unsigned char mac[ETH_ALEN], |
| 66 | unsigned int vid, enum macaccess_entry_type type) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 67 | { |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 68 | u32 cmd = ANA_TABLES_MACACCESS_VALID | |
| 69 | ANA_TABLES_MACACCESS_DEST_IDX(port) | |
| 70 | ANA_TABLES_MACACCESS_ENTRYTYPE(type) | |
| 71 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); |
| 72 | unsigned int mc_ports; |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 73 | int err; |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 74 | |
| 75 | /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ |
| 76 | if (type == ENTRYTYPE_MACv4) |
| 77 | mc_ports = (mac[1] << 8) | mac[2]; |
| 78 | else if (type == ENTRYTYPE_MACv6) |
| 79 | mc_ports = (mac[0] << 8) | mac[1]; |
| 80 | else |
| 81 | mc_ports = 0; |
| 82 | |
| 83 | if (mc_ports & BIT(ocelot->num_phys_ports)) |
| 84 | cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; |
| 85 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 86 | ocelot_mact_select(ocelot, mac, vid); |
| 87 | |
| 88 | /* Issue a write command */ |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 89 | ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 90 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 91 | err = ocelot_mact_wait_for_completion(ocelot); |
| 92 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 93 | return err; |
| 94 | } |
| 95 | |
| 96 | int ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 97 | const unsigned char mac[ETH_ALEN], |
| 98 | unsigned int vid, enum macaccess_entry_type type) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | mutex_lock(&ocelot->mact_lock); |
| 103 | ret = __ocelot_mact_learn(ocelot, port, mac, vid, type); |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 104 | mutex_unlock(&ocelot->mact_lock); |
| 105 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 106 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 107 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 108 | EXPORT_SYMBOL(ocelot_mact_learn); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 109 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 110 | int ocelot_mact_forget(struct ocelot *ocelot, |
| 111 | const unsigned char mac[ETH_ALEN], unsigned int vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 112 | { |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 113 | int err; |
| 114 | |
| 115 | mutex_lock(&ocelot->mact_lock); |
| 116 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 117 | ocelot_mact_select(ocelot, mac, vid); |
| 118 | |
| 119 | /* Issue a forget command */ |
| 120 | ocelot_write(ocelot, |
| 121 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), |
| 122 | ANA_TABLES_MACACCESS); |
| 123 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 124 | err = ocelot_mact_wait_for_completion(ocelot); |
| 125 | |
| 126 | mutex_unlock(&ocelot->mact_lock); |
| 127 | |
| 128 | return err; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 129 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 130 | EXPORT_SYMBOL(ocelot_mact_forget); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 131 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 132 | int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx, |
| 133 | const unsigned char mac[ETH_ALEN], |
| 134 | unsigned int vid, enum macaccess_entry_type *type) |
| 135 | { |
| 136 | int val; |
| 137 | |
| 138 | mutex_lock(&ocelot->mact_lock); |
| 139 | |
| 140 | ocelot_mact_select(ocelot, mac, vid); |
| 141 | |
| 142 | /* Issue a read command with MACACCESS_VALID=1. */ |
| 143 | ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | |
| 144 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 145 | ANA_TABLES_MACACCESS); |
| 146 | |
| 147 | if (ocelot_mact_wait_for_completion(ocelot)) { |
| 148 | mutex_unlock(&ocelot->mact_lock); |
| 149 | return -ETIMEDOUT; |
| 150 | } |
| 151 | |
| 152 | /* Read back the entry flags */ |
| 153 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 154 | |
| 155 | mutex_unlock(&ocelot->mact_lock); |
| 156 | |
| 157 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 158 | return -ENOENT; |
| 159 | |
| 160 | *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val); |
| 161 | *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | EXPORT_SYMBOL(ocelot_mact_lookup); |
| 166 | |
| 167 | int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx, |
| 168 | const unsigned char mac[ETH_ALEN], |
| 169 | unsigned int vid, |
| 170 | enum macaccess_entry_type type, |
| 171 | int sfid, int ssid) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | mutex_lock(&ocelot->mact_lock); |
| 176 | |
| 177 | ocelot_write(ocelot, |
| 178 | (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) | |
| 179 | ANA_TABLES_STREAMDATA_SFID(sfid) | |
| 180 | (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) | |
| 181 | ANA_TABLES_STREAMDATA_SSID(ssid), |
| 182 | ANA_TABLES_STREAMDATA); |
| 183 | |
| 184 | ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type); |
| 185 | |
| 186 | mutex_unlock(&ocelot->mact_lock); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | EXPORT_SYMBOL(ocelot_mact_learn_streamdata); |
| 191 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 192 | static void ocelot_mact_init(struct ocelot *ocelot) |
| 193 | { |
| 194 | /* Configure the learning mode entries attributes: |
| 195 | * - Do not copy the frame to the CPU extraction queues. |
| 196 | * - Use the vlan and mac_cpoy for dmac lookup. |
| 197 | */ |
| 198 | ocelot_rmw(ocelot, 0, |
| 199 | ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS |
| 200 | | ANA_AGENCTRL_LEARN_FWD_KILL |
| 201 | | ANA_AGENCTRL_LEARN_IGNORE_VLAN, |
| 202 | ANA_AGENCTRL); |
| 203 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 204 | /* Clear the MAC table. We are not concurrent with anyone, so |
| 205 | * holding &ocelot->mact_lock is pointless. |
| 206 | */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 207 | ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); |
| 208 | } |
| 209 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 210 | static void ocelot_vcap_enable(struct ocelot *ocelot, int port) |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 211 | { |
| 212 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | |
| 213 | ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 214 | ANA_PORT_VCAP_S2_CFG, port); |
Xiaoliang Yang | 75944fd | 2020-10-02 15:02:23 +0300 | [diff] [blame] | 215 | |
| 216 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, |
| 217 | ANA_PORT_VCAP_CFG, port); |
Xiaoliang Yang | 2f17c05 | 2020-10-02 15:02:24 +0300 | [diff] [blame] | 218 | |
| 219 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, |
| 220 | REW_PORT_CFG_ES0_EN, |
| 221 | REW_PORT_CFG, port); |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 224 | static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) |
| 225 | { |
| 226 | return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); |
| 227 | } |
| 228 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 229 | static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) |
| 230 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 231 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 232 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 233 | return readx_poll_timeout(ocelot_vlant_read_vlanaccess, |
| 234 | ocelot, |
| 235 | val, |
| 236 | (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == |
| 237 | ANA_TABLES_VLANACCESS_CMD_IDLE, |
| 238 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 241 | static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) |
| 242 | { |
| 243 | /* Select the VID to configure */ |
| 244 | ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), |
| 245 | ANA_TABLES_VLANTIDX); |
| 246 | /* Set the vlan port members mask and issue a write command */ |
| 247 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | |
| 248 | ANA_TABLES_VLANACCESS_CMD_WRITE, |
| 249 | ANA_TABLES_VLANACCESS); |
| 250 | |
| 251 | return ocelot_vlant_wait_for_completion(ocelot); |
| 252 | } |
| 253 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 254 | static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port) |
| 255 | { |
| 256 | struct ocelot_bridge_vlan *vlan; |
| 257 | int num_untagged = 0; |
| 258 | |
| 259 | list_for_each_entry(vlan, &ocelot->vlans, list) { |
| 260 | if (!(vlan->portmask & BIT(port))) |
| 261 | continue; |
| 262 | |
| 263 | if (vlan->untagged & BIT(port)) |
| 264 | num_untagged++; |
| 265 | } |
| 266 | |
| 267 | return num_untagged; |
| 268 | } |
| 269 | |
| 270 | static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port) |
| 271 | { |
| 272 | struct ocelot_bridge_vlan *vlan; |
| 273 | int num_tagged = 0; |
| 274 | |
| 275 | list_for_each_entry(vlan, &ocelot->vlans, list) { |
| 276 | if (!(vlan->portmask & BIT(port))) |
| 277 | continue; |
| 278 | |
| 279 | if (!(vlan->untagged & BIT(port))) |
| 280 | num_tagged++; |
| 281 | } |
| 282 | |
| 283 | return num_tagged; |
| 284 | } |
| 285 | |
| 286 | /* We use native VLAN when we have to mix egress-tagged VLANs with exactly |
| 287 | * _one_ egress-untagged VLAN (_the_ native VLAN) |
| 288 | */ |
| 289 | static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port) |
| 290 | { |
| 291 | return ocelot_port_num_tagged_vlans(ocelot, port) && |
| 292 | ocelot_port_num_untagged_vlans(ocelot, port) == 1; |
| 293 | } |
| 294 | |
| 295 | static struct ocelot_bridge_vlan * |
| 296 | ocelot_port_find_native_vlan(struct ocelot *ocelot, int port) |
| 297 | { |
| 298 | struct ocelot_bridge_vlan *vlan; |
| 299 | |
| 300 | list_for_each_entry(vlan, &ocelot->vlans, list) |
| 301 | if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port)) |
| 302 | return vlan; |
| 303 | |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable, |
| 308 | * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness |
| 309 | * state of the port. |
| 310 | */ |
| 311 | static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port) |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 312 | { |
| 313 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 314 | enum ocelot_port_tag_config tag_cfg; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 315 | bool uses_native_vlan = false; |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 316 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 317 | if (ocelot_port->vlan_aware) { |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 318 | uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port); |
| 319 | |
| 320 | if (uses_native_vlan) |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 321 | tag_cfg = OCELOT_PORT_TAG_NATIVE; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 322 | else if (ocelot_port_num_untagged_vlans(ocelot, port)) |
| 323 | tag_cfg = OCELOT_PORT_TAG_DISABLED; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 324 | else |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 325 | tag_cfg = OCELOT_PORT_TAG_TRUNK; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 326 | } else { |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 327 | tag_cfg = OCELOT_PORT_TAG_DISABLED; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 328 | } |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 329 | |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 330 | ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg), |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 331 | REW_TAG_CFG_TAG_CFG_M, |
| 332 | REW_TAG_CFG, port); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 333 | |
| 334 | if (uses_native_vlan) { |
| 335 | struct ocelot_bridge_vlan *native_vlan; |
| 336 | |
| 337 | /* Not having a native VLAN is impossible, because |
| 338 | * ocelot_port_num_untagged_vlans has returned 1. |
| 339 | * So there is no use in checking for NULL here. |
| 340 | */ |
| 341 | native_vlan = ocelot_port_find_native_vlan(ocelot, port); |
| 342 | |
| 343 | ocelot_rmw_gix(ocelot, |
| 344 | REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid), |
| 345 | REW_PORT_VLAN_CFG_PORT_VID_M, |
| 346 | REW_PORT_VLAN_CFG, port); |
| 347 | } |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 350 | /* Default vlan to clasify for untagged frames (may be zero) */ |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 351 | static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 352 | const struct ocelot_bridge_vlan *pvid_vlan) |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 353 | { |
| 354 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 355 | u16 pvid = OCELOT_VLAN_UNAWARE_PVID; |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 356 | u32 val = 0; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 357 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 358 | ocelot_port->pvid_vlan = pvid_vlan; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 359 | |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 360 | if (ocelot_port->vlan_aware && pvid_vlan) |
| 361 | pvid = pvid_vlan->vid; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 362 | |
| 363 | ocelot_rmw_gix(ocelot, |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 364 | ANA_PORT_VLAN_CFG_VLAN_VID(pvid), |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 365 | ANA_PORT_VLAN_CFG_VLAN_VID_M, |
| 366 | ANA_PORT_VLAN_CFG, port); |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 367 | |
| 368 | /* If there's no pvid, we should drop not only untagged traffic (which |
| 369 | * happens automatically), but also 802.1p traffic which gets |
| 370 | * classified to VLAN 0, but that is always in our RX filter, so it |
| 371 | * would get accepted were it not for this setting. |
| 372 | */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 373 | if (!pvid_vlan && ocelot_port->vlan_aware) |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 374 | val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 375 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; |
| 376 | |
| 377 | ocelot_rmw_gix(ocelot, val, |
| 378 | ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 379 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, |
| 380 | ANA_PORT_DROP_CFG, port); |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 383 | static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot, |
| 384 | u16 vid) |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 385 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 386 | struct ocelot_bridge_vlan *vlan; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 387 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 388 | list_for_each_entry(vlan, &ocelot->vlans, list) |
| 389 | if (vlan->vid == vid) |
| 390 | return vlan; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 391 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 392 | return NULL; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 393 | } |
| 394 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 395 | static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid, |
| 396 | bool untagged) |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 397 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 398 | struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); |
| 399 | unsigned long portmask; |
| 400 | int err; |
| 401 | |
| 402 | if (vlan) { |
| 403 | portmask = vlan->portmask | BIT(port); |
| 404 | |
| 405 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 406 | if (err) |
| 407 | return err; |
| 408 | |
| 409 | vlan->portmask = portmask; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 410 | /* Bridge VLANs can be overwritten with a different |
| 411 | * egress-tagging setting, so make sure to override an untagged |
| 412 | * with a tagged VID if that's going on. |
| 413 | */ |
| 414 | if (untagged) |
| 415 | vlan->untagged |= BIT(port); |
| 416 | else |
| 417 | vlan->untagged &= ~BIT(port); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 423 | if (!vlan) |
| 424 | return -ENOMEM; |
| 425 | |
| 426 | portmask = BIT(port); |
| 427 | |
| 428 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 429 | if (err) { |
| 430 | kfree(vlan); |
| 431 | return err; |
| 432 | } |
| 433 | |
| 434 | vlan->vid = vid; |
| 435 | vlan->portmask = portmask; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 436 | if (untagged) |
| 437 | vlan->untagged = BIT(port); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 438 | INIT_LIST_HEAD(&vlan->list); |
| 439 | list_add_tail(&vlan->list, &ocelot->vlans); |
| 440 | |
| 441 | return 0; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid) |
| 445 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 446 | struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); |
| 447 | unsigned long portmask; |
| 448 | int err; |
| 449 | |
| 450 | if (!vlan) |
| 451 | return 0; |
| 452 | |
| 453 | portmask = vlan->portmask & ~BIT(port); |
| 454 | |
| 455 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 456 | if (err) |
| 457 | return err; |
| 458 | |
| 459 | vlan->portmask = portmask; |
| 460 | if (vlan->portmask) |
| 461 | return 0; |
| 462 | |
| 463 | list_del(&vlan->list); |
| 464 | kfree(vlan); |
| 465 | |
| 466 | return 0; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 467 | } |
| 468 | |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 469 | int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, |
Vladimir Oltean | 3b95d1b | 2021-08-19 20:40:07 +0300 | [diff] [blame] | 470 | bool vlan_aware, struct netlink_ext_ack *extack) |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 471 | { |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 472 | struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 473 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 474 | struct ocelot_vcap_filter *filter; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 475 | u32 val; |
| 476 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 477 | list_for_each_entry(filter, &block->rules, list) { |
| 478 | if (filter->ingress_port_mask & BIT(port) && |
| 479 | filter->action.vid_replace_ena) { |
Vladimir Oltean | 3b95d1b | 2021-08-19 20:40:07 +0300 | [diff] [blame] | 480 | NL_SET_ERR_MSG_MOD(extack, |
| 481 | "Cannot change VLAN state with vlan modify rules active"); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 482 | return -EBUSY; |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 483 | } |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 484 | } |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 485 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 486 | ocelot_port->vlan_aware = vlan_aware; |
| 487 | |
| 488 | if (vlan_aware) |
| 489 | val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 490 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); |
| 491 | else |
| 492 | val = 0; |
| 493 | ocelot_rmw_gix(ocelot, val, |
| 494 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 495 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, |
| 496 | ANA_PORT_VLAN_CFG, port); |
| 497 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 498 | ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 499 | ocelot_port_manage_port_tag(ocelot, port); |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 500 | |
| 501 | return 0; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 502 | } |
| 503 | EXPORT_SYMBOL(ocelot_port_vlan_filtering); |
| 504 | |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 505 | int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
Vladimir Oltean | 01af940 | 2021-08-19 20:40:06 +0300 | [diff] [blame] | 506 | bool untagged, struct netlink_ext_ack *extack) |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 507 | { |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 508 | if (untagged) { |
| 509 | /* We are adding an egress-tagged VLAN */ |
| 510 | if (ocelot_port_uses_native_vlan(ocelot, port)) { |
| 511 | NL_SET_ERR_MSG_MOD(extack, |
| 512 | "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN"); |
| 513 | return -EBUSY; |
| 514 | } |
| 515 | } else { |
| 516 | /* We are adding an egress-tagged VLAN */ |
| 517 | if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) { |
| 518 | NL_SET_ERR_MSG_MOD(extack, |
| 519 | "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs"); |
| 520 | return -EBUSY; |
| 521 | } |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | EXPORT_SYMBOL(ocelot_vlan_prepare); |
| 527 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 528 | int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
| 529 | bool untagged) |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 530 | { |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 531 | int err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 532 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 533 | err = ocelot_vlan_member_add(ocelot, port, vid, untagged); |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 534 | if (err) |
| 535 | return err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 536 | |
| 537 | /* Default ingress vlan classification */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 538 | if (pvid) |
| 539 | ocelot_port_set_pvid(ocelot, port, |
| 540 | ocelot_bridge_vlan_find(ocelot, vid)); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 541 | |
| 542 | /* Untagged egress vlan clasification */ |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 543 | ocelot_port_manage_port_tag(ocelot, port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 544 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 547 | EXPORT_SYMBOL(ocelot_vlan_add); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 548 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 549 | int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) |
Vladimir Oltean | 9855934 | 2019-11-09 15:02:48 +0200 | [diff] [blame] | 550 | { |
| 551 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 552 | int err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 553 | |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 554 | err = ocelot_vlan_member_del(ocelot, port, vid); |
| 555 | if (err) |
| 556 | return err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 557 | |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 558 | /* Ingress */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 559 | if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid) |
| 560 | ocelot_port_set_pvid(ocelot, port, NULL); |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 561 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 562 | /* Egress */ |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 563 | ocelot_port_manage_port_tag(ocelot, port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 564 | |
| 565 | return 0; |
| 566 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 567 | EXPORT_SYMBOL(ocelot_vlan_del); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 568 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 569 | static void ocelot_vlan_init(struct ocelot *ocelot) |
| 570 | { |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 571 | unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 572 | u16 port, vid; |
| 573 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 574 | /* Clear VLAN table, by default all ports are members of all VLANs */ |
| 575 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, |
| 576 | ANA_TABLES_VLANACCESS); |
| 577 | ocelot_vlant_wait_for_completion(ocelot); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 578 | |
| 579 | /* Configure the port VLAN memberships */ |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 580 | for (vid = 1; vid < VLAN_N_VID; vid++) |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 581 | ocelot_vlant_set_mask(ocelot, vid, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 582 | |
| 583 | /* Because VLAN filtering is enabled, we need VID 0 to get untagged |
| 584 | * traffic. It is added automatically if 8021q module is loaded, but |
| 585 | * we can't rely on it since module may be not loaded. |
| 586 | */ |
Vladimir Oltean | bfbab31 | 2021-10-20 20:58:51 +0300 | [diff] [blame] | 587 | ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 588 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 589 | /* Set vlan ingress filter mask to all ports but the CPU port by |
| 590 | * default. |
| 591 | */ |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 592 | ocelot_write(ocelot, all_ports, ANA_VLANMASK); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 593 | |
| 594 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 595 | ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); |
| 596 | ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); |
| 597 | } |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 600 | static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) |
| 601 | { |
| 602 | return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); |
| 603 | } |
| 604 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 605 | static int ocelot_port_flush(struct ocelot *ocelot, int port) |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 606 | { |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 607 | unsigned int pause_ena; |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 608 | int err, val; |
| 609 | |
| 610 | /* Disable dequeuing from the egress queues */ |
| 611 | ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, |
| 612 | QSYS_PORT_MODE_DEQUEUE_DIS, |
| 613 | QSYS_PORT_MODE, port); |
| 614 | |
| 615 | /* Disable flow control */ |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 616 | ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena); |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 617 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); |
| 618 | |
| 619 | /* Disable priority flow control */ |
| 620 | ocelot_fields_write(ocelot, port, |
| 621 | QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); |
| 622 | |
| 623 | /* Wait at least the time it takes to receive a frame of maximum length |
| 624 | * at the port. |
| 625 | * Worst-case delays for 10 kilobyte jumbo frames are: |
| 626 | * 8 ms on a 10M port |
| 627 | * 800 μs on a 100M port |
| 628 | * 80 μs on a 1G port |
| 629 | * 32 μs on a 2.5G port |
| 630 | */ |
| 631 | usleep_range(8000, 10000); |
| 632 | |
| 633 | /* Disable half duplex backpressure. */ |
| 634 | ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, |
| 635 | SYS_FRONT_PORT_MODE, port); |
| 636 | |
| 637 | /* Flush the queues associated with the port. */ |
| 638 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, |
| 639 | REW_PORT_CFG, port); |
| 640 | |
| 641 | /* Enable dequeuing from the egress queues. */ |
| 642 | ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, |
| 643 | port); |
| 644 | |
| 645 | /* Wait until flushing is complete. */ |
| 646 | err = read_poll_timeout(ocelot_read_eq_avail, val, !val, |
| 647 | 100, 2000000, false, ocelot, port); |
| 648 | |
| 649 | /* Clear flushing again. */ |
| 650 | ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); |
| 651 | |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 652 | /* Re-enable flow control */ |
| 653 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena); |
| 654 | |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 655 | return err; |
| 656 | } |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 657 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 658 | void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port, |
| 659 | unsigned int link_an_mode, |
| 660 | phy_interface_t interface, |
| 661 | unsigned long quirks) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 662 | { |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 663 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 664 | int err; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 665 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 666 | ocelot_port->speed = SPEED_UNKNOWN; |
| 667 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 668 | ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA, |
| 669 | DEV_MAC_ENA_CFG); |
| 670 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 671 | if (ocelot->ops->cut_through_fwd) { |
| 672 | mutex_lock(&ocelot->fwd_domain_lock); |
| 673 | ocelot->ops->cut_through_fwd(ocelot); |
| 674 | mutex_unlock(&ocelot->fwd_domain_lock); |
| 675 | } |
| 676 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 677 | ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); |
| 678 | |
| 679 | err = ocelot_port_flush(ocelot, port); |
| 680 | if (err) |
| 681 | dev_err(ocelot->dev, "failed to flush port %d: %d\n", |
| 682 | port, err); |
| 683 | |
| 684 | /* Put the port in reset. */ |
| 685 | if (interface != PHY_INTERFACE_MODE_QSGMII || |
| 686 | !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP)) |
| 687 | ocelot_port_rmwl(ocelot_port, |
| 688 | DEV_CLOCK_CFG_MAC_TX_RST | |
Wan Jiabing | 74a3bc4 | 2021-10-11 10:27:41 +0800 | [diff] [blame] | 689 | DEV_CLOCK_CFG_MAC_RX_RST, |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 690 | DEV_CLOCK_CFG_MAC_TX_RST | |
Wan Jiabing | 74a3bc4 | 2021-10-11 10:27:41 +0800 | [diff] [blame] | 691 | DEV_CLOCK_CFG_MAC_RX_RST, |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 692 | DEV_CLOCK_CFG); |
| 693 | } |
| 694 | EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down); |
| 695 | |
| 696 | void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port, |
| 697 | struct phy_device *phydev, |
| 698 | unsigned int link_an_mode, |
| 699 | phy_interface_t interface, |
| 700 | int speed, int duplex, |
| 701 | bool tx_pause, bool rx_pause, |
| 702 | unsigned long quirks) |
| 703 | { |
| 704 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 705 | int mac_speed, mode = 0; |
| 706 | u32 mac_fc_cfg; |
| 707 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 708 | ocelot_port->speed = speed; |
| 709 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 710 | /* The MAC might be integrated in systems where the MAC speed is fixed |
| 711 | * and it's the PCS who is performing the rate adaptation, so we have |
| 712 | * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG |
| 713 | * (which is also its default value). |
| 714 | */ |
| 715 | if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) || |
| 716 | speed == SPEED_1000) { |
| 717 | mac_speed = OCELOT_SPEED_1000; |
| 718 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 719 | } else if (speed == SPEED_2500) { |
| 720 | mac_speed = OCELOT_SPEED_2500; |
| 721 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 722 | } else if (speed == SPEED_100) { |
| 723 | mac_speed = OCELOT_SPEED_100; |
| 724 | } else { |
| 725 | mac_speed = OCELOT_SPEED_10; |
| 726 | } |
| 727 | |
| 728 | if (duplex == DUPLEX_FULL) |
| 729 | mode |= DEV_MAC_MODE_CFG_FDX_ENA; |
| 730 | |
| 731 | ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG); |
| 732 | |
| 733 | /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and |
| 734 | * PORT_RST bits in DEV_CLOCK_CFG. |
| 735 | */ |
| 736 | ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed), |
| 737 | DEV_CLOCK_CFG); |
| 738 | |
| 739 | switch (speed) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 740 | case SPEED_10: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 741 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 742 | break; |
| 743 | case SPEED_100: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 744 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 745 | break; |
| 746 | case SPEED_1000: |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 747 | case SPEED_2500: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 748 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 749 | break; |
| 750 | default: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 751 | dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", |
| 752 | port, speed); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 756 | /* Handle RX pause in all cases, with 2500base-X this is used for rate |
| 757 | * adaptation. |
| 758 | */ |
| 759 | mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 760 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 761 | if (tx_pause) |
| 762 | mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | |
| 763 | SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | |
| 764 | SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | |
| 765 | SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 766 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 767 | /* Flow control. Link speed is only used here to evaluate the time |
| 768 | * specification in incoming pause frames. |
| 769 | */ |
| 770 | ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 771 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 772 | ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 773 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 774 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause); |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 775 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 776 | /* Undo the effects of ocelot_phylink_mac_link_down: |
| 777 | * enable MAC module |
| 778 | */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 779 | ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 780 | DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); |
| 781 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 782 | /* If the port supports cut-through forwarding, update the masks before |
| 783 | * enabling forwarding on the port. |
| 784 | */ |
| 785 | if (ocelot->ops->cut_through_fwd) { |
| 786 | mutex_lock(&ocelot->fwd_domain_lock); |
| 787 | ocelot->ops->cut_through_fwd(ocelot); |
| 788 | mutex_unlock(&ocelot->fwd_domain_lock); |
| 789 | } |
| 790 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 791 | /* Core: Enable port for frame transfer */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 792 | ocelot_fields_write(ocelot, port, |
| 793 | QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 794 | } |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 795 | EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 796 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 797 | static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, |
| 798 | struct sk_buff *clone) |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 799 | { |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 800 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 801 | unsigned long flags; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 802 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 803 | spin_lock_irqsave(&ocelot->ts_id_lock, flags); |
| 804 | |
| 805 | if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID || |
| 806 | ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) { |
| 807 | spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); |
| 808 | return -EBUSY; |
| 809 | } |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 810 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 811 | skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; |
Yangbo Lu | c4b364c | 2021-04-27 12:22:00 +0800 | [diff] [blame] | 812 | /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ |
| 813 | OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 814 | |
Vladimir Oltean | c57fe00 | 2021-10-12 14:40:35 +0300 | [diff] [blame] | 815 | ocelot_port->ts_id++; |
| 816 | if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID) |
| 817 | ocelot_port->ts_id = 0; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 818 | |
| 819 | ocelot_port->ptp_skbs_in_flight++; |
| 820 | ocelot->ptp_skbs_in_flight++; |
| 821 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 822 | skb_queue_tail(&ocelot_port->tx_skbs, clone); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 823 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 824 | spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); |
| 825 | |
| 826 | return 0; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 827 | } |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 828 | |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 829 | static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb, |
| 830 | unsigned int ptp_class) |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 831 | { |
| 832 | struct ptp_header *hdr; |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 833 | u8 msgtype, twostep; |
| 834 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 835 | hdr = ptp_parse_header(skb, ptp_class); |
| 836 | if (!hdr) |
| 837 | return false; |
| 838 | |
| 839 | msgtype = ptp_get_msgtype(hdr, ptp_class); |
| 840 | twostep = hdr->flag_field[0] & 0x2; |
| 841 | |
| 842 | if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) |
| 843 | return true; |
| 844 | |
| 845 | return false; |
| 846 | } |
| 847 | |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 848 | int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port, |
| 849 | struct sk_buff *skb, |
| 850 | struct sk_buff **clone) |
| 851 | { |
| 852 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 853 | u8 ptp_cmd = ocelot_port->ptp_cmd; |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 854 | unsigned int ptp_class; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 855 | int err; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 856 | |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 857 | /* Don't do anything if PTP timestamping not enabled */ |
| 858 | if (!ptp_cmd) |
| 859 | return 0; |
| 860 | |
| 861 | ptp_class = ptp_classify_raw(skb); |
| 862 | if (ptp_class == PTP_CLASS_NONE) |
| 863 | return -EINVAL; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 864 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 865 | /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ |
| 866 | if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 867 | if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) { |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 868 | OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | /* Fall back to two-step timestamping */ |
| 873 | ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
| 874 | } |
| 875 | |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 876 | if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { |
| 877 | *clone = skb_clone_sk(skb); |
| 878 | if (!(*clone)) |
| 879 | return -ENOMEM; |
| 880 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 881 | err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone); |
| 882 | if (err) |
| 883 | return err; |
| 884 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 885 | OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 886 | OCELOT_SKB_CB(*clone)->ptp_class = ptp_class; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | EXPORT_SYMBOL(ocelot_port_txtstamp_request); |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 892 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 893 | static void ocelot_get_hwtimestamp(struct ocelot *ocelot, |
| 894 | struct timespec64 *ts) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 895 | { |
| 896 | unsigned long flags; |
| 897 | u32 val; |
| 898 | |
| 899 | spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); |
| 900 | |
| 901 | /* Read current PTP time to get seconds */ |
| 902 | val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); |
| 903 | |
| 904 | val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); |
| 905 | val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); |
| 906 | ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); |
| 907 | ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); |
| 908 | |
| 909 | /* Read packet HW timestamp from FIFO */ |
| 910 | val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); |
| 911 | ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); |
| 912 | |
| 913 | /* Sec has incremented since the ts was registered */ |
| 914 | if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) |
| 915 | ts->tv_sec--; |
| 916 | |
| 917 | spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); |
| 918 | } |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 919 | |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 920 | static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid) |
| 921 | { |
| 922 | struct ptp_header *hdr; |
| 923 | |
| 924 | hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class); |
| 925 | if (WARN_ON(!hdr)) |
| 926 | return false; |
| 927 | |
| 928 | return seqid == ntohs(hdr->sequence_id); |
| 929 | } |
| 930 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 931 | void ocelot_get_txtstamp(struct ocelot *ocelot) |
| 932 | { |
| 933 | int budget = OCELOT_PTP_QUEUE_SZ; |
| 934 | |
| 935 | while (budget--) { |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 936 | struct sk_buff *skb, *skb_tmp, *skb_match = NULL; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 937 | struct skb_shared_hwtstamps shhwtstamps; |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 938 | u32 val, id, seqid, txport; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 939 | struct ocelot_port *port; |
| 940 | struct timespec64 ts; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 941 | unsigned long flags; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 942 | |
| 943 | val = ocelot_read(ocelot, SYS_PTP_STATUS); |
| 944 | |
| 945 | /* Check if a timestamp can be retrieved */ |
| 946 | if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) |
| 947 | break; |
| 948 | |
| 949 | WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); |
| 950 | |
| 951 | /* Retrieve the ts ID and Tx port */ |
| 952 | id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); |
| 953 | txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 954 | seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 955 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 956 | port = ocelot->ports[txport]; |
| 957 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 958 | spin_lock(&ocelot->ts_id_lock); |
| 959 | port->ptp_skbs_in_flight--; |
| 960 | ocelot->ptp_skbs_in_flight--; |
| 961 | spin_unlock(&ocelot->ts_id_lock); |
| 962 | |
| 963 | /* Retrieve its associated skb */ |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 964 | try_again: |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 965 | spin_lock_irqsave(&port->tx_skbs.lock, flags); |
| 966 | |
| 967 | skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { |
Yangbo Lu | c4b364c | 2021-04-27 12:22:00 +0800 | [diff] [blame] | 968 | if (OCELOT_SKB_CB(skb)->ts_id != id) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 969 | continue; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 970 | __skb_unlink(skb, &port->tx_skbs); |
| 971 | skb_match = skb; |
Yangbo Lu | fc62c09 | 2019-11-27 15:27:56 +0800 | [diff] [blame] | 972 | break; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 975 | spin_unlock_irqrestore(&port->tx_skbs.lock, flags); |
| 976 | |
Vladimir Oltean | 9fde506 | 2021-10-12 14:40:37 +0300 | [diff] [blame] | 977 | if (WARN_ON(!skb_match)) |
| 978 | continue; |
| 979 | |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 980 | if (!ocelot_validate_ptp_skb(skb_match, seqid)) { |
| 981 | dev_err_ratelimited(ocelot->dev, |
| 982 | "port %d received stale TX timestamp for seqid %d, discarding\n", |
| 983 | txport, seqid); |
| 984 | dev_kfree_skb_any(skb); |
| 985 | goto try_again; |
| 986 | } |
| 987 | |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 988 | /* Get the h/w timestamp */ |
| 989 | ocelot_get_hwtimestamp(ocelot, &ts); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 990 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 991 | /* Set the timestamp into the skb */ |
| 992 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 993 | shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 994 | skb_complete_tx_timestamp(skb_match, &shhwtstamps); |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 995 | |
| 996 | /* Next ts */ |
| 997 | ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | EXPORT_SYMBOL(ocelot_get_txtstamp); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1001 | |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1002 | static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, |
| 1003 | u32 *rval) |
| 1004 | { |
| 1005 | u32 bytes_valid, val; |
| 1006 | |
| 1007 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1008 | if (val == XTR_NOT_READY) { |
| 1009 | if (ifh) |
| 1010 | return -EIO; |
| 1011 | |
| 1012 | do { |
| 1013 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1014 | } while (val == XTR_NOT_READY); |
| 1015 | } |
| 1016 | |
| 1017 | switch (val) { |
| 1018 | case XTR_ABORT: |
| 1019 | return -EIO; |
| 1020 | case XTR_EOF_0: |
| 1021 | case XTR_EOF_1: |
| 1022 | case XTR_EOF_2: |
| 1023 | case XTR_EOF_3: |
| 1024 | case XTR_PRUNED: |
| 1025 | bytes_valid = XTR_VALID_BYTES(val); |
| 1026 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1027 | if (val == XTR_ESCAPE) |
| 1028 | *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1029 | else |
| 1030 | *rval = val; |
| 1031 | |
| 1032 | return bytes_valid; |
| 1033 | case XTR_ESCAPE: |
| 1034 | *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1035 | |
| 1036 | return 4; |
| 1037 | default: |
| 1038 | *rval = val; |
| 1039 | |
| 1040 | return 4; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) |
| 1045 | { |
| 1046 | int i, err = 0; |
| 1047 | |
| 1048 | for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { |
| 1049 | err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); |
| 1050 | if (err != 4) |
| 1051 | return (err < 0) ? err : -EIO; |
| 1052 | } |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
Clément Léger | b471a71 | 2021-12-09 16:49:09 +0100 | [diff] [blame] | 1057 | void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb, |
| 1058 | u64 timestamp) |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1059 | { |
| 1060 | struct skb_shared_hwtstamps *shhwtstamps; |
Horatiu Vultur | 2ed2c5f | 2021-03-16 21:10:19 +0100 | [diff] [blame] | 1061 | u64 tod_in_ns, full_ts_in_ns; |
Clément Léger | b471a71 | 2021-12-09 16:49:09 +0100 | [diff] [blame] | 1062 | struct timespec64 ts; |
| 1063 | |
| 1064 | ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); |
| 1065 | |
| 1066 | tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); |
| 1067 | if ((tod_in_ns & 0xffffffff) < timestamp) |
| 1068 | full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | |
| 1069 | timestamp; |
| 1070 | else |
| 1071 | full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | |
| 1072 | timestamp; |
| 1073 | |
| 1074 | shhwtstamps = skb_hwtstamps(skb); |
| 1075 | memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); |
| 1076 | shhwtstamps->hwtstamp = full_ts_in_ns; |
| 1077 | } |
| 1078 | EXPORT_SYMBOL(ocelot_ptp_rx_timestamp); |
| 1079 | |
| 1080 | int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) |
| 1081 | { |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1082 | u64 timestamp, src_port, len; |
| 1083 | u32 xfh[OCELOT_TAG_LEN / 4]; |
| 1084 | struct net_device *dev; |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1085 | struct sk_buff *skb; |
| 1086 | int sz, buf_len; |
| 1087 | u32 val, *buf; |
| 1088 | int err; |
| 1089 | |
| 1090 | err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); |
| 1091 | if (err) |
| 1092 | return err; |
| 1093 | |
| 1094 | ocelot_xfh_get_src_port(xfh, &src_port); |
| 1095 | ocelot_xfh_get_len(xfh, &len); |
| 1096 | ocelot_xfh_get_rew_val(xfh, ×tamp); |
| 1097 | |
| 1098 | if (WARN_ON(src_port >= ocelot->num_phys_ports)) |
| 1099 | return -EINVAL; |
| 1100 | |
| 1101 | dev = ocelot->ops->port_to_netdev(ocelot, src_port); |
| 1102 | if (!dev) |
| 1103 | return -EINVAL; |
| 1104 | |
| 1105 | skb = netdev_alloc_skb(dev, len); |
| 1106 | if (unlikely(!skb)) { |
| 1107 | netdev_err(dev, "Unable to allocate sk_buff\n"); |
| 1108 | return -ENOMEM; |
| 1109 | } |
| 1110 | |
| 1111 | buf_len = len - ETH_FCS_LEN; |
| 1112 | buf = (u32 *)skb_put(skb, buf_len); |
| 1113 | |
| 1114 | len = 0; |
| 1115 | do { |
| 1116 | sz = ocelot_rx_frame_word(ocelot, grp, false, &val); |
| 1117 | if (sz < 0) { |
| 1118 | err = sz; |
| 1119 | goto out_free_skb; |
| 1120 | } |
| 1121 | *buf++ = val; |
| 1122 | len += sz; |
| 1123 | } while (len < buf_len); |
| 1124 | |
| 1125 | /* Read the FCS */ |
| 1126 | sz = ocelot_rx_frame_word(ocelot, grp, false, &val); |
| 1127 | if (sz < 0) { |
| 1128 | err = sz; |
| 1129 | goto out_free_skb; |
| 1130 | } |
| 1131 | |
| 1132 | /* Update the statistics if part of the FCS was read before */ |
| 1133 | len -= ETH_FCS_LEN - sz; |
| 1134 | |
| 1135 | if (unlikely(dev->features & NETIF_F_RXFCS)) { |
| 1136 | buf = (u32 *)skb_put(skb, ETH_FCS_LEN); |
| 1137 | *buf = val; |
| 1138 | } |
| 1139 | |
Clément Léger | b471a71 | 2021-12-09 16:49:09 +0100 | [diff] [blame] | 1140 | if (ocelot->ptp) |
| 1141 | ocelot_ptp_rx_timestamp(ocelot, skb, timestamp); |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1142 | |
| 1143 | /* Everything we see on an interface that is in the HW bridge |
| 1144 | * has already been forwarded. |
| 1145 | */ |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1146 | if (ocelot->ports[src_port]->bridge) |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1147 | skb->offload_fwd_mark = 1; |
| 1148 | |
| 1149 | skb->protocol = eth_type_trans(skb, dev); |
Horatiu Vultur | d8ea7ff | 2021-02-16 22:42:03 +0100 | [diff] [blame] | 1150 | |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1151 | *nskb = skb; |
| 1152 | |
| 1153 | return 0; |
| 1154 | |
| 1155 | out_free_skb: |
| 1156 | kfree_skb(skb); |
| 1157 | return err; |
| 1158 | } |
| 1159 | EXPORT_SYMBOL(ocelot_xtr_poll_frame); |
| 1160 | |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1161 | bool ocelot_can_inject(struct ocelot *ocelot, int grp) |
| 1162 | { |
| 1163 | u32 val = ocelot_read(ocelot, QS_INJ_STATUS); |
| 1164 | |
| 1165 | if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) |
| 1166 | return false; |
| 1167 | if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) |
| 1168 | return false; |
| 1169 | |
| 1170 | return true; |
| 1171 | } |
| 1172 | EXPORT_SYMBOL(ocelot_can_inject); |
| 1173 | |
Clément Léger | e5150f00 | 2021-12-09 16:49:08 +0100 | [diff] [blame] | 1174 | void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag) |
| 1175 | { |
| 1176 | ocelot_ifh_set_bypass(ifh, 1); |
| 1177 | ocelot_ifh_set_dest(ifh, BIT_ULL(port)); |
| 1178 | ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); |
| 1179 | if (vlan_tag) |
| 1180 | ocelot_ifh_set_vlan_tci(ifh, vlan_tag); |
| 1181 | if (rew_op) |
| 1182 | ocelot_ifh_set_rew_op(ifh, rew_op); |
| 1183 | } |
| 1184 | EXPORT_SYMBOL(ocelot_ifh_port_set); |
| 1185 | |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1186 | void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, |
| 1187 | u32 rew_op, struct sk_buff *skb) |
| 1188 | { |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1189 | u32 ifh[OCELOT_TAG_LEN / 4] = {0}; |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1190 | unsigned int i, count, last; |
| 1191 | |
| 1192 | ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | |
| 1193 | QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); |
| 1194 | |
Clément Léger | e5150f00 | 2021-12-09 16:49:08 +0100 | [diff] [blame] | 1195 | ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb)); |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1196 | |
| 1197 | for (i = 0; i < OCELOT_TAG_LEN / 4; i++) |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1198 | ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1199 | |
| 1200 | count = DIV_ROUND_UP(skb->len, 4); |
| 1201 | last = skb->len % 4; |
| 1202 | for (i = 0; i < count; i++) |
| 1203 | ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); |
| 1204 | |
| 1205 | /* Add padding */ |
| 1206 | while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { |
| 1207 | ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); |
| 1208 | i++; |
| 1209 | } |
| 1210 | |
| 1211 | /* Indicate EOF and valid bytes in last word */ |
| 1212 | ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | |
| 1213 | QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | |
| 1214 | QS_INJ_CTRL_EOF, |
| 1215 | QS_INJ_CTRL, grp); |
| 1216 | |
| 1217 | /* Add dummy CRC */ |
| 1218 | ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); |
| 1219 | skb_tx_timestamp(skb); |
| 1220 | |
| 1221 | skb->dev->stats.tx_packets++; |
| 1222 | skb->dev->stats.tx_bytes += skb->len; |
| 1223 | } |
| 1224 | EXPORT_SYMBOL(ocelot_port_inject_frame); |
| 1225 | |
Vladimir Oltean | 0a6f17c | 2021-02-14 00:38:01 +0200 | [diff] [blame] | 1226 | void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) |
| 1227 | { |
| 1228 | while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) |
| 1229 | ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1230 | } |
| 1231 | EXPORT_SYMBOL(ocelot_drain_cpu_queue); |
| 1232 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1233 | int ocelot_fdb_add(struct ocelot *ocelot, int port, |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 1234 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1235 | { |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1236 | int pgid = port; |
| 1237 | |
| 1238 | if (port == ocelot->npi) |
| 1239 | pgid = PGID_CPU; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1240 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1241 | return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1242 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1243 | EXPORT_SYMBOL(ocelot_fdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1244 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1245 | int ocelot_fdb_del(struct ocelot *ocelot, int port, |
| 1246 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1247 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1248 | return ocelot_mact_forget(ocelot, addr, vid); |
| 1249 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1250 | EXPORT_SYMBOL(ocelot_fdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1251 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1252 | int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 1253 | bool is_static, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1254 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1255 | struct ocelot_dump_ctx *dump = data; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1256 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 1257 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 1258 | struct nlmsghdr *nlh; |
| 1259 | struct ndmsg *ndm; |
| 1260 | |
| 1261 | if (dump->idx < dump->cb->args[2]) |
| 1262 | goto skip; |
| 1263 | |
| 1264 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 1265 | sizeof(*ndm), NLM_F_MULTI); |
| 1266 | if (!nlh) |
| 1267 | return -EMSGSIZE; |
| 1268 | |
| 1269 | ndm = nlmsg_data(nlh); |
| 1270 | ndm->ndm_family = AF_BRIDGE; |
| 1271 | ndm->ndm_pad1 = 0; |
| 1272 | ndm->ndm_pad2 = 0; |
| 1273 | ndm->ndm_flags = NTF_SELF; |
| 1274 | ndm->ndm_type = 0; |
| 1275 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1276 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1277 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1278 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1279 | goto nla_put_failure; |
| 1280 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1281 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1282 | goto nla_put_failure; |
| 1283 | |
| 1284 | nlmsg_end(dump->skb, nlh); |
| 1285 | |
| 1286 | skip: |
| 1287 | dump->idx++; |
| 1288 | return 0; |
| 1289 | |
| 1290 | nla_put_failure: |
| 1291 | nlmsg_cancel(dump->skb, nlh); |
| 1292 | return -EMSGSIZE; |
| 1293 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1294 | EXPORT_SYMBOL(ocelot_port_fdb_do_dump); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1295 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1296 | /* Caller must hold &ocelot->mact_lock */ |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1297 | static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, |
| 1298 | struct ocelot_mact_entry *entry) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1299 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1300 | u32 val, dst, macl, mach; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1301 | char mac[ETH_ALEN]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1302 | |
| 1303 | /* Set row and column to read from */ |
| 1304 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); |
| 1305 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); |
| 1306 | |
| 1307 | /* Issue a read command */ |
| 1308 | ocelot_write(ocelot, |
| 1309 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 1310 | ANA_TABLES_MACACCESS); |
| 1311 | |
| 1312 | if (ocelot_mact_wait_for_completion(ocelot)) |
| 1313 | return -ETIMEDOUT; |
| 1314 | |
| 1315 | /* Read the entry flags */ |
| 1316 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 1317 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 1318 | return -EINVAL; |
| 1319 | |
| 1320 | /* If the entry read has another port configured as its destination, |
| 1321 | * do not report it. |
| 1322 | */ |
| 1323 | dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1324 | if (dst != port) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1325 | return -EINVAL; |
| 1326 | |
| 1327 | /* Get the entry's MAC address and VLAN id */ |
| 1328 | macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); |
| 1329 | mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); |
| 1330 | |
| 1331 | mac[0] = (mach >> 8) & 0xff; |
| 1332 | mac[1] = (mach >> 0) & 0xff; |
| 1333 | mac[2] = (macl >> 24) & 0xff; |
| 1334 | mac[3] = (macl >> 16) & 0xff; |
| 1335 | mac[4] = (macl >> 8) & 0xff; |
| 1336 | mac[5] = (macl >> 0) & 0xff; |
| 1337 | |
| 1338 | entry->vid = (mach >> 16) & 0xfff; |
| 1339 | ether_addr_copy(entry->mac, mac); |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Vladimir Oltean | 5cad43a | 2022-01-07 16:42:29 +0200 | [diff] [blame] | 1344 | int ocelot_mact_flush(struct ocelot *ocelot, int port) |
| 1345 | { |
| 1346 | int err; |
| 1347 | |
| 1348 | mutex_lock(&ocelot->mact_lock); |
| 1349 | |
| 1350 | /* Program ageing filter for a single port */ |
| 1351 | ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port), |
| 1352 | ANA_ANAGEFIL); |
| 1353 | |
| 1354 | /* Flushing dynamic FDB entries requires two successive age scans */ |
| 1355 | ocelot_write(ocelot, |
| 1356 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE), |
| 1357 | ANA_TABLES_MACACCESS); |
| 1358 | |
| 1359 | err = ocelot_mact_wait_for_completion(ocelot); |
| 1360 | if (err) { |
| 1361 | mutex_unlock(&ocelot->mact_lock); |
| 1362 | return err; |
| 1363 | } |
| 1364 | |
| 1365 | /* And second... */ |
| 1366 | ocelot_write(ocelot, |
| 1367 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE), |
| 1368 | ANA_TABLES_MACACCESS); |
| 1369 | |
| 1370 | err = ocelot_mact_wait_for_completion(ocelot); |
| 1371 | |
| 1372 | /* Restore ageing filter */ |
| 1373 | ocelot_write(ocelot, 0, ANA_ANAGEFIL); |
| 1374 | |
| 1375 | mutex_unlock(&ocelot->mact_lock); |
| 1376 | |
| 1377 | return err; |
| 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(ocelot_mact_flush); |
| 1380 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1381 | int ocelot_fdb_dump(struct ocelot *ocelot, int port, |
| 1382 | dsa_fdb_dump_cb_t *cb, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1383 | { |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1384 | int err = 0; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1385 | int i, j; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1386 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1387 | /* We could take the lock just around ocelot_mact_read, but doing so |
| 1388 | * thousands of times in a row seems rather pointless and inefficient. |
| 1389 | */ |
| 1390 | mutex_lock(&ocelot->mact_lock); |
| 1391 | |
Vladimir Oltean | 21ce7f3 | 2020-05-04 01:20:26 +0300 | [diff] [blame] | 1392 | /* Loop through all the mac tables entries. */ |
| 1393 | for (i = 0; i < ocelot->num_mact_rows; i++) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1394 | for (j = 0; j < 4; j++) { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1395 | struct ocelot_mact_entry entry; |
| 1396 | bool is_static; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1397 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1398 | err = ocelot_mact_read(ocelot, port, i, j, &entry); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1399 | /* If the entry is invalid (wrong port, invalid...), |
| 1400 | * skip it. |
| 1401 | */ |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1402 | if (err == -EINVAL) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1403 | continue; |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1404 | else if (err) |
| 1405 | break; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1406 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1407 | is_static = (entry.type == ENTRYTYPE_LOCKED); |
| 1408 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1409 | err = cb(entry.mac, entry.vid, is_static, data); |
| 1410 | if (err) |
| 1411 | break; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1415 | mutex_unlock(&ocelot->mact_lock); |
| 1416 | |
| 1417 | return err; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1418 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1419 | EXPORT_SYMBOL(ocelot_fdb_dump); |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1420 | |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1421 | static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap) |
| 1422 | { |
| 1423 | trap->key_type = OCELOT_VCAP_KEY_ETYPE; |
| 1424 | *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588); |
| 1425 | *(__be16 *)trap->key.etype.etype.mask = htons(0xffff); |
| 1426 | } |
| 1427 | |
| 1428 | static void |
| 1429 | ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap) |
| 1430 | { |
| 1431 | trap->key_type = OCELOT_VCAP_KEY_IPV4; |
| 1432 | trap->key.ipv4.dport.value = PTP_EV_PORT; |
| 1433 | trap->key.ipv4.dport.mask = 0xffff; |
| 1434 | } |
| 1435 | |
| 1436 | static void |
| 1437 | ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap) |
| 1438 | { |
| 1439 | trap->key_type = OCELOT_VCAP_KEY_IPV6; |
| 1440 | trap->key.ipv6.dport.value = PTP_EV_PORT; |
| 1441 | trap->key.ipv6.dport.mask = 0xffff; |
| 1442 | } |
| 1443 | |
| 1444 | static void |
| 1445 | ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap) |
| 1446 | { |
| 1447 | trap->key_type = OCELOT_VCAP_KEY_IPV4; |
| 1448 | trap->key.ipv4.dport.value = PTP_GEN_PORT; |
| 1449 | trap->key.ipv4.dport.mask = 0xffff; |
| 1450 | } |
| 1451 | |
| 1452 | static void |
| 1453 | ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap) |
| 1454 | { |
| 1455 | trap->key_type = OCELOT_VCAP_KEY_IPV6; |
| 1456 | trap->key.ipv6.dport.value = PTP_GEN_PORT; |
| 1457 | trap->key.ipv6.dport.mask = 0xffff; |
| 1458 | } |
| 1459 | |
| 1460 | static int ocelot_trap_add(struct ocelot *ocelot, int port, |
| 1461 | unsigned long cookie, |
| 1462 | void (*populate)(struct ocelot_vcap_filter *f)) |
| 1463 | { |
| 1464 | struct ocelot_vcap_block *block_vcap_is2; |
| 1465 | struct ocelot_vcap_filter *trap; |
| 1466 | bool new = false; |
| 1467 | int err; |
| 1468 | |
| 1469 | block_vcap_is2 = &ocelot->block[VCAP_IS2]; |
| 1470 | |
| 1471 | trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, |
| 1472 | false); |
| 1473 | if (!trap) { |
| 1474 | trap = kzalloc(sizeof(*trap), GFP_KERNEL); |
| 1475 | if (!trap) |
| 1476 | return -ENOMEM; |
| 1477 | |
| 1478 | populate(trap); |
| 1479 | trap->prio = 1; |
| 1480 | trap->id.cookie = cookie; |
| 1481 | trap->id.tc_offload = false; |
| 1482 | trap->block_id = VCAP_IS2; |
| 1483 | trap->type = OCELOT_VCAP_FILTER_OFFLOAD; |
| 1484 | trap->lookup = 0; |
| 1485 | trap->action.cpu_copy_ena = true; |
| 1486 | trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; |
| 1487 | trap->action.port_mask = 0; |
| 1488 | new = true; |
| 1489 | } |
| 1490 | |
| 1491 | trap->ingress_port_mask |= BIT(port); |
| 1492 | |
| 1493 | if (new) |
| 1494 | err = ocelot_vcap_filter_add(ocelot, trap, NULL); |
| 1495 | else |
| 1496 | err = ocelot_vcap_filter_replace(ocelot, trap); |
| 1497 | if (err) { |
| 1498 | trap->ingress_port_mask &= ~BIT(port); |
| 1499 | if (!trap->ingress_port_mask) |
| 1500 | kfree(trap); |
| 1501 | return err; |
| 1502 | } |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static int ocelot_trap_del(struct ocelot *ocelot, int port, |
| 1508 | unsigned long cookie) |
| 1509 | { |
| 1510 | struct ocelot_vcap_block *block_vcap_is2; |
| 1511 | struct ocelot_vcap_filter *trap; |
| 1512 | |
| 1513 | block_vcap_is2 = &ocelot->block[VCAP_IS2]; |
| 1514 | |
| 1515 | trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie, |
| 1516 | false); |
| 1517 | if (!trap) |
| 1518 | return 0; |
| 1519 | |
| 1520 | trap->ingress_port_mask &= ~BIT(port); |
| 1521 | if (!trap->ingress_port_mask) |
| 1522 | return ocelot_vcap_filter_del(ocelot, trap); |
| 1523 | |
| 1524 | return ocelot_vcap_filter_replace(ocelot, trap); |
| 1525 | } |
| 1526 | |
| 1527 | static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port) |
| 1528 | { |
| 1529 | unsigned long l2_cookie = ocelot->num_phys_ports + 1; |
| 1530 | |
| 1531 | return ocelot_trap_add(ocelot, port, l2_cookie, |
| 1532 | ocelot_populate_l2_ptp_trap_key); |
| 1533 | } |
| 1534 | |
| 1535 | static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port) |
| 1536 | { |
| 1537 | unsigned long l2_cookie = ocelot->num_phys_ports + 1; |
| 1538 | |
| 1539 | return ocelot_trap_del(ocelot, port, l2_cookie); |
| 1540 | } |
| 1541 | |
| 1542 | static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port) |
| 1543 | { |
| 1544 | unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2; |
| 1545 | unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3; |
| 1546 | int err; |
| 1547 | |
| 1548 | err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, |
| 1549 | ocelot_populate_ipv4_ptp_event_trap_key); |
| 1550 | if (err) |
| 1551 | return err; |
| 1552 | |
| 1553 | err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, |
| 1554 | ocelot_populate_ipv4_ptp_general_trap_key); |
| 1555 | if (err) |
| 1556 | ocelot_trap_del(ocelot, port, ipv4_ev_cookie); |
| 1557 | |
| 1558 | return err; |
| 1559 | } |
| 1560 | |
| 1561 | static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port) |
| 1562 | { |
| 1563 | unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2; |
| 1564 | unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3; |
| 1565 | int err; |
| 1566 | |
| 1567 | err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie); |
| 1568 | err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie); |
| 1569 | return err; |
| 1570 | } |
| 1571 | |
| 1572 | static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port) |
| 1573 | { |
| 1574 | unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4; |
| 1575 | unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5; |
| 1576 | int err; |
| 1577 | |
| 1578 | err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, |
| 1579 | ocelot_populate_ipv6_ptp_event_trap_key); |
| 1580 | if (err) |
| 1581 | return err; |
| 1582 | |
| 1583 | err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, |
| 1584 | ocelot_populate_ipv6_ptp_general_trap_key); |
| 1585 | if (err) |
| 1586 | ocelot_trap_del(ocelot, port, ipv6_ev_cookie); |
| 1587 | |
| 1588 | return err; |
| 1589 | } |
| 1590 | |
| 1591 | static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port) |
| 1592 | { |
| 1593 | unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4; |
| 1594 | unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5; |
| 1595 | int err; |
| 1596 | |
| 1597 | err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie); |
| 1598 | err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie); |
| 1599 | return err; |
| 1600 | } |
| 1601 | |
| 1602 | static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port, |
| 1603 | bool l2, bool l4) |
| 1604 | { |
| 1605 | int err; |
| 1606 | |
| 1607 | if (l2) |
| 1608 | err = ocelot_l2_ptp_trap_add(ocelot, port); |
| 1609 | else |
| 1610 | err = ocelot_l2_ptp_trap_del(ocelot, port); |
| 1611 | if (err) |
| 1612 | return err; |
| 1613 | |
| 1614 | if (l4) { |
| 1615 | err = ocelot_ipv4_ptp_trap_add(ocelot, port); |
| 1616 | if (err) |
| 1617 | goto err_ipv4; |
| 1618 | |
| 1619 | err = ocelot_ipv6_ptp_trap_add(ocelot, port); |
| 1620 | if (err) |
| 1621 | goto err_ipv6; |
| 1622 | } else { |
| 1623 | err = ocelot_ipv4_ptp_trap_del(ocelot, port); |
| 1624 | |
| 1625 | err |= ocelot_ipv6_ptp_trap_del(ocelot, port); |
| 1626 | } |
| 1627 | if (err) |
| 1628 | return err; |
| 1629 | |
| 1630 | return 0; |
| 1631 | |
| 1632 | err_ipv6: |
| 1633 | ocelot_ipv4_ptp_trap_del(ocelot, port); |
| 1634 | err_ipv4: |
| 1635 | if (l2) |
| 1636 | ocelot_l2_ptp_trap_del(ocelot, port); |
| 1637 | return err; |
| 1638 | } |
| 1639 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1640 | int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1641 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1642 | return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, |
| 1643 | sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; |
| 1644 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1645 | EXPORT_SYMBOL(ocelot_hwstamp_get); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1646 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1647 | int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1648 | { |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1649 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1650 | bool l2 = false, l4 = false; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1651 | struct hwtstamp_config cfg; |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1652 | int err; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1653 | |
| 1654 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 1655 | return -EFAULT; |
| 1656 | |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1657 | /* Tx type sanity check */ |
| 1658 | switch (cfg.tx_type) { |
| 1659 | case HWTSTAMP_TX_ON: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1660 | ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1661 | break; |
| 1662 | case HWTSTAMP_TX_ONESTEP_SYNC: |
| 1663 | /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we |
| 1664 | * need to update the origin time. |
| 1665 | */ |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1666 | ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1667 | break; |
| 1668 | case HWTSTAMP_TX_OFF: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1669 | ocelot_port->ptp_cmd = 0; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1670 | break; |
| 1671 | default: |
| 1672 | return -ERANGE; |
| 1673 | } |
| 1674 | |
| 1675 | mutex_lock(&ocelot->ptp_lock); |
| 1676 | |
| 1677 | switch (cfg.rx_filter) { |
| 1678 | case HWTSTAMP_FILTER_NONE: |
| 1679 | break; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1680 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 1681 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 1682 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1683 | l4 = true; |
| 1684 | break; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1685 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1686 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 1687 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1688 | l2 = true; |
| 1689 | break; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1690 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 1691 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 1692 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1693 | l2 = true; |
| 1694 | l4 = true; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1695 | break; |
| 1696 | default: |
| 1697 | mutex_unlock(&ocelot->ptp_lock); |
| 1698 | return -ERANGE; |
| 1699 | } |
| 1700 | |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1701 | err = ocelot_setup_ptp_traps(ocelot, port, l2, l4); |
Lv Ruyi | 9c32950 | 2021-11-30 11:24:43 +0000 | [diff] [blame] | 1702 | if (err) { |
| 1703 | mutex_unlock(&ocelot->ptp_lock); |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1704 | return err; |
Lv Ruyi | 9c32950 | 2021-11-30 11:24:43 +0000 | [diff] [blame] | 1705 | } |
Vladimir Oltean | 96ca08c | 2021-11-26 19:28:44 +0200 | [diff] [blame] | 1706 | |
| 1707 | if (l2 && l4) |
| 1708 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 1709 | else if (l2) |
| 1710 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; |
| 1711 | else if (l4) |
| 1712 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; |
| 1713 | else |
| 1714 | cfg.rx_filter = HWTSTAMP_FILTER_NONE; |
| 1715 | |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1716 | /* Commit back the result & save it */ |
| 1717 | memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); |
| 1718 | mutex_unlock(&ocelot->ptp_lock); |
| 1719 | |
| 1720 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 1721 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1722 | EXPORT_SYMBOL(ocelot_hwstamp_set); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1723 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1724 | void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1725 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1726 | int i; |
| 1727 | |
| 1728 | if (sset != ETH_SS_STATS) |
| 1729 | return; |
| 1730 | |
| 1731 | for (i = 0; i < ocelot->num_stats; i++) |
| 1732 | memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, |
| 1733 | ETH_GSTRING_LEN); |
| 1734 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1735 | EXPORT_SYMBOL(ocelot_get_strings); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1736 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1737 | static void ocelot_update_stats(struct ocelot *ocelot) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1738 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1739 | int i, j; |
| 1740 | |
| 1741 | mutex_lock(&ocelot->stats_lock); |
| 1742 | |
| 1743 | for (i = 0; i < ocelot->num_phys_ports; i++) { |
| 1744 | /* Configure the port to read the stats from */ |
| 1745 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); |
| 1746 | |
| 1747 | for (j = 0; j < ocelot->num_stats; j++) { |
| 1748 | u32 val; |
| 1749 | unsigned int idx = i * ocelot->num_stats + j; |
| 1750 | |
| 1751 | val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, |
| 1752 | ocelot->stats_layout[j].offset); |
| 1753 | |
| 1754 | if (val < (ocelot->stats[idx] & U32_MAX)) |
| 1755 | ocelot->stats[idx] += (u64)1 << 32; |
| 1756 | |
| 1757 | ocelot->stats[idx] = (ocelot->stats[idx] & |
| 1758 | ~(u64)U32_MAX) + val; |
| 1759 | } |
| 1760 | } |
| 1761 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1762 | mutex_unlock(&ocelot->stats_lock); |
| 1763 | } |
| 1764 | |
| 1765 | static void ocelot_check_stats_work(struct work_struct *work) |
| 1766 | { |
| 1767 | struct delayed_work *del_work = to_delayed_work(work); |
| 1768 | struct ocelot *ocelot = container_of(del_work, struct ocelot, |
| 1769 | stats_work); |
| 1770 | |
| 1771 | ocelot_update_stats(ocelot); |
| 1772 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1773 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 1774 | OCELOT_STATS_CHECK_DELAY); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1777 | void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1778 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1779 | int i; |
| 1780 | |
| 1781 | /* check and update now */ |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1782 | ocelot_update_stats(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1783 | |
| 1784 | /* Copy all counters */ |
| 1785 | for (i = 0; i < ocelot->num_stats; i++) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1786 | *data++ = ocelot->stats[port * ocelot->num_stats + i]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1787 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1788 | EXPORT_SYMBOL(ocelot_get_ethtool_stats); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1789 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1790 | int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1791 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1792 | if (sset != ETH_SS_STATS) |
| 1793 | return -EOPNOTSUPP; |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1794 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1795 | return ocelot->num_stats; |
| 1796 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1797 | EXPORT_SYMBOL(ocelot_get_sset_count); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1798 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1799 | int ocelot_get_ts_info(struct ocelot *ocelot, int port, |
| 1800 | struct ethtool_ts_info *info) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1801 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1802 | info->phc_index = ocelot->ptp_clock ? |
| 1803 | ptp_clock_index(ocelot->ptp_clock) : -1; |
Yangbo Lu | d2b09a8 | 2020-04-20 10:46:46 +0800 | [diff] [blame] | 1804 | if (info->phc_index == -1) { |
| 1805 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1806 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1807 | SOF_TIMESTAMPING_SOFTWARE; |
| 1808 | return 0; |
| 1809 | } |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1810 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1811 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1812 | SOF_TIMESTAMPING_SOFTWARE | |
| 1813 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1814 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1815 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1816 | info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | |
| 1817 | BIT(HWTSTAMP_TX_ONESTEP_SYNC); |
Vladimir Oltean | c49a35e | 2021-11-26 19:28:45 +0200 | [diff] [blame] | 1818 | info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | |
| 1819 | BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) | |
| 1820 | BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | |
| 1821 | BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1822 | |
| 1823 | return 0; |
| 1824 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1825 | EXPORT_SYMBOL(ocelot_get_ts_info); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1826 | |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 1827 | static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond) |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1828 | { |
| 1829 | u32 mask = 0; |
| 1830 | int port; |
| 1831 | |
| 1832 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1833 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1834 | |
| 1835 | if (!ocelot_port) |
| 1836 | continue; |
| 1837 | |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 1838 | if (ocelot_port->bond == bond) |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1839 | mask |= BIT(port); |
| 1840 | } |
| 1841 | |
| 1842 | return mask; |
| 1843 | } |
| 1844 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1845 | u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port) |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1846 | { |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1847 | struct ocelot_port *ocelot_port = ocelot->ports[src_port]; |
Vladimir Oltean | a8bd9fa | 2021-11-25 14:58:07 +0200 | [diff] [blame] | 1848 | const struct net_device *bridge; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1849 | u32 mask = 0; |
| 1850 | int port; |
| 1851 | |
Vladimir Oltean | a8bd9fa | 2021-11-25 14:58:07 +0200 | [diff] [blame] | 1852 | if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING) |
| 1853 | return 0; |
| 1854 | |
| 1855 | bridge = ocelot_port->bridge; |
| 1856 | if (!bridge) |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1857 | return 0; |
| 1858 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1859 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1860 | ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1861 | |
| 1862 | if (!ocelot_port) |
| 1863 | continue; |
| 1864 | |
| 1865 | if (ocelot_port->stp_state == BR_STATE_FORWARDING && |
| 1866 | ocelot_port->bridge == bridge) |
| 1867 | mask |= BIT(port); |
| 1868 | } |
| 1869 | |
| 1870 | return mask; |
| 1871 | } |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1872 | EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask); |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1873 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1874 | u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1875 | { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1876 | u32 mask = 0; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1877 | int port; |
| 1878 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1879 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1880 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1881 | |
| 1882 | if (!ocelot_port) |
| 1883 | continue; |
| 1884 | |
| 1885 | if (ocelot_port->is_dsa_8021q_cpu) |
| 1886 | mask |= BIT(port); |
| 1887 | } |
| 1888 | |
| 1889 | return mask; |
| 1890 | } |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1891 | EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask); |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1892 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1893 | void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining) |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1894 | { |
| 1895 | unsigned long cpu_fwd_mask; |
| 1896 | int port; |
| 1897 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1898 | lockdep_assert_held(&ocelot->fwd_domain_lock); |
| 1899 | |
| 1900 | /* If cut-through forwarding is supported, update the masks before a |
| 1901 | * port joins the forwarding domain, to avoid potential underruns if it |
| 1902 | * has the highest speed from the new domain. |
| 1903 | */ |
| 1904 | if (joining && ocelot->ops->cut_through_fwd) |
| 1905 | ocelot->ops->cut_through_fwd(ocelot); |
| 1906 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1907 | /* If a DSA tag_8021q CPU exists, it needs to be included in the |
| 1908 | * regular forwarding path of the front ports regardless of whether |
| 1909 | * those are bridged or standalone. |
| 1910 | * If DSA tag_8021q is not used, this returns 0, which is fine because |
| 1911 | * the hardware-based CPU port module can be a destination for packets |
| 1912 | * even if it isn't part of PGID_SRC. |
| 1913 | */ |
| 1914 | cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); |
| 1915 | |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1916 | /* Apply FWD mask. The loop is needed to add/remove the current port as |
| 1917 | * a source for the other ports. |
| 1918 | */ |
| 1919 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1920 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1921 | unsigned long mask; |
| 1922 | |
| 1923 | if (!ocelot_port) { |
| 1924 | /* Unused ports can't send anywhere */ |
| 1925 | mask = 0; |
| 1926 | } else if (ocelot_port->is_dsa_8021q_cpu) { |
| 1927 | /* The DSA tag_8021q CPU ports need to be able to |
| 1928 | * forward packets to all other ports except for |
| 1929 | * themselves |
| 1930 | */ |
| 1931 | mask = GENMASK(ocelot->num_phys_ports - 1, 0); |
| 1932 | mask &= ~cpu_fwd_mask; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1933 | } else if (ocelot_port->bridge) { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1934 | struct net_device *bond = ocelot_port->bond; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1935 | |
Vladimir Oltean | a8bd9fa | 2021-11-25 14:58:07 +0200 | [diff] [blame] | 1936 | mask = ocelot_get_bridge_fwd_mask(ocelot, port); |
Vladimir Oltean | c193014 | 2021-08-17 19:04:25 +0300 | [diff] [blame] | 1937 | mask |= cpu_fwd_mask; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1938 | mask &= ~BIT(port); |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 1939 | if (bond) |
| 1940 | mask &= ~ocelot_get_bond_mask(ocelot, bond); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1941 | } else { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1942 | /* Standalone ports forward only to DSA tag_8021q CPU |
| 1943 | * ports (if those exist), or to the hardware CPU port |
| 1944 | * module otherwise. |
| 1945 | */ |
| 1946 | mask = cpu_fwd_mask; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1947 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1948 | |
| 1949 | ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1950 | } |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1951 | |
| 1952 | /* If cut-through forwarding is supported and a port is leaving, there |
| 1953 | * is a chance that cut-through was disabled on the other ports due to |
| 1954 | * the port which is leaving (it has a higher link speed). We need to |
| 1955 | * update the cut-through masks of the remaining ports no earlier than |
| 1956 | * after the port has left, to prevent underruns from happening between |
| 1957 | * the cut-through update and the forwarding domain update. |
| 1958 | */ |
| 1959 | if (!joining && ocelot->ops->cut_through_fwd) |
| 1960 | ocelot->ops->cut_through_fwd(ocelot); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1961 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1962 | EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1963 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1964 | void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1965 | { |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 1966 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1967 | u32 learn_ena = 0; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1968 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1969 | mutex_lock(&ocelot->fwd_domain_lock); |
| 1970 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1971 | ocelot_port->stp_state = state; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1972 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1973 | if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && |
| 1974 | ocelot_port->learn_ena) |
| 1975 | learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1976 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1977 | ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, |
| 1978 | ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1979 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 1980 | ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING); |
| 1981 | |
| 1982 | mutex_unlock(&ocelot->fwd_domain_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1983 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1984 | EXPORT_SYMBOL(ocelot_bridge_stp_state_set); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1985 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1986 | void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1987 | { |
Vladimir Oltean | c0d7ecc | 2020-05-04 01:20:27 +0300 | [diff] [blame] | 1988 | unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); |
| 1989 | |
| 1990 | /* Setting AGE_PERIOD to zero effectively disables automatic aging, |
| 1991 | * which is clearly not what our intention is. So avoid that. |
| 1992 | */ |
| 1993 | if (!age_period) |
| 1994 | age_period = 1; |
| 1995 | |
| 1996 | ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1997 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1998 | EXPORT_SYMBOL(ocelot_set_ageing_time); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1999 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2000 | static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, |
| 2001 | const unsigned char *addr, |
| 2002 | u16 vid) |
| 2003 | { |
| 2004 | struct ocelot_multicast *mc; |
| 2005 | |
| 2006 | list_for_each_entry(mc, &ocelot->multicast, list) { |
| 2007 | if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) |
| 2008 | return mc; |
| 2009 | } |
| 2010 | |
| 2011 | return NULL; |
| 2012 | } |
| 2013 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2014 | static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) |
| 2015 | { |
| 2016 | if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) |
| 2017 | return ENTRYTYPE_MACv4; |
| 2018 | if (addr[0] == 0x33 && addr[1] == 0x33) |
| 2019 | return ENTRYTYPE_MACv6; |
Vladimir Oltean | 7c31314 | 2020-10-29 04:27:34 +0200 | [diff] [blame] | 2020 | return ENTRYTYPE_LOCKED; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2021 | } |
| 2022 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2023 | static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, |
| 2024 | unsigned long ports) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2025 | { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2026 | struct ocelot_pgid *pgid; |
| 2027 | |
| 2028 | pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); |
| 2029 | if (!pgid) |
| 2030 | return ERR_PTR(-ENOMEM); |
| 2031 | |
| 2032 | pgid->ports = ports; |
| 2033 | pgid->index = index; |
| 2034 | refcount_set(&pgid->refcount, 1); |
| 2035 | list_add_tail(&pgid->list, &ocelot->pgids); |
| 2036 | |
| 2037 | return pgid; |
| 2038 | } |
| 2039 | |
| 2040 | static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) |
| 2041 | { |
| 2042 | if (!refcount_dec_and_test(&pgid->refcount)) |
| 2043 | return; |
| 2044 | |
| 2045 | list_del(&pgid->list); |
| 2046 | kfree(pgid); |
| 2047 | } |
| 2048 | |
| 2049 | static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, |
| 2050 | const struct ocelot_multicast *mc) |
| 2051 | { |
| 2052 | struct ocelot_pgid *pgid; |
| 2053 | int index; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2054 | |
| 2055 | /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and |
| 2056 | * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the |
| 2057 | * destination mask table (PGID), the destination set is programmed as |
| 2058 | * part of the entry MAC address.", and the DEST_IDX is set to 0. |
| 2059 | */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2060 | if (mc->entry_type == ENTRYTYPE_MACv4 || |
| 2061 | mc->entry_type == ENTRYTYPE_MACv6) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2062 | return ocelot_pgid_alloc(ocelot, 0, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2063 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2064 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 2065 | /* When searching for a nonreserved multicast PGID, ignore the |
| 2066 | * dummy PGID of zero that we have for MACv4/MACv6 entries |
| 2067 | */ |
| 2068 | if (pgid->index && pgid->ports == mc->ports) { |
| 2069 | refcount_inc(&pgid->refcount); |
| 2070 | return pgid; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | /* Search for a free index in the nonreserved multicast PGID area */ |
| 2075 | for_each_nonreserved_multicast_dest_pgid(ocelot, index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2076 | bool used = false; |
| 2077 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2078 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 2079 | if (pgid->index == index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2080 | used = true; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | if (!used) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2086 | return ocelot_pgid_alloc(ocelot, index, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2087 | } |
| 2088 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2089 | return ERR_PTR(-ENOSPC); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | static void ocelot_encode_ports_to_mdb(unsigned char *addr, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2093 | struct ocelot_multicast *mc) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2094 | { |
Vladimir Oltean | ebbd860 | 2020-10-29 04:27:35 +0200 | [diff] [blame] | 2095 | ether_addr_copy(addr, mc->addr); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2096 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2097 | if (mc->entry_type == ENTRYTYPE_MACv4) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2098 | addr[0] = 0; |
| 2099 | addr[1] = mc->ports >> 8; |
| 2100 | addr[2] = mc->ports & 0xff; |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2101 | } else if (mc->entry_type == ENTRYTYPE_MACv6) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 2102 | addr[0] = mc->ports >> 8; |
| 2103 | addr[1] = mc->ports & 0xff; |
| 2104 | } |
| 2105 | } |
| 2106 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 2107 | int ocelot_port_mdb_add(struct ocelot *ocelot, int port, |
| 2108 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2109 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2110 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 2111 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2112 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2113 | u16 vid = mdb->vid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2114 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 2115 | if (port == ocelot->npi) |
| 2116 | port = ocelot->num_phys_ports; |
| 2117 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2118 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 2119 | if (!mc) { |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 2120 | /* New entry */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2121 | mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); |
| 2122 | if (!mc) |
| 2123 | return -ENOMEM; |
| 2124 | |
| 2125 | mc->entry_type = ocelot_classify_mdb(mdb->addr); |
| 2126 | ether_addr_copy(mc->addr, mdb->addr); |
| 2127 | mc->vid = vid; |
| 2128 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2129 | list_add_tail(&mc->list, &ocelot->multicast); |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 2130 | } else { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2131 | /* Existing entry. Clean up the current port mask from |
| 2132 | * hardware now, because we'll be modifying it. |
| 2133 | */ |
| 2134 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2135 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2136 | ocelot_mact_forget(ocelot, addr, vid); |
| 2137 | } |
| 2138 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 2139 | mc->ports |= BIT(port); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2140 | |
| 2141 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 2142 | if (IS_ERR(pgid)) { |
| 2143 | dev_err(ocelot->dev, |
| 2144 | "Cannot allocate PGID for mdb %pM vid %d\n", |
| 2145 | mc->addr, mc->vid); |
| 2146 | devm_kfree(ocelot->dev, mc); |
| 2147 | return PTR_ERR(pgid); |
| 2148 | } |
| 2149 | mc->pgid = pgid; |
| 2150 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2151 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2152 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2153 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 2154 | mc->entry_type != ENTRYTYPE_MACv6) |
| 2155 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 2156 | pgid->index); |
| 2157 | |
| 2158 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2159 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2160 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 2161 | EXPORT_SYMBOL(ocelot_port_mdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2162 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 2163 | int ocelot_port_mdb_del(struct ocelot *ocelot, int port, |
| 2164 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2165 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2166 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 2167 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2168 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2169 | u16 vid = mdb->vid; |
| 2170 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 2171 | if (port == ocelot->npi) |
| 2172 | port = ocelot->num_phys_ports; |
| 2173 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2174 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 2175 | if (!mc) |
| 2176 | return -ENOENT; |
| 2177 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2178 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2179 | ocelot_mact_forget(ocelot, addr, vid); |
| 2180 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2181 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 2182 | mc->ports &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2183 | if (!mc->ports) { |
| 2184 | list_del(&mc->list); |
| 2185 | devm_kfree(ocelot->dev, mc); |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2189 | /* We have a PGID with fewer ports now */ |
| 2190 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 2191 | if (IS_ERR(pgid)) |
| 2192 | return PTR_ERR(pgid); |
| 2193 | mc->pgid = pgid; |
| 2194 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2195 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2196 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2197 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 2198 | mc->entry_type != ENTRYTYPE_MACv6) |
| 2199 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 2200 | pgid->index); |
| 2201 | |
| 2202 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 2203 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2204 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 2205 | EXPORT_SYMBOL(ocelot_port_mdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2206 | |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 2207 | void ocelot_port_bridge_join(struct ocelot *ocelot, int port, |
| 2208 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2209 | { |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 2210 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2211 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2212 | mutex_lock(&ocelot->fwd_domain_lock); |
| 2213 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 2214 | ocelot_port->bridge = bridge; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2215 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2216 | ocelot_apply_bridge_fwd_mask(ocelot, true); |
| 2217 | |
| 2218 | mutex_unlock(&ocelot->fwd_domain_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2219 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2220 | EXPORT_SYMBOL(ocelot_port_bridge_join); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2221 | |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 2222 | void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, |
| 2223 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2224 | { |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 2225 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 2226 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2227 | mutex_lock(&ocelot->fwd_domain_lock); |
| 2228 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 2229 | ocelot_port->bridge = NULL; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 2230 | |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 2231 | ocelot_port_set_pvid(ocelot, port, NULL); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 2232 | ocelot_port_manage_port_tag(ocelot, port); |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2233 | ocelot_apply_bridge_fwd_mask(ocelot, false); |
| 2234 | |
| 2235 | mutex_unlock(&ocelot->fwd_domain_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2236 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2237 | EXPORT_SYMBOL(ocelot_port_bridge_leave); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2238 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2239 | static void ocelot_set_aggr_pgids(struct ocelot *ocelot) |
| 2240 | { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2241 | unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2242 | int i, port, lag; |
| 2243 | |
| 2244 | /* Reset destination and aggregation PGIDS */ |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 2245 | for_each_unicast_dest_pgid(ocelot, port) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2246 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 2247 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 2248 | for_each_aggr_pgid(ocelot, i) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2249 | ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 2250 | ANA_PGID_PGID, i); |
| 2251 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2252 | /* The visited ports bitmask holds the list of ports offloading any |
| 2253 | * bonding interface. Initially we mark all these ports as unvisited, |
| 2254 | * then every time we visit a port in this bitmask, we know that it is |
| 2255 | * the lowest numbered port, i.e. the one whose logical ID == physical |
| 2256 | * port ID == LAG ID. So we mark as visited all further ports in the |
| 2257 | * bitmask that are offloading the same bonding interface. This way, |
| 2258 | * we set up the aggregation PGIDs only once per bonding interface. |
| 2259 | */ |
| 2260 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2261 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2262 | |
| 2263 | if (!ocelot_port || !ocelot_port->bond) |
| 2264 | continue; |
| 2265 | |
| 2266 | visited &= ~BIT(port); |
| 2267 | } |
| 2268 | |
| 2269 | /* Now, set PGIDs for each active LAG */ |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2270 | for (lag = 0; lag < ocelot->num_phys_ports; lag++) { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2271 | struct net_device *bond = ocelot->ports[lag]->bond; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 2272 | int num_active_ports = 0; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2273 | unsigned long bond_mask; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2274 | u8 aggr_idx[16]; |
| 2275 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2276 | if (!bond || (visited & BIT(lag))) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2277 | continue; |
| 2278 | |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 2279 | bond_mask = ocelot_get_bond_mask(ocelot, bond); |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2280 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2281 | for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 2282 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2283 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2284 | // Destination mask |
| 2285 | ocelot_write_rix(ocelot, bond_mask, |
| 2286 | ANA_PGID_PGID, port); |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 2287 | |
| 2288 | if (ocelot_port->lag_tx_active) |
| 2289 | aggr_idx[num_active_ports++] = port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2290 | } |
| 2291 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 2292 | for_each_aggr_pgid(ocelot, i) { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2293 | u32 ac; |
| 2294 | |
| 2295 | ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); |
| 2296 | ac &= ~bond_mask; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 2297 | /* Don't do division by zero if there was no active |
| 2298 | * port. Just make all aggregation codes zero. |
| 2299 | */ |
| 2300 | if (num_active_ports) |
| 2301 | ac |= BIT(aggr_idx[i % num_active_ports]); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2302 | ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); |
| 2303 | } |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 2304 | |
| 2305 | /* Mark all ports in the same LAG as visited to avoid applying |
| 2306 | * the same config again. |
| 2307 | */ |
| 2308 | for (port = lag; port < ocelot->num_phys_ports; port++) { |
| 2309 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2310 | |
| 2311 | if (!ocelot_port) |
| 2312 | continue; |
| 2313 | |
| 2314 | if (ocelot_port->bond == bond) |
| 2315 | visited |= BIT(port); |
| 2316 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2320 | /* When offloading a bonding interface, the switch ports configured under the |
| 2321 | * same bond must have the same logical port ID, equal to the physical port ID |
| 2322 | * of the lowest numbered physical port in that bond. Otherwise, in standalone/ |
| 2323 | * bridged mode, each port has a logical port ID equal to its physical port ID. |
| 2324 | */ |
| 2325 | static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2326 | { |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2327 | int port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2328 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2329 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2330 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2331 | struct net_device *bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2332 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2333 | if (!ocelot_port) |
| 2334 | continue; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2335 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2336 | bond = ocelot_port->bond; |
| 2337 | if (bond) { |
Vladimir Oltean | a14e6b6 | 2022-01-07 18:43:32 +0200 | [diff] [blame] | 2338 | int lag = __ffs(ocelot_get_bond_mask(ocelot, bond)); |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2339 | |
| 2340 | ocelot_rmw_gix(ocelot, |
| 2341 | ANA_PORT_PORT_CFG_PORTID_VAL(lag), |
| 2342 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 2343 | ANA_PORT_PORT_CFG, port); |
| 2344 | } else { |
| 2345 | ocelot_rmw_gix(ocelot, |
| 2346 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 2347 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 2348 | ANA_PORT_PORT_CFG, port); |
| 2349 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2353 | int ocelot_port_lag_join(struct ocelot *ocelot, int port, |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 2354 | struct net_device *bond, |
| 2355 | struct netdev_lag_upper_info *info) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2356 | { |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 2357 | if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) |
| 2358 | return -EOPNOTSUPP; |
| 2359 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2360 | mutex_lock(&ocelot->fwd_domain_lock); |
| 2361 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 2362 | ocelot->ports[port]->bond = bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2363 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2364 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2365 | ocelot_apply_bridge_fwd_mask(ocelot, true); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2366 | ocelot_set_aggr_pgids(ocelot); |
| 2367 | |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2368 | mutex_unlock(&ocelot->fwd_domain_lock); |
| 2369 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2370 | return 0; |
| 2371 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2372 | EXPORT_SYMBOL(ocelot_port_lag_join); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2373 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2374 | void ocelot_port_lag_leave(struct ocelot *ocelot, int port, |
| 2375 | struct net_device *bond) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2376 | { |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2377 | mutex_lock(&ocelot->fwd_domain_lock); |
| 2378 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 2379 | ocelot->ports[port]->bond = NULL; |
| 2380 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2381 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2382 | ocelot_apply_bridge_fwd_mask(ocelot, false); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2383 | ocelot_set_aggr_pgids(ocelot); |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2384 | |
| 2385 | mutex_unlock(&ocelot->fwd_domain_lock); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2386 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2387 | EXPORT_SYMBOL(ocelot_port_lag_leave); |
Petr Machata | 0e332c8 | 2018-11-22 23:30:11 +0000 | [diff] [blame] | 2388 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 2389 | void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) |
| 2390 | { |
| 2391 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2392 | |
| 2393 | ocelot_port->lag_tx_active = lag_tx_active; |
| 2394 | |
| 2395 | /* Rebalance the LAGs */ |
| 2396 | ocelot_set_aggr_pgids(ocelot); |
| 2397 | } |
| 2398 | EXPORT_SYMBOL(ocelot_port_lag_change); |
| 2399 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2400 | /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. |
| 2401 | * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2402 | * In the special case that it's the NPI port that we're configuring, the |
| 2403 | * length of the tag and optional prefix needs to be accounted for privately, |
| 2404 | * in order to be able to sustain communication at the requested @sdu. |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2405 | */ |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2406 | void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2407 | { |
| 2408 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2409 | int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2410 | int pause_start, pause_stop; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2411 | int atop, atop_tot; |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2412 | |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2413 | if (port == ocelot->npi) { |
| 2414 | maxlen += OCELOT_TAG_LEN; |
| 2415 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2416 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2417 | maxlen += OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2418 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2419 | maxlen += OCELOT_LONG_PREFIX_LEN; |
| 2420 | } |
| 2421 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2422 | ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2423 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2424 | /* Set Pause watermark hysteresis */ |
| 2425 | pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; |
| 2426 | pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 2427 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, |
| 2428 | pause_start); |
| 2429 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, |
| 2430 | pause_stop); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2431 | |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2432 | /* Tail dropping watermarks */ |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2433 | atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2434 | OCELOT_BUFFER_CELL_SZ; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2435 | atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; |
| 2436 | ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); |
| 2437 | ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2438 | } |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2439 | EXPORT_SYMBOL(ocelot_port_set_maxlen); |
| 2440 | |
| 2441 | int ocelot_get_max_mtu(struct ocelot *ocelot, int port) |
| 2442 | { |
| 2443 | int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; |
| 2444 | |
| 2445 | if (port == ocelot->npi) { |
| 2446 | max_mtu -= OCELOT_TAG_LEN; |
| 2447 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2448 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2449 | max_mtu -= OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2450 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2451 | max_mtu -= OCELOT_LONG_PREFIX_LEN; |
| 2452 | } |
| 2453 | |
| 2454 | return max_mtu; |
| 2455 | } |
| 2456 | EXPORT_SYMBOL(ocelot_get_max_mtu); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2457 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 2458 | static void ocelot_port_set_learning(struct ocelot *ocelot, int port, |
| 2459 | bool enabled) |
| 2460 | { |
| 2461 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2462 | u32 val = 0; |
| 2463 | |
| 2464 | if (enabled) |
| 2465 | val = ANA_PORT_PORT_CFG_LEARN_ENA; |
| 2466 | |
| 2467 | ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, |
| 2468 | ANA_PORT_PORT_CFG, port); |
| 2469 | |
| 2470 | ocelot_port->learn_ena = enabled; |
| 2471 | } |
| 2472 | |
| 2473 | static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, |
| 2474 | bool enabled) |
| 2475 | { |
| 2476 | u32 val = 0; |
| 2477 | |
| 2478 | if (enabled) |
| 2479 | val = BIT(port); |
| 2480 | |
| 2481 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); |
| 2482 | } |
| 2483 | |
| 2484 | static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, |
| 2485 | bool enabled) |
| 2486 | { |
| 2487 | u32 val = 0; |
| 2488 | |
| 2489 | if (enabled) |
| 2490 | val = BIT(port); |
| 2491 | |
| 2492 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); |
| 2493 | } |
| 2494 | |
| 2495 | static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, |
| 2496 | bool enabled) |
| 2497 | { |
| 2498 | u32 val = 0; |
| 2499 | |
| 2500 | if (enabled) |
| 2501 | val = BIT(port); |
| 2502 | |
| 2503 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); |
| 2504 | } |
| 2505 | |
| 2506 | int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, |
| 2507 | struct switchdev_brport_flags flags) |
| 2508 | { |
| 2509 | if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | |
| 2510 | BR_BCAST_FLOOD)) |
| 2511 | return -EINVAL; |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); |
| 2516 | |
| 2517 | void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, |
| 2518 | struct switchdev_brport_flags flags) |
| 2519 | { |
| 2520 | if (flags.mask & BR_LEARNING) |
| 2521 | ocelot_port_set_learning(ocelot, port, |
| 2522 | !!(flags.val & BR_LEARNING)); |
| 2523 | |
| 2524 | if (flags.mask & BR_FLOOD) |
| 2525 | ocelot_port_set_ucast_flood(ocelot, port, |
| 2526 | !!(flags.val & BR_FLOOD)); |
| 2527 | |
| 2528 | if (flags.mask & BR_MCAST_FLOOD) |
| 2529 | ocelot_port_set_mcast_flood(ocelot, port, |
| 2530 | !!(flags.val & BR_MCAST_FLOOD)); |
| 2531 | |
| 2532 | if (flags.mask & BR_BCAST_FLOOD) |
| 2533 | ocelot_port_set_bcast_flood(ocelot, port, |
| 2534 | !!(flags.val & BR_BCAST_FLOOD)); |
| 2535 | } |
| 2536 | EXPORT_SYMBOL(ocelot_port_bridge_flags); |
| 2537 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2538 | void ocelot_init_port(struct ocelot *ocelot, int port) |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2539 | { |
| 2540 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2541 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 2542 | skb_queue_head_init(&ocelot_port->tx_skbs); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2543 | |
| 2544 | /* Basic L2 initialization */ |
| 2545 | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2546 | /* Set MAC IFG Gaps |
| 2547 | * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 |
| 2548 | * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 |
| 2549 | */ |
| 2550 | ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), |
| 2551 | DEV_MAC_IFG_CFG); |
| 2552 | |
| 2553 | /* Load seed (0) and set MAC HDX late collision */ |
| 2554 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | |
| 2555 | DEV_MAC_HDX_CFG_SEED_LOAD, |
| 2556 | DEV_MAC_HDX_CFG); |
| 2557 | mdelay(1); |
| 2558 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), |
| 2559 | DEV_MAC_HDX_CFG); |
| 2560 | |
| 2561 | /* Set Max Length and maximum tags allowed */ |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2562 | ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2563 | ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | |
| 2564 | DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2565 | DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2566 | DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, |
| 2567 | DEV_MAC_TAGS_CFG); |
| 2568 | |
| 2569 | /* Set SMAC of Pause frame (00:00:00:00:00:00) */ |
| 2570 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); |
| 2571 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); |
| 2572 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2573 | /* Enable transmission of pause frames */ |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 2574 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2575 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2576 | /* Drop frames with multicast source address */ |
| 2577 | ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 2578 | ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 2579 | ANA_PORT_DROP_CFG, port); |
| 2580 | |
| 2581 | /* Set default VLAN and tag type to 8021Q. */ |
| 2582 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), |
| 2583 | REW_PORT_VLAN_CFG_PORT_TPID_M, |
| 2584 | REW_PORT_VLAN_CFG, port); |
| 2585 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 2586 | /* Disable source address learning for standalone mode */ |
| 2587 | ocelot_port_set_learning(ocelot, port, false); |
| 2588 | |
Vladimir Oltean | 46efe4e | 2021-08-15 04:47:47 +0300 | [diff] [blame] | 2589 | /* Set the port's initial logical port ID value, enable receiving |
| 2590 | * frames on it, and configure the MAC address learning type to |
| 2591 | * automatic. |
| 2592 | */ |
| 2593 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | |
| 2594 | ANA_PORT_PORT_CFG_RECV_ENA | |
| 2595 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 2596 | ANA_PORT_PORT_CFG, port); |
| 2597 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2598 | /* Enable vcap lookups */ |
| 2599 | ocelot_vcap_enable(ocelot, port); |
| 2600 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2601 | EXPORT_SYMBOL(ocelot_init_port); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2602 | |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2603 | /* Configure and enable the CPU port module, which is a set of queues |
| 2604 | * accessible through register MMIO, frame DMA or Ethernet (in case |
| 2605 | * NPI mode is used). |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2606 | */ |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2607 | static void ocelot_cpu_port_init(struct ocelot *ocelot) |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2608 | { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2609 | int cpu = ocelot->num_phys_ports; |
| 2610 | |
| 2611 | /* The unicast destination PGID for the CPU port module is unused */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2612 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2613 | /* Instead set up a multicast destination PGID for traffic copied to |
| 2614 | * the CPU. Whitelisted MAC addresses like the port netdevice MAC |
| 2615 | * addresses will be copied to the CPU via this PGID. |
| 2616 | */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2617 | ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); |
| 2618 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | |
| 2619 | ANA_PORT_PORT_CFG_PORTID_VAL(cpu), |
| 2620 | ANA_PORT_PORT_CFG, cpu); |
| 2621 | |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2622 | /* Enable CPU port module */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2623 | ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2624 | /* CPU port Injection/Extraction configuration */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2625 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2626 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2627 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2628 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2629 | |
| 2630 | /* Configure the CPU port to be VLAN aware */ |
Vladimir Oltean | bfbab31 | 2021-10-20 20:58:51 +0300 | [diff] [blame] | 2631 | ocelot_write_gix(ocelot, |
| 2632 | ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) | |
| 2633 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 2634 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2635 | ANA_PORT_VLAN_CFG, cpu); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2636 | } |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2637 | |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2638 | static void ocelot_detect_features(struct ocelot *ocelot) |
| 2639 | { |
| 2640 | int mmgt, eq_ctrl; |
| 2641 | |
| 2642 | /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds |
| 2643 | * the number of 240-byte free memory words (aka 4-cell chunks) and not |
| 2644 | * 192 bytes as the documentation incorrectly says. |
| 2645 | */ |
| 2646 | mmgt = ocelot_read(ocelot, SYS_MMGT); |
| 2647 | ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); |
| 2648 | |
| 2649 | eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); |
| 2650 | ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2653 | int ocelot_init(struct ocelot *ocelot) |
| 2654 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2655 | char queue_name[32]; |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2656 | int i, ret; |
| 2657 | u32 port; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2658 | |
Vladimir Oltean | 3a77b59 | 2019-11-14 17:03:26 +0200 | [diff] [blame] | 2659 | if (ocelot->ops->reset) { |
| 2660 | ret = ocelot->ops->reset(ocelot); |
| 2661 | if (ret) { |
| 2662 | dev_err(ocelot->dev, "Switch reset failed\n"); |
| 2663 | return ret; |
| 2664 | } |
| 2665 | } |
| 2666 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2667 | ocelot->stats = devm_kcalloc(ocelot->dev, |
| 2668 | ocelot->num_phys_ports * ocelot->num_stats, |
| 2669 | sizeof(u64), GFP_KERNEL); |
| 2670 | if (!ocelot->stats) |
| 2671 | return -ENOMEM; |
| 2672 | |
| 2673 | mutex_init(&ocelot->stats_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2674 | mutex_init(&ocelot->ptp_lock); |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 2675 | mutex_init(&ocelot->mact_lock); |
Vladimir Oltean | 8abe197 | 2021-11-25 14:58:08 +0200 | [diff] [blame] | 2676 | mutex_init(&ocelot->fwd_domain_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2677 | spin_lock_init(&ocelot->ptp_clock_lock); |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 2678 | spin_lock_init(&ocelot->ts_id_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2679 | snprintf(queue_name, sizeof(queue_name), "%s-stats", |
| 2680 | dev_name(ocelot->dev)); |
| 2681 | ocelot->stats_queue = create_singlethread_workqueue(queue_name); |
| 2682 | if (!ocelot->stats_queue) |
| 2683 | return -ENOMEM; |
| 2684 | |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 2685 | ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); |
| 2686 | if (!ocelot->owq) { |
| 2687 | destroy_workqueue(ocelot->stats_queue); |
| 2688 | return -ENOMEM; |
| 2689 | } |
| 2690 | |
Claudiu Manoil | 2b120dd | 2019-11-09 15:02:58 +0200 | [diff] [blame] | 2691 | INIT_LIST_HEAD(&ocelot->multicast); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2692 | INIT_LIST_HEAD(&ocelot->pgids); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 2693 | INIT_LIST_HEAD(&ocelot->vlans); |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2694 | ocelot_detect_features(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2695 | ocelot_mact_init(ocelot); |
| 2696 | ocelot_vlan_init(ocelot); |
Vladimir Oltean | aae4e50 | 2020-06-20 18:43:46 +0300 | [diff] [blame] | 2697 | ocelot_vcap_init(ocelot); |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2698 | ocelot_cpu_port_init(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2699 | |
Xiaoliang Yang | 23e2c50 | 2021-11-18 18:11:59 +0800 | [diff] [blame] | 2700 | if (ocelot->ops->psfp_init) |
| 2701 | ocelot->ops->psfp_init(ocelot); |
| 2702 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2703 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2704 | /* Clear all counters (5 groups) */ |
| 2705 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | |
| 2706 | SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), |
| 2707 | SYS_STAT_CFG); |
| 2708 | } |
| 2709 | |
| 2710 | /* Only use S-Tag */ |
| 2711 | ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); |
| 2712 | |
| 2713 | /* Aggregation mode */ |
| 2714 | ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | |
| 2715 | ANA_AGGR_CFG_AC_DMAC_ENA | |
| 2716 | ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | |
Vladimir Oltean | f79c20c | 2021-02-06 00:02:13 +0200 | [diff] [blame] | 2717 | ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | |
| 2718 | ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | |
| 2719 | ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, |
| 2720 | ANA_AGGR_CFG); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2721 | |
| 2722 | /* Set MAC age time to default value. The entry is aged after |
| 2723 | * 2*AGE_PERIOD |
| 2724 | */ |
| 2725 | ocelot_write(ocelot, |
| 2726 | ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), |
| 2727 | ANA_AUTOAGE); |
| 2728 | |
| 2729 | /* Disable learning for frames discarded by VLAN ingress filtering */ |
| 2730 | regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); |
| 2731 | |
| 2732 | /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ |
| 2733 | ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | |
| 2734 | SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); |
| 2735 | |
| 2736 | /* Setup flooding PGIDs */ |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 2737 | for (i = 0; i < ocelot->num_flooding_pgids; i++) |
| 2738 | ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 2739 | ANA_FLOODING_FLD_BROADCAST(PGID_BC) | |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 2740 | ANA_FLOODING_FLD_UNICAST(PGID_UC), |
| 2741 | ANA_FLOODING, i); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2742 | ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | |
| 2743 | ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | |
| 2744 | ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | |
| 2745 | ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), |
| 2746 | ANA_FLOODING_IPMC); |
| 2747 | |
| 2748 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2749 | /* Transmit the frame to the local port. */ |
| 2750 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 2751 | /* Do not forward BPDU frames to the front ports. */ |
| 2752 | ocelot_write_gix(ocelot, |
| 2753 | ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), |
| 2754 | ANA_PORT_CPU_FWD_BPDU_CFG, |
| 2755 | port); |
| 2756 | /* Ensure bridging is disabled */ |
| 2757 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); |
| 2758 | } |
| 2759 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 2760 | for_each_nonreserved_multicast_dest_pgid(ocelot, i) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2761 | u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); |
| 2762 | |
| 2763 | ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); |
| 2764 | } |
Horatiu Vultur | ebb1bb4 | 2021-03-16 21:10:17 +0100 | [diff] [blame] | 2765 | |
| 2766 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); |
| 2767 | |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 2768 | /* Allow broadcast and unknown L2 multicast to the CPU. */ |
| 2769 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2770 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2771 | ANA_PGID_PGID, PGID_MC); |
| 2772 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2773 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2774 | ANA_PGID_PGID, PGID_BC); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2775 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); |
| 2776 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); |
| 2777 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2778 | /* Allow manual injection via DEVCPU_QS registers, and byte swap these |
| 2779 | * registers endianness. |
| 2780 | */ |
| 2781 | ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | |
| 2782 | QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); |
| 2783 | ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | |
| 2784 | QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); |
| 2785 | ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | |
| 2786 | ANA_CPUQ_CFG_CPUQ_LRN(2) | |
| 2787 | ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | |
| 2788 | ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | |
| 2789 | ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | |
| 2790 | ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | |
| 2791 | ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | |
| 2792 | ANA_CPUQ_CFG_CPUQ_IGMP(6) | |
| 2793 | ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); |
| 2794 | for (i = 0; i < 16; i++) |
| 2795 | ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | |
| 2796 | ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), |
| 2797 | ANA_CPUQ_8021_CFG, i); |
| 2798 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 2799 | INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2800 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 2801 | OCELOT_STATS_CHECK_DELAY); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2802 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2803 | return 0; |
| 2804 | } |
| 2805 | EXPORT_SYMBOL(ocelot_init); |
| 2806 | |
| 2807 | void ocelot_deinit(struct ocelot *ocelot) |
| 2808 | { |
Claudiu Manoil | c5d1396 | 2019-07-25 16:33:18 +0300 | [diff] [blame] | 2809 | cancel_delayed_work(&ocelot->stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2810 | destroy_workqueue(ocelot->stats_queue); |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 2811 | destroy_workqueue(ocelot->owq); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2812 | mutex_destroy(&ocelot->stats_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2813 | } |
| 2814 | EXPORT_SYMBOL(ocelot_deinit); |
| 2815 | |
Vladimir Oltean | e5fb512 | 2020-09-18 04:07:30 +0300 | [diff] [blame] | 2816 | void ocelot_deinit_port(struct ocelot *ocelot, int port) |
| 2817 | { |
| 2818 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2819 | |
| 2820 | skb_queue_purge(&ocelot_port->tx_skbs); |
| 2821 | } |
| 2822 | EXPORT_SYMBOL(ocelot_deinit_port); |
| 2823 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2824 | MODULE_LICENSE("Dual MIT/GPL"); |