dsa.h 16 KB

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