dsa.h 14 KB

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