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