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