xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision f8523d0e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 NXP Semiconductors
3  */
4 #include <uapi/linux/if_bridge.h>
5 #include <soc/mscc/ocelot_vcap.h>
6 #include <soc/mscc/ocelot_qsys.h>
7 #include <soc/mscc/ocelot_sys.h>
8 #include <soc/mscc/ocelot_dev.h>
9 #include <soc/mscc/ocelot_ana.h>
10 #include <soc/mscc/ocelot_ptp.h>
11 #include <soc/mscc/ocelot.h>
12 #include <linux/packing.h>
13 #include <linux/module.h>
14 #include <linux/of_net.h>
15 #include <linux/pci.h>
16 #include <linux/of.h>
17 #include <net/pkt_sched.h>
18 #include <net/dsa.h>
19 #include "felix.h"
20 
21 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
22 						    int port,
23 						    enum dsa_tag_protocol mp)
24 {
25 	return DSA_TAG_PROTO_OCELOT;
26 }
27 
28 static int felix_set_ageing_time(struct dsa_switch *ds,
29 				 unsigned int ageing_time)
30 {
31 	struct ocelot *ocelot = ds->priv;
32 
33 	ocelot_set_ageing_time(ocelot, ageing_time);
34 
35 	return 0;
36 }
37 
38 static int felix_fdb_dump(struct dsa_switch *ds, int port,
39 			  dsa_fdb_dump_cb_t *cb, void *data)
40 {
41 	struct ocelot *ocelot = ds->priv;
42 
43 	return ocelot_fdb_dump(ocelot, port, cb, data);
44 }
45 
46 static int felix_fdb_add(struct dsa_switch *ds, int port,
47 			 const unsigned char *addr, u16 vid)
48 {
49 	struct ocelot *ocelot = ds->priv;
50 
51 	return ocelot_fdb_add(ocelot, port, addr, vid);
52 }
53 
54 static int felix_fdb_del(struct dsa_switch *ds, int port,
55 			 const unsigned char *addr, u16 vid)
56 {
57 	struct ocelot *ocelot = ds->priv;
58 
59 	return ocelot_fdb_del(ocelot, port, addr, vid);
60 }
61 
62 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
63 				       u8 state)
64 {
65 	struct ocelot *ocelot = ds->priv;
66 
67 	return ocelot_bridge_stp_state_set(ocelot, port, state);
68 }
69 
70 static int felix_bridge_join(struct dsa_switch *ds, int port,
71 			     struct net_device *br)
72 {
73 	struct ocelot *ocelot = ds->priv;
74 
75 	return ocelot_port_bridge_join(ocelot, port, br);
76 }
77 
78 static void felix_bridge_leave(struct dsa_switch *ds, int port,
79 			       struct net_device *br)
80 {
81 	struct ocelot *ocelot = ds->priv;
82 
83 	ocelot_port_bridge_leave(ocelot, port, br);
84 }
85 
86 /* This callback needs to be present */
87 static int felix_vlan_prepare(struct dsa_switch *ds, int port,
88 			      const struct switchdev_obj_port_vlan *vlan)
89 {
90 	return 0;
91 }
92 
93 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
94 {
95 	struct ocelot *ocelot = ds->priv;
96 
97 	ocelot_port_vlan_filtering(ocelot, port, enabled);
98 
99 	return 0;
100 }
101 
102 static void felix_vlan_add(struct dsa_switch *ds, int port,
103 			   const struct switchdev_obj_port_vlan *vlan)
104 {
105 	struct ocelot *ocelot = ds->priv;
106 	u16 flags = vlan->flags;
107 	u16 vid;
108 	int err;
109 
110 	if (dsa_is_cpu_port(ds, port))
111 		flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
112 
113 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
114 		err = ocelot_vlan_add(ocelot, port, vid,
115 				      flags & BRIDGE_VLAN_INFO_PVID,
116 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
117 		if (err) {
118 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
119 				vid, port, err);
120 			return;
121 		}
122 	}
123 }
124 
125 static int felix_vlan_del(struct dsa_switch *ds, int port,
126 			  const struct switchdev_obj_port_vlan *vlan)
127 {
128 	struct ocelot *ocelot = ds->priv;
129 	u16 vid;
130 	int err;
131 
132 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
133 		err = ocelot_vlan_del(ocelot, port, vid);
134 		if (err) {
135 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
136 				vid, port, err);
137 			return err;
138 		}
139 	}
140 	return 0;
141 }
142 
143 static int felix_port_enable(struct dsa_switch *ds, int port,
144 			     struct phy_device *phy)
145 {
146 	struct ocelot *ocelot = ds->priv;
147 
148 	ocelot_port_enable(ocelot, port, phy);
149 
150 	return 0;
151 }
152 
153 static void felix_port_disable(struct dsa_switch *ds, int port)
154 {
155 	struct ocelot *ocelot = ds->priv;
156 
157 	return ocelot_port_disable(ocelot, port);
158 }
159 
160 static void felix_phylink_validate(struct dsa_switch *ds, int port,
161 				   unsigned long *supported,
162 				   struct phylink_link_state *state)
163 {
164 	struct ocelot *ocelot = ds->priv;
165 	struct ocelot_port *ocelot_port = ocelot->ports[port];
166 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
167 
168 	if (state->interface != PHY_INTERFACE_MODE_NA &&
169 	    state->interface != ocelot_port->phy_mode) {
170 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
171 		return;
172 	}
173 
174 	/* No half-duplex. */
175 	phylink_set_port_modes(mask);
176 	phylink_set(mask, Autoneg);
177 	phylink_set(mask, Pause);
178 	phylink_set(mask, Asym_Pause);
179 	phylink_set(mask, 10baseT_Full);
180 	phylink_set(mask, 100baseT_Full);
181 	phylink_set(mask, 1000baseT_Full);
182 
183 	if (state->interface == PHY_INTERFACE_MODE_INTERNAL ||
184 	    state->interface == PHY_INTERFACE_MODE_2500BASEX ||
185 	    state->interface == PHY_INTERFACE_MODE_USXGMII) {
186 		phylink_set(mask, 2500baseT_Full);
187 		phylink_set(mask, 2500baseX_Full);
188 	}
189 
190 	bitmap_and(supported, supported, mask,
191 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
192 	bitmap_and(state->advertising, state->advertising, mask,
193 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
194 }
195 
196 static int felix_phylink_mac_pcs_get_state(struct dsa_switch *ds, int port,
197 					   struct phylink_link_state *state)
198 {
199 	struct ocelot *ocelot = ds->priv;
200 	struct felix *felix = ocelot_to_felix(ocelot);
201 
202 	if (felix->info->pcs_link_state)
203 		felix->info->pcs_link_state(ocelot, port, state);
204 
205 	return 0;
206 }
207 
208 static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
209 				     unsigned int link_an_mode,
210 				     const struct phylink_link_state *state)
211 {
212 	struct ocelot *ocelot = ds->priv;
213 	struct ocelot_port *ocelot_port = ocelot->ports[port];
214 	struct felix *felix = ocelot_to_felix(ocelot);
215 	u32 mac_fc_cfg;
216 
217 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
218 	 * PORT_RST bits in CLOCK_CFG
219 	 */
220 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(state->speed),
221 			   DEV_CLOCK_CFG);
222 
223 	/* Flow control. Link speed is only used here to evaluate the time
224 	 * specification in incoming pause frames.
225 	 */
226 	mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(state->speed);
227 
228 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
229 	 * adaptation.
230 	 */
231 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
232 
233 	if (state->pause & MLO_PAUSE_TX)
234 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
235 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
236 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
237 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
238 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
239 
240 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
241 
242 	if (felix->info->pcs_init)
243 		felix->info->pcs_init(ocelot, port, link_an_mode, state);
244 
245 	if (felix->info->port_sched_speed_set)
246 		felix->info->port_sched_speed_set(ocelot, port,
247 						  state->speed);
248 }
249 
250 static void felix_phylink_mac_an_restart(struct dsa_switch *ds, int port)
251 {
252 	struct ocelot *ocelot = ds->priv;
253 	struct felix *felix = ocelot_to_felix(ocelot);
254 
255 	if (felix->info->pcs_an_restart)
256 		felix->info->pcs_an_restart(ocelot, port);
257 }
258 
259 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
260 					unsigned int link_an_mode,
261 					phy_interface_t interface)
262 {
263 	struct ocelot *ocelot = ds->priv;
264 	struct ocelot_port *ocelot_port = ocelot->ports[port];
265 
266 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
267 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
268 		       QSYS_SWITCH_PORT_MODE, port);
269 }
270 
271 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
272 				      unsigned int link_an_mode,
273 				      phy_interface_t interface,
274 				      struct phy_device *phydev,
275 				      int speed, int duplex,
276 				      bool tx_pause, bool rx_pause)
277 {
278 	struct ocelot *ocelot = ds->priv;
279 	struct ocelot_port *ocelot_port = ocelot->ports[port];
280 
281 	/* Enable MAC module */
282 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
283 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
284 
285 	/* Enable receiving frames on the port, and activate auto-learning of
286 	 * MAC addresses.
287 	 */
288 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
289 			 ANA_PORT_PORT_CFG_RECV_ENA |
290 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
291 			 ANA_PORT_PORT_CFG, port);
292 
293 	/* Core: Enable port for frame transfer */
294 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
295 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
296 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
297 			 QSYS_SWITCH_PORT_MODE, port);
298 }
299 
300 static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
301 {
302 	int i;
303 
304 	ocelot_rmw_gix(ocelot,
305 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
306 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
307 		       ANA_PORT_QOS_CFG,
308 		       port);
309 
310 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
311 		ocelot_rmw_ix(ocelot,
312 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
313 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
314 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
315 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
316 			      ANA_PORT_PCP_DEI_MAP,
317 			      port, i);
318 	}
319 }
320 
321 static void felix_get_strings(struct dsa_switch *ds, int port,
322 			      u32 stringset, u8 *data)
323 {
324 	struct ocelot *ocelot = ds->priv;
325 
326 	return ocelot_get_strings(ocelot, port, stringset, data);
327 }
328 
329 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
330 {
331 	struct ocelot *ocelot = ds->priv;
332 
333 	ocelot_get_ethtool_stats(ocelot, port, data);
334 }
335 
336 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
337 {
338 	struct ocelot *ocelot = ds->priv;
339 
340 	return ocelot_get_sset_count(ocelot, port, sset);
341 }
342 
343 static int felix_get_ts_info(struct dsa_switch *ds, int port,
344 			     struct ethtool_ts_info *info)
345 {
346 	struct ocelot *ocelot = ds->priv;
347 
348 	return ocelot_get_ts_info(ocelot, port, info);
349 }
350 
351 static int felix_parse_ports_node(struct felix *felix,
352 				  struct device_node *ports_node,
353 				  phy_interface_t *port_phy_modes)
354 {
355 	struct ocelot *ocelot = &felix->ocelot;
356 	struct device *dev = felix->ocelot.dev;
357 	struct device_node *child;
358 
359 	for_each_available_child_of_node(ports_node, child) {
360 		phy_interface_t phy_mode;
361 		u32 port;
362 		int err;
363 
364 		/* Get switch port number from DT */
365 		if (of_property_read_u32(child, "reg", &port) < 0) {
366 			dev_err(dev, "Port number not defined in device tree "
367 				"(property \"reg\")\n");
368 			of_node_put(child);
369 			return -ENODEV;
370 		}
371 
372 		/* Get PHY mode from DT */
373 		err = of_get_phy_mode(child, &phy_mode);
374 		if (err) {
375 			dev_err(dev, "Failed to read phy-mode or "
376 				"phy-interface-type property for port %d\n",
377 				port);
378 			of_node_put(child);
379 			return -ENODEV;
380 		}
381 
382 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
383 		if (err < 0) {
384 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
385 				phy_modes(phy_mode), port);
386 			return err;
387 		}
388 
389 		port_phy_modes[port] = phy_mode;
390 	}
391 
392 	return 0;
393 }
394 
395 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
396 {
397 	struct device *dev = felix->ocelot.dev;
398 	struct device_node *switch_node;
399 	struct device_node *ports_node;
400 	int err;
401 
402 	switch_node = dev->of_node;
403 
404 	ports_node = of_get_child_by_name(switch_node, "ports");
405 	if (!ports_node) {
406 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
407 		return -ENODEV;
408 	}
409 
410 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
411 	of_node_put(ports_node);
412 
413 	return err;
414 }
415 
416 static int felix_init_structs(struct felix *felix, int num_phys_ports)
417 {
418 	struct ocelot *ocelot = &felix->ocelot;
419 	phy_interface_t *port_phy_modes;
420 	resource_size_t switch_base;
421 	struct resource res;
422 	int port, i, err;
423 
424 	ocelot->num_phys_ports = num_phys_ports;
425 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
426 				     sizeof(struct ocelot_port *), GFP_KERNEL);
427 	if (!ocelot->ports)
428 		return -ENOMEM;
429 
430 	ocelot->map		= felix->info->map;
431 	ocelot->stats_layout	= felix->info->stats_layout;
432 	ocelot->num_stats	= felix->info->num_stats;
433 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
434 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
435 	ocelot->vcap_is2_keys	= felix->info->vcap_is2_keys;
436 	ocelot->vcap_is2_actions= felix->info->vcap_is2_actions;
437 	ocelot->vcap		= felix->info->vcap;
438 	ocelot->ops		= felix->info->ops;
439 
440 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
441 				 GFP_KERNEL);
442 	if (!port_phy_modes)
443 		return -ENOMEM;
444 
445 	err = felix_parse_dt(felix, port_phy_modes);
446 	if (err) {
447 		kfree(port_phy_modes);
448 		return err;
449 	}
450 
451 	switch_base = pci_resource_start(felix->pdev,
452 					 felix->info->switch_pci_bar);
453 
454 	for (i = 0; i < TARGET_MAX; i++) {
455 		struct regmap *target;
456 
457 		if (!felix->info->target_io_res[i].name)
458 			continue;
459 
460 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
461 		res.flags = IORESOURCE_MEM;
462 		res.start += switch_base;
463 		res.end += switch_base;
464 
465 		target = ocelot_regmap_init(ocelot, &res);
466 		if (IS_ERR(target)) {
467 			dev_err(ocelot->dev,
468 				"Failed to map device memory space\n");
469 			kfree(port_phy_modes);
470 			return PTR_ERR(target);
471 		}
472 
473 		ocelot->targets[i] = target;
474 	}
475 
476 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
477 	if (err) {
478 		dev_err(ocelot->dev, "failed to init reg fields map\n");
479 		kfree(port_phy_modes);
480 		return err;
481 	}
482 
483 	for (port = 0; port < num_phys_ports; port++) {
484 		struct ocelot_port *ocelot_port;
485 		void __iomem *port_regs;
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 += switch_base;
500 		res.end += switch_base;
501 
502 		port_regs = devm_ioremap_resource(ocelot->dev, &res);
503 		if (IS_ERR(port_regs)) {
504 			dev_err(ocelot->dev,
505 				"failed to map registers for port %d\n", port);
506 			kfree(port_phy_modes);
507 			return PTR_ERR(port_regs);
508 		}
509 
510 		ocelot_port->phy_mode = port_phy_modes[port];
511 		ocelot_port->ocelot = ocelot;
512 		ocelot_port->regs = port_regs;
513 		ocelot->ports[port] = ocelot_port;
514 	}
515 
516 	kfree(port_phy_modes);
517 
518 	if (felix->info->mdio_bus_alloc) {
519 		err = felix->info->mdio_bus_alloc(ocelot);
520 		if (err < 0)
521 			return err;
522 	}
523 
524 	return 0;
525 }
526 
527 static struct ptp_clock_info ocelot_ptp_clock_info = {
528 	.owner		= THIS_MODULE,
529 	.name		= "felix ptp",
530 	.max_adj	= 0x7fffffff,
531 	.n_alarm	= 0,
532 	.n_ext_ts	= 0,
533 	.n_per_out	= OCELOT_PTP_PINS_NUM,
534 	.n_pins		= OCELOT_PTP_PINS_NUM,
535 	.pps		= 0,
536 	.gettime64	= ocelot_ptp_gettime64,
537 	.settime64	= ocelot_ptp_settime64,
538 	.adjtime	= ocelot_ptp_adjtime,
539 	.adjfine	= ocelot_ptp_adjfine,
540 	.verify		= ocelot_ptp_verify,
541 	.enable		= ocelot_ptp_enable,
542 };
543 
544 /* Hardware initialization done here so that we can allocate structures with
545  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
546  * us to allocate structures twice (leak memory) and map PCI memory twice
547  * (which will not work).
548  */
549 static int felix_setup(struct dsa_switch *ds)
550 {
551 	struct ocelot *ocelot = ds->priv;
552 	struct felix *felix = ocelot_to_felix(ocelot);
553 	int port, err;
554 	int tc;
555 
556 	err = felix_init_structs(felix, ds->num_ports);
557 	if (err)
558 		return err;
559 
560 	ocelot_init(ocelot);
561 	if (ocelot->ptp) {
562 		err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
563 		if (err) {
564 			dev_err(ocelot->dev,
565 				"Timestamp initialization failed\n");
566 			ocelot->ptp = 0;
567 		}
568 	}
569 
570 	for (port = 0; port < ds->num_ports; port++) {
571 		ocelot_init_port(ocelot, port);
572 
573 		/* Bring up the CPU port module and configure the NPI port */
574 		if (dsa_is_cpu_port(ds, port))
575 			ocelot_configure_cpu(ocelot, port,
576 					     OCELOT_TAG_PREFIX_NONE,
577 					     OCELOT_TAG_PREFIX_LONG);
578 
579 		/* Set the default QoS Classification based on PCP and DEI
580 		 * bits of vlan tag.
581 		 */
582 		felix_port_qos_map_init(ocelot, port);
583 	}
584 
585 	/* Include the CPU port module in the forwarding mask for unknown
586 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
587 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
588 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
589 	 */
590 	ocelot_write_rix(ocelot,
591 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
592 			 ANA_PGID_PGID, PGID_UC);
593 	/* Setup the per-traffic class flooding PGIDs */
594 	for (tc = 0; tc < FELIX_NUM_TC; tc++)
595 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
596 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
597 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
598 				 ANA_FLOODING, tc);
599 
600 	ds->mtu_enforcement_ingress = true;
601 	ds->configure_vlan_while_not_filtering = true;
602 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
603 	 * isn't instantiated for the Felix PF.
604 	 * In-band AN may take a few ms to complete, so we need to poll.
605 	 */
606 	ds->pcs_poll = true;
607 
608 	return 0;
609 }
610 
611 static void felix_teardown(struct dsa_switch *ds)
612 {
613 	struct ocelot *ocelot = ds->priv;
614 	struct felix *felix = ocelot_to_felix(ocelot);
615 
616 	if (felix->info->mdio_bus_free)
617 		felix->info->mdio_bus_free(ocelot);
618 
619 	ocelot_deinit_timestamp(ocelot);
620 	/* stop workqueue thread */
621 	ocelot_deinit(ocelot);
622 }
623 
624 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
625 			      struct ifreq *ifr)
626 {
627 	struct ocelot *ocelot = ds->priv;
628 
629 	return ocelot_hwstamp_get(ocelot, port, ifr);
630 }
631 
632 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
633 			      struct ifreq *ifr)
634 {
635 	struct ocelot *ocelot = ds->priv;
636 
637 	return ocelot_hwstamp_set(ocelot, port, ifr);
638 }
639 
640 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
641 			   struct sk_buff *skb, unsigned int type)
642 {
643 	struct skb_shared_hwtstamps *shhwtstamps;
644 	struct ocelot *ocelot = ds->priv;
645 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
646 	u32 tstamp_lo, tstamp_hi;
647 	struct timespec64 ts;
648 	u64 tstamp, val;
649 
650 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
651 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
652 
653 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
654 	tstamp_lo = (u32)val;
655 
656 	tstamp_hi = tstamp >> 32;
657 	if ((tstamp & 0xffffffff) < tstamp_lo)
658 		tstamp_hi--;
659 
660 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
661 
662 	shhwtstamps = skb_hwtstamps(skb);
663 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
664 	shhwtstamps->hwtstamp = tstamp;
665 	return false;
666 }
667 
668 static bool felix_txtstamp(struct dsa_switch *ds, int port,
669 			   struct sk_buff *clone, unsigned int type)
670 {
671 	struct ocelot *ocelot = ds->priv;
672 	struct ocelot_port *ocelot_port = ocelot->ports[port];
673 
674 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
675 		return true;
676 
677 	return false;
678 }
679 
680 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
681 {
682 	struct ocelot *ocelot = ds->priv;
683 
684 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
685 
686 	return 0;
687 }
688 
689 static int felix_get_max_mtu(struct dsa_switch *ds, int port)
690 {
691 	struct ocelot *ocelot = ds->priv;
692 
693 	return ocelot_get_max_mtu(ocelot, port);
694 }
695 
696 static int felix_cls_flower_add(struct dsa_switch *ds, int port,
697 				struct flow_cls_offload *cls, bool ingress)
698 {
699 	struct ocelot *ocelot = ds->priv;
700 
701 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
702 }
703 
704 static int felix_cls_flower_del(struct dsa_switch *ds, int port,
705 				struct flow_cls_offload *cls, bool ingress)
706 {
707 	struct ocelot *ocelot = ds->priv;
708 
709 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
710 }
711 
712 static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
713 				  struct flow_cls_offload *cls, bool ingress)
714 {
715 	struct ocelot *ocelot = ds->priv;
716 
717 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
718 }
719 
720 static int felix_port_policer_add(struct dsa_switch *ds, int port,
721 				  struct dsa_mall_policer_tc_entry *policer)
722 {
723 	struct ocelot *ocelot = ds->priv;
724 	struct ocelot_policer pol = {
725 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
726 		.burst = div_u64(policer->rate_bytes_per_sec *
727 				 PSCHED_NS2TICKS(policer->burst),
728 				 PSCHED_TICKS_PER_SEC),
729 	};
730 
731 	return ocelot_port_policer_add(ocelot, port, &pol);
732 }
733 
734 static void felix_port_policer_del(struct dsa_switch *ds, int port)
735 {
736 	struct ocelot *ocelot = ds->priv;
737 
738 	ocelot_port_policer_del(ocelot, port);
739 }
740 
741 static int felix_port_setup_tc(struct dsa_switch *ds, int port,
742 			       enum tc_setup_type type,
743 			       void *type_data)
744 {
745 	struct ocelot *ocelot = ds->priv;
746 	struct felix *felix = ocelot_to_felix(ocelot);
747 
748 	if (felix->info->port_setup_tc)
749 		return felix->info->port_setup_tc(ds, port, type, type_data);
750 	else
751 		return -EOPNOTSUPP;
752 }
753 
754 static const struct dsa_switch_ops felix_switch_ops = {
755 	.get_tag_protocol	= felix_get_tag_protocol,
756 	.setup			= felix_setup,
757 	.teardown		= felix_teardown,
758 	.set_ageing_time	= felix_set_ageing_time,
759 	.get_strings		= felix_get_strings,
760 	.get_ethtool_stats	= felix_get_ethtool_stats,
761 	.get_sset_count		= felix_get_sset_count,
762 	.get_ts_info		= felix_get_ts_info,
763 	.phylink_validate	= felix_phylink_validate,
764 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
765 	.phylink_mac_config	= felix_phylink_mac_config,
766 	.phylink_mac_an_restart	= felix_phylink_mac_an_restart,
767 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
768 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
769 	.port_enable		= felix_port_enable,
770 	.port_disable		= felix_port_disable,
771 	.port_fdb_dump		= felix_fdb_dump,
772 	.port_fdb_add		= felix_fdb_add,
773 	.port_fdb_del		= felix_fdb_del,
774 	.port_bridge_join	= felix_bridge_join,
775 	.port_bridge_leave	= felix_bridge_leave,
776 	.port_stp_state_set	= felix_bridge_stp_state_set,
777 	.port_vlan_prepare	= felix_vlan_prepare,
778 	.port_vlan_filtering	= felix_vlan_filtering,
779 	.port_vlan_add		= felix_vlan_add,
780 	.port_vlan_del		= felix_vlan_del,
781 	.port_hwtstamp_get	= felix_hwtstamp_get,
782 	.port_hwtstamp_set	= felix_hwtstamp_set,
783 	.port_rxtstamp		= felix_rxtstamp,
784 	.port_txtstamp		= felix_txtstamp,
785 	.port_change_mtu	= felix_change_mtu,
786 	.port_max_mtu		= felix_get_max_mtu,
787 	.port_policer_add	= felix_port_policer_add,
788 	.port_policer_del	= felix_port_policer_del,
789 	.cls_flower_add		= felix_cls_flower_add,
790 	.cls_flower_del		= felix_cls_flower_del,
791 	.cls_flower_stats	= felix_cls_flower_stats,
792 	.port_setup_tc          = felix_port_setup_tc,
793 };
794 
795 static struct felix_info *felix_instance_tbl[] = {
796 	[FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
797 };
798 
799 static irqreturn_t felix_irq_handler(int irq, void *data)
800 {
801 	struct ocelot *ocelot = (struct ocelot *)data;
802 
803 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
804 	 * and preemption status change interrupt on each port.
805 	 *
806 	 * - Get txtstamp if have
807 	 * - TODO: handle preemption. Without handling it, driver may get
808 	 *   interrupt storm.
809 	 */
810 
811 	ocelot_get_txtstamp(ocelot);
812 
813 	return IRQ_HANDLED;
814 }
815 
816 static int felix_pci_probe(struct pci_dev *pdev,
817 			   const struct pci_device_id *id)
818 {
819 	enum felix_instance instance = id->driver_data;
820 	struct dsa_switch *ds;
821 	struct ocelot *ocelot;
822 	struct felix *felix;
823 	int err;
824 
825 	if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) {
826 		dev_info(&pdev->dev, "device is disabled, skipping\n");
827 		return -ENODEV;
828 	}
829 
830 	err = pci_enable_device(pdev);
831 	if (err) {
832 		dev_err(&pdev->dev, "device enable failed\n");
833 		goto err_pci_enable;
834 	}
835 
836 	/* set up for high or low dma */
837 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
838 	if (err) {
839 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
840 		if (err) {
841 			dev_err(&pdev->dev,
842 				"DMA configuration failed: 0x%x\n", err);
843 			goto err_dma;
844 		}
845 	}
846 
847 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
848 	if (!felix) {
849 		err = -ENOMEM;
850 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
851 		goto err_alloc_felix;
852 	}
853 
854 	pci_set_drvdata(pdev, felix);
855 	ocelot = &felix->ocelot;
856 	ocelot->dev = &pdev->dev;
857 	felix->pdev = pdev;
858 	felix->info = felix_instance_tbl[instance];
859 
860 	pci_set_master(pdev);
861 
862 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
863 					&felix_irq_handler, IRQF_ONESHOT,
864 					"felix-intb", ocelot);
865 	if (err) {
866 		dev_err(&pdev->dev, "Failed to request irq\n");
867 		goto err_alloc_irq;
868 	}
869 
870 	ocelot->ptp = 1;
871 
872 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
873 	if (!ds) {
874 		err = -ENOMEM;
875 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
876 		goto err_alloc_ds;
877 	}
878 
879 	ds->dev = &pdev->dev;
880 	ds->num_ports = felix->info->num_ports;
881 	ds->num_tx_queues = felix->info->num_tx_queues;
882 	ds->ops = &felix_switch_ops;
883 	ds->priv = ocelot;
884 	felix->ds = ds;
885 
886 	err = dsa_register_switch(ds);
887 	if (err) {
888 		dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
889 		goto err_register_ds;
890 	}
891 
892 	return 0;
893 
894 err_register_ds:
895 	kfree(ds);
896 err_alloc_ds:
897 err_alloc_irq:
898 err_alloc_felix:
899 	kfree(felix);
900 err_dma:
901 	pci_disable_device(pdev);
902 err_pci_enable:
903 	return err;
904 }
905 
906 static void felix_pci_remove(struct pci_dev *pdev)
907 {
908 	struct felix *felix;
909 
910 	felix = pci_get_drvdata(pdev);
911 
912 	dsa_unregister_switch(felix->ds);
913 
914 	kfree(felix->ds);
915 	kfree(felix);
916 
917 	pci_disable_device(pdev);
918 }
919 
920 static struct pci_device_id felix_ids[] = {
921 	{
922 		/* NXP LS1028A */
923 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
924 		.driver_data = FELIX_INSTANCE_VSC9959,
925 	},
926 	{ 0, }
927 };
928 MODULE_DEVICE_TABLE(pci, felix_ids);
929 
930 static struct pci_driver felix_pci_driver = {
931 	.name		= KBUILD_MODNAME,
932 	.id_table	= felix_ids,
933 	.probe		= felix_pci_probe,
934 	.remove		= felix_pci_remove,
935 };
936 
937 module_pci_driver(felix_pci_driver);
938 
939 MODULE_DESCRIPTION("Felix Switch driver");
940 MODULE_LICENSE("GPL v2");
941