blob: e6de86552df0eeb433ca68a7de88210647720c17 [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
Xiaoliang Yang0568c3b2021-11-18 18:11:57 +080064static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
65 const unsigned char mac[ETH_ALEN],
66 unsigned int vid, enum macaccess_entry_type type)
Alexandre 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
Alexandre Bellonia556c762018-05-14 22:04:57 +020086 ocelot_mact_select(ocelot, mac, vid);
87
88 /* Issue a write command */
Alban Bedel584b7cf2021-01-19 15:06:38 +010089 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
Alexandre Bellonia556c762018-05-14 22:04:57 +020090
Vladimir Oltean24683462021-10-24 20:17:51 +030091 err = ocelot_mact_wait_for_completion(ocelot);
92
Xiaoliang Yang0568c3b2021-11-18 18:11:57 +080093 return err;
94}
95
96int ocelot_mact_learn(struct ocelot *ocelot, int port,
97 const unsigned char mac[ETH_ALEN],
98 unsigned int vid, enum macaccess_entry_type type)
99{
100 int ret;
101
102 mutex_lock(&ocelot->mact_lock);
103 ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
Vladimir Oltean24683462021-10-24 20:17:51 +0300104 mutex_unlock(&ocelot->mact_lock);
105
Xiaoliang Yang0568c3b2021-11-18 18:11:57 +0800106 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200107}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300108EXPORT_SYMBOL(ocelot_mact_learn);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200109
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300110int ocelot_mact_forget(struct ocelot *ocelot,
111 const unsigned char mac[ETH_ALEN], unsigned int vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200112{
Vladimir Oltean24683462021-10-24 20:17:51 +0300113 int err;
114
115 mutex_lock(&ocelot->mact_lock);
116
Alexandre Bellonia556c762018-05-14 22:04:57 +0200117 ocelot_mact_select(ocelot, mac, vid);
118
119 /* Issue a forget command */
120 ocelot_write(ocelot,
121 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
122 ANA_TABLES_MACACCESS);
123
Vladimir Oltean24683462021-10-24 20:17:51 +0300124 err = ocelot_mact_wait_for_completion(ocelot);
125
126 mutex_unlock(&ocelot->mact_lock);
127
128 return err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200129}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300130EXPORT_SYMBOL(ocelot_mact_forget);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200131
Xiaoliang Yang0568c3b2021-11-18 18:11:57 +0800132int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
133 const unsigned char mac[ETH_ALEN],
134 unsigned int vid, enum macaccess_entry_type *type)
135{
136 int val;
137
138 mutex_lock(&ocelot->mact_lock);
139
140 ocelot_mact_select(ocelot, mac, vid);
141
142 /* Issue a read command with MACACCESS_VALID=1. */
143 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
144 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
145 ANA_TABLES_MACACCESS);
146
147 if (ocelot_mact_wait_for_completion(ocelot)) {
148 mutex_unlock(&ocelot->mact_lock);
149 return -ETIMEDOUT;
150 }
151
152 /* Read back the entry flags */
153 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
154
155 mutex_unlock(&ocelot->mact_lock);
156
157 if (!(val & ANA_TABLES_MACACCESS_VALID))
158 return -ENOENT;
159
160 *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
161 *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
162
163 return 0;
164}
165EXPORT_SYMBOL(ocelot_mact_lookup);
166
167int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
168 const unsigned char mac[ETH_ALEN],
169 unsigned int vid,
170 enum macaccess_entry_type type,
171 int sfid, int ssid)
172{
173 int ret;
174
175 mutex_lock(&ocelot->mact_lock);
176
177 ocelot_write(ocelot,
178 (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
179 ANA_TABLES_STREAMDATA_SFID(sfid) |
180 (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
181 ANA_TABLES_STREAMDATA_SSID(ssid),
182 ANA_TABLES_STREAMDATA);
183
184 ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
185
186 mutex_unlock(&ocelot->mact_lock);
187
188 return ret;
189}
190EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
191
Alexandre Bellonia556c762018-05-14 22:04:57 +0200192static void ocelot_mact_init(struct ocelot *ocelot)
193{
194 /* Configure the learning mode entries attributes:
195 * - Do not copy the frame to the CPU extraction queues.
196 * - Use the vlan and mac_cpoy for dmac lookup.
197 */
198 ocelot_rmw(ocelot, 0,
199 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
200 | ANA_AGENCTRL_LEARN_FWD_KILL
201 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
202 ANA_AGENCTRL);
203
Vladimir Oltean24683462021-10-24 20:17:51 +0300204 /* Clear the MAC table. We are not concurrent with anyone, so
205 * holding &ocelot->mact_lock is pointless.
206 */
Alexandre Bellonia556c762018-05-14 22:04:57 +0200207 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
208}
209
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200210static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200211{
212 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
213 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200214 ANA_PORT_VCAP_S2_CFG, port);
Xiaoliang Yang75944fd2020-10-02 15:02:23 +0300215
216 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
217 ANA_PORT_VCAP_CFG, port);
Xiaoliang Yang2f17c052020-10-02 15:02:24 +0300218
219 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
220 REW_PORT_CFG_ES0_EN,
221 REW_PORT_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200222}
223
Steen Hegelund639c1b22018-12-20 14:16:31 +0100224static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
225{
226 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
227}
228
Alexandre Bellonia556c762018-05-14 22:04:57 +0200229static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
230{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100231 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200232
Steen Hegelund639c1b22018-12-20 14:16:31 +0100233 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
234 ocelot,
235 val,
236 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
237 ANA_TABLES_VLANACCESS_CMD_IDLE,
238 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200239}
240
Antoine Tenart71425292018-06-26 14:28:49 +0200241static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
242{
243 /* Select the VID to configure */
244 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
245 ANA_TABLES_VLANTIDX);
246 /* Set the vlan port members mask and issue a write command */
247 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
248 ANA_TABLES_VLANACCESS_CMD_WRITE,
249 ANA_TABLES_VLANACCESS);
250
251 return ocelot_vlant_wait_for_completion(ocelot);
252}
253
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300254static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
255{
256 struct ocelot_bridge_vlan *vlan;
257 int num_untagged = 0;
258
259 list_for_each_entry(vlan, &ocelot->vlans, list) {
260 if (!(vlan->portmask & BIT(port)))
261 continue;
262
263 if (vlan->untagged & BIT(port))
264 num_untagged++;
265 }
266
267 return num_untagged;
268}
269
270static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
271{
272 struct ocelot_bridge_vlan *vlan;
273 int num_tagged = 0;
274
275 list_for_each_entry(vlan, &ocelot->vlans, list) {
276 if (!(vlan->portmask & BIT(port)))
277 continue;
278
279 if (!(vlan->untagged & BIT(port)))
280 num_tagged++;
281 }
282
283 return num_tagged;
284}
285
286/* We use native VLAN when we have to mix egress-tagged VLANs with exactly
287 * _one_ egress-untagged VLAN (_the_ native VLAN)
288 */
289static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
290{
291 return ocelot_port_num_tagged_vlans(ocelot, port) &&
292 ocelot_port_num_untagged_vlans(ocelot, port) == 1;
293}
294
295static struct ocelot_bridge_vlan *
296ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
297{
298 struct ocelot_bridge_vlan *vlan;
299
300 list_for_each_entry(vlan, &ocelot->vlans, list)
301 if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
302 return vlan;
303
304 return NULL;
305}
306
307/* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
308 * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
309 * state of the port.
310 */
311static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200312{
313 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300314 enum ocelot_port_tag_config tag_cfg;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300315 bool uses_native_vlan = false;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200316
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300317 if (ocelot_port->vlan_aware) {
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300318 uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
319
320 if (uses_native_vlan)
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300321 tag_cfg = OCELOT_PORT_TAG_NATIVE;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300322 else if (ocelot_port_num_untagged_vlans(ocelot, port))
323 tag_cfg = OCELOT_PORT_TAG_DISABLED;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300324 else
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300325 tag_cfg = OCELOT_PORT_TAG_TRUNK;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300326 } else {
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300327 tag_cfg = OCELOT_PORT_TAG_DISABLED;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300328 }
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300329
Vladimir Oltean62a22bc2021-10-20 20:58:48 +0300330 ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300331 REW_TAG_CFG_TAG_CFG_M,
332 REW_TAG_CFG, port);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300333
334 if (uses_native_vlan) {
335 struct ocelot_bridge_vlan *native_vlan;
336
337 /* Not having a native VLAN is impossible, because
338 * ocelot_port_num_untagged_vlans has returned 1.
339 * So there is no use in checking for NULL here.
340 */
341 native_vlan = ocelot_port_find_native_vlan(ocelot, port);
342
343 ocelot_rmw_gix(ocelot,
344 REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
345 REW_PORT_VLAN_CFG_PORT_VID_M,
346 REW_PORT_VLAN_CFG, port);
347 }
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200348}
349
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200350/* Default vlan to clasify for untagged frames (may be zero) */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200351static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
Vladimir Olteand4004422021-10-20 20:58:52 +0300352 const struct ocelot_bridge_vlan *pvid_vlan)
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200353{
354 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteand4004422021-10-20 20:58:52 +0300355 u16 pvid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200356 u32 val = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200357
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200358 ocelot_port->pvid_vlan = pvid_vlan;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200359
Vladimir Olteand4004422021-10-20 20:58:52 +0300360 if (ocelot_port->vlan_aware && pvid_vlan)
361 pvid = pvid_vlan->vid;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200362
363 ocelot_rmw_gix(ocelot,
Vladimir Olteand4004422021-10-20 20:58:52 +0300364 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200365 ANA_PORT_VLAN_CFG_VLAN_VID_M,
366 ANA_PORT_VLAN_CFG, port);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200367
368 /* If there's no pvid, we should drop not only untagged traffic (which
369 * happens automatically), but also 802.1p traffic which gets
370 * classified to VLAN 0, but that is always in our RX filter, so it
371 * would get accepted were it not for this setting.
372 */
Vladimir Olteand4004422021-10-20 20:58:52 +0300373 if (!pvid_vlan && ocelot_port->vlan_aware)
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200374 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
375 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
376
377 ocelot_rmw_gix(ocelot, val,
378 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
379 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
380 ANA_PORT_DROP_CFG, port);
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200381}
382
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300383static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
384 u16 vid)
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300385{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300386 struct ocelot_bridge_vlan *vlan;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300387
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300388 list_for_each_entry(vlan, &ocelot->vlans, list)
389 if (vlan->vid == vid)
390 return vlan;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300391
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300392 return NULL;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300393}
394
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300395static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
396 bool untagged)
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300397{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300398 struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
399 unsigned long portmask;
400 int err;
401
402 if (vlan) {
403 portmask = vlan->portmask | BIT(port);
404
405 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
406 if (err)
407 return err;
408
409 vlan->portmask = portmask;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300410 /* Bridge VLANs can be overwritten with a different
411 * egress-tagging setting, so make sure to override an untagged
412 * with a tagged VID if that's going on.
413 */
414 if (untagged)
415 vlan->untagged |= BIT(port);
416 else
417 vlan->untagged &= ~BIT(port);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300418
419 return 0;
420 }
421
422 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
423 if (!vlan)
424 return -ENOMEM;
425
426 portmask = BIT(port);
427
428 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
429 if (err) {
430 kfree(vlan);
431 return err;
432 }
433
434 vlan->vid = vid;
435 vlan->portmask = portmask;
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300436 if (untagged)
437 vlan->untagged = BIT(port);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300438 INIT_LIST_HEAD(&vlan->list);
439 list_add_tail(&vlan->list, &ocelot->vlans);
440
441 return 0;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300442}
443
444static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
445{
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300446 struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
447 unsigned long portmask;
448 int err;
449
450 if (!vlan)
451 return 0;
452
453 portmask = vlan->portmask & ~BIT(port);
454
455 err = ocelot_vlant_set_mask(ocelot, vid, portmask);
456 if (err)
457 return err;
458
459 vlan->portmask = portmask;
460 if (vlan->portmask)
461 return 0;
462
463 list_del(&vlan->list);
464 kfree(vlan);
465
466 return 0;
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300467}
468
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300469int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300470 bool vlan_aware, struct netlink_ext_ack *extack)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300471{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200472 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300473 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200474 struct ocelot_vcap_filter *filter;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300475 u32 val;
476
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200477 list_for_each_entry(filter, &block->rules, list) {
478 if (filter->ingress_port_mask & BIT(port) &&
479 filter->action.vid_replace_ena) {
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300480 NL_SET_ERR_MSG_MOD(extack,
481 "Cannot change VLAN state with vlan modify rules active");
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200482 return -EBUSY;
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300483 }
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300484 }
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300485
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300486 ocelot_port->vlan_aware = vlan_aware;
487
488 if (vlan_aware)
489 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
490 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
491 else
492 val = 0;
493 ocelot_rmw_gix(ocelot, val,
494 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
495 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
496 ANA_PORT_VLAN_CFG, port);
497
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200498 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300499 ocelot_port_manage_port_tag(ocelot, port);
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300500
501 return 0;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300502}
503EXPORT_SYMBOL(ocelot_port_vlan_filtering);
504
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200505int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
Vladimir Oltean01af9402021-08-19 20:40:06 +0300506 bool untagged, struct netlink_ext_ack *extack)
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200507{
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300508 if (untagged) {
509 /* We are adding an egress-tagged VLAN */
510 if (ocelot_port_uses_native_vlan(ocelot, port)) {
511 NL_SET_ERR_MSG_MOD(extack,
512 "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
513 return -EBUSY;
514 }
515 } else {
516 /* We are adding an egress-tagged VLAN */
517 if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
518 NL_SET_ERR_MSG_MOD(extack,
519 "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
520 return -EBUSY;
521 }
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200522 }
523
524 return 0;
525}
526EXPORT_SYMBOL(ocelot_vlan_prepare);
527
Vladimir Oltean5e256362019-11-14 17:03:27 +0200528int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
529 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200530{
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300531 int err;
Antoine Tenart71425292018-06-26 14:28:49 +0200532
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300533 err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300534 if (err)
535 return err;
Antoine Tenart71425292018-06-26 14:28:49 +0200536
537 /* Default ingress vlan classification */
Vladimir Olteand4004422021-10-20 20:58:52 +0300538 if (pvid)
539 ocelot_port_set_pvid(ocelot, port,
540 ocelot_bridge_vlan_find(ocelot, vid));
Antoine Tenart71425292018-06-26 14:28:49 +0200541
542 /* Untagged egress vlan clasification */
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300543 ocelot_port_manage_port_tag(ocelot, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200544
Antoine Tenart71425292018-06-26 14:28:49 +0200545 return 0;
546}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200547EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200548
Vladimir Oltean5e256362019-11-14 17:03:27 +0200549int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200550{
551 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300552 int err;
Antoine Tenart71425292018-06-26 14:28:49 +0200553
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300554 err = ocelot_vlan_member_del(ocelot, port, vid);
555 if (err)
556 return err;
Antoine Tenart71425292018-06-26 14:28:49 +0200557
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200558 /* Ingress */
Vladimir Olteand4004422021-10-20 20:58:52 +0300559 if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
560 ocelot_port_set_pvid(ocelot, port, NULL);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200561
Antoine Tenart71425292018-06-26 14:28:49 +0200562 /* Egress */
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +0300563 ocelot_port_manage_port_tag(ocelot, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200564
565 return 0;
566}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200567EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200568
Alexandre Bellonia556c762018-05-14 22:04:57 +0200569static void ocelot_vlan_init(struct ocelot *ocelot)
570{
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300571 unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200572 u16 port, vid;
573
Alexandre Bellonia556c762018-05-14 22:04:57 +0200574 /* Clear VLAN table, by default all ports are members of all VLANs */
575 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
576 ANA_TABLES_VLANACCESS);
577 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200578
579 /* Configure the port VLAN memberships */
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300580 for (vid = 1; vid < VLAN_N_VID; vid++)
Vladimir Oltean90e0aa82021-10-20 20:58:49 +0300581 ocelot_vlant_set_mask(ocelot, vid, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200582
583 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
584 * traffic. It is added automatically if 8021q module is loaded, but
585 * we can't rely on it since module may be not loaded.
586 */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300587 ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports);
Antoine Tenart71425292018-06-26 14:28:49 +0200588
Antoine Tenart71425292018-06-26 14:28:49 +0200589 /* Set vlan ingress filter mask to all ports but the CPU port by
590 * default.
591 */
Vladimir Olteanbbf6a2d2021-08-19 20:40:08 +0300592 ocelot_write(ocelot, all_ports, ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200593
594 for (port = 0; port < ocelot->num_phys_ports; port++) {
595 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
596 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
597 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200598}
599
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200600static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
601{
602 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
603}
604
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300605static int ocelot_port_flush(struct ocelot *ocelot, int port)
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200606{
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300607 unsigned int pause_ena;
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200608 int err, val;
609
610 /* Disable dequeuing from the egress queues */
611 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
612 QSYS_PORT_MODE_DEQUEUE_DIS,
613 QSYS_PORT_MODE, port);
614
615 /* Disable flow control */
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300616 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200617 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
618
619 /* Disable priority flow control */
620 ocelot_fields_write(ocelot, port,
621 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
622
623 /* Wait at least the time it takes to receive a frame of maximum length
624 * at the port.
625 * Worst-case delays for 10 kilobyte jumbo frames are:
626 * 8 ms on a 10M port
627 * 800 μs on a 100M port
628 * 80 μs on a 1G port
629 * 32 μs on a 2.5G port
630 */
631 usleep_range(8000, 10000);
632
633 /* Disable half duplex backpressure. */
634 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
635 SYS_FRONT_PORT_MODE, port);
636
637 /* Flush the queues associated with the port. */
638 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
639 REW_PORT_CFG, port);
640
641 /* Enable dequeuing from the egress queues. */
642 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
643 port);
644
645 /* Wait until flushing is complete. */
646 err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
647 100, 2000000, false, ocelot, port);
648
649 /* Clear flushing again. */
650 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
651
Vladimir Oltean1650bdb2021-06-08 14:15:35 +0300652 /* Re-enable flow control */
653 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
654
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200655 return err;
656}
Vladimir Olteaneb4733d2021-02-08 19:36:27 +0200657
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300658void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
659 unsigned int link_an_mode,
660 phy_interface_t interface,
661 unsigned long quirks)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200662{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200663 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300664 int err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200665
Vladimir Oltean8abe1972021-11-25 14:58:08 +0200666 ocelot_port->speed = SPEED_UNKNOWN;
667
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300668 ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
669 DEV_MAC_ENA_CFG);
670
Vladimir Oltean8abe1972021-11-25 14:58:08 +0200671 if (ocelot->ops->cut_through_fwd) {
672 mutex_lock(&ocelot->fwd_domain_lock);
673 ocelot->ops->cut_through_fwd(ocelot);
674 mutex_unlock(&ocelot->fwd_domain_lock);
675 }
676
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300677 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
678
679 err = ocelot_port_flush(ocelot, port);
680 if (err)
681 dev_err(ocelot->dev, "failed to flush port %d: %d\n",
682 port, err);
683
684 /* Put the port in reset. */
685 if (interface != PHY_INTERFACE_MODE_QSGMII ||
686 !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
687 ocelot_port_rmwl(ocelot_port,
688 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +0800689 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300690 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +0800691 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300692 DEV_CLOCK_CFG);
693}
694EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
695
696void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
697 struct phy_device *phydev,
698 unsigned int link_an_mode,
699 phy_interface_t interface,
700 int speed, int duplex,
701 bool tx_pause, bool rx_pause,
702 unsigned long quirks)
703{
704 struct ocelot_port *ocelot_port = ocelot->ports[port];
705 int mac_speed, mode = 0;
706 u32 mac_fc_cfg;
707
Vladimir Oltean8abe1972021-11-25 14:58:08 +0200708 ocelot_port->speed = speed;
709
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300710 /* The MAC might be integrated in systems where the MAC speed is fixed
711 * and it's the PCS who is performing the rate adaptation, so we have
712 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
713 * (which is also its default value).
714 */
715 if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
716 speed == SPEED_1000) {
717 mac_speed = OCELOT_SPEED_1000;
718 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
719 } else if (speed == SPEED_2500) {
720 mac_speed = OCELOT_SPEED_2500;
721 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
722 } else if (speed == SPEED_100) {
723 mac_speed = OCELOT_SPEED_100;
724 } else {
725 mac_speed = OCELOT_SPEED_10;
726 }
727
728 if (duplex == DUPLEX_FULL)
729 mode |= DEV_MAC_MODE_CFG_FDX_ENA;
730
731 ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
732
733 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
734 * PORT_RST bits in DEV_CLOCK_CFG.
735 */
736 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
737 DEV_CLOCK_CFG);
738
739 switch (speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200740 case SPEED_10:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300741 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200742 break;
743 case SPEED_100:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300744 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200745 break;
746 case SPEED_1000:
Alexandre Bellonia556c762018-05-14 22:04:57 +0200747 case SPEED_2500:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300748 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200749 break;
750 default:
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300751 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
752 port, speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200753 return;
754 }
755
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300756 /* Handle RX pause in all cases, with 2500base-X this is used for rate
757 * adaptation.
758 */
759 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200760
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300761 if (tx_pause)
762 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
763 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
764 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
765 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200766
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300767 /* Flow control. Link speed is only used here to evaluate the time
768 * specification in incoming pause frames.
769 */
770 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200771
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300772 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200773
Vladimir Oltean33cb0ff2022-01-12 22:21:27 +0200774 /* Don't attempt to send PAUSE frames on the NPI port, it's broken */
775 if (port != ocelot->npi)
776 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
777 tx_pause);
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200778
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300779 /* Undo the effects of ocelot_phylink_mac_link_down:
780 * enable MAC module
781 */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200782 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200783 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
784
Vladimir Oltean8abe1972021-11-25 14:58:08 +0200785 /* If the port supports cut-through forwarding, update the masks before
786 * enabling forwarding on the port.
787 */
788 if (ocelot->ops->cut_through_fwd) {
789 mutex_lock(&ocelot->fwd_domain_lock);
790 ocelot->ops->cut_through_fwd(ocelot);
791 mutex_unlock(&ocelot->fwd_domain_lock);
792 }
793
Alexandre Bellonia556c762018-05-14 22:04:57 +0200794 /* Core: Enable port for frame transfer */
Vladimir Oltean886e1382020-07-13 19:57:03 +0300795 ocelot_fields_write(ocelot, port,
796 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200797}
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300798EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200799
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300800static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
801 struct sk_buff *clone)
Yangbo Lu400928b2019-11-20 16:23:16 +0800802{
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300803 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300804 unsigned long flags;
Yangbo Lu400928b2019-11-20 16:23:16 +0800805
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300806 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
807
808 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
809 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
810 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
811 return -EBUSY;
812 }
Vladimir Oltean65652432020-09-18 04:07:24 +0300813
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300814 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
Yangbo Luc4b364c2021-04-27 12:22:00 +0800815 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
816 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300817
Vladimir Olteanc57fe002021-10-12 14:40:35 +0300818 ocelot_port->ts_id++;
819 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
820 ocelot_port->ts_id = 0;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300821
822 ocelot_port->ptp_skbs_in_flight++;
823 ocelot->ptp_skbs_in_flight++;
824
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300825 skb_queue_tail(&ocelot_port->tx_skbs, clone);
Vladimir Oltean65652432020-09-18 04:07:24 +0300826
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300827 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
828
829 return 0;
Yangbo Lu400928b2019-11-20 16:23:16 +0800830}
Yangbo Lu682eaad2021-04-27 12:22:02 +0800831
Vladimir Olteanfba01282021-10-12 14:40:38 +0300832static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
833 unsigned int ptp_class)
Yangbo Lu39e53082021-04-27 12:22:03 +0800834{
835 struct ptp_header *hdr;
Yangbo Lu39e53082021-04-27 12:22:03 +0800836 u8 msgtype, twostep;
837
Yangbo Lu39e53082021-04-27 12:22:03 +0800838 hdr = ptp_parse_header(skb, ptp_class);
839 if (!hdr)
840 return false;
841
842 msgtype = ptp_get_msgtype(hdr, ptp_class);
843 twostep = hdr->flag_field[0] & 0x2;
844
845 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
846 return true;
847
848 return false;
849}
850
Yangbo Lu682eaad2021-04-27 12:22:02 +0800851int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
852 struct sk_buff *skb,
853 struct sk_buff **clone)
854{
855 struct ocelot_port *ocelot_port = ocelot->ports[port];
856 u8 ptp_cmd = ocelot_port->ptp_cmd;
Vladimir Olteanfba01282021-10-12 14:40:38 +0300857 unsigned int ptp_class;
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300858 int err;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800859
Vladimir Olteanfba01282021-10-12 14:40:38 +0300860 /* Don't do anything if PTP timestamping not enabled */
861 if (!ptp_cmd)
862 return 0;
863
864 ptp_class = ptp_classify_raw(skb);
865 if (ptp_class == PTP_CLASS_NONE)
866 return -EINVAL;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800867
Yangbo Lu39e53082021-04-27 12:22:03 +0800868 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
869 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
Vladimir Olteanfba01282021-10-12 14:40:38 +0300870 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
Yangbo Lu39e53082021-04-27 12:22:03 +0800871 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
872 return 0;
873 }
874
875 /* Fall back to two-step timestamping */
876 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
877 }
878
Yangbo Lu682eaad2021-04-27 12:22:02 +0800879 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
880 *clone = skb_clone_sk(skb);
881 if (!(*clone))
882 return -ENOMEM;
883
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300884 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
885 if (err)
886 return err;
887
Yangbo Lu39e53082021-04-27 12:22:03 +0800888 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300889 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
Yangbo Lu682eaad2021-04-27 12:22:02 +0800890 }
891
892 return 0;
893}
894EXPORT_SYMBOL(ocelot_port_txtstamp_request);
Yangbo Lu400928b2019-11-20 16:23:16 +0800895
Yangbo Lue23a7b32019-11-20 16:23:15 +0800896static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
897 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200898{
899 unsigned long flags;
900 u32 val;
901
902 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
903
904 /* Read current PTP time to get seconds */
905 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
906
907 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
908 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
909 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
910 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
911
912 /* Read packet HW timestamp from FIFO */
913 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
914 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
915
916 /* Sec has incremented since the ts was registered */
917 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
918 ts->tv_sec--;
919
920 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
921}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800922
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300923static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
924{
925 struct ptp_header *hdr;
926
927 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
928 if (WARN_ON(!hdr))
929 return false;
930
931 return seqid == ntohs(hdr->sequence_id);
932}
933
Yangbo Lue23a7b32019-11-20 16:23:15 +0800934void ocelot_get_txtstamp(struct ocelot *ocelot)
935{
936 int budget = OCELOT_PTP_QUEUE_SZ;
937
938 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800939 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800940 struct skb_shared_hwtstamps shhwtstamps;
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300941 u32 val, id, seqid, txport;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800942 struct ocelot_port *port;
943 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800944 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800945
946 val = ocelot_read(ocelot, SYS_PTP_STATUS);
947
948 /* Check if a timestamp can be retrieved */
949 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
950 break;
951
952 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
953
954 /* Retrieve the ts ID and Tx port */
955 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
956 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300957 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800958
Yangbo Lue23a7b32019-11-20 16:23:15 +0800959 port = ocelot->ports[txport];
960
Vladimir Oltean52849bc2021-10-12 14:40:36 +0300961 spin_lock(&ocelot->ts_id_lock);
962 port->ptp_skbs_in_flight--;
963 ocelot->ptp_skbs_in_flight--;
964 spin_unlock(&ocelot->ts_id_lock);
965
966 /* Retrieve its associated skb */
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300967try_again:
Yangbo Lub049da12019-11-27 15:27:57 +0800968 spin_lock_irqsave(&port->tx_skbs.lock, flags);
969
970 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
Yangbo Luc4b364c2021-04-27 12:22:00 +0800971 if (OCELOT_SKB_CB(skb)->ts_id != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800972 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800973 __skb_unlink(skb, &port->tx_skbs);
974 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800975 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800976 }
977
Yangbo Lub049da12019-11-27 15:27:57 +0800978 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
979
Vladimir Oltean9fde5062021-10-12 14:40:37 +0300980 if (WARN_ON(!skb_match))
981 continue;
982
Vladimir Olteanebb4c6a2021-10-12 14:40:39 +0300983 if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
984 dev_err_ratelimited(ocelot->dev,
985 "port %d received stale TX timestamp for seqid %d, discarding\n",
986 txport, seqid);
987 dev_kfree_skb_any(skb);
988 goto try_again;
989 }
990
laurent brando5fd82202020-07-27 18:26:14 +0800991 /* Get the h/w timestamp */
992 ocelot_get_hwtimestamp(ocelot, &ts);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800993
Yangbo Lue23a7b32019-11-20 16:23:15 +0800994 /* Set the timestamp into the skb */
995 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
996 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300997 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
laurent brando5fd82202020-07-27 18:26:14 +0800998
999 /* Next ts */
1000 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
Yangbo Lue23a7b32019-11-20 16:23:15 +08001001 }
1002}
1003EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001004
Vladimir Oltean924ee312021-02-14 00:37:59 +02001005static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1006 u32 *rval)
1007{
1008 u32 bytes_valid, val;
1009
1010 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1011 if (val == XTR_NOT_READY) {
1012 if (ifh)
1013 return -EIO;
1014
1015 do {
1016 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1017 } while (val == XTR_NOT_READY);
1018 }
1019
1020 switch (val) {
1021 case XTR_ABORT:
1022 return -EIO;
1023 case XTR_EOF_0:
1024 case XTR_EOF_1:
1025 case XTR_EOF_2:
1026 case XTR_EOF_3:
1027 case XTR_PRUNED:
1028 bytes_valid = XTR_VALID_BYTES(val);
1029 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1030 if (val == XTR_ESCAPE)
1031 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1032 else
1033 *rval = val;
1034
1035 return bytes_valid;
1036 case XTR_ESCAPE:
1037 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1038
1039 return 4;
1040 default:
1041 *rval = val;
1042
1043 return 4;
1044 }
1045}
1046
1047static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1048{
1049 int i, err = 0;
1050
1051 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1052 err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1053 if (err != 4)
1054 return (err < 0) ? err : -EIO;
1055 }
1056
1057 return 0;
1058}
1059
Clément Légerb471a712021-12-09 16:49:09 +01001060void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1061 u64 timestamp)
Vladimir Oltean924ee312021-02-14 00:37:59 +02001062{
1063 struct skb_shared_hwtstamps *shhwtstamps;
Horatiu Vultur2ed2c5f2021-03-16 21:10:19 +01001064 u64 tod_in_ns, full_ts_in_ns;
Clément Légerb471a712021-12-09 16:49:09 +01001065 struct timespec64 ts;
1066
1067 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1068
1069 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1070 if ((tod_in_ns & 0xffffffff) < timestamp)
1071 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1072 timestamp;
1073 else
1074 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1075 timestamp;
1076
1077 shhwtstamps = skb_hwtstamps(skb);
1078 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1079 shhwtstamps->hwtstamp = full_ts_in_ns;
1080}
1081EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1082
1083int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1084{
Vladimir Oltean924ee312021-02-14 00:37:59 +02001085 u64 timestamp, src_port, len;
1086 u32 xfh[OCELOT_TAG_LEN / 4];
1087 struct net_device *dev;
Vladimir Oltean924ee312021-02-14 00:37:59 +02001088 struct sk_buff *skb;
1089 int sz, buf_len;
1090 u32 val, *buf;
1091 int err;
1092
1093 err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1094 if (err)
1095 return err;
1096
1097 ocelot_xfh_get_src_port(xfh, &src_port);
1098 ocelot_xfh_get_len(xfh, &len);
1099 ocelot_xfh_get_rew_val(xfh, &timestamp);
1100
1101 if (WARN_ON(src_port >= ocelot->num_phys_ports))
1102 return -EINVAL;
1103
1104 dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1105 if (!dev)
1106 return -EINVAL;
1107
1108 skb = netdev_alloc_skb(dev, len);
1109 if (unlikely(!skb)) {
1110 netdev_err(dev, "Unable to allocate sk_buff\n");
1111 return -ENOMEM;
1112 }
1113
1114 buf_len = len - ETH_FCS_LEN;
1115 buf = (u32 *)skb_put(skb, buf_len);
1116
1117 len = 0;
1118 do {
1119 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1120 if (sz < 0) {
1121 err = sz;
1122 goto out_free_skb;
1123 }
1124 *buf++ = val;
1125 len += sz;
1126 } while (len < buf_len);
1127
1128 /* Read the FCS */
1129 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1130 if (sz < 0) {
1131 err = sz;
1132 goto out_free_skb;
1133 }
1134
1135 /* Update the statistics if part of the FCS was read before */
1136 len -= ETH_FCS_LEN - sz;
1137
1138 if (unlikely(dev->features & NETIF_F_RXFCS)) {
1139 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1140 *buf = val;
1141 }
1142
Clément Légerb471a712021-12-09 16:49:09 +01001143 if (ocelot->ptp)
1144 ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
Vladimir Oltean924ee312021-02-14 00:37:59 +02001145
1146 /* Everything we see on an interface that is in the HW bridge
1147 * has already been forwarded.
1148 */
Vladimir Olteandf291e52021-03-19 01:36:36 +02001149 if (ocelot->ports[src_port]->bridge)
Vladimir Oltean924ee312021-02-14 00:37:59 +02001150 skb->offload_fwd_mark = 1;
1151
1152 skb->protocol = eth_type_trans(skb, dev);
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001153
Vladimir Oltean924ee312021-02-14 00:37:59 +02001154 *nskb = skb;
1155
1156 return 0;
1157
1158out_free_skb:
1159 kfree_skb(skb);
1160 return err;
1161}
1162EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1163
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001164bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1165{
1166 u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1167
1168 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1169 return false;
1170 if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1171 return false;
1172
1173 return true;
1174}
1175EXPORT_SYMBOL(ocelot_can_inject);
1176
Clément Légere5150f002021-12-09 16:49:08 +01001177void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1178{
1179 ocelot_ifh_set_bypass(ifh, 1);
1180 ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1181 ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1182 if (vlan_tag)
1183 ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1184 if (rew_op)
1185 ocelot_ifh_set_rew_op(ifh, rew_op);
1186}
1187EXPORT_SYMBOL(ocelot_ifh_port_set);
1188
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001189void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1190 u32 rew_op, struct sk_buff *skb)
1191{
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001192 u32 ifh[OCELOT_TAG_LEN / 4] = {0};
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001193 unsigned int i, count, last;
1194
1195 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1196 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1197
Clément Légere5150f002021-12-09 16:49:08 +01001198 ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001199
1200 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
Vladimir Oltean40d3f292021-02-14 00:37:56 +02001201 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
Vladimir Oltean137ffbc2021-02-14 00:37:54 +02001202
1203 count = DIV_ROUND_UP(skb->len, 4);
1204 last = skb->len % 4;
1205 for (i = 0; i < count; i++)
1206 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1207
1208 /* Add padding */
1209 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1210 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1211 i++;
1212 }
1213
1214 /* Indicate EOF and valid bytes in last word */
1215 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1216 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1217 QS_INJ_CTRL_EOF,
1218 QS_INJ_CTRL, grp);
1219
1220 /* Add dummy CRC */
1221 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1222 skb_tx_timestamp(skb);
1223
1224 skb->dev->stats.tx_packets++;
1225 skb->dev->stats.tx_bytes += skb->len;
1226}
1227EXPORT_SYMBOL(ocelot_port_inject_frame);
1228
Vladimir Oltean0a6f17c2021-02-14 00:38:01 +02001229void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
1230{
1231 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
1232 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1233}
1234EXPORT_SYMBOL(ocelot_drain_cpu_queue);
1235
Vladimir Oltean5e256362019-11-14 17:03:27 +02001236int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +03001237 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001238{
Vladimir Oltean471beb12020-06-21 14:46:00 +03001239 int pgid = port;
1240
1241 if (port == ocelot->npi)
1242 pgid = PGID_CPU;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001243
Vladimir Oltean471beb12020-06-21 14:46:00 +03001244 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001245}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001246EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001247
Vladimir Oltean5e256362019-11-14 17:03:27 +02001248int ocelot_fdb_del(struct ocelot *ocelot, int port,
1249 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001250{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001251 return ocelot_mact_forget(ocelot, addr, vid);
1252}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001253EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001254
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001255int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1256 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001257{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001258 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001259 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1260 u32 seq = dump->cb->nlh->nlmsg_seq;
1261 struct nlmsghdr *nlh;
1262 struct ndmsg *ndm;
1263
1264 if (dump->idx < dump->cb->args[2])
1265 goto skip;
1266
1267 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1268 sizeof(*ndm), NLM_F_MULTI);
1269 if (!nlh)
1270 return -EMSGSIZE;
1271
1272 ndm = nlmsg_data(nlh);
1273 ndm->ndm_family = AF_BRIDGE;
1274 ndm->ndm_pad1 = 0;
1275 ndm->ndm_pad2 = 0;
1276 ndm->ndm_flags = NTF_SELF;
1277 ndm->ndm_type = 0;
1278 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001279 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001280
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001281 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +02001282 goto nla_put_failure;
1283
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001284 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +02001285 goto nla_put_failure;
1286
1287 nlmsg_end(dump->skb, nlh);
1288
1289skip:
1290 dump->idx++;
1291 return 0;
1292
1293nla_put_failure:
1294 nlmsg_cancel(dump->skb, nlh);
1295 return -EMSGSIZE;
1296}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001297EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001298
Vladimir Oltean24683462021-10-24 20:17:51 +03001299/* Caller must hold &ocelot->mact_lock */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001300static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1301 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001302{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001303 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001304 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +02001305
1306 /* Set row and column to read from */
1307 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1308 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1309
1310 /* Issue a read command */
1311 ocelot_write(ocelot,
1312 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1313 ANA_TABLES_MACACCESS);
1314
1315 if (ocelot_mact_wait_for_completion(ocelot))
1316 return -ETIMEDOUT;
1317
1318 /* Read the entry flags */
1319 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1320 if (!(val & ANA_TABLES_MACACCESS_VALID))
1321 return -EINVAL;
1322
1323 /* If the entry read has another port configured as its destination,
1324 * do not report it.
1325 */
1326 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001327 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001328 return -EINVAL;
1329
1330 /* Get the entry's MAC address and VLAN id */
1331 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1332 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1333
1334 mac[0] = (mach >> 8) & 0xff;
1335 mac[1] = (mach >> 0) & 0xff;
1336 mac[2] = (macl >> 24) & 0xff;
1337 mac[3] = (macl >> 16) & 0xff;
1338 mac[4] = (macl >> 8) & 0xff;
1339 mac[5] = (macl >> 0) & 0xff;
1340
1341 entry->vid = (mach >> 16) & 0xfff;
1342 ether_addr_copy(entry->mac, mac);
1343
1344 return 0;
1345}
1346
Vladimir Oltean5cad43a2022-01-07 16:42:29 +02001347int ocelot_mact_flush(struct ocelot *ocelot, int port)
1348{
1349 int err;
1350
1351 mutex_lock(&ocelot->mact_lock);
1352
1353 /* Program ageing filter for a single port */
1354 ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
1355 ANA_ANAGEFIL);
1356
1357 /* Flushing dynamic FDB entries requires two successive age scans */
1358 ocelot_write(ocelot,
1359 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
1360 ANA_TABLES_MACACCESS);
1361
1362 err = ocelot_mact_wait_for_completion(ocelot);
1363 if (err) {
1364 mutex_unlock(&ocelot->mact_lock);
1365 return err;
1366 }
1367
1368 /* And second... */
1369 ocelot_write(ocelot,
1370 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
1371 ANA_TABLES_MACACCESS);
1372
1373 err = ocelot_mact_wait_for_completion(ocelot);
1374
1375 /* Restore ageing filter */
1376 ocelot_write(ocelot, 0, ANA_ANAGEFIL);
1377
1378 mutex_unlock(&ocelot->mact_lock);
1379
1380 return err;
1381}
1382EXPORT_SYMBOL_GPL(ocelot_mact_flush);
1383
Vladimir Oltean5e256362019-11-14 17:03:27 +02001384int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1385 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001386{
Vladimir Oltean24683462021-10-24 20:17:51 +03001387 int err = 0;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001388 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001389
Vladimir Oltean24683462021-10-24 20:17:51 +03001390 /* We could take the lock just around ocelot_mact_read, but doing so
1391 * thousands of times in a row seems rather pointless and inefficient.
1392 */
1393 mutex_lock(&ocelot->mact_lock);
1394
Vladimir Oltean21ce7f32020-05-04 01:20:26 +03001395 /* Loop through all the mac tables entries. */
1396 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001397 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001398 struct ocelot_mact_entry entry;
1399 bool is_static;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001400
Vladimir Oltean24683462021-10-24 20:17:51 +03001401 err = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001402 /* If the entry is invalid (wrong port, invalid...),
1403 * skip it.
1404 */
Vladimir Oltean24683462021-10-24 20:17:51 +03001405 if (err == -EINVAL)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001406 continue;
Vladimir Oltean24683462021-10-24 20:17:51 +03001407 else if (err)
1408 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001409
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001410 is_static = (entry.type == ENTRYTYPE_LOCKED);
1411
Vladimir Oltean24683462021-10-24 20:17:51 +03001412 err = cb(entry.mac, entry.vid, is_static, data);
1413 if (err)
1414 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001415 }
1416 }
1417
Vladimir Oltean24683462021-10-24 20:17:51 +03001418 mutex_unlock(&ocelot->mact_lock);
1419
1420 return err;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001421}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001422EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001423
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001424static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
1425{
1426 trap->key_type = OCELOT_VCAP_KEY_ETYPE;
1427 *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
1428 *(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
1429}
1430
1431static void
1432ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1433{
1434 trap->key_type = OCELOT_VCAP_KEY_IPV4;
Vladimir Oltean59085202022-02-05 01:03:21 +02001435 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
1436 trap->key.ipv4.proto.mask[0] = 0xff;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001437 trap->key.ipv4.dport.value = PTP_EV_PORT;
1438 trap->key.ipv4.dport.mask = 0xffff;
1439}
1440
1441static void
1442ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1443{
1444 trap->key_type = OCELOT_VCAP_KEY_IPV6;
Vladimir Oltean59085202022-02-05 01:03:21 +02001445 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
1446 trap->key.ipv4.proto.mask[0] = 0xff;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001447 trap->key.ipv6.dport.value = PTP_EV_PORT;
1448 trap->key.ipv6.dport.mask = 0xffff;
1449}
1450
1451static void
1452ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1453{
1454 trap->key_type = OCELOT_VCAP_KEY_IPV4;
Vladimir Oltean59085202022-02-05 01:03:21 +02001455 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
1456 trap->key.ipv4.proto.mask[0] = 0xff;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001457 trap->key.ipv4.dport.value = PTP_GEN_PORT;
1458 trap->key.ipv4.dport.mask = 0xffff;
1459}
1460
1461static void
1462ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1463{
1464 trap->key_type = OCELOT_VCAP_KEY_IPV6;
Vladimir Oltean59085202022-02-05 01:03:21 +02001465 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
1466 trap->key.ipv4.proto.mask[0] = 0xff;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001467 trap->key.ipv6.dport.value = PTP_GEN_PORT;
1468 trap->key.ipv6.dport.mask = 0xffff;
1469}
1470
1471static int ocelot_trap_add(struct ocelot *ocelot, int port,
1472 unsigned long cookie,
1473 void (*populate)(struct ocelot_vcap_filter *f))
1474{
1475 struct ocelot_vcap_block *block_vcap_is2;
1476 struct ocelot_vcap_filter *trap;
1477 bool new = false;
1478 int err;
1479
1480 block_vcap_is2 = &ocelot->block[VCAP_IS2];
1481
1482 trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1483 false);
1484 if (!trap) {
1485 trap = kzalloc(sizeof(*trap), GFP_KERNEL);
1486 if (!trap)
1487 return -ENOMEM;
1488
1489 populate(trap);
1490 trap->prio = 1;
1491 trap->id.cookie = cookie;
1492 trap->id.tc_offload = false;
1493 trap->block_id = VCAP_IS2;
1494 trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
1495 trap->lookup = 0;
1496 trap->action.cpu_copy_ena = true;
1497 trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
1498 trap->action.port_mask = 0;
1499 new = true;
1500 }
1501
1502 trap->ingress_port_mask |= BIT(port);
1503
1504 if (new)
1505 err = ocelot_vcap_filter_add(ocelot, trap, NULL);
1506 else
1507 err = ocelot_vcap_filter_replace(ocelot, trap);
1508 if (err) {
1509 trap->ingress_port_mask &= ~BIT(port);
1510 if (!trap->ingress_port_mask)
1511 kfree(trap);
1512 return err;
1513 }
1514
1515 return 0;
1516}
1517
1518static int ocelot_trap_del(struct ocelot *ocelot, int port,
1519 unsigned long cookie)
1520{
1521 struct ocelot_vcap_block *block_vcap_is2;
1522 struct ocelot_vcap_filter *trap;
1523
1524 block_vcap_is2 = &ocelot->block[VCAP_IS2];
1525
1526 trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1527 false);
1528 if (!trap)
1529 return 0;
1530
1531 trap->ingress_port_mask &= ~BIT(port);
1532 if (!trap->ingress_port_mask)
1533 return ocelot_vcap_filter_del(ocelot, trap);
1534
1535 return ocelot_vcap_filter_replace(ocelot, trap);
1536}
1537
1538static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
1539{
1540 unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1541
1542 return ocelot_trap_add(ocelot, port, l2_cookie,
1543 ocelot_populate_l2_ptp_trap_key);
1544}
1545
1546static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
1547{
1548 unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1549
1550 return ocelot_trap_del(ocelot, port, l2_cookie);
1551}
1552
1553static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
1554{
1555 unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1556 unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1557 int err;
1558
1559 err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie,
1560 ocelot_populate_ipv4_ptp_event_trap_key);
1561 if (err)
1562 return err;
1563
1564 err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie,
1565 ocelot_populate_ipv4_ptp_general_trap_key);
1566 if (err)
1567 ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1568
1569 return err;
1570}
1571
1572static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
1573{
1574 unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1575 unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1576 int err;
1577
1578 err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1579 err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
1580 return err;
1581}
1582
1583static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
1584{
1585 unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1586 unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1587 int err;
1588
1589 err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie,
1590 ocelot_populate_ipv6_ptp_event_trap_key);
1591 if (err)
1592 return err;
1593
1594 err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie,
1595 ocelot_populate_ipv6_ptp_general_trap_key);
1596 if (err)
1597 ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1598
1599 return err;
1600}
1601
1602static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
1603{
1604 unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1605 unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1606 int err;
1607
1608 err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1609 err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
1610 return err;
1611}
1612
1613static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
1614 bool l2, bool l4)
1615{
1616 int err;
1617
1618 if (l2)
1619 err = ocelot_l2_ptp_trap_add(ocelot, port);
1620 else
1621 err = ocelot_l2_ptp_trap_del(ocelot, port);
1622 if (err)
1623 return err;
1624
1625 if (l4) {
1626 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
1627 if (err)
1628 goto err_ipv4;
1629
1630 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
1631 if (err)
1632 goto err_ipv6;
1633 } else {
1634 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
1635
1636 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
1637 }
1638 if (err)
1639 return err;
1640
1641 return 0;
1642
1643err_ipv6:
1644 ocelot_ipv4_ptp_trap_del(ocelot, port);
1645err_ipv4:
1646 if (l2)
1647 ocelot_l2_ptp_trap_del(ocelot, port);
1648 return err;
1649}
1650
Yangbo Luf1459222019-11-20 16:23:14 +08001651int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001652{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001653 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1654 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1655}
Yangbo Luf1459222019-11-20 16:23:14 +08001656EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001657
Yangbo Luf1459222019-11-20 16:23:14 +08001658int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001659{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001660 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001661 bool l2 = false, l4 = false;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001662 struct hwtstamp_config cfg;
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001663 int err;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001664
1665 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1666 return -EFAULT;
1667
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001668 /* Tx type sanity check */
1669 switch (cfg.tx_type) {
1670 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001671 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001672 break;
1673 case HWTSTAMP_TX_ONESTEP_SYNC:
1674 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1675 * need to update the origin time.
1676 */
Vladimir Oltean306fd442019-11-09 15:02:50 +02001677 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001678 break;
1679 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001680 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001681 break;
1682 default:
1683 return -ERANGE;
1684 }
1685
1686 mutex_lock(&ocelot->ptp_lock);
1687
1688 switch (cfg.rx_filter) {
1689 case HWTSTAMP_FILTER_NONE:
1690 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001691 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1692 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1693 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001694 l4 = true;
1695 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001696 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1697 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1698 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001699 l2 = true;
1700 break;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001701 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1702 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1703 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001704 l2 = true;
1705 l4 = true;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001706 break;
1707 default:
1708 mutex_unlock(&ocelot->ptp_lock);
1709 return -ERANGE;
1710 }
1711
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001712 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
Lv Ruyi9c329502021-11-30 11:24:43 +00001713 if (err) {
1714 mutex_unlock(&ocelot->ptp_lock);
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001715 return err;
Lv Ruyi9c329502021-11-30 11:24:43 +00001716 }
Vladimir Oltean96ca08c2021-11-26 19:28:44 +02001717
1718 if (l2 && l4)
1719 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1720 else if (l2)
1721 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1722 else if (l4)
1723 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
1724 else
1725 cfg.rx_filter = HWTSTAMP_FILTER_NONE;
1726
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001727 /* Commit back the result & save it */
1728 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1729 mutex_unlock(&ocelot->ptp_lock);
1730
1731 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1732}
Yangbo Luf1459222019-11-20 16:23:14 +08001733EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001734
Vladimir Oltean5e256362019-11-14 17:03:27 +02001735void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001736{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001737 int i;
1738
1739 if (sset != ETH_SS_STATS)
1740 return;
1741
1742 for (i = 0; i < ocelot->num_stats; i++)
1743 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1744 ETH_GSTRING_LEN);
1745}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001746EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001747
Colin Foster7fbf6792022-02-10 07:04:51 -08001748/* Caller must hold &ocelot->stats_lock */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001749static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001750{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001751 int i, j;
1752
Alexandre Bellonia556c762018-05-14 22:04:57 +02001753 for (i = 0; i < ocelot->num_phys_ports; i++) {
1754 /* Configure the port to read the stats from */
1755 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1756
1757 for (j = 0; j < ocelot->num_stats; j++) {
1758 u32 val;
1759 unsigned int idx = i * ocelot->num_stats + j;
1760
1761 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1762 ocelot->stats_layout[j].offset);
1763
1764 if (val < (ocelot->stats[idx] & U32_MAX))
1765 ocelot->stats[idx] += (u64)1 << 32;
1766
1767 ocelot->stats[idx] = (ocelot->stats[idx] &
1768 ~(u64)U32_MAX) + val;
1769 }
1770 }
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001771}
1772
1773static void ocelot_check_stats_work(struct work_struct *work)
1774{
1775 struct delayed_work *del_work = to_delayed_work(work);
1776 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1777 stats_work);
1778
Colin Foster7fbf6792022-02-10 07:04:51 -08001779 mutex_lock(&ocelot->stats_lock);
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001780 ocelot_update_stats(ocelot);
Colin Foster7fbf6792022-02-10 07:04:51 -08001781 mutex_unlock(&ocelot->stats_lock);
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001782
Alexandre Bellonia556c762018-05-14 22:04:57 +02001783 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1784 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001785}
1786
Vladimir Oltean5e256362019-11-14 17:03:27 +02001787void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001788{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001789 int i;
1790
Colin Foster7fbf6792022-02-10 07:04:51 -08001791 mutex_lock(&ocelot->stats_lock);
1792
Alexandre Bellonia556c762018-05-14 22:04:57 +02001793 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001794 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001795
1796 /* Copy all counters */
1797 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001798 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Colin Foster7fbf6792022-02-10 07:04:51 -08001799
1800 mutex_unlock(&ocelot->stats_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001801}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001802EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001803
Vladimir Oltean5e256362019-11-14 17:03:27 +02001804int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001805{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001806 if (sset != ETH_SS_STATS)
1807 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001808
Alexandre Bellonia556c762018-05-14 22:04:57 +02001809 return ocelot->num_stats;
1810}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001811EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001812
Vladimir Oltean5e256362019-11-14 17:03:27 +02001813int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1814 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001815{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001816 info->phc_index = ocelot->ptp_clock ?
1817 ptp_clock_index(ocelot->ptp_clock) : -1;
Yangbo Lud2b09a82020-04-20 10:46:46 +08001818 if (info->phc_index == -1) {
1819 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1820 SOF_TIMESTAMPING_RX_SOFTWARE |
1821 SOF_TIMESTAMPING_SOFTWARE;
1822 return 0;
1823 }
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001824 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1825 SOF_TIMESTAMPING_RX_SOFTWARE |
1826 SOF_TIMESTAMPING_SOFTWARE |
1827 SOF_TIMESTAMPING_TX_HARDWARE |
1828 SOF_TIMESTAMPING_RX_HARDWARE |
1829 SOF_TIMESTAMPING_RAW_HARDWARE;
1830 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1831 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
Vladimir Olteanc49a35e2021-11-26 19:28:45 +02001832 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1833 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1834 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1835 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001836
1837 return 0;
1838}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001839EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001840
Vladimir Olteana14e6b62022-01-07 18:43:32 +02001841static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
Vladimir Olteanb80af652021-02-06 00:02:14 +02001842{
1843 u32 mask = 0;
1844 int port;
1845
1846 for (port = 0; port < ocelot->num_phys_ports; port++) {
1847 struct ocelot_port *ocelot_port = ocelot->ports[port];
1848
1849 if (!ocelot_port)
1850 continue;
1851
Vladimir Olteana14e6b62022-01-07 18:43:32 +02001852 if (ocelot_port->bond == bond)
Vladimir Olteanb80af652021-02-06 00:02:14 +02001853 mask |= BIT(port);
1854 }
1855
1856 return mask;
1857}
1858
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001859u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
Vladimir Olteandf291e52021-03-19 01:36:36 +02001860{
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001861 struct ocelot_port *ocelot_port = ocelot->ports[src_port];
Vladimir Olteana8bd9fa2021-11-25 14:58:07 +02001862 const struct net_device *bridge;
Vladimir Olteandf291e52021-03-19 01:36:36 +02001863 u32 mask = 0;
1864 int port;
1865
Vladimir Olteana8bd9fa2021-11-25 14:58:07 +02001866 if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1867 return 0;
1868
1869 bridge = ocelot_port->bridge;
1870 if (!bridge)
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001871 return 0;
1872
Vladimir Olteandf291e52021-03-19 01:36:36 +02001873 for (port = 0; port < ocelot->num_phys_ports; port++) {
Vladimir Olteanacc64f52021-09-22 19:03:38 -07001874 ocelot_port = ocelot->ports[port];
Vladimir Olteandf291e52021-03-19 01:36:36 +02001875
1876 if (!ocelot_port)
1877 continue;
1878
1879 if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1880 ocelot_port->bridge == bridge)
1881 mask |= BIT(port);
1882 }
1883
1884 return mask;
1885}
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001886EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
Vladimir Olteandf291e52021-03-19 01:36:36 +02001887
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001888u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
Vladimir Oltean9b521252021-01-29 03:00:02 +02001889{
Vladimir Olteane21268e2021-01-29 03:00:09 +02001890 u32 mask = 0;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001891 int port;
1892
Vladimir Olteane21268e2021-01-29 03:00:09 +02001893 for (port = 0; port < ocelot->num_phys_ports; port++) {
1894 struct ocelot_port *ocelot_port = ocelot->ports[port];
1895
1896 if (!ocelot_port)
1897 continue;
1898
1899 if (ocelot_port->is_dsa_8021q_cpu)
1900 mask |= BIT(port);
1901 }
1902
1903 return mask;
1904}
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001905EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask);
Vladimir Olteane21268e2021-01-29 03:00:09 +02001906
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001907void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
Vladimir Olteane21268e2021-01-29 03:00:09 +02001908{
1909 unsigned long cpu_fwd_mask;
1910 int port;
1911
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001912 lockdep_assert_held(&ocelot->fwd_domain_lock);
1913
1914 /* If cut-through forwarding is supported, update the masks before a
1915 * port joins the forwarding domain, to avoid potential underruns if it
1916 * has the highest speed from the new domain.
1917 */
1918 if (joining && ocelot->ops->cut_through_fwd)
1919 ocelot->ops->cut_through_fwd(ocelot);
1920
Vladimir Olteane21268e2021-01-29 03:00:09 +02001921 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1922 * regular forwarding path of the front ports regardless of whether
1923 * those are bridged or standalone.
1924 * If DSA tag_8021q is not used, this returns 0, which is fine because
1925 * the hardware-based CPU port module can be a destination for packets
1926 * even if it isn't part of PGID_SRC.
1927 */
1928 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1929
Vladimir Oltean9b521252021-01-29 03:00:02 +02001930 /* Apply FWD mask. The loop is needed to add/remove the current port as
1931 * a source for the other ports.
1932 */
1933 for (port = 0; port < ocelot->num_phys_ports; port++) {
Vladimir Olteane21268e2021-01-29 03:00:09 +02001934 struct ocelot_port *ocelot_port = ocelot->ports[port];
1935 unsigned long mask;
1936
1937 if (!ocelot_port) {
1938 /* Unused ports can't send anywhere */
1939 mask = 0;
1940 } else if (ocelot_port->is_dsa_8021q_cpu) {
1941 /* The DSA tag_8021q CPU ports need to be able to
1942 * forward packets to all other ports except for
1943 * themselves
1944 */
1945 mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1946 mask &= ~cpu_fwd_mask;
Vladimir Olteandf291e52021-03-19 01:36:36 +02001947 } else if (ocelot_port->bridge) {
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001948 struct net_device *bond = ocelot_port->bond;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001949
Vladimir Olteana8bd9fa2021-11-25 14:58:07 +02001950 mask = ocelot_get_bridge_fwd_mask(ocelot, port);
Vladimir Olteanc1930142021-08-17 19:04:25 +03001951 mask |= cpu_fwd_mask;
Vladimir Olteandf291e52021-03-19 01:36:36 +02001952 mask &= ~BIT(port);
Vladimir Olteana14e6b62022-01-07 18:43:32 +02001953 if (bond)
1954 mask &= ~ocelot_get_bond_mask(ocelot, bond);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001955 } else {
Vladimir Olteane21268e2021-01-29 03:00:09 +02001956 /* Standalone ports forward only to DSA tag_8021q CPU
1957 * ports (if those exist), or to the hardware CPU port
1958 * module otherwise.
1959 */
1960 mask = cpu_fwd_mask;
Vladimir Oltean9b521252021-01-29 03:00:02 +02001961 }
Vladimir Olteane21268e2021-01-29 03:00:09 +02001962
1963 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001964 }
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001965
1966 /* If cut-through forwarding is supported and a port is leaving, there
1967 * is a chance that cut-through was disabled on the other ports due to
1968 * the port which is leaving (it has a higher link speed). We need to
1969 * update the cut-through masks of the remaining ports no earlier than
1970 * after the port has left, to prevent underruns from happening between
1971 * the cut-through update and the forwarding domain update.
1972 */
1973 if (!joining && ocelot->ops->cut_through_fwd)
1974 ocelot->ops->cut_through_fwd(ocelot);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001975}
Vladimir Olteane21268e2021-01-29 03:00:09 +02001976EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001977
Vladimir Oltean5e256362019-11-14 17:03:27 +02001978void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001979{
Vladimir Oltean421741e2021-02-12 17:15:59 +02001980 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteandf291e52021-03-19 01:36:36 +02001981 u32 learn_ena = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001982
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001983 mutex_lock(&ocelot->fwd_domain_lock);
1984
Vladimir Olteandf291e52021-03-19 01:36:36 +02001985 ocelot_port->stp_state = state;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001986
Vladimir Olteandf291e52021-03-19 01:36:36 +02001987 if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1988 ocelot_port->learn_ena)
1989 learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001990
Vladimir Olteandf291e52021-03-19 01:36:36 +02001991 ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1992 ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001993
Vladimir Oltean8abe1972021-11-25 14:58:08 +02001994 ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
1995
1996 mutex_unlock(&ocelot->fwd_domain_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001997}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001998EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001999
Vladimir Oltean5e256362019-11-14 17:03:27 +02002000void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02002001{
Vladimir Olteanc0d7ecc2020-05-04 01:20:27 +03002002 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
2003
2004 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
2005 * which is clearly not what our intention is. So avoid that.
2006 */
2007 if (!age_period)
2008 age_period = 1;
2009
2010 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002011}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002012EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002013
Alexandre Bellonia556c762018-05-14 22:04:57 +02002014static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
2015 const unsigned char *addr,
2016 u16 vid)
2017{
2018 struct ocelot_multicast *mc;
2019
2020 list_for_each_entry(mc, &ocelot->multicast, list) {
2021 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
2022 return mc;
2023 }
2024
2025 return NULL;
2026}
2027
Vladimir Oltean9403c152020-06-21 14:46:03 +03002028static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
2029{
2030 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
2031 return ENTRYTYPE_MACv4;
2032 if (addr[0] == 0x33 && addr[1] == 0x33)
2033 return ENTRYTYPE_MACv6;
Vladimir Oltean7c313142020-10-29 04:27:34 +02002034 return ENTRYTYPE_LOCKED;
Vladimir Oltean9403c152020-06-21 14:46:03 +03002035}
2036
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002037static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
2038 unsigned long ports)
Vladimir Oltean9403c152020-06-21 14:46:03 +03002039{
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002040 struct ocelot_pgid *pgid;
2041
2042 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
2043 if (!pgid)
2044 return ERR_PTR(-ENOMEM);
2045
2046 pgid->ports = ports;
2047 pgid->index = index;
2048 refcount_set(&pgid->refcount, 1);
2049 list_add_tail(&pgid->list, &ocelot->pgids);
2050
2051 return pgid;
2052}
2053
2054static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
2055{
2056 if (!refcount_dec_and_test(&pgid->refcount))
2057 return;
2058
2059 list_del(&pgid->list);
2060 kfree(pgid);
2061}
2062
2063static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
2064 const struct ocelot_multicast *mc)
2065{
2066 struct ocelot_pgid *pgid;
2067 int index;
Vladimir Oltean9403c152020-06-21 14:46:03 +03002068
2069 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
2070 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
2071 * destination mask table (PGID), the destination set is programmed as
2072 * part of the entry MAC address.", and the DEST_IDX is set to 0.
2073 */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002074 if (mc->entry_type == ENTRYTYPE_MACv4 ||
2075 mc->entry_type == ENTRYTYPE_MACv6)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002076 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03002077
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002078 list_for_each_entry(pgid, &ocelot->pgids, list) {
2079 /* When searching for a nonreserved multicast PGID, ignore the
2080 * dummy PGID of zero that we have for MACv4/MACv6 entries
2081 */
2082 if (pgid->index && pgid->ports == mc->ports) {
2083 refcount_inc(&pgid->refcount);
2084 return pgid;
2085 }
2086 }
2087
2088 /* Search for a free index in the nonreserved multicast PGID area */
2089 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03002090 bool used = false;
2091
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002092 list_for_each_entry(pgid, &ocelot->pgids, list) {
2093 if (pgid->index == index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03002094 used = true;
2095 break;
2096 }
2097 }
2098
2099 if (!used)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002100 return ocelot_pgid_alloc(ocelot, index, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03002101 }
2102
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002103 return ERR_PTR(-ENOSPC);
Vladimir Oltean9403c152020-06-21 14:46:03 +03002104}
2105
2106static void ocelot_encode_ports_to_mdb(unsigned char *addr,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002107 struct ocelot_multicast *mc)
Vladimir Oltean9403c152020-06-21 14:46:03 +03002108{
Vladimir Olteanebbd8602020-10-29 04:27:35 +02002109 ether_addr_copy(addr, mc->addr);
Vladimir Oltean9403c152020-06-21 14:46:03 +03002110
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002111 if (mc->entry_type == ENTRYTYPE_MACv4) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03002112 addr[0] = 0;
2113 addr[1] = mc->ports >> 8;
2114 addr[2] = mc->ports & 0xff;
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002115 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03002116 addr[0] = mc->ports >> 8;
2117 addr[1] = mc->ports & 0xff;
2118 }
2119}
2120
Vladimir Oltean209edf92020-06-21 14:46:01 +03002121int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
2122 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002123{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002124 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002125 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002126 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002127 u16 vid = mdb->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002128
Vladimir Oltean471beb12020-06-21 14:46:00 +03002129 if (port == ocelot->npi)
2130 port = ocelot->num_phys_ports;
2131
Alexandre Bellonia556c762018-05-14 22:04:57 +02002132 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2133 if (!mc) {
Vladimir Oltean728e69a2020-10-29 04:27:36 +02002134 /* New entry */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002135 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
2136 if (!mc)
2137 return -ENOMEM;
2138
2139 mc->entry_type = ocelot_classify_mdb(mdb->addr);
2140 ether_addr_copy(mc->addr, mdb->addr);
2141 mc->vid = vid;
2142
Alexandre Bellonia556c762018-05-14 22:04:57 +02002143 list_add_tail(&mc->list, &ocelot->multicast);
Vladimir Oltean728e69a2020-10-29 04:27:36 +02002144 } else {
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002145 /* Existing entry. Clean up the current port mask from
2146 * hardware now, because we'll be modifying it.
2147 */
2148 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002149 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002150 ocelot_mact_forget(ocelot, addr, vid);
2151 }
2152
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002153 mc->ports |= BIT(port);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002154
2155 pgid = ocelot_mdb_get_pgid(ocelot, mc);
2156 if (IS_ERR(pgid)) {
2157 dev_err(ocelot->dev,
2158 "Cannot allocate PGID for mdb %pM vid %d\n",
2159 mc->addr, mc->vid);
2160 devm_kfree(ocelot->dev, mc);
2161 return PTR_ERR(pgid);
2162 }
2163 mc->pgid = pgid;
2164
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002165 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002166
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002167 if (mc->entry_type != ENTRYTYPE_MACv4 &&
2168 mc->entry_type != ENTRYTYPE_MACv6)
2169 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2170 pgid->index);
2171
2172 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002173 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002174}
Vladimir Oltean209edf92020-06-21 14:46:01 +03002175EXPORT_SYMBOL(ocelot_port_mdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002176
Vladimir Oltean209edf92020-06-21 14:46:01 +03002177int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
2178 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002179{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002180 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002181 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002182 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002183 u16 vid = mdb->vid;
2184
Vladimir Oltean471beb12020-06-21 14:46:00 +03002185 if (port == ocelot->npi)
2186 port = ocelot->num_phys_ports;
2187
Alexandre Bellonia556c762018-05-14 22:04:57 +02002188 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2189 if (!mc)
2190 return -ENOENT;
2191
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002192 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002193 ocelot_mact_forget(ocelot, addr, vid);
2194
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002195 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002196 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002197 if (!mc->ports) {
2198 list_del(&mc->list);
2199 devm_kfree(ocelot->dev, mc);
2200 return 0;
2201 }
2202
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002203 /* We have a PGID with fewer ports now */
2204 pgid = ocelot_mdb_get_pgid(ocelot, mc);
2205 if (IS_ERR(pgid))
2206 return PTR_ERR(pgid);
2207 mc->pgid = pgid;
2208
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002209 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002210
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002211 if (mc->entry_type != ENTRYTYPE_MACv4 &&
2212 mc->entry_type != ENTRYTYPE_MACv6)
2213 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2214 pgid->index);
2215
2216 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02002217 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002218}
Vladimir Oltean209edf92020-06-21 14:46:01 +03002219EXPORT_SYMBOL(ocelot_port_mdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002220
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002221void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
2222 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002223{
Vladimir Olteandf291e52021-03-19 01:36:36 +02002224 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +02002225
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002226 mutex_lock(&ocelot->fwd_domain_lock);
2227
Vladimir Olteandf291e52021-03-19 01:36:36 +02002228 ocelot_port->bridge = bridge;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002229
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002230 ocelot_apply_bridge_fwd_mask(ocelot, true);
2231
2232 mutex_unlock(&ocelot->fwd_domain_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002233}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002234EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002235
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02002236void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2237 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02002238{
Vladimir Olteandf291e52021-03-19 01:36:36 +02002239 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean2e554a72020-10-03 01:06:46 +03002240
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002241 mutex_lock(&ocelot->fwd_domain_lock);
2242
Vladimir Olteandf291e52021-03-19 01:36:36 +02002243 ocelot_port->bridge = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02002244
Vladimir Olteand4004422021-10-20 20:58:52 +03002245 ocelot_port_set_pvid(ocelot, port, NULL);
Vladimir Oltean0da1a1c2021-10-20 20:58:50 +03002246 ocelot_port_manage_port_tag(ocelot, port);
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002247 ocelot_apply_bridge_fwd_mask(ocelot, false);
2248
2249 mutex_unlock(&ocelot->fwd_domain_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002250}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002251EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002252
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002253static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2254{
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002255 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002256 int i, port, lag;
2257
2258 /* Reset destination and aggregation PGIDS */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002259 for_each_unicast_dest_pgid(ocelot, port)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002260 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2261
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002262 for_each_aggr_pgid(ocelot, i)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002263 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2264 ANA_PGID_PGID, i);
2265
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002266 /* The visited ports bitmask holds the list of ports offloading any
2267 * bonding interface. Initially we mark all these ports as unvisited,
2268 * then every time we visit a port in this bitmask, we know that it is
2269 * the lowest numbered port, i.e. the one whose logical ID == physical
2270 * port ID == LAG ID. So we mark as visited all further ports in the
2271 * bitmask that are offloading the same bonding interface. This way,
2272 * we set up the aggregation PGIDs only once per bonding interface.
2273 */
2274 for (port = 0; port < ocelot->num_phys_ports; port++) {
2275 struct ocelot_port *ocelot_port = ocelot->ports[port];
2276
2277 if (!ocelot_port || !ocelot_port->bond)
2278 continue;
2279
2280 visited &= ~BIT(port);
2281 }
2282
2283 /* Now, set PGIDs for each active LAG */
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002284 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002285 struct net_device *bond = ocelot->ports[lag]->bond;
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002286 int num_active_ports = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002287 unsigned long bond_mask;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002288 u8 aggr_idx[16];
2289
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002290 if (!bond || (visited & BIT(lag)))
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002291 continue;
2292
Vladimir Olteana14e6b62022-01-07 18:43:32 +02002293 bond_mask = ocelot_get_bond_mask(ocelot, bond);
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002294
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002295 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
Vladimir Olteana14e6b62022-01-07 18:43:32 +02002296 struct ocelot_port *ocelot_port = ocelot->ports[port];
2297
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002298 // Destination mask
2299 ocelot_write_rix(ocelot, bond_mask,
2300 ANA_PGID_PGID, port);
Vladimir Olteana14e6b62022-01-07 18:43:32 +02002301
2302 if (ocelot_port->lag_tx_active)
2303 aggr_idx[num_active_ports++] = port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002304 }
2305
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002306 for_each_aggr_pgid(ocelot, i) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002307 u32 ac;
2308
2309 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2310 ac &= ~bond_mask;
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002311 /* Don't do division by zero if there was no active
2312 * port. Just make all aggregation codes zero.
2313 */
2314 if (num_active_ports)
2315 ac |= BIT(aggr_idx[i % num_active_ports]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002316 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2317 }
Vladimir Oltean528d3f12021-02-06 00:02:17 +02002318
2319 /* Mark all ports in the same LAG as visited to avoid applying
2320 * the same config again.
2321 */
2322 for (port = lag; port < ocelot->num_phys_ports; port++) {
2323 struct ocelot_port *ocelot_port = ocelot->ports[port];
2324
2325 if (!ocelot_port)
2326 continue;
2327
2328 if (ocelot_port->bond == bond)
2329 visited |= BIT(port);
2330 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002331 }
2332}
2333
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002334/* When offloading a bonding interface, the switch ports configured under the
2335 * same bond must have the same logical port ID, equal to the physical port ID
2336 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
2337 * bridged mode, each port has a logical port ID equal to its physical port ID.
2338 */
2339static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002340{
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002341 int port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002342
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002343 for (port = 0; port < ocelot->num_phys_ports; port++) {
2344 struct ocelot_port *ocelot_port = ocelot->ports[port];
2345 struct net_device *bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002346
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002347 if (!ocelot_port)
2348 continue;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002349
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002350 bond = ocelot_port->bond;
2351 if (bond) {
Vladimir Olteana14e6b62022-01-07 18:43:32 +02002352 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond));
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002353
2354 ocelot_rmw_gix(ocelot,
2355 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
2356 ANA_PORT_PORT_CFG_PORTID_VAL_M,
2357 ANA_PORT_PORT_CFG, port);
2358 } else {
2359 ocelot_rmw_gix(ocelot,
2360 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2361 ANA_PORT_PORT_CFG_PORTID_VAL_M,
2362 ANA_PORT_PORT_CFG, port);
2363 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002364 }
2365}
2366
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002367int ocelot_port_lag_join(struct ocelot *ocelot, int port,
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02002368 struct net_device *bond,
2369 struct netdev_lag_upper_info *info)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002370{
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02002371 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2372 return -EOPNOTSUPP;
2373
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002374 mutex_lock(&ocelot->fwd_domain_lock);
2375
Vladimir Olteanb80af652021-02-06 00:02:14 +02002376 ocelot->ports[port]->bond = bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002377
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002378 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002379 ocelot_apply_bridge_fwd_mask(ocelot, true);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002380 ocelot_set_aggr_pgids(ocelot);
2381
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002382 mutex_unlock(&ocelot->fwd_domain_lock);
2383
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002384 return 0;
2385}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002386EXPORT_SYMBOL(ocelot_port_lag_join);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002387
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002388void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2389 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002390{
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002391 mutex_lock(&ocelot->fwd_domain_lock);
2392
Vladimir Olteanb80af652021-02-06 00:02:14 +02002393 ocelot->ports[port]->bond = NULL;
2394
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02002395 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002396 ocelot_apply_bridge_fwd_mask(ocelot, false);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002397 ocelot_set_aggr_pgids(ocelot);
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002398
2399 mutex_unlock(&ocelot->fwd_domain_lock);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002400}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03002401EXPORT_SYMBOL(ocelot_port_lag_leave);
Petr Machata0e332c82018-11-22 23:30:11 +00002402
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02002403void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
2404{
2405 struct ocelot_port *ocelot_port = ocelot->ports[port];
2406
2407 ocelot_port->lag_tx_active = lag_tx_active;
2408
2409 /* Rebalance the LAGs */
2410 ocelot_set_aggr_pgids(ocelot);
2411}
2412EXPORT_SYMBOL(ocelot_port_lag_change);
2413
Vladimir Olteana8015de2020-03-10 03:28:18 +02002414/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2415 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002416 * In the special case that it's the NPI port that we're configuring, the
2417 * length of the tag and optional prefix needs to be accounted for privately,
2418 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02002419 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002420void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02002421{
2422 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02002423 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002424 int pause_start, pause_stop;
Vladimir Oltean601e9842020-10-05 12:09:11 +03002425 int atop, atop_tot;
Vladimir Oltean31350d72019-11-09 15:02:56 +02002426
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002427 if (port == ocelot->npi) {
2428 maxlen += OCELOT_TAG_LEN;
2429
Vladimir Olteancacea622021-01-29 03:00:03 +02002430 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002431 maxlen += OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02002432 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002433 maxlen += OCELOT_LONG_PREFIX_LEN;
2434 }
2435
Vladimir Olteana8015de2020-03-10 03:28:18 +02002436 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002437
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002438 /* Set Pause watermark hysteresis */
2439 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2440 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
Maxim Kochetkov541132f2020-07-13 19:57:07 +03002441 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2442 pause_start);
2443 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2444 pause_stop);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002445
Vladimir Oltean601e9842020-10-05 12:09:11 +03002446 /* Tail dropping watermarks */
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002447 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
Vladimir Olteana8015de2020-03-10 03:28:18 +02002448 OCELOT_BUFFER_CELL_SZ;
Vladimir Oltean601e9842020-10-05 12:09:11 +03002449 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2450 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2451 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002452}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002453EXPORT_SYMBOL(ocelot_port_set_maxlen);
2454
2455int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
2456{
2457 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
2458
2459 if (port == ocelot->npi) {
2460 max_mtu -= OCELOT_TAG_LEN;
2461
Vladimir Olteancacea622021-01-29 03:00:03 +02002462 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002463 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02002464 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002465 max_mtu -= OCELOT_LONG_PREFIX_LEN;
2466 }
2467
2468 return max_mtu;
2469}
2470EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002471
Vladimir Oltean421741e2021-02-12 17:15:59 +02002472static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2473 bool enabled)
2474{
2475 struct ocelot_port *ocelot_port = ocelot->ports[port];
2476 u32 val = 0;
2477
2478 if (enabled)
2479 val = ANA_PORT_PORT_CFG_LEARN_ENA;
2480
2481 ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2482 ANA_PORT_PORT_CFG, port);
2483
2484 ocelot_port->learn_ena = enabled;
2485}
2486
2487static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2488 bool enabled)
2489{
2490 u32 val = 0;
2491
2492 if (enabled)
2493 val = BIT(port);
2494
2495 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2496}
2497
2498static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2499 bool enabled)
2500{
2501 u32 val = 0;
2502
2503 if (enabled)
2504 val = BIT(port);
2505
2506 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2507}
2508
2509static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2510 bool enabled)
2511{
2512 u32 val = 0;
2513
2514 if (enabled)
2515 val = BIT(port);
2516
2517 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2518}
2519
2520int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2521 struct switchdev_brport_flags flags)
2522{
2523 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2524 BR_BCAST_FLOOD))
2525 return -EINVAL;
2526
2527 return 0;
2528}
2529EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2530
2531void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2532 struct switchdev_brport_flags flags)
2533{
2534 if (flags.mask & BR_LEARNING)
2535 ocelot_port_set_learning(ocelot, port,
2536 !!(flags.val & BR_LEARNING));
2537
2538 if (flags.mask & BR_FLOOD)
2539 ocelot_port_set_ucast_flood(ocelot, port,
2540 !!(flags.val & BR_FLOOD));
2541
2542 if (flags.mask & BR_MCAST_FLOOD)
2543 ocelot_port_set_mcast_flood(ocelot, port,
2544 !!(flags.val & BR_MCAST_FLOOD));
2545
2546 if (flags.mask & BR_BCAST_FLOOD)
2547 ocelot_port_set_bcast_flood(ocelot, port,
2548 !!(flags.val & BR_BCAST_FLOOD));
2549}
2550EXPORT_SYMBOL(ocelot_port_bridge_flags);
2551
Vladimir Oltean5e256362019-11-14 17:03:27 +02002552void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002553{
2554 struct ocelot_port *ocelot_port = ocelot->ports[port];
2555
Yangbo Lub049da12019-11-27 15:27:57 +08002556 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002557
2558 /* Basic L2 initialization */
2559
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002560 /* Set MAC IFG Gaps
2561 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2562 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2563 */
2564 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2565 DEV_MAC_IFG_CFG);
2566
2567 /* Load seed (0) and set MAC HDX late collision */
2568 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2569 DEV_MAC_HDX_CFG_SEED_LOAD,
2570 DEV_MAC_HDX_CFG);
2571 mdelay(1);
2572 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2573 DEV_MAC_HDX_CFG);
2574
2575 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02002576 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002577 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2578 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02002579 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002580 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2581 DEV_MAC_TAGS_CFG);
2582
2583 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2584 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2585 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2586
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002587 /* Enable transmission of pause frames */
Maxim Kochetkov541132f2020-07-13 19:57:07 +03002588 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
Vladimir Olteane8e6e732020-07-13 19:57:05 +03002589
Vladimir Oltean31350d72019-11-09 15:02:56 +02002590 /* Drop frames with multicast source address */
2591 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2592 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2593 ANA_PORT_DROP_CFG, port);
2594
2595 /* Set default VLAN and tag type to 8021Q. */
2596 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2597 REW_PORT_VLAN_CFG_PORT_TPID_M,
2598 REW_PORT_VLAN_CFG, port);
2599
Vladimir Oltean421741e2021-02-12 17:15:59 +02002600 /* Disable source address learning for standalone mode */
2601 ocelot_port_set_learning(ocelot, port, false);
2602
Vladimir Oltean46efe4e2021-08-15 04:47:47 +03002603 /* Set the port's initial logical port ID value, enable receiving
2604 * frames on it, and configure the MAC address learning type to
2605 * automatic.
2606 */
2607 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
2608 ANA_PORT_PORT_CFG_RECV_ENA |
2609 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2610 ANA_PORT_PORT_CFG, port);
2611
Vladimir Oltean31350d72019-11-09 15:02:56 +02002612 /* Enable vcap lookups */
2613 ocelot_vcap_enable(ocelot, port);
2614}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002615EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002616
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002617/* Configure and enable the CPU port module, which is a set of queues
2618 * accessible through register MMIO, frame DMA or Ethernet (in case
2619 * NPI mode is used).
Vladimir Oltean69df5782020-02-29 16:50:02 +02002620 */
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002621static void ocelot_cpu_port_init(struct ocelot *ocelot)
Vladimir Oltean21468192019-11-09 15:03:00 +02002622{
Vladimir Oltean69df5782020-02-29 16:50:02 +02002623 int cpu = ocelot->num_phys_ports;
2624
2625 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02002626 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002627 /* Instead set up a multicast destination PGID for traffic copied to
2628 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2629 * addresses will be copied to the CPU via this PGID.
2630 */
Vladimir Oltean21468192019-11-09 15:03:00 +02002631 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2632 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2633 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2634 ANA_PORT_PORT_CFG, cpu);
2635
Vladimir Oltean69df5782020-02-29 16:50:02 +02002636 /* Enable CPU port module */
Vladimir Oltean886e1382020-07-13 19:57:03 +03002637 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002638 /* CPU port Injection/Extraction configuration */
Vladimir Oltean886e1382020-07-13 19:57:03 +03002639 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02002640 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean886e1382020-07-13 19:57:03 +03002641 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02002642 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean21468192019-11-09 15:03:00 +02002643
2644 /* Configure the CPU port to be VLAN aware */
Vladimir Olteanbfbab312021-10-20 20:58:51 +03002645 ocelot_write_gix(ocelot,
2646 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) |
2647 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2648 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
Vladimir Oltean21468192019-11-09 15:03:00 +02002649 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02002650}
Vladimir Oltean21468192019-11-09 15:03:00 +02002651
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002652static void ocelot_detect_features(struct ocelot *ocelot)
2653{
2654 int mmgt, eq_ctrl;
2655
2656 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2657 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2658 * 192 bytes as the documentation incorrectly says.
2659 */
2660 mmgt = ocelot_read(ocelot, SYS_MMGT);
2661 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2662
2663 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2664 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002665}
2666
Alexandre Bellonia556c762018-05-14 22:04:57 +02002667int ocelot_init(struct ocelot *ocelot)
2668{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002669 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02002670 int i, ret;
2671 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002672
Vladimir Oltean3a77b592019-11-14 17:03:26 +02002673 if (ocelot->ops->reset) {
2674 ret = ocelot->ops->reset(ocelot);
2675 if (ret) {
2676 dev_err(ocelot->dev, "Switch reset failed\n");
2677 return ret;
2678 }
2679 }
2680
Alexandre Bellonia556c762018-05-14 22:04:57 +02002681 ocelot->stats = devm_kcalloc(ocelot->dev,
2682 ocelot->num_phys_ports * ocelot->num_stats,
2683 sizeof(u64), GFP_KERNEL);
2684 if (!ocelot->stats)
2685 return -ENOMEM;
2686
2687 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002688 mutex_init(&ocelot->ptp_lock);
Vladimir Oltean24683462021-10-24 20:17:51 +03002689 mutex_init(&ocelot->mact_lock);
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002690 mutex_init(&ocelot->fwd_domain_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002691 spin_lock_init(&ocelot->ptp_clock_lock);
Vladimir Oltean52849bc2021-10-12 14:40:36 +03002692 spin_lock_init(&ocelot->ts_id_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002693 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2694 dev_name(ocelot->dev));
2695 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2696 if (!ocelot->stats_queue)
2697 return -ENOMEM;
2698
Vladimir Olteanca0b2722020-12-12 21:16:12 +02002699 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2700 if (!ocelot->owq) {
2701 destroy_workqueue(ocelot->stats_queue);
2702 return -ENOMEM;
2703 }
2704
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02002705 INIT_LIST_HEAD(&ocelot->multicast);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02002706 INIT_LIST_HEAD(&ocelot->pgids);
Vladimir Oltean90e0aa82021-10-20 20:58:49 +03002707 INIT_LIST_HEAD(&ocelot->vlans);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02002708 ocelot_detect_features(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002709 ocelot_mact_init(ocelot);
2710 ocelot_vlan_init(ocelot);
Vladimir Olteanaae4e502020-06-20 18:43:46 +03002711 ocelot_vcap_init(ocelot);
Vladimir Oltean2d44b092020-09-26 22:32:01 +03002712 ocelot_cpu_port_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002713
Xiaoliang Yang23e2c502021-11-18 18:11:59 +08002714 if (ocelot->ops->psfp_init)
2715 ocelot->ops->psfp_init(ocelot);
2716
Alexandre Bellonia556c762018-05-14 22:04:57 +02002717 for (port = 0; port < ocelot->num_phys_ports; port++) {
2718 /* Clear all counters (5 groups) */
2719 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2720 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2721 SYS_STAT_CFG);
2722 }
2723
2724 /* Only use S-Tag */
2725 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2726
2727 /* Aggregation mode */
2728 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2729 ANA_AGGR_CFG_AC_DMAC_ENA |
2730 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
Vladimir Olteanf79c20c2021-02-06 00:02:13 +02002731 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2732 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2733 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2734 ANA_AGGR_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002735
2736 /* Set MAC age time to default value. The entry is aged after
2737 * 2*AGE_PERIOD
2738 */
2739 ocelot_write(ocelot,
2740 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2741 ANA_AUTOAGE);
2742
2743 /* Disable learning for frames discarded by VLAN ingress filtering */
2744 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2745
2746 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2747 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2748 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2749
2750 /* Setup flooding PGIDs */
Vladimir Olteanedd24102020-12-04 19:54:16 +02002751 for (i = 0; i < ocelot->num_flooding_pgids; i++)
2752 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
Vladimir Olteanb360d942021-02-12 17:15:58 +02002753 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
Vladimir Olteanedd24102020-12-04 19:54:16 +02002754 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2755 ANA_FLOODING, i);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002756 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2757 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2758 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2759 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2760 ANA_FLOODING_IPMC);
2761
2762 for (port = 0; port < ocelot->num_phys_ports; port++) {
2763 /* Transmit the frame to the local port. */
2764 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2765 /* Do not forward BPDU frames to the front ports. */
2766 ocelot_write_gix(ocelot,
2767 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2768 ANA_PORT_CPU_FWD_BPDU_CFG,
2769 port);
2770 /* Ensure bridging is disabled */
2771 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2772 }
2773
Vladimir Oltean96b029b2020-06-21 14:46:02 +03002774 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02002775 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2776
2777 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2778 }
Horatiu Vulturebb1bb42021-03-16 21:10:17 +01002779
2780 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2781
Vladimir Olteanb360d942021-02-12 17:15:58 +02002782 /* Allow broadcast and unknown L2 multicast to the CPU. */
2783 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2784 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2785 ANA_PGID_PGID, PGID_MC);
2786 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2787 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2788 ANA_PGID_PGID, PGID_BC);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002789 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2790 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2791
Alexandre Bellonia556c762018-05-14 22:04:57 +02002792 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2793 * registers endianness.
2794 */
2795 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2796 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2797 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2798 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2799 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2800 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2801 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2802 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2803 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2804 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2805 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2806 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2807 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2808 for (i = 0; i < 16; i++)
2809 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2810 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2811 ANA_CPUQ_8021_CFG, i);
2812
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002813 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002814 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2815 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002816
Alexandre Bellonia556c762018-05-14 22:04:57 +02002817 return 0;
2818}
2819EXPORT_SYMBOL(ocelot_init);
2820
2821void ocelot_deinit(struct ocelot *ocelot)
2822{
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002823 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002824 destroy_workqueue(ocelot->stats_queue);
Vladimir Olteanca0b2722020-12-12 21:16:12 +02002825 destroy_workqueue(ocelot->owq);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002826 mutex_destroy(&ocelot->stats_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002827}
2828EXPORT_SYMBOL(ocelot_deinit);
2829
Vladimir Olteane5fb5122020-09-18 04:07:30 +03002830void ocelot_deinit_port(struct ocelot *ocelot, int port)
2831{
2832 struct ocelot_port *ocelot_port = ocelot->ports[port];
2833
2834 skb_queue_purge(&ocelot_port->tx_skbs);
2835}
2836EXPORT_SYMBOL(ocelot_deinit_port);
2837
Alexandre Bellonia556c762018-05-14 22:04:57 +02002838MODULE_LICENSE("Dual MIT/GPL");