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