dsa.h 9.9 KB

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