dsa.h 12 KB

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