1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 NXP Semiconductors
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/platform_device.h>
17 #include <linux/packing.h>
18 #include <linux/module.h>
19 #include <linux/of_net.h>
20 #include <linux/pci.h>
21 #include <linux/of.h>
22 #include <linux/pcs-lynx.h>
23 #include <net/pkt_sched.h>
24 #include <net/dsa.h>
25 #include "felix.h"
26
felix_get_tag_protocol(struct dsa_switch * ds,int port,enum dsa_tag_protocol mp)27 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
28 int port,
29 enum dsa_tag_protocol mp)
30 {
31 return DSA_TAG_PROTO_OCELOT;
32 }
33
felix_set_ageing_time(struct dsa_switch * ds,unsigned int ageing_time)34 static int felix_set_ageing_time(struct dsa_switch *ds,
35 unsigned int ageing_time)
36 {
37 struct ocelot *ocelot = ds->priv;
38
39 ocelot_set_ageing_time(ocelot, ageing_time);
40
41 return 0;
42 }
43
felix_fdb_dump(struct dsa_switch * ds,int port,dsa_fdb_dump_cb_t * cb,void * data)44 static int felix_fdb_dump(struct dsa_switch *ds, int port,
45 dsa_fdb_dump_cb_t *cb, void *data)
46 {
47 struct ocelot *ocelot = ds->priv;
48
49 return ocelot_fdb_dump(ocelot, port, cb, data);
50 }
51
felix_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)52 static int felix_fdb_add(struct dsa_switch *ds, int port,
53 const unsigned char *addr, u16 vid)
54 {
55 struct ocelot *ocelot = ds->priv;
56
57 return ocelot_fdb_add(ocelot, port, addr, vid);
58 }
59
felix_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)60 static int felix_fdb_del(struct dsa_switch *ds, int port,
61 const unsigned char *addr, u16 vid)
62 {
63 struct ocelot *ocelot = ds->priv;
64
65 return ocelot_fdb_del(ocelot, port, addr, vid);
66 }
67
68 /* This callback needs to be present */
felix_mdb_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)69 static int felix_mdb_prepare(struct dsa_switch *ds, int port,
70 const struct switchdev_obj_port_mdb *mdb)
71 {
72 return 0;
73 }
74
felix_mdb_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)75 static void felix_mdb_add(struct dsa_switch *ds, int port,
76 const struct switchdev_obj_port_mdb *mdb)
77 {
78 struct ocelot *ocelot = ds->priv;
79
80 ocelot_port_mdb_add(ocelot, port, mdb);
81 }
82
felix_mdb_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)83 static int felix_mdb_del(struct dsa_switch *ds, int port,
84 const struct switchdev_obj_port_mdb *mdb)
85 {
86 struct ocelot *ocelot = ds->priv;
87
88 return ocelot_port_mdb_del(ocelot, port, mdb);
89 }
90
felix_bridge_stp_state_set(struct dsa_switch * ds,int port,u8 state)91 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
92 u8 state)
93 {
94 struct ocelot *ocelot = ds->priv;
95
96 return ocelot_bridge_stp_state_set(ocelot, port, state);
97 }
98
felix_bridge_join(struct dsa_switch * ds,int port,struct net_device * br)99 static int felix_bridge_join(struct dsa_switch *ds, int port,
100 struct net_device *br)
101 {
102 struct ocelot *ocelot = ds->priv;
103
104 return ocelot_port_bridge_join(ocelot, port, br);
105 }
106
felix_bridge_leave(struct dsa_switch * ds,int port,struct net_device * br)107 static void felix_bridge_leave(struct dsa_switch *ds, int port,
108 struct net_device *br)
109 {
110 struct ocelot *ocelot = ds->priv;
111
112 ocelot_port_bridge_leave(ocelot, port, br);
113 }
114
115 /* This callback needs to be present */
felix_vlan_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)116 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
117 const struct switchdev_obj_port_vlan *vlan)
118 {
119 return 0;
120 }
121
felix_vlan_filtering(struct dsa_switch * ds,int port,bool enabled,struct switchdev_trans * trans)122 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
123 struct switchdev_trans *trans)
124 {
125 struct ocelot *ocelot = ds->priv;
126
127 return ocelot_port_vlan_filtering(ocelot, port, enabled, trans);
128 }
129
felix_vlan_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)130 static void felix_vlan_add(struct dsa_switch *ds, int port,
131 const struct switchdev_obj_port_vlan *vlan)
132 {
133 struct ocelot *ocelot = ds->priv;
134 u16 flags = vlan->flags;
135 u16 vid;
136 int err;
137
138 if (dsa_is_cpu_port(ds, port))
139 flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
140
141 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
142 err = ocelot_vlan_add(ocelot, port, vid,
143 flags & BRIDGE_VLAN_INFO_PVID,
144 flags & BRIDGE_VLAN_INFO_UNTAGGED);
145 if (err) {
146 dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
147 vid, port, err);
148 return;
149 }
150 }
151 }
152
felix_vlan_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)153 static int felix_vlan_del(struct dsa_switch *ds, int port,
154 const struct switchdev_obj_port_vlan *vlan)
155 {
156 struct ocelot *ocelot = ds->priv;
157 u16 vid;
158 int err;
159
160 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
161 err = ocelot_vlan_del(ocelot, port, vid);
162 if (err) {
163 dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
164 vid, port, err);
165 return err;
166 }
167 }
168 return 0;
169 }
170
felix_port_enable(struct dsa_switch * ds,int port,struct phy_device * phy)171 static int felix_port_enable(struct dsa_switch *ds, int port,
172 struct phy_device *phy)
173 {
174 struct ocelot *ocelot = ds->priv;
175
176 ocelot_port_enable(ocelot, port, phy);
177
178 return 0;
179 }
180
felix_port_disable(struct dsa_switch * ds,int port)181 static void felix_port_disable(struct dsa_switch *ds, int port)
182 {
183 struct ocelot *ocelot = ds->priv;
184
185 return ocelot_port_disable(ocelot, port);
186 }
187
felix_phylink_validate(struct dsa_switch * ds,int port,unsigned long * supported,struct phylink_link_state * state)188 static void felix_phylink_validate(struct dsa_switch *ds, int port,
189 unsigned long *supported,
190 struct phylink_link_state *state)
191 {
192 struct ocelot *ocelot = ds->priv;
193 struct felix *felix = ocelot_to_felix(ocelot);
194
195 if (felix->info->phylink_validate)
196 felix->info->phylink_validate(ocelot, port, supported, state);
197 }
198
felix_phylink_mac_config(struct dsa_switch * ds,int port,unsigned int link_an_mode,const struct phylink_link_state * state)199 static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
200 unsigned int link_an_mode,
201 const struct phylink_link_state *state)
202 {
203 struct ocelot *ocelot = ds->priv;
204 struct felix *felix = ocelot_to_felix(ocelot);
205 struct dsa_port *dp = dsa_to_port(ds, port);
206
207 if (felix->pcs[port])
208 phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs);
209 }
210
felix_phylink_mac_link_down(struct dsa_switch * ds,int port,unsigned int link_an_mode,phy_interface_t interface)211 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
212 unsigned int link_an_mode,
213 phy_interface_t interface)
214 {
215 struct ocelot *ocelot = ds->priv;
216 struct ocelot_port *ocelot_port = ocelot->ports[port];
217
218 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
219 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
220 }
221
felix_phylink_mac_link_up(struct dsa_switch * ds,int port,unsigned int link_an_mode,phy_interface_t interface,struct phy_device * phydev,int speed,int duplex,bool tx_pause,bool rx_pause)222 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
223 unsigned int link_an_mode,
224 phy_interface_t interface,
225 struct phy_device *phydev,
226 int speed, int duplex,
227 bool tx_pause, bool rx_pause)
228 {
229 struct ocelot *ocelot = ds->priv;
230 struct ocelot_port *ocelot_port = ocelot->ports[port];
231 struct felix *felix = ocelot_to_felix(ocelot);
232 u32 mac_fc_cfg;
233
234 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
235 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
236 * integrated is that the MAC speed is fixed and it's the PCS who is
237 * performing the rate adaptation, so we have to write "1000Mbps" into
238 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
239 * value).
240 */
241 ocelot_port_writel(ocelot_port,
242 DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
243 DEV_CLOCK_CFG);
244
245 switch (speed) {
246 case SPEED_10:
247 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
248 break;
249 case SPEED_100:
250 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
251 break;
252 case SPEED_1000:
253 case SPEED_2500:
254 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
255 break;
256 default:
257 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
258 port, speed);
259 return;
260 }
261
262 /* handle Rx pause in all cases, with 2500base-X this is used for rate
263 * adaptation.
264 */
265 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
266
267 if (tx_pause)
268 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
269 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
270 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
271 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
272
273 /* Flow control. Link speed is only used here to evaluate the time
274 * specification in incoming pause frames.
275 */
276 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
277
278 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
279
280 /* Undo the effects of felix_phylink_mac_link_down:
281 * enable MAC module
282 */
283 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
284 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
285
286 /* Enable receiving frames on the port, and activate auto-learning of
287 * MAC addresses.
288 */
289 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
290 ANA_PORT_PORT_CFG_RECV_ENA |
291 ANA_PORT_PORT_CFG_PORTID_VAL(port),
292 ANA_PORT_PORT_CFG, port);
293
294 /* Core: Enable port for frame transfer */
295 ocelot_fields_write(ocelot, port,
296 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
297
298 if (felix->info->port_sched_speed_set)
299 felix->info->port_sched_speed_set(ocelot, port, speed);
300 }
301
felix_port_qos_map_init(struct ocelot * ocelot,int port)302 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
303 {
304 int i;
305
306 ocelot_rmw_gix(ocelot,
307 ANA_PORT_QOS_CFG_QOS_PCP_ENA,
308 ANA_PORT_QOS_CFG_QOS_PCP_ENA,
309 ANA_PORT_QOS_CFG,
310 port);
311
312 for (i = 0; i < FELIX_NUM_TC * 2; i++) {
313 ocelot_rmw_ix(ocelot,
314 (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
315 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
316 ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
317 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
318 ANA_PORT_PCP_DEI_MAP,
319 port, i);
320 }
321 }
322
felix_get_strings(struct dsa_switch * ds,int port,u32 stringset,u8 * data)323 static void felix_get_strings(struct dsa_switch *ds, int port,
324 u32 stringset, u8 *data)
325 {
326 struct ocelot *ocelot = ds->priv;
327
328 return ocelot_get_strings(ocelot, port, stringset, data);
329 }
330
felix_get_ethtool_stats(struct dsa_switch * ds,int port,u64 * data)331 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
332 {
333 struct ocelot *ocelot = ds->priv;
334
335 ocelot_get_ethtool_stats(ocelot, port, data);
336 }
337
felix_get_sset_count(struct dsa_switch * ds,int port,int sset)338 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
339 {
340 struct ocelot *ocelot = ds->priv;
341
342 return ocelot_get_sset_count(ocelot, port, sset);
343 }
344
felix_get_ts_info(struct dsa_switch * ds,int port,struct ethtool_ts_info * info)345 static int felix_get_ts_info(struct dsa_switch *ds, int port,
346 struct ethtool_ts_info *info)
347 {
348 struct ocelot *ocelot = ds->priv;
349
350 return ocelot_get_ts_info(ocelot, port, info);
351 }
352
felix_parse_ports_node(struct felix * felix,struct device_node * ports_node,phy_interface_t * port_phy_modes)353 static int felix_parse_ports_node(struct felix *felix,
354 struct device_node *ports_node,
355 phy_interface_t *port_phy_modes)
356 {
357 struct ocelot *ocelot = &felix->ocelot;
358 struct device *dev = felix->ocelot.dev;
359 struct device_node *child;
360
361 for_each_available_child_of_node(ports_node, child) {
362 phy_interface_t phy_mode;
363 u32 port;
364 int err;
365
366 /* Get switch port number from DT */
367 if (of_property_read_u32(child, "reg", &port) < 0) {
368 dev_err(dev, "Port number not defined in device tree "
369 "(property \"reg\")\n");
370 of_node_put(child);
371 return -ENODEV;
372 }
373
374 /* Get PHY mode from DT */
375 err = of_get_phy_mode(child, &phy_mode);
376 if (err) {
377 dev_err(dev, "Failed to read phy-mode or "
378 "phy-interface-type property for port %d\n",
379 port);
380 of_node_put(child);
381 return -ENODEV;
382 }
383
384 err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
385 if (err < 0) {
386 dev_err(dev, "Unsupported PHY mode %s on port %d\n",
387 phy_modes(phy_mode), port);
388 of_node_put(child);
389 return err;
390 }
391
392 port_phy_modes[port] = phy_mode;
393 }
394
395 return 0;
396 }
397
felix_parse_dt(struct felix * felix,phy_interface_t * port_phy_modes)398 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
399 {
400 struct device *dev = felix->ocelot.dev;
401 struct device_node *switch_node;
402 struct device_node *ports_node;
403 int err;
404
405 switch_node = dev->of_node;
406
407 ports_node = of_get_child_by_name(switch_node, "ports");
408 if (!ports_node) {
409 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
410 return -ENODEV;
411 }
412
413 err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
414 of_node_put(ports_node);
415
416 return err;
417 }
418
felix_init_structs(struct felix * felix,int num_phys_ports)419 static int felix_init_structs(struct felix *felix, int num_phys_ports)
420 {
421 struct ocelot *ocelot = &felix->ocelot;
422 phy_interface_t *port_phy_modes;
423 struct resource res;
424 int port, i, err;
425
426 ocelot->num_phys_ports = num_phys_ports;
427 ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
428 sizeof(struct ocelot_port *), GFP_KERNEL);
429 if (!ocelot->ports)
430 return -ENOMEM;
431
432 ocelot->map = felix->info->map;
433 ocelot->stats_layout = felix->info->stats_layout;
434 ocelot->num_stats = felix->info->num_stats;
435 ocelot->shared_queue_sz = felix->info->shared_queue_sz;
436 ocelot->num_mact_rows = felix->info->num_mact_rows;
437 ocelot->vcap = felix->info->vcap;
438 ocelot->ops = felix->info->ops;
439 ocelot->inj_prefix = OCELOT_TAG_PREFIX_SHORT;
440 ocelot->xtr_prefix = OCELOT_TAG_PREFIX_SHORT;
441
442 port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
443 GFP_KERNEL);
444 if (!port_phy_modes)
445 return -ENOMEM;
446
447 err = felix_parse_dt(felix, port_phy_modes);
448 if (err) {
449 kfree(port_phy_modes);
450 return err;
451 }
452
453 for (i = 0; i < TARGET_MAX; i++) {
454 struct regmap *target;
455
456 if (!felix->info->target_io_res[i].name)
457 continue;
458
459 memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
460 res.flags = IORESOURCE_MEM;
461 res.start += felix->switch_base;
462 res.end += felix->switch_base;
463
464 target = ocelot_regmap_init(ocelot, &res);
465 if (IS_ERR(target)) {
466 dev_err(ocelot->dev,
467 "Failed to map device memory space\n");
468 kfree(port_phy_modes);
469 return PTR_ERR(target);
470 }
471
472 ocelot->targets[i] = target;
473 }
474
475 err = ocelot_regfields_init(ocelot, felix->info->regfields);
476 if (err) {
477 dev_err(ocelot->dev, "failed to init reg fields map\n");
478 kfree(port_phy_modes);
479 return err;
480 }
481
482 for (port = 0; port < num_phys_ports; port++) {
483 struct ocelot_port *ocelot_port;
484 struct regmap *target;
485 u8 *template;
486
487 ocelot_port = devm_kzalloc(ocelot->dev,
488 sizeof(struct ocelot_port),
489 GFP_KERNEL);
490 if (!ocelot_port) {
491 dev_err(ocelot->dev,
492 "failed to allocate port memory\n");
493 kfree(port_phy_modes);
494 return -ENOMEM;
495 }
496
497 memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
498 res.flags = IORESOURCE_MEM;
499 res.start += felix->switch_base;
500 res.end += felix->switch_base;
501
502 target = ocelot_regmap_init(ocelot, &res);
503 if (IS_ERR(target)) {
504 dev_err(ocelot->dev,
505 "Failed to map memory space for port %d\n",
506 port);
507 kfree(port_phy_modes);
508 return PTR_ERR(target);
509 }
510
511 template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN,
512 GFP_KERNEL);
513 if (!template) {
514 dev_err(ocelot->dev,
515 "Failed to allocate memory for DSA tag\n");
516 kfree(port_phy_modes);
517 return -ENOMEM;
518 }
519
520 ocelot_port->phy_mode = port_phy_modes[port];
521 ocelot_port->ocelot = ocelot;
522 ocelot_port->target = target;
523 ocelot_port->xmit_template = template;
524 ocelot->ports[port] = ocelot_port;
525
526 felix->info->xmit_template_populate(ocelot, port);
527 }
528
529 kfree(port_phy_modes);
530
531 if (felix->info->mdio_bus_alloc) {
532 err = felix->info->mdio_bus_alloc(ocelot);
533 if (err < 0)
534 return err;
535 }
536
537 return 0;
538 }
539
540 /* The CPU port module is connected to the Node Processor Interface (NPI). This
541 * is the mode through which frames can be injected from and extracted to an
542 * external CPU, over Ethernet.
543 */
felix_npi_port_init(struct ocelot * ocelot,int port)544 static void felix_npi_port_init(struct ocelot *ocelot, int port)
545 {
546 ocelot->npi = port;
547
548 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
549 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
550 QSYS_EXT_CPU_CFG);
551
552 /* NPI port Injection/Extraction configuration */
553 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
554 ocelot->xtr_prefix);
555 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
556 ocelot->inj_prefix);
557
558 /* Disable transmission of pause frames */
559 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
560 }
561
562 /* Hardware initialization done here so that we can allocate structures with
563 * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
564 * us to allocate structures twice (leak memory) and map PCI memory twice
565 * (which will not work).
566 */
felix_setup(struct dsa_switch * ds)567 static int felix_setup(struct dsa_switch *ds)
568 {
569 struct ocelot *ocelot = ds->priv;
570 struct felix *felix = ocelot_to_felix(ocelot);
571 int port, err;
572
573 err = felix_init_structs(felix, ds->num_ports);
574 if (err)
575 return err;
576
577 err = ocelot_init(ocelot);
578 if (err)
579 return err;
580
581 if (ocelot->ptp) {
582 err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
583 if (err) {
584 dev_err(ocelot->dev,
585 "Timestamp initialization failed\n");
586 ocelot->ptp = 0;
587 }
588 }
589
590 for (port = 0; port < ds->num_ports; port++) {
591 ocelot_init_port(ocelot, port);
592
593 if (dsa_is_cpu_port(ds, port))
594 felix_npi_port_init(ocelot, port);
595
596 /* Set the default QoS Classification based on PCP and DEI
597 * bits of vlan tag.
598 */
599 felix_port_qos_map_init(ocelot, port);
600 }
601
602 /* Include the CPU port module in the forwarding mask for unknown
603 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
604 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
605 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
606 */
607 ocelot_write_rix(ocelot,
608 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
609 ANA_PGID_PGID, PGID_UC);
610
611 ds->mtu_enforcement_ingress = true;
612 ds->configure_vlan_while_not_filtering = true;
613
614 return 0;
615 }
616
felix_teardown(struct dsa_switch * ds)617 static void felix_teardown(struct dsa_switch *ds)
618 {
619 struct ocelot *ocelot = ds->priv;
620 struct felix *felix = ocelot_to_felix(ocelot);
621 int port;
622
623 if (felix->info->mdio_bus_free)
624 felix->info->mdio_bus_free(ocelot);
625
626 for (port = 0; port < ocelot->num_phys_ports; port++)
627 ocelot_deinit_port(ocelot, port);
628 ocelot_deinit_timestamp(ocelot);
629 /* stop workqueue thread */
630 ocelot_deinit(ocelot);
631 }
632
felix_hwtstamp_get(struct dsa_switch * ds,int port,struct ifreq * ifr)633 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
634 struct ifreq *ifr)
635 {
636 struct ocelot *ocelot = ds->priv;
637
638 return ocelot_hwstamp_get(ocelot, port, ifr);
639 }
640
felix_hwtstamp_set(struct dsa_switch * ds,int port,struct ifreq * ifr)641 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
642 struct ifreq *ifr)
643 {
644 struct ocelot *ocelot = ds->priv;
645
646 return ocelot_hwstamp_set(ocelot, port, ifr);
647 }
648
felix_rxtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb,unsigned int type)649 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
650 struct sk_buff *skb, unsigned int type)
651 {
652 struct skb_shared_hwtstamps *shhwtstamps;
653 struct ocelot *ocelot = ds->priv;
654 u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
655 u32 tstamp_lo, tstamp_hi;
656 struct timespec64 ts;
657 u64 tstamp, val;
658
659 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
660 tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
661
662 packing(extraction, &val, 116, 85, OCELOT_TAG_LEN, UNPACK, 0);
663 tstamp_lo = (u32)val;
664
665 tstamp_hi = tstamp >> 32;
666 if ((tstamp & 0xffffffff) < tstamp_lo)
667 tstamp_hi--;
668
669 tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
670
671 shhwtstamps = skb_hwtstamps(skb);
672 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
673 shhwtstamps->hwtstamp = tstamp;
674 return false;
675 }
676
felix_txtstamp(struct dsa_switch * ds,int port,struct sk_buff * clone,unsigned int type)677 static bool felix_txtstamp(struct dsa_switch *ds, int port,
678 struct sk_buff *clone, unsigned int type)
679 {
680 struct ocelot *ocelot = ds->priv;
681 struct ocelot_port *ocelot_port = ocelot->ports[port];
682
683 if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
684 ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
685 ocelot_port_add_txtstamp_skb(ocelot, port, clone);
686 return true;
687 }
688
689 return false;
690 }
691
felix_change_mtu(struct dsa_switch * ds,int port,int new_mtu)692 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
693 {
694 struct ocelot *ocelot = ds->priv;
695
696 ocelot_port_set_maxlen(ocelot, port, new_mtu);
697
698 return 0;
699 }
700
felix_get_max_mtu(struct dsa_switch * ds,int port)701 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
702 {
703 struct ocelot *ocelot = ds->priv;
704
705 return ocelot_get_max_mtu(ocelot, port);
706 }
707
felix_cls_flower_add(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)708 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
709 struct flow_cls_offload *cls, bool ingress)
710 {
711 struct ocelot *ocelot = ds->priv;
712
713 return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
714 }
715
felix_cls_flower_del(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)716 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
717 struct flow_cls_offload *cls, bool ingress)
718 {
719 struct ocelot *ocelot = ds->priv;
720
721 return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
722 }
723
felix_cls_flower_stats(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)724 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
725 struct flow_cls_offload *cls, bool ingress)
726 {
727 struct ocelot *ocelot = ds->priv;
728
729 return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
730 }
731
felix_port_policer_add(struct dsa_switch * ds,int port,struct dsa_mall_policer_tc_entry * policer)732 static int felix_port_policer_add(struct dsa_switch *ds, int port,
733 struct dsa_mall_policer_tc_entry *policer)
734 {
735 struct ocelot *ocelot = ds->priv;
736 struct ocelot_policer pol = {
737 .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
738 .burst = policer->burst,
739 };
740
741 return ocelot_port_policer_add(ocelot, port, &pol);
742 }
743
felix_port_policer_del(struct dsa_switch * ds,int port)744 static void felix_port_policer_del(struct dsa_switch *ds, int port)
745 {
746 struct ocelot *ocelot = ds->priv;
747
748 ocelot_port_policer_del(ocelot, port);
749 }
750
felix_port_setup_tc(struct dsa_switch * ds,int port,enum tc_setup_type type,void * type_data)751 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
752 enum tc_setup_type type,
753 void *type_data)
754 {
755 struct ocelot *ocelot = ds->priv;
756 struct felix *felix = ocelot_to_felix(ocelot);
757
758 if (felix->info->port_setup_tc)
759 return felix->info->port_setup_tc(ds, port, type, type_data);
760 else
761 return -EOPNOTSUPP;
762 }
763
764 const struct dsa_switch_ops felix_switch_ops = {
765 .get_tag_protocol = felix_get_tag_protocol,
766 .setup = felix_setup,
767 .teardown = felix_teardown,
768 .set_ageing_time = felix_set_ageing_time,
769 .get_strings = felix_get_strings,
770 .get_ethtool_stats = felix_get_ethtool_stats,
771 .get_sset_count = felix_get_sset_count,
772 .get_ts_info = felix_get_ts_info,
773 .phylink_validate = felix_phylink_validate,
774 .phylink_mac_config = felix_phylink_mac_config,
775 .phylink_mac_link_down = felix_phylink_mac_link_down,
776 .phylink_mac_link_up = felix_phylink_mac_link_up,
777 .port_enable = felix_port_enable,
778 .port_disable = felix_port_disable,
779 .port_fdb_dump = felix_fdb_dump,
780 .port_fdb_add = felix_fdb_add,
781 .port_fdb_del = felix_fdb_del,
782 .port_mdb_prepare = felix_mdb_prepare,
783 .port_mdb_add = felix_mdb_add,
784 .port_mdb_del = felix_mdb_del,
785 .port_bridge_join = felix_bridge_join,
786 .port_bridge_leave = felix_bridge_leave,
787 .port_stp_state_set = felix_bridge_stp_state_set,
788 .port_vlan_prepare = felix_vlan_prepare,
789 .port_vlan_filtering = felix_vlan_filtering,
790 .port_vlan_add = felix_vlan_add,
791 .port_vlan_del = felix_vlan_del,
792 .port_hwtstamp_get = felix_hwtstamp_get,
793 .port_hwtstamp_set = felix_hwtstamp_set,
794 .port_rxtstamp = felix_rxtstamp,
795 .port_txtstamp = felix_txtstamp,
796 .port_change_mtu = felix_change_mtu,
797 .port_max_mtu = felix_get_max_mtu,
798 .port_policer_add = felix_port_policer_add,
799 .port_policer_del = felix_port_policer_del,
800 .cls_flower_add = felix_cls_flower_add,
801 .cls_flower_del = felix_cls_flower_del,
802 .cls_flower_stats = felix_cls_flower_stats,
803 .port_setup_tc = felix_port_setup_tc,
804 };
805
felix_port_to_netdev(struct ocelot * ocelot,int port)806 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
807 {
808 struct felix *felix = ocelot_to_felix(ocelot);
809 struct dsa_switch *ds = felix->ds;
810
811 if (!dsa_is_user_port(ds, port))
812 return NULL;
813
814 return dsa_to_port(ds, port)->slave;
815 }
816
felix_netdev_to_port(struct net_device * dev)817 int felix_netdev_to_port(struct net_device *dev)
818 {
819 struct dsa_port *dp;
820
821 dp = dsa_port_from_netdev(dev);
822 if (IS_ERR(dp))
823 return -EINVAL;
824
825 return dp->index;
826 }
827