net: dsa: felix: drop the ptp_type argument from felix_check_xtr_pkt()
[linux-block.git] / drivers / net / dsa / ocelot / felix.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019-2021 NXP
3  *
4  * This is an umbrella module for all network switches that are
5  * register-compatible with Ocelot and that perform I/O to their host CPU
6  * through an NPI (Node Processor Interface) Ethernet port.
7  */
8 #include <uapi/linux/if_bridge.h>
9 #include <soc/mscc/ocelot_vcap.h>
10 #include <soc/mscc/ocelot_qsys.h>
11 #include <soc/mscc/ocelot_sys.h>
12 #include <soc/mscc/ocelot_dev.h>
13 #include <soc/mscc/ocelot_ana.h>
14 #include <soc/mscc/ocelot_ptp.h>
15 #include <soc/mscc/ocelot.h>
16 #include <linux/dsa/8021q.h>
17 #include <linux/dsa/ocelot.h>
18 #include <linux/platform_device.h>
19 #include <linux/ptp_classify.h>
20 #include <linux/module.h>
21 #include <linux/of_net.h>
22 #include <linux/pci.h>
23 #include <linux/of.h>
24 #include <net/pkt_sched.h>
25 #include <net/dsa.h>
26 #include "felix.h"
27
28 /* Translate the DSA database API into the ocelot switch library API,
29  * which uses VID 0 for all ports that aren't part of a bridge,
30  * and expects the bridge_dev to be NULL in that case.
31  */
32 static struct net_device *felix_classify_db(struct dsa_db db)
33 {
34         switch (db.type) {
35         case DSA_DB_PORT:
36         case DSA_DB_LAG:
37                 return NULL;
38         case DSA_DB_BRIDGE:
39                 return db.bridge.dev;
40         default:
41                 return ERR_PTR(-EOPNOTSUPP);
42         }
43 }
44
45 /* We are called before felix_npi_port_init(), so ocelot->npi is -1. */
46 static int felix_migrate_fdbs_to_npi_port(struct dsa_switch *ds, int port,
47                                           const unsigned char *addr, u16 vid,
48                                           struct dsa_db db)
49 {
50         struct net_device *bridge_dev = felix_classify_db(db);
51         struct ocelot *ocelot = ds->priv;
52         int cpu = ocelot->num_phys_ports;
53         int err;
54
55         err = ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
56         if (err)
57                 return err;
58
59         return ocelot_fdb_add(ocelot, cpu, addr, vid, bridge_dev);
60 }
61
62 static int felix_migrate_mdbs_to_npi_port(struct dsa_switch *ds, int port,
63                                           const unsigned char *addr, u16 vid,
64                                           struct dsa_db db)
65 {
66         struct net_device *bridge_dev = felix_classify_db(db);
67         struct switchdev_obj_port_mdb mdb;
68         struct ocelot *ocelot = ds->priv;
69         int cpu = ocelot->num_phys_ports;
70         int err;
71
72         memset(&mdb, 0, sizeof(mdb));
73         ether_addr_copy(mdb.addr, addr);
74         mdb.vid = vid;
75
76         err = ocelot_port_mdb_del(ocelot, port, &mdb, bridge_dev);
77         if (err)
78                 return err;
79
80         return ocelot_port_mdb_add(ocelot, cpu, &mdb, bridge_dev);
81 }
82
83 static void felix_migrate_pgid_bit(struct dsa_switch *ds, int from, int to,
84                                    int pgid)
85 {
86         struct ocelot *ocelot = ds->priv;
87         bool on;
88         u32 val;
89
90         val = ocelot_read_rix(ocelot, ANA_PGID_PGID, pgid);
91         on = !!(val & BIT(from));
92         val &= ~BIT(from);
93         if (on)
94                 val |= BIT(to);
95         else
96                 val &= ~BIT(to);
97
98         ocelot_write_rix(ocelot, val, ANA_PGID_PGID, pgid);
99 }
100
101 static void felix_migrate_flood_to_npi_port(struct dsa_switch *ds, int port)
102 {
103         struct ocelot *ocelot = ds->priv;
104
105         felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_UC);
106         felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_MC);
107         felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_BC);
108 }
109
110 static void
111 felix_migrate_flood_to_tag_8021q_port(struct dsa_switch *ds, int port)
112 {
113         struct ocelot *ocelot = ds->priv;
114
115         felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_UC);
116         felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_MC);
117         felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_BC);
118 }
119
120 /* ocelot->npi was already set to -1 by felix_npi_port_deinit, so
121  * ocelot_fdb_add() will not redirect FDB entries towards the
122  * CPU port module here, which is what we want.
123  */
124 static int
125 felix_migrate_fdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
126                                      const unsigned char *addr, u16 vid,
127                                      struct dsa_db db)
128 {
129         struct net_device *bridge_dev = felix_classify_db(db);
130         struct ocelot *ocelot = ds->priv;
131         int cpu = ocelot->num_phys_ports;
132         int err;
133
134         err = ocelot_fdb_del(ocelot, cpu, addr, vid, bridge_dev);
135         if (err)
136                 return err;
137
138         return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
139 }
140
141 static int
142 felix_migrate_mdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
143                                      const unsigned char *addr, u16 vid,
144                                      struct dsa_db db)
145 {
146         struct net_device *bridge_dev = felix_classify_db(db);
147         struct switchdev_obj_port_mdb mdb;
148         struct ocelot *ocelot = ds->priv;
149         int cpu = ocelot->num_phys_ports;
150         int err;
151
152         memset(&mdb, 0, sizeof(mdb));
153         ether_addr_copy(mdb.addr, addr);
154         mdb.vid = vid;
155
156         err = ocelot_port_mdb_del(ocelot, cpu, &mdb, bridge_dev);
157         if (err)
158                 return err;
159
160         return ocelot_port_mdb_add(ocelot, port, &mdb, bridge_dev);
161 }
162
163 /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
164  * the tagger can perform RX source port identification.
165  */
166 static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
167 {
168         struct ocelot_vcap_filter *outer_tagging_rule;
169         struct ocelot *ocelot = &felix->ocelot;
170         struct dsa_switch *ds = felix->ds;
171         int key_length, upstream, err;
172
173         key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
174         upstream = dsa_upstream_port(ds, port);
175
176         outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
177                                      GFP_KERNEL);
178         if (!outer_tagging_rule)
179                 return -ENOMEM;
180
181         outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
182         outer_tagging_rule->prio = 1;
183         outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
184         outer_tagging_rule->id.tc_offload = false;
185         outer_tagging_rule->block_id = VCAP_ES0;
186         outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
187         outer_tagging_rule->lookup = 0;
188         outer_tagging_rule->ingress_port.value = port;
189         outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
190         outer_tagging_rule->egress_port.value = upstream;
191         outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
192         outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
193         outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
194         outer_tagging_rule->action.tag_a_vid_sel = 1;
195         outer_tagging_rule->action.vid_a_val = vid;
196
197         err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
198         if (err)
199                 kfree(outer_tagging_rule);
200
201         return err;
202 }
203
204 static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
205 {
206         struct ocelot_vcap_filter *outer_tagging_rule;
207         struct ocelot_vcap_block *block_vcap_es0;
208         struct ocelot *ocelot = &felix->ocelot;
209
210         block_vcap_es0 = &ocelot->block[VCAP_ES0];
211
212         outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
213                                                                  port, false);
214         if (!outer_tagging_rule)
215                 return -ENOENT;
216
217         return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
218 }
219
220 /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
221  * rules for steering those tagged packets towards the correct destination port
222  */
223 static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
224 {
225         struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
226         struct ocelot *ocelot = &felix->ocelot;
227         struct dsa_switch *ds = felix->ds;
228         int upstream, err;
229
230         untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
231         if (!untagging_rule)
232                 return -ENOMEM;
233
234         redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
235         if (!redirect_rule) {
236                 kfree(untagging_rule);
237                 return -ENOMEM;
238         }
239
240         upstream = dsa_upstream_port(ds, port);
241
242         untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
243         untagging_rule->ingress_port_mask = BIT(upstream);
244         untagging_rule->vlan.vid.value = vid;
245         untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
246         untagging_rule->prio = 1;
247         untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
248         untagging_rule->id.tc_offload = false;
249         untagging_rule->block_id = VCAP_IS1;
250         untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
251         untagging_rule->lookup = 0;
252         untagging_rule->action.vlan_pop_cnt_ena = true;
253         untagging_rule->action.vlan_pop_cnt = 1;
254         untagging_rule->action.pag_override_mask = 0xff;
255         untagging_rule->action.pag_val = port;
256
257         err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
258         if (err) {
259                 kfree(untagging_rule);
260                 kfree(redirect_rule);
261                 return err;
262         }
263
264         redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
265         redirect_rule->ingress_port_mask = BIT(upstream);
266         redirect_rule->pag = port;
267         redirect_rule->prio = 1;
268         redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
269         redirect_rule->id.tc_offload = false;
270         redirect_rule->block_id = VCAP_IS2;
271         redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
272         redirect_rule->lookup = 0;
273         redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
274         redirect_rule->action.port_mask = BIT(port);
275
276         err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
277         if (err) {
278                 ocelot_vcap_filter_del(ocelot, untagging_rule);
279                 kfree(redirect_rule);
280                 return err;
281         }
282
283         return 0;
284 }
285
286 static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
287 {
288         struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
289         struct ocelot_vcap_block *block_vcap_is1;
290         struct ocelot_vcap_block *block_vcap_is2;
291         struct ocelot *ocelot = &felix->ocelot;
292         int err;
293
294         block_vcap_is1 = &ocelot->block[VCAP_IS1];
295         block_vcap_is2 = &ocelot->block[VCAP_IS2];
296
297         untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
298                                                              port, false);
299         if (!untagging_rule)
300                 return -ENOENT;
301
302         err = ocelot_vcap_filter_del(ocelot, untagging_rule);
303         if (err)
304                 return err;
305
306         redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
307                                                             port, false);
308         if (!redirect_rule)
309                 return -ENOENT;
310
311         return ocelot_vcap_filter_del(ocelot, redirect_rule);
312 }
313
314 static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
315                                     u16 flags)
316 {
317         struct ocelot *ocelot = ds->priv;
318         int err;
319
320         /* tag_8021q.c assumes we are implementing this via port VLAN
321          * membership, which we aren't. So we don't need to add any VCAP filter
322          * for the CPU port.
323          */
324         if (!dsa_is_user_port(ds, port))
325                 return 0;
326
327         err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
328         if (err)
329                 return err;
330
331         err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
332         if (err) {
333                 felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
334                 return err;
335         }
336
337         return 0;
338 }
339
340 static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
341 {
342         struct ocelot *ocelot = ds->priv;
343         int err;
344
345         if (!dsa_is_user_port(ds, port))
346                 return 0;
347
348         err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
349         if (err)
350                 return err;
351
352         err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
353         if (err) {
354                 felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
355                 return err;
356         }
357
358         return 0;
359 }
360
361 /* Alternatively to using the NPI functionality, that same hardware MAC
362  * connected internally to the enetc or fman DSA master can be configured to
363  * use the software-defined tag_8021q frame format. As far as the hardware is
364  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
365  * module are now disconnected from it, but can still be accessed through
366  * register-based MMIO.
367  */
368 static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
369 {
370         mutex_lock(&ocelot->fwd_domain_lock);
371
372         ocelot_port_set_dsa_8021q_cpu(ocelot, port);
373
374         /* Overwrite PGID_CPU with the non-tagging port */
375         ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
376
377         ocelot_apply_bridge_fwd_mask(ocelot, true);
378
379         mutex_unlock(&ocelot->fwd_domain_lock);
380 }
381
382 static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
383 {
384         mutex_lock(&ocelot->fwd_domain_lock);
385
386         ocelot->ports[port]->is_dsa_8021q_cpu = false;
387         ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
388
389         /* Restore PGID_CPU */
390         ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
391                          PGID_CPU);
392
393         ocelot_apply_bridge_fwd_mask(ocelot, true);
394
395         mutex_unlock(&ocelot->fwd_domain_lock);
396 }
397
398 /* On switches with no extraction IRQ wired, trapped packets need to be
399  * replicated over Ethernet as well, otherwise we'd get no notification of
400  * their arrival when using the ocelot-8021q tagging protocol.
401  */
402 static int felix_update_trapping_destinations(struct dsa_switch *ds,
403                                               bool using_tag_8021q)
404 {
405         struct ocelot *ocelot = ds->priv;
406         struct felix *felix = ocelot_to_felix(ocelot);
407         struct ocelot_vcap_filter *trap;
408         enum ocelot_mask_mode mask_mode;
409         unsigned long port_mask;
410         struct dsa_port *dp;
411         bool cpu_copy_ena;
412         int cpu = -1, err;
413
414         if (!felix->info->quirk_no_xtr_irq)
415                 return 0;
416
417         /* Figure out the current CPU port */
418         dsa_switch_for_each_cpu_port(dp, ds) {
419                 cpu = dp->index;
420                 break;
421         }
422
423         /* We are sure that "cpu" was found, otherwise
424          * dsa_tree_setup_default_cpu() would have failed earlier.
425          */
426
427         /* Make sure all traps are set up for that destination */
428         list_for_each_entry(trap, &ocelot->traps, trap_list) {
429                 /* Figure out the current trapping destination */
430                 if (using_tag_8021q) {
431                         /* Redirect to the tag_8021q CPU port. If timestamps
432                          * are necessary, also copy trapped packets to the CPU
433                          * port module.
434                          */
435                         mask_mode = OCELOT_MASK_MODE_REDIRECT;
436                         port_mask = BIT(cpu);
437                         cpu_copy_ena = !!trap->take_ts;
438                 } else {
439                         /* Trap packets only to the CPU port module, which is
440                          * redirected to the NPI port (the DSA CPU port)
441                          */
442                         mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
443                         port_mask = 0;
444                         cpu_copy_ena = true;
445                 }
446
447                 if (trap->action.mask_mode == mask_mode &&
448                     trap->action.port_mask == port_mask &&
449                     trap->action.cpu_copy_ena == cpu_copy_ena)
450                         continue;
451
452                 trap->action.mask_mode = mask_mode;
453                 trap->action.port_mask = port_mask;
454                 trap->action.cpu_copy_ena = cpu_copy_ena;
455
456                 err = ocelot_vcap_filter_replace(ocelot, trap);
457                 if (err)
458                         return err;
459         }
460
461         return 0;
462 }
463
464 static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu, bool change)
465 {
466         struct ocelot *ocelot = ds->priv;
467         struct dsa_port *dp;
468         int err;
469
470         felix_8021q_cpu_port_init(ocelot, cpu);
471
472         dsa_switch_for_each_available_port(dp, ds) {
473                 /* This overwrites ocelot_init():
474                  * Do not forward BPDU frames to the CPU port module,
475                  * for 2 reasons:
476                  * - When these packets are injected from the tag_8021q
477                  *   CPU port, we want them to go out, not loop back
478                  *   into the system.
479                  * - STP traffic ingressing on a user port should go to
480                  *   the tag_8021q CPU port, not to the hardware CPU
481                  *   port module.
482                  */
483                 ocelot_write_gix(ocelot,
484                                  ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
485                                  ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
486         }
487
488         err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
489         if (err)
490                 return err;
491
492         if (change) {
493                 err = dsa_port_walk_fdbs(ds, cpu,
494                                          felix_migrate_fdbs_to_tag_8021q_port);
495                 if (err)
496                         goto out_tag_8021q_unregister;
497
498                 err = dsa_port_walk_mdbs(ds, cpu,
499                                          felix_migrate_mdbs_to_tag_8021q_port);
500                 if (err)
501                         goto out_migrate_fdbs;
502
503                 felix_migrate_flood_to_tag_8021q_port(ds, cpu);
504         }
505
506         err = felix_update_trapping_destinations(ds, true);
507         if (err)
508                 goto out_migrate_flood;
509
510         /* The ownership of the CPU port module's queues might have just been
511          * transferred to the tag_8021q tagger from the NPI-based tagger.
512          * So there might still be all sorts of crap in the queues. On the
513          * other hand, the MMIO-based matching of PTP frames is very brittle,
514          * so we need to be careful that there are no extra frames to be
515          * dequeued over MMIO, since we would never know to discard them.
516          */
517         ocelot_drain_cpu_queue(ocelot, 0);
518
519         return 0;
520
521 out_migrate_flood:
522         if (change)
523                 felix_migrate_flood_to_npi_port(ds, cpu);
524         if (change)
525                 dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
526 out_migrate_fdbs:
527         if (change)
528                 dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
529 out_tag_8021q_unregister:
530         dsa_tag_8021q_unregister(ds);
531         return err;
532 }
533
534 static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
535 {
536         struct ocelot *ocelot = ds->priv;
537         struct dsa_port *dp;
538         int err;
539
540         err = felix_update_trapping_destinations(ds, false);
541         if (err)
542                 dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
543                         err);
544
545         dsa_tag_8021q_unregister(ds);
546
547         dsa_switch_for_each_available_port(dp, ds) {
548                 /* Restore the logic from ocelot_init:
549                  * do not forward BPDU frames to the front ports.
550                  */
551                 ocelot_write_gix(ocelot,
552                                  ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
553                                  ANA_PORT_CPU_FWD_BPDU_CFG,
554                                  dp->index);
555         }
556
557         felix_8021q_cpu_port_deinit(ocelot, cpu);
558 }
559
560 /* The CPU port module is connected to the Node Processor Interface (NPI). This
561  * is the mode through which frames can be injected from and extracted to an
562  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
563  * running Linux, and this forms a DSA setup together with the enetc or fman
564  * DSA master.
565  */
566 static void felix_npi_port_init(struct ocelot *ocelot, int port)
567 {
568         ocelot->npi = port;
569
570         ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
571                      QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
572                      QSYS_EXT_CPU_CFG);
573
574         /* NPI port Injection/Extraction configuration */
575         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
576                             ocelot->npi_xtr_prefix);
577         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
578                             ocelot->npi_inj_prefix);
579
580         /* Disable transmission of pause frames */
581         ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
582 }
583
584 static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
585 {
586         /* Restore hardware defaults */
587         int unused_port = ocelot->num_phys_ports + 2;
588
589         ocelot->npi = -1;
590
591         ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
592                      QSYS_EXT_CPU_CFG);
593
594         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
595                             OCELOT_TAG_PREFIX_DISABLED);
596         ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
597                             OCELOT_TAG_PREFIX_DISABLED);
598
599         /* Enable transmission of pause frames */
600         ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
601 }
602
603 static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu, bool change)
604 {
605         struct ocelot *ocelot = ds->priv;
606         int err;
607
608         if (change) {
609                 err = dsa_port_walk_fdbs(ds, cpu,
610                                          felix_migrate_fdbs_to_npi_port);
611                 if (err)
612                         return err;
613
614                 err = dsa_port_walk_mdbs(ds, cpu,
615                                          felix_migrate_mdbs_to_npi_port);
616                 if (err)
617                         goto out_migrate_fdbs;
618
619                 felix_migrate_flood_to_npi_port(ds, cpu);
620         }
621
622         felix_npi_port_init(ocelot, cpu);
623
624         return 0;
625
626 out_migrate_fdbs:
627         if (change)
628                 dsa_port_walk_fdbs(ds, cpu,
629                                    felix_migrate_fdbs_to_tag_8021q_port);
630
631         return err;
632 }
633
634 static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
635 {
636         struct ocelot *ocelot = ds->priv;
637
638         felix_npi_port_deinit(ocelot, cpu);
639 }
640
641 static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
642                                   enum dsa_tag_protocol proto, bool change)
643 {
644         int err;
645
646         switch (proto) {
647         case DSA_TAG_PROTO_SEVILLE:
648         case DSA_TAG_PROTO_OCELOT:
649                 err = felix_setup_tag_npi(ds, cpu, change);
650                 break;
651         case DSA_TAG_PROTO_OCELOT_8021Q:
652                 err = felix_setup_tag_8021q(ds, cpu, change);
653                 break;
654         default:
655                 err = -EPROTONOSUPPORT;
656         }
657
658         return err;
659 }
660
661 static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
662                                    enum dsa_tag_protocol proto)
663 {
664         switch (proto) {
665         case DSA_TAG_PROTO_SEVILLE:
666         case DSA_TAG_PROTO_OCELOT:
667                 felix_teardown_tag_npi(ds, cpu);
668                 break;
669         case DSA_TAG_PROTO_OCELOT_8021Q:
670                 felix_teardown_tag_8021q(ds, cpu);
671                 break;
672         default:
673                 break;
674         }
675 }
676
677 /* This always leaves the switch in a consistent state, because although the
678  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
679  * or the restoration is guaranteed to work.
680  */
681 static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
682                                      enum dsa_tag_protocol proto)
683 {
684         struct ocelot *ocelot = ds->priv;
685         struct felix *felix = ocelot_to_felix(ocelot);
686         enum dsa_tag_protocol old_proto = felix->tag_proto;
687         int err;
688
689         if (proto != DSA_TAG_PROTO_SEVILLE &&
690             proto != DSA_TAG_PROTO_OCELOT &&
691             proto != DSA_TAG_PROTO_OCELOT_8021Q)
692                 return -EPROTONOSUPPORT;
693
694         felix_del_tag_protocol(ds, cpu, old_proto);
695
696         err = felix_set_tag_protocol(ds, cpu, proto, true);
697         if (err) {
698                 felix_set_tag_protocol(ds, cpu, old_proto, true);
699                 return err;
700         }
701
702         felix->tag_proto = proto;
703
704         return 0;
705 }
706
707 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
708                                                     int port,
709                                                     enum dsa_tag_protocol mp)
710 {
711         struct ocelot *ocelot = ds->priv;
712         struct felix *felix = ocelot_to_felix(ocelot);
713
714         return felix->tag_proto;
715 }
716
717 static int felix_set_ageing_time(struct dsa_switch *ds,
718                                  unsigned int ageing_time)
719 {
720         struct ocelot *ocelot = ds->priv;
721
722         ocelot_set_ageing_time(ocelot, ageing_time);
723
724         return 0;
725 }
726
727 static void felix_port_fast_age(struct dsa_switch *ds, int port)
728 {
729         struct ocelot *ocelot = ds->priv;
730         int err;
731
732         err = ocelot_mact_flush(ocelot, port);
733         if (err)
734                 dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
735                         port, ERR_PTR(err));
736 }
737
738 static int felix_fdb_dump(struct dsa_switch *ds, int port,
739                           dsa_fdb_dump_cb_t *cb, void *data)
740 {
741         struct ocelot *ocelot = ds->priv;
742
743         return ocelot_fdb_dump(ocelot, port, cb, data);
744 }
745
746 static int felix_fdb_add(struct dsa_switch *ds, int port,
747                          const unsigned char *addr, u16 vid,
748                          struct dsa_db db)
749 {
750         struct net_device *bridge_dev = felix_classify_db(db);
751         struct ocelot *ocelot = ds->priv;
752
753         if (IS_ERR(bridge_dev))
754                 return PTR_ERR(bridge_dev);
755
756         return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
757 }
758
759 static int felix_fdb_del(struct dsa_switch *ds, int port,
760                          const unsigned char *addr, u16 vid,
761                          struct dsa_db db)
762 {
763         struct net_device *bridge_dev = felix_classify_db(db);
764         struct ocelot *ocelot = ds->priv;
765
766         if (IS_ERR(bridge_dev))
767                 return PTR_ERR(bridge_dev);
768
769         return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
770 }
771
772 static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
773                              const unsigned char *addr, u16 vid,
774                              struct dsa_db db)
775 {
776         struct net_device *bridge_dev = felix_classify_db(db);
777         struct ocelot *ocelot = ds->priv;
778
779         if (IS_ERR(bridge_dev))
780                 return PTR_ERR(bridge_dev);
781
782         return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
783 }
784
785 static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
786                              const unsigned char *addr, u16 vid,
787                              struct dsa_db db)
788 {
789         struct net_device *bridge_dev = felix_classify_db(db);
790         struct ocelot *ocelot = ds->priv;
791
792         if (IS_ERR(bridge_dev))
793                 return PTR_ERR(bridge_dev);
794
795         return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
796 }
797
798 static int felix_mdb_add(struct dsa_switch *ds, int port,
799                          const struct switchdev_obj_port_mdb *mdb,
800                          struct dsa_db db)
801 {
802         struct net_device *bridge_dev = felix_classify_db(db);
803         struct ocelot *ocelot = ds->priv;
804
805         if (IS_ERR(bridge_dev))
806                 return PTR_ERR(bridge_dev);
807
808         return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
809 }
810
811 static int felix_mdb_del(struct dsa_switch *ds, int port,
812                          const struct switchdev_obj_port_mdb *mdb,
813                          struct dsa_db db)
814 {
815         struct net_device *bridge_dev = felix_classify_db(db);
816         struct ocelot *ocelot = ds->priv;
817
818         if (IS_ERR(bridge_dev))
819                 return PTR_ERR(bridge_dev);
820
821         return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
822 }
823
824 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
825                                        u8 state)
826 {
827         struct ocelot *ocelot = ds->priv;
828
829         return ocelot_bridge_stp_state_set(ocelot, port, state);
830 }
831
832 static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
833                                   struct switchdev_brport_flags val,
834                                   struct netlink_ext_ack *extack)
835 {
836         struct ocelot *ocelot = ds->priv;
837
838         return ocelot_port_pre_bridge_flags(ocelot, port, val);
839 }
840
841 static int felix_bridge_flags(struct dsa_switch *ds, int port,
842                               struct switchdev_brport_flags val,
843                               struct netlink_ext_ack *extack)
844 {
845         struct ocelot *ocelot = ds->priv;
846
847         ocelot_port_bridge_flags(ocelot, port, val);
848
849         return 0;
850 }
851
852 static int felix_bridge_join(struct dsa_switch *ds, int port,
853                              struct dsa_bridge bridge, bool *tx_fwd_offload,
854                              struct netlink_ext_ack *extack)
855 {
856         struct ocelot *ocelot = ds->priv;
857
858         return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
859                                        extack);
860 }
861
862 static void felix_bridge_leave(struct dsa_switch *ds, int port,
863                                struct dsa_bridge bridge)
864 {
865         struct ocelot *ocelot = ds->priv;
866
867         ocelot_port_bridge_leave(ocelot, port, bridge.dev);
868 }
869
870 static int felix_lag_join(struct dsa_switch *ds, int port,
871                           struct dsa_lag lag,
872                           struct netdev_lag_upper_info *info)
873 {
874         struct ocelot *ocelot = ds->priv;
875
876         return ocelot_port_lag_join(ocelot, port, lag.dev, info);
877 }
878
879 static int felix_lag_leave(struct dsa_switch *ds, int port,
880                            struct dsa_lag lag)
881 {
882         struct ocelot *ocelot = ds->priv;
883
884         ocelot_port_lag_leave(ocelot, port, lag.dev);
885
886         return 0;
887 }
888
889 static int felix_lag_change(struct dsa_switch *ds, int port)
890 {
891         struct dsa_port *dp = dsa_to_port(ds, port);
892         struct ocelot *ocelot = ds->priv;
893
894         ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
895
896         return 0;
897 }
898
899 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
900                               const struct switchdev_obj_port_vlan *vlan,
901                               struct netlink_ext_ack *extack)
902 {
903         struct ocelot *ocelot = ds->priv;
904         u16 flags = vlan->flags;
905
906         /* Ocelot switches copy frames as-is to the CPU, so the flags:
907          * egress-untagged or not, pvid or not, make no difference. This
908          * behavior is already better than what DSA just tries to approximate
909          * when it installs the VLAN with the same flags on the CPU port.
910          * Just accept any configuration, and don't let ocelot deny installing
911          * multiple native VLANs on the NPI port, because the switch doesn't
912          * look at the port tag settings towards the NPI interface anyway.
913          */
914         if (port == ocelot->npi)
915                 return 0;
916
917         return ocelot_vlan_prepare(ocelot, port, vlan->vid,
918                                    flags & BRIDGE_VLAN_INFO_PVID,
919                                    flags & BRIDGE_VLAN_INFO_UNTAGGED,
920                                    extack);
921 }
922
923 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
924                                 struct netlink_ext_ack *extack)
925 {
926         struct ocelot *ocelot = ds->priv;
927
928         return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
929 }
930
931 static int felix_vlan_add(struct dsa_switch *ds, int port,
932                           const struct switchdev_obj_port_vlan *vlan,
933                           struct netlink_ext_ack *extack)
934 {
935         struct ocelot *ocelot = ds->priv;
936         u16 flags = vlan->flags;
937         int err;
938
939         err = felix_vlan_prepare(ds, port, vlan, extack);
940         if (err)
941                 return err;
942
943         return ocelot_vlan_add(ocelot, port, vlan->vid,
944                                flags & BRIDGE_VLAN_INFO_PVID,
945                                flags & BRIDGE_VLAN_INFO_UNTAGGED);
946 }
947
948 static int felix_vlan_del(struct dsa_switch *ds, int port,
949                           const struct switchdev_obj_port_vlan *vlan)
950 {
951         struct ocelot *ocelot = ds->priv;
952
953         return ocelot_vlan_del(ocelot, port, vlan->vid);
954 }
955
956 static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
957                                    struct phylink_config *config)
958 {
959         struct ocelot *ocelot = ds->priv;
960
961         /* This driver does not make use of the speed, duplex, pause or the
962          * advertisement in its mac_config, so it is safe to mark this driver
963          * as non-legacy.
964          */
965         config->legacy_pre_march2020 = false;
966
967         __set_bit(ocelot->ports[port]->phy_mode,
968                   config->supported_interfaces);
969 }
970
971 static void felix_phylink_validate(struct dsa_switch *ds, int port,
972                                    unsigned long *supported,
973                                    struct phylink_link_state *state)
974 {
975         struct ocelot *ocelot = ds->priv;
976         struct felix *felix = ocelot_to_felix(ocelot);
977
978         if (felix->info->phylink_validate)
979                 felix->info->phylink_validate(ocelot, port, supported, state);
980 }
981
982 static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
983                                                         int port,
984                                                         phy_interface_t iface)
985 {
986         struct ocelot *ocelot = ds->priv;
987         struct felix *felix = ocelot_to_felix(ocelot);
988         struct phylink_pcs *pcs = NULL;
989
990         if (felix->pcs && felix->pcs[port])
991                 pcs = felix->pcs[port];
992
993         return pcs;
994 }
995
996 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
997                                         unsigned int link_an_mode,
998                                         phy_interface_t interface)
999 {
1000         struct ocelot *ocelot = ds->priv;
1001
1002         ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1003                                      FELIX_MAC_QUIRKS);
1004 }
1005
1006 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1007                                       unsigned int link_an_mode,
1008                                       phy_interface_t interface,
1009                                       struct phy_device *phydev,
1010                                       int speed, int duplex,
1011                                       bool tx_pause, bool rx_pause)
1012 {
1013         struct ocelot *ocelot = ds->priv;
1014         struct felix *felix = ocelot_to_felix(ocelot);
1015
1016         ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1017                                    interface, speed, duplex, tx_pause, rx_pause,
1018                                    FELIX_MAC_QUIRKS);
1019
1020         if (felix->info->port_sched_speed_set)
1021                 felix->info->port_sched_speed_set(ocelot, port, speed);
1022 }
1023
1024 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1025 {
1026         int i;
1027
1028         ocelot_rmw_gix(ocelot,
1029                        ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1030                        ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1031                        ANA_PORT_QOS_CFG,
1032                        port);
1033
1034         for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1035                 ocelot_rmw_ix(ocelot,
1036                               (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1037                               ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1038                               ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1039                               ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1040                               ANA_PORT_PCP_DEI_MAP,
1041                               port, i);
1042         }
1043 }
1044
1045 static void felix_get_strings(struct dsa_switch *ds, int port,
1046                               u32 stringset, u8 *data)
1047 {
1048         struct ocelot *ocelot = ds->priv;
1049
1050         return ocelot_get_strings(ocelot, port, stringset, data);
1051 }
1052
1053 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
1054 {
1055         struct ocelot *ocelot = ds->priv;
1056
1057         ocelot_get_ethtool_stats(ocelot, port, data);
1058 }
1059
1060 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
1061 {
1062         struct ocelot *ocelot = ds->priv;
1063
1064         return ocelot_get_sset_count(ocelot, port, sset);
1065 }
1066
1067 static int felix_get_ts_info(struct dsa_switch *ds, int port,
1068                              struct ethtool_ts_info *info)
1069 {
1070         struct ocelot *ocelot = ds->priv;
1071
1072         return ocelot_get_ts_info(ocelot, port, info);
1073 }
1074
1075 static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1076         [PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1077         [PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1078         [PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1079         [PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1080         [PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1081 };
1082
1083 static int felix_validate_phy_mode(struct felix *felix, int port,
1084                                    phy_interface_t phy_mode)
1085 {
1086         u32 modes = felix->info->port_modes[port];
1087
1088         if (felix_phy_match_table[phy_mode] & modes)
1089                 return 0;
1090         return -EOPNOTSUPP;
1091 }
1092
1093 static int felix_parse_ports_node(struct felix *felix,
1094                                   struct device_node *ports_node,
1095                                   phy_interface_t *port_phy_modes)
1096 {
1097         struct device *dev = felix->ocelot.dev;
1098         struct device_node *child;
1099
1100         for_each_available_child_of_node(ports_node, child) {
1101                 phy_interface_t phy_mode;
1102                 u32 port;
1103                 int err;
1104
1105                 /* Get switch port number from DT */
1106                 if (of_property_read_u32(child, "reg", &port) < 0) {
1107                         dev_err(dev, "Port number not defined in device tree "
1108                                 "(property \"reg\")\n");
1109                         of_node_put(child);
1110                         return -ENODEV;
1111                 }
1112
1113                 /* Get PHY mode from DT */
1114                 err = of_get_phy_mode(child, &phy_mode);
1115                 if (err) {
1116                         dev_err(dev, "Failed to read phy-mode or "
1117                                 "phy-interface-type property for port %d\n",
1118                                 port);
1119                         of_node_put(child);
1120                         return -ENODEV;
1121                 }
1122
1123                 err = felix_validate_phy_mode(felix, port, phy_mode);
1124                 if (err < 0) {
1125                         dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1126                                 phy_modes(phy_mode), port);
1127                         of_node_put(child);
1128                         return err;
1129                 }
1130
1131                 port_phy_modes[port] = phy_mode;
1132         }
1133
1134         return 0;
1135 }
1136
1137 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1138 {
1139         struct device *dev = felix->ocelot.dev;
1140         struct device_node *switch_node;
1141         struct device_node *ports_node;
1142         int err;
1143
1144         switch_node = dev->of_node;
1145
1146         ports_node = of_get_child_by_name(switch_node, "ports");
1147         if (!ports_node)
1148                 ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1149         if (!ports_node) {
1150                 dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1151                 return -ENODEV;
1152         }
1153
1154         err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1155         of_node_put(ports_node);
1156
1157         return err;
1158 }
1159
1160 static int felix_init_structs(struct felix *felix, int num_phys_ports)
1161 {
1162         struct ocelot *ocelot = &felix->ocelot;
1163         phy_interface_t *port_phy_modes;
1164         struct resource res;
1165         int port, i, err;
1166
1167         ocelot->num_phys_ports = num_phys_ports;
1168         ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
1169                                      sizeof(struct ocelot_port *), GFP_KERNEL);
1170         if (!ocelot->ports)
1171                 return -ENOMEM;
1172
1173         ocelot->map             = felix->info->map;
1174         ocelot->stats_layout    = felix->info->stats_layout;
1175         ocelot->num_stats       = felix->info->num_stats;
1176         ocelot->num_mact_rows   = felix->info->num_mact_rows;
1177         ocelot->vcap            = felix->info->vcap;
1178         ocelot->vcap_pol.base   = felix->info->vcap_pol_base;
1179         ocelot->vcap_pol.max    = felix->info->vcap_pol_max;
1180         ocelot->vcap_pol.base2  = felix->info->vcap_pol_base2;
1181         ocelot->vcap_pol.max2   = felix->info->vcap_pol_max2;
1182         ocelot->ops             = felix->info->ops;
1183         ocelot->npi_inj_prefix  = OCELOT_TAG_PREFIX_SHORT;
1184         ocelot->npi_xtr_prefix  = OCELOT_TAG_PREFIX_SHORT;
1185         ocelot->devlink         = felix->ds->devlink;
1186
1187         port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1188                                  GFP_KERNEL);
1189         if (!port_phy_modes)
1190                 return -ENOMEM;
1191
1192         err = felix_parse_dt(felix, port_phy_modes);
1193         if (err) {
1194                 kfree(port_phy_modes);
1195                 return err;
1196         }
1197
1198         for (i = 0; i < TARGET_MAX; i++) {
1199                 struct regmap *target;
1200
1201                 if (!felix->info->target_io_res[i].name)
1202                         continue;
1203
1204                 memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1205                 res.flags = IORESOURCE_MEM;
1206                 res.start += felix->switch_base;
1207                 res.end += felix->switch_base;
1208
1209                 target = felix->info->init_regmap(ocelot, &res);
1210                 if (IS_ERR(target)) {
1211                         dev_err(ocelot->dev,
1212                                 "Failed to map device memory space\n");
1213                         kfree(port_phy_modes);
1214                         return PTR_ERR(target);
1215                 }
1216
1217                 ocelot->targets[i] = target;
1218         }
1219
1220         err = ocelot_regfields_init(ocelot, felix->info->regfields);
1221         if (err) {
1222                 dev_err(ocelot->dev, "failed to init reg fields map\n");
1223                 kfree(port_phy_modes);
1224                 return err;
1225         }
1226
1227         for (port = 0; port < num_phys_ports; port++) {
1228                 struct ocelot_port *ocelot_port;
1229                 struct regmap *target;
1230
1231                 ocelot_port = devm_kzalloc(ocelot->dev,
1232                                            sizeof(struct ocelot_port),
1233                                            GFP_KERNEL);
1234                 if (!ocelot_port) {
1235                         dev_err(ocelot->dev,
1236                                 "failed to allocate port memory\n");
1237                         kfree(port_phy_modes);
1238                         return -ENOMEM;
1239                 }
1240
1241                 memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1242                 res.flags = IORESOURCE_MEM;
1243                 res.start += felix->switch_base;
1244                 res.end += felix->switch_base;
1245
1246                 target = felix->info->init_regmap(ocelot, &res);
1247                 if (IS_ERR(target)) {
1248                         dev_err(ocelot->dev,
1249                                 "Failed to map memory space for port %d\n",
1250                                 port);
1251                         kfree(port_phy_modes);
1252                         return PTR_ERR(target);
1253                 }
1254
1255                 ocelot_port->phy_mode = port_phy_modes[port];
1256                 ocelot_port->ocelot = ocelot;
1257                 ocelot_port->target = target;
1258                 ocelot->ports[port] = ocelot_port;
1259         }
1260
1261         kfree(port_phy_modes);
1262
1263         if (felix->info->mdio_bus_alloc) {
1264                 err = felix->info->mdio_bus_alloc(ocelot);
1265                 if (err < 0)
1266                         return err;
1267         }
1268
1269         return 0;
1270 }
1271
1272 static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
1273                                            struct sk_buff *skb)
1274 {
1275         struct ocelot_port *ocelot_port = ocelot->ports[port];
1276         struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
1277         struct sk_buff *skb_match = NULL, *skb_tmp;
1278         unsigned long flags;
1279
1280         if (!clone)
1281                 return;
1282
1283         spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
1284
1285         skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
1286                 if (skb != clone)
1287                         continue;
1288                 __skb_unlink(skb, &ocelot_port->tx_skbs);
1289                 skb_match = skb;
1290                 break;
1291         }
1292
1293         spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
1294
1295         WARN_ONCE(!skb_match,
1296                   "Could not find skb clone in TX timestamping list\n");
1297 }
1298
1299 #define work_to_xmit_work(w) \
1300                 container_of((w), struct felix_deferred_xmit_work, work)
1301
1302 static void felix_port_deferred_xmit(struct kthread_work *work)
1303 {
1304         struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
1305         struct dsa_switch *ds = xmit_work->dp->ds;
1306         struct sk_buff *skb = xmit_work->skb;
1307         u32 rew_op = ocelot_ptp_rew_op(skb);
1308         struct ocelot *ocelot = ds->priv;
1309         int port = xmit_work->dp->index;
1310         int retries = 10;
1311
1312         do {
1313                 if (ocelot_can_inject(ocelot, 0))
1314                         break;
1315
1316                 cpu_relax();
1317         } while (--retries);
1318
1319         if (!retries) {
1320                 dev_err(ocelot->dev, "port %d failed to inject skb\n",
1321                         port);
1322                 ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
1323                 kfree_skb(skb);
1324                 return;
1325         }
1326
1327         ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
1328
1329         consume_skb(skb);
1330         kfree(xmit_work);
1331 }
1332
1333 static int felix_connect_tag_protocol(struct dsa_switch *ds,
1334                                       enum dsa_tag_protocol proto)
1335 {
1336         struct ocelot_8021q_tagger_data *tagger_data;
1337
1338         switch (proto) {
1339         case DSA_TAG_PROTO_OCELOT_8021Q:
1340                 tagger_data = ocelot_8021q_tagger_data(ds);
1341                 tagger_data->xmit_work_fn = felix_port_deferred_xmit;
1342                 return 0;
1343         case DSA_TAG_PROTO_OCELOT:
1344         case DSA_TAG_PROTO_SEVILLE:
1345                 return 0;
1346         default:
1347                 return -EPROTONOSUPPORT;
1348         }
1349 }
1350
1351 /* Hardware initialization done here so that we can allocate structures with
1352  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
1353  * us to allocate structures twice (leak memory) and map PCI memory twice
1354  * (which will not work).
1355  */
1356 static int felix_setup(struct dsa_switch *ds)
1357 {
1358         struct ocelot *ocelot = ds->priv;
1359         struct felix *felix = ocelot_to_felix(ocelot);
1360         struct dsa_port *dp;
1361         int err;
1362
1363         err = felix_init_structs(felix, ds->num_ports);
1364         if (err)
1365                 return err;
1366
1367         err = ocelot_init(ocelot);
1368         if (err)
1369                 goto out_mdiobus_free;
1370
1371         if (ocelot->ptp) {
1372                 err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
1373                 if (err) {
1374                         dev_err(ocelot->dev,
1375                                 "Timestamp initialization failed\n");
1376                         ocelot->ptp = 0;
1377                 }
1378         }
1379
1380         dsa_switch_for_each_available_port(dp, ds) {
1381                 ocelot_init_port(ocelot, dp->index);
1382
1383                 /* Set the default QoS Classification based on PCP and DEI
1384                  * bits of vlan tag.
1385                  */
1386                 felix_port_qos_map_init(ocelot, dp->index);
1387         }
1388
1389         err = ocelot_devlink_sb_register(ocelot);
1390         if (err)
1391                 goto out_deinit_ports;
1392
1393         dsa_switch_for_each_cpu_port(dp, ds) {
1394                 /* The initial tag protocol is NPI which always returns 0, so
1395                  * there's no real point in checking for errors.
1396                  */
1397                 felix_set_tag_protocol(ds, dp->index, felix->tag_proto, false);
1398                 break;
1399         }
1400
1401         ds->mtu_enforcement_ingress = true;
1402         ds->assisted_learning_on_cpu_port = true;
1403         ds->fdb_isolation = true;
1404         ds->max_num_bridges = ds->num_ports;
1405
1406         return 0;
1407
1408 out_deinit_ports:
1409         dsa_switch_for_each_available_port(dp, ds)
1410                 ocelot_deinit_port(ocelot, dp->index);
1411
1412         ocelot_deinit_timestamp(ocelot);
1413         ocelot_deinit(ocelot);
1414
1415 out_mdiobus_free:
1416         if (felix->info->mdio_bus_free)
1417                 felix->info->mdio_bus_free(ocelot);
1418
1419         return err;
1420 }
1421
1422 static void felix_teardown(struct dsa_switch *ds)
1423 {
1424         struct ocelot *ocelot = ds->priv;
1425         struct felix *felix = ocelot_to_felix(ocelot);
1426         struct dsa_port *dp;
1427
1428         dsa_switch_for_each_cpu_port(dp, ds) {
1429                 felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
1430                 break;
1431         }
1432
1433         dsa_switch_for_each_available_port(dp, ds)
1434                 ocelot_deinit_port(ocelot, dp->index);
1435
1436         ocelot_devlink_sb_unregister(ocelot);
1437         ocelot_deinit_timestamp(ocelot);
1438         ocelot_deinit(ocelot);
1439
1440         if (felix->info->mdio_bus_free)
1441                 felix->info->mdio_bus_free(ocelot);
1442 }
1443
1444 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1445                               struct ifreq *ifr)
1446 {
1447         struct ocelot *ocelot = ds->priv;
1448
1449         return ocelot_hwstamp_get(ocelot, port, ifr);
1450 }
1451
1452 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1453                               struct ifreq *ifr)
1454 {
1455         struct ocelot *ocelot = ds->priv;
1456         struct felix *felix = ocelot_to_felix(ocelot);
1457         bool using_tag_8021q;
1458         int err;
1459
1460         err = ocelot_hwstamp_set(ocelot, port, ifr);
1461         if (err)
1462                 return err;
1463
1464         using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1465
1466         return felix_update_trapping_destinations(ds, using_tag_8021q);
1467 }
1468
1469 static bool felix_check_xtr_pkt(struct ocelot *ocelot)
1470 {
1471         struct felix *felix = ocelot_to_felix(ocelot);
1472         int err, grp = 0;
1473
1474         if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
1475                 return false;
1476
1477         if (!felix->info->quirk_no_xtr_irq)
1478                 return false;
1479
1480         while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
1481                 struct sk_buff *skb;
1482                 unsigned int type;
1483
1484                 err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
1485                 if (err)
1486                         goto out;
1487
1488                 /* We trap to the CPU port module all PTP frames, but
1489                  * felix_rxtstamp() only gets called for event frames.
1490                  * So we need to avoid sending duplicate general
1491                  * message frames by running a second BPF classifier
1492                  * here and dropping those.
1493                  */
1494                 __skb_push(skb, ETH_HLEN);
1495
1496                 type = ptp_classify_raw(skb);
1497
1498                 __skb_pull(skb, ETH_HLEN);
1499
1500                 if (type == PTP_CLASS_NONE) {
1501                         kfree_skb(skb);
1502                         continue;
1503                 }
1504
1505                 netif_rx(skb);
1506         }
1507
1508 out:
1509         if (err < 0)
1510                 ocelot_drain_cpu_queue(ocelot, 0);
1511
1512         return true;
1513 }
1514
1515 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1516                            struct sk_buff *skb, unsigned int type)
1517 {
1518         u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1519         struct skb_shared_hwtstamps *shhwtstamps;
1520         struct ocelot *ocelot = ds->priv;
1521         struct timespec64 ts;
1522         u32 tstamp_hi;
1523         u64 tstamp;
1524
1525         /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
1526          * for RX timestamping. Then free it, and poll for its copy through
1527          * MMIO in the CPU port module, and inject that into the stack from
1528          * ocelot_xtr_poll().
1529          */
1530         if (felix_check_xtr_pkt(ocelot)) {
1531                 kfree_skb(skb);
1532                 return true;
1533         }
1534
1535         ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1536         tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1537
1538         tstamp_hi = tstamp >> 32;
1539         if ((tstamp & 0xffffffff) < tstamp_lo)
1540                 tstamp_hi--;
1541
1542         tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1543
1544         shhwtstamps = skb_hwtstamps(skb);
1545         memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1546         shhwtstamps->hwtstamp = tstamp;
1547         return false;
1548 }
1549
1550 static void felix_txtstamp(struct dsa_switch *ds, int port,
1551                            struct sk_buff *skb)
1552 {
1553         struct ocelot *ocelot = ds->priv;
1554         struct sk_buff *clone = NULL;
1555
1556         if (!ocelot->ptp)
1557                 return;
1558
1559         if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
1560                 dev_err_ratelimited(ds->dev,
1561                                     "port %d delivering skb without TX timestamp\n",
1562                                     port);
1563                 return;
1564         }
1565
1566         if (clone)
1567                 OCELOT_SKB_CB(skb)->clone = clone;
1568 }
1569
1570 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1571 {
1572         struct ocelot *ocelot = ds->priv;
1573
1574         ocelot_port_set_maxlen(ocelot, port, new_mtu);
1575
1576         return 0;
1577 }
1578
1579 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
1580 {
1581         struct ocelot *ocelot = ds->priv;
1582
1583         return ocelot_get_max_mtu(ocelot, port);
1584 }
1585
1586 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
1587                                 struct flow_cls_offload *cls, bool ingress)
1588 {
1589         struct ocelot *ocelot = ds->priv;
1590         struct felix *felix = ocelot_to_felix(ocelot);
1591         bool using_tag_8021q;
1592         int err;
1593
1594         err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
1595         if (err)
1596                 return err;
1597
1598         using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
1599
1600         return felix_update_trapping_destinations(ds, using_tag_8021q);
1601 }
1602
1603 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
1604                                 struct flow_cls_offload *cls, bool ingress)
1605 {
1606         struct ocelot *ocelot = ds->priv;
1607
1608         return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
1609 }
1610
1611 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
1612                                   struct flow_cls_offload *cls, bool ingress)
1613 {
1614         struct ocelot *ocelot = ds->priv;
1615
1616         return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
1617 }
1618
1619 static int felix_port_policer_add(struct dsa_switch *ds, int port,
1620                                   struct dsa_mall_policer_tc_entry *policer)
1621 {
1622         struct ocelot *ocelot = ds->priv;
1623         struct ocelot_policer pol = {
1624                 .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
1625                 .burst = policer->burst,
1626         };
1627
1628         return ocelot_port_policer_add(ocelot, port, &pol);
1629 }
1630
1631 static void felix_port_policer_del(struct dsa_switch *ds, int port)
1632 {
1633         struct ocelot *ocelot = ds->priv;
1634
1635         ocelot_port_policer_del(ocelot, port);
1636 }
1637
1638 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1639                                enum tc_setup_type type,
1640                                void *type_data)
1641 {
1642         struct ocelot *ocelot = ds->priv;
1643         struct felix *felix = ocelot_to_felix(ocelot);
1644
1645         if (felix->info->port_setup_tc)
1646                 return felix->info->port_setup_tc(ds, port, type, type_data);
1647         else
1648                 return -EOPNOTSUPP;
1649 }
1650
1651 static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1652                              u16 pool_index,
1653                              struct devlink_sb_pool_info *pool_info)
1654 {
1655         struct ocelot *ocelot = ds->priv;
1656
1657         return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1658 }
1659
1660 static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1661                              u16 pool_index, u32 size,
1662                              enum devlink_sb_threshold_type threshold_type,
1663                              struct netlink_ext_ack *extack)
1664 {
1665         struct ocelot *ocelot = ds->priv;
1666
1667         return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1668                                   threshold_type, extack);
1669 }
1670
1671 static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1672                                   unsigned int sb_index, u16 pool_index,
1673                                   u32 *p_threshold)
1674 {
1675         struct ocelot *ocelot = ds->priv;
1676
1677         return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1678                                        p_threshold);
1679 }
1680
1681 static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1682                                   unsigned int sb_index, u16 pool_index,
1683                                   u32 threshold, struct netlink_ext_ack *extack)
1684 {
1685         struct ocelot *ocelot = ds->priv;
1686
1687         return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1688                                        threshold, extack);
1689 }
1690
1691 static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1692                                      unsigned int sb_index, u16 tc_index,
1693                                      enum devlink_sb_pool_type pool_type,
1694                                      u16 *p_pool_index, u32 *p_threshold)
1695 {
1696         struct ocelot *ocelot = ds->priv;
1697
1698         return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1699                                           pool_type, p_pool_index,
1700                                           p_threshold);
1701 }
1702
1703 static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1704                                      unsigned int sb_index, u16 tc_index,
1705                                      enum devlink_sb_pool_type pool_type,
1706                                      u16 pool_index, u32 threshold,
1707                                      struct netlink_ext_ack *extack)
1708 {
1709         struct ocelot *ocelot = ds->priv;
1710
1711         return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1712                                           pool_type, pool_index, threshold,
1713                                           extack);
1714 }
1715
1716 static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1717                                  unsigned int sb_index)
1718 {
1719         struct ocelot *ocelot = ds->priv;
1720
1721         return ocelot_sb_occ_snapshot(ocelot, sb_index);
1722 }
1723
1724 static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1725                                   unsigned int sb_index)
1726 {
1727         struct ocelot *ocelot = ds->priv;
1728
1729         return ocelot_sb_occ_max_clear(ocelot, sb_index);
1730 }
1731
1732 static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1733                                       unsigned int sb_index, u16 pool_index,
1734                                       u32 *p_cur, u32 *p_max)
1735 {
1736         struct ocelot *ocelot = ds->priv;
1737
1738         return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1739                                            p_cur, p_max);
1740 }
1741
1742 static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1743                                          unsigned int sb_index, u16 tc_index,
1744                                          enum devlink_sb_pool_type pool_type,
1745                                          u32 *p_cur, u32 *p_max)
1746 {
1747         struct ocelot *ocelot = ds->priv;
1748
1749         return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1750                                               pool_type, p_cur, p_max);
1751 }
1752
1753 static int felix_mrp_add(struct dsa_switch *ds, int port,
1754                          const struct switchdev_obj_mrp *mrp)
1755 {
1756         struct ocelot *ocelot = ds->priv;
1757
1758         return ocelot_mrp_add(ocelot, port, mrp);
1759 }
1760
1761 static int felix_mrp_del(struct dsa_switch *ds, int port,
1762                          const struct switchdev_obj_mrp *mrp)
1763 {
1764         struct ocelot *ocelot = ds->priv;
1765
1766         return ocelot_mrp_add(ocelot, port, mrp);
1767 }
1768
1769 static int
1770 felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1771                         const struct switchdev_obj_ring_role_mrp *mrp)
1772 {
1773         struct ocelot *ocelot = ds->priv;
1774
1775         return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1776 }
1777
1778 static int
1779 felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1780                         const struct switchdev_obj_ring_role_mrp *mrp)
1781 {
1782         struct ocelot *ocelot = ds->priv;
1783
1784         return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1785 }
1786
1787 const struct dsa_switch_ops felix_switch_ops = {
1788         .get_tag_protocol               = felix_get_tag_protocol,
1789         .change_tag_protocol            = felix_change_tag_protocol,
1790         .connect_tag_protocol           = felix_connect_tag_protocol,
1791         .setup                          = felix_setup,
1792         .teardown                       = felix_teardown,
1793         .set_ageing_time                = felix_set_ageing_time,
1794         .get_strings                    = felix_get_strings,
1795         .get_ethtool_stats              = felix_get_ethtool_stats,
1796         .get_sset_count                 = felix_get_sset_count,
1797         .get_ts_info                    = felix_get_ts_info,
1798         .phylink_get_caps               = felix_phylink_get_caps,
1799         .phylink_validate               = felix_phylink_validate,
1800         .phylink_mac_select_pcs         = felix_phylink_mac_select_pcs,
1801         .phylink_mac_link_down          = felix_phylink_mac_link_down,
1802         .phylink_mac_link_up            = felix_phylink_mac_link_up,
1803         .port_fast_age                  = felix_port_fast_age,
1804         .port_fdb_dump                  = felix_fdb_dump,
1805         .port_fdb_add                   = felix_fdb_add,
1806         .port_fdb_del                   = felix_fdb_del,
1807         .lag_fdb_add                    = felix_lag_fdb_add,
1808         .lag_fdb_del                    = felix_lag_fdb_del,
1809         .port_mdb_add                   = felix_mdb_add,
1810         .port_mdb_del                   = felix_mdb_del,
1811         .port_pre_bridge_flags          = felix_pre_bridge_flags,
1812         .port_bridge_flags              = felix_bridge_flags,
1813         .port_bridge_join               = felix_bridge_join,
1814         .port_bridge_leave              = felix_bridge_leave,
1815         .port_lag_join                  = felix_lag_join,
1816         .port_lag_leave                 = felix_lag_leave,
1817         .port_lag_change                = felix_lag_change,
1818         .port_stp_state_set             = felix_bridge_stp_state_set,
1819         .port_vlan_filtering            = felix_vlan_filtering,
1820         .port_vlan_add                  = felix_vlan_add,
1821         .port_vlan_del                  = felix_vlan_del,
1822         .port_hwtstamp_get              = felix_hwtstamp_get,
1823         .port_hwtstamp_set              = felix_hwtstamp_set,
1824         .port_rxtstamp                  = felix_rxtstamp,
1825         .port_txtstamp                  = felix_txtstamp,
1826         .port_change_mtu                = felix_change_mtu,
1827         .port_max_mtu                   = felix_get_max_mtu,
1828         .port_policer_add               = felix_port_policer_add,
1829         .port_policer_del               = felix_port_policer_del,
1830         .cls_flower_add                 = felix_cls_flower_add,
1831         .cls_flower_del                 = felix_cls_flower_del,
1832         .cls_flower_stats               = felix_cls_flower_stats,
1833         .port_setup_tc                  = felix_port_setup_tc,
1834         .devlink_sb_pool_get            = felix_sb_pool_get,
1835         .devlink_sb_pool_set            = felix_sb_pool_set,
1836         .devlink_sb_port_pool_get       = felix_sb_port_pool_get,
1837         .devlink_sb_port_pool_set       = felix_sb_port_pool_set,
1838         .devlink_sb_tc_pool_bind_get    = felix_sb_tc_pool_bind_get,
1839         .devlink_sb_tc_pool_bind_set    = felix_sb_tc_pool_bind_set,
1840         .devlink_sb_occ_snapshot        = felix_sb_occ_snapshot,
1841         .devlink_sb_occ_max_clear       = felix_sb_occ_max_clear,
1842         .devlink_sb_occ_port_pool_get   = felix_sb_occ_port_pool_get,
1843         .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1844         .port_mrp_add                   = felix_mrp_add,
1845         .port_mrp_del                   = felix_mrp_del,
1846         .port_mrp_add_ring_role         = felix_mrp_add_ring_role,
1847         .port_mrp_del_ring_role         = felix_mrp_del_ring_role,
1848         .tag_8021q_vlan_add             = felix_tag_8021q_vlan_add,
1849         .tag_8021q_vlan_del             = felix_tag_8021q_vlan_del,
1850 };
1851
1852 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1853 {
1854         struct felix *felix = ocelot_to_felix(ocelot);
1855         struct dsa_switch *ds = felix->ds;
1856
1857         if (!dsa_is_user_port(ds, port))
1858                 return NULL;
1859
1860         return dsa_to_port(ds, port)->slave;
1861 }
1862
1863 int felix_netdev_to_port(struct net_device *dev)
1864 {
1865         struct dsa_port *dp;
1866
1867         dp = dsa_port_from_netdev(dev);
1868         if (IS_ERR(dp))
1869                 return -EINVAL;
1870
1871         return dp->index;
1872 }