1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3 * Microsemi Ocelot Switch driver
4 *
5 * Copyright (c) 2017 Microsemi Corporation
6 */
7 #include <linux/dsa/ocelot.h>
8 #include <linux/if_bridge.h>
9 #include <linux/ptp_classify.h>
10 #include <soc/mscc/ocelot_vcap.h>
11 #include "ocelot.h"
12 #include "ocelot_vcap.h"
13
14 #define TABLE_UPDATE_SLEEP_US 10
15 #define TABLE_UPDATE_TIMEOUT_US 100000
16
17 struct ocelot_mact_entry {
18 u8 mac[ETH_ALEN];
19 u16 vid;
20 enum macaccess_entry_type type;
21 };
22
ocelot_mact_read_macaccess(struct ocelot * ocelot)23 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
24 {
25 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
26 }
27
ocelot_mact_wait_for_completion(struct ocelot * ocelot)28 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
29 {
30 u32 val;
31
32 return readx_poll_timeout(ocelot_mact_read_macaccess,
33 ocelot, val,
34 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
35 MACACCESS_CMD_IDLE,
36 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
37 }
38
ocelot_mact_select(struct ocelot * ocelot,const unsigned char mac[ETH_ALEN],unsigned int vid)39 static void ocelot_mact_select(struct ocelot *ocelot,
40 const unsigned char mac[ETH_ALEN],
41 unsigned int vid)
42 {
43 u32 macl = 0, mach = 0;
44
45 /* Set the MAC address to handle and the vlan associated in a format
46 * understood by the hardware.
47 */
48 mach |= vid << 16;
49 mach |= mac[0] << 8;
50 mach |= mac[1] << 0;
51 macl |= mac[2] << 24;
52 macl |= mac[3] << 16;
53 macl |= mac[4] << 8;
54 macl |= mac[5] << 0;
55
56 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
57 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
58
59 }
60
ocelot_mact_learn(struct ocelot * ocelot,int port,const unsigned char mac[ETH_ALEN],unsigned int vid,enum macaccess_entry_type type)61 int ocelot_mact_learn(struct ocelot *ocelot, int port,
62 const unsigned char mac[ETH_ALEN],
63 unsigned int vid, enum macaccess_entry_type type)
64 {
65 u32 cmd = ANA_TABLES_MACACCESS_VALID |
66 ANA_TABLES_MACACCESS_DEST_IDX(port) |
67 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
69 unsigned int mc_ports;
70
71 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
72 if (type == ENTRYTYPE_MACv4)
73 mc_ports = (mac[1] << 8) | mac[2];
74 else if (type == ENTRYTYPE_MACv6)
75 mc_ports = (mac[0] << 8) | mac[1];
76 else
77 mc_ports = 0;
78
79 if (mc_ports & BIT(ocelot->num_phys_ports))
80 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
81
82 ocelot_mact_select(ocelot, mac, vid);
83
84 /* Issue a write command */
85 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
86
87 return ocelot_mact_wait_for_completion(ocelot);
88 }
89 EXPORT_SYMBOL(ocelot_mact_learn);
90
ocelot_mact_forget(struct ocelot * ocelot,const unsigned char mac[ETH_ALEN],unsigned int vid)91 int ocelot_mact_forget(struct ocelot *ocelot,
92 const unsigned char mac[ETH_ALEN], unsigned int vid)
93 {
94 ocelot_mact_select(ocelot, mac, vid);
95
96 /* Issue a forget command */
97 ocelot_write(ocelot,
98 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
99 ANA_TABLES_MACACCESS);
100
101 return ocelot_mact_wait_for_completion(ocelot);
102 }
103 EXPORT_SYMBOL(ocelot_mact_forget);
104
ocelot_mact_init(struct ocelot * ocelot)105 static void ocelot_mact_init(struct ocelot *ocelot)
106 {
107 /* Configure the learning mode entries attributes:
108 * - Do not copy the frame to the CPU extraction queues.
109 * - Use the vlan and mac_cpoy for dmac lookup.
110 */
111 ocelot_rmw(ocelot, 0,
112 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
113 | ANA_AGENCTRL_LEARN_FWD_KILL
114 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
115 ANA_AGENCTRL);
116
117 /* Clear the MAC table */
118 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
119 }
120
ocelot_vcap_enable(struct ocelot * ocelot,int port)121 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
122 {
123 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
124 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
125 ANA_PORT_VCAP_S2_CFG, port);
126
127 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
128 ANA_PORT_VCAP_CFG, port);
129
130 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
131 REW_PORT_CFG_ES0_EN,
132 REW_PORT_CFG, port);
133 }
134
ocelot_vlant_read_vlanaccess(struct ocelot * ocelot)135 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
136 {
137 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
138 }
139
ocelot_vlant_wait_for_completion(struct ocelot * ocelot)140 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
141 {
142 u32 val;
143
144 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
145 ocelot,
146 val,
147 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
148 ANA_TABLES_VLANACCESS_CMD_IDLE,
149 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
150 }
151
ocelot_vlant_set_mask(struct ocelot * ocelot,u16 vid,u32 mask)152 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
153 {
154 /* Select the VID to configure */
155 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
156 ANA_TABLES_VLANTIDX);
157 /* Set the vlan port members mask and issue a write command */
158 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
159 ANA_TABLES_VLANACCESS_CMD_WRITE,
160 ANA_TABLES_VLANACCESS);
161
162 return ocelot_vlant_wait_for_completion(ocelot);
163 }
164
ocelot_port_set_native_vlan(struct ocelot * ocelot,int port,struct ocelot_vlan native_vlan)165 static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
166 struct ocelot_vlan native_vlan)
167 {
168 struct ocelot_port *ocelot_port = ocelot->ports[port];
169 u32 val = 0;
170
171 ocelot_port->native_vlan = native_vlan;
172
173 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
174 REW_PORT_VLAN_CFG_PORT_VID_M,
175 REW_PORT_VLAN_CFG, port);
176
177 if (ocelot_port->vlan_aware) {
178 if (native_vlan.valid)
179 /* Tag all frames except when VID == DEFAULT_VLAN */
180 val = REW_TAG_CFG_TAG_CFG(1);
181 else
182 /* Tag all frames */
183 val = REW_TAG_CFG_TAG_CFG(3);
184 } else {
185 /* Port tagging disabled. */
186 val = REW_TAG_CFG_TAG_CFG(0);
187 }
188 ocelot_rmw_gix(ocelot, val,
189 REW_TAG_CFG_TAG_CFG_M,
190 REW_TAG_CFG, port);
191 }
192
193 /* Default vlan to clasify for untagged frames (may be zero) */
ocelot_port_set_pvid(struct ocelot * ocelot,int port,struct ocelot_vlan pvid_vlan)194 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
195 struct ocelot_vlan pvid_vlan)
196 {
197 struct ocelot_port *ocelot_port = ocelot->ports[port];
198 u32 val = 0;
199
200 ocelot_port->pvid_vlan = pvid_vlan;
201
202 if (!ocelot_port->vlan_aware)
203 pvid_vlan.vid = 0;
204
205 ocelot_rmw_gix(ocelot,
206 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
207 ANA_PORT_VLAN_CFG_VLAN_VID_M,
208 ANA_PORT_VLAN_CFG, port);
209
210 /* If there's no pvid, we should drop not only untagged traffic (which
211 * happens automatically), but also 802.1p traffic which gets
212 * classified to VLAN 0, but that is always in our RX filter, so it
213 * would get accepted were it not for this setting.
214 */
215 if (!pvid_vlan.valid && ocelot_port->vlan_aware)
216 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
217 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
218
219 ocelot_rmw_gix(ocelot, val,
220 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
221 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
222 ANA_PORT_DROP_CFG, port);
223 }
224
ocelot_vlan_member_set(struct ocelot * ocelot,u32 vlan_mask,u16 vid)225 static int ocelot_vlan_member_set(struct ocelot *ocelot, u32 vlan_mask, u16 vid)
226 {
227 int err;
228
229 err = ocelot_vlant_set_mask(ocelot, vid, vlan_mask);
230 if (err)
231 return err;
232
233 ocelot->vlan_mask[vid] = vlan_mask;
234
235 return 0;
236 }
237
ocelot_vlan_member_add(struct ocelot * ocelot,int port,u16 vid)238 static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid)
239 {
240 return ocelot_vlan_member_set(ocelot,
241 ocelot->vlan_mask[vid] | BIT(port),
242 vid);
243 }
244
ocelot_vlan_member_del(struct ocelot * ocelot,int port,u16 vid)245 static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
246 {
247 return ocelot_vlan_member_set(ocelot,
248 ocelot->vlan_mask[vid] & ~BIT(port),
249 vid);
250 }
251
ocelot_port_vlan_filtering(struct ocelot * ocelot,int port,bool vlan_aware,struct netlink_ext_ack * extack)252 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
253 bool vlan_aware, struct netlink_ext_ack *extack)
254 {
255 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
256 struct ocelot_port *ocelot_port = ocelot->ports[port];
257 struct ocelot_vcap_filter *filter;
258 u32 val;
259
260 list_for_each_entry(filter, &block->rules, list) {
261 if (filter->ingress_port_mask & BIT(port) &&
262 filter->action.vid_replace_ena) {
263 NL_SET_ERR_MSG_MOD(extack,
264 "Cannot change VLAN state with vlan modify rules active");
265 return -EBUSY;
266 }
267 }
268
269 ocelot_port->vlan_aware = vlan_aware;
270
271 if (vlan_aware)
272 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
273 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
274 else
275 val = 0;
276 ocelot_rmw_gix(ocelot, val,
277 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
278 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
279 ANA_PORT_VLAN_CFG, port);
280
281 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
282 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
283
284 return 0;
285 }
286 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
287
ocelot_vlan_prepare(struct ocelot * ocelot,int port,u16 vid,bool pvid,bool untagged,struct netlink_ext_ack * extack)288 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
289 bool untagged, struct netlink_ext_ack *extack)
290 {
291 struct ocelot_port *ocelot_port = ocelot->ports[port];
292
293 /* Deny changing the native VLAN, but always permit deleting it */
294 if (untagged && ocelot_port->native_vlan.vid != vid &&
295 ocelot_port->native_vlan.valid) {
296 NL_SET_ERR_MSG_MOD(extack,
297 "Port already has a native VLAN");
298 return -EBUSY;
299 }
300
301 return 0;
302 }
303 EXPORT_SYMBOL(ocelot_vlan_prepare);
304
ocelot_vlan_add(struct ocelot * ocelot,int port,u16 vid,bool pvid,bool untagged)305 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
306 bool untagged)
307 {
308 int err;
309
310 err = ocelot_vlan_member_add(ocelot, port, vid);
311 if (err)
312 return err;
313
314 /* Default ingress vlan classification */
315 if (pvid) {
316 struct ocelot_vlan pvid_vlan;
317
318 pvid_vlan.vid = vid;
319 pvid_vlan.valid = true;
320 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
321 }
322
323 /* Untagged egress vlan clasification */
324 if (untagged) {
325 struct ocelot_vlan native_vlan;
326
327 native_vlan.vid = vid;
328 native_vlan.valid = true;
329 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
330 }
331
332 return 0;
333 }
334 EXPORT_SYMBOL(ocelot_vlan_add);
335
ocelot_vlan_del(struct ocelot * ocelot,int port,u16 vid)336 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
337 {
338 struct ocelot_port *ocelot_port = ocelot->ports[port];
339 int err;
340
341 err = ocelot_vlan_member_del(ocelot, port, vid);
342 if (err)
343 return err;
344
345 /* Ingress */
346 if (ocelot_port->pvid_vlan.vid == vid) {
347 struct ocelot_vlan pvid_vlan = {0};
348
349 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
350 }
351
352 /* Egress */
353 if (ocelot_port->native_vlan.vid == vid) {
354 struct ocelot_vlan native_vlan = {0};
355
356 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
357 }
358
359 return 0;
360 }
361 EXPORT_SYMBOL(ocelot_vlan_del);
362
ocelot_vlan_init(struct ocelot * ocelot)363 static void ocelot_vlan_init(struct ocelot *ocelot)
364 {
365 unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
366 u16 port, vid;
367
368 /* Clear VLAN table, by default all ports are members of all VLANs */
369 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
370 ANA_TABLES_VLANACCESS);
371 ocelot_vlant_wait_for_completion(ocelot);
372
373 /* Configure the port VLAN memberships */
374 for (vid = 1; vid < VLAN_N_VID; vid++)
375 ocelot_vlan_member_set(ocelot, 0, vid);
376
377 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
378 * traffic. It is added automatically if 8021q module is loaded, but
379 * we can't rely on it since module may be not loaded.
380 */
381 ocelot_vlan_member_set(ocelot, all_ports, 0);
382
383 /* Set vlan ingress filter mask to all ports but the CPU port by
384 * default.
385 */
386 ocelot_write(ocelot, all_ports, ANA_VLANMASK);
387
388 for (port = 0; port < ocelot->num_phys_ports; port++) {
389 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
390 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
391 }
392 }
393
ocelot_read_eq_avail(struct ocelot * ocelot,int port)394 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
395 {
396 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
397 }
398
ocelot_port_flush(struct ocelot * ocelot,int port)399 static int ocelot_port_flush(struct ocelot *ocelot, int port)
400 {
401 unsigned int pause_ena;
402 int err, val;
403
404 /* Disable dequeuing from the egress queues */
405 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
406 QSYS_PORT_MODE_DEQUEUE_DIS,
407 QSYS_PORT_MODE, port);
408
409 /* Disable flow control */
410 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
411 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
412
413 /* Disable priority flow control */
414 ocelot_fields_write(ocelot, port,
415 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
416
417 /* Wait at least the time it takes to receive a frame of maximum length
418 * at the port.
419 * Worst-case delays for 10 kilobyte jumbo frames are:
420 * 8 ms on a 10M port
421 * 800 μs on a 100M port
422 * 80 μs on a 1G port
423 * 32 μs on a 2.5G port
424 */
425 usleep_range(8000, 10000);
426
427 /* Disable half duplex backpressure. */
428 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
429 SYS_FRONT_PORT_MODE, port);
430
431 /* Flush the queues associated with the port. */
432 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
433 REW_PORT_CFG, port);
434
435 /* Enable dequeuing from the egress queues. */
436 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
437 port);
438
439 /* Wait until flushing is complete. */
440 err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
441 100, 2000000, false, ocelot, port);
442
443 /* Clear flushing again. */
444 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
445
446 /* Re-enable flow control */
447 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
448
449 return err;
450 }
451
ocelot_phylink_mac_link_down(struct ocelot * ocelot,int port,unsigned int link_an_mode,phy_interface_t interface,unsigned long quirks)452 void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
453 unsigned int link_an_mode,
454 phy_interface_t interface,
455 unsigned long quirks)
456 {
457 struct ocelot_port *ocelot_port = ocelot->ports[port];
458 int err;
459
460 ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
461 DEV_MAC_ENA_CFG);
462
463 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
464
465 err = ocelot_port_flush(ocelot, port);
466 if (err)
467 dev_err(ocelot->dev, "failed to flush port %d: %d\n",
468 port, err);
469
470 /* Put the port in reset. */
471 if (interface != PHY_INTERFACE_MODE_QSGMII ||
472 !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
473 ocelot_port_rmwl(ocelot_port,
474 DEV_CLOCK_CFG_MAC_TX_RST |
475 DEV_CLOCK_CFG_MAC_RX_RST,
476 DEV_CLOCK_CFG_MAC_TX_RST |
477 DEV_CLOCK_CFG_MAC_RX_RST,
478 DEV_CLOCK_CFG);
479 }
480 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
481
ocelot_phylink_mac_link_up(struct ocelot * ocelot,int port,struct phy_device * phydev,unsigned int link_an_mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause,unsigned long quirks)482 void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
483 struct phy_device *phydev,
484 unsigned int link_an_mode,
485 phy_interface_t interface,
486 int speed, int duplex,
487 bool tx_pause, bool rx_pause,
488 unsigned long quirks)
489 {
490 struct ocelot_port *ocelot_port = ocelot->ports[port];
491 int mac_speed, mode = 0;
492 u32 mac_fc_cfg;
493
494 /* The MAC might be integrated in systems where the MAC speed is fixed
495 * and it's the PCS who is performing the rate adaptation, so we have
496 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
497 * (which is also its default value).
498 */
499 if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
500 speed == SPEED_1000) {
501 mac_speed = OCELOT_SPEED_1000;
502 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
503 } else if (speed == SPEED_2500) {
504 mac_speed = OCELOT_SPEED_2500;
505 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
506 } else if (speed == SPEED_100) {
507 mac_speed = OCELOT_SPEED_100;
508 } else {
509 mac_speed = OCELOT_SPEED_10;
510 }
511
512 if (duplex == DUPLEX_FULL)
513 mode |= DEV_MAC_MODE_CFG_FDX_ENA;
514
515 ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
516
517 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
518 * PORT_RST bits in DEV_CLOCK_CFG.
519 */
520 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
521 DEV_CLOCK_CFG);
522
523 switch (speed) {
524 case SPEED_10:
525 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
526 break;
527 case SPEED_100:
528 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
529 break;
530 case SPEED_1000:
531 case SPEED_2500:
532 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
533 break;
534 default:
535 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
536 port, speed);
537 return;
538 }
539
540 /* Handle RX pause in all cases, with 2500base-X this is used for rate
541 * adaptation.
542 */
543 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
544
545 if (tx_pause)
546 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
547 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
548 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
549 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
550
551 /* Flow control. Link speed is only used here to evaluate the time
552 * specification in incoming pause frames.
553 */
554 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
555
556 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
557
558 /* Don't attempt to send PAUSE frames on the NPI port, it's broken */
559 if (port != ocelot->npi)
560 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
561 tx_pause);
562
563 /* Undo the effects of ocelot_phylink_mac_link_down:
564 * enable MAC module
565 */
566 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
567 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
568
569 /* Core: Enable port for frame transfer */
570 ocelot_fields_write(ocelot, port,
571 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
572 }
573 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
574
ocelot_port_add_txtstamp_skb(struct ocelot * ocelot,int port,struct sk_buff * clone)575 static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
576 struct sk_buff *clone)
577 {
578 struct ocelot_port *ocelot_port = ocelot->ports[port];
579 unsigned long flags;
580
581 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
582
583 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
584 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
585 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
586 return -EBUSY;
587 }
588
589 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
590 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
591 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
592
593 ocelot_port->ts_id++;
594 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
595 ocelot_port->ts_id = 0;
596
597 ocelot_port->ptp_skbs_in_flight++;
598 ocelot->ptp_skbs_in_flight++;
599
600 skb_queue_tail(&ocelot_port->tx_skbs, clone);
601
602 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
603
604 return 0;
605 }
606
ocelot_ptp_is_onestep_sync(struct sk_buff * skb,unsigned int ptp_class)607 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
608 unsigned int ptp_class)
609 {
610 struct ptp_header *hdr;
611 u8 msgtype, twostep;
612
613 hdr = ptp_parse_header(skb, ptp_class);
614 if (!hdr)
615 return false;
616
617 msgtype = ptp_get_msgtype(hdr, ptp_class);
618 twostep = hdr->flag_field[0] & 0x2;
619
620 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
621 return true;
622
623 return false;
624 }
625
ocelot_port_txtstamp_request(struct ocelot * ocelot,int port,struct sk_buff * skb,struct sk_buff ** clone)626 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
627 struct sk_buff *skb,
628 struct sk_buff **clone)
629 {
630 struct ocelot_port *ocelot_port = ocelot->ports[port];
631 u8 ptp_cmd = ocelot_port->ptp_cmd;
632 unsigned int ptp_class;
633 int err;
634
635 /* Don't do anything if PTP timestamping not enabled */
636 if (!ptp_cmd)
637 return 0;
638
639 ptp_class = ptp_classify_raw(skb);
640 if (ptp_class == PTP_CLASS_NONE)
641 return -EINVAL;
642
643 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
644 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
645 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
646 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
647 return 0;
648 }
649
650 /* Fall back to two-step timestamping */
651 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
652 }
653
654 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
655 *clone = skb_clone_sk(skb);
656 if (!(*clone))
657 return -ENOMEM;
658
659 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
660 if (err)
661 return err;
662
663 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
664 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
665 }
666
667 return 0;
668 }
669 EXPORT_SYMBOL(ocelot_port_txtstamp_request);
670
ocelot_get_hwtimestamp(struct ocelot * ocelot,struct timespec64 * ts)671 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
672 struct timespec64 *ts)
673 {
674 unsigned long flags;
675 u32 val;
676
677 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
678
679 /* Read current PTP time to get seconds */
680 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
681
682 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
683 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
684 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
685 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
686
687 /* Read packet HW timestamp from FIFO */
688 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
689 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
690
691 /* Sec has incremented since the ts was registered */
692 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
693 ts->tv_sec--;
694
695 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
696 }
697
ocelot_validate_ptp_skb(struct sk_buff * clone,u16 seqid)698 static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
699 {
700 struct ptp_header *hdr;
701
702 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
703 if (WARN_ON(!hdr))
704 return false;
705
706 return seqid == ntohs(hdr->sequence_id);
707 }
708
ocelot_get_txtstamp(struct ocelot * ocelot)709 void ocelot_get_txtstamp(struct ocelot *ocelot)
710 {
711 int budget = OCELOT_PTP_QUEUE_SZ;
712
713 while (budget--) {
714 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
715 struct skb_shared_hwtstamps shhwtstamps;
716 u32 val, id, seqid, txport;
717 struct ocelot_port *port;
718 struct timespec64 ts;
719 unsigned long flags;
720
721 val = ocelot_read(ocelot, SYS_PTP_STATUS);
722
723 /* Check if a timestamp can be retrieved */
724 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
725 break;
726
727 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
728
729 /* Retrieve the ts ID and Tx port */
730 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
731 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
732 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
733
734 port = ocelot->ports[txport];
735
736 spin_lock(&ocelot->ts_id_lock);
737 port->ptp_skbs_in_flight--;
738 ocelot->ptp_skbs_in_flight--;
739 spin_unlock(&ocelot->ts_id_lock);
740
741 /* Retrieve its associated skb */
742 try_again:
743 spin_lock_irqsave(&port->tx_skbs.lock, flags);
744
745 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
746 if (OCELOT_SKB_CB(skb)->ts_id != id)
747 continue;
748 __skb_unlink(skb, &port->tx_skbs);
749 skb_match = skb;
750 break;
751 }
752
753 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
754
755 if (WARN_ON(!skb_match))
756 continue;
757
758 if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
759 dev_err_ratelimited(ocelot->dev,
760 "port %d received stale TX timestamp for seqid %d, discarding\n",
761 txport, seqid);
762 dev_kfree_skb_any(skb);
763 goto try_again;
764 }
765
766 /* Get the h/w timestamp */
767 ocelot_get_hwtimestamp(ocelot, &ts);
768
769 /* Set the timestamp into the skb */
770 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
771 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
772 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
773
774 /* Next ts */
775 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
776 }
777 }
778 EXPORT_SYMBOL(ocelot_get_txtstamp);
779
ocelot_rx_frame_word(struct ocelot * ocelot,u8 grp,bool ifh,u32 * rval)780 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
781 u32 *rval)
782 {
783 u32 bytes_valid, val;
784
785 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
786 if (val == XTR_NOT_READY) {
787 if (ifh)
788 return -EIO;
789
790 do {
791 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
792 } while (val == XTR_NOT_READY);
793 }
794
795 switch (val) {
796 case XTR_ABORT:
797 return -EIO;
798 case XTR_EOF_0:
799 case XTR_EOF_1:
800 case XTR_EOF_2:
801 case XTR_EOF_3:
802 case XTR_PRUNED:
803 bytes_valid = XTR_VALID_BYTES(val);
804 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
805 if (val == XTR_ESCAPE)
806 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
807 else
808 *rval = val;
809
810 return bytes_valid;
811 case XTR_ESCAPE:
812 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
813
814 return 4;
815 default:
816 *rval = val;
817
818 return 4;
819 }
820 }
821
ocelot_xtr_poll_xfh(struct ocelot * ocelot,int grp,u32 * xfh)822 static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
823 {
824 int i, err = 0;
825
826 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
827 err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
828 if (err != 4)
829 return (err < 0) ? err : -EIO;
830 }
831
832 return 0;
833 }
834
ocelot_xtr_poll_frame(struct ocelot * ocelot,int grp,struct sk_buff ** nskb)835 int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
836 {
837 struct skb_shared_hwtstamps *shhwtstamps;
838 u64 tod_in_ns, full_ts_in_ns;
839 u64 timestamp, src_port, len;
840 u32 xfh[OCELOT_TAG_LEN / 4];
841 struct net_device *dev;
842 struct timespec64 ts;
843 struct sk_buff *skb;
844 int sz, buf_len;
845 u32 val, *buf;
846 int err;
847
848 err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
849 if (err)
850 return err;
851
852 ocelot_xfh_get_src_port(xfh, &src_port);
853 ocelot_xfh_get_len(xfh, &len);
854 ocelot_xfh_get_rew_val(xfh, ×tamp);
855
856 if (WARN_ON(src_port >= ocelot->num_phys_ports))
857 return -EINVAL;
858
859 dev = ocelot->ops->port_to_netdev(ocelot, src_port);
860 if (!dev)
861 return -EINVAL;
862
863 skb = netdev_alloc_skb(dev, len);
864 if (unlikely(!skb)) {
865 netdev_err(dev, "Unable to allocate sk_buff\n");
866 return -ENOMEM;
867 }
868
869 buf_len = len - ETH_FCS_LEN;
870 buf = (u32 *)skb_put(skb, buf_len);
871
872 len = 0;
873 do {
874 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
875 if (sz < 0) {
876 err = sz;
877 goto out_free_skb;
878 }
879 *buf++ = val;
880 len += sz;
881 } while (len < buf_len);
882
883 /* Read the FCS */
884 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
885 if (sz < 0) {
886 err = sz;
887 goto out_free_skb;
888 }
889
890 /* Update the statistics if part of the FCS was read before */
891 len -= ETH_FCS_LEN - sz;
892
893 if (unlikely(dev->features & NETIF_F_RXFCS)) {
894 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
895 *buf = val;
896 }
897
898 if (ocelot->ptp) {
899 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
900
901 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
902 if ((tod_in_ns & 0xffffffff) < timestamp)
903 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
904 timestamp;
905 else
906 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
907 timestamp;
908
909 shhwtstamps = skb_hwtstamps(skb);
910 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
911 shhwtstamps->hwtstamp = full_ts_in_ns;
912 }
913
914 /* Everything we see on an interface that is in the HW bridge
915 * has already been forwarded.
916 */
917 if (ocelot->ports[src_port]->bridge)
918 skb->offload_fwd_mark = 1;
919
920 skb->protocol = eth_type_trans(skb, dev);
921
922 *nskb = skb;
923
924 return 0;
925
926 out_free_skb:
927 kfree_skb(skb);
928 return err;
929 }
930 EXPORT_SYMBOL(ocelot_xtr_poll_frame);
931
ocelot_can_inject(struct ocelot * ocelot,int grp)932 bool ocelot_can_inject(struct ocelot *ocelot, int grp)
933 {
934 u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
935
936 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
937 return false;
938 if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
939 return false;
940
941 return true;
942 }
943 EXPORT_SYMBOL(ocelot_can_inject);
944
ocelot_port_inject_frame(struct ocelot * ocelot,int port,int grp,u32 rew_op,struct sk_buff * skb)945 void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
946 u32 rew_op, struct sk_buff *skb)
947 {
948 u32 ifh[OCELOT_TAG_LEN / 4] = {0};
949 unsigned int i, count, last;
950
951 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
952 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
953
954 ocelot_ifh_set_bypass(ifh, 1);
955 ocelot_ifh_set_dest(ifh, BIT_ULL(port));
956 ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
957 ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
958 ocelot_ifh_set_rew_op(ifh, rew_op);
959
960 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
961 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
962
963 count = DIV_ROUND_UP(skb->len, 4);
964 last = skb->len % 4;
965 for (i = 0; i < count; i++)
966 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
967
968 /* Add padding */
969 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
970 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
971 i++;
972 }
973
974 /* Indicate EOF and valid bytes in last word */
975 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
976 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
977 QS_INJ_CTRL_EOF,
978 QS_INJ_CTRL, grp);
979
980 /* Add dummy CRC */
981 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
982 skb_tx_timestamp(skb);
983
984 skb->dev->stats.tx_packets++;
985 skb->dev->stats.tx_bytes += skb->len;
986 }
987 EXPORT_SYMBOL(ocelot_port_inject_frame);
988
ocelot_drain_cpu_queue(struct ocelot * ocelot,int grp)989 void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
990 {
991 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
992 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
993 }
994 EXPORT_SYMBOL(ocelot_drain_cpu_queue);
995
ocelot_fdb_add(struct ocelot * ocelot,int port,const unsigned char * addr,u16 vid)996 int ocelot_fdb_add(struct ocelot *ocelot, int port,
997 const unsigned char *addr, u16 vid)
998 {
999 int pgid = port;
1000
1001 if (port == ocelot->npi)
1002 pgid = PGID_CPU;
1003
1004 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1005 }
1006 EXPORT_SYMBOL(ocelot_fdb_add);
1007
ocelot_fdb_del(struct ocelot * ocelot,int port,const unsigned char * addr,u16 vid)1008 int ocelot_fdb_del(struct ocelot *ocelot, int port,
1009 const unsigned char *addr, u16 vid)
1010 {
1011 return ocelot_mact_forget(ocelot, addr, vid);
1012 }
1013 EXPORT_SYMBOL(ocelot_fdb_del);
1014
ocelot_port_fdb_do_dump(const unsigned char * addr,u16 vid,bool is_static,void * data)1015 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1016 bool is_static, void *data)
1017 {
1018 struct ocelot_dump_ctx *dump = data;
1019 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1020 u32 seq = dump->cb->nlh->nlmsg_seq;
1021 struct nlmsghdr *nlh;
1022 struct ndmsg *ndm;
1023
1024 if (dump->idx < dump->cb->args[2])
1025 goto skip;
1026
1027 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1028 sizeof(*ndm), NLM_F_MULTI);
1029 if (!nlh)
1030 return -EMSGSIZE;
1031
1032 ndm = nlmsg_data(nlh);
1033 ndm->ndm_family = AF_BRIDGE;
1034 ndm->ndm_pad1 = 0;
1035 ndm->ndm_pad2 = 0;
1036 ndm->ndm_flags = NTF_SELF;
1037 ndm->ndm_type = 0;
1038 ndm->ndm_ifindex = dump->dev->ifindex;
1039 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
1040
1041 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1042 goto nla_put_failure;
1043
1044 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1045 goto nla_put_failure;
1046
1047 nlmsg_end(dump->skb, nlh);
1048
1049 skip:
1050 dump->idx++;
1051 return 0;
1052
1053 nla_put_failure:
1054 nlmsg_cancel(dump->skb, nlh);
1055 return -EMSGSIZE;
1056 }
1057 EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1058
ocelot_mact_read(struct ocelot * ocelot,int port,int row,int col,struct ocelot_mact_entry * entry)1059 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1060 struct ocelot_mact_entry *entry)
1061 {
1062 u32 val, dst, macl, mach;
1063 char mac[ETH_ALEN];
1064
1065 /* Set row and column to read from */
1066 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1067 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1068
1069 /* Issue a read command */
1070 ocelot_write(ocelot,
1071 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1072 ANA_TABLES_MACACCESS);
1073
1074 if (ocelot_mact_wait_for_completion(ocelot))
1075 return -ETIMEDOUT;
1076
1077 /* Read the entry flags */
1078 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1079 if (!(val & ANA_TABLES_MACACCESS_VALID))
1080 return -EINVAL;
1081
1082 /* If the entry read has another port configured as its destination,
1083 * do not report it.
1084 */
1085 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1086 if (dst != port)
1087 return -EINVAL;
1088
1089 /* Get the entry's MAC address and VLAN id */
1090 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1091 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1092
1093 mac[0] = (mach >> 8) & 0xff;
1094 mac[1] = (mach >> 0) & 0xff;
1095 mac[2] = (macl >> 24) & 0xff;
1096 mac[3] = (macl >> 16) & 0xff;
1097 mac[4] = (macl >> 8) & 0xff;
1098 mac[5] = (macl >> 0) & 0xff;
1099
1100 entry->vid = (mach >> 16) & 0xfff;
1101 ether_addr_copy(entry->mac, mac);
1102
1103 return 0;
1104 }
1105
ocelot_fdb_dump(struct ocelot * ocelot,int port,dsa_fdb_dump_cb_t * cb,void * data)1106 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1107 dsa_fdb_dump_cb_t *cb, void *data)
1108 {
1109 int i, j;
1110
1111 /* Loop through all the mac tables entries. */
1112 for (i = 0; i < ocelot->num_mact_rows; i++) {
1113 for (j = 0; j < 4; j++) {
1114 struct ocelot_mact_entry entry;
1115 bool is_static;
1116 int ret;
1117
1118 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1119 /* If the entry is invalid (wrong port, invalid...),
1120 * skip it.
1121 */
1122 if (ret == -EINVAL)
1123 continue;
1124 else if (ret)
1125 return ret;
1126
1127 is_static = (entry.type == ENTRYTYPE_LOCKED);
1128
1129 ret = cb(entry.mac, entry.vid, is_static, data);
1130 if (ret)
1131 return ret;
1132 }
1133 }
1134
1135 return 0;
1136 }
1137 EXPORT_SYMBOL(ocelot_fdb_dump);
1138
ocelot_hwstamp_get(struct ocelot * ocelot,int port,struct ifreq * ifr)1139 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
1140 {
1141 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1142 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1143 }
1144 EXPORT_SYMBOL(ocelot_hwstamp_get);
1145
ocelot_hwstamp_set(struct ocelot * ocelot,int port,struct ifreq * ifr)1146 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
1147 {
1148 struct ocelot_port *ocelot_port = ocelot->ports[port];
1149 struct hwtstamp_config cfg;
1150
1151 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1152 return -EFAULT;
1153
1154 /* reserved for future extensions */
1155 if (cfg.flags)
1156 return -EINVAL;
1157
1158 /* Tx type sanity check */
1159 switch (cfg.tx_type) {
1160 case HWTSTAMP_TX_ON:
1161 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
1162 break;
1163 case HWTSTAMP_TX_ONESTEP_SYNC:
1164 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1165 * need to update the origin time.
1166 */
1167 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
1168 break;
1169 case HWTSTAMP_TX_OFF:
1170 ocelot_port->ptp_cmd = 0;
1171 break;
1172 default:
1173 return -ERANGE;
1174 }
1175
1176 mutex_lock(&ocelot->ptp_lock);
1177
1178 switch (cfg.rx_filter) {
1179 case HWTSTAMP_FILTER_NONE:
1180 break;
1181 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1182 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1183 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1184 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1185 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1186 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1187 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1188 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1189 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1190 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1191 break;
1192 default:
1193 mutex_unlock(&ocelot->ptp_lock);
1194 return -ERANGE;
1195 }
1196
1197 /* Commit back the result & save it */
1198 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1199 mutex_unlock(&ocelot->ptp_lock);
1200
1201 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1202 }
1203 EXPORT_SYMBOL(ocelot_hwstamp_set);
1204
ocelot_get_strings(struct ocelot * ocelot,int port,u32 sset,u8 * data)1205 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1206 {
1207 int i;
1208
1209 if (sset != ETH_SS_STATS)
1210 return;
1211
1212 for (i = 0; i < ocelot->num_stats; i++)
1213 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1214 ETH_GSTRING_LEN);
1215 }
1216 EXPORT_SYMBOL(ocelot_get_strings);
1217
1218 /* Caller must hold &ocelot->stats_lock */
ocelot_update_stats(struct ocelot * ocelot)1219 static void ocelot_update_stats(struct ocelot *ocelot)
1220 {
1221 int i, j;
1222
1223 for (i = 0; i < ocelot->num_phys_ports; i++) {
1224 /* Configure the port to read the stats from */
1225 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1226
1227 for (j = 0; j < ocelot->num_stats; j++) {
1228 u32 val;
1229 unsigned int idx = i * ocelot->num_stats + j;
1230
1231 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1232 ocelot->stats_layout[j].offset);
1233
1234 if (val < (ocelot->stats[idx] & U32_MAX))
1235 ocelot->stats[idx] += (u64)1 << 32;
1236
1237 ocelot->stats[idx] = (ocelot->stats[idx] &
1238 ~(u64)U32_MAX) + val;
1239 }
1240 }
1241 }
1242
ocelot_check_stats_work(struct work_struct * work)1243 static void ocelot_check_stats_work(struct work_struct *work)
1244 {
1245 struct delayed_work *del_work = to_delayed_work(work);
1246 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1247 stats_work);
1248
1249 mutex_lock(&ocelot->stats_lock);
1250 ocelot_update_stats(ocelot);
1251 mutex_unlock(&ocelot->stats_lock);
1252
1253 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1254 OCELOT_STATS_CHECK_DELAY);
1255 }
1256
ocelot_get_ethtool_stats(struct ocelot * ocelot,int port,u64 * data)1257 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1258 {
1259 int i;
1260
1261 mutex_lock(&ocelot->stats_lock);
1262
1263 /* check and update now */
1264 ocelot_update_stats(ocelot);
1265
1266 /* Copy all counters */
1267 for (i = 0; i < ocelot->num_stats; i++)
1268 *data++ = ocelot->stats[port * ocelot->num_stats + i];
1269
1270 mutex_unlock(&ocelot->stats_lock);
1271 }
1272 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1273
ocelot_get_sset_count(struct ocelot * ocelot,int port,int sset)1274 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1275 {
1276 if (sset != ETH_SS_STATS)
1277 return -EOPNOTSUPP;
1278
1279 return ocelot->num_stats;
1280 }
1281 EXPORT_SYMBOL(ocelot_get_sset_count);
1282
ocelot_get_ts_info(struct ocelot * ocelot,int port,struct ethtool_ts_info * info)1283 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1284 struct ethtool_ts_info *info)
1285 {
1286 info->phc_index = ocelot->ptp_clock ?
1287 ptp_clock_index(ocelot->ptp_clock) : -1;
1288 if (info->phc_index == -1) {
1289 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1290 SOF_TIMESTAMPING_RX_SOFTWARE |
1291 SOF_TIMESTAMPING_SOFTWARE;
1292 return 0;
1293 }
1294 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1295 SOF_TIMESTAMPING_RX_SOFTWARE |
1296 SOF_TIMESTAMPING_SOFTWARE |
1297 SOF_TIMESTAMPING_TX_HARDWARE |
1298 SOF_TIMESTAMPING_RX_HARDWARE |
1299 SOF_TIMESTAMPING_RAW_HARDWARE;
1300 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1301 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1302 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1303 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1304 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1305 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
1306
1307 return 0;
1308 }
1309 EXPORT_SYMBOL(ocelot_get_ts_info);
1310
ocelot_get_bond_mask(struct ocelot * ocelot,struct net_device * bond)1311 static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1312 {
1313 u32 mask = 0;
1314 int port;
1315
1316 for (port = 0; port < ocelot->num_phys_ports; port++) {
1317 struct ocelot_port *ocelot_port = ocelot->ports[port];
1318
1319 if (!ocelot_port)
1320 continue;
1321
1322 if (ocelot_port->bond == bond)
1323 mask |= BIT(port);
1324 }
1325
1326 return mask;
1327 }
1328
ocelot_get_bridge_fwd_mask(struct ocelot * ocelot,int src_port,struct net_device * bridge)1329 static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
1330 struct net_device *bridge)
1331 {
1332 struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1333 u32 mask = 0;
1334 int port;
1335
1336 if (!ocelot_port || ocelot_port->bridge != bridge ||
1337 ocelot_port->stp_state != BR_STATE_FORWARDING)
1338 return 0;
1339
1340 for (port = 0; port < ocelot->num_phys_ports; port++) {
1341 ocelot_port = ocelot->ports[port];
1342
1343 if (!ocelot_port)
1344 continue;
1345
1346 if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1347 ocelot_port->bridge == bridge)
1348 mask |= BIT(port);
1349 }
1350
1351 return mask;
1352 }
1353
ocelot_get_dsa_8021q_cpu_mask(struct ocelot * ocelot)1354 static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
1355 {
1356 u32 mask = 0;
1357 int port;
1358
1359 for (port = 0; port < ocelot->num_phys_ports; port++) {
1360 struct ocelot_port *ocelot_port = ocelot->ports[port];
1361
1362 if (!ocelot_port)
1363 continue;
1364
1365 if (ocelot_port->is_dsa_8021q_cpu)
1366 mask |= BIT(port);
1367 }
1368
1369 return mask;
1370 }
1371
ocelot_apply_bridge_fwd_mask(struct ocelot * ocelot)1372 void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1373 {
1374 unsigned long cpu_fwd_mask;
1375 int port;
1376
1377 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1378 * regular forwarding path of the front ports regardless of whether
1379 * those are bridged or standalone.
1380 * If DSA tag_8021q is not used, this returns 0, which is fine because
1381 * the hardware-based CPU port module can be a destination for packets
1382 * even if it isn't part of PGID_SRC.
1383 */
1384 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1385
1386 /* Apply FWD mask. The loop is needed to add/remove the current port as
1387 * a source for the other ports.
1388 */
1389 for (port = 0; port < ocelot->num_phys_ports; port++) {
1390 struct ocelot_port *ocelot_port = ocelot->ports[port];
1391 unsigned long mask;
1392
1393 if (!ocelot_port) {
1394 /* Unused ports can't send anywhere */
1395 mask = 0;
1396 } else if (ocelot_port->is_dsa_8021q_cpu) {
1397 /* The DSA tag_8021q CPU ports need to be able to
1398 * forward packets to all other ports except for
1399 * themselves
1400 */
1401 mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1402 mask &= ~cpu_fwd_mask;
1403 } else if (ocelot_port->bridge) {
1404 struct net_device *bridge = ocelot_port->bridge;
1405 struct net_device *bond = ocelot_port->bond;
1406
1407 mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
1408 mask |= cpu_fwd_mask;
1409 mask &= ~BIT(port);
1410 if (bond)
1411 mask &= ~ocelot_get_bond_mask(ocelot, bond);
1412 } else {
1413 /* Standalone ports forward only to DSA tag_8021q CPU
1414 * ports (if those exist), or to the hardware CPU port
1415 * module otherwise.
1416 */
1417 mask = cpu_fwd_mask;
1418 }
1419
1420 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
1421 }
1422 }
1423 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
1424
ocelot_bridge_stp_state_set(struct ocelot * ocelot,int port,u8 state)1425 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1426 {
1427 struct ocelot_port *ocelot_port = ocelot->ports[port];
1428 u32 learn_ena = 0;
1429
1430 ocelot_port->stp_state = state;
1431
1432 if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1433 ocelot_port->learn_ena)
1434 learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1435
1436 ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1437 ANA_PORT_PORT_CFG, port);
1438
1439 ocelot_apply_bridge_fwd_mask(ocelot);
1440 }
1441 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1442
ocelot_set_ageing_time(struct ocelot * ocelot,unsigned int msecs)1443 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1444 {
1445 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1446
1447 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1448 * which is clearly not what our intention is. So avoid that.
1449 */
1450 if (!age_period)
1451 age_period = 1;
1452
1453 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1454 }
1455 EXPORT_SYMBOL(ocelot_set_ageing_time);
1456
ocelot_multicast_get(struct ocelot * ocelot,const unsigned char * addr,u16 vid)1457 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1458 const unsigned char *addr,
1459 u16 vid)
1460 {
1461 struct ocelot_multicast *mc;
1462
1463 list_for_each_entry(mc, &ocelot->multicast, list) {
1464 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1465 return mc;
1466 }
1467
1468 return NULL;
1469 }
1470
ocelot_classify_mdb(const unsigned char * addr)1471 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1472 {
1473 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1474 return ENTRYTYPE_MACv4;
1475 if (addr[0] == 0x33 && addr[1] == 0x33)
1476 return ENTRYTYPE_MACv6;
1477 return ENTRYTYPE_LOCKED;
1478 }
1479
ocelot_pgid_alloc(struct ocelot * ocelot,int index,unsigned long ports)1480 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1481 unsigned long ports)
1482 {
1483 struct ocelot_pgid *pgid;
1484
1485 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1486 if (!pgid)
1487 return ERR_PTR(-ENOMEM);
1488
1489 pgid->ports = ports;
1490 pgid->index = index;
1491 refcount_set(&pgid->refcount, 1);
1492 list_add_tail(&pgid->list, &ocelot->pgids);
1493
1494 return pgid;
1495 }
1496
ocelot_pgid_free(struct ocelot * ocelot,struct ocelot_pgid * pgid)1497 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1498 {
1499 if (!refcount_dec_and_test(&pgid->refcount))
1500 return;
1501
1502 list_del(&pgid->list);
1503 kfree(pgid);
1504 }
1505
ocelot_mdb_get_pgid(struct ocelot * ocelot,const struct ocelot_multicast * mc)1506 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1507 const struct ocelot_multicast *mc)
1508 {
1509 struct ocelot_pgid *pgid;
1510 int index;
1511
1512 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1513 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1514 * destination mask table (PGID), the destination set is programmed as
1515 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1516 */
1517 if (mc->entry_type == ENTRYTYPE_MACv4 ||
1518 mc->entry_type == ENTRYTYPE_MACv6)
1519 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
1520
1521 list_for_each_entry(pgid, &ocelot->pgids, list) {
1522 /* When searching for a nonreserved multicast PGID, ignore the
1523 * dummy PGID of zero that we have for MACv4/MACv6 entries
1524 */
1525 if (pgid->index && pgid->ports == mc->ports) {
1526 refcount_inc(&pgid->refcount);
1527 return pgid;
1528 }
1529 }
1530
1531 /* Search for a free index in the nonreserved multicast PGID area */
1532 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
1533 bool used = false;
1534
1535 list_for_each_entry(pgid, &ocelot->pgids, list) {
1536 if (pgid->index == index) {
1537 used = true;
1538 break;
1539 }
1540 }
1541
1542 if (!used)
1543 return ocelot_pgid_alloc(ocelot, index, mc->ports);
1544 }
1545
1546 return ERR_PTR(-ENOSPC);
1547 }
1548
ocelot_encode_ports_to_mdb(unsigned char * addr,struct ocelot_multicast * mc)1549 static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1550 struct ocelot_multicast *mc)
1551 {
1552 ether_addr_copy(addr, mc->addr);
1553
1554 if (mc->entry_type == ENTRYTYPE_MACv4) {
1555 addr[0] = 0;
1556 addr[1] = mc->ports >> 8;
1557 addr[2] = mc->ports & 0xff;
1558 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
1559 addr[0] = mc->ports >> 8;
1560 addr[1] = mc->ports & 0xff;
1561 }
1562 }
1563
ocelot_port_mdb_add(struct ocelot * ocelot,int port,const struct switchdev_obj_port_mdb * mdb)1564 int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1565 const struct switchdev_obj_port_mdb *mdb)
1566 {
1567 unsigned char addr[ETH_ALEN];
1568 struct ocelot_multicast *mc;
1569 struct ocelot_pgid *pgid;
1570 u16 vid = mdb->vid;
1571
1572 if (port == ocelot->npi)
1573 port = ocelot->num_phys_ports;
1574
1575 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1576 if (!mc) {
1577 /* New entry */
1578 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1579 if (!mc)
1580 return -ENOMEM;
1581
1582 mc->entry_type = ocelot_classify_mdb(mdb->addr);
1583 ether_addr_copy(mc->addr, mdb->addr);
1584 mc->vid = vid;
1585
1586 list_add_tail(&mc->list, &ocelot->multicast);
1587 } else {
1588 /* Existing entry. Clean up the current port mask from
1589 * hardware now, because we'll be modifying it.
1590 */
1591 ocelot_pgid_free(ocelot, mc->pgid);
1592 ocelot_encode_ports_to_mdb(addr, mc);
1593 ocelot_mact_forget(ocelot, addr, vid);
1594 }
1595
1596 mc->ports |= BIT(port);
1597
1598 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1599 if (IS_ERR(pgid)) {
1600 dev_err(ocelot->dev,
1601 "Cannot allocate PGID for mdb %pM vid %d\n",
1602 mc->addr, mc->vid);
1603 devm_kfree(ocelot->dev, mc);
1604 return PTR_ERR(pgid);
1605 }
1606 mc->pgid = pgid;
1607
1608 ocelot_encode_ports_to_mdb(addr, mc);
1609
1610 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1611 mc->entry_type != ENTRYTYPE_MACv6)
1612 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1613 pgid->index);
1614
1615 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1616 mc->entry_type);
1617 }
1618 EXPORT_SYMBOL(ocelot_port_mdb_add);
1619
ocelot_port_mdb_del(struct ocelot * ocelot,int port,const struct switchdev_obj_port_mdb * mdb)1620 int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1621 const struct switchdev_obj_port_mdb *mdb)
1622 {
1623 unsigned char addr[ETH_ALEN];
1624 struct ocelot_multicast *mc;
1625 struct ocelot_pgid *pgid;
1626 u16 vid = mdb->vid;
1627
1628 if (port == ocelot->npi)
1629 port = ocelot->num_phys_ports;
1630
1631 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1632 if (!mc)
1633 return -ENOENT;
1634
1635 ocelot_encode_ports_to_mdb(addr, mc);
1636 ocelot_mact_forget(ocelot, addr, vid);
1637
1638 ocelot_pgid_free(ocelot, mc->pgid);
1639 mc->ports &= ~BIT(port);
1640 if (!mc->ports) {
1641 list_del(&mc->list);
1642 devm_kfree(ocelot->dev, mc);
1643 return 0;
1644 }
1645
1646 /* We have a PGID with fewer ports now */
1647 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1648 if (IS_ERR(pgid))
1649 return PTR_ERR(pgid);
1650 mc->pgid = pgid;
1651
1652 ocelot_encode_ports_to_mdb(addr, mc);
1653
1654 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1655 mc->entry_type != ENTRYTYPE_MACv6)
1656 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1657 pgid->index);
1658
1659 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1660 mc->entry_type);
1661 }
1662 EXPORT_SYMBOL(ocelot_port_mdb_del);
1663
ocelot_port_bridge_join(struct ocelot * ocelot,int port,struct net_device * bridge)1664 void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1665 struct net_device *bridge)
1666 {
1667 struct ocelot_port *ocelot_port = ocelot->ports[port];
1668
1669 ocelot_port->bridge = bridge;
1670
1671 ocelot_apply_bridge_fwd_mask(ocelot);
1672 }
1673 EXPORT_SYMBOL(ocelot_port_bridge_join);
1674
ocelot_port_bridge_leave(struct ocelot * ocelot,int port,struct net_device * bridge)1675 void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1676 struct net_device *bridge)
1677 {
1678 struct ocelot_port *ocelot_port = ocelot->ports[port];
1679 struct ocelot_vlan pvid = {0}, native_vlan = {0};
1680
1681 ocelot_port->bridge = NULL;
1682
1683 ocelot_port_set_pvid(ocelot, port, pvid);
1684 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1685 ocelot_apply_bridge_fwd_mask(ocelot);
1686 }
1687 EXPORT_SYMBOL(ocelot_port_bridge_leave);
1688
ocelot_set_aggr_pgids(struct ocelot * ocelot)1689 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1690 {
1691 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1692 int i, port, lag;
1693
1694 /* Reset destination and aggregation PGIDS */
1695 for_each_unicast_dest_pgid(ocelot, port)
1696 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1697
1698 for_each_aggr_pgid(ocelot, i)
1699 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1700 ANA_PGID_PGID, i);
1701
1702 /* The visited ports bitmask holds the list of ports offloading any
1703 * bonding interface. Initially we mark all these ports as unvisited,
1704 * then every time we visit a port in this bitmask, we know that it is
1705 * the lowest numbered port, i.e. the one whose logical ID == physical
1706 * port ID == LAG ID. So we mark as visited all further ports in the
1707 * bitmask that are offloading the same bonding interface. This way,
1708 * we set up the aggregation PGIDs only once per bonding interface.
1709 */
1710 for (port = 0; port < ocelot->num_phys_ports; port++) {
1711 struct ocelot_port *ocelot_port = ocelot->ports[port];
1712
1713 if (!ocelot_port || !ocelot_port->bond)
1714 continue;
1715
1716 visited &= ~BIT(port);
1717 }
1718
1719 /* Now, set PGIDs for each active LAG */
1720 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1721 struct net_device *bond = ocelot->ports[lag]->bond;
1722 int num_active_ports = 0;
1723 unsigned long bond_mask;
1724 u8 aggr_idx[16];
1725
1726 if (!bond || (visited & BIT(lag)))
1727 continue;
1728
1729 bond_mask = ocelot_get_bond_mask(ocelot, bond);
1730
1731 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1732 struct ocelot_port *ocelot_port = ocelot->ports[port];
1733
1734 // Destination mask
1735 ocelot_write_rix(ocelot, bond_mask,
1736 ANA_PGID_PGID, port);
1737
1738 if (ocelot_port->lag_tx_active)
1739 aggr_idx[num_active_ports++] = port;
1740 }
1741
1742 for_each_aggr_pgid(ocelot, i) {
1743 u32 ac;
1744
1745 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1746 ac &= ~bond_mask;
1747 /* Don't do division by zero if there was no active
1748 * port. Just make all aggregation codes zero.
1749 */
1750 if (num_active_ports)
1751 ac |= BIT(aggr_idx[i % num_active_ports]);
1752 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1753 }
1754
1755 /* Mark all ports in the same LAG as visited to avoid applying
1756 * the same config again.
1757 */
1758 for (port = lag; port < ocelot->num_phys_ports; port++) {
1759 struct ocelot_port *ocelot_port = ocelot->ports[port];
1760
1761 if (!ocelot_port)
1762 continue;
1763
1764 if (ocelot_port->bond == bond)
1765 visited |= BIT(port);
1766 }
1767 }
1768 }
1769
1770 /* When offloading a bonding interface, the switch ports configured under the
1771 * same bond must have the same logical port ID, equal to the physical port ID
1772 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
1773 * bridged mode, each port has a logical port ID equal to its physical port ID.
1774 */
ocelot_setup_logical_port_ids(struct ocelot * ocelot)1775 static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1776 {
1777 int port;
1778
1779 for (port = 0; port < ocelot->num_phys_ports; port++) {
1780 struct ocelot_port *ocelot_port = ocelot->ports[port];
1781 struct net_device *bond;
1782
1783 if (!ocelot_port)
1784 continue;
1785
1786 bond = ocelot_port->bond;
1787 if (bond) {
1788 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond));
1789
1790 ocelot_rmw_gix(ocelot,
1791 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1792 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1793 ANA_PORT_PORT_CFG, port);
1794 } else {
1795 ocelot_rmw_gix(ocelot,
1796 ANA_PORT_PORT_CFG_PORTID_VAL(port),
1797 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1798 ANA_PORT_PORT_CFG, port);
1799 }
1800 }
1801 }
1802
ocelot_port_lag_join(struct ocelot * ocelot,int port,struct net_device * bond,struct netdev_lag_upper_info * info)1803 int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1804 struct net_device *bond,
1805 struct netdev_lag_upper_info *info)
1806 {
1807 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1808 return -EOPNOTSUPP;
1809
1810 ocelot->ports[port]->bond = bond;
1811
1812 ocelot_setup_logical_port_ids(ocelot);
1813 ocelot_apply_bridge_fwd_mask(ocelot);
1814 ocelot_set_aggr_pgids(ocelot);
1815
1816 return 0;
1817 }
1818 EXPORT_SYMBOL(ocelot_port_lag_join);
1819
ocelot_port_lag_leave(struct ocelot * ocelot,int port,struct net_device * bond)1820 void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1821 struct net_device *bond)
1822 {
1823 ocelot->ports[port]->bond = NULL;
1824
1825 ocelot_setup_logical_port_ids(ocelot);
1826 ocelot_apply_bridge_fwd_mask(ocelot);
1827 ocelot_set_aggr_pgids(ocelot);
1828 }
1829 EXPORT_SYMBOL(ocelot_port_lag_leave);
1830
ocelot_port_lag_change(struct ocelot * ocelot,int port,bool lag_tx_active)1831 void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
1832 {
1833 struct ocelot_port *ocelot_port = ocelot->ports[port];
1834
1835 ocelot_port->lag_tx_active = lag_tx_active;
1836
1837 /* Rebalance the LAGs */
1838 ocelot_set_aggr_pgids(ocelot);
1839 }
1840 EXPORT_SYMBOL(ocelot_port_lag_change);
1841
1842 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1843 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1844 * In the special case that it's the NPI port that we're configuring, the
1845 * length of the tag and optional prefix needs to be accounted for privately,
1846 * in order to be able to sustain communication at the requested @sdu.
1847 */
ocelot_port_set_maxlen(struct ocelot * ocelot,int port,size_t sdu)1848 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
1849 {
1850 struct ocelot_port *ocelot_port = ocelot->ports[port];
1851 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1852 int pause_start, pause_stop;
1853 int atop, atop_tot;
1854
1855 if (port == ocelot->npi) {
1856 maxlen += OCELOT_TAG_LEN;
1857
1858 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1859 maxlen += OCELOT_SHORT_PREFIX_LEN;
1860 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
1861 maxlen += OCELOT_LONG_PREFIX_LEN;
1862 }
1863
1864 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1865
1866 /* Set Pause watermark hysteresis */
1867 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1868 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1869 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1870 pause_start);
1871 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1872 pause_stop);
1873
1874 /* Tail dropping watermarks */
1875 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1876 OCELOT_BUFFER_CELL_SZ;
1877 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1878 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1879 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1880 }
1881 EXPORT_SYMBOL(ocelot_port_set_maxlen);
1882
ocelot_get_max_mtu(struct ocelot * ocelot,int port)1883 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1884 {
1885 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1886
1887 if (port == ocelot->npi) {
1888 max_mtu -= OCELOT_TAG_LEN;
1889
1890 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1891 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1892 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
1893 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1894 }
1895
1896 return max_mtu;
1897 }
1898 EXPORT_SYMBOL(ocelot_get_max_mtu);
1899
ocelot_port_set_learning(struct ocelot * ocelot,int port,bool enabled)1900 static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1901 bool enabled)
1902 {
1903 struct ocelot_port *ocelot_port = ocelot->ports[port];
1904 u32 val = 0;
1905
1906 if (enabled)
1907 val = ANA_PORT_PORT_CFG_LEARN_ENA;
1908
1909 ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1910 ANA_PORT_PORT_CFG, port);
1911
1912 ocelot_port->learn_ena = enabled;
1913 }
1914
ocelot_port_set_ucast_flood(struct ocelot * ocelot,int port,bool enabled)1915 static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1916 bool enabled)
1917 {
1918 u32 val = 0;
1919
1920 if (enabled)
1921 val = BIT(port);
1922
1923 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1924 }
1925
ocelot_port_set_mcast_flood(struct ocelot * ocelot,int port,bool enabled)1926 static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1927 bool enabled)
1928 {
1929 u32 val = 0;
1930
1931 if (enabled)
1932 val = BIT(port);
1933
1934 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1935 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
1936 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
1937 }
1938
ocelot_port_set_bcast_flood(struct ocelot * ocelot,int port,bool enabled)1939 static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1940 bool enabled)
1941 {
1942 u32 val = 0;
1943
1944 if (enabled)
1945 val = BIT(port);
1946
1947 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1948 }
1949
ocelot_port_pre_bridge_flags(struct ocelot * ocelot,int port,struct switchdev_brport_flags flags)1950 int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1951 struct switchdev_brport_flags flags)
1952 {
1953 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1954 BR_BCAST_FLOOD))
1955 return -EINVAL;
1956
1957 return 0;
1958 }
1959 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1960
ocelot_port_bridge_flags(struct ocelot * ocelot,int port,struct switchdev_brport_flags flags)1961 void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1962 struct switchdev_brport_flags flags)
1963 {
1964 if (flags.mask & BR_LEARNING)
1965 ocelot_port_set_learning(ocelot, port,
1966 !!(flags.val & BR_LEARNING));
1967
1968 if (flags.mask & BR_FLOOD)
1969 ocelot_port_set_ucast_flood(ocelot, port,
1970 !!(flags.val & BR_FLOOD));
1971
1972 if (flags.mask & BR_MCAST_FLOOD)
1973 ocelot_port_set_mcast_flood(ocelot, port,
1974 !!(flags.val & BR_MCAST_FLOOD));
1975
1976 if (flags.mask & BR_BCAST_FLOOD)
1977 ocelot_port_set_bcast_flood(ocelot, port,
1978 !!(flags.val & BR_BCAST_FLOOD));
1979 }
1980 EXPORT_SYMBOL(ocelot_port_bridge_flags);
1981
ocelot_init_port(struct ocelot * ocelot,int port)1982 void ocelot_init_port(struct ocelot *ocelot, int port)
1983 {
1984 struct ocelot_port *ocelot_port = ocelot->ports[port];
1985
1986 skb_queue_head_init(&ocelot_port->tx_skbs);
1987
1988 /* Basic L2 initialization */
1989
1990 /* Set MAC IFG Gaps
1991 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1992 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1993 */
1994 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1995 DEV_MAC_IFG_CFG);
1996
1997 /* Load seed (0) and set MAC HDX late collision */
1998 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1999 DEV_MAC_HDX_CFG_SEED_LOAD,
2000 DEV_MAC_HDX_CFG);
2001 mdelay(1);
2002 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2003 DEV_MAC_HDX_CFG);
2004
2005 /* Set Max Length and maximum tags allowed */
2006 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
2007 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2008 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2009 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
2010 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2011 DEV_MAC_TAGS_CFG);
2012
2013 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2014 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2015 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2016
2017 /* Enable transmission of pause frames */
2018 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2019
2020 /* Drop frames with multicast source address */
2021 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2022 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2023 ANA_PORT_DROP_CFG, port);
2024
2025 /* Set default VLAN and tag type to 8021Q. */
2026 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2027 REW_PORT_VLAN_CFG_PORT_TPID_M,
2028 REW_PORT_VLAN_CFG, port);
2029
2030 /* Disable source address learning for standalone mode */
2031 ocelot_port_set_learning(ocelot, port, false);
2032
2033 /* Set the port's initial logical port ID value, enable receiving
2034 * frames on it, and configure the MAC address learning type to
2035 * automatic.
2036 */
2037 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
2038 ANA_PORT_PORT_CFG_RECV_ENA |
2039 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2040 ANA_PORT_PORT_CFG, port);
2041
2042 /* Enable vcap lookups */
2043 ocelot_vcap_enable(ocelot, port);
2044 }
2045 EXPORT_SYMBOL(ocelot_init_port);
2046
2047 /* Configure and enable the CPU port module, which is a set of queues
2048 * accessible through register MMIO, frame DMA or Ethernet (in case
2049 * NPI mode is used).
2050 */
ocelot_cpu_port_init(struct ocelot * ocelot)2051 static void ocelot_cpu_port_init(struct ocelot *ocelot)
2052 {
2053 int cpu = ocelot->num_phys_ports;
2054
2055 /* The unicast destination PGID for the CPU port module is unused */
2056 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2057 /* Instead set up a multicast destination PGID for traffic copied to
2058 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2059 * addresses will be copied to the CPU via this PGID.
2060 */
2061 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2062 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2063 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2064 ANA_PORT_PORT_CFG, cpu);
2065
2066 /* Enable CPU port module */
2067 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
2068 /* CPU port Injection/Extraction configuration */
2069 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2070 OCELOT_TAG_PREFIX_NONE);
2071 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2072 OCELOT_TAG_PREFIX_NONE);
2073
2074 /* Configure the CPU port to be VLAN aware */
2075 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2076 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2077 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2078 ANA_PORT_VLAN_CFG, cpu);
2079 }
2080
ocelot_detect_features(struct ocelot * ocelot)2081 static void ocelot_detect_features(struct ocelot *ocelot)
2082 {
2083 int mmgt, eq_ctrl;
2084
2085 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2086 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2087 * 192 bytes as the documentation incorrectly says.
2088 */
2089 mmgt = ocelot_read(ocelot, SYS_MMGT);
2090 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2091
2092 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2093 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2094 }
2095
ocelot_init(struct ocelot * ocelot)2096 int ocelot_init(struct ocelot *ocelot)
2097 {
2098 char queue_name[32];
2099 int i, ret;
2100 u32 port;
2101
2102 if (ocelot->ops->reset) {
2103 ret = ocelot->ops->reset(ocelot);
2104 if (ret) {
2105 dev_err(ocelot->dev, "Switch reset failed\n");
2106 return ret;
2107 }
2108 }
2109
2110 ocelot->stats = devm_kcalloc(ocelot->dev,
2111 ocelot->num_phys_ports * ocelot->num_stats,
2112 sizeof(u64), GFP_KERNEL);
2113 if (!ocelot->stats)
2114 return -ENOMEM;
2115
2116 mutex_init(&ocelot->stats_lock);
2117 mutex_init(&ocelot->ptp_lock);
2118 spin_lock_init(&ocelot->ptp_clock_lock);
2119 spin_lock_init(&ocelot->ts_id_lock);
2120 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2121 dev_name(ocelot->dev));
2122 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2123 if (!ocelot->stats_queue)
2124 return -ENOMEM;
2125
2126 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2127 if (!ocelot->owq) {
2128 destroy_workqueue(ocelot->stats_queue);
2129 return -ENOMEM;
2130 }
2131
2132 INIT_LIST_HEAD(&ocelot->multicast);
2133 INIT_LIST_HEAD(&ocelot->pgids);
2134 ocelot_detect_features(ocelot);
2135 ocelot_mact_init(ocelot);
2136 ocelot_vlan_init(ocelot);
2137 ocelot_vcap_init(ocelot);
2138 ocelot_cpu_port_init(ocelot);
2139
2140 for (port = 0; port < ocelot->num_phys_ports; port++) {
2141 /* Clear all counters (5 groups) */
2142 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2143 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2144 SYS_STAT_CFG);
2145 }
2146
2147 /* Only use S-Tag */
2148 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2149
2150 /* Aggregation mode */
2151 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2152 ANA_AGGR_CFG_AC_DMAC_ENA |
2153 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2154 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2155 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2156 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2157 ANA_AGGR_CFG);
2158
2159 /* Set MAC age time to default value. The entry is aged after
2160 * 2*AGE_PERIOD
2161 */
2162 ocelot_write(ocelot,
2163 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2164 ANA_AUTOAGE);
2165
2166 /* Disable learning for frames discarded by VLAN ingress filtering */
2167 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2168
2169 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2170 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2171 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2172
2173 /* Setup flooding PGIDs */
2174 for (i = 0; i < ocelot->num_flooding_pgids; i++)
2175 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2176 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2177 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2178 ANA_FLOODING, i);
2179 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2180 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2181 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2182 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2183 ANA_FLOODING_IPMC);
2184
2185 for (port = 0; port < ocelot->num_phys_ports; port++) {
2186 /* Transmit the frame to the local port. */
2187 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2188 /* Do not forward BPDU frames to the front ports. */
2189 ocelot_write_gix(ocelot,
2190 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2191 ANA_PORT_CPU_FWD_BPDU_CFG,
2192 port);
2193 /* Ensure bridging is disabled */
2194 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2195 }
2196
2197 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2198 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2199
2200 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2201 }
2202
2203 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2204
2205 /* Allow broadcast and unknown L2 multicast to the CPU. */
2206 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2207 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2208 ANA_PGID_PGID, PGID_MC);
2209 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2210 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2211 ANA_PGID_PGID, PGID_MCIPV4);
2212 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2213 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2214 ANA_PGID_PGID, PGID_MCIPV6);
2215 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2216 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2217 ANA_PGID_PGID, PGID_BC);
2218
2219 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2220 * registers endianness.
2221 */
2222 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2223 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2224 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2225 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2226 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2227 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2228 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2229 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2230 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2231 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2232 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2233 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2234 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2235 for (i = 0; i < 16; i++)
2236 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2237 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2238 ANA_CPUQ_8021_CFG, i);
2239
2240 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2241 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2242 OCELOT_STATS_CHECK_DELAY);
2243
2244 return 0;
2245 }
2246 EXPORT_SYMBOL(ocelot_init);
2247
ocelot_deinit(struct ocelot * ocelot)2248 void ocelot_deinit(struct ocelot *ocelot)
2249 {
2250 cancel_delayed_work(&ocelot->stats_work);
2251 destroy_workqueue(ocelot->stats_queue);
2252 destroy_workqueue(ocelot->owq);
2253 mutex_destroy(&ocelot->stats_lock);
2254 }
2255 EXPORT_SYMBOL(ocelot_deinit);
2256
ocelot_deinit_port(struct ocelot * ocelot,int port)2257 void ocelot_deinit_port(struct ocelot *ocelot, int port)
2258 {
2259 struct ocelot_port *ocelot_port = ocelot->ports[port];
2260
2261 skb_queue_purge(&ocelot_port->tx_skbs);
2262 }
2263 EXPORT_SYMBOL(ocelot_deinit_port);
2264
2265 MODULE_LICENSE("Dual MIT/GPL");
2266