dsa.h 12 KB

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