dsa.h 15 KB

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