dsa.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_switch_tree {
  89. struct list_head list;
  90. /* Notifier chain for switch-wide events */
  91. struct raw_notifier_head nh;
  92. /* Tree identifier */
  93. u32 tree;
  94. /* Number of switches attached to this tree */
  95. struct kref refcount;
  96. /* Has this tree been applied to the hardware? */
  97. bool applied;
  98. /*
  99. * Configuration data for the platform device that owns
  100. * this dsa switch tree instance.
  101. */
  102. struct dsa_platform_data *pd;
  103. /* Copy of tag_ops->rcv for faster access in hot path */
  104. struct sk_buff * (*rcv)(struct sk_buff *skb,
  105. struct net_device *dev,
  106. struct packet_type *pt,
  107. struct net_device *orig_dev);
  108. /*
  109. * The switch port to which the CPU is attached.
  110. */
  111. struct dsa_port *cpu_dp;
  112. /*
  113. * Data for the individual switch chips.
  114. */
  115. struct dsa_switch *ds[DSA_MAX_SWITCHES];
  116. /*
  117. * Tagging protocol operations for adding and removing an
  118. * encapsulation tag.
  119. */
  120. const struct dsa_device_ops *tag_ops;
  121. };
  122. /* TC matchall action types, only mirroring for now */
  123. enum dsa_port_mall_action_type {
  124. DSA_PORT_MALL_MIRROR,
  125. };
  126. /* TC mirroring entry */
  127. struct dsa_mall_mirror_tc_entry {
  128. u8 to_local_port;
  129. bool ingress;
  130. };
  131. /* TC matchall entry */
  132. struct dsa_mall_tc_entry {
  133. struct list_head list;
  134. unsigned long cookie;
  135. enum dsa_port_mall_action_type type;
  136. union {
  137. struct dsa_mall_mirror_tc_entry mirror;
  138. };
  139. };
  140. struct dsa_port {
  141. struct dsa_switch *ds;
  142. unsigned int index;
  143. const char *name;
  144. struct dsa_port *cpu_dp;
  145. struct net_device *netdev;
  146. struct device_node *dn;
  147. unsigned int ageing_time;
  148. u8 stp_state;
  149. struct net_device *bridge_dev;
  150. struct devlink_port devlink_port;
  151. /*
  152. * Original copy of the master netdev ethtool_ops
  153. */
  154. struct ethtool_ops ethtool_ops;
  155. const struct ethtool_ops *orig_ethtool_ops;
  156. };
  157. struct dsa_switch {
  158. struct device *dev;
  159. /*
  160. * Parent switch tree, and switch index.
  161. */
  162. struct dsa_switch_tree *dst;
  163. int index;
  164. /* Listener for switch fabric events */
  165. struct notifier_block nb;
  166. /*
  167. * Give the switch driver somewhere to hang its private data
  168. * structure.
  169. */
  170. void *priv;
  171. /*
  172. * Configuration data for this switch.
  173. */
  174. struct dsa_chip_data *cd;
  175. /*
  176. * The switch operations.
  177. */
  178. const struct dsa_switch_ops *ops;
  179. /*
  180. * An array of which element [a] indicates which port on this
  181. * switch should be used to send packets to that are destined
  182. * for switch a. Can be NULL if there is only one switch chip.
  183. */
  184. s8 rtable[DSA_MAX_SWITCHES];
  185. /*
  186. * Slave mii_bus and devices for the individual ports.
  187. */
  188. u32 dsa_port_mask;
  189. u32 cpu_port_mask;
  190. u32 enabled_port_mask;
  191. u32 phys_mii_mask;
  192. struct mii_bus *slave_mii_bus;
  193. /* Ageing Time limits in msecs */
  194. unsigned int ageing_time_min;
  195. unsigned int ageing_time_max;
  196. /* devlink used to represent this switch device */
  197. struct devlink *devlink;
  198. /* Dynamically allocated ports, keep last */
  199. size_t num_ports;
  200. struct dsa_port ports[];
  201. };
  202. static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
  203. {
  204. return !!(ds->cpu_port_mask & (1 << p));
  205. }
  206. static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
  207. {
  208. return !!((ds->dsa_port_mask) & (1 << p));
  209. }
  210. static inline bool dsa_is_normal_port(struct dsa_switch *ds, int p)
  211. {
  212. return !dsa_is_cpu_port(ds, p) && !dsa_is_dsa_port(ds, p);
  213. }
  214. static inline bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
  215. {
  216. return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev;
  217. }
  218. static inline u8 dsa_upstream_port(struct dsa_switch *ds)
  219. {
  220. struct dsa_switch_tree *dst = ds->dst;
  221. /*
  222. * If this is the root switch (i.e. the switch that connects
  223. * to the CPU), return the cpu port number on this switch.
  224. * Else return the (DSA) port number that connects to the
  225. * switch that is one hop closer to the cpu.
  226. */
  227. if (dst->cpu_dp->ds == ds)
  228. return dst->cpu_dp->index;
  229. else
  230. return ds->rtable[dst->cpu_dp->ds->index];
  231. }
  232. struct dsa_switch_ops {
  233. /*
  234. * Legacy probing.
  235. */
  236. const char *(*probe)(struct device *dsa_dev,
  237. struct device *host_dev, int sw_addr,
  238. void **priv);
  239. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds);
  240. int (*setup)(struct dsa_switch *ds);
  241. int (*set_addr)(struct dsa_switch *ds, u8 *addr);
  242. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  243. /*
  244. * Access to the switch's PHY registers.
  245. */
  246. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  247. int (*phy_write)(struct dsa_switch *ds, int port,
  248. int regnum, u16 val);
  249. /*
  250. * Link state adjustment (called from libphy)
  251. */
  252. void (*adjust_link)(struct dsa_switch *ds, int port,
  253. struct phy_device *phydev);
  254. void (*fixed_link_update)(struct dsa_switch *ds, int port,
  255. struct fixed_phy_status *st);
  256. /*
  257. * ethtool hardware statistics.
  258. */
  259. void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
  260. void (*get_ethtool_stats)(struct dsa_switch *ds,
  261. int port, uint64_t *data);
  262. int (*get_sset_count)(struct dsa_switch *ds);
  263. /*
  264. * ethtool Wake-on-LAN
  265. */
  266. void (*get_wol)(struct dsa_switch *ds, int port,
  267. struct ethtool_wolinfo *w);
  268. int (*set_wol)(struct dsa_switch *ds, int port,
  269. struct ethtool_wolinfo *w);
  270. /*
  271. * Suspend and resume
  272. */
  273. int (*suspend)(struct dsa_switch *ds);
  274. int (*resume)(struct dsa_switch *ds);
  275. /*
  276. * Port enable/disable
  277. */
  278. int (*port_enable)(struct dsa_switch *ds, int port,
  279. struct phy_device *phy);
  280. void (*port_disable)(struct dsa_switch *ds, int port,
  281. struct phy_device *phy);
  282. /*
  283. * EEE setttings
  284. */
  285. int (*set_eee)(struct dsa_switch *ds, int port,
  286. struct phy_device *phydev,
  287. struct ethtool_eee *e);
  288. int (*get_eee)(struct dsa_switch *ds, int port,
  289. struct ethtool_eee *e);
  290. /* EEPROM access */
  291. int (*get_eeprom_len)(struct dsa_switch *ds);
  292. int (*get_eeprom)(struct dsa_switch *ds,
  293. struct ethtool_eeprom *eeprom, u8 *data);
  294. int (*set_eeprom)(struct dsa_switch *ds,
  295. struct ethtool_eeprom *eeprom, u8 *data);
  296. /*
  297. * Register access.
  298. */
  299. int (*get_regs_len)(struct dsa_switch *ds, int port);
  300. void (*get_regs)(struct dsa_switch *ds, int port,
  301. struct ethtool_regs *regs, void *p);
  302. /*
  303. * Bridge integration
  304. */
  305. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  306. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  307. struct net_device *bridge);
  308. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  309. struct net_device *bridge);
  310. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  311. u8 state);
  312. void (*port_fast_age)(struct dsa_switch *ds, int port);
  313. /*
  314. * VLAN support
  315. */
  316. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  317. bool vlan_filtering);
  318. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  319. const struct switchdev_obj_port_vlan *vlan,
  320. struct switchdev_trans *trans);
  321. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  322. const struct switchdev_obj_port_vlan *vlan,
  323. struct switchdev_trans *trans);
  324. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  325. const struct switchdev_obj_port_vlan *vlan);
  326. int (*port_vlan_dump)(struct dsa_switch *ds, int port,
  327. struct switchdev_obj_port_vlan *vlan,
  328. switchdev_obj_dump_cb_t *cb);
  329. /*
  330. * Forwarding database
  331. */
  332. int (*port_fdb_prepare)(struct dsa_switch *ds, int port,
  333. const struct switchdev_obj_port_fdb *fdb,
  334. struct switchdev_trans *trans);
  335. void (*port_fdb_add)(struct dsa_switch *ds, int port,
  336. const struct switchdev_obj_port_fdb *fdb,
  337. struct switchdev_trans *trans);
  338. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  339. const struct switchdev_obj_port_fdb *fdb);
  340. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  341. struct switchdev_obj_port_fdb *fdb,
  342. switchdev_obj_dump_cb_t *cb);
  343. /*
  344. * Multicast database
  345. */
  346. int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
  347. const struct switchdev_obj_port_mdb *mdb,
  348. struct switchdev_trans *trans);
  349. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  350. const struct switchdev_obj_port_mdb *mdb,
  351. struct switchdev_trans *trans);
  352. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  353. const struct switchdev_obj_port_mdb *mdb);
  354. int (*port_mdb_dump)(struct dsa_switch *ds, int port,
  355. struct switchdev_obj_port_mdb *mdb,
  356. switchdev_obj_dump_cb_t *cb);
  357. /*
  358. * RXNFC
  359. */
  360. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  361. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  362. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  363. struct ethtool_rxnfc *nfc);
  364. /*
  365. * TC integration
  366. */
  367. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  368. struct dsa_mall_mirror_tc_entry *mirror,
  369. bool ingress);
  370. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  371. struct dsa_mall_mirror_tc_entry *mirror);
  372. /*
  373. * Cross-chip operations
  374. */
  375. int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
  376. int port, struct net_device *br);
  377. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
  378. int port, struct net_device *br);
  379. };
  380. struct dsa_switch_driver {
  381. struct list_head list;
  382. const struct dsa_switch_ops *ops;
  383. };
  384. /* Legacy driver registration */
  385. void register_switch_driver(struct dsa_switch_driver *type);
  386. void unregister_switch_driver(struct dsa_switch_driver *type);
  387. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  388. struct net_device *dsa_dev_to_net_device(struct device *dev);
  389. /* Keep inline for faster access in hot path */
  390. static inline bool netdev_uses_dsa(struct net_device *dev)
  391. {
  392. #if IS_ENABLED(CONFIG_NET_DSA)
  393. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  394. #endif
  395. return false;
  396. }
  397. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
  398. void dsa_unregister_switch(struct dsa_switch *ds);
  399. int dsa_register_switch(struct dsa_switch *ds);
  400. #ifdef CONFIG_PM_SLEEP
  401. int dsa_switch_suspend(struct dsa_switch *ds);
  402. int dsa_switch_resume(struct dsa_switch *ds);
  403. #else
  404. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  405. {
  406. return 0;
  407. }
  408. static inline int dsa_switch_resume(struct dsa_switch *ds)
  409. {
  410. return 0;
  411. }
  412. #endif /* CONFIG_PM_SLEEP */
  413. #endif