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 | */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 7 | #include <linux/if_bridge.h> |
Vladimir Oltean | 2096805 | 2020-09-30 01:27:26 +0300 | [diff] [blame] | 8 | #include <soc/mscc/ocelot_vcap.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 9 | #include "ocelot.h" |
Vladimir Oltean | 3c83654 | 2020-06-20 18:43:45 +0300 | [diff] [blame] | 10 | #include "ocelot_vcap.h" |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 11 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 12 | #define TABLE_UPDATE_SLEEP_US 10 |
| 13 | #define TABLE_UPDATE_TIMEOUT_US 100000 |
| 14 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 15 | struct ocelot_mact_entry { |
| 16 | u8 mac[ETH_ALEN]; |
| 17 | u16 vid; |
| 18 | enum macaccess_entry_type type; |
| 19 | }; |
| 20 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 21 | static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) |
| 22 | { |
| 23 | return ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 24 | } |
| 25 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 26 | static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) |
| 27 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 28 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 29 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 30 | return readx_poll_timeout(ocelot_mact_read_macaccess, |
| 31 | ocelot, val, |
| 32 | (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == |
| 33 | MACACCESS_CMD_IDLE, |
| 34 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static void ocelot_mact_select(struct ocelot *ocelot, |
| 38 | const unsigned char mac[ETH_ALEN], |
| 39 | unsigned int vid) |
| 40 | { |
| 41 | u32 macl = 0, mach = 0; |
| 42 | |
| 43 | /* Set the MAC address to handle and the vlan associated in a format |
| 44 | * understood by the hardware. |
| 45 | */ |
| 46 | mach |= vid << 16; |
| 47 | mach |= mac[0] << 8; |
| 48 | mach |= mac[1] << 0; |
| 49 | macl |= mac[2] << 24; |
| 50 | macl |= mac[3] << 16; |
| 51 | macl |= mac[4] << 8; |
| 52 | macl |= mac[5] << 0; |
| 53 | |
| 54 | ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); |
| 55 | ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); |
| 56 | |
| 57 | } |
| 58 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 59 | int ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 60 | const unsigned char mac[ETH_ALEN], |
| 61 | unsigned int vid, enum macaccess_entry_type type) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 62 | { |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 63 | u32 cmd = ANA_TABLES_MACACCESS_VALID | |
| 64 | ANA_TABLES_MACACCESS_DEST_IDX(port) | |
| 65 | ANA_TABLES_MACACCESS_ENTRYTYPE(type) | |
| 66 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); |
| 67 | unsigned int mc_ports; |
| 68 | |
| 69 | /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ |
| 70 | if (type == ENTRYTYPE_MACv4) |
| 71 | mc_ports = (mac[1] << 8) | mac[2]; |
| 72 | else if (type == ENTRYTYPE_MACv6) |
| 73 | mc_ports = (mac[0] << 8) | mac[1]; |
| 74 | else |
| 75 | mc_ports = 0; |
| 76 | |
| 77 | if (mc_ports & BIT(ocelot->num_phys_ports)) |
| 78 | cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; |
| 79 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 80 | ocelot_mact_select(ocelot, mac, vid); |
| 81 | |
| 82 | /* Issue a write command */ |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 83 | ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 84 | |
| 85 | return ocelot_mact_wait_for_completion(ocelot); |
| 86 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 87 | EXPORT_SYMBOL(ocelot_mact_learn); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 88 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 89 | int ocelot_mact_forget(struct ocelot *ocelot, |
| 90 | const unsigned char mac[ETH_ALEN], unsigned int vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 91 | { |
| 92 | ocelot_mact_select(ocelot, mac, vid); |
| 93 | |
| 94 | /* Issue a forget command */ |
| 95 | ocelot_write(ocelot, |
| 96 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), |
| 97 | ANA_TABLES_MACACCESS); |
| 98 | |
| 99 | return ocelot_mact_wait_for_completion(ocelot); |
| 100 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 101 | EXPORT_SYMBOL(ocelot_mact_forget); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 102 | |
| 103 | static void ocelot_mact_init(struct ocelot *ocelot) |
| 104 | { |
| 105 | /* Configure the learning mode entries attributes: |
| 106 | * - Do not copy the frame to the CPU extraction queues. |
| 107 | * - Use the vlan and mac_cpoy for dmac lookup. |
| 108 | */ |
| 109 | ocelot_rmw(ocelot, 0, |
| 110 | ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS |
| 111 | | ANA_AGENCTRL_LEARN_FWD_KILL |
| 112 | | ANA_AGENCTRL_LEARN_IGNORE_VLAN, |
| 113 | ANA_AGENCTRL); |
| 114 | |
| 115 | /* Clear the MAC table */ |
| 116 | ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); |
| 117 | } |
| 118 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 119 | static void ocelot_vcap_enable(struct ocelot *ocelot, int port) |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 120 | { |
| 121 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | |
| 122 | ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 123 | ANA_PORT_VCAP_S2_CFG, port); |
Xiaoliang Yang | 75944fd | 2020-10-02 15:02:23 +0300 | [diff] [blame] | 124 | |
| 125 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, |
| 126 | ANA_PORT_VCAP_CFG, port); |
Xiaoliang Yang | 2f17c05 | 2020-10-02 15:02:24 +0300 | [diff] [blame] | 127 | |
| 128 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, |
| 129 | REW_PORT_CFG_ES0_EN, |
| 130 | REW_PORT_CFG, port); |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 133 | static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) |
| 134 | { |
| 135 | return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); |
| 136 | } |
| 137 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 138 | static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) |
| 139 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 140 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 141 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 142 | return readx_poll_timeout(ocelot_vlant_read_vlanaccess, |
| 143 | ocelot, |
| 144 | val, |
| 145 | (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == |
| 146 | ANA_TABLES_VLANACCESS_CMD_IDLE, |
| 147 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 150 | static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) |
| 151 | { |
| 152 | /* Select the VID to configure */ |
| 153 | ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), |
| 154 | ANA_TABLES_VLANTIDX); |
| 155 | /* Set the vlan port members mask and issue a write command */ |
| 156 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | |
| 157 | ANA_TABLES_VLANACCESS_CMD_WRITE, |
| 158 | ANA_TABLES_VLANACCESS); |
| 159 | |
| 160 | return ocelot_vlant_wait_for_completion(ocelot); |
| 161 | } |
| 162 | |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 163 | static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, |
| 164 | struct ocelot_vlan native_vlan) |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 165 | { |
| 166 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 167 | u32 val = 0; |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 168 | |
Vladimir Oltean | e2b2e83 | 2020-10-31 12:29:13 +0200 | [diff] [blame] | 169 | ocelot_port->native_vlan = native_vlan; |
| 170 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 171 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid), |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 172 | REW_PORT_VLAN_CFG_PORT_VID_M, |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 173 | REW_PORT_VLAN_CFG, port); |
| 174 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 175 | if (ocelot_port->vlan_aware) { |
Vladimir Oltean | e2b2e83 | 2020-10-31 12:29:13 +0200 | [diff] [blame] | 176 | if (native_vlan.valid) |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 177 | /* Tag all frames except when VID == DEFAULT_VLAN */ |
| 178 | val = REW_TAG_CFG_TAG_CFG(1); |
| 179 | else |
| 180 | /* Tag all frames */ |
| 181 | val = REW_TAG_CFG_TAG_CFG(3); |
| 182 | } else { |
| 183 | /* Port tagging disabled. */ |
| 184 | val = REW_TAG_CFG_TAG_CFG(0); |
| 185 | } |
| 186 | ocelot_rmw_gix(ocelot, val, |
| 187 | REW_TAG_CFG_TAG_CFG_M, |
| 188 | REW_TAG_CFG, port); |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 191 | /* Default vlan to clasify for untagged frames (may be zero) */ |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 192 | static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, |
| 193 | struct ocelot_vlan pvid_vlan) |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 194 | { |
| 195 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 196 | u32 val = 0; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 197 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 198 | ocelot_port->pvid_vlan = pvid_vlan; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 199 | |
| 200 | if (!ocelot_port->vlan_aware) |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 201 | pvid_vlan.vid = 0; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 202 | |
| 203 | ocelot_rmw_gix(ocelot, |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 204 | ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid), |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 205 | ANA_PORT_VLAN_CFG_VLAN_VID_M, |
| 206 | ANA_PORT_VLAN_CFG, port); |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 207 | |
| 208 | /* If there's no pvid, we should drop not only untagged traffic (which |
| 209 | * happens automatically), but also 802.1p traffic which gets |
| 210 | * classified to VLAN 0, but that is always in our RX filter, so it |
| 211 | * would get accepted were it not for this setting. |
| 212 | */ |
| 213 | if (!pvid_vlan.valid && ocelot_port->vlan_aware) |
| 214 | val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 215 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; |
| 216 | |
| 217 | ocelot_rmw_gix(ocelot, val, |
| 218 | ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 219 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, |
| 220 | ANA_PORT_DROP_CFG, port); |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 223 | int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 224 | bool vlan_aware) |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 225 | { |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 226 | struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 227 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 228 | struct ocelot_vcap_filter *filter; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 229 | u32 val; |
| 230 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 231 | list_for_each_entry(filter, &block->rules, list) { |
| 232 | if (filter->ingress_port_mask & BIT(port) && |
| 233 | filter->action.vid_replace_ena) { |
| 234 | dev_err(ocelot->dev, |
| 235 | "Cannot change VLAN state with vlan modify rules active\n"); |
| 236 | return -EBUSY; |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 237 | } |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 238 | } |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 239 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 240 | ocelot_port->vlan_aware = vlan_aware; |
| 241 | |
| 242 | if (vlan_aware) |
| 243 | val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 244 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); |
| 245 | else |
| 246 | val = 0; |
| 247 | ocelot_rmw_gix(ocelot, val, |
| 248 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 249 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, |
| 250 | ANA_PORT_VLAN_CFG, port); |
| 251 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 252 | ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); |
| 253 | ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan); |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 254 | |
| 255 | return 0; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 256 | } |
| 257 | EXPORT_SYMBOL(ocelot_port_vlan_filtering); |
| 258 | |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 259 | int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
| 260 | bool untagged) |
| 261 | { |
| 262 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 263 | |
| 264 | /* Deny changing the native VLAN, but always permit deleting it */ |
| 265 | if (untagged && ocelot_port->native_vlan.vid != vid && |
| 266 | ocelot_port->native_vlan.valid) { |
| 267 | dev_err(ocelot->dev, |
| 268 | "Port already has a native VLAN: %d\n", |
| 269 | ocelot_port->native_vlan.vid); |
| 270 | return -EBUSY; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | EXPORT_SYMBOL(ocelot_vlan_prepare); |
| 276 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 277 | int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
| 278 | bool untagged) |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 279 | { |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 280 | int ret; |
| 281 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 282 | /* Make the port a member of the VLAN */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 283 | ocelot->vlan_mask[vid] |= BIT(port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 284 | ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | /* Default ingress vlan classification */ |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 289 | if (pvid) { |
| 290 | struct ocelot_vlan pvid_vlan; |
| 291 | |
| 292 | pvid_vlan.vid = vid; |
Vladimir Oltean | e2b2e83 | 2020-10-31 12:29:13 +0200 | [diff] [blame] | 293 | pvid_vlan.valid = true; |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 294 | ocelot_port_set_pvid(ocelot, port, pvid_vlan); |
| 295 | } |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 296 | |
| 297 | /* Untagged egress vlan clasification */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 298 | if (untagged) { |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 299 | struct ocelot_vlan native_vlan; |
| 300 | |
| 301 | native_vlan.vid = vid; |
Vladimir Oltean | e2b2e83 | 2020-10-31 12:29:13 +0200 | [diff] [blame] | 302 | native_vlan.valid = true; |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 303 | ocelot_port_set_native_vlan(ocelot, port, native_vlan); |
Vladimir Oltean | b9cd75e | 2019-10-26 21:04:27 +0300 | [diff] [blame] | 304 | } |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 305 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 308 | EXPORT_SYMBOL(ocelot_vlan_add); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 309 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 310 | int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) |
Vladimir Oltean | 9855934 | 2019-11-09 15:02:48 +0200 | [diff] [blame] | 311 | { |
| 312 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 313 | int ret; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 314 | |
| 315 | /* Stop the port from being a member of the vlan */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 316 | ocelot->vlan_mask[vid] &= ~BIT(port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 317 | ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 321 | /* Ingress */ |
| 322 | if (ocelot_port->pvid_vlan.vid == vid) { |
| 323 | struct ocelot_vlan pvid_vlan = {0}; |
| 324 | |
| 325 | ocelot_port_set_pvid(ocelot, port, pvid_vlan); |
| 326 | } |
| 327 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 328 | /* Egress */ |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 329 | if (ocelot_port->native_vlan.vid == vid) { |
Vladimir Oltean | e2b2e83 | 2020-10-31 12:29:13 +0200 | [diff] [blame] | 330 | struct ocelot_vlan native_vlan = {0}; |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 331 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 332 | ocelot_port_set_native_vlan(ocelot, port, native_vlan); |
| 333 | } |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 337 | EXPORT_SYMBOL(ocelot_vlan_del); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 338 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 339 | static void ocelot_vlan_init(struct ocelot *ocelot) |
| 340 | { |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 341 | u16 port, vid; |
| 342 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 343 | /* Clear VLAN table, by default all ports are members of all VLANs */ |
| 344 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, |
| 345 | ANA_TABLES_VLANACCESS); |
| 346 | ocelot_vlant_wait_for_completion(ocelot); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 347 | |
| 348 | /* Configure the port VLAN memberships */ |
| 349 | for (vid = 1; vid < VLAN_N_VID; vid++) { |
| 350 | ocelot->vlan_mask[vid] = 0; |
| 351 | ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 352 | } |
| 353 | |
| 354 | /* Because VLAN filtering is enabled, we need VID 0 to get untagged |
| 355 | * traffic. It is added automatically if 8021q module is loaded, but |
| 356 | * we can't rely on it since module may be not loaded. |
| 357 | */ |
| 358 | ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); |
| 359 | ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); |
| 360 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 361 | /* Set vlan ingress filter mask to all ports but the CPU port by |
| 362 | * default. |
| 363 | */ |
Vladimir Oltean | 714d0ff | 2019-11-09 15:02:55 +0200 | [diff] [blame] | 364 | ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 365 | ANA_VLANMASK); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 366 | |
| 367 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 368 | ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); |
| 369 | ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); |
| 370 | } |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 373 | static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) |
| 374 | { |
| 375 | return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); |
| 376 | } |
| 377 | |
| 378 | int ocelot_port_flush(struct ocelot *ocelot, int port) |
| 379 | { |
| 380 | int err, val; |
| 381 | |
| 382 | /* Disable dequeuing from the egress queues */ |
| 383 | ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, |
| 384 | QSYS_PORT_MODE_DEQUEUE_DIS, |
| 385 | QSYS_PORT_MODE, port); |
| 386 | |
| 387 | /* Disable flow control */ |
| 388 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); |
| 389 | |
| 390 | /* Disable priority flow control */ |
| 391 | ocelot_fields_write(ocelot, port, |
| 392 | QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); |
| 393 | |
| 394 | /* Wait at least the time it takes to receive a frame of maximum length |
| 395 | * at the port. |
| 396 | * Worst-case delays for 10 kilobyte jumbo frames are: |
| 397 | * 8 ms on a 10M port |
| 398 | * 800 μs on a 100M port |
| 399 | * 80 μs on a 1G port |
| 400 | * 32 μs on a 2.5G port |
| 401 | */ |
| 402 | usleep_range(8000, 10000); |
| 403 | |
| 404 | /* Disable half duplex backpressure. */ |
| 405 | ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, |
| 406 | SYS_FRONT_PORT_MODE, port); |
| 407 | |
| 408 | /* Flush the queues associated with the port. */ |
| 409 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, |
| 410 | REW_PORT_CFG, port); |
| 411 | |
| 412 | /* Enable dequeuing from the egress queues. */ |
| 413 | ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, |
| 414 | port); |
| 415 | |
| 416 | /* Wait until flushing is complete. */ |
| 417 | err = read_poll_timeout(ocelot_read_eq_avail, val, !val, |
| 418 | 100, 2000000, false, ocelot, port); |
| 419 | |
| 420 | /* Clear flushing again. */ |
| 421 | ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); |
| 422 | |
| 423 | return err; |
| 424 | } |
| 425 | EXPORT_SYMBOL(ocelot_port_flush); |
| 426 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 427 | void ocelot_adjust_link(struct ocelot *ocelot, int port, |
| 428 | struct phy_device *phydev) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 429 | { |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 430 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 431 | int speed, mode = 0; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 432 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 433 | switch (phydev->speed) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 434 | case SPEED_10: |
| 435 | speed = OCELOT_SPEED_10; |
| 436 | break; |
| 437 | case SPEED_100: |
| 438 | speed = OCELOT_SPEED_100; |
| 439 | break; |
| 440 | case SPEED_1000: |
| 441 | speed = OCELOT_SPEED_1000; |
| 442 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 443 | break; |
| 444 | case SPEED_2500: |
| 445 | speed = OCELOT_SPEED_2500; |
| 446 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 447 | break; |
| 448 | default: |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 449 | dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n", |
| 450 | port, phydev->speed); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 454 | phy_print_status(phydev); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 455 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 456 | if (!phydev->link) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 457 | return; |
| 458 | |
| 459 | /* Only full duplex supported for now */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 460 | ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 461 | mode, DEV_MAC_MODE_CFG); |
| 462 | |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 463 | /* Disable HDX fast control */ |
| 464 | ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, |
| 465 | DEV_PORT_MISC); |
| 466 | |
| 467 | /* SGMII only for now */ |
| 468 | ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, |
| 469 | PCS1G_MODE_CFG); |
| 470 | ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); |
| 471 | |
| 472 | /* Enable PCS */ |
| 473 | ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); |
| 474 | |
| 475 | /* No aneg on SGMII */ |
| 476 | ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); |
| 477 | |
| 478 | /* No loopback */ |
| 479 | ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 480 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 481 | /* Enable MAC module */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 482 | ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 483 | DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); |
| 484 | |
| 485 | /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of |
| 486 | * reset */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 487 | ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed), |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 488 | DEV_CLOCK_CFG); |
| 489 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 490 | /* No PFC */ |
| 491 | ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 492 | ANA_PFC_PFC_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 493 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 494 | /* Core: Enable port for frame transfer */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 495 | ocelot_fields_write(ocelot, port, |
| 496 | QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 497 | |
| 498 | /* Flow control */ |
| 499 | ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | |
| 500 | SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | |
| 501 | SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | |
| 502 | SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | |
| 503 | SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 504 | SYS_MAC_FC_CFG, port); |
| 505 | ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 506 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 507 | EXPORT_SYMBOL(ocelot_adjust_link); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 508 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 509 | void ocelot_port_enable(struct ocelot *ocelot, int port, |
| 510 | struct phy_device *phy) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 511 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 512 | /* Enable receiving frames on the port, and activate auto-learning of |
| 513 | * MAC addresses. |
| 514 | */ |
| 515 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | |
| 516 | ANA_PORT_PORT_CFG_RECV_ENA | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 517 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 518 | ANA_PORT_PORT_CFG, port); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 519 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 520 | EXPORT_SYMBOL(ocelot_port_enable); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 521 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 522 | void ocelot_port_disable(struct ocelot *ocelot, int port) |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 523 | { |
| 524 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 525 | |
| 526 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 527 | ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 528 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 529 | EXPORT_SYMBOL(ocelot_port_disable); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 530 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 531 | void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, |
| 532 | struct sk_buff *clone) |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 533 | { |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 534 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 535 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 536 | spin_lock(&ocelot_port->ts_id_lock); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 537 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 538 | skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; |
| 539 | /* Store timestamp ID in cb[0] of sk_buff */ |
| 540 | clone->cb[0] = ocelot_port->ts_id; |
| 541 | ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; |
| 542 | skb_queue_tail(&ocelot_port->tx_skbs, clone); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 543 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 544 | spin_unlock(&ocelot_port->ts_id_lock); |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 545 | } |
| 546 | EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb); |
| 547 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 548 | static void ocelot_get_hwtimestamp(struct ocelot *ocelot, |
| 549 | struct timespec64 *ts) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 550 | { |
| 551 | unsigned long flags; |
| 552 | u32 val; |
| 553 | |
| 554 | spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); |
| 555 | |
| 556 | /* Read current PTP time to get seconds */ |
| 557 | val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); |
| 558 | |
| 559 | val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); |
| 560 | val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); |
| 561 | ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); |
| 562 | ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); |
| 563 | |
| 564 | /* Read packet HW timestamp from FIFO */ |
| 565 | val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); |
| 566 | ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); |
| 567 | |
| 568 | /* Sec has incremented since the ts was registered */ |
| 569 | if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) |
| 570 | ts->tv_sec--; |
| 571 | |
| 572 | spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); |
| 573 | } |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 574 | |
| 575 | void ocelot_get_txtstamp(struct ocelot *ocelot) |
| 576 | { |
| 577 | int budget = OCELOT_PTP_QUEUE_SZ; |
| 578 | |
| 579 | while (budget--) { |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 580 | struct sk_buff *skb, *skb_tmp, *skb_match = NULL; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 581 | struct skb_shared_hwtstamps shhwtstamps; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 582 | struct ocelot_port *port; |
| 583 | struct timespec64 ts; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 584 | unsigned long flags; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 585 | u32 val, id, txport; |
| 586 | |
| 587 | val = ocelot_read(ocelot, SYS_PTP_STATUS); |
| 588 | |
| 589 | /* Check if a timestamp can be retrieved */ |
| 590 | if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) |
| 591 | break; |
| 592 | |
| 593 | WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); |
| 594 | |
| 595 | /* Retrieve the ts ID and Tx port */ |
| 596 | id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); |
| 597 | txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); |
| 598 | |
| 599 | /* Retrieve its associated skb */ |
| 600 | port = ocelot->ports[txport]; |
| 601 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 602 | spin_lock_irqsave(&port->tx_skbs.lock, flags); |
| 603 | |
| 604 | skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { |
| 605 | if (skb->cb[0] != id) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 606 | continue; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 607 | __skb_unlink(skb, &port->tx_skbs); |
| 608 | skb_match = skb; |
Yangbo Lu | fc62c09 | 2019-11-27 15:27:56 +0800 | [diff] [blame] | 609 | break; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 610 | } |
| 611 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 612 | spin_unlock_irqrestore(&port->tx_skbs.lock, flags); |
| 613 | |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 614 | /* Get the h/w timestamp */ |
| 615 | ocelot_get_hwtimestamp(ocelot, &ts); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 616 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 617 | if (unlikely(!skb_match)) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 618 | continue; |
| 619 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 620 | /* Set the timestamp into the skb */ |
| 621 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 622 | shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 623 | skb_complete_tx_timestamp(skb_match, &shhwtstamps); |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 624 | |
| 625 | /* Next ts */ |
| 626 | ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | EXPORT_SYMBOL(ocelot_get_txtstamp); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 630 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 631 | int ocelot_fdb_add(struct ocelot *ocelot, int port, |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 632 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 633 | { |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 634 | int pgid = port; |
| 635 | |
| 636 | if (port == ocelot->npi) |
| 637 | pgid = PGID_CPU; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 638 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 639 | return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 640 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 641 | EXPORT_SYMBOL(ocelot_fdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 642 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 643 | int ocelot_fdb_del(struct ocelot *ocelot, int port, |
| 644 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 645 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 646 | return ocelot_mact_forget(ocelot, addr, vid); |
| 647 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 648 | EXPORT_SYMBOL(ocelot_fdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 649 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 650 | int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 651 | bool is_static, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 652 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 653 | struct ocelot_dump_ctx *dump = data; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 654 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 655 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 656 | struct nlmsghdr *nlh; |
| 657 | struct ndmsg *ndm; |
| 658 | |
| 659 | if (dump->idx < dump->cb->args[2]) |
| 660 | goto skip; |
| 661 | |
| 662 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 663 | sizeof(*ndm), NLM_F_MULTI); |
| 664 | if (!nlh) |
| 665 | return -EMSGSIZE; |
| 666 | |
| 667 | ndm = nlmsg_data(nlh); |
| 668 | ndm->ndm_family = AF_BRIDGE; |
| 669 | ndm->ndm_pad1 = 0; |
| 670 | ndm->ndm_pad2 = 0; |
| 671 | ndm->ndm_flags = NTF_SELF; |
| 672 | ndm->ndm_type = 0; |
| 673 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 674 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 675 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 676 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 677 | goto nla_put_failure; |
| 678 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 679 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 680 | goto nla_put_failure; |
| 681 | |
| 682 | nlmsg_end(dump->skb, nlh); |
| 683 | |
| 684 | skip: |
| 685 | dump->idx++; |
| 686 | return 0; |
| 687 | |
| 688 | nla_put_failure: |
| 689 | nlmsg_cancel(dump->skb, nlh); |
| 690 | return -EMSGSIZE; |
| 691 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 692 | EXPORT_SYMBOL(ocelot_port_fdb_do_dump); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 693 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 694 | static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, |
| 695 | struct ocelot_mact_entry *entry) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 696 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 697 | u32 val, dst, macl, mach; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 698 | char mac[ETH_ALEN]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 699 | |
| 700 | /* Set row and column to read from */ |
| 701 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); |
| 702 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); |
| 703 | |
| 704 | /* Issue a read command */ |
| 705 | ocelot_write(ocelot, |
| 706 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 707 | ANA_TABLES_MACACCESS); |
| 708 | |
| 709 | if (ocelot_mact_wait_for_completion(ocelot)) |
| 710 | return -ETIMEDOUT; |
| 711 | |
| 712 | /* Read the entry flags */ |
| 713 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 714 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 715 | return -EINVAL; |
| 716 | |
| 717 | /* If the entry read has another port configured as its destination, |
| 718 | * do not report it. |
| 719 | */ |
| 720 | dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 721 | if (dst != port) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 722 | return -EINVAL; |
| 723 | |
| 724 | /* Get the entry's MAC address and VLAN id */ |
| 725 | macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); |
| 726 | mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); |
| 727 | |
| 728 | mac[0] = (mach >> 8) & 0xff; |
| 729 | mac[1] = (mach >> 0) & 0xff; |
| 730 | mac[2] = (macl >> 24) & 0xff; |
| 731 | mac[3] = (macl >> 16) & 0xff; |
| 732 | mac[4] = (macl >> 8) & 0xff; |
| 733 | mac[5] = (macl >> 0) & 0xff; |
| 734 | |
| 735 | entry->vid = (mach >> 16) & 0xfff; |
| 736 | ether_addr_copy(entry->mac, mac); |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 741 | int ocelot_fdb_dump(struct ocelot *ocelot, int port, |
| 742 | dsa_fdb_dump_cb_t *cb, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 743 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 744 | int i, j; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 745 | |
Vladimir Oltean | 21ce7f3 | 2020-05-04 01:20:26 +0300 | [diff] [blame] | 746 | /* Loop through all the mac tables entries. */ |
| 747 | for (i = 0; i < ocelot->num_mact_rows; i++) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 748 | for (j = 0; j < 4; j++) { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 749 | struct ocelot_mact_entry entry; |
| 750 | bool is_static; |
| 751 | int ret; |
| 752 | |
| 753 | ret = ocelot_mact_read(ocelot, port, i, j, &entry); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 754 | /* If the entry is invalid (wrong port, invalid...), |
| 755 | * skip it. |
| 756 | */ |
| 757 | if (ret == -EINVAL) |
| 758 | continue; |
| 759 | else if (ret) |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 760 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 761 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 762 | is_static = (entry.type == ENTRYTYPE_LOCKED); |
| 763 | |
| 764 | ret = cb(entry.mac, entry.vid, is_static, data); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 765 | if (ret) |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 766 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 770 | return 0; |
| 771 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 772 | EXPORT_SYMBOL(ocelot_fdb_dump); |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 773 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 774 | int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 775 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 776 | return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, |
| 777 | sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; |
| 778 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 779 | EXPORT_SYMBOL(ocelot_hwstamp_get); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 780 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 781 | int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 782 | { |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 783 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 784 | struct hwtstamp_config cfg; |
| 785 | |
| 786 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 787 | return -EFAULT; |
| 788 | |
| 789 | /* reserved for future extensions */ |
| 790 | if (cfg.flags) |
| 791 | return -EINVAL; |
| 792 | |
| 793 | /* Tx type sanity check */ |
| 794 | switch (cfg.tx_type) { |
| 795 | case HWTSTAMP_TX_ON: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 796 | ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 797 | break; |
| 798 | case HWTSTAMP_TX_ONESTEP_SYNC: |
| 799 | /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we |
| 800 | * need to update the origin time. |
| 801 | */ |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 802 | ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 803 | break; |
| 804 | case HWTSTAMP_TX_OFF: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 805 | ocelot_port->ptp_cmd = 0; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 806 | break; |
| 807 | default: |
| 808 | return -ERANGE; |
| 809 | } |
| 810 | |
| 811 | mutex_lock(&ocelot->ptp_lock); |
| 812 | |
| 813 | switch (cfg.rx_filter) { |
| 814 | case HWTSTAMP_FILTER_NONE: |
| 815 | break; |
| 816 | case HWTSTAMP_FILTER_ALL: |
| 817 | case HWTSTAMP_FILTER_SOME: |
| 818 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 819 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 820 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
| 821 | case HWTSTAMP_FILTER_NTP_ALL: |
| 822 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 823 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 824 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 825 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 826 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 827 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 828 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 829 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 830 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
| 831 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 832 | break; |
| 833 | default: |
| 834 | mutex_unlock(&ocelot->ptp_lock); |
| 835 | return -ERANGE; |
| 836 | } |
| 837 | |
| 838 | /* Commit back the result & save it */ |
| 839 | memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); |
| 840 | mutex_unlock(&ocelot->ptp_lock); |
| 841 | |
| 842 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 843 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 844 | EXPORT_SYMBOL(ocelot_hwstamp_set); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 845 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 846 | 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] | 847 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 848 | int i; |
| 849 | |
| 850 | if (sset != ETH_SS_STATS) |
| 851 | return; |
| 852 | |
| 853 | for (i = 0; i < ocelot->num_stats; i++) |
| 854 | memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, |
| 855 | ETH_GSTRING_LEN); |
| 856 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 857 | EXPORT_SYMBOL(ocelot_get_strings); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 858 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 859 | static void ocelot_update_stats(struct ocelot *ocelot) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 860 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 861 | int i, j; |
| 862 | |
| 863 | mutex_lock(&ocelot->stats_lock); |
| 864 | |
| 865 | for (i = 0; i < ocelot->num_phys_ports; i++) { |
| 866 | /* Configure the port to read the stats from */ |
| 867 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); |
| 868 | |
| 869 | for (j = 0; j < ocelot->num_stats; j++) { |
| 870 | u32 val; |
| 871 | unsigned int idx = i * ocelot->num_stats + j; |
| 872 | |
| 873 | val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, |
| 874 | ocelot->stats_layout[j].offset); |
| 875 | |
| 876 | if (val < (ocelot->stats[idx] & U32_MAX)) |
| 877 | ocelot->stats[idx] += (u64)1 << 32; |
| 878 | |
| 879 | ocelot->stats[idx] = (ocelot->stats[idx] & |
| 880 | ~(u64)U32_MAX) + val; |
| 881 | } |
| 882 | } |
| 883 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 884 | mutex_unlock(&ocelot->stats_lock); |
| 885 | } |
| 886 | |
| 887 | static void ocelot_check_stats_work(struct work_struct *work) |
| 888 | { |
| 889 | struct delayed_work *del_work = to_delayed_work(work); |
| 890 | struct ocelot *ocelot = container_of(del_work, struct ocelot, |
| 891 | stats_work); |
| 892 | |
| 893 | ocelot_update_stats(ocelot); |
| 894 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 895 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 896 | OCELOT_STATS_CHECK_DELAY); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 899 | void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 900 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 901 | int i; |
| 902 | |
| 903 | /* check and update now */ |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 904 | ocelot_update_stats(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 905 | |
| 906 | /* Copy all counters */ |
| 907 | for (i = 0; i < ocelot->num_stats; i++) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 908 | *data++ = ocelot->stats[port * ocelot->num_stats + i]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 909 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 910 | EXPORT_SYMBOL(ocelot_get_ethtool_stats); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 911 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 912 | int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 913 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 914 | if (sset != ETH_SS_STATS) |
| 915 | return -EOPNOTSUPP; |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 916 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 917 | return ocelot->num_stats; |
| 918 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 919 | EXPORT_SYMBOL(ocelot_get_sset_count); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 920 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 921 | int ocelot_get_ts_info(struct ocelot *ocelot, int port, |
| 922 | struct ethtool_ts_info *info) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 923 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 924 | info->phc_index = ocelot->ptp_clock ? |
| 925 | ptp_clock_index(ocelot->ptp_clock) : -1; |
Yangbo Lu | d2b09a8 | 2020-04-20 10:46:46 +0800 | [diff] [blame] | 926 | if (info->phc_index == -1) { |
| 927 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 928 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 929 | SOF_TIMESTAMPING_SOFTWARE; |
| 930 | return 0; |
| 931 | } |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 932 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 933 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 934 | SOF_TIMESTAMPING_SOFTWARE | |
| 935 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 936 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 937 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 938 | info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | |
| 939 | BIT(HWTSTAMP_TX_ONESTEP_SYNC); |
| 940 | info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); |
| 941 | |
| 942 | return 0; |
| 943 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 944 | EXPORT_SYMBOL(ocelot_get_ts_info); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 945 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 946 | static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond, |
| 947 | bool only_active_ports) |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 948 | { |
| 949 | u32 mask = 0; |
| 950 | int port; |
| 951 | |
| 952 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 953 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 954 | |
| 955 | if (!ocelot_port) |
| 956 | continue; |
| 957 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 958 | if (ocelot_port->bond == bond) { |
| 959 | if (only_active_ports && !ocelot_port->lag_tx_active) |
| 960 | continue; |
| 961 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 962 | mask |= BIT(port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 963 | } |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | return mask; |
| 967 | } |
| 968 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 969 | static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 970 | { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 971 | u32 mask = 0; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 972 | int port; |
| 973 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 974 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 975 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 976 | |
| 977 | if (!ocelot_port) |
| 978 | continue; |
| 979 | |
| 980 | if (ocelot_port->is_dsa_8021q_cpu) |
| 981 | mask |= BIT(port); |
| 982 | } |
| 983 | |
| 984 | return mask; |
| 985 | } |
| 986 | |
| 987 | void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) |
| 988 | { |
| 989 | unsigned long cpu_fwd_mask; |
| 990 | int port; |
| 991 | |
| 992 | /* If a DSA tag_8021q CPU exists, it needs to be included in the |
| 993 | * regular forwarding path of the front ports regardless of whether |
| 994 | * those are bridged or standalone. |
| 995 | * If DSA tag_8021q is not used, this returns 0, which is fine because |
| 996 | * the hardware-based CPU port module can be a destination for packets |
| 997 | * even if it isn't part of PGID_SRC. |
| 998 | */ |
| 999 | cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); |
| 1000 | |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1001 | /* Apply FWD mask. The loop is needed to add/remove the current port as |
| 1002 | * a source for the other ports. |
| 1003 | */ |
| 1004 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1005 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1006 | unsigned long mask; |
| 1007 | |
| 1008 | if (!ocelot_port) { |
| 1009 | /* Unused ports can't send anywhere */ |
| 1010 | mask = 0; |
| 1011 | } else if (ocelot_port->is_dsa_8021q_cpu) { |
| 1012 | /* The DSA tag_8021q CPU ports need to be able to |
| 1013 | * forward packets to all other ports except for |
| 1014 | * themselves |
| 1015 | */ |
| 1016 | mask = GENMASK(ocelot->num_phys_ports - 1, 0); |
| 1017 | mask &= ~cpu_fwd_mask; |
| 1018 | } else if (ocelot->bridge_fwd_mask & BIT(port)) { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1019 | struct net_device *bond = ocelot_port->bond; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1020 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1021 | mask = ocelot->bridge_fwd_mask & ~BIT(port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1022 | if (bond) { |
| 1023 | mask &= ~ocelot_get_bond_mask(ocelot, bond, |
| 1024 | false); |
| 1025 | } |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1026 | } else { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1027 | /* Standalone ports forward only to DSA tag_8021q CPU |
| 1028 | * ports (if those exist), or to the hardware CPU port |
| 1029 | * module otherwise. |
| 1030 | */ |
| 1031 | mask = cpu_fwd_mask; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1032 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1033 | |
| 1034 | ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1035 | } |
| 1036 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1037 | EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1038 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame^] | 1041 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1042 | u32 port_cfg; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1043 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1044 | if (!(BIT(port) & ocelot->bridge_mask)) |
| 1045 | return; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1046 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1047 | port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1048 | |
| 1049 | switch (state) { |
| 1050 | case BR_STATE_FORWARDING: |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1051 | ocelot->bridge_fwd_mask |= BIT(port); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1052 | fallthrough; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1053 | case BR_STATE_LEARNING: |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame^] | 1054 | if (ocelot_port->learn_ena) |
| 1055 | port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1056 | break; |
| 1057 | |
| 1058 | default: |
| 1059 | port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1060 | ocelot->bridge_fwd_mask &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1064 | ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1065 | |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1066 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1067 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1068 | EXPORT_SYMBOL(ocelot_bridge_stp_state_set); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1069 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1070 | void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1071 | { |
Vladimir Oltean | c0d7ecc | 2020-05-04 01:20:27 +0300 | [diff] [blame] | 1072 | unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); |
| 1073 | |
| 1074 | /* Setting AGE_PERIOD to zero effectively disables automatic aging, |
| 1075 | * which is clearly not what our intention is. So avoid that. |
| 1076 | */ |
| 1077 | if (!age_period) |
| 1078 | age_period = 1; |
| 1079 | |
| 1080 | ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1081 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1082 | EXPORT_SYMBOL(ocelot_set_ageing_time); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1083 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1084 | static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, |
| 1085 | const unsigned char *addr, |
| 1086 | u16 vid) |
| 1087 | { |
| 1088 | struct ocelot_multicast *mc; |
| 1089 | |
| 1090 | list_for_each_entry(mc, &ocelot->multicast, list) { |
| 1091 | if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) |
| 1092 | return mc; |
| 1093 | } |
| 1094 | |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1098 | static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) |
| 1099 | { |
| 1100 | if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) |
| 1101 | return ENTRYTYPE_MACv4; |
| 1102 | if (addr[0] == 0x33 && addr[1] == 0x33) |
| 1103 | return ENTRYTYPE_MACv6; |
Vladimir Oltean | 7c31314 | 2020-10-29 04:27:34 +0200 | [diff] [blame] | 1104 | return ENTRYTYPE_LOCKED; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1105 | } |
| 1106 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1107 | static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, |
| 1108 | unsigned long ports) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1109 | { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1110 | struct ocelot_pgid *pgid; |
| 1111 | |
| 1112 | pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); |
| 1113 | if (!pgid) |
| 1114 | return ERR_PTR(-ENOMEM); |
| 1115 | |
| 1116 | pgid->ports = ports; |
| 1117 | pgid->index = index; |
| 1118 | refcount_set(&pgid->refcount, 1); |
| 1119 | list_add_tail(&pgid->list, &ocelot->pgids); |
| 1120 | |
| 1121 | return pgid; |
| 1122 | } |
| 1123 | |
| 1124 | static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) |
| 1125 | { |
| 1126 | if (!refcount_dec_and_test(&pgid->refcount)) |
| 1127 | return; |
| 1128 | |
| 1129 | list_del(&pgid->list); |
| 1130 | kfree(pgid); |
| 1131 | } |
| 1132 | |
| 1133 | static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, |
| 1134 | const struct ocelot_multicast *mc) |
| 1135 | { |
| 1136 | struct ocelot_pgid *pgid; |
| 1137 | int index; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1138 | |
| 1139 | /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and |
| 1140 | * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the |
| 1141 | * destination mask table (PGID), the destination set is programmed as |
| 1142 | * part of the entry MAC address.", and the DEST_IDX is set to 0. |
| 1143 | */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1144 | if (mc->entry_type == ENTRYTYPE_MACv4 || |
| 1145 | mc->entry_type == ENTRYTYPE_MACv6) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1146 | return ocelot_pgid_alloc(ocelot, 0, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1147 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1148 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 1149 | /* When searching for a nonreserved multicast PGID, ignore the |
| 1150 | * dummy PGID of zero that we have for MACv4/MACv6 entries |
| 1151 | */ |
| 1152 | if (pgid->index && pgid->ports == mc->ports) { |
| 1153 | refcount_inc(&pgid->refcount); |
| 1154 | return pgid; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* Search for a free index in the nonreserved multicast PGID area */ |
| 1159 | for_each_nonreserved_multicast_dest_pgid(ocelot, index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1160 | bool used = false; |
| 1161 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1162 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 1163 | if (pgid->index == index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1164 | used = true; |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | if (!used) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1170 | return ocelot_pgid_alloc(ocelot, index, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1171 | } |
| 1172 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1173 | return ERR_PTR(-ENOSPC); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static void ocelot_encode_ports_to_mdb(unsigned char *addr, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1177 | struct ocelot_multicast *mc) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1178 | { |
Vladimir Oltean | ebbd860 | 2020-10-29 04:27:35 +0200 | [diff] [blame] | 1179 | ether_addr_copy(addr, mc->addr); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1180 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1181 | if (mc->entry_type == ENTRYTYPE_MACv4) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1182 | addr[0] = 0; |
| 1183 | addr[1] = mc->ports >> 8; |
| 1184 | addr[2] = mc->ports & 0xff; |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1185 | } else if (mc->entry_type == ENTRYTYPE_MACv6) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1186 | addr[0] = mc->ports >> 8; |
| 1187 | addr[1] = mc->ports & 0xff; |
| 1188 | } |
| 1189 | } |
| 1190 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1191 | int ocelot_port_mdb_add(struct ocelot *ocelot, int port, |
| 1192 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1193 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1194 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1195 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1196 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1197 | u16 vid = mdb->vid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1198 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1199 | if (port == ocelot->npi) |
| 1200 | port = ocelot->num_phys_ports; |
| 1201 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1202 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1203 | if (!mc) { |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 1204 | /* New entry */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1205 | mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); |
| 1206 | if (!mc) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | mc->entry_type = ocelot_classify_mdb(mdb->addr); |
| 1210 | ether_addr_copy(mc->addr, mdb->addr); |
| 1211 | mc->vid = vid; |
| 1212 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1213 | list_add_tail(&mc->list, &ocelot->multicast); |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 1214 | } else { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1215 | /* Existing entry. Clean up the current port mask from |
| 1216 | * hardware now, because we'll be modifying it. |
| 1217 | */ |
| 1218 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1219 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1220 | ocelot_mact_forget(ocelot, addr, vid); |
| 1221 | } |
| 1222 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1223 | mc->ports |= BIT(port); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1224 | |
| 1225 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 1226 | if (IS_ERR(pgid)) { |
| 1227 | dev_err(ocelot->dev, |
| 1228 | "Cannot allocate PGID for mdb %pM vid %d\n", |
| 1229 | mc->addr, mc->vid); |
| 1230 | devm_kfree(ocelot->dev, mc); |
| 1231 | return PTR_ERR(pgid); |
| 1232 | } |
| 1233 | mc->pgid = pgid; |
| 1234 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1235 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1236 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1237 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 1238 | mc->entry_type != ENTRYTYPE_MACv6) |
| 1239 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 1240 | pgid->index); |
| 1241 | |
| 1242 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1243 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1244 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1245 | EXPORT_SYMBOL(ocelot_port_mdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1246 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1247 | int ocelot_port_mdb_del(struct ocelot *ocelot, int port, |
| 1248 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1249 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1250 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1251 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1252 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1253 | u16 vid = mdb->vid; |
| 1254 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1255 | if (port == ocelot->npi) |
| 1256 | port = ocelot->num_phys_ports; |
| 1257 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1258 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1259 | if (!mc) |
| 1260 | return -ENOENT; |
| 1261 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1262 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1263 | ocelot_mact_forget(ocelot, addr, vid); |
| 1264 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1265 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1266 | mc->ports &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1267 | if (!mc->ports) { |
| 1268 | list_del(&mc->list); |
| 1269 | devm_kfree(ocelot->dev, mc); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1273 | /* We have a PGID with fewer ports now */ |
| 1274 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 1275 | if (IS_ERR(pgid)) |
| 1276 | return PTR_ERR(pgid); |
| 1277 | mc->pgid = pgid; |
| 1278 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1279 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1280 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1281 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 1282 | mc->entry_type != ENTRYTYPE_MACv6) |
| 1283 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 1284 | pgid->index); |
| 1285 | |
| 1286 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1287 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1288 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1289 | EXPORT_SYMBOL(ocelot_port_mdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1290 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1291 | int ocelot_port_bridge_join(struct ocelot *ocelot, int port, |
| 1292 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1293 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1294 | if (!ocelot->bridge_mask) { |
| 1295 | ocelot->hw_bridge_dev = bridge; |
| 1296 | } else { |
| 1297 | if (ocelot->hw_bridge_dev != bridge) |
| 1298 | /* This is adding the port to a second bridge, this is |
| 1299 | * unsupported */ |
| 1300 | return -ENODEV; |
| 1301 | } |
| 1302 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1303 | ocelot->bridge_mask |= BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1304 | |
| 1305 | return 0; |
| 1306 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1307 | EXPORT_SYMBOL(ocelot_port_bridge_join); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1308 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1309 | int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, |
| 1310 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1311 | { |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 1312 | struct ocelot_vlan pvid = {0}, native_vlan = {0}; |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 1313 | int ret; |
| 1314 | |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 1315 | ocelot->bridge_mask &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1316 | |
| 1317 | if (!ocelot->bridge_mask) |
| 1318 | ocelot->hw_bridge_dev = NULL; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 1319 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 1320 | ret = ocelot_port_vlan_filtering(ocelot, port, false); |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 1321 | if (ret) |
| 1322 | return ret; |
| 1323 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 1324 | ocelot_port_set_pvid(ocelot, port, pvid); |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 1325 | ocelot_port_set_native_vlan(ocelot, port, native_vlan); |
| 1326 | |
| 1327 | return 0; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1328 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1329 | EXPORT_SYMBOL(ocelot_port_bridge_leave); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1330 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1331 | static void ocelot_set_aggr_pgids(struct ocelot *ocelot) |
| 1332 | { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1333 | unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1334 | int i, port, lag; |
| 1335 | |
| 1336 | /* Reset destination and aggregation PGIDS */ |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1337 | for_each_unicast_dest_pgid(ocelot, port) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1338 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 1339 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1340 | for_each_aggr_pgid(ocelot, i) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1341 | ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 1342 | ANA_PGID_PGID, i); |
| 1343 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1344 | /* The visited ports bitmask holds the list of ports offloading any |
| 1345 | * bonding interface. Initially we mark all these ports as unvisited, |
| 1346 | * then every time we visit a port in this bitmask, we know that it is |
| 1347 | * the lowest numbered port, i.e. the one whose logical ID == physical |
| 1348 | * port ID == LAG ID. So we mark as visited all further ports in the |
| 1349 | * bitmask that are offloading the same bonding interface. This way, |
| 1350 | * we set up the aggregation PGIDs only once per bonding interface. |
| 1351 | */ |
| 1352 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1353 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1354 | |
| 1355 | if (!ocelot_port || !ocelot_port->bond) |
| 1356 | continue; |
| 1357 | |
| 1358 | visited &= ~BIT(port); |
| 1359 | } |
| 1360 | |
| 1361 | /* Now, set PGIDs for each active LAG */ |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1362 | for (lag = 0; lag < ocelot->num_phys_ports; lag++) { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1363 | struct net_device *bond = ocelot->ports[lag]->bond; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1364 | int num_active_ports = 0; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1365 | unsigned long bond_mask; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1366 | u8 aggr_idx[16]; |
| 1367 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1368 | if (!bond || (visited & BIT(lag))) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1369 | continue; |
| 1370 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1371 | bond_mask = ocelot_get_bond_mask(ocelot, bond, true); |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1372 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1373 | for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { |
| 1374 | // Destination mask |
| 1375 | ocelot_write_rix(ocelot, bond_mask, |
| 1376 | ANA_PGID_PGID, port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1377 | aggr_idx[num_active_ports++] = port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1380 | for_each_aggr_pgid(ocelot, i) { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1381 | u32 ac; |
| 1382 | |
| 1383 | ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); |
| 1384 | ac &= ~bond_mask; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1385 | /* Don't do division by zero if there was no active |
| 1386 | * port. Just make all aggregation codes zero. |
| 1387 | */ |
| 1388 | if (num_active_ports) |
| 1389 | ac |= BIT(aggr_idx[i % num_active_ports]); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1390 | ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); |
| 1391 | } |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1392 | |
| 1393 | /* Mark all ports in the same LAG as visited to avoid applying |
| 1394 | * the same config again. |
| 1395 | */ |
| 1396 | for (port = lag; port < ocelot->num_phys_ports; port++) { |
| 1397 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1398 | |
| 1399 | if (!ocelot_port) |
| 1400 | continue; |
| 1401 | |
| 1402 | if (ocelot_port->bond == bond) |
| 1403 | visited |= BIT(port); |
| 1404 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1408 | /* When offloading a bonding interface, the switch ports configured under the |
| 1409 | * same bond must have the same logical port ID, equal to the physical port ID |
| 1410 | * of the lowest numbered physical port in that bond. Otherwise, in standalone/ |
| 1411 | * bridged mode, each port has a logical port ID equal to its physical port ID. |
| 1412 | */ |
| 1413 | static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1414 | { |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1415 | int port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1416 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1417 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1418 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1419 | struct net_device *bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1420 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1421 | if (!ocelot_port) |
| 1422 | continue; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1423 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1424 | bond = ocelot_port->bond; |
| 1425 | if (bond) { |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1426 | int lag = __ffs(ocelot_get_bond_mask(ocelot, bond, |
| 1427 | false)); |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1428 | |
| 1429 | ocelot_rmw_gix(ocelot, |
| 1430 | ANA_PORT_PORT_CFG_PORTID_VAL(lag), |
| 1431 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 1432 | ANA_PORT_PORT_CFG, port); |
| 1433 | } else { |
| 1434 | ocelot_rmw_gix(ocelot, |
| 1435 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 1436 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 1437 | ANA_PORT_PORT_CFG, port); |
| 1438 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1442 | int ocelot_port_lag_join(struct ocelot *ocelot, int port, |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 1443 | struct net_device *bond, |
| 1444 | struct netdev_lag_upper_info *info) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1445 | { |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 1446 | if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) |
| 1447 | return -EOPNOTSUPP; |
| 1448 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1449 | ocelot->ports[port]->bond = bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1450 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1451 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1452 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1453 | ocelot_set_aggr_pgids(ocelot); |
| 1454 | |
| 1455 | return 0; |
| 1456 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1457 | EXPORT_SYMBOL(ocelot_port_lag_join); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1458 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1459 | void ocelot_port_lag_leave(struct ocelot *ocelot, int port, |
| 1460 | struct net_device *bond) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1461 | { |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1462 | ocelot->ports[port]->bond = NULL; |
| 1463 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1464 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1465 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1466 | ocelot_set_aggr_pgids(ocelot); |
| 1467 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1468 | EXPORT_SYMBOL(ocelot_port_lag_leave); |
Petr Machata | 0e332c8 | 2018-11-22 23:30:11 +0000 | [diff] [blame] | 1469 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1470 | void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) |
| 1471 | { |
| 1472 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1473 | |
| 1474 | ocelot_port->lag_tx_active = lag_tx_active; |
| 1475 | |
| 1476 | /* Rebalance the LAGs */ |
| 1477 | ocelot_set_aggr_pgids(ocelot); |
| 1478 | } |
| 1479 | EXPORT_SYMBOL(ocelot_port_lag_change); |
| 1480 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1481 | /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. |
| 1482 | * 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] | 1483 | * In the special case that it's the NPI port that we're configuring, the |
| 1484 | * length of the tag and optional prefix needs to be accounted for privately, |
| 1485 | * in order to be able to sustain communication at the requested @sdu. |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1486 | */ |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1487 | 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] | 1488 | { |
| 1489 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1490 | int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1491 | int pause_start, pause_stop; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 1492 | int atop, atop_tot; |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1493 | |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1494 | if (port == ocelot->npi) { |
| 1495 | maxlen += OCELOT_TAG_LEN; |
| 1496 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1497 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1498 | maxlen += OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1499 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1500 | maxlen += OCELOT_LONG_PREFIX_LEN; |
| 1501 | } |
| 1502 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1503 | ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1504 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1505 | /* Set Pause watermark hysteresis */ |
| 1506 | pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; |
| 1507 | pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 1508 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, |
| 1509 | pause_start); |
| 1510 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, |
| 1511 | pause_stop); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1512 | |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 1513 | /* Tail dropping watermarks */ |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 1514 | atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1515 | OCELOT_BUFFER_CELL_SZ; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 1516 | atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; |
| 1517 | ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); |
| 1518 | 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] | 1519 | } |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1520 | EXPORT_SYMBOL(ocelot_port_set_maxlen); |
| 1521 | |
| 1522 | int ocelot_get_max_mtu(struct ocelot *ocelot, int port) |
| 1523 | { |
| 1524 | int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; |
| 1525 | |
| 1526 | if (port == ocelot->npi) { |
| 1527 | max_mtu -= OCELOT_TAG_LEN; |
| 1528 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1529 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1530 | max_mtu -= OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1531 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1532 | max_mtu -= OCELOT_LONG_PREFIX_LEN; |
| 1533 | } |
| 1534 | |
| 1535 | return max_mtu; |
| 1536 | } |
| 1537 | EXPORT_SYMBOL(ocelot_get_max_mtu); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1538 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame^] | 1539 | static void ocelot_port_set_learning(struct ocelot *ocelot, int port, |
| 1540 | bool enabled) |
| 1541 | { |
| 1542 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1543 | u32 val = 0; |
| 1544 | |
| 1545 | if (enabled) |
| 1546 | val = ANA_PORT_PORT_CFG_LEARN_ENA; |
| 1547 | |
| 1548 | ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, |
| 1549 | ANA_PORT_PORT_CFG, port); |
| 1550 | |
| 1551 | ocelot_port->learn_ena = enabled; |
| 1552 | } |
| 1553 | |
| 1554 | static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, |
| 1555 | bool enabled) |
| 1556 | { |
| 1557 | u32 val = 0; |
| 1558 | |
| 1559 | if (enabled) |
| 1560 | val = BIT(port); |
| 1561 | |
| 1562 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); |
| 1563 | } |
| 1564 | |
| 1565 | static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, |
| 1566 | bool enabled) |
| 1567 | { |
| 1568 | u32 val = 0; |
| 1569 | |
| 1570 | if (enabled) |
| 1571 | val = BIT(port); |
| 1572 | |
| 1573 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); |
| 1574 | } |
| 1575 | |
| 1576 | static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, |
| 1577 | bool enabled) |
| 1578 | { |
| 1579 | u32 val = 0; |
| 1580 | |
| 1581 | if (enabled) |
| 1582 | val = BIT(port); |
| 1583 | |
| 1584 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); |
| 1585 | } |
| 1586 | |
| 1587 | int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, |
| 1588 | struct switchdev_brport_flags flags) |
| 1589 | { |
| 1590 | if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | |
| 1591 | BR_BCAST_FLOOD)) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); |
| 1597 | |
| 1598 | void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, |
| 1599 | struct switchdev_brport_flags flags) |
| 1600 | { |
| 1601 | if (flags.mask & BR_LEARNING) |
| 1602 | ocelot_port_set_learning(ocelot, port, |
| 1603 | !!(flags.val & BR_LEARNING)); |
| 1604 | |
| 1605 | if (flags.mask & BR_FLOOD) |
| 1606 | ocelot_port_set_ucast_flood(ocelot, port, |
| 1607 | !!(flags.val & BR_FLOOD)); |
| 1608 | |
| 1609 | if (flags.mask & BR_MCAST_FLOOD) |
| 1610 | ocelot_port_set_mcast_flood(ocelot, port, |
| 1611 | !!(flags.val & BR_MCAST_FLOOD)); |
| 1612 | |
| 1613 | if (flags.mask & BR_BCAST_FLOOD) |
| 1614 | ocelot_port_set_bcast_flood(ocelot, port, |
| 1615 | !!(flags.val & BR_BCAST_FLOOD)); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(ocelot_port_bridge_flags); |
| 1618 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1619 | void ocelot_init_port(struct ocelot *ocelot, int port) |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1620 | { |
| 1621 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1622 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 1623 | skb_queue_head_init(&ocelot_port->tx_skbs); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 1624 | spin_lock_init(&ocelot_port->ts_id_lock); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1625 | |
| 1626 | /* Basic L2 initialization */ |
| 1627 | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1628 | /* Set MAC IFG Gaps |
| 1629 | * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 |
| 1630 | * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 |
| 1631 | */ |
| 1632 | ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), |
| 1633 | DEV_MAC_IFG_CFG); |
| 1634 | |
| 1635 | /* Load seed (0) and set MAC HDX late collision */ |
| 1636 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | |
| 1637 | DEV_MAC_HDX_CFG_SEED_LOAD, |
| 1638 | DEV_MAC_HDX_CFG); |
| 1639 | mdelay(1); |
| 1640 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), |
| 1641 | DEV_MAC_HDX_CFG); |
| 1642 | |
| 1643 | /* Set Max Length and maximum tags allowed */ |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1644 | ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1645 | ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | |
| 1646 | DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1647 | DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1648 | DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, |
| 1649 | DEV_MAC_TAGS_CFG); |
| 1650 | |
| 1651 | /* Set SMAC of Pause frame (00:00:00:00:00:00) */ |
| 1652 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); |
| 1653 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); |
| 1654 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1655 | /* Enable transmission of pause frames */ |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 1656 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1657 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1658 | /* Drop frames with multicast source address */ |
| 1659 | ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 1660 | ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 1661 | ANA_PORT_DROP_CFG, port); |
| 1662 | |
| 1663 | /* Set default VLAN and tag type to 8021Q. */ |
| 1664 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), |
| 1665 | REW_PORT_VLAN_CFG_PORT_TPID_M, |
| 1666 | REW_PORT_VLAN_CFG, port); |
| 1667 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame^] | 1668 | /* Disable source address learning for standalone mode */ |
| 1669 | ocelot_port_set_learning(ocelot, port, false); |
| 1670 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1671 | /* Enable vcap lookups */ |
| 1672 | ocelot_vcap_enable(ocelot, port); |
| 1673 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1674 | EXPORT_SYMBOL(ocelot_init_port); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1675 | |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1676 | /* Configure and enable the CPU port module, which is a set of queues |
| 1677 | * accessible through register MMIO, frame DMA or Ethernet (in case |
| 1678 | * NPI mode is used). |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1679 | */ |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1680 | static void ocelot_cpu_port_init(struct ocelot *ocelot) |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1681 | { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1682 | int cpu = ocelot->num_phys_ports; |
| 1683 | |
| 1684 | /* The unicast destination PGID for the CPU port module is unused */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1685 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1686 | /* Instead set up a multicast destination PGID for traffic copied to |
| 1687 | * the CPU. Whitelisted MAC addresses like the port netdevice MAC |
| 1688 | * addresses will be copied to the CPU via this PGID. |
| 1689 | */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1690 | ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); |
| 1691 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | |
| 1692 | ANA_PORT_PORT_CFG_PORTID_VAL(cpu), |
| 1693 | ANA_PORT_PORT_CFG, cpu); |
| 1694 | |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1695 | /* Enable CPU port module */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1696 | ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1697 | /* CPU port Injection/Extraction configuration */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1698 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1699 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1700 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 1701 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1702 | |
| 1703 | /* Configure the CPU port to be VLAN aware */ |
| 1704 | ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | |
| 1705 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 1706 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), |
| 1707 | ANA_PORT_VLAN_CFG, cpu); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1708 | } |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1709 | |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 1710 | static void ocelot_detect_features(struct ocelot *ocelot) |
| 1711 | { |
| 1712 | int mmgt, eq_ctrl; |
| 1713 | |
| 1714 | /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds |
| 1715 | * the number of 240-byte free memory words (aka 4-cell chunks) and not |
| 1716 | * 192 bytes as the documentation incorrectly says. |
| 1717 | */ |
| 1718 | mmgt = ocelot_read(ocelot, SYS_MMGT); |
| 1719 | ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); |
| 1720 | |
| 1721 | eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); |
| 1722 | 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] | 1723 | } |
| 1724 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1725 | int ocelot_init(struct ocelot *ocelot) |
| 1726 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1727 | char queue_name[32]; |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1728 | int i, ret; |
| 1729 | u32 port; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1730 | |
Vladimir Oltean | 3a77b59 | 2019-11-14 17:03:26 +0200 | [diff] [blame] | 1731 | if (ocelot->ops->reset) { |
| 1732 | ret = ocelot->ops->reset(ocelot); |
| 1733 | if (ret) { |
| 1734 | dev_err(ocelot->dev, "Switch reset failed\n"); |
| 1735 | return ret; |
| 1736 | } |
| 1737 | } |
| 1738 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1739 | ocelot->stats = devm_kcalloc(ocelot->dev, |
| 1740 | ocelot->num_phys_ports * ocelot->num_stats, |
| 1741 | sizeof(u64), GFP_KERNEL); |
| 1742 | if (!ocelot->stats) |
| 1743 | return -ENOMEM; |
| 1744 | |
| 1745 | mutex_init(&ocelot->stats_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1746 | mutex_init(&ocelot->ptp_lock); |
| 1747 | spin_lock_init(&ocelot->ptp_clock_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1748 | snprintf(queue_name, sizeof(queue_name), "%s-stats", |
| 1749 | dev_name(ocelot->dev)); |
| 1750 | ocelot->stats_queue = create_singlethread_workqueue(queue_name); |
| 1751 | if (!ocelot->stats_queue) |
| 1752 | return -ENOMEM; |
| 1753 | |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 1754 | ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); |
| 1755 | if (!ocelot->owq) { |
| 1756 | destroy_workqueue(ocelot->stats_queue); |
| 1757 | return -ENOMEM; |
| 1758 | } |
| 1759 | |
Claudiu Manoil | 2b120dd | 2019-11-09 15:02:58 +0200 | [diff] [blame] | 1760 | INIT_LIST_HEAD(&ocelot->multicast); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1761 | INIT_LIST_HEAD(&ocelot->pgids); |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 1762 | ocelot_detect_features(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1763 | ocelot_mact_init(ocelot); |
| 1764 | ocelot_vlan_init(ocelot); |
Vladimir Oltean | aae4e50 | 2020-06-20 18:43:46 +0300 | [diff] [blame] | 1765 | ocelot_vcap_init(ocelot); |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1766 | ocelot_cpu_port_init(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1767 | |
| 1768 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1769 | /* Clear all counters (5 groups) */ |
| 1770 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | |
| 1771 | SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), |
| 1772 | SYS_STAT_CFG); |
| 1773 | } |
| 1774 | |
| 1775 | /* Only use S-Tag */ |
| 1776 | ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); |
| 1777 | |
| 1778 | /* Aggregation mode */ |
| 1779 | ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | |
| 1780 | ANA_AGGR_CFG_AC_DMAC_ENA | |
| 1781 | ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | |
Vladimir Oltean | f79c20c | 2021-02-06 00:02:13 +0200 | [diff] [blame] | 1782 | ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | |
| 1783 | ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | |
| 1784 | ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, |
| 1785 | ANA_AGGR_CFG); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1786 | |
| 1787 | /* Set MAC age time to default value. The entry is aged after |
| 1788 | * 2*AGE_PERIOD |
| 1789 | */ |
| 1790 | ocelot_write(ocelot, |
| 1791 | ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), |
| 1792 | ANA_AUTOAGE); |
| 1793 | |
| 1794 | /* Disable learning for frames discarded by VLAN ingress filtering */ |
| 1795 | regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); |
| 1796 | |
| 1797 | /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ |
| 1798 | ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | |
| 1799 | SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); |
| 1800 | |
| 1801 | /* Setup flooding PGIDs */ |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 1802 | for (i = 0; i < ocelot->num_flooding_pgids; i++) |
| 1803 | ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 1804 | ANA_FLOODING_FLD_BROADCAST(PGID_BC) | |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 1805 | ANA_FLOODING_FLD_UNICAST(PGID_UC), |
| 1806 | ANA_FLOODING, i); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1807 | ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | |
| 1808 | ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | |
| 1809 | ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | |
| 1810 | ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), |
| 1811 | ANA_FLOODING_IPMC); |
| 1812 | |
| 1813 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1814 | /* Transmit the frame to the local port. */ |
| 1815 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 1816 | /* Do not forward BPDU frames to the front ports. */ |
| 1817 | ocelot_write_gix(ocelot, |
| 1818 | ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), |
| 1819 | ANA_PORT_CPU_FWD_BPDU_CFG, |
| 1820 | port); |
| 1821 | /* Ensure bridging is disabled */ |
| 1822 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); |
| 1823 | } |
| 1824 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1825 | for_each_nonreserved_multicast_dest_pgid(ocelot, i) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1826 | u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); |
| 1827 | |
| 1828 | ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); |
| 1829 | } |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 1830 | /* Allow broadcast and unknown L2 multicast to the CPU. */ |
| 1831 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 1832 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 1833 | ANA_PGID_PGID, PGID_MC); |
| 1834 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 1835 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 1836 | ANA_PGID_PGID, PGID_BC); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1837 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); |
| 1838 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); |
| 1839 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1840 | /* Allow manual injection via DEVCPU_QS registers, and byte swap these |
| 1841 | * registers endianness. |
| 1842 | */ |
| 1843 | ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | |
| 1844 | QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); |
| 1845 | ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | |
| 1846 | QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); |
| 1847 | ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | |
| 1848 | ANA_CPUQ_CFG_CPUQ_LRN(2) | |
| 1849 | ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | |
| 1850 | ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | |
| 1851 | ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | |
| 1852 | ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | |
| 1853 | ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | |
| 1854 | ANA_CPUQ_CFG_CPUQ_IGMP(6) | |
| 1855 | ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); |
| 1856 | for (i = 0; i < 16; i++) |
| 1857 | ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | |
| 1858 | ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), |
| 1859 | ANA_CPUQ_8021_CFG, i); |
| 1860 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1861 | INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1862 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 1863 | OCELOT_STATS_CHECK_DELAY); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1864 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL(ocelot_init); |
| 1868 | |
| 1869 | void ocelot_deinit(struct ocelot *ocelot) |
| 1870 | { |
Claudiu Manoil | c5d1396 | 2019-07-25 16:33:18 +0300 | [diff] [blame] | 1871 | cancel_delayed_work(&ocelot->stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1872 | destroy_workqueue(ocelot->stats_queue); |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 1873 | destroy_workqueue(ocelot->owq); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1874 | mutex_destroy(&ocelot->stats_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1875 | } |
| 1876 | EXPORT_SYMBOL(ocelot_deinit); |
| 1877 | |
Vladimir Oltean | e5fb512 | 2020-09-18 04:07:30 +0300 | [diff] [blame] | 1878 | void ocelot_deinit_port(struct ocelot *ocelot, int port) |
| 1879 | { |
| 1880 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1881 | |
| 1882 | skb_queue_purge(&ocelot_port->tx_skbs); |
| 1883 | } |
| 1884 | EXPORT_SYMBOL(ocelot_deinit_port); |
| 1885 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1886 | MODULE_LICENSE("Dual MIT/GPL"); |