dsa.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #ifndef __LINUX_NET_DSA_H
  11. #define __LINUX_NET_DSA_H
  12. #include <linux/if.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/list.h>
  15. #include <linux/notifier.h>
  16. #include <linux/timer.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/of.h>
  19. #include <linux/ethtool.h>
  20. #include <net/devlink.h>
  21. #include <net/switchdev.h>
  22. struct tc_action;
  23. struct phy_device;
  24. struct fixed_phy_status;
  25. enum dsa_tag_protocol {
  26. DSA_TAG_PROTO_NONE = 0,
  27. DSA_TAG_PROTO_BRCM,
  28. DSA_TAG_PROTO_DSA,
  29. DSA_TAG_PROTO_EDSA,
  30. DSA_TAG_PROTO_KSZ,
  31. DSA_TAG_PROTO_LAN9303,
  32. DSA_TAG_PROTO_MTK,
  33. DSA_TAG_PROTO_QCA,
  34. DSA_TAG_PROTO_TRAILER,
  35. DSA_TAG_LAST, /* MUST BE LAST */
  36. };
  37. #define DSA_MAX_SWITCHES 4
  38. #define DSA_MAX_PORTS 12
  39. #define DSA_RTABLE_NONE -1
  40. struct dsa_chip_data {
  41. /*
  42. * How to access the switch configuration registers.
  43. */
  44. struct device *host_dev;
  45. int sw_addr;
  46. /*
  47. * Reference to network devices
  48. */
  49. struct device *netdev[DSA_MAX_PORTS];
  50. /* set to size of eeprom if supported by the switch */
  51. int eeprom_len;
  52. /* Device tree node pointer for this specific switch chip
  53. * used during switch setup in case additional properties
  54. * and resources needs to be used
  55. */
  56. struct device_node *of_node;
  57. /*
  58. * The names of the switch's ports. Use "cpu" to
  59. * designate the switch port that the cpu is connected to,
  60. * "dsa" to indicate that this port is a DSA link to
  61. * another switch, NULL to indicate the port is unused,
  62. * or any other string to indicate this is a physical port.
  63. */
  64. char *port_names[DSA_MAX_PORTS];
  65. struct device_node *port_dn[DSA_MAX_PORTS];
  66. /*
  67. * An array of which element [a] indicates which port on this
  68. * switch should be used to send packets to that are destined
  69. * for switch a. Can be NULL if there is only one switch chip.
  70. */
  71. s8 rtable[DSA_MAX_SWITCHES];
  72. };
  73. struct dsa_platform_data {
  74. /*
  75. * Reference to a Linux network interface that connects
  76. * to the root switch chip of the tree.
  77. */
  78. struct device *netdev;
  79. struct net_device *of_netdev;
  80. /*
  81. * Info structs describing each of the switch chips
  82. * connected via this network interface.
  83. */
  84. int nr_chips;
  85. struct dsa_chip_data *chip;
  86. };
  87. struct packet_type;
  88. struct dsa_device_ops {
  89. struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
  90. struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev,
  91. struct packet_type *pt);
  92. int (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
  93. int *offset);
  94. };
  95. struct dsa_switch_tree {
  96. struct list_head list;
  97. /* Notifier chain for switch-wide events */
  98. struct raw_notifier_head nh;
  99. /* Tree identifier */
  100. u32 tree;
  101. /* Number of switches attached to this tree */
  102. struct kref refcount;
  103. /* Has this tree been applied to the hardware? */
  104. bool applied;
  105. /*
  106. * Configuration data for the platform device that owns
  107. * this dsa switch tree instance.
  108. */
  109. struct dsa_platform_data *pd;
  110. /* Copy of tag_ops->rcv for faster access in hot path */
  111. struct sk_buff * (*rcv)(struct sk_buff *skb,
  112. struct net_device *dev,
  113. struct packet_type *pt);
  114. /*
  115. * The switch port to which the CPU is attached.
  116. */
  117. struct dsa_port *cpu_dp;
  118. /*
  119. * Data for the individual switch chips.
  120. */
  121. struct dsa_switch *ds[DSA_MAX_SWITCHES];
  122. /*
  123. * Tagging protocol operations for adding and removing an
  124. * encapsulation tag.
  125. */
  126. const struct dsa_device_ops *tag_ops;
  127. };
  128. /* TC matchall action types, only mirroring for now */
  129. enum dsa_port_mall_action_type {
  130. DSA_PORT_MALL_MIRROR,
  131. };
  132. /* TC mirroring entry */
  133. struct dsa_mall_mirror_tc_entry {
  134. u8 to_local_port;
  135. bool ingress;
  136. };
  137. /* TC matchall entry */
  138. struct dsa_mall_tc_entry {
  139. struct list_head list;
  140. unsigned long cookie;
  141. enum dsa_port_mall_action_type type;
  142. union {
  143. struct dsa_mall_mirror_tc_entry mirror;
  144. };
  145. };
  146. struct dsa_port {
  147. struct dsa_switch *ds;
  148. unsigned int index;
  149. const char *name;
  150. struct dsa_port *cpu_dp;
  151. struct net_device *netdev;
  152. struct device_node *dn;
  153. unsigned int ageing_time;
  154. u8 stp_state;
  155. struct net_device *bridge_dev;
  156. struct devlink_port devlink_port;
  157. /*
  158. * Original copy of the master netdev ethtool_ops
  159. */
  160. struct ethtool_ops ethtool_ops;
  161. const struct ethtool_ops *orig_ethtool_ops;
  162. };
  163. struct dsa_switch {
  164. struct device *dev;
  165. /*
  166. * Parent switch tree, and switch index.
  167. */
  168. struct dsa_switch_tree *dst;
  169. int index;
  170. /* Listener for switch fabric events */
  171. struct notifier_block nb;
  172. /*
  173. * Give the switch driver somewhere to hang its private data
  174. * structure.
  175. */
  176. void *priv;
  177. /*
  178. * Configuration data for this switch.
  179. */
  180. struct dsa_chip_data *cd;
  181. /*
  182. * The switch operations.
  183. */
  184. const struct dsa_switch_ops *ops;
  185. /*
  186. * An array of which element [a] indicates which port on this
  187. * switch should be used to send packets to that are destined
  188. * for switch a. Can be NULL if there is only one switch chip.
  189. */
  190. s8 rtable[DSA_MAX_SWITCHES];
  191. /*
  192. * Slave mii_bus and devices for the individual ports.
  193. */
  194. u32 dsa_port_mask;
  195. u32 cpu_port_mask;
  196. u32 enabled_port_mask;
  197. u32 phys_mii_mask;
  198. struct mii_bus *slave_mii_bus;
  199. /* Ageing Time limits in msecs */
  200. unsigned int ageing_time_min;
  201. unsigned int ageing_time_max;
  202. /* devlink used to represent this switch device */
  203. struct devlink *devlink;
  204. /* Number of switch port queues */
  205. unsigned int num_tx_queues;
  206. /* Dynamically allocated ports, keep last */
  207. size_t num_ports;
  208. struct dsa_port ports[];
  209. };
  210. static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
  211. {
  212. return !!(ds->cpu_port_mask & (1 << p));
  213. }
  214. static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
  215. {
  216. return !!((ds->dsa_port_mask) & (1 << p));
  217. }
  218. static inline bool dsa_is_normal_port(struct dsa_switch *ds, int p)
  219. {
  220. return !dsa_is_cpu_port(ds, p) && !dsa_is_dsa_port(ds, p);
  221. }
  222. static inline u8 dsa_upstream_port(struct dsa_switch *ds)
  223. {
  224. struct dsa_switch_tree *dst = ds->dst;
  225. /*
  226. * If this is the root switch (i.e. the switch that connects
  227. * to the CPU), return the cpu port number on this switch.
  228. * Else return the (DSA) port number that connects to the
  229. * switch that is one hop closer to the cpu.
  230. */
  231. if (dst->cpu_dp->ds == ds)
  232. return dst->cpu_dp->index;
  233. else
  234. return ds->rtable[dst->cpu_dp->ds->index];
  235. }
  236. typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
  237. bool is_static, void *data);
  238. struct dsa_switch_ops {
  239. /*
  240. * Legacy probing.
  241. */
  242. const char *(*probe)(struct device *dsa_dev,
  243. struct device *host_dev, int sw_addr,
  244. void **priv);
  245. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds);
  246. int (*setup)(struct dsa_switch *ds);
  247. int (*set_addr)(struct dsa_switch *ds, u8 *addr);
  248. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  249. /*
  250. * Access to the switch's PHY registers.
  251. */
  252. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  253. int (*phy_write)(struct dsa_switch *ds, int port,
  254. int regnum, u16 val);
  255. /*
  256. * Link state adjustment (called from libphy)
  257. */
  258. void (*adjust_link)(struct dsa_switch *ds, int port,
  259. struct phy_device *phydev);
  260. void (*fixed_link_update)(struct dsa_switch *ds, int port,
  261. struct fixed_phy_status *st);
  262. /*
  263. * ethtool hardware statistics.
  264. */
  265. void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
  266. void (*get_ethtool_stats)(struct dsa_switch *ds,
  267. int port, uint64_t *data);
  268. int (*get_sset_count)(struct dsa_switch *ds);
  269. /*
  270. * ethtool Wake-on-LAN
  271. */
  272. void (*get_wol)(struct dsa_switch *ds, int port,
  273. struct ethtool_wolinfo *w);
  274. int (*set_wol)(struct dsa_switch *ds, int port,
  275. struct ethtool_wolinfo *w);
  276. /*
  277. * Suspend and resume
  278. */
  279. int (*suspend)(struct dsa_switch *ds);
  280. int (*resume)(struct dsa_switch *ds);
  281. /*
  282. * Port enable/disable
  283. */
  284. int (*port_enable)(struct dsa_switch *ds, int port,
  285. struct phy_device *phy);
  286. void (*port_disable)(struct dsa_switch *ds, int port,
  287. struct phy_device *phy);
  288. /*
  289. * Port's MAC EEE settings
  290. */
  291. int (*set_mac_eee)(struct dsa_switch *ds, int port,
  292. struct ethtool_eee *e);
  293. int (*get_mac_eee)(struct dsa_switch *ds, int port,
  294. struct ethtool_eee *e);
  295. /* EEPROM access */
  296. int (*get_eeprom_len)(struct dsa_switch *ds);
  297. int (*get_eeprom)(struct dsa_switch *ds,
  298. struct ethtool_eeprom *eeprom, u8 *data);
  299. int (*set_eeprom)(struct dsa_switch *ds,
  300. struct ethtool_eeprom *eeprom, u8 *data);
  301. /*
  302. * Register access.
  303. */
  304. int (*get_regs_len)(struct dsa_switch *ds, int port);
  305. void (*get_regs)(struct dsa_switch *ds, int port,
  306. struct ethtool_regs *regs, void *p);
  307. /*
  308. * Bridge integration
  309. */
  310. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  311. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  312. struct net_device *bridge);
  313. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  314. struct net_device *bridge);
  315. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  316. u8 state);
  317. void (*port_fast_age)(struct dsa_switch *ds, int port);
  318. /*
  319. * VLAN support
  320. */
  321. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  322. bool vlan_filtering);
  323. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  324. const struct switchdev_obj_port_vlan *vlan,
  325. struct switchdev_trans *trans);
  326. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  327. const struct switchdev_obj_port_vlan *vlan,
  328. struct switchdev_trans *trans);
  329. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  330. const struct switchdev_obj_port_vlan *vlan);
  331. /*
  332. * Forwarding database
  333. */
  334. int (*port_fdb_add)(struct dsa_switch *ds, int port,
  335. const unsigned char *addr, u16 vid);
  336. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  337. const unsigned char *addr, u16 vid);
  338. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  339. dsa_fdb_dump_cb_t *cb, void *data);
  340. /*
  341. * Multicast database
  342. */
  343. int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
  344. const struct switchdev_obj_port_mdb *mdb,
  345. struct switchdev_trans *trans);
  346. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  347. const struct switchdev_obj_port_mdb *mdb,
  348. struct switchdev_trans *trans);
  349. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  350. const struct switchdev_obj_port_mdb *mdb);
  351. /*
  352. * RXNFC
  353. */
  354. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  355. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  356. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  357. struct ethtool_rxnfc *nfc);
  358. /*
  359. * TC integration
  360. */
  361. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  362. struct dsa_mall_mirror_tc_entry *mirror,
  363. bool ingress);
  364. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  365. struct dsa_mall_mirror_tc_entry *mirror);
  366. /*
  367. * Cross-chip operations
  368. */
  369. int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
  370. int port, struct net_device *br);
  371. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
  372. int port, struct net_device *br);
  373. };
  374. struct dsa_switch_driver {
  375. struct list_head list;
  376. const struct dsa_switch_ops *ops;
  377. };
  378. /* Legacy driver registration */
  379. void register_switch_driver(struct dsa_switch_driver *type);
  380. void unregister_switch_driver(struct dsa_switch_driver *type);
  381. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  382. struct net_device *dsa_dev_to_net_device(struct device *dev);
  383. /* Keep inline for faster access in hot path */
  384. static inline bool netdev_uses_dsa(struct net_device *dev)
  385. {
  386. #if IS_ENABLED(CONFIG_NET_DSA)
  387. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  388. #endif
  389. return false;
  390. }
  391. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
  392. void dsa_unregister_switch(struct dsa_switch *ds);
  393. int dsa_register_switch(struct dsa_switch *ds);
  394. #ifdef CONFIG_PM_SLEEP
  395. int dsa_switch_suspend(struct dsa_switch *ds);
  396. int dsa_switch_resume(struct dsa_switch *ds);
  397. #else
  398. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  399. {
  400. return 0;
  401. }
  402. static inline int dsa_switch_resume(struct dsa_switch *ds)
  403. {
  404. return 0;
  405. }
  406. #endif /* CONFIG_PM_SLEEP */
  407. #endif