dsa.h 13 KB

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