xref: /linux/drivers/net/dsa/ocelot/felix.c (revision 397692eab35cbbd83681880c6a2dbcdb9fd84386)
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_qsys.h>
6 #include <soc/mscc/ocelot_sys.h>
7 #include <soc/mscc/ocelot_dev.h>
8 #include <soc/mscc/ocelot_ana.h>
9 #include <soc/mscc/ocelot.h>
10 #include <linux/packing.h>
11 #include <linux/module.h>
12 #include <linux/of_net.h>
13 #include <linux/pci.h>
14 #include <linux/of.h>
15 #include <net/dsa.h>
16 #include "felix.h"
17 
18 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
19 						    int port,
20 						    enum dsa_tag_protocol mp)
21 {
22 	return DSA_TAG_PROTO_OCELOT;
23 }
24 
25 static int felix_set_ageing_time(struct dsa_switch *ds,
26 				 unsigned int ageing_time)
27 {
28 	struct ocelot *ocelot = ds->priv;
29 
30 	ocelot_set_ageing_time(ocelot, ageing_time);
31 
32 	return 0;
33 }
34 
35 static int felix_fdb_dump(struct dsa_switch *ds, int port,
36 			  dsa_fdb_dump_cb_t *cb, void *data)
37 {
38 	struct ocelot *ocelot = ds->priv;
39 
40 	return ocelot_fdb_dump(ocelot, port, cb, data);
41 }
42 
43 static int felix_fdb_add(struct dsa_switch *ds, int port,
44 			 const unsigned char *addr, u16 vid)
45 {
46 	struct ocelot *ocelot = ds->priv;
47 	bool vlan_aware;
48 
49 	vlan_aware = dsa_port_is_vlan_filtering(dsa_to_port(ds, port));
50 
51 	return ocelot_fdb_add(ocelot, port, addr, vid, vlan_aware);
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->ops		= felix->info->ops;
405 
406 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
407 				 GFP_KERNEL);
408 	if (!port_phy_modes)
409 		return -ENOMEM;
410 
411 	err = felix_parse_dt(felix, port_phy_modes);
412 	if (err) {
413 		kfree(port_phy_modes);
414 		return err;
415 	}
416 
417 	switch_base = pci_resource_start(felix->pdev,
418 					 felix->info->switch_pci_bar);
419 
420 	for (i = 0; i < TARGET_MAX; i++) {
421 		struct regmap *target;
422 		struct resource *res;
423 
424 		if (!felix->info->target_io_res[i].name)
425 			continue;
426 
427 		res = &felix->info->target_io_res[i];
428 		res->flags = IORESOURCE_MEM;
429 		res->start += switch_base;
430 		res->end += switch_base;
431 
432 		target = ocelot_regmap_init(ocelot, res);
433 		if (IS_ERR(target)) {
434 			dev_err(ocelot->dev,
435 				"Failed to map device memory space\n");
436 			kfree(port_phy_modes);
437 			return PTR_ERR(target);
438 		}
439 
440 		ocelot->targets[i] = target;
441 	}
442 
443 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
444 	if (err) {
445 		dev_err(ocelot->dev, "failed to init reg fields map\n");
446 		kfree(port_phy_modes);
447 		return err;
448 	}
449 
450 	for (port = 0; port < num_phys_ports; port++) {
451 		struct ocelot_port *ocelot_port;
452 		void __iomem *port_regs;
453 		struct resource *res;
454 
455 		ocelot_port = devm_kzalloc(ocelot->dev,
456 					   sizeof(struct ocelot_port),
457 					   GFP_KERNEL);
458 		if (!ocelot_port) {
459 			dev_err(ocelot->dev,
460 				"failed to allocate port memory\n");
461 			kfree(port_phy_modes);
462 			return -ENOMEM;
463 		}
464 
465 		res = &felix->info->port_io_res[port];
466 		res->flags = IORESOURCE_MEM;
467 		res->start += switch_base;
468 		res->end += switch_base;
469 
470 		port_regs = devm_ioremap_resource(ocelot->dev, res);
471 		if (IS_ERR(port_regs)) {
472 			dev_err(ocelot->dev,
473 				"failed to map registers for port %d\n", port);
474 			kfree(port_phy_modes);
475 			return PTR_ERR(port_regs);
476 		}
477 
478 		ocelot_port->phy_mode = port_phy_modes[port];
479 		ocelot_port->ocelot = ocelot;
480 		ocelot_port->regs = port_regs;
481 		ocelot->ports[port] = ocelot_port;
482 	}
483 
484 	kfree(port_phy_modes);
485 
486 	if (felix->info->mdio_bus_alloc) {
487 		err = felix->info->mdio_bus_alloc(ocelot);
488 		if (err < 0)
489 			return err;
490 	}
491 
492 	return 0;
493 }
494 
495 /* Hardware initialization done here so that we can allocate structures with
496  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
497  * us to allocate structures twice (leak memory) and map PCI memory twice
498  * (which will not work).
499  */
500 static int felix_setup(struct dsa_switch *ds)
501 {
502 	struct ocelot *ocelot = ds->priv;
503 	struct felix *felix = ocelot_to_felix(ocelot);
504 	int port, err;
505 
506 	err = felix_init_structs(felix, ds->num_ports);
507 	if (err)
508 		return err;
509 
510 	ocelot_init(ocelot);
511 
512 	for (port = 0; port < ds->num_ports; port++) {
513 		ocelot_init_port(ocelot, port);
514 
515 		if (dsa_is_cpu_port(ds, port))
516 			ocelot_set_cpu_port(ocelot, port,
517 					    OCELOT_TAG_PREFIX_NONE,
518 					    OCELOT_TAG_PREFIX_LONG);
519 	}
520 
521 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
522 	 * isn't instantiated for the Felix PF.
523 	 * In-band AN may take a few ms to complete, so we need to poll.
524 	 */
525 	ds->pcs_poll = true;
526 
527 	return 0;
528 }
529 
530 static void felix_teardown(struct dsa_switch *ds)
531 {
532 	struct ocelot *ocelot = ds->priv;
533 	struct felix *felix = ocelot_to_felix(ocelot);
534 
535 	if (felix->info->mdio_bus_free)
536 		felix->info->mdio_bus_free(ocelot);
537 
538 	/* stop workqueue thread */
539 	ocelot_deinit(ocelot);
540 }
541 
542 static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
543 			      struct ifreq *ifr)
544 {
545 	struct ocelot *ocelot = ds->priv;
546 
547 	return ocelot_hwstamp_get(ocelot, port, ifr);
548 }
549 
550 static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
551 			      struct ifreq *ifr)
552 {
553 	struct ocelot *ocelot = ds->priv;
554 
555 	return ocelot_hwstamp_set(ocelot, port, ifr);
556 }
557 
558 static bool felix_rxtstamp(struct dsa_switch *ds, int port,
559 			   struct sk_buff *skb, unsigned int type)
560 {
561 	struct skb_shared_hwtstamps *shhwtstamps;
562 	struct ocelot *ocelot = ds->priv;
563 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
564 	u32 tstamp_lo, tstamp_hi;
565 	struct timespec64 ts;
566 	u64 tstamp, val;
567 
568 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
569 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
570 
571 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
572 	tstamp_lo = (u32)val;
573 
574 	tstamp_hi = tstamp >> 32;
575 	if ((tstamp & 0xffffffff) < tstamp_lo)
576 		tstamp_hi--;
577 
578 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
579 
580 	shhwtstamps = skb_hwtstamps(skb);
581 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
582 	shhwtstamps->hwtstamp = tstamp;
583 	return false;
584 }
585 
586 static bool felix_txtstamp(struct dsa_switch *ds, int port,
587 			   struct sk_buff *clone, unsigned int type)
588 {
589 	struct ocelot *ocelot = ds->priv;
590 	struct ocelot_port *ocelot_port = ocelot->ports[port];
591 
592 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
593 		return true;
594 
595 	return false;
596 }
597 
598 static const struct dsa_switch_ops felix_switch_ops = {
599 	.get_tag_protocol	= felix_get_tag_protocol,
600 	.setup			= felix_setup,
601 	.teardown		= felix_teardown,
602 	.set_ageing_time	= felix_set_ageing_time,
603 	.get_strings		= felix_get_strings,
604 	.get_ethtool_stats	= felix_get_ethtool_stats,
605 	.get_sset_count		= felix_get_sset_count,
606 	.get_ts_info		= felix_get_ts_info,
607 	.phylink_validate	= felix_phylink_validate,
608 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
609 	.phylink_mac_config	= felix_phylink_mac_config,
610 	.phylink_mac_an_restart	= felix_phylink_mac_an_restart,
611 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
612 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
613 	.port_enable		= felix_port_enable,
614 	.port_disable		= felix_port_disable,
615 	.port_fdb_dump		= felix_fdb_dump,
616 	.port_fdb_add		= felix_fdb_add,
617 	.port_fdb_del		= felix_fdb_del,
618 	.port_bridge_join	= felix_bridge_join,
619 	.port_bridge_leave	= felix_bridge_leave,
620 	.port_stp_state_set	= felix_bridge_stp_state_set,
621 	.port_vlan_prepare	= felix_vlan_prepare,
622 	.port_vlan_filtering	= felix_vlan_filtering,
623 	.port_vlan_add		= felix_vlan_add,
624 	.port_vlan_del		= felix_vlan_del,
625 	.port_hwtstamp_get	= felix_hwtstamp_get,
626 	.port_hwtstamp_set	= felix_hwtstamp_set,
627 	.port_rxtstamp		= felix_rxtstamp,
628 	.port_txtstamp		= felix_txtstamp,
629 };
630 
631 static struct felix_info *felix_instance_tbl[] = {
632 	[FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
633 };
634 
635 static irqreturn_t felix_irq_handler(int irq, void *data)
636 {
637 	struct ocelot *ocelot = (struct ocelot *)data;
638 
639 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
640 	 * and preemption status change interrupt on each port.
641 	 *
642 	 * - Get txtstamp if have
643 	 * - TODO: handle preemption. Without handling it, driver may get
644 	 *   interrupt storm.
645 	 */
646 
647 	ocelot_get_txtstamp(ocelot);
648 
649 	return IRQ_HANDLED;
650 }
651 
652 static int felix_pci_probe(struct pci_dev *pdev,
653 			   const struct pci_device_id *id)
654 {
655 	enum felix_instance instance = id->driver_data;
656 	struct dsa_switch *ds;
657 	struct ocelot *ocelot;
658 	struct felix *felix;
659 	int err;
660 
661 	err = pci_enable_device(pdev);
662 	if (err) {
663 		dev_err(&pdev->dev, "device enable failed\n");
664 		goto err_pci_enable;
665 	}
666 
667 	/* set up for high or low dma */
668 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
669 	if (err) {
670 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
671 		if (err) {
672 			dev_err(&pdev->dev,
673 				"DMA configuration failed: 0x%x\n", err);
674 			goto err_dma;
675 		}
676 	}
677 
678 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
679 	if (!felix) {
680 		err = -ENOMEM;
681 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
682 		goto err_alloc_felix;
683 	}
684 
685 	pci_set_drvdata(pdev, felix);
686 	ocelot = &felix->ocelot;
687 	ocelot->dev = &pdev->dev;
688 	felix->pdev = pdev;
689 	felix->info = felix_instance_tbl[instance];
690 
691 	pci_set_master(pdev);
692 
693 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
694 					&felix_irq_handler, IRQF_ONESHOT,
695 					"felix-intb", ocelot);
696 	if (err) {
697 		dev_err(&pdev->dev, "Failed to request irq\n");
698 		goto err_alloc_irq;
699 	}
700 
701 	ocelot->ptp = 1;
702 
703 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
704 	if (!ds) {
705 		err = -ENOMEM;
706 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
707 		goto err_alloc_ds;
708 	}
709 
710 	ds->dev = &pdev->dev;
711 	ds->num_ports = felix->info->num_ports;
712 	ds->ops = &felix_switch_ops;
713 	ds->priv = ocelot;
714 	felix->ds = ds;
715 
716 	err = dsa_register_switch(ds);
717 	if (err) {
718 		dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
719 		goto err_register_ds;
720 	}
721 
722 	return 0;
723 
724 err_register_ds:
725 	kfree(ds);
726 err_alloc_ds:
727 err_alloc_irq:
728 err_alloc_felix:
729 	kfree(felix);
730 err_dma:
731 	pci_disable_device(pdev);
732 err_pci_enable:
733 	return err;
734 }
735 
736 static void felix_pci_remove(struct pci_dev *pdev)
737 {
738 	struct felix *felix;
739 
740 	felix = pci_get_drvdata(pdev);
741 
742 	dsa_unregister_switch(felix->ds);
743 
744 	kfree(felix->ds);
745 	kfree(felix);
746 
747 	pci_disable_device(pdev);
748 }
749 
750 static struct pci_device_id felix_ids[] = {
751 	{
752 		/* NXP LS1028A */
753 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
754 		.driver_data = FELIX_INSTANCE_VSC9959,
755 	},
756 	{ 0, }
757 };
758 MODULE_DEVICE_TABLE(pci, felix_ids);
759 
760 static struct pci_driver felix_pci_driver = {
761 	.name		= KBUILD_MODNAME,
762 	.id_table	= felix_ids,
763 	.probe		= felix_pci_probe,
764 	.remove		= felix_pci_remove,
765 };
766 
767 module_pci_driver(felix_pci_driver);
768 
769 MODULE_DESCRIPTION("Felix Switch driver");
770 MODULE_LICENSE("GPL v2");
771