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