dsa.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. const char *mac;
  167. struct device_node *dn;
  168. unsigned int ageing_time;
  169. u8 stp_state;
  170. struct net_device *bridge_dev;
  171. struct devlink_port devlink_port;
  172. struct phylink *pl;
  173. /*
  174. * Original copy of the master netdev ethtool_ops
  175. */
  176. const struct ethtool_ops *orig_ethtool_ops;
  177. };
  178. struct dsa_switch {
  179. struct device *dev;
  180. /*
  181. * Parent switch tree, and switch index.
  182. */
  183. struct dsa_switch_tree *dst;
  184. unsigned int index;
  185. /* Listener for switch fabric events */
  186. struct notifier_block nb;
  187. /*
  188. * Give the switch driver somewhere to hang its private data
  189. * structure.
  190. */
  191. void *priv;
  192. /*
  193. * Configuration data for this switch.
  194. */
  195. struct dsa_chip_data *cd;
  196. /*
  197. * The switch operations.
  198. */
  199. const struct dsa_switch_ops *ops;
  200. /*
  201. * An array of which element [a] indicates which port on this
  202. * switch should be used to send packets to that are destined
  203. * for switch a. Can be NULL if there is only one switch chip.
  204. */
  205. s8 rtable[DSA_MAX_SWITCHES];
  206. /*
  207. * Slave mii_bus and devices for the individual ports.
  208. */
  209. u32 phys_mii_mask;
  210. struct mii_bus *slave_mii_bus;
  211. /* Ageing Time limits in msecs */
  212. unsigned int ageing_time_min;
  213. unsigned int ageing_time_max;
  214. /* devlink used to represent this switch device */
  215. struct devlink *devlink;
  216. /* Number of switch port queues */
  217. unsigned int num_tx_queues;
  218. unsigned long *bitmap;
  219. unsigned long _bitmap;
  220. /* Dynamically allocated ports, keep last */
  221. size_t num_ports;
  222. struct dsa_port ports[];
  223. };
  224. static inline const struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
  225. {
  226. return &ds->ports[p];
  227. }
  228. static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
  229. {
  230. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
  231. }
  232. static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
  233. {
  234. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
  235. }
  236. static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
  237. {
  238. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
  239. }
  240. static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
  241. {
  242. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
  243. }
  244. static inline u32 dsa_user_ports(struct dsa_switch *ds)
  245. {
  246. u32 mask = 0;
  247. int p;
  248. for (p = 0; p < ds->num_ports; p++)
  249. if (dsa_is_user_port(ds, p))
  250. mask |= BIT(p);
  251. return mask;
  252. }
  253. /* Return the local port used to reach an arbitrary switch port */
  254. static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
  255. int port)
  256. {
  257. if (device == ds->index)
  258. return port;
  259. else
  260. return ds->rtable[device];
  261. }
  262. /* Return the local port used to reach the dedicated CPU port */
  263. static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
  264. {
  265. const struct dsa_port *dp = dsa_to_port(ds, port);
  266. const struct dsa_port *cpu_dp = dp->cpu_dp;
  267. if (!cpu_dp)
  268. return port;
  269. return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
  270. }
  271. typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
  272. bool is_static, void *data);
  273. struct dsa_switch_ops {
  274. #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
  275. /*
  276. * Legacy probing.
  277. */
  278. const char *(*probe)(struct device *dsa_dev,
  279. struct device *host_dev, int sw_addr,
  280. void **priv);
  281. #endif
  282. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
  283. int port);
  284. int (*setup)(struct dsa_switch *ds);
  285. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  286. /*
  287. * Access to the switch's PHY registers.
  288. */
  289. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  290. int (*phy_write)(struct dsa_switch *ds, int port,
  291. int regnum, u16 val);
  292. /*
  293. * Link state adjustment (called from libphy)
  294. */
  295. void (*adjust_link)(struct dsa_switch *ds, int port,
  296. struct phy_device *phydev);
  297. void (*fixed_link_update)(struct dsa_switch *ds, int port,
  298. struct fixed_phy_status *st);
  299. /*
  300. * PHYLINK integration
  301. */
  302. void (*phylink_validate)(struct dsa_switch *ds, int port,
  303. unsigned long *supported,
  304. struct phylink_link_state *state);
  305. int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
  306. struct phylink_link_state *state);
  307. void (*phylink_mac_config)(struct dsa_switch *ds, int port,
  308. unsigned int mode,
  309. const struct phylink_link_state *state);
  310. void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
  311. void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
  312. unsigned int mode,
  313. phy_interface_t interface);
  314. void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
  315. unsigned int mode,
  316. phy_interface_t interface,
  317. struct phy_device *phydev);
  318. void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
  319. struct phylink_link_state *state);
  320. /*
  321. * ethtool hardware statistics.
  322. */
  323. void (*get_strings)(struct dsa_switch *ds, int port,
  324. u32 stringset, uint8_t *data);
  325. void (*get_ethtool_stats)(struct dsa_switch *ds,
  326. int port, uint64_t *data);
  327. int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
  328. void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
  329. int port, uint64_t *data);
  330. /*
  331. * ethtool Wake-on-LAN
  332. */
  333. void (*get_wol)(struct dsa_switch *ds, int port,
  334. struct ethtool_wolinfo *w);
  335. int (*set_wol)(struct dsa_switch *ds, int port,
  336. struct ethtool_wolinfo *w);
  337. /*
  338. * ethtool timestamp info
  339. */
  340. int (*get_ts_info)(struct dsa_switch *ds, int port,
  341. struct ethtool_ts_info *ts);
  342. /*
  343. * Suspend and resume
  344. */
  345. int (*suspend)(struct dsa_switch *ds);
  346. int (*resume)(struct dsa_switch *ds);
  347. /*
  348. * Port enable/disable
  349. */
  350. int (*port_enable)(struct dsa_switch *ds, int port,
  351. struct phy_device *phy);
  352. void (*port_disable)(struct dsa_switch *ds, int port,
  353. struct phy_device *phy);
  354. /*
  355. * Port's MAC EEE settings
  356. */
  357. int (*set_mac_eee)(struct dsa_switch *ds, int port,
  358. struct ethtool_eee *e);
  359. int (*get_mac_eee)(struct dsa_switch *ds, int port,
  360. struct ethtool_eee *e);
  361. /* EEPROM access */
  362. int (*get_eeprom_len)(struct dsa_switch *ds);
  363. int (*get_eeprom)(struct dsa_switch *ds,
  364. struct ethtool_eeprom *eeprom, u8 *data);
  365. int (*set_eeprom)(struct dsa_switch *ds,
  366. struct ethtool_eeprom *eeprom, u8 *data);
  367. /*
  368. * Register access.
  369. */
  370. int (*get_regs_len)(struct dsa_switch *ds, int port);
  371. void (*get_regs)(struct dsa_switch *ds, int port,
  372. struct ethtool_regs *regs, void *p);
  373. /*
  374. * Bridge integration
  375. */
  376. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  377. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  378. struct net_device *bridge);
  379. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  380. struct net_device *bridge);
  381. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  382. u8 state);
  383. void (*port_fast_age)(struct dsa_switch *ds, int port);
  384. /*
  385. * VLAN support
  386. */
  387. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  388. bool vlan_filtering);
  389. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  390. const struct switchdev_obj_port_vlan *vlan);
  391. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  392. const struct switchdev_obj_port_vlan *vlan);
  393. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  394. const struct switchdev_obj_port_vlan *vlan);
  395. /*
  396. * Forwarding database
  397. */
  398. int (*port_fdb_add)(struct dsa_switch *ds, int port,
  399. const unsigned char *addr, u16 vid);
  400. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  401. const unsigned char *addr, u16 vid);
  402. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  403. dsa_fdb_dump_cb_t *cb, void *data);
  404. /*
  405. * Multicast database
  406. */
  407. int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
  408. const struct switchdev_obj_port_mdb *mdb);
  409. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  410. const struct switchdev_obj_port_mdb *mdb);
  411. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  412. const struct switchdev_obj_port_mdb *mdb);
  413. /*
  414. * RXNFC
  415. */
  416. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  417. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  418. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  419. struct ethtool_rxnfc *nfc);
  420. /*
  421. * TC integration
  422. */
  423. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  424. struct dsa_mall_mirror_tc_entry *mirror,
  425. bool ingress);
  426. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  427. struct dsa_mall_mirror_tc_entry *mirror);
  428. /*
  429. * Cross-chip operations
  430. */
  431. int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
  432. int port, struct net_device *br);
  433. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
  434. int port, struct net_device *br);
  435. /*
  436. * PTP functionality
  437. */
  438. int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
  439. struct ifreq *ifr);
  440. int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
  441. struct ifreq *ifr);
  442. bool (*port_txtstamp)(struct dsa_switch *ds, int port,
  443. struct sk_buff *clone, unsigned int type);
  444. bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
  445. struct sk_buff *skb, unsigned int type);
  446. };
  447. struct dsa_switch_driver {
  448. struct list_head list;
  449. const struct dsa_switch_ops *ops;
  450. };
  451. #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
  452. /* Legacy driver registration */
  453. void register_switch_driver(struct dsa_switch_driver *type);
  454. void unregister_switch_driver(struct dsa_switch_driver *type);
  455. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  456. #else
  457. static inline void register_switch_driver(struct dsa_switch_driver *type) { }
  458. static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
  459. static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  460. {
  461. return NULL;
  462. }
  463. #endif
  464. struct net_device *dsa_dev_to_net_device(struct device *dev);
  465. /* Keep inline for faster access in hot path */
  466. static inline bool netdev_uses_dsa(struct net_device *dev)
  467. {
  468. #if IS_ENABLED(CONFIG_NET_DSA)
  469. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  470. #endif
  471. return false;
  472. }
  473. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
  474. void dsa_unregister_switch(struct dsa_switch *ds);
  475. int dsa_register_switch(struct dsa_switch *ds);
  476. #ifdef CONFIG_PM_SLEEP
  477. int dsa_switch_suspend(struct dsa_switch *ds);
  478. int dsa_switch_resume(struct dsa_switch *ds);
  479. #else
  480. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  481. {
  482. return 0;
  483. }
  484. static inline int dsa_switch_resume(struct dsa_switch *ds)
  485. {
  486. return 0;
  487. }
  488. #endif /* CONFIG_PM_SLEEP */
  489. enum dsa_notifier_type {
  490. DSA_PORT_REGISTER,
  491. DSA_PORT_UNREGISTER,
  492. };
  493. struct dsa_notifier_info {
  494. struct net_device *dev;
  495. };
  496. struct dsa_notifier_register_info {
  497. struct dsa_notifier_info info; /* must be first */
  498. struct net_device *master;
  499. unsigned int port_number;
  500. unsigned int switch_number;
  501. };
  502. static inline struct net_device *
  503. dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
  504. {
  505. return info->dev;
  506. }
  507. #if IS_ENABLED(CONFIG_NET_DSA)
  508. int register_dsa_notifier(struct notifier_block *nb);
  509. int unregister_dsa_notifier(struct notifier_block *nb);
  510. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  511. struct dsa_notifier_info *info);
  512. #else
  513. static inline int register_dsa_notifier(struct notifier_block *nb)
  514. {
  515. return 0;
  516. }
  517. static inline int unregister_dsa_notifier(struct notifier_block *nb)
  518. {
  519. return 0;
  520. }
  521. static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  522. struct dsa_notifier_info *info)
  523. {
  524. return NOTIFY_DONE;
  525. }
  526. #endif
  527. /* Broadcom tag specific helpers to insert and extract queue/port number */
  528. #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
  529. #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
  530. #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
  531. int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
  532. int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
  533. int dsa_port_get_phy_sset_count(struct dsa_port *dp);
  534. void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
  535. #endif