dsa: Move tagger name into its ops structure
[linux-2.6-block.git] / include / net / dsa.h
1 /*
2  * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10
11 #ifndef __LINUX_NET_DSA_H
12 #define __LINUX_NET_DSA_H
13
14 #include <linux/if.h>
15 #include <linux/if_ether.h>
16 #include <linux/list.h>
17 #include <linux/notifier.h>
18 #include <linux/timer.h>
19 #include <linux/workqueue.h>
20 #include <linux/of.h>
21 #include <linux/ethtool.h>
22 #include <linux/net_tstamp.h>
23 #include <linux/phy.h>
24 #include <linux/platform_data/dsa.h>
25 #include <net/devlink.h>
26 #include <net/switchdev.h>
27
28 struct tc_action;
29 struct phy_device;
30 struct fixed_phy_status;
31 struct phylink_link_state;
32
33 enum dsa_tag_protocol {
34         DSA_TAG_PROTO_NONE = 0,
35         DSA_TAG_PROTO_BRCM,
36         DSA_TAG_PROTO_BRCM_PREPEND,
37         DSA_TAG_PROTO_DSA,
38         DSA_TAG_PROTO_EDSA,
39         DSA_TAG_PROTO_GSWIP,
40         DSA_TAG_PROTO_KSZ9477,
41         DSA_TAG_PROTO_KSZ9893,
42         DSA_TAG_PROTO_LAN9303,
43         DSA_TAG_PROTO_MTK,
44         DSA_TAG_PROTO_QCA,
45         DSA_TAG_PROTO_TRAILER,
46         DSA_TAG_LAST,           /* MUST BE LAST */
47 };
48
49 struct packet_type;
50 struct dsa_switch;
51
52 struct dsa_device_ops {
53         struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
54         struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
55                                struct packet_type *pt);
56         int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
57                             int *offset);
58         unsigned int overhead;
59         const char *name;
60 };
61
62 struct dsa_switch_tree {
63         struct list_head        list;
64
65         /* Notifier chain for switch-wide events */
66         struct raw_notifier_head        nh;
67
68         /* Tree identifier */
69         unsigned int index;
70
71         /* Number of switches attached to this tree */
72         struct kref refcount;
73
74         /* Has this tree been applied to the hardware? */
75         bool setup;
76
77         /*
78          * Configuration data for the platform device that owns
79          * this dsa switch tree instance.
80          */
81         struct dsa_platform_data        *pd;
82
83         /*
84          * The switch port to which the CPU is attached.
85          */
86         struct dsa_port         *cpu_dp;
87
88         /*
89          * Data for the individual switch chips.
90          */
91         struct dsa_switch       *ds[DSA_MAX_SWITCHES];
92 };
93
94 /* TC matchall action types, only mirroring for now */
95 enum dsa_port_mall_action_type {
96         DSA_PORT_MALL_MIRROR,
97 };
98
99 /* TC mirroring entry */
100 struct dsa_mall_mirror_tc_entry {
101         u8 to_local_port;
102         bool ingress;
103 };
104
105 /* TC matchall entry */
106 struct dsa_mall_tc_entry {
107         struct list_head list;
108         unsigned long cookie;
109         enum dsa_port_mall_action_type type;
110         union {
111                 struct dsa_mall_mirror_tc_entry mirror;
112         };
113 };
114
115
116 struct dsa_port {
117         /* A CPU port is physically connected to a master device.
118          * A user port exposed to userspace has a slave device.
119          */
120         union {
121                 struct net_device *master;
122                 struct net_device *slave;
123         };
124
125         /* CPU port tagging operations used by master or slave devices */
126         const struct dsa_device_ops *tag_ops;
127
128         /* Copies for faster access in master receive hot path */
129         struct dsa_switch_tree *dst;
130         struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
131                                struct packet_type *pt);
132
133         enum {
134                 DSA_PORT_TYPE_UNUSED = 0,
135                 DSA_PORT_TYPE_CPU,
136                 DSA_PORT_TYPE_DSA,
137                 DSA_PORT_TYPE_USER,
138         } type;
139
140         struct dsa_switch       *ds;
141         unsigned int            index;
142         const char              *name;
143         const struct dsa_port   *cpu_dp;
144         const char              *mac;
145         struct device_node      *dn;
146         unsigned int            ageing_time;
147         u8                      stp_state;
148         struct net_device       *bridge_dev;
149         struct devlink_port     devlink_port;
150         struct phylink          *pl;
151         /*
152          * Original copy of the master netdev ethtool_ops
153          */
154         const struct ethtool_ops *orig_ethtool_ops;
155
156         /*
157          * Original copy of the master netdev net_device_ops
158          */
159         const struct net_device_ops *orig_ndo_ops;
160 };
161
162 struct dsa_switch {
163         struct device *dev;
164
165         /*
166          * Parent switch tree, and switch index.
167          */
168         struct dsa_switch_tree  *dst;
169         unsigned int            index;
170
171         /* Listener for switch fabric events */
172         struct notifier_block   nb;
173
174         /*
175          * Give the switch driver somewhere to hang its private data
176          * structure.
177          */
178         void *priv;
179
180         /*
181          * Configuration data for this switch.
182          */
183         struct dsa_chip_data    *cd;
184
185         /*
186          * The switch operations.
187          */
188         const struct dsa_switch_ops     *ops;
189
190         /*
191          * An array of which element [a] indicates which port on this
192          * switch should be used to send packets to that are destined
193          * for switch a. Can be NULL if there is only one switch chip.
194          */
195         s8              rtable[DSA_MAX_SWITCHES];
196
197         /*
198          * Slave mii_bus and devices for the individual ports.
199          */
200         u32                     phys_mii_mask;
201         struct mii_bus          *slave_mii_bus;
202
203         /* Ageing Time limits in msecs */
204         unsigned int ageing_time_min;
205         unsigned int ageing_time_max;
206
207         /* devlink used to represent this switch device */
208         struct devlink          *devlink;
209
210         /* Number of switch port queues */
211         unsigned int            num_tx_queues;
212
213         unsigned long           *bitmap;
214         unsigned long           _bitmap;
215
216         /* Dynamically allocated ports, keep last */
217         size_t num_ports;
218         struct dsa_port ports[];
219 };
220
221 static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
222 {
223         return &ds->ports[p];
224 }
225
226 static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
227 {
228         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
229 }
230
231 static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
232 {
233         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
234 }
235
236 static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
237 {
238         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
239 }
240
241 static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
242 {
243         return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
244 }
245
246 static inline u32 dsa_user_ports(struct dsa_switch *ds)
247 {
248         u32 mask = 0;
249         int p;
250
251         for (p = 0; p < ds->num_ports; p++)
252                 if (dsa_is_user_port(ds, p))
253                         mask |= BIT(p);
254
255         return mask;
256 }
257
258 /* Return the local port used to reach an arbitrary switch port */
259 static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
260                                             int port)
261 {
262         if (device == ds->index)
263                 return port;
264         else
265                 return ds->rtable[device];
266 }
267
268 /* Return the local port used to reach the dedicated CPU port */
269 static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
270 {
271         const struct dsa_port *dp = dsa_to_port(ds, port);
272         const struct dsa_port *cpu_dp = dp->cpu_dp;
273
274         if (!cpu_dp)
275                 return port;
276
277         return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
278 }
279
280 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
281                               bool is_static, void *data);
282 struct dsa_switch_ops {
283 #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
284         /*
285          * Legacy probing.
286          */
287         const char      *(*probe)(struct device *dsa_dev,
288                                   struct device *host_dev, int sw_addr,
289                                   void **priv);
290 #endif
291
292         enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
293                                                   int port);
294
295         int     (*setup)(struct dsa_switch *ds);
296         u32     (*get_phy_flags)(struct dsa_switch *ds, int port);
297
298         /*
299          * Access to the switch's PHY registers.
300          */
301         int     (*phy_read)(struct dsa_switch *ds, int port, int regnum);
302         int     (*phy_write)(struct dsa_switch *ds, int port,
303                              int regnum, u16 val);
304
305         /*
306          * Link state adjustment (called from libphy)
307          */
308         void    (*adjust_link)(struct dsa_switch *ds, int port,
309                                 struct phy_device *phydev);
310         void    (*fixed_link_update)(struct dsa_switch *ds, int port,
311                                 struct fixed_phy_status *st);
312
313         /*
314          * PHYLINK integration
315          */
316         void    (*phylink_validate)(struct dsa_switch *ds, int port,
317                                     unsigned long *supported,
318                                     struct phylink_link_state *state);
319         int     (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
320                                           struct phylink_link_state *state);
321         void    (*phylink_mac_config)(struct dsa_switch *ds, int port,
322                                       unsigned int mode,
323                                       const struct phylink_link_state *state);
324         void    (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
325         void    (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
326                                          unsigned int mode,
327                                          phy_interface_t interface);
328         void    (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
329                                        unsigned int mode,
330                                        phy_interface_t interface,
331                                        struct phy_device *phydev);
332         void    (*phylink_fixed_state)(struct dsa_switch *ds, int port,
333                                        struct phylink_link_state *state);
334         /*
335          * ethtool hardware statistics.
336          */
337         void    (*get_strings)(struct dsa_switch *ds, int port,
338                                u32 stringset, uint8_t *data);
339         void    (*get_ethtool_stats)(struct dsa_switch *ds,
340                                      int port, uint64_t *data);
341         int     (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
342         void    (*get_ethtool_phy_stats)(struct dsa_switch *ds,
343                                          int port, uint64_t *data);
344
345         /*
346          * ethtool Wake-on-LAN
347          */
348         void    (*get_wol)(struct dsa_switch *ds, int port,
349                            struct ethtool_wolinfo *w);
350         int     (*set_wol)(struct dsa_switch *ds, int port,
351                            struct ethtool_wolinfo *w);
352
353         /*
354          * ethtool timestamp info
355          */
356         int     (*get_ts_info)(struct dsa_switch *ds, int port,
357                                struct ethtool_ts_info *ts);
358
359         /*
360          * Suspend and resume
361          */
362         int     (*suspend)(struct dsa_switch *ds);
363         int     (*resume)(struct dsa_switch *ds);
364
365         /*
366          * Port enable/disable
367          */
368         int     (*port_enable)(struct dsa_switch *ds, int port,
369                                struct phy_device *phy);
370         void    (*port_disable)(struct dsa_switch *ds, int port);
371
372         /*
373          * Port's MAC EEE settings
374          */
375         int     (*set_mac_eee)(struct dsa_switch *ds, int port,
376                                struct ethtool_eee *e);
377         int     (*get_mac_eee)(struct dsa_switch *ds, int port,
378                                struct ethtool_eee *e);
379
380         /* EEPROM access */
381         int     (*get_eeprom_len)(struct dsa_switch *ds);
382         int     (*get_eeprom)(struct dsa_switch *ds,
383                               struct ethtool_eeprom *eeprom, u8 *data);
384         int     (*set_eeprom)(struct dsa_switch *ds,
385                               struct ethtool_eeprom *eeprom, u8 *data);
386
387         /*
388          * Register access.
389          */
390         int     (*get_regs_len)(struct dsa_switch *ds, int port);
391         void    (*get_regs)(struct dsa_switch *ds, int port,
392                             struct ethtool_regs *regs, void *p);
393
394         /*
395          * Bridge integration
396          */
397         int     (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
398         int     (*port_bridge_join)(struct dsa_switch *ds, int port,
399                                     struct net_device *bridge);
400         void    (*port_bridge_leave)(struct dsa_switch *ds, int port,
401                                      struct net_device *bridge);
402         void    (*port_stp_state_set)(struct dsa_switch *ds, int port,
403                                       u8 state);
404         void    (*port_fast_age)(struct dsa_switch *ds, int port);
405         int     (*port_egress_floods)(struct dsa_switch *ds, int port,
406                                       bool unicast, bool multicast);
407
408         /*
409          * VLAN support
410          */
411         int     (*port_vlan_filtering)(struct dsa_switch *ds, int port,
412                                        bool vlan_filtering);
413         int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
414                                  const struct switchdev_obj_port_vlan *vlan);
415         void (*port_vlan_add)(struct dsa_switch *ds, int port,
416                               const struct switchdev_obj_port_vlan *vlan);
417         int     (*port_vlan_del)(struct dsa_switch *ds, int port,
418                                  const struct switchdev_obj_port_vlan *vlan);
419         /*
420          * Forwarding database
421          */
422         int     (*port_fdb_add)(struct dsa_switch *ds, int port,
423                                 const unsigned char *addr, u16 vid);
424         int     (*port_fdb_del)(struct dsa_switch *ds, int port,
425                                 const unsigned char *addr, u16 vid);
426         int     (*port_fdb_dump)(struct dsa_switch *ds, int port,
427                                  dsa_fdb_dump_cb_t *cb, void *data);
428
429         /*
430          * Multicast database
431          */
432         int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
433                                 const struct switchdev_obj_port_mdb *mdb);
434         void (*port_mdb_add)(struct dsa_switch *ds, int port,
435                              const struct switchdev_obj_port_mdb *mdb);
436         int     (*port_mdb_del)(struct dsa_switch *ds, int port,
437                                 const struct switchdev_obj_port_mdb *mdb);
438         /*
439          * RXNFC
440          */
441         int     (*get_rxnfc)(struct dsa_switch *ds, int port,
442                              struct ethtool_rxnfc *nfc, u32 *rule_locs);
443         int     (*set_rxnfc)(struct dsa_switch *ds, int port,
444                              struct ethtool_rxnfc *nfc);
445
446         /*
447          * TC integration
448          */
449         int     (*port_mirror_add)(struct dsa_switch *ds, int port,
450                                    struct dsa_mall_mirror_tc_entry *mirror,
451                                    bool ingress);
452         void    (*port_mirror_del)(struct dsa_switch *ds, int port,
453                                    struct dsa_mall_mirror_tc_entry *mirror);
454
455         /*
456          * Cross-chip operations
457          */
458         int     (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
459                                          int port, struct net_device *br);
460         void    (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
461                                           int port, struct net_device *br);
462
463         /*
464          * PTP functionality
465          */
466         int     (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
467                                      struct ifreq *ifr);
468         int     (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
469                                      struct ifreq *ifr);
470         bool    (*port_txtstamp)(struct dsa_switch *ds, int port,
471                                  struct sk_buff *clone, unsigned int type);
472         bool    (*port_rxtstamp)(struct dsa_switch *ds, int port,
473                                  struct sk_buff *skb, unsigned int type);
474 };
475
476 struct dsa_switch_driver {
477         struct list_head        list;
478         const struct dsa_switch_ops *ops;
479 };
480
481 #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
482 /* Legacy driver registration */
483 void register_switch_driver(struct dsa_switch_driver *type);
484 void unregister_switch_driver(struct dsa_switch_driver *type);
485 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
486
487 #else
488 static inline void register_switch_driver(struct dsa_switch_driver *type) { }
489 static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
490 static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
491 {
492         return NULL;
493 }
494 #endif
495 struct net_device *dsa_dev_to_net_device(struct device *dev);
496
497 /* Keep inline for faster access in hot path */
498 static inline bool netdev_uses_dsa(struct net_device *dev)
499 {
500 #if IS_ENABLED(CONFIG_NET_DSA)
501         return dev->dsa_ptr && dev->dsa_ptr->rcv;
502 #endif
503         return false;
504 }
505
506 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
507 void dsa_unregister_switch(struct dsa_switch *ds);
508 int dsa_register_switch(struct dsa_switch *ds);
509 #ifdef CONFIG_PM_SLEEP
510 int dsa_switch_suspend(struct dsa_switch *ds);
511 int dsa_switch_resume(struct dsa_switch *ds);
512 #else
513 static inline int dsa_switch_suspend(struct dsa_switch *ds)
514 {
515         return 0;
516 }
517 static inline int dsa_switch_resume(struct dsa_switch *ds)
518 {
519         return 0;
520 }
521 #endif /* CONFIG_PM_SLEEP */
522
523 enum dsa_notifier_type {
524         DSA_PORT_REGISTER,
525         DSA_PORT_UNREGISTER,
526 };
527
528 struct dsa_notifier_info {
529         struct net_device *dev;
530 };
531
532 struct dsa_notifier_register_info {
533         struct dsa_notifier_info info;  /* must be first */
534         struct net_device *master;
535         unsigned int port_number;
536         unsigned int switch_number;
537 };
538
539 static inline struct net_device *
540 dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
541 {
542         return info->dev;
543 }
544
545 #if IS_ENABLED(CONFIG_NET_DSA)
546 int register_dsa_notifier(struct notifier_block *nb);
547 int unregister_dsa_notifier(struct notifier_block *nb);
548 int call_dsa_notifiers(unsigned long val, struct net_device *dev,
549                        struct dsa_notifier_info *info);
550 #else
551 static inline int register_dsa_notifier(struct notifier_block *nb)
552 {
553         return 0;
554 }
555
556 static inline int unregister_dsa_notifier(struct notifier_block *nb)
557 {
558         return 0;
559 }
560
561 static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
562                                      struct dsa_notifier_info *info)
563 {
564         return NOTIFY_DONE;
565 }
566 #endif
567
568 /* Broadcom tag specific helpers to insert and extract queue/port number */
569 #define BRCM_TAG_SET_PORT_QUEUE(p, q)   ((p) << 8 | q)
570 #define BRCM_TAG_GET_PORT(v)            ((v) >> 8)
571 #define BRCM_TAG_GET_QUEUE(v)           ((v) & 0xff)
572
573
574 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
575 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
576 int dsa_port_get_phy_sset_count(struct dsa_port *dp);
577 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
578
579 #endif