dsa.h 16 KB

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