dsa.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_ether.h>
  13. #include <linux/list.h>
  14. #include <linux/timer.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/of.h>
  17. #include <linux/phy.h>
  18. #include <linux/phy_fixed.h>
  19. #include <linux/ethtool.h>
  20. enum dsa_tag_protocol {
  21. DSA_TAG_PROTO_NONE = 0,
  22. DSA_TAG_PROTO_DSA,
  23. DSA_TAG_PROTO_TRAILER,
  24. DSA_TAG_PROTO_EDSA,
  25. DSA_TAG_PROTO_BRCM,
  26. DSA_TAG_PROTO_QCA,
  27. DSA_TAG_LAST, /* MUST BE LAST */
  28. };
  29. #define DSA_MAX_SWITCHES 4
  30. #define DSA_MAX_PORTS 12
  31. #define DSA_RTABLE_NONE -1
  32. struct dsa_chip_data {
  33. /*
  34. * How to access the switch configuration registers.
  35. */
  36. struct device *host_dev;
  37. int sw_addr;
  38. /* set to size of eeprom if supported by the switch */
  39. int eeprom_len;
  40. /* Device tree node pointer for this specific switch chip
  41. * used during switch setup in case additional properties
  42. * and resources needs to be used
  43. */
  44. struct device_node *of_node;
  45. /*
  46. * The names of the switch's ports. Use "cpu" to
  47. * designate the switch port that the cpu is connected to,
  48. * "dsa" to indicate that this port is a DSA link to
  49. * another switch, NULL to indicate the port is unused,
  50. * or any other string to indicate this is a physical port.
  51. */
  52. char *port_names[DSA_MAX_PORTS];
  53. struct device_node *port_dn[DSA_MAX_PORTS];
  54. /*
  55. * An array of which element [a] indicates which port on this
  56. * switch should be used to send packets to that are destined
  57. * for switch a. Can be NULL if there is only one switch chip.
  58. */
  59. s8 rtable[DSA_MAX_SWITCHES];
  60. };
  61. struct dsa_platform_data {
  62. /*
  63. * Reference to a Linux network interface that connects
  64. * to the root switch chip of the tree.
  65. */
  66. struct device *netdev;
  67. struct net_device *of_netdev;
  68. /*
  69. * Info structs describing each of the switch chips
  70. * connected via this network interface.
  71. */
  72. int nr_chips;
  73. struct dsa_chip_data *chip;
  74. };
  75. struct packet_type;
  76. struct dsa_switch_tree {
  77. struct list_head list;
  78. /* Tree identifier */
  79. u32 tree;
  80. /* Number of switches attached to this tree */
  81. struct kref refcount;
  82. /* Has this tree been applied to the hardware? */
  83. bool applied;
  84. /*
  85. * Configuration data for the platform device that owns
  86. * this dsa switch tree instance.
  87. */
  88. struct dsa_platform_data *pd;
  89. /*
  90. * Reference to network device to use, and which tagging
  91. * protocol to use.
  92. */
  93. struct net_device *master_netdev;
  94. int (*rcv)(struct sk_buff *skb,
  95. struct net_device *dev,
  96. struct packet_type *pt,
  97. struct net_device *orig_dev);
  98. /*
  99. * Original copy of the master netdev ethtool_ops
  100. */
  101. struct ethtool_ops master_ethtool_ops;
  102. const struct ethtool_ops *master_orig_ethtool_ops;
  103. /*
  104. * The switch and port to which the CPU is attached.
  105. */
  106. s8 cpu_switch;
  107. s8 cpu_port;
  108. /*
  109. * Data for the individual switch chips.
  110. */
  111. struct dsa_switch *ds[DSA_MAX_SWITCHES];
  112. /*
  113. * Tagging protocol operations for adding and removing an
  114. * encapsulation tag.
  115. */
  116. const struct dsa_device_ops *tag_ops;
  117. };
  118. struct dsa_port {
  119. struct net_device *netdev;
  120. struct device_node *dn;
  121. unsigned int ageing_time;
  122. u8 stp_state;
  123. };
  124. struct dsa_switch {
  125. struct device *dev;
  126. /*
  127. * Parent switch tree, and switch index.
  128. */
  129. struct dsa_switch_tree *dst;
  130. int index;
  131. /*
  132. * Give the switch driver somewhere to hang its private data
  133. * structure.
  134. */
  135. void *priv;
  136. /*
  137. * Configuration data for this switch.
  138. */
  139. struct dsa_chip_data *cd;
  140. /*
  141. * The switch operations.
  142. */
  143. struct dsa_switch_ops *ops;
  144. /*
  145. * An array of which element [a] indicates which port on this
  146. * switch should be used to send packets to that are destined
  147. * for switch a. Can be NULL if there is only one switch chip.
  148. */
  149. s8 rtable[DSA_MAX_SWITCHES];
  150. #ifdef CONFIG_NET_DSA_HWMON
  151. /*
  152. * Hardware monitoring information
  153. */
  154. char hwmon_name[IFNAMSIZ + 8];
  155. struct device *hwmon_dev;
  156. #endif
  157. /*
  158. * The lower device this switch uses to talk to the host
  159. */
  160. struct net_device *master_netdev;
  161. /*
  162. * Slave mii_bus and devices for the individual ports.
  163. */
  164. u32 dsa_port_mask;
  165. u32 cpu_port_mask;
  166. u32 enabled_port_mask;
  167. u32 phys_mii_mask;
  168. struct dsa_port ports[DSA_MAX_PORTS];
  169. struct mii_bus *slave_mii_bus;
  170. };
  171. static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
  172. {
  173. return !!(ds->index == ds->dst->cpu_switch && p == ds->dst->cpu_port);
  174. }
  175. static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
  176. {
  177. return !!((ds->dsa_port_mask) & (1 << p));
  178. }
  179. static inline bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
  180. {
  181. return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev;
  182. }
  183. static inline u8 dsa_upstream_port(struct dsa_switch *ds)
  184. {
  185. struct dsa_switch_tree *dst = ds->dst;
  186. /*
  187. * If this is the root switch (i.e. the switch that connects
  188. * to the CPU), return the cpu port number on this switch.
  189. * Else return the (DSA) port number that connects to the
  190. * switch that is one hop closer to the cpu.
  191. */
  192. if (dst->cpu_switch == ds->index)
  193. return dst->cpu_port;
  194. else
  195. return ds->rtable[dst->cpu_switch];
  196. }
  197. struct switchdev_trans;
  198. struct switchdev_obj;
  199. struct switchdev_obj_port_fdb;
  200. struct switchdev_obj_port_mdb;
  201. struct switchdev_obj_port_vlan;
  202. struct dsa_switch_ops {
  203. struct list_head list;
  204. /*
  205. * Probing and setup.
  206. */
  207. const char *(*probe)(struct device *dsa_dev,
  208. struct device *host_dev, int sw_addr,
  209. void **priv);
  210. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds);
  211. int (*setup)(struct dsa_switch *ds);
  212. int (*set_addr)(struct dsa_switch *ds, u8 *addr);
  213. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  214. /*
  215. * Access to the switch's PHY registers.
  216. */
  217. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  218. int (*phy_write)(struct dsa_switch *ds, int port,
  219. int regnum, u16 val);
  220. /*
  221. * Link state adjustment (called from libphy)
  222. */
  223. void (*adjust_link)(struct dsa_switch *ds, int port,
  224. struct phy_device *phydev);
  225. void (*fixed_link_update)(struct dsa_switch *ds, int port,
  226. struct fixed_phy_status *st);
  227. /*
  228. * ethtool hardware statistics.
  229. */
  230. void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
  231. void (*get_ethtool_stats)(struct dsa_switch *ds,
  232. int port, uint64_t *data);
  233. int (*get_sset_count)(struct dsa_switch *ds);
  234. /*
  235. * ethtool Wake-on-LAN
  236. */
  237. void (*get_wol)(struct dsa_switch *ds, int port,
  238. struct ethtool_wolinfo *w);
  239. int (*set_wol)(struct dsa_switch *ds, int port,
  240. struct ethtool_wolinfo *w);
  241. /*
  242. * Suspend and resume
  243. */
  244. int (*suspend)(struct dsa_switch *ds);
  245. int (*resume)(struct dsa_switch *ds);
  246. /*
  247. * Port enable/disable
  248. */
  249. int (*port_enable)(struct dsa_switch *ds, int port,
  250. struct phy_device *phy);
  251. void (*port_disable)(struct dsa_switch *ds, int port,
  252. struct phy_device *phy);
  253. /*
  254. * EEE setttings
  255. */
  256. int (*set_eee)(struct dsa_switch *ds, int port,
  257. struct phy_device *phydev,
  258. struct ethtool_eee *e);
  259. int (*get_eee)(struct dsa_switch *ds, int port,
  260. struct ethtool_eee *e);
  261. #ifdef CONFIG_NET_DSA_HWMON
  262. /* Hardware monitoring */
  263. int (*get_temp)(struct dsa_switch *ds, int *temp);
  264. int (*get_temp_limit)(struct dsa_switch *ds, int *temp);
  265. int (*set_temp_limit)(struct dsa_switch *ds, int temp);
  266. int (*get_temp_alarm)(struct dsa_switch *ds, bool *alarm);
  267. #endif
  268. /* EEPROM access */
  269. int (*get_eeprom_len)(struct dsa_switch *ds);
  270. int (*get_eeprom)(struct dsa_switch *ds,
  271. struct ethtool_eeprom *eeprom, u8 *data);
  272. int (*set_eeprom)(struct dsa_switch *ds,
  273. struct ethtool_eeprom *eeprom, u8 *data);
  274. /*
  275. * Register access.
  276. */
  277. int (*get_regs_len)(struct dsa_switch *ds, int port);
  278. void (*get_regs)(struct dsa_switch *ds, int port,
  279. struct ethtool_regs *regs, void *p);
  280. /*
  281. * Bridge integration
  282. */
  283. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  284. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  285. struct net_device *bridge);
  286. void (*port_bridge_leave)(struct dsa_switch *ds, int port);
  287. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  288. u8 state);
  289. void (*port_fast_age)(struct dsa_switch *ds, int port);
  290. /*
  291. * VLAN support
  292. */
  293. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  294. bool vlan_filtering);
  295. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  296. const struct switchdev_obj_port_vlan *vlan,
  297. struct switchdev_trans *trans);
  298. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  299. const struct switchdev_obj_port_vlan *vlan,
  300. struct switchdev_trans *trans);
  301. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  302. const struct switchdev_obj_port_vlan *vlan);
  303. int (*port_vlan_dump)(struct dsa_switch *ds, int port,
  304. struct switchdev_obj_port_vlan *vlan,
  305. int (*cb)(struct switchdev_obj *obj));
  306. /*
  307. * Forwarding database
  308. */
  309. int (*port_fdb_prepare)(struct dsa_switch *ds, int port,
  310. const struct switchdev_obj_port_fdb *fdb,
  311. struct switchdev_trans *trans);
  312. void (*port_fdb_add)(struct dsa_switch *ds, int port,
  313. const struct switchdev_obj_port_fdb *fdb,
  314. struct switchdev_trans *trans);
  315. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  316. const struct switchdev_obj_port_fdb *fdb);
  317. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  318. struct switchdev_obj_port_fdb *fdb,
  319. int (*cb)(struct switchdev_obj *obj));
  320. /*
  321. * Multicast database
  322. */
  323. int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
  324. const struct switchdev_obj_port_mdb *mdb,
  325. struct switchdev_trans *trans);
  326. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  327. const struct switchdev_obj_port_mdb *mdb,
  328. struct switchdev_trans *trans);
  329. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  330. const struct switchdev_obj_port_mdb *mdb);
  331. int (*port_mdb_dump)(struct dsa_switch *ds, int port,
  332. struct switchdev_obj_port_mdb *mdb,
  333. int (*cb)(struct switchdev_obj *obj));
  334. };
  335. void register_switch_driver(struct dsa_switch_ops *type);
  336. void unregister_switch_driver(struct dsa_switch_ops *type);
  337. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  338. static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
  339. {
  340. return dst->rcv != NULL;
  341. }
  342. void dsa_unregister_switch(struct dsa_switch *ds);
  343. int dsa_register_switch(struct dsa_switch *ds, struct device_node *np);
  344. #ifdef CONFIG_PM_SLEEP
  345. int dsa_switch_suspend(struct dsa_switch *ds);
  346. int dsa_switch_resume(struct dsa_switch *ds);
  347. #else
  348. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  349. {
  350. return 0;
  351. }
  352. static inline int dsa_switch_resume(struct dsa_switch *ds)
  353. {
  354. return 0;
  355. }
  356. #endif /* CONFIG_PM_SLEEP */
  357. #endif