blob: 409cde1e59c6f2caeb34848ca405cc873007c7ed [file] [log] [blame]
Alexandre Bellonia556c762018-05-14 22:04:57 +02001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi Ocelot Switch driver
4 *
5 * Copyright (c) 2017 Microsemi Corporation
6 */
Vladimir Oltean40d3f292021-02-14 00:37:56 +02007#include <linux/dsa/ocelot.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +02008#include <linux/if_bridge.h>
Yangbo Lu39e53082021-04-27 12:22:03 +08009#include <linux/ptp_classify.h>
Vladimir Oltean20968052020-09-30 01:27:26 +030010#include <soc/mscc/ocelot_vcap.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +020011#include "ocelot.h"
Vladimir Oltean3c836542020-06-20 18:43:45 +030012#include "ocelot_vcap.h"
Alexandre Bellonia556c762018-05-14 22:04:57 +020013
Steen Hegelund639c1b22018-12-20 14:16:31 +010014#define TABLE_UPDATE_SLEEP_US 10
15#define TABLE_UPDATE_TIMEOUT_US 100000
16
Alexandre Bellonia556c762018-05-14 22:04:57 +020017struct ocelot_mact_entry {
18 u8 mac[ETH_ALEN];
19 u16 vid;
20 enum macaccess_entry_type type;
21};
22
Vladimir Oltean24683462021-10-24 20:17:51 +030023/* Caller must hold &ocelot->mact_lock */
Steen Hegelund639c1b22018-12-20 14:16:31 +010024static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
25{
26 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
27}
28
Vladimir Oltean24683462021-10-24 20:17:51 +030029/* Caller must hold &ocelot->mact_lock */
Alexandre Bellonia556c762018-05-14 22:04:57 +020030static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
31{
Steen Hegelund639c1b22018-12-20 14:16:31 +010032 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +020033
Steen Hegelund639c1b22018-12-20 14:16:31 +010034 return readx_poll_timeout(ocelot_mact_read_macaccess,
35 ocelot, val,
36 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
37 MACACCESS_CMD_IDLE,
38 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +020039}
40
Vladimir Oltean24683462021-10-24 20:17:51 +030041/* Caller must hold &ocelot->mact_lock */
Alexandre Bellonia556c762018-05-14 22:04:57 +020042static void ocelot_mact_select(struct ocelot *ocelot,
43 const unsigned char mac[ETH_ALEN],
44 unsigned int vid)
45{
46 u32 macl = 0, mach = 0;
47
48 /* Set the MAC address to handle and the vlan associated in a format
49 * understood by the hardware.
50 */
51 mach |= vid << 16;
52 mach |= mac[0] << 8;
53 mach |= mac[1] << 0;
54 macl |= mac[2] << 24;
55 macl |= mac[3] << 16;
56 macl |= mac[4] << 8;
57 macl |= mac[5] << 0;
58
59 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
60 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
61
62}
63
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030064int ocelot_mact_learn(struct ocelot *ocelot, int port,
65 const unsigned char mac[ETH_ALEN],
66 unsigned int vid, enum macaccess_entry_type type)
Alexandre Bellonia556c762018-05-14 22:04:57 +020067{
Alban Bedel584b7cf2021-01-19 15:06:38 +010068 u32 cmd = ANA_TABLES_MACACCESS_VALID |
69 ANA_TABLES_MACACCESS_DEST_IDX(port) |
70 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
71 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
72 unsigned int mc_ports;
Vladimir Oltean24683462021-10-24 20:17:51 +030073 int err;
Alban Bedel584b7cf2021-01-19 15:06:38 +010074
75 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
76 if (type == ENTRYTYPE_MACv4)
77 mc_ports = (mac[1] << 8) | mac[2];
78 else if (type == ENTRYTYPE_MACv6)
79 mc_ports = (mac[0] << 8) | mac[1];
80 else
81 mc_ports = 0;
82
83 if (mc_ports & BIT(ocelot->num_phys_ports))
84 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
85
Vladimir Oltean24683462021-10-24 20:17:51 +030086 mutex_lock(&ocelot->mact_lock);
87
Alexandre Bellonia556c762018-05-14 22:04:57 +020088 ocelot_mact_select(ocelot, mac, vid);
89
90 /* Issue a write command */
Alban Bedel584b7cf2021-01-19 15:06:38 +010091 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
Alexandre Bellonia556c762018-05-14 22:04:57 +020092
Vladimir Oltean24683462021-10-24 20:17:51 +030093 err = ocelot_mact_wait_for_completion(ocelot);
94
95 mutex_unlock(&ocelot->mact_lock);
96
97 return err;
Alexandre Bellonia556c762018-05-14 22:04:57 +020098}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030099EXPORT_SYMBOL(ocelot_mact_learn);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200100
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300101int ocelot_mact_forget(struct ocelot *ocelot,
102 const unsigned char mac[ETH_ALEN], unsigned int vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200103{
Vladimir Oltean24683462021-10-24 20:17:51 +0300104 int err;
105
106 mutex_lock(&ocelot->mact_lock);
107
Alexandre Bellonia556c762018-05-14 22:04:57 +0200108 ocelot_mact_select(ocelot, mac, vid);
109
110 /* Issue a forget command */
111 ocelot_write(ocelot,
112 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
113 ANA_TABLES_MACACCESS);
114
Vladimir Oltean24683462021-10-24 20:17:51 +0300115 err = ocelot_mact_wait_for_completion(ocelot);
116
117 mutex_unlock(&ocelot->mact_lock);
118
119 return err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200120}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300121EXPORT_SYMBOL(ocelot_mact_forget);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200122
123static void ocelot_mact_init(struct ocelot *ocelot)
124{
125 /* Configure the learning mode entries attributes:
126 * - Do not copy the frame to the CPU extraction queues.
127 * - Use the vlan and mac_cpoy for dmac lookup.
128 */
129 ocelot_rmw(ocelot, 0,
130 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
131 | ANA_AGENCTRL_LEARN_FWD_KILL
132 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
133 ANA_AGENCTRL);
134
Vladimir Oltean24683462021-10-24 20:17:51 +0300135 /* Clear the MAC table. We are not concurrent with anyone, so
136 * holding &ocelot->mact_lock is pointless.
137 */
Alexandre Bellonia556c762018-05-14 22:04:57 +0200138 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
139}
140
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200141static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200142{
143 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
144 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200145 ANA_PORT_VCAP_S2_CFG, port);
Xiaoliang Yang75944fd2020-10-02 15:02:23 +0300146
147 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
148 ANA_PORT_VCAP_CFG, port);
Xiaoliang Yang2f17c052020-10-02 15:02:24 +0300149
150 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
151 REW_PORT_CFG_ES0_EN,
152 REW_PORT_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200153}
154
Steen Hegelund639c1b22018-12-20 14:16:31 +0100155static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
156{
157 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
158}
159
Alexandre Bellonia556c762018-05-14 22:04:57 +0200160static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
161{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100162 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200163
Steen Hegelund639c1b22018-12-20 14:16:31 +0100164 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
165 ocelot,
166 val,
167 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
168 ANA_TABLES_VLANACCESS_CMD_IDLE,
169 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200170}
171
Antoine Tenart71425292018-06-26 14:28:49 +0200172static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
173{
174 /* Select the VID to configure */
175 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
176 ANA_TABLES_VLANTIDX);
177 /* Set the vlan port members mask and issue a write command */
178 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
179 ANA_TABLES_VLANACCESS_CMD_WRITE,
180 ANA_TABLES_VLANACCESS);
181
182 return ocelot_vlant_wait_for_completion(ocelot);
183}
184
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300185static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
186{
187 struct ocelot_bridge_vlan *vlan;
188 int num_untagged = 0;
189
190 list_for_each_entry(vlan, &ocelot->vlans, list) {
191 if (!(vlan->portmask & BIT(port)))
192 continue;
193
194 if (vlan->untagged & BIT(port))
195 num_untagged++;
196 }
197
198 return num_untagged;
199}
200
201static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
202{
203 struct ocelot_bridge_vlan *vlan;
204 int num_tagged = 0;
205
206 list_for_each_entry(vlan, &ocelot->vlans, list) {
207 if (!(vlan->portmask & BIT(port)))
208 continue;
209
210 if (!(vlan->untagged & BIT(port)))
211 num_tagged++;
212 }
213
214 return num_tagged;
215}
216
217/* We use native VLAN when we have to mix egress-tagged VLANs with exactly
218 * _one_ egress-untagged VLAN (_the_ native VLAN)
219 */
220static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
221{
222 return ocelot_port_num_tagged_vlans(ocelot, port) &&
223 ocelot_port_num_untagged_vlans(ocelot, port) == 1;
224}
225
226static struct ocelot_bridge_vlan *
227ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
228{
229 struct ocelot_bridge_vlan *vlan;
230
231 list_for_each_entry(vlan, &ocelot->vlans, list)
232 if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
233 return vlan;
234
235 return NULL;
236}
237
238/* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
239 * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
240 * state of the port.
241 */
242static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200243{
244 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300245 enum ocelot_port_tag_config tag_cfg;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300246 bool uses_native_vlan = false;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200247
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300248 if (ocelot_port->vlan_aware) {
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300249 uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
250
251 if (uses_native_vlan)
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300252 tag_cfg = OCELOT_PORT_TAG_NATIVE;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300253 else if (ocelot_port_num_untagged_vlans(ocelot, port))
254 tag_cfg = OCELOT_PORT_TAG_DISABLED;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300255 else
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300256 tag_cfg = OCELOT_PORT_TAG_TRUNK;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300257 } else {
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300258 tag_cfg = OCELOT_PORT_TAG_DISABLED;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300259 }
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300260
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300261 ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300262 REW_TAG_CFG_TAG_CFG_M,
263 REW_TAG_CFG, port);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300264
265 if (uses_native_vlan) {
266 struct ocelot_bridge_vlan *native_vlan;
267
268 /* Not having a native VLAN is impossible, because
269 * ocelot_port_num_untagged_vlans has returned 1.
270 * So there is no use in checking for NULL here.
271 */
272 native_vlan = ocelot_port_find_native_vlan(ocelot, port);
273
274 ocelot_rmw_gix(ocelot,
275 REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
276 REW_PORT_VLAN_CFG_PORT_VID_M,
277 REW_PORT_VLAN_CFG, port);
278 }
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200279}
280
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200281/* Default vlan to clasify for untagged frames (may be zero) */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200282static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
Vladimir Olteand4004422021-10-20 20:58:52 +0300283 const struct ocelot_bridge_vlan *pvid_vlan)
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200284{
285 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteand4004422021-10-20 20:58:52 +0300286 u16 pvid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200287 u32 val = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200288
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200289 ocelot_port->pvid_vlan = pvid_vlan;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200290
Vladimir Olteand4004422021-10-20 20:58:52 +0300291 if (ocelot_port->vlan_aware && pvid_vlan)
292 pvid = pvid_vlan->vid;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200293
294 ocelot_rmw_gix(ocelot,
Vladimir Olteand4004422021-10-20 20:58:52 +0300295 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200296 ANA_PORT_VLAN_CFG_VLAN_VID_M,
297 ANA_PORT_VLAN_CFG, port);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200298
299 /* If there's no pvid, we should drop not only untagged traffic (which
300 * happens automatically), but also 802.1p traffic which gets
301 * classified to VLAN 0, but that is always in our RX filter, so it
302 * would get accepted were it not for this setting.
303 */
Vladimir Olteand4004422021-10-20 20:58:52 +0300304 if (!pvid_vlan && ocelot_port->vlan_aware)
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200305 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
306 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
307
308 ocelot_rmw_gix(ocelot, val,
309 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
310 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
311 ANA_PORT_DROP_CFG, port);
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200312}
313
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300314static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
315 u16 vid)
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300316{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300317 struct ocelot_bridge_vlan *vlan;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300318
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300319 list_for_each_entry(vlan, &ocelot->vlans, list)
320 if (vlan->vid == vid)
321 return vlan;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300322
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300323 return NULL;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300324}
325
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300326static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
327 bool untagged)
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300328{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300329 struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
330 unsigned long portmask;
331 int err;
332
333 if (vlan) {
334 portmask = vlan->portmask | BIT(port);
335
336 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
337 if (err)
338 return err;
339
340 vlan->portmask = portmask;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300341 /* Bridge VLANs can be overwritten with a different
342 * egress-tagging setting, so make sure to override an untagged
343 * with a tagged VID if that's going on.
344 */
345 if (untagged)
346 vlan->untagged |= BIT(port);
347 else
348 vlan->untagged &= ~BIT(port);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300349
350 return 0;
351 }
352
353 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
354 if (!vlan)
355 return -ENOMEM;
356
357 portmask = BIT(port);
358
359 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
360 if (err) {
361 kfree(vlan);
362 return err;
363 }
364
365 vlan->vid = vid;
366 vlan->portmask = portmask;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300367 if (untagged)
368 vlan->untagged = BIT(port);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300369 INIT_LIST_HEAD(&vlan->list);
370 list_add_tail(&vlan->list, &ocelot->vlans);
371
372 return 0;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300373}
374
375static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
376{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300377 struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
378 unsigned long portmask;
379 int err;
380
381 if (!vlan)
382 return 0;
383
384 portmask = vlan->portmask & ~BIT(port);
385
386 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
387 if (err)
388 return err;
389
390 vlan->portmask = portmask;
391 if (vlan->portmask)
392 return 0;
393
394 list_del(&vlan->list);
395 kfree(vlan);
396
397 return 0;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300398}
399
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300400int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300401 bool vlan_aware, struct netlink_ext_ack *extack)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300402{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200403 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300404 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200405 struct ocelot_vcap_filter *filter;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300406 u32 val;
407
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200408 list_for_each_entry(filter, &block->rules, list) {
409 if (filter->ingress_port_mask & BIT(port) &&
410 filter->action.vid_replace_ena) {
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300411 NL_SET_ERR_MSG_MOD(extack,
412 "Cannot change VLAN state with vlan modify rules active");
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200413 return -EBUSY;
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300414 }
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300415 }
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300416
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300417 ocelot_port->vlan_aware = vlan_aware;
418
419 if (vlan_aware)
420 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
421 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
422 else
423 val = 0;
424 ocelot_rmw_gix(ocelot, val,
425 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
426 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
427 ANA_PORT_VLAN_CFG, port);
428
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200429 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300430 ocelot_port_manage_port_tag(ocelot, port);
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300431
432 return 0;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300433}
434EXPORT_SYMBOL(ocelot_port_vlan_filtering);
435
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200436int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
Vladimir Oltean01af9402021-08-19 20:40:06 +0300437 bool untagged, struct netlink_ext_ack *extack)
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200438{
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300439 if (untagged) {
440 /* We are adding an egress-tagged VLAN */
441 if (ocelot_port_uses_native_vlan(ocelot, port)) {
442 NL_SET_ERR_MSG_MOD(extack,
443 "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
444 return -EBUSY;
445 }
446 } else {
447 /* We are adding an egress-tagged VLAN */
448 if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
449 NL_SET_ERR_MSG_MOD(extack,
450 "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
451 return -EBUSY;
452 }
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200453 }
454
455 return 0;
456}
457EXPORT_SYMBOL(ocelot_vlan_prepare);
458
Vladimir Oltean5e256362019-11-14 17:03:27 +0200459int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
460 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200461{
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300462 int err;
Antoine Tenart71425292018-06-26 14:28:49 +0200463
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300464 err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300465 if (err)
466 return err;
Antoine Tenart71425292018-06-26 14:28:49 +0200467
468 /* Default ingress vlan classification */
Vladimir Olteand4004422021-10-20 20:58:52 +0300469 if (pvid)
470 ocelot_port_set_pvid(ocelot, port,
471 ocelot_bridge_vlan_find(ocelot, vid));
Antoine Tenart71425292018-06-26 14:28:49 +0200472
473 /* Untagged egress vlan clasification */
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300474 ocelot_port_manage_port_tag(ocelot, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200475
Antoine Tenart71425292018-06-26 14:28:49 +0200476 return 0;
477}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200478EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200479
Vladimir Oltean5e256362019-11-14 17:03:27 +0200480int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200481{
482 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300483 int err;
Antoine Tenart71425292018-06-26 14:28:49 +0200484
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300485 err = ocelot_vlan_member_del(ocelot, port, vid);
486 if (err)
487 return err;
Antoine Tenart71425292018-06-26 14:28:49 +0200488
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200489 /* Ingress */
Vladimir Olteand4004422021-10-20 20:58:52 +0300490 if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
491 ocelot_port_set_pvid(ocelot, port, NULL);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200492
Antoine Tenart71425292018-06-26 14:28:49 +0200493 /* Egress */
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300494 ocelot_port_manage_port_tag(ocelot, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200495
496 return 0;
497}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200498EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200499
Alexandre Bellonia556c762018-05-14 22:04:57 +0200500static void ocelot_vlan_init(struct ocelot *ocelot)
501{
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300502 unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200503 u16 port, vid;
504
Alexandre Bellonia556c762018-05-14 22:04:57 +0200505 /* Clear VLAN table, by default all ports are members of all VLANs */
506 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
507 ANA_TABLES_VLANACCESS);
508 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200509
510 /* Configure the port VLAN memberships */
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300511 for (vid = 1; vid < VLAN_N_VID; vid++)
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300512 ocelot_vlant_set_mask(ocelot, vid, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200513
514 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
515 * traffic. It is added automatically if 8021q module is loaded, but
516 * we can't rely on it since module may be not loaded.
517 */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300518 ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports);
Antoine Tenart71425292018-06-26 14:28:49 +0200519
Antoine Tenart71425292018-06-26 14:28:49 +0200520 /* Set vlan ingress filter mask to all ports but the CPU port by
521 * default.
522 */
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300523 ocelot_write(ocelot, all_ports, ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200524
525 for (port = 0; port < ocelot->num_phys_ports; port++) {
526 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
527 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
528 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200529}
530
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200531static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
532{
533 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
534}
535
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300536static int ocelot_port_flush(struct ocelot *ocelot, int port)
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200537{
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300538 unsigned int pause_ena;
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200539 int err, val;
540
541 /* Disable dequeuing from the egress queues */
542 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
543 QSYS_PORT_MODE_DEQUEUE_DIS,
544 QSYS_PORT_MODE, port);
545
546 /* Disable flow control */
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300547 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200548 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
549
550 /* Disable priority flow control */
551 ocelot_fields_write(ocelot, port,
552 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
553
554 /* Wait at least the time it takes to receive a frame of maximum length
555 * at the port.
556 * Worst-case delays for 10 kilobyte jumbo frames are:
557 * 8 ms on a 10M port
558 * 800 μs on a 100M port
559 * 80 μs on a 1G port
560 * 32 μs on a 2.5G port
561 */
562 usleep_range(8000, 10000);
563
564 /* Disable half duplex backpressure. */
565 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
566 SYS_FRONT_PORT_MODE, port);
567
568 /* Flush the queues associated with the port. */
569 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
570 REW_PORT_CFG, port);
571
572 /* Enable dequeuing from the egress queues. */
573 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
574 port);
575
576 /* Wait until flushing is complete. */
577 err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
578 100, 2000000, false, ocelot, port);
579
580 /* Clear flushing again. */
581 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
582
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300583 /* Re-enable flow control */
584 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
585
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200586 return err;
587}
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200588
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300589void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
590 unsigned int link_an_mode,
591 phy_interface_t interface,
592 unsigned long quirks)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200593{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200594 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300595 int err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200596
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300597 ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
598 DEV_MAC_ENA_CFG);
599
600 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
601
602 err = ocelot_port_flush(ocelot, port);
603 if (err)
604 dev_err(ocelot->dev, "failed to flush port %d: %d\n",
605 port, err);
606
607 /* Put the port in reset. */
608 if (interface != PHY_INTERFACE_MODE_QSGMII ||
609 !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
610 ocelot_port_rmwl(ocelot_port,
611 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +0800612 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300613 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +0800614 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300615 DEV_CLOCK_CFG);
616}
617EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
618
619void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
620 struct phy_device *phydev,
621 unsigned int link_an_mode,
622 phy_interface_t interface,
623 int speed, int duplex,
624 bool tx_pause, bool rx_pause,
625 unsigned long quirks)
626{
627 struct ocelot_port *ocelot_port = ocelot->ports[port];
628 int mac_speed, mode = 0;
629 u32 mac_fc_cfg;
630
631 /* The MAC might be integrated in systems where the MAC speed is fixed
632 * and it's the PCS who is performing the rate adaptation, so we have
633 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
634 * (which is also its default value).
635 */
636 if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
637 speed == SPEED_1000) {
638 mac_speed = OCELOT_SPEED_1000;
639 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
640 } else if (speed == SPEED_2500) {
641 mac_speed = OCELOT_SPEED_2500;
642 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
643 } else if (speed == SPEED_100) {
644 mac_speed = OCELOT_SPEED_100;
645 } else {
646 mac_speed = OCELOT_SPEED_10;
647 }
648
649 if (duplex == DUPLEX_FULL)
650 mode |= DEV_MAC_MODE_CFG_FDX_ENA;
651
652 ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
653
654 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
655 * PORT_RST bits in DEV_CLOCK_CFG.
656 */
657 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
658 DEV_CLOCK_CFG);
659
660 switch (speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200661 case SPEED_10:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300662 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200663 break;
664 case SPEED_100:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300665 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200666 break;
667 case SPEED_1000:
Alexandre Bellonia556c762018-05-14 22:04:57 +0200668 case SPEED_2500:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300669 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200670 break;
671 default:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300672 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
673 port, speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200674 return;
675 }
676
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300677 /* Handle RX pause in all cases, with 2500base-X this is used for rate
678 * adaptation.
679 */
680 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200681
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300682 if (tx_pause)
683 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
684 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
685 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
686 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200687
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300688 /* Flow control. Link speed is only used here to evaluate the time
689 * specification in incoming pause frames.
690 */
691 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200692
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300693 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200694
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300695 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200696
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300697 /* Undo the effects of ocelot_phylink_mac_link_down:
698 * enable MAC module
699 */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200700 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200701 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
702
Alexandre Bellonia556c762018-05-14 22:04:57 +0200703 /* Core: Enable port for frame transfer */
Vladimir Oltean886e1382020-07-13 19:57:03 +0300704 ocelot_fields_write(ocelot, port,
705 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200706}
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300707EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200708
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300709static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
710 struct sk_buff *clone)
Yangbo Lu400928b2019-11-20 16:23:16 +0800711{
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300712 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300713 unsigned long flags;
Yangbo Lu400928b2019-11-20 16:23:16 +0800714
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300715 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
716
717 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
718 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
719 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
720 return -EBUSY;
721 }
Vladimir Oltean65652432020-09-18 04:07:24 +0300722
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300723 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
Yangbo Luc4b364c2021-04-27 12:22:00 +0800724 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
725 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300726
Vladimir Olteanc57fe002021-10-12 14:40:35 +0300727 ocelot_port->ts_id++;
728 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
729 ocelot_port->ts_id = 0;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300730
731 ocelot_port->ptp_skbs_in_flight++;
732 ocelot->ptp_skbs_in_flight++;
733
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300734 skb_queue_tail(&ocelot_port->tx_skbs, clone);
Vladimir Oltean65652432020-09-18 04:07:24 +0300735
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300736 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
737
738 return 0;
Yangbo Lu400928b2019-11-20 16:23:16 +0800739}
Yangbo Lu682eaad2021-04-27 12:22:02 +0800740
Vladimir Olteanfba01282021-10-12 14:40:38 +0300741static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
742 unsigned int ptp_class)
Yangbo Lu39e53082021-04-27 12:22:03 +0800743{
744 struct ptp_header *hdr;
Yangbo Lu39e53082021-04-27 12:22:03 +0800745 u8 msgtype, twostep;
746
Yangbo Lu39e53082021-04-27 12:22:03 +0800747 hdr = ptp_parse_header(skb, ptp_class);
748 if (!hdr)
749 return false;
750
751 msgtype = ptp_get_msgtype(hdr, ptp_class);
752 twostep = hdr->flag_field[0] & 0x2;
753
754 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
755 return true;
756
757 return false;
758}
759
Yangbo Lu682eaad2021-04-27 12:22:02 +0800760int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
761 struct sk_buff *skb,
762 struct sk_buff **clone)
763{
764 struct ocelot_port *ocelot_port = ocelot->ports[port];
765 u8 ptp_cmd = ocelot_port->ptp_cmd;
Vladimir Olteanfba01282021-10-12 14:40:38 +0300766 unsigned int ptp_class;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300767 int err;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800768
Vladimir Olteanfba01282021-10-12 14:40:38 +0300769 /* Don't do anything if PTP timestamping not enabled */
770 if (!ptp_cmd)
771 return 0;
772
773 ptp_class = ptp_classify_raw(skb);
774 if (ptp_class == PTP_CLASS_NONE)
775 return -EINVAL;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800776
Yangbo Lu39e53082021-04-27 12:22:03 +0800777 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
778 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
Vladimir Olteanfba01282021-10-12 14:40:38 +0300779 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
Yangbo Lu39e53082021-04-27 12:22:03 +0800780 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
781 return 0;
782 }
783
784 /* Fall back to two-step timestamping */
785 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
786 }
787
Yangbo Lu682eaad2021-04-27 12:22:02 +0800788 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
789 *clone = skb_clone_sk(skb);
790 if (!(*clone))
791 return -ENOMEM;
792
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300793 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
794 if (err)
795 return err;
796
Yangbo Lu39e53082021-04-27 12:22:03 +0800797 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300798 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800799 }
800
801 return 0;
802}
803EXPORT_SYMBOL(ocelot_port_txtstamp_request);
Yangbo Lu400928b2019-11-20 16:23:16 +0800804
Yangbo Lue23a7b32019-11-20 16:23:15 +0800805static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
806 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200807{
808 unsigned long flags;
809 u32 val;
810
811 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
812
813 /* Read current PTP time to get seconds */
814 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
815
816 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
817 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
818 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
819 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
820
821 /* Read packet HW timestamp from FIFO */
822 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
823 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
824
825 /* Sec has incremented since the ts was registered */
826 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
827 ts->tv_sec--;
828
829 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
830}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800831
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300832static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
833{
834 struct ptp_header *hdr;
835
836 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
837 if (WARN_ON(!hdr))
838 return false;
839
840 return seqid == ntohs(hdr->sequence_id);
841}
842
Yangbo Lue23a7b32019-11-20 16:23:15 +0800843void ocelot_get_txtstamp(struct ocelot *ocelot)
844{
845 int budget = OCELOT_PTP_QUEUE_SZ;
846
847 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800848 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800849 struct skb_shared_hwtstamps shhwtstamps;
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300850 u32 val, id, seqid, txport;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800851 struct ocelot_port *port;
852 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800853 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800854
855 val = ocelot_read(ocelot, SYS_PTP_STATUS);
856
857 /* Check if a timestamp can be retrieved */
858 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
859 break;
860
861 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
862
863 /* Retrieve the ts ID and Tx port */
864 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
865 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300866 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800867
Yangbo Lue23a7b32019-11-20 16:23:15 +0800868 port = ocelot->ports[txport];
869
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300870 spin_lock(&ocelot->ts_id_lock);
871 port->ptp_skbs_in_flight--;
872 ocelot->ptp_skbs_in_flight--;
873 spin_unlock(&ocelot->ts_id_lock);
874
875 /* Retrieve its associated skb */
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300876try_again:
Yangbo Lub049da12019-11-27 15:27:57 +0800877 spin_lock_irqsave(&port->tx_skbs.lock, flags);
878
879 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
Yangbo Luc4b364c2021-04-27 12:22:00 +0800880 if (OCELOT_SKB_CB(skb)->ts_id != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800881 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800882 __skb_unlink(skb, &port->tx_skbs);
883 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800884 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800885 }
886
Yangbo Lub049da12019-11-27 15:27:57 +0800887 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
888
Vladimir Oltean9fde5062021-10-12 14:40:37 +0300889 if (WARN_ON(!skb_match))
890 continue;
891
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300892 if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
893 dev_err_ratelimited(ocelot->dev,
894 "port %d received stale TX timestamp for seqid %d, discarding\n",
895 txport, seqid);
896 dev_kfree_skb_any(skb);
897 goto try_again;
898 }
899
laurent brando5fd82202020-07-27 18:26:14 +0800900 /* Get the h/w timestamp */
901 ocelot_get_hwtimestamp(ocelot, &ts);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800902
Yangbo Lue23a7b32019-11-20 16:23:15 +0800903 /* Set the timestamp into the skb */
904 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
905 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300906 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
laurent brando5fd82202020-07-27 18:26:14 +0800907
908 /* Next ts */
909 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800910 }
911}
912EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200913
Vladimir Oltean924ee312021-02-14 00:37:59 +0200914static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
915 u32 *rval)
916{
917 u32 bytes_valid, val;
918
919 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
920 if (val == XTR_NOT_READY) {
921 if (ifh)
922 return -EIO;
923
924 do {
925 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
926 } while (val == XTR_NOT_READY);
927 }
928
929 switch (val) {
930 case XTR_ABORT:
931 return -EIO;
932 case XTR_EOF_0:
933 case XTR_EOF_1:
934 case XTR_EOF_2:
935 case XTR_EOF_3:
936 case XTR_PRUNED:
937 bytes_valid = XTR_VALID_BYTES(val);
938 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
939 if (val == XTR_ESCAPE)
940 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
941 else
942 *rval = val;
943
944 return bytes_valid;
945 case XTR_ESCAPE:
946 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
947
948 return 4;
949 default:
950 *rval = val;
951
952 return 4;
953 }
954}
955
956static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
957{
958 int i, err = 0;
959
960 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
961 err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
962 if (err != 4)
963 return (err < 0) ? err : -EIO;
964 }
965
966 return 0;
967}
968
969int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
970{
971 struct skb_shared_hwtstamps *shhwtstamps;
Horatiu Vultur2ed2c5f2021-03-16 21:10:19 +0100972 u64 tod_in_ns, full_ts_in_ns;
Vladimir Oltean924ee312021-02-14 00:37:59 +0200973 u64 timestamp, src_port, len;
974 u32 xfh[OCELOT_TAG_LEN / 4];
975 struct net_device *dev;
976 struct timespec64 ts;
977 struct sk_buff *skb;
978 int sz, buf_len;
979 u32 val, *buf;
980 int err;
981
982 err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
983 if (err)
984 return err;
985
986 ocelot_xfh_get_src_port(xfh, &src_port);
987 ocelot_xfh_get_len(xfh, &len);
988 ocelot_xfh_get_rew_val(xfh, &timestamp);
989
990 if (WARN_ON(src_port >= ocelot->num_phys_ports))
991 return -EINVAL;
992
993 dev = ocelot->ops->port_to_netdev(ocelot, src_port);
994 if (!dev)
995 return -EINVAL;
996
997 skb = netdev_alloc_skb(dev, len);
998 if (unlikely(!skb)) {
999 netdev_err(dev, "Unable to allocate sk_buff\n");
1000 return -ENOMEM;
1001 }
1002
1003 buf_len = len - ETH_FCS_LEN;
1004 buf = (u32 *)skb_put(skb, buf_len);
1005
1006 len = 0;
1007 do {
1008 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1009 if (sz < 0) {
1010 err = sz;
1011 goto out_free_skb;
1012 }
1013 *buf++ = val;
1014 len += sz;
1015 } while (len < buf_len);
1016
1017 /* Read the FCS */
1018 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1019 if (sz < 0) {
1020 err = sz;
1021 goto out_free_skb;
1022 }
1023
1024 /* Update the statistics if part of the FCS was read before */
1025 len -= ETH_FCS_LEN - sz;
1026
1027 if (unlikely(dev->features & NETIF_F_RXFCS)) {
1028 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1029 *buf = val;
1030 }
1031
1032 if (ocelot->ptp) {
1033 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1034
1035 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1036 if ((tod_in_ns & 0xffffffff) < timestamp)
1037 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1038 timestamp;
1039 else
1040 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1041 timestamp;
1042
1043 shhwtstamps = skb_hwtstamps(skb);
1044 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1045 shhwtstamps->hwtstamp = full_ts_in_ns;
1046 }
1047
1048 /* Everything we see on an interface that is in the HW bridge
1049 * has already been forwarded.
1050 */
Vladimir Olteandf291e52021-03-19 01:36:36 +02001051 if (ocelot->ports[src_port]->bridge)
Vladimir Oltean924ee312021-02-14 00:37:59 +02001052 skb->offload_fwd_mark = 1;
1053
1054 skb->protocol = eth_type_trans(skb, dev);
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001055
Vladimir Oltean924ee312021-02-14 00:37:59 +02001056 *nskb = skb;
1057
1058 return 0;
1059
1060out_free_skb:
1061 kfree_skb(skb);
1062 return err;
1063}
1064EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1065
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001066bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1067{
1068 u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1069
1070 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1071 return false;
1072 if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1073 return false;
1074
1075 return true;
1076}
1077EXPORT_SYMBOL(ocelot_can_inject);
1078
1079void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1080 u32 rew_op, struct sk_buff *skb)
1081{
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001082 u32 ifh[OCELOT_TAG_LEN / 4] = {0};
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001083 unsigned int i, count, last;
1084
1085 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1086 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1087
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001088 ocelot_ifh_set_bypass(ifh, 1);
Vladimir Oltean1f778d52021-02-15 15:31:43 +02001089 ocelot_ifh_set_dest(ifh, BIT_ULL(port));
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001090 ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
Vladimir Olteane8c07222021-10-01 18:15:27 +03001091 ocelot_ifh_set_vlan_tci(ifh, skb_vlan_tag_get(skb));
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001092 ocelot_ifh_set_rew_op(ifh, rew_op);
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001093
1094 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001095 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001096
1097 count = DIV_ROUND_UP(skb->len, 4);
1098 last = skb->len % 4;
1099 for (i = 0; i < count; i++)
1100 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1101
1102 /* Add padding */
1103 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1104 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1105 i++;
1106 }
1107
1108 /* Indicate EOF and valid bytes in last word */
1109 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1110 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1111 QS_INJ_CTRL_EOF,
1112 QS_INJ_CTRL, grp);
1113
1114 /* Add dummy CRC */
1115 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1116 skb_tx_timestamp(skb);
1117
1118 skb->dev->stats.tx_packets++;
1119 skb->dev->stats.tx_bytes += skb->len;
1120}
1121EXPORT_SYMBOL(ocelot_port_inject_frame);
1122
Vladimir Oltean0a6f17c2021-02-14 00:38:01 +02001123void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
1124{
1125 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
1126 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1127}
1128EXPORT_SYMBOL(ocelot_drain_cpu_queue);
1129
Vladimir Oltean5e256362019-11-14 17:03:27 +02001130int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +03001131 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001132{
Vladimir Oltean471beb12020-06-21 14:46:00 +03001133 int pgid = port;
1134
1135 if (port == ocelot->npi)
1136 pgid = PGID_CPU;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001137
Vladimir Oltean471beb12020-06-21 14:46:00 +03001138 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001139}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001140EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001141
Vladimir Oltean5e256362019-11-14 17:03:27 +02001142int ocelot_fdb_del(struct ocelot *ocelot, int port,
1143 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001144{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001145 return ocelot_mact_forget(ocelot, addr, vid);
1146}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001147EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001148
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001149int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1150 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001151{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001152 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001153 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1154 u32 seq = dump->cb->nlh->nlmsg_seq;
1155 struct nlmsghdr *nlh;
1156 struct ndmsg *ndm;
1157
1158 if (dump->idx < dump->cb->args[2])
1159 goto skip;
1160
1161 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1162 sizeof(*ndm), NLM_F_MULTI);
1163 if (!nlh)
1164 return -EMSGSIZE;
1165
1166 ndm = nlmsg_data(nlh);
1167 ndm->ndm_family = AF_BRIDGE;
1168 ndm->ndm_pad1 = 0;
1169 ndm->ndm_pad2 = 0;
1170 ndm->ndm_flags = NTF_SELF;
1171 ndm->ndm_type = 0;
1172 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001173 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001174
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001175 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +02001176 goto nla_put_failure;
1177
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001178 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +02001179 goto nla_put_failure;
1180
1181 nlmsg_end(dump->skb, nlh);
1182
1183skip:
1184 dump->idx++;
1185 return 0;
1186
1187nla_put_failure:
1188 nlmsg_cancel(dump->skb, nlh);
1189 return -EMSGSIZE;
1190}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001191EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001192
Vladimir Oltean24683462021-10-24 20:17:51 +03001193/* Caller must hold &ocelot->mact_lock */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001194static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1195 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001196{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001197 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001198 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +02001199
1200 /* Set row and column to read from */
1201 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1202 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1203
1204 /* Issue a read command */
1205 ocelot_write(ocelot,
1206 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1207 ANA_TABLES_MACACCESS);
1208
1209 if (ocelot_mact_wait_for_completion(ocelot))
1210 return -ETIMEDOUT;
1211
1212 /* Read the entry flags */
1213 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1214 if (!(val & ANA_TABLES_MACACCESS_VALID))
1215 return -EINVAL;
1216
1217 /* If the entry read has another port configured as its destination,
1218 * do not report it.
1219 */
1220 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001221 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001222 return -EINVAL;
1223
1224 /* Get the entry's MAC address and VLAN id */
1225 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1226 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1227
1228 mac[0] = (mach >> 8) & 0xff;
1229 mac[1] = (mach >> 0) & 0xff;
1230 mac[2] = (macl >> 24) & 0xff;
1231 mac[3] = (macl >> 16) & 0xff;
1232 mac[4] = (macl >> 8) & 0xff;
1233 mac[5] = (macl >> 0) & 0xff;
1234
1235 entry->vid = (mach >> 16) & 0xfff;
1236 ether_addr_copy(entry->mac, mac);
1237
1238 return 0;
1239}
1240
Vladimir Oltean5e256362019-11-14 17:03:27 +02001241int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1242 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001243{
Vladimir Oltean24683462021-10-24 20:17:51 +03001244 int err = 0;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001245 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001246
Vladimir Oltean24683462021-10-24 20:17:51 +03001247 /* We could take the lock just around ocelot_mact_read, but doing so
1248 * thousands of times in a row seems rather pointless and inefficient.
1249 */
1250 mutex_lock(&ocelot->mact_lock);
1251
Vladimir Oltean21ce7f32020-05-04 01:20:26 +03001252 /* Loop through all the mac tables entries. */
1253 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001254 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001255 struct ocelot_mact_entry entry;
1256 bool is_static;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001257
Vladimir Oltean24683462021-10-24 20:17:51 +03001258 err = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001259 /* If the entry is invalid (wrong port, invalid...),
1260 * skip it.
1261 */
Vladimir Oltean24683462021-10-24 20:17:51 +03001262 if (err == -EINVAL)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001263 continue;
Vladimir Oltean24683462021-10-24 20:17:51 +03001264 else if (err)
1265 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001266
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001267 is_static = (entry.type == ENTRYTYPE_LOCKED);
1268
Vladimir Oltean24683462021-10-24 20:17:51 +03001269 err = cb(entry.mac, entry.vid, is_static, data);
1270 if (err)
1271 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001272 }
1273 }
1274
Vladimir Oltean24683462021-10-24 20:17:51 +03001275 mutex_unlock(&ocelot->mact_lock);
1276
1277 return err;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001278}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001279EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001280
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001281static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
1282{
1283 trap->key_type = OCELOT_VCAP_KEY_ETYPE;
1284 *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
1285 *(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
1286}
1287
1288static void
1289ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1290{
1291 trap->key_type = OCELOT_VCAP_KEY_IPV4;
1292 trap->key.ipv4.dport.value = PTP_EV_PORT;
1293 trap->key.ipv4.dport.mask = 0xffff;
1294}
1295
1296static void
1297ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1298{
1299 trap->key_type = OCELOT_VCAP_KEY_IPV6;
1300 trap->key.ipv6.dport.value = PTP_EV_PORT;
1301 trap->key.ipv6.dport.mask = 0xffff;
1302}
1303
1304static void
1305ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1306{
1307 trap->key_type = OCELOT_VCAP_KEY_IPV4;
1308 trap->key.ipv4.dport.value = PTP_GEN_PORT;
1309 trap->key.ipv4.dport.mask = 0xffff;
1310}
1311
1312static void
1313ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1314{
1315 trap->key_type = OCELOT_VCAP_KEY_IPV6;
1316 trap->key.ipv6.dport.value = PTP_GEN_PORT;
1317 trap->key.ipv6.dport.mask = 0xffff;
1318}
1319
1320static int ocelot_trap_add(struct ocelot *ocelot, int port,
1321 unsigned long cookie,
1322 void (*populate)(struct ocelot_vcap_filter *f))
1323{
1324 struct ocelot_vcap_block *block_vcap_is2;
1325 struct ocelot_vcap_filter *trap;
1326 bool new = false;
1327 int err;
1328
1329 block_vcap_is2 = &ocelot->block[VCAP_IS2];
1330
1331 trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1332 false);
1333 if (!trap) {
1334 trap = kzalloc(sizeof(*trap), GFP_KERNEL);
1335 if (!trap)
1336 return -ENOMEM;
1337
1338 populate(trap);
1339 trap->prio = 1;
1340 trap->id.cookie = cookie;
1341 trap->id.tc_offload = false;
1342 trap->block_id = VCAP_IS2;
1343 trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
1344 trap->lookup = 0;
1345 trap->action.cpu_copy_ena = true;
1346 trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
1347 trap->action.port_mask = 0;
1348 new = true;
1349 }
1350
1351 trap->ingress_port_mask |= BIT(port);
1352
1353 if (new)
1354 err = ocelot_vcap_filter_add(ocelot, trap, NULL);
1355 else
1356 err = ocelot_vcap_filter_replace(ocelot, trap);
1357 if (err) {
1358 trap->ingress_port_mask &= ~BIT(port);
1359 if (!trap->ingress_port_mask)
1360 kfree(trap);
1361 return err;
1362 }
1363
1364 return 0;
1365}
1366
1367static int ocelot_trap_del(struct ocelot *ocelot, int port,
1368 unsigned long cookie)
1369{
1370 struct ocelot_vcap_block *block_vcap_is2;
1371 struct ocelot_vcap_filter *trap;
1372
1373 block_vcap_is2 = &ocelot->block[VCAP_IS2];
1374
1375 trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1376 false);
1377 if (!trap)
1378 return 0;
1379
1380 trap->ingress_port_mask &= ~BIT(port);
1381 if (!trap->ingress_port_mask)
1382 return ocelot_vcap_filter_del(ocelot, trap);
1383
1384 return ocelot_vcap_filter_replace(ocelot, trap);
1385}
1386
1387static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
1388{
1389 unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1390
1391 return ocelot_trap_add(ocelot, port, l2_cookie,
1392 ocelot_populate_l2_ptp_trap_key);
1393}
1394
1395static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
1396{
1397 unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1398
1399 return ocelot_trap_del(ocelot, port, l2_cookie);
1400}
1401
1402static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
1403{
1404 unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1405 unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1406 int err;
1407
1408 err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie,
1409 ocelot_populate_ipv4_ptp_event_trap_key);
1410 if (err)
1411 return err;
1412
1413 err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie,
1414 ocelot_populate_ipv4_ptp_general_trap_key);
1415 if (err)
1416 ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1417
1418 return err;
1419}
1420
1421static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
1422{
1423 unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1424 unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1425 int err;
1426
1427 err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1428 err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
1429 return err;
1430}
1431
1432static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
1433{
1434 unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1435 unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1436 int err;
1437
1438 err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie,
1439 ocelot_populate_ipv6_ptp_event_trap_key);
1440 if (err)
1441 return err;
1442
1443 err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie,
1444 ocelot_populate_ipv6_ptp_general_trap_key);
1445 if (err)
1446 ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1447
1448 return err;
1449}
1450
1451static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
1452{
1453 unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1454 unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1455 int err;
1456
1457 err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1458 err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
1459 return err;
1460}
1461
1462static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
1463 bool l2, bool l4)
1464{
1465 int err;
1466
1467 if (l2)
1468 err = ocelot_l2_ptp_trap_add(ocelot, port);
1469 else
1470 err = ocelot_l2_ptp_trap_del(ocelot, port);
1471 if (err)
1472 return err;
1473
1474 if (l4) {
1475 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
1476 if (err)
1477 goto err_ipv4;
1478
1479 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
1480 if (err)
1481 goto err_ipv6;
1482 } else {
1483 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
1484
1485 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
1486 }
1487 if (err)
1488 return err;
1489
1490 return 0;
1491
1492err_ipv6:
1493 ocelot_ipv4_ptp_trap_del(ocelot, port);
1494err_ipv4:
1495 if (l2)
1496 ocelot_l2_ptp_trap_del(ocelot, port);
1497 return err;
1498}
1499
Yangbo Luf1459222019-11-20 16:23:14 +08001500int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001501{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001502 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1503 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1504}
Yangbo Luf1459222019-11-20 16:23:14 +08001505EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001506
Yangbo Luf1459222019-11-20 16:23:14 +08001507int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001508{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001509 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001510 bool l2 = false, l4 = false;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001511 struct hwtstamp_config cfg;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001512 int err;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001513
1514 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1515 return -EFAULT;
1516
1517 /* reserved for future extensions */
1518 if (cfg.flags)
1519 return -EINVAL;
1520
1521 /* Tx type sanity check */
1522 switch (cfg.tx_type) {
1523 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001524 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001525 break;
1526 case HWTSTAMP_TX_ONESTEP_SYNC:
1527 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1528 * need to update the origin time.
1529 */
Vladimir Oltean306fd442019-11-09 15:02:50 +02001530 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001531 break;
1532 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001533 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001534 break;
1535 default:
1536 return -ERANGE;
1537 }
1538
1539 mutex_lock(&ocelot->ptp_lock);
1540
1541 switch (cfg.rx_filter) {
1542 case HWTSTAMP_FILTER_NONE:
1543 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001544 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1545 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1546 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001547 l4 = true;
1548 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001549 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1550 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1551 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001552 l2 = true;
1553 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001554 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1555 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1556 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001557 l2 = true;
1558 l4 = true;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001559 break;
1560 default:
1561 mutex_unlock(&ocelot->ptp_lock);
1562 return -ERANGE;
1563 }
1564
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001565 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
1566 if (err)
1567 return err;
1568
1569 if (l2 && l4)
1570 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1571 else if (l2)
1572 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1573 else if (l4)
1574 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
1575 else
1576 cfg.rx_filter = HWTSTAMP_FILTER_NONE;
1577
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001578 /* Commit back the result & save it */
1579 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1580 mutex_unlock(&ocelot->ptp_lock);
1581
1582 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1583}
Yangbo Luf1459222019-11-20 16:23:14 +08001584EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001585
Vladimir Oltean5e256362019-11-14 17:03:27 +02001586void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001587{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001588 int i;
1589
1590 if (sset != ETH_SS_STATS)
1591 return;
1592
1593 for (i = 0; i < ocelot->num_stats; i++)
1594 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1595 ETH_GSTRING_LEN);
1596}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001597EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001598
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001599static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001600{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001601 int i, j;
1602
1603 mutex_lock(&ocelot->stats_lock);
1604
1605 for (i = 0; i < ocelot->num_phys_ports; i++) {
1606 /* Configure the port to read the stats from */
1607 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1608
1609 for (j = 0; j < ocelot->num_stats; j++) {
1610 u32 val;
1611 unsigned int idx = i * ocelot->num_stats + j;
1612
1613 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1614 ocelot->stats_layout[j].offset);
1615
1616 if (val < (ocelot->stats[idx] & U32_MAX))
1617 ocelot->stats[idx] += (u64)1 << 32;
1618
1619 ocelot->stats[idx] = (ocelot->stats[idx] &
1620 ~(u64)U32_MAX) + val;
1621 }
1622 }
1623
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001624 mutex_unlock(&ocelot->stats_lock);
1625}
1626
1627static void ocelot_check_stats_work(struct work_struct *work)
1628{
1629 struct delayed_work *del_work = to_delayed_work(work);
1630 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1631 stats_work);
1632
1633 ocelot_update_stats(ocelot);
1634
Alexandre Bellonia556c762018-05-14 22:04:57 +02001635 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1636 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001637}
1638
Vladimir Oltean5e256362019-11-14 17:03:27 +02001639void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001640{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001641 int i;
1642
1643 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001644 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001645
1646 /* Copy all counters */
1647 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001648 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +02001649}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001650EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001651
Vladimir Oltean5e256362019-11-14 17:03:27 +02001652int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001653{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001654 if (sset != ETH_SS_STATS)
1655 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001656
Alexandre Bellonia556c762018-05-14 22:04:57 +02001657 return ocelot->num_stats;
1658}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001659EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001660
Vladimir Oltean5e256362019-11-14 17:03:27 +02001661int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1662 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001663{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001664 info->phc_index = ocelot->ptp_clock ?
1665 ptp_clock_index(ocelot->ptp_clock) : -1;
Yangbo Lud2b09a82020-04-20 10:46:46 +08001666 if (info->phc_index == -1) {
1667 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1668 SOF_TIMESTAMPING_RX_SOFTWARE |
1669 SOF_TIMESTAMPING_SOFTWARE;
1670 return 0;
1671 }
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001672 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1673 SOF_TIMESTAMPING_RX_SOFTWARE |
1674 SOF_TIMESTAMPING_SOFTWARE |
1675 SOF_TIMESTAMPING_TX_HARDWARE |
1676 SOF_TIMESTAMPING_RX_HARDWARE |
1677 SOF_TIMESTAMPING_RAW_HARDWARE;
1678 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1679 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
Vladimir Olteanc49a35e2021-11-26 19:28:45 +02001680 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1681 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1682 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1683 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001684
1685 return 0;
1686}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001687EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001688
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001689static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
1690 bool only_active_ports)
Vladimir Olteanb80af652021-02-06 00:02:14 +02001691{
1692 u32 mask = 0;
1693 int port;
1694
1695 for (port = 0; port < ocelot->num_phys_ports; port++) {
1696 struct ocelot_port *ocelot_port = ocelot->ports[port];
1697
1698 if (!ocelot_port)
1699 continue;
1700
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001701 if (ocelot_port->bond == bond) {
1702 if (only_active_ports && !ocelot_port->lag_tx_active)
1703 continue;
1704
Vladimir Olteanb80af652021-02-06 00:02:14 +02001705 mask |= BIT(port);
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001706 }
Vladimir Olteanb80af652021-02-06 00:02:14 +02001707 }
1708
1709 return mask;
1710}
1711
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001712static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
Vladimir Olteandf291e52021-03-19 01:36:36 +02001713 struct net_device *bridge)
1714{
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001715 struct ocelot_port *ocelot_port = ocelot->ports[src_port];
Vladimir Olteandf291e52021-03-19 01:36:36 +02001716 u32 mask = 0;
1717 int port;
1718
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001719 if (!ocelot_port || ocelot_port->bridge != bridge ||
1720 ocelot_port->stp_state != BR_STATE_FORWARDING)
1721 return 0;
1722
Vladimir Olteandf291e52021-03-19 01:36:36 +02001723 for (port = 0; port < ocelot->num_phys_ports; port++) {
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001724 ocelot_port = ocelot->ports[port];
Vladimir Olteandf291e52021-03-19 01:36:36 +02001725
1726 if (!ocelot_port)
1727 continue;
1728
1729 if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1730 ocelot_port->bridge == bridge)
1731 mask |= BIT(port);
1732 }
1733
1734 return mask;
1735}
1736
Vladimir Olteane21268e2021-01-29 03:00:09 +02001737static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
Vladimir Oltean9b521252021-01-29 03:00:02 +02001738{
Vladimir Olteane21268e2021-01-29 03:00:09 +02001739 u32 mask = 0;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001740 int port;
1741
Vladimir Olteane21268e2021-01-29 03:00:09 +02001742 for (port = 0; port < ocelot->num_phys_ports; port++) {
1743 struct ocelot_port *ocelot_port = ocelot->ports[port];
1744
1745 if (!ocelot_port)
1746 continue;
1747
1748 if (ocelot_port->is_dsa_8021q_cpu)
1749 mask |= BIT(port);
1750 }
1751
1752 return mask;
1753}
1754
1755void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1756{
1757 unsigned long cpu_fwd_mask;
1758 int port;
1759
1760 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1761 * regular forwarding path of the front ports regardless of whether
1762 * those are bridged or standalone.
1763 * If DSA tag_8021q is not used, this returns 0, which is fine because
1764 * the hardware-based CPU port module can be a destination for packets
1765 * even if it isn't part of PGID_SRC.
1766 */
1767 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1768
Vladimir Oltean9b521252021-01-29 03:00:02 +02001769 /* Apply FWD mask. The loop is needed to add/remove the current port as
1770 * a source for the other ports.
1771 */
1772 for (port = 0; port < ocelot->num_phys_ports; port++) {
Vladimir Olteane21268e2021-01-29 03:00:09 +02001773 struct ocelot_port *ocelot_port = ocelot->ports[port];
1774 unsigned long mask;
1775
1776 if (!ocelot_port) {
1777 /* Unused ports can't send anywhere */
1778 mask = 0;
1779 } else if (ocelot_port->is_dsa_8021q_cpu) {
1780 /* The DSA tag_8021q CPU ports need to be able to
1781 * forward packets to all other ports except for
1782 * themselves
1783 */
1784 mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1785 mask &= ~cpu_fwd_mask;
Vladimir Olteandf291e52021-03-19 01:36:36 +02001786 } else if (ocelot_port->bridge) {
1787 struct net_device *bridge = ocelot_port->bridge;
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001788 struct net_device *bond = ocelot_port->bond;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001789
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001790 mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
Vladimir Olteanc1930142021-08-17 19:04:25 +03001791 mask |= cpu_fwd_mask;
Vladimir Olteandf291e52021-03-19 01:36:36 +02001792 mask &= ~BIT(port);
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001793 if (bond) {
1794 mask &= ~ocelot_get_bond_mask(ocelot, bond,
1795 false);
1796 }
Vladimir Oltean9b521252021-01-29 03:00:02 +02001797 } else {
Vladimir Olteane21268e2021-01-29 03:00:09 +02001798 /* Standalone ports forward only to DSA tag_8021q CPU
1799 * ports (if those exist), or to the hardware CPU port
1800 * module otherwise.
1801 */
1802 mask = cpu_fwd_mask;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001803 }
Vladimir Olteane21268e2021-01-29 03:00:09 +02001804
1805 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001806 }
1807}
Vladimir Olteane21268e2021-01-29 03:00:09 +02001808EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001809
Vladimir Oltean5e256362019-11-14 17:03:27 +02001810void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001811{
Vladimir Oltean421741e2021-02-12 17:15:59 +02001812 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteandf291e52021-03-19 01:36:36 +02001813 u32 learn_ena = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001814
Vladimir Olteandf291e52021-03-19 01:36:36 +02001815 ocelot_port->stp_state = state;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001816
Vladimir Olteandf291e52021-03-19 01:36:36 +02001817 if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1818 ocelot_port->learn_ena)
1819 learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001820
Vladimir Olteandf291e52021-03-19 01:36:36 +02001821 ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1822 ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001823
Vladimir Oltean9b521252021-01-29 03:00:02 +02001824 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001825}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001826EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001827
Vladimir Oltean5e256362019-11-14 17:03:27 +02001828void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001829{
Vladimir Olteanc0d7ecc2020-05-04 01:20:27 +03001830 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1831
1832 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1833 * which is clearly not what our intention is. So avoid that.
1834 */
1835 if (!age_period)
1836 age_period = 1;
1837
1838 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001839}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001840EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001841
Alexandre Bellonia556c762018-05-14 22:04:57 +02001842static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1843 const unsigned char *addr,
1844 u16 vid)
1845{
1846 struct ocelot_multicast *mc;
1847
1848 list_for_each_entry(mc, &ocelot->multicast, list) {
1849 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1850 return mc;
1851 }
1852
1853 return NULL;
1854}
1855
Vladimir Oltean9403c152020-06-21 14:46:03 +03001856static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1857{
1858 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1859 return ENTRYTYPE_MACv4;
1860 if (addr[0] == 0x33 && addr[1] == 0x33)
1861 return ENTRYTYPE_MACv6;
Vladimir Oltean7c313142020-10-29 04:27:34 +02001862 return ENTRYTYPE_LOCKED;
Vladimir Oltean9403c152020-06-21 14:46:03 +03001863}
1864
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001865static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1866 unsigned long ports)
Vladimir Oltean9403c152020-06-21 14:46:03 +03001867{
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001868 struct ocelot_pgid *pgid;
1869
1870 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1871 if (!pgid)
1872 return ERR_PTR(-ENOMEM);
1873
1874 pgid->ports = ports;
1875 pgid->index = index;
1876 refcount_set(&pgid->refcount, 1);
1877 list_add_tail(&pgid->list, &ocelot->pgids);
1878
1879 return pgid;
1880}
1881
1882static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1883{
1884 if (!refcount_dec_and_test(&pgid->refcount))
1885 return;
1886
1887 list_del(&pgid->list);
1888 kfree(pgid);
1889}
1890
1891static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1892 const struct ocelot_multicast *mc)
1893{
1894 struct ocelot_pgid *pgid;
1895 int index;
Vladimir Oltean9403c152020-06-21 14:46:03 +03001896
1897 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1898 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1899 * destination mask table (PGID), the destination set is programmed as
1900 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1901 */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001902 if (mc->entry_type == ENTRYTYPE_MACv4 ||
1903 mc->entry_type == ENTRYTYPE_MACv6)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001904 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001905
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001906 list_for_each_entry(pgid, &ocelot->pgids, list) {
1907 /* When searching for a nonreserved multicast PGID, ignore the
1908 * dummy PGID of zero that we have for MACv4/MACv6 entries
1909 */
1910 if (pgid->index && pgid->ports == mc->ports) {
1911 refcount_inc(&pgid->refcount);
1912 return pgid;
1913 }
1914 }
1915
1916 /* Search for a free index in the nonreserved multicast PGID area */
1917 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001918 bool used = false;
1919
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001920 list_for_each_entry(pgid, &ocelot->pgids, list) {
1921 if (pgid->index == index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001922 used = true;
1923 break;
1924 }
1925 }
1926
1927 if (!used)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001928 return ocelot_pgid_alloc(ocelot, index, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001929 }
1930
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001931 return ERR_PTR(-ENOSPC);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001932}
1933
1934static void ocelot_encode_ports_to_mdb(unsigned char *addr,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001935 struct ocelot_multicast *mc)
Vladimir Oltean9403c152020-06-21 14:46:03 +03001936{
Vladimir Olteanebbd8602020-10-29 04:27:35 +02001937 ether_addr_copy(addr, mc->addr);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001938
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001939 if (mc->entry_type == ENTRYTYPE_MACv4) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001940 addr[0] = 0;
1941 addr[1] = mc->ports >> 8;
1942 addr[2] = mc->ports & 0xff;
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001943 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001944 addr[0] = mc->ports >> 8;
1945 addr[1] = mc->ports & 0xff;
1946 }
1947}
1948
Vladimir Oltean209edf92020-06-21 14:46:01 +03001949int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1950 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001951{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001952 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001953 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001954 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001955 u16 vid = mdb->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001956
Vladimir Oltean471beb12020-06-21 14:46:00 +03001957 if (port == ocelot->npi)
1958 port = ocelot->num_phys_ports;
1959
Alexandre Bellonia556c762018-05-14 22:04:57 +02001960 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1961 if (!mc) {
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001962 /* New entry */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001963 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1964 if (!mc)
1965 return -ENOMEM;
1966
1967 mc->entry_type = ocelot_classify_mdb(mdb->addr);
1968 ether_addr_copy(mc->addr, mdb->addr);
1969 mc->vid = vid;
1970
Alexandre Bellonia556c762018-05-14 22:04:57 +02001971 list_add_tail(&mc->list, &ocelot->multicast);
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001972 } else {
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001973 /* Existing entry. Clean up the current port mask from
1974 * hardware now, because we'll be modifying it.
1975 */
1976 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001977 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001978 ocelot_mact_forget(ocelot, addr, vid);
1979 }
1980
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001981 mc->ports |= BIT(port);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001982
1983 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1984 if (IS_ERR(pgid)) {
1985 dev_err(ocelot->dev,
1986 "Cannot allocate PGID for mdb %pM vid %d\n",
1987 mc->addr, mc->vid);
1988 devm_kfree(ocelot->dev, mc);
1989 return PTR_ERR(pgid);
1990 }
1991 mc->pgid = pgid;
1992
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001993 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001994
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001995 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1996 mc->entry_type != ENTRYTYPE_MACv6)
1997 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1998 pgid->index);
1999
2000 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002001 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002002}
Vladimir Oltean209edf92020-06-21 14:46:01 +03002003EXPORT_SYMBOL(ocelot_port_mdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002004
Vladimir Oltean209edf92020-06-21 14:46:01 +03002005int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
2006 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002007{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002008 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002009 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002010 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002011 u16 vid = mdb->vid;
2012
Vladimir Oltean471beb12020-06-21 14:46:00 +03002013 if (port == ocelot->npi)
2014 port = ocelot->num_phys_ports;
2015
Alexandre Bellonia556c762018-05-14 22:04:57 +02002016 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2017 if (!mc)
2018 return -ENOENT;
2019
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002020 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002021 ocelot_mact_forget(ocelot, addr, vid);
2022
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002023 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002024 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002025 if (!mc->ports) {
2026 list_del(&mc->list);
2027 devm_kfree(ocelot->dev, mc);
2028 return 0;
2029 }
2030
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002031 /* We have a PGID with fewer ports now */
2032 pgid = ocelot_mdb_get_pgid(ocelot, mc);
2033 if (IS_ERR(pgid))
2034 return PTR_ERR(pgid);
2035 mc->pgid = pgid;
2036
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002037 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002038
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002039 if (mc->entry_type != ENTRYTYPE_MACv4 &&
2040 mc->entry_type != ENTRYTYPE_MACv6)
2041 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2042 pgid->index);
2043
2044 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002045 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002046}
Vladimir Oltean209edf92020-06-21 14:46:01 +03002047EXPORT_SYMBOL(ocelot_port_mdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002048
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002049void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
2050 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002051{
Vladimir Olteandf291e52021-03-19 01:36:36 +02002052 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +02002053
Vladimir Olteandf291e52021-03-19 01:36:36 +02002054 ocelot_port->bridge = bridge;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002055
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002056 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002057}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002058EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002059
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002060void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2061 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002062{
Vladimir Olteandf291e52021-03-19 01:36:36 +02002063 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean2e554a72020-10-03 01:06:46 +03002064
Vladimir Olteandf291e52021-03-19 01:36:36 +02002065 ocelot_port->bridge = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02002066
Vladimir Olteand4004422021-10-20 20:58:52 +03002067 ocelot_port_set_pvid(ocelot, port, NULL);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +03002068 ocelot_port_manage_port_tag(ocelot, port);
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002069 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002070}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002071EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002072
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002073static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2074{
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002075 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002076 int i, port, lag;
2077
2078 /* Reset destination and aggregation PGIDS */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002079 for_each_unicast_dest_pgid(ocelot, port)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002080 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2081
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002082 for_each_aggr_pgid(ocelot, i)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002083 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2084 ANA_PGID_PGID, i);
2085
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002086 /* The visited ports bitmask holds the list of ports offloading any
2087 * bonding interface. Initially we mark all these ports as unvisited,
2088 * then every time we visit a port in this bitmask, we know that it is
2089 * the lowest numbered port, i.e. the one whose logical ID == physical
2090 * port ID == LAG ID. So we mark as visited all further ports in the
2091 * bitmask that are offloading the same bonding interface. This way,
2092 * we set up the aggregation PGIDs only once per bonding interface.
2093 */
2094 for (port = 0; port < ocelot->num_phys_ports; port++) {
2095 struct ocelot_port *ocelot_port = ocelot->ports[port];
2096
2097 if (!ocelot_port || !ocelot_port->bond)
2098 continue;
2099
2100 visited &= ~BIT(port);
2101 }
2102
2103 /* Now, set PGIDs for each active LAG */
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002104 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002105 struct net_device *bond = ocelot->ports[lag]->bond;
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002106 int num_active_ports = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002107 unsigned long bond_mask;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002108 u8 aggr_idx[16];
2109
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002110 if (!bond || (visited & BIT(lag)))
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002111 continue;
2112
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002113 bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002114
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002115 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2116 // Destination mask
2117 ocelot_write_rix(ocelot, bond_mask,
2118 ANA_PGID_PGID, port);
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002119 aggr_idx[num_active_ports++] = port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002120 }
2121
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002122 for_each_aggr_pgid(ocelot, i) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002123 u32 ac;
2124
2125 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2126 ac &= ~bond_mask;
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002127 /* Don't do division by zero if there was no active
2128 * port. Just make all aggregation codes zero.
2129 */
2130 if (num_active_ports)
2131 ac |= BIT(aggr_idx[i % num_active_ports]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002132 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2133 }
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002134
2135 /* Mark all ports in the same LAG as visited to avoid applying
2136 * the same config again.
2137 */
2138 for (port = lag; port < ocelot->num_phys_ports; port++) {
2139 struct ocelot_port *ocelot_port = ocelot->ports[port];
2140
2141 if (!ocelot_port)
2142 continue;
2143
2144 if (ocelot_port->bond == bond)
2145 visited |= BIT(port);
2146 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002147 }
2148}
2149
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002150/* When offloading a bonding interface, the switch ports configured under the
2151 * same bond must have the same logical port ID, equal to the physical port ID
2152 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
2153 * bridged mode, each port has a logical port ID equal to its physical port ID.
2154 */
2155static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002156{
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002157 int port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002158
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002159 for (port = 0; port < ocelot->num_phys_ports; port++) {
2160 struct ocelot_port *ocelot_port = ocelot->ports[port];
2161 struct net_device *bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002162
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002163 if (!ocelot_port)
2164 continue;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002165
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002166 bond = ocelot_port->bond;
2167 if (bond) {
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002168 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
2169 false));
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002170
2171 ocelot_rmw_gix(ocelot,
2172 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
2173 ANA_PORT_PORT_CFG_PORTID_VAL_M,
2174 ANA_PORT_PORT_CFG, port);
2175 } else {
2176 ocelot_rmw_gix(ocelot,
2177 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2178 ANA_PORT_PORT_CFG_PORTID_VAL_M,
2179 ANA_PORT_PORT_CFG, port);
2180 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002181 }
2182}
2183
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002184int ocelot_port_lag_join(struct ocelot *ocelot, int port,
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02002185 struct net_device *bond,
2186 struct netdev_lag_upper_info *info)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002187{
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02002188 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2189 return -EOPNOTSUPP;
2190
Vladimir Olteanb80af652021-02-06 00:02:14 +02002191 ocelot->ports[port]->bond = bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002192
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002193 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean9b521252021-01-29 03:00:02 +02002194 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002195 ocelot_set_aggr_pgids(ocelot);
2196
2197 return 0;
2198}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002199EXPORT_SYMBOL(ocelot_port_lag_join);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002200
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002201void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2202 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002203{
Vladimir Olteanb80af652021-02-06 00:02:14 +02002204 ocelot->ports[port]->bond = NULL;
2205
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002206 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean9b521252021-01-29 03:00:02 +02002207 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002208 ocelot_set_aggr_pgids(ocelot);
2209}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002210EXPORT_SYMBOL(ocelot_port_lag_leave);
Petr Machata0e332c82018-11-22 23:30:11 +00002211
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002212void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
2213{
2214 struct ocelot_port *ocelot_port = ocelot->ports[port];
2215
2216 ocelot_port->lag_tx_active = lag_tx_active;
2217
2218 /* Rebalance the LAGs */
2219 ocelot_set_aggr_pgids(ocelot);
2220}
2221EXPORT_SYMBOL(ocelot_port_lag_change);
2222
Vladimir Olteana8015de2020-03-10 03:28:18 +02002223/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2224 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002225 * In the special case that it's the NPI port that we're configuring, the
2226 * length of the tag and optional prefix needs to be accounted for privately,
2227 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02002228 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002229void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02002230{
2231 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02002232 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002233 int pause_start, pause_stop;
Vladimir Oltean601e9842020-10-05 12:09:11 +03002234 int atop, atop_tot;
Vladimir Oltean31350d72019-11-09 15:02:56 +02002235
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002236 if (port == ocelot->npi) {
2237 maxlen += OCELOT_TAG_LEN;
2238
Vladimir Olteancacea622021-01-29 03:00:03 +02002239 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002240 maxlen += OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02002241 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002242 maxlen += OCELOT_LONG_PREFIX_LEN;
2243 }
2244
Vladimir Olteana8015de2020-03-10 03:28:18 +02002245 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002246
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002247 /* Set Pause watermark hysteresis */
2248 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2249 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
Maxim Kochetkov541132f2020-07-13 19:57:07 +03002250 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2251 pause_start);
2252 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2253 pause_stop);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002254
Vladimir Oltean601e9842020-10-05 12:09:11 +03002255 /* Tail dropping watermarks */
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002256 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
Vladimir Olteana8015de2020-03-10 03:28:18 +02002257 OCELOT_BUFFER_CELL_SZ;
Vladimir Oltean601e9842020-10-05 12:09:11 +03002258 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2259 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2260 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002261}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002262EXPORT_SYMBOL(ocelot_port_set_maxlen);
2263
2264int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
2265{
2266 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
2267
2268 if (port == ocelot->npi) {
2269 max_mtu -= OCELOT_TAG_LEN;
2270
Vladimir Olteancacea622021-01-29 03:00:03 +02002271 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002272 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02002273 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002274 max_mtu -= OCELOT_LONG_PREFIX_LEN;
2275 }
2276
2277 return max_mtu;
2278}
2279EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002280
Vladimir Oltean421741e2021-02-12 17:15:59 +02002281static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2282 bool enabled)
2283{
2284 struct ocelot_port *ocelot_port = ocelot->ports[port];
2285 u32 val = 0;
2286
2287 if (enabled)
2288 val = ANA_PORT_PORT_CFG_LEARN_ENA;
2289
2290 ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2291 ANA_PORT_PORT_CFG, port);
2292
2293 ocelot_port->learn_ena = enabled;
2294}
2295
2296static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2297 bool enabled)
2298{
2299 u32 val = 0;
2300
2301 if (enabled)
2302 val = BIT(port);
2303
2304 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2305}
2306
2307static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2308 bool enabled)
2309{
2310 u32 val = 0;
2311
2312 if (enabled)
2313 val = BIT(port);
2314
2315 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2316}
2317
2318static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2319 bool enabled)
2320{
2321 u32 val = 0;
2322
2323 if (enabled)
2324 val = BIT(port);
2325
2326 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2327}
2328
2329int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2330 struct switchdev_brport_flags flags)
2331{
2332 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2333 BR_BCAST_FLOOD))
2334 return -EINVAL;
2335
2336 return 0;
2337}
2338EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2339
2340void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2341 struct switchdev_brport_flags flags)
2342{
2343 if (flags.mask & BR_LEARNING)
2344 ocelot_port_set_learning(ocelot, port,
2345 !!(flags.val & BR_LEARNING));
2346
2347 if (flags.mask & BR_FLOOD)
2348 ocelot_port_set_ucast_flood(ocelot, port,
2349 !!(flags.val & BR_FLOOD));
2350
2351 if (flags.mask & BR_MCAST_FLOOD)
2352 ocelot_port_set_mcast_flood(ocelot, port,
2353 !!(flags.val & BR_MCAST_FLOOD));
2354
2355 if (flags.mask & BR_BCAST_FLOOD)
2356 ocelot_port_set_bcast_flood(ocelot, port,
2357 !!(flags.val & BR_BCAST_FLOOD));
2358}
2359EXPORT_SYMBOL(ocelot_port_bridge_flags);
2360
Vladimir Oltean5e256362019-11-14 17:03:27 +02002361void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002362{
2363 struct ocelot_port *ocelot_port = ocelot->ports[port];
2364
Yangbo Lub049da12019-11-27 15:27:57 +08002365 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002366
2367 /* Basic L2 initialization */
2368
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002369 /* Set MAC IFG Gaps
2370 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2371 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2372 */
2373 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2374 DEV_MAC_IFG_CFG);
2375
2376 /* Load seed (0) and set MAC HDX late collision */
2377 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2378 DEV_MAC_HDX_CFG_SEED_LOAD,
2379 DEV_MAC_HDX_CFG);
2380 mdelay(1);
2381 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2382 DEV_MAC_HDX_CFG);
2383
2384 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02002385 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002386 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2387 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02002388 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002389 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2390 DEV_MAC_TAGS_CFG);
2391
2392 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2393 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2394 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2395
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002396 /* Enable transmission of pause frames */
Maxim Kochetkov541132f2020-07-13 19:57:07 +03002397 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002398
Vladimir Oltean31350d72019-11-09 15:02:56 +02002399 /* Drop frames with multicast source address */
2400 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2401 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2402 ANA_PORT_DROP_CFG, port);
2403
2404 /* Set default VLAN and tag type to 8021Q. */
2405 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2406 REW_PORT_VLAN_CFG_PORT_TPID_M,
2407 REW_PORT_VLAN_CFG, port);
2408
Vladimir Oltean421741e2021-02-12 17:15:59 +02002409 /* Disable source address learning for standalone mode */
2410 ocelot_port_set_learning(ocelot, port, false);
2411
Vladimir Oltean46efe4e2021-08-15 04:47:47 +03002412 /* Set the port's initial logical port ID value, enable receiving
2413 * frames on it, and configure the MAC address learning type to
2414 * automatic.
2415 */
2416 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
2417 ANA_PORT_PORT_CFG_RECV_ENA |
2418 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2419 ANA_PORT_PORT_CFG, port);
2420
Vladimir Oltean31350d72019-11-09 15:02:56 +02002421 /* Enable vcap lookups */
2422 ocelot_vcap_enable(ocelot, port);
2423}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002424EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002425
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002426/* Configure and enable the CPU port module, which is a set of queues
2427 * accessible through register MMIO, frame DMA or Ethernet (in case
2428 * NPI mode is used).
Vladimir Oltean69df5782020-02-29 16:50:02 +02002429 */
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002430static void ocelot_cpu_port_init(struct ocelot *ocelot)
Vladimir Oltean21468192019-11-09 15:03:00 +02002431{
Vladimir Oltean69df5782020-02-29 16:50:02 +02002432 int cpu = ocelot->num_phys_ports;
2433
2434 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02002435 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002436 /* Instead set up a multicast destination PGID for traffic copied to
2437 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2438 * addresses will be copied to the CPU via this PGID.
2439 */
Vladimir Oltean21468192019-11-09 15:03:00 +02002440 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2441 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2442 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2443 ANA_PORT_PORT_CFG, cpu);
2444
Vladimir Oltean69df5782020-02-29 16:50:02 +02002445 /* Enable CPU port module */
Vladimir Oltean886e1382020-07-13 19:57:03 +03002446 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002447 /* CPU port Injection/Extraction configuration */
Vladimir Oltean886e1382020-07-13 19:57:03 +03002448 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02002449 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean886e1382020-07-13 19:57:03 +03002450 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02002451 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean21468192019-11-09 15:03:00 +02002452
2453 /* Configure the CPU port to be VLAN aware */
Vladimir Olteanbfbab312021-10-20 20:58:51 +03002454 ocelot_write_gix(ocelot,
2455 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) |
2456 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2457 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
Vladimir Oltean21468192019-11-09 15:03:00 +02002458 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02002459}
Vladimir Oltean21468192019-11-09 15:03:00 +02002460
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002461static void ocelot_detect_features(struct ocelot *ocelot)
2462{
2463 int mmgt, eq_ctrl;
2464
2465 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2466 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2467 * 192 bytes as the documentation incorrectly says.
2468 */
2469 mmgt = ocelot_read(ocelot, SYS_MMGT);
2470 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2471
2472 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2473 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002474}
2475
Alexandre Bellonia556c762018-05-14 22:04:57 +02002476int ocelot_init(struct ocelot *ocelot)
2477{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002478 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02002479 int i, ret;
2480 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002481
Vladimir Oltean3a77b592019-11-14 17:03:26 +02002482 if (ocelot->ops->reset) {
2483 ret = ocelot->ops->reset(ocelot);
2484 if (ret) {
2485 dev_err(ocelot->dev, "Switch reset failed\n");
2486 return ret;
2487 }
2488 }
2489
Alexandre Bellonia556c762018-05-14 22:04:57 +02002490 ocelot->stats = devm_kcalloc(ocelot->dev,
2491 ocelot->num_phys_ports * ocelot->num_stats,
2492 sizeof(u64), GFP_KERNEL);
2493 if (!ocelot->stats)
2494 return -ENOMEM;
2495
2496 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002497 mutex_init(&ocelot->ptp_lock);
Vladimir Oltean24683462021-10-24 20:17:51 +03002498 mutex_init(&ocelot->mact_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002499 spin_lock_init(&ocelot->ptp_clock_lock);
Vladimir Oltean52849bc2021-10-12 14:40:36 +03002500 spin_lock_init(&ocelot->ts_id_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002501 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2502 dev_name(ocelot->dev));
2503 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2504 if (!ocelot->stats_queue)
2505 return -ENOMEM;
2506
Vladimir Olteanca0b2722020-12-12 21:16:12 +02002507 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2508 if (!ocelot->owq) {
2509 destroy_workqueue(ocelot->stats_queue);
2510 return -ENOMEM;
2511 }
2512
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02002513 INIT_LIST_HEAD(&ocelot->multicast);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002514 INIT_LIST_HEAD(&ocelot->pgids);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +03002515 INIT_LIST_HEAD(&ocelot->vlans);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002516 ocelot_detect_features(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002517 ocelot_mact_init(ocelot);
2518 ocelot_vlan_init(ocelot);
Vladimir Olteanaae4e502020-06-20 18:43:46 +03002519 ocelot_vcap_init(ocelot);
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002520 ocelot_cpu_port_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002521
2522 for (port = 0; port < ocelot->num_phys_ports; port++) {
2523 /* Clear all counters (5 groups) */
2524 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2525 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2526 SYS_STAT_CFG);
2527 }
2528
2529 /* Only use S-Tag */
2530 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2531
2532 /* Aggregation mode */
2533 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2534 ANA_AGGR_CFG_AC_DMAC_ENA |
2535 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
Vladimir Olteanf79c20c2021-02-06 00:02:13 +02002536 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2537 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2538 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2539 ANA_AGGR_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002540
2541 /* Set MAC age time to default value. The entry is aged after
2542 * 2*AGE_PERIOD
2543 */
2544 ocelot_write(ocelot,
2545 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2546 ANA_AUTOAGE);
2547
2548 /* Disable learning for frames discarded by VLAN ingress filtering */
2549 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2550
2551 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2552 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2553 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2554
2555 /* Setup flooding PGIDs */
Vladimir Olteanedd24102020-12-04 19:54:16 +02002556 for (i = 0; i < ocelot->num_flooding_pgids; i++)
2557 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
Vladimir Olteanb360d942021-02-12 17:15:58 +02002558 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
Vladimir Olteanedd24102020-12-04 19:54:16 +02002559 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2560 ANA_FLOODING, i);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002561 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2562 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2563 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2564 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2565 ANA_FLOODING_IPMC);
2566
2567 for (port = 0; port < ocelot->num_phys_ports; port++) {
2568 /* Transmit the frame to the local port. */
2569 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2570 /* Do not forward BPDU frames to the front ports. */
2571 ocelot_write_gix(ocelot,
2572 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2573 ANA_PORT_CPU_FWD_BPDU_CFG,
2574 port);
2575 /* Ensure bridging is disabled */
2576 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2577 }
2578
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002579 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02002580 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2581
2582 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2583 }
Horatiu Vulturebb1bb42021-03-16 21:10:17 +01002584
2585 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2586
Vladimir Olteanb360d942021-02-12 17:15:58 +02002587 /* Allow broadcast and unknown L2 multicast to the CPU. */
2588 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2589 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2590 ANA_PGID_PGID, PGID_MC);
2591 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2592 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2593 ANA_PGID_PGID, PGID_BC);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002594 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2595 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2596
Alexandre Bellonia556c762018-05-14 22:04:57 +02002597 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2598 * registers endianness.
2599 */
2600 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2601 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2602 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2603 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2604 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2605 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2606 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2607 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2608 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2609 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2610 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2611 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2612 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2613 for (i = 0; i < 16; i++)
2614 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2615 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2616 ANA_CPUQ_8021_CFG, i);
2617
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002618 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002619 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2620 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002621
Alexandre Bellonia556c762018-05-14 22:04:57 +02002622 return 0;
2623}
2624EXPORT_SYMBOL(ocelot_init);
2625
2626void ocelot_deinit(struct ocelot *ocelot)
2627{
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002628 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002629 destroy_workqueue(ocelot->stats_queue);
Vladimir Olteanca0b2722020-12-12 21:16:12 +02002630 destroy_workqueue(ocelot->owq);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002631 mutex_destroy(&ocelot->stats_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002632}
2633EXPORT_SYMBOL(ocelot_deinit);
2634
Vladimir Olteane5fb5122020-09-18 04:07:30 +03002635void ocelot_deinit_port(struct ocelot *ocelot, int port)
2636{
2637 struct ocelot_port *ocelot_port = ocelot->ports[port];
2638
2639 skb_queue_purge(&ocelot_port->tx_skbs);
2640}
2641EXPORT_SYMBOL(ocelot_deinit_port);
2642
Alexandre Bellonia556c762018-05-14 22:04:57 +02002643MODULE_LICENSE("Dual MIT/GPL");