dsa.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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, uint8_t *data);
  297. void (*get_ethtool_stats)(struct dsa_switch *ds,
  298. int port, uint64_t *data);
  299. int (*get_sset_count)(struct dsa_switch *ds);
  300. /*
  301. * ethtool Wake-on-LAN
  302. */
  303. void (*get_wol)(struct dsa_switch *ds, int port,
  304. struct ethtool_wolinfo *w);
  305. int (*set_wol)(struct dsa_switch *ds, int port,
  306. struct ethtool_wolinfo *w);
  307. /*
  308. * ethtool timestamp info
  309. */
  310. int (*get_ts_info)(struct dsa_switch *ds, int port,
  311. struct ethtool_ts_info *ts);
  312. /*
  313. * Suspend and resume
  314. */
  315. int (*suspend)(struct dsa_switch *ds);
  316. int (*resume)(struct dsa_switch *ds);
  317. /*
  318. * Port enable/disable
  319. */
  320. int (*port_enable)(struct dsa_switch *ds, int port,
  321. struct phy_device *phy);
  322. void (*port_disable)(struct dsa_switch *ds, int port,
  323. struct phy_device *phy);
  324. /*
  325. * Port's MAC EEE settings
  326. */
  327. int (*set_mac_eee)(struct dsa_switch *ds, int port,
  328. struct ethtool_eee *e);
  329. int (*get_mac_eee)(struct dsa_switch *ds, int port,
  330. struct ethtool_eee *e);
  331. /* EEPROM access */
  332. int (*get_eeprom_len)(struct dsa_switch *ds);
  333. int (*get_eeprom)(struct dsa_switch *ds,
  334. struct ethtool_eeprom *eeprom, u8 *data);
  335. int (*set_eeprom)(struct dsa_switch *ds,
  336. struct ethtool_eeprom *eeprom, u8 *data);
  337. /*
  338. * Register access.
  339. */
  340. int (*get_regs_len)(struct dsa_switch *ds, int port);
  341. void (*get_regs)(struct dsa_switch *ds, int port,
  342. struct ethtool_regs *regs, void *p);
  343. /*
  344. * Bridge integration
  345. */
  346. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  347. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  348. struct net_device *bridge);
  349. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  350. struct net_device *bridge);
  351. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  352. u8 state);
  353. void (*port_fast_age)(struct dsa_switch *ds, int port);
  354. /*
  355. * VLAN support
  356. */
  357. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  358. bool vlan_filtering);
  359. int (*port_vlan_prepare)(struct dsa_switch *ds, int port,
  360. const struct switchdev_obj_port_vlan *vlan);
  361. void (*port_vlan_add)(struct dsa_switch *ds, int port,
  362. const struct switchdev_obj_port_vlan *vlan);
  363. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  364. const struct switchdev_obj_port_vlan *vlan);
  365. /*
  366. * Forwarding database
  367. */
  368. int (*port_fdb_add)(struct dsa_switch *ds, int port,
  369. const unsigned char *addr, u16 vid);
  370. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  371. const unsigned char *addr, u16 vid);
  372. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  373. dsa_fdb_dump_cb_t *cb, void *data);
  374. /*
  375. * Multicast database
  376. */
  377. int (*port_mdb_prepare)(struct dsa_switch *ds, int port,
  378. const struct switchdev_obj_port_mdb *mdb);
  379. void (*port_mdb_add)(struct dsa_switch *ds, int port,
  380. const struct switchdev_obj_port_mdb *mdb);
  381. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  382. const struct switchdev_obj_port_mdb *mdb);
  383. /*
  384. * RXNFC
  385. */
  386. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  387. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  388. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  389. struct ethtool_rxnfc *nfc);
  390. /*
  391. * TC integration
  392. */
  393. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  394. struct dsa_mall_mirror_tc_entry *mirror,
  395. bool ingress);
  396. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  397. struct dsa_mall_mirror_tc_entry *mirror);
  398. /*
  399. * Cross-chip operations
  400. */
  401. int (*crosschip_bridge_join)(struct dsa_switch *ds, int sw_index,
  402. int port, struct net_device *br);
  403. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int sw_index,
  404. int port, struct net_device *br);
  405. /*
  406. * PTP functionality
  407. */
  408. int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
  409. struct ifreq *ifr);
  410. int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
  411. struct ifreq *ifr);
  412. bool (*port_txtstamp)(struct dsa_switch *ds, int port,
  413. struct sk_buff *clone, unsigned int type);
  414. bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
  415. struct sk_buff *skb, unsigned int type);
  416. };
  417. struct dsa_switch_driver {
  418. struct list_head list;
  419. const struct dsa_switch_ops *ops;
  420. };
  421. #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
  422. /* Legacy driver registration */
  423. void register_switch_driver(struct dsa_switch_driver *type);
  424. void unregister_switch_driver(struct dsa_switch_driver *type);
  425. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
  426. #else
  427. static inline void register_switch_driver(struct dsa_switch_driver *type) { }
  428. static inline void unregister_switch_driver(struct dsa_switch_driver *type) { }
  429. static inline struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  430. {
  431. return NULL;
  432. }
  433. #endif
  434. struct net_device *dsa_dev_to_net_device(struct device *dev);
  435. /* Keep inline for faster access in hot path */
  436. static inline bool netdev_uses_dsa(struct net_device *dev)
  437. {
  438. #if IS_ENABLED(CONFIG_NET_DSA)
  439. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  440. #endif
  441. return false;
  442. }
  443. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n);
  444. void dsa_unregister_switch(struct dsa_switch *ds);
  445. int dsa_register_switch(struct dsa_switch *ds);
  446. #ifdef CONFIG_PM_SLEEP
  447. int dsa_switch_suspend(struct dsa_switch *ds);
  448. int dsa_switch_resume(struct dsa_switch *ds);
  449. #else
  450. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  451. {
  452. return 0;
  453. }
  454. static inline int dsa_switch_resume(struct dsa_switch *ds)
  455. {
  456. return 0;
  457. }
  458. #endif /* CONFIG_PM_SLEEP */
  459. enum dsa_notifier_type {
  460. DSA_PORT_REGISTER,
  461. DSA_PORT_UNREGISTER,
  462. };
  463. struct dsa_notifier_info {
  464. struct net_device *dev;
  465. };
  466. struct dsa_notifier_register_info {
  467. struct dsa_notifier_info info; /* must be first */
  468. struct net_device *master;
  469. unsigned int port_number;
  470. unsigned int switch_number;
  471. };
  472. static inline struct net_device *
  473. dsa_notifier_info_to_dev(const struct dsa_notifier_info *info)
  474. {
  475. return info->dev;
  476. }
  477. #if IS_ENABLED(CONFIG_NET_DSA)
  478. int register_dsa_notifier(struct notifier_block *nb);
  479. int unregister_dsa_notifier(struct notifier_block *nb);
  480. int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  481. struct dsa_notifier_info *info);
  482. #else
  483. static inline int register_dsa_notifier(struct notifier_block *nb)
  484. {
  485. return 0;
  486. }
  487. static inline int unregister_dsa_notifier(struct notifier_block *nb)
  488. {
  489. return 0;
  490. }
  491. static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
  492. struct dsa_notifier_info *info)
  493. {
  494. return NOTIFY_DONE;
  495. }
  496. #endif
  497. /* Broadcom tag specific helpers to insert and extract queue/port number */
  498. #define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
  499. #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
  500. #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
  501. #endif