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