dsa.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. /* Return the local port used to reach an arbitrary switch port */
  246. static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
  247. int port)
  248. {
  249. if (device == ds->index)
  250. return port;
  251. else
  252. return ds->rtable[device];
  253. }
  254. /* Return the local port used to reach the dedicated CPU port */
  255. static inline u8 dsa_upstream_port(struct dsa_switch *ds)
  256. {
  257. struct dsa_switch_tree *dst = ds->dst;
  258. struct dsa_port *cpu_dp = dst->cpu_dp;
  259. return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
  260. }
  261. typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
  262. bool is_static, void *data);
  263. struct dsa_switch_ops {
  264. /*
  265. * Legacy probing.
  266. */
  267. const char *(*probe)(struct device *dsa_dev,
  268. struct device *host_dev, int sw_addr,
  269. void **priv);
  270. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
  271. int port);
  272. int (*setup)(struct dsa_switch *ds);
  273. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  274. /*
  275. * Access to the switch's PHY registers.
  276. */
  277. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  278. int (*phy_write)(struct dsa_switch *ds, int port,
  279. int regnum, u16 val);
  280. /*
  281. * Link state adjustment (called from libphy)
  282. */
  283. void (*adjust_link)(struct dsa_switch *ds, int port,
  284. struct phy_device *phydev);
  285. void (*fixed_link_update)(struct dsa_switch *ds, int port,
  286. struct fixed_phy_status *st);
  287. /*
  288. * ethtool hardware statistics.
  289. */
  290. void (*get_strings)(struct dsa_switch *ds, int port, uint8_t *data);
  291. void (*get_ethtool_stats)(struct dsa_switch *ds,
  292. int port, uint64_t *data);
  293. int (*get_sset_count)(struct dsa_switch *ds);
  294. /*
  295. * ethtool Wake-on-LAN
  296. */
  297. void (*get_wol)(struct dsa_switch *ds, int port,
  298. struct ethtool_wolinfo *w);
  299. int (*set_wol)(struct dsa_switch *ds, int port,
  300. struct ethtool_wolinfo *w);
  301. /*
  302. * Suspend and resume
  303. */
  304. int (*suspend)(struct dsa_switch *ds);
  305. int (*resume)(struct dsa_switch *ds);
  306. /*
  307. * Port enable/disable
  308. */
  309. int (*port_enable)(struct dsa_switch *ds, int port,
  310. struct phy_device *phy);
  311. void (*port_disable)(struct dsa_switch *ds, int port,
  312. struct phy_device *phy);
  313. /*
  314. * Port's MAC EEE settings
  315. */
  316. int (*set_mac_eee)(struct dsa_switch *ds, int port,
  317. struct ethtool_eee *e);
  318. int (*get_mac_eee)(struct dsa_switch *ds, int port,
  319. struct ethtool_eee *e);
  320. /* EEPROM access */
  321. int (*get_eeprom_len)(struct dsa_switch *ds);
  322. int (*get_eeprom)(struct dsa_switch *ds,
  323. struct ethtool_eeprom *eeprom, u8 *data);
  324. int (*set_eeprom)(struct dsa_switch *ds,
  325. struct ethtool_eeprom *eeprom, u8 *data);
  326. /*
  327. * Register access.
  328. */
  329. int (*get_regs_len)(struct dsa_switch *ds, int port);
  330. void (*get_regs)(struct dsa_switch *ds, int port,
  331. struct ethtool_regs *regs, void *p);
  332. /*
  333. * Bridge integration
  334. */
  335. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  336. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  337. struct net_device *bridge);
  338. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  339. struct net_device *bridge);
  340. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  341. u8 state);
  342. void (*port_fast_age)(struct dsa_switch *ds, int port);
  343. /*
  344. * VLAN support
  345. */
  346. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  347. bool vlan_filtering);
  348. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  349. const struct switchdev_obj_port_vlan *vlan);
  350. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  351. const struct switchdev_obj_port_vlan *vlan);
  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. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  369. const struct switchdev_obj_port_mdb *mdb);
  370. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  371. const struct switchdev_obj_port_mdb *mdb);
  372. /*
  373. * RXNFC
  374. */
  375. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  376. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  377. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  378. struct ethtool_rxnfc *nfc);
  379. /*
  380. * TC integration
  381. */
  382. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  383. struct dsa_mall_mirror_tc_entry *mirror,
  384. bool ingress);
  385. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  386. struct dsa_mall_mirror_tc_entry *mirror);
  387. /*
  388. * Cross-chip operations
  389. */
  390. int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
  391. int port, struct net_device *br);
  392. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
  393. int port, struct net_device *br);
  394. };
  395. struct dsa_switch_driver {
  396. struct list_head list;
  397. const struct dsa_switch_ops *ops;
  398. };
  399. /* Legacy driver registration */
  400. void register_switch_driver(struct dsa_switch_driver *type);
  401. void unregister_switch_driver(struct dsa_switch_driver *type);
  402. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  403. struct net_device *dsa_dev_to_net_device(struct device *dev);
  404. /* Keep inline for faster access in hot path */
  405. static inline bool netdev_uses_dsa(struct net_device *dev)
  406. {
  407. #if IS_ENABLED(CONFIG_NET_DSA)
  408. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  409. #endif
  410. return false;
  411. }
  412. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
  413. void dsa_unregister_switch(struct dsa_switch *ds);
  414. int dsa_register_switch(struct dsa_switch *ds);
  415. #ifdef CONFIG_PM_SLEEP
  416. int dsa_switch_suspend(struct dsa_switch *ds);
  417. int dsa_switch_resume(struct dsa_switch *ds);
  418. #else
  419. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  420. {
  421. return 0;
  422. }
  423. static inline int dsa_switch_resume(struct dsa_switch *ds)
  424. {
  425. return 0;
  426. }
  427. #endif /* CONFIG_PM_SLEEP */
  428. enum dsa_notifier_type {
  429. DSA_PORT_REGISTER,
  430. DSA_PORT_UNREGISTER,
  431. };
  432. struct dsa_notifier_info {
  433. struct net_device *dev;
  434. };
  435. struct dsa_notifier_register_info {
  436. struct dsa_notifier_info info; /* must be first */
  437. struct net_device *master;
  438. unsigned int port_number;
  439. unsigned int switch_number;
  440. };
  441. static inline struct net_device *
  442. dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
  443. {
  444. return info->dev;
  445. }
  446. #if IS_ENABLED(CONFIG_NET_DSA)
  447. int register_dsa_notifier(struct notifier_block *nb);
  448. int unregister_dsa_notifier(struct notifier_block *nb);
  449. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  450. struct dsa_notifier_info *info);
  451. #else
  452. static inline int register_dsa_notifier(struct notifier_block *nb)
  453. {
  454. return 0;
  455. }
  456. static inline int unregister_dsa_notifier(struct notifier_block *nb)
  457. {
  458. return 0;
  459. }
  460. static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  461. struct dsa_notifier_info *info)
  462. {
  463. return NOTIFY_DONE;
  464. }
  465. #endif
  466. /* Broadcom tag specific helpers to insert and extract queue/port number */
  467. #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
  468. #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
  469. #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
  470. #endif