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