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