dsa.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * include/net/dsa.h - Driver for Distributed Switch Architecture switch chips
  4. * Copyright (c) 2008-2009 Marvell Semiconductor
  5. */
  6. #ifndef __LINUX_NET_DSA_H
  7. #define __LINUX_NET_DSA_H
  8. #include <linux/if.h>
  9. #include <linux/if_ether.h>
  10. #include <linux/list.h>
  11. #include <linux/notifier.h>
  12. #include <linux/timer.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/of.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/net_tstamp.h>
  17. #include <linux/phy.h>
  18. #include <linux/platform_data/dsa.h>
  19. #include <linux/phylink.h>
  20. #include <net/devlink.h>
  21. #include <net/switchdev.h>
  22. struct dsa_8021q_context;
  23. struct tc_action;
  24. #define DSA_TAG_PROTO_NONE_VALUE 0
  25. #define DSA_TAG_PROTO_BRCM_VALUE 1
  26. #define DSA_TAG_PROTO_BRCM_PREPEND_VALUE 2
  27. #define DSA_TAG_PROTO_DSA_VALUE 3
  28. #define DSA_TAG_PROTO_EDSA_VALUE 4
  29. #define DSA_TAG_PROTO_GSWIP_VALUE 5
  30. #define DSA_TAG_PROTO_KSZ9477_VALUE 6
  31. #define DSA_TAG_PROTO_KSZ9893_VALUE 7
  32. #define DSA_TAG_PROTO_LAN9303_VALUE 8
  33. #define DSA_TAG_PROTO_MTK_VALUE 9
  34. #define DSA_TAG_PROTO_QCA_VALUE 10
  35. #define DSA_TAG_PROTO_TRAILER_VALUE 11
  36. #define DSA_TAG_PROTO_8021Q_VALUE 12
  37. #define DSA_TAG_PROTO_SJA1105_VALUE 13
  38. #define DSA_TAG_PROTO_KSZ8795_VALUE 14
  39. #define DSA_TAG_PROTO_OCELOT_VALUE 15
  40. #define DSA_TAG_PROTO_AR9331_VALUE 16
  41. #define DSA_TAG_PROTO_RTL4_A_VALUE 17
  42. #define DSA_TAG_PROTO_HELLCREEK_VALUE 18
  43. #define DSA_TAG_PROTO_XRS700X_VALUE 19
  44. #define DSA_TAG_PROTO_OCELOT_8021Q_VALUE 20
  45. #define DSA_TAG_PROTO_SEVILLE_VALUE 21
  46. #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22
  47. #define DSA_TAG_PROTO_SJA1110_VALUE 23
  48. #define DSA_TAG_PROTO_RTL8_4_VALUE 24
  49. #define DSA_TAG_PROTO_RTL8_4T_VALUE 25
  50. #define DSA_TAG_PROTO_RZN1_A5PSW_VALUE 26
  51. #define DSA_TAG_PROTO_LAN937X_VALUE 27
  52. #define DSA_TAG_PROTO_VSC73XX_8021Q_VALUE 28
  53. #define DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE 29
  54. #define DSA_TAG_PROTO_YT921X_VALUE 30
  55. #define DSA_TAG_PROTO_MXL_GSW1XX_VALUE 31
  56. #define DSA_TAG_PROTO_MXL862_VALUE 32
  57. enum dsa_tag_protocol {
  58. DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
  59. DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
  60. DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
  61. DSA_TAG_PROTO_BRCM_LEGACY_FCS = DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE,
  62. DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
  63. DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
  64. DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
  65. DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
  66. DSA_TAG_PROTO_KSZ9477 = DSA_TAG_PROTO_KSZ9477_VALUE,
  67. DSA_TAG_PROTO_KSZ9893 = DSA_TAG_PROTO_KSZ9893_VALUE,
  68. DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
  69. DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
  70. DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
  71. DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
  72. DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
  73. DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
  74. DSA_TAG_PROTO_KSZ8795 = DSA_TAG_PROTO_KSZ8795_VALUE,
  75. DSA_TAG_PROTO_OCELOT = DSA_TAG_PROTO_OCELOT_VALUE,
  76. DSA_TAG_PROTO_AR9331 = DSA_TAG_PROTO_AR9331_VALUE,
  77. DSA_TAG_PROTO_RTL4_A = DSA_TAG_PROTO_RTL4_A_VALUE,
  78. DSA_TAG_PROTO_HELLCREEK = DSA_TAG_PROTO_HELLCREEK_VALUE,
  79. DSA_TAG_PROTO_XRS700X = DSA_TAG_PROTO_XRS700X_VALUE,
  80. DSA_TAG_PROTO_OCELOT_8021Q = DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
  81. DSA_TAG_PROTO_SEVILLE = DSA_TAG_PROTO_SEVILLE_VALUE,
  82. DSA_TAG_PROTO_SJA1110 = DSA_TAG_PROTO_SJA1110_VALUE,
  83. DSA_TAG_PROTO_RTL8_4 = DSA_TAG_PROTO_RTL8_4_VALUE,
  84. DSA_TAG_PROTO_RTL8_4T = DSA_TAG_PROTO_RTL8_4T_VALUE,
  85. DSA_TAG_PROTO_RZN1_A5PSW = DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
  86. DSA_TAG_PROTO_LAN937X = DSA_TAG_PROTO_LAN937X_VALUE,
  87. DSA_TAG_PROTO_VSC73XX_8021Q = DSA_TAG_PROTO_VSC73XX_8021Q_VALUE,
  88. DSA_TAG_PROTO_YT921X = DSA_TAG_PROTO_YT921X_VALUE,
  89. DSA_TAG_PROTO_MXL_GSW1XX = DSA_TAG_PROTO_MXL_GSW1XX_VALUE,
  90. DSA_TAG_PROTO_MXL862 = DSA_TAG_PROTO_MXL862_VALUE,
  91. };
  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. void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
  97. int *offset);
  98. int (*connect)(struct dsa_switch *ds);
  99. void (*disconnect)(struct dsa_switch *ds);
  100. unsigned int needed_headroom;
  101. unsigned int needed_tailroom;
  102. const char *name;
  103. enum dsa_tag_protocol proto;
  104. /* Some tagging protocols either mangle or shift the destination MAC
  105. * address, in which case the DSA conduit would drop packets on ingress
  106. * if what it understands out of the destination MAC address is not in
  107. * its RX filter.
  108. */
  109. bool promisc_on_conduit;
  110. };
  111. struct dsa_lag {
  112. struct net_device *dev;
  113. unsigned int id;
  114. struct mutex fdb_lock;
  115. struct list_head fdbs;
  116. refcount_t refcount;
  117. };
  118. struct dsa_switch_tree {
  119. struct list_head list;
  120. /* List of switch ports */
  121. struct list_head ports;
  122. /* Notifier chain for switch-wide events */
  123. struct raw_notifier_head nh;
  124. /* Tree identifier */
  125. unsigned int index;
  126. /* Number of switches attached to this tree */
  127. struct kref refcount;
  128. /* Maps offloaded LAG netdevs to a zero-based linear ID for
  129. * drivers that need it.
  130. */
  131. struct dsa_lag **lags;
  132. /* Tagging protocol operations */
  133. const struct dsa_device_ops *tag_ops;
  134. /* Default tagging protocol preferred by the switches in this
  135. * tree.
  136. */
  137. enum dsa_tag_protocol default_proto;
  138. /* Has this tree been applied to the hardware? */
  139. bool setup;
  140. /*
  141. * Configuration data for the platform device that owns
  142. * this dsa switch tree instance.
  143. */
  144. struct dsa_platform_data *pd;
  145. /* List of DSA links composing the routing table */
  146. struct list_head rtable;
  147. /* Length of "lags" array */
  148. unsigned int lags_len;
  149. /* Track the largest switch index within a tree */
  150. unsigned int last_switch;
  151. };
  152. /* LAG IDs are one-based, the dst->lags array is zero-based */
  153. #define dsa_lags_foreach_id(_id, _dst) \
  154. for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
  155. if ((_dst)->lags[(_id) - 1])
  156. #define dsa_lag_foreach_port(_dp, _dst, _lag) \
  157. list_for_each_entry((_dp), &(_dst)->ports, list) \
  158. if (dsa_port_offloads_lag((_dp), (_lag)))
  159. #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
  160. list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
  161. if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
  162. static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
  163. unsigned int id)
  164. {
  165. /* DSA LAG IDs are one-based, dst->lags is zero-based */
  166. return dst->lags[id - 1];
  167. }
  168. static inline int dsa_lag_id(struct dsa_switch_tree *dst,
  169. struct net_device *lag_dev)
  170. {
  171. unsigned int id;
  172. dsa_lags_foreach_id(id, dst) {
  173. struct dsa_lag *lag = dsa_lag_by_id(dst, id);
  174. if (lag->dev == lag_dev)
  175. return lag->id;
  176. }
  177. return -ENODEV;
  178. }
  179. /* TC matchall action types */
  180. enum dsa_port_mall_action_type {
  181. DSA_PORT_MALL_MIRROR,
  182. DSA_PORT_MALL_POLICER,
  183. };
  184. /* TC mirroring entry */
  185. struct dsa_mall_mirror_tc_entry {
  186. u8 to_local_port;
  187. bool ingress;
  188. };
  189. /* TC matchall entry */
  190. struct dsa_mall_tc_entry {
  191. struct list_head list;
  192. unsigned long cookie;
  193. enum dsa_port_mall_action_type type;
  194. union {
  195. struct dsa_mall_mirror_tc_entry mirror;
  196. struct flow_action_police policer;
  197. };
  198. };
  199. struct dsa_bridge {
  200. struct net_device *dev;
  201. unsigned int num;
  202. bool tx_fwd_offload;
  203. refcount_t refcount;
  204. };
  205. struct dsa_port {
  206. /* A CPU port is physically connected to a conduit device. A user port
  207. * exposes a network device to user-space, called 'user' here.
  208. */
  209. union {
  210. struct net_device *conduit;
  211. struct net_device *user;
  212. };
  213. /* Copy of the tagging protocol operations, for quicker access
  214. * in the data path. Valid only for the CPU ports.
  215. */
  216. const struct dsa_device_ops *tag_ops;
  217. /* Copies for faster access in conduit receive hot path */
  218. struct dsa_switch_tree *dst;
  219. struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
  220. struct dsa_switch *ds;
  221. unsigned int index;
  222. enum {
  223. DSA_PORT_TYPE_UNUSED = 0,
  224. DSA_PORT_TYPE_CPU,
  225. DSA_PORT_TYPE_DSA,
  226. DSA_PORT_TYPE_USER,
  227. } type;
  228. const char *name;
  229. struct dsa_port *cpu_dp;
  230. u8 mac[ETH_ALEN];
  231. u8 stp_state;
  232. /* Warning: the following bit fields are not atomic, and updating them
  233. * can only be done from code paths where concurrency is not possible
  234. * (probe time or under rtnl_lock).
  235. */
  236. u8 vlan_filtering:1;
  237. /* Managed by DSA on user ports and by drivers on CPU and DSA ports */
  238. u8 learning:1;
  239. u8 lag_tx_enabled:1;
  240. /* conduit state bits, valid only on CPU ports */
  241. u8 conduit_admin_up:1;
  242. u8 conduit_oper_up:1;
  243. /* Valid only on user ports */
  244. u8 cpu_port_in_lag:1;
  245. u8 setup:1;
  246. struct device_node *dn;
  247. unsigned int ageing_time;
  248. struct dsa_bridge *bridge;
  249. struct devlink_port devlink_port;
  250. struct phylink *pl;
  251. struct phylink_config pl_config;
  252. netdevice_tracker conduit_tracker;
  253. struct dsa_lag *lag;
  254. struct net_device *hsr_dev;
  255. struct list_head list;
  256. /*
  257. * Original copy of the conduit netdev ethtool_ops
  258. */
  259. const struct ethtool_ops *orig_ethtool_ops;
  260. /* List of MAC addresses that must be forwarded on this port.
  261. * These are only valid on CPU ports and DSA links.
  262. */
  263. struct mutex addr_lists_lock;
  264. struct list_head fdbs;
  265. struct list_head mdbs;
  266. struct mutex vlans_lock;
  267. union {
  268. /* List of VLANs that CPU and DSA ports are members of.
  269. * Access to this is serialized by the sleepable @vlans_lock.
  270. */
  271. struct list_head vlans;
  272. /* List of VLANs that user ports are members of.
  273. * Access to this is serialized by netif_addr_lock_bh().
  274. */
  275. struct list_head user_vlans;
  276. };
  277. };
  278. static inline struct dsa_port *
  279. dsa_phylink_to_port(struct phylink_config *config)
  280. {
  281. return container_of(config, struct dsa_port, pl_config);
  282. }
  283. /* TODO: ideally DSA ports would have a single dp->link_dp member,
  284. * and no dst->rtable nor this struct dsa_link would be needed,
  285. * but this would require some more complex tree walking,
  286. * so keep it stupid at the moment and list them all.
  287. */
  288. struct dsa_link {
  289. struct dsa_port *dp;
  290. struct dsa_port *link_dp;
  291. struct list_head list;
  292. };
  293. enum dsa_db_type {
  294. DSA_DB_PORT,
  295. DSA_DB_LAG,
  296. DSA_DB_BRIDGE,
  297. };
  298. struct dsa_db {
  299. enum dsa_db_type type;
  300. union {
  301. const struct dsa_port *dp;
  302. struct dsa_lag lag;
  303. struct dsa_bridge bridge;
  304. };
  305. };
  306. struct dsa_mac_addr {
  307. unsigned char addr[ETH_ALEN];
  308. u16 vid;
  309. refcount_t refcount;
  310. struct list_head list;
  311. struct dsa_db db;
  312. };
  313. struct dsa_vlan {
  314. u16 vid;
  315. refcount_t refcount;
  316. struct list_head list;
  317. };
  318. struct dsa_switch {
  319. struct device *dev;
  320. /*
  321. * Parent switch tree, and switch index.
  322. */
  323. struct dsa_switch_tree *dst;
  324. unsigned int index;
  325. /* Warning: the following bit fields are not atomic, and updating them
  326. * can only be done from code paths where concurrency is not possible
  327. * (probe time or under rtnl_lock).
  328. */
  329. u32 setup:1;
  330. /* Disallow bridge core from requesting different VLAN awareness
  331. * settings on ports if not hardware-supported
  332. */
  333. u32 vlan_filtering_is_global:1;
  334. /* Keep VLAN filtering enabled on ports not offloading any upper */
  335. u32 needs_standalone_vlan_filtering:1;
  336. /* Pass .port_vlan_add and .port_vlan_del to drivers even for bridges
  337. * that have vlan_filtering=0. All drivers should ideally set this (and
  338. * then the option would get removed), but it is unknown whether this
  339. * would break things or not.
  340. */
  341. u32 configure_vlan_while_not_filtering:1;
  342. /* Pop the default_pvid of VLAN-unaware bridge ports from tagged frames.
  343. * DEPRECATED: Do NOT set this field in new drivers. Instead look at
  344. * the dsa_software_vlan_untag() comments.
  345. */
  346. u32 untag_bridge_pvid:1;
  347. /* Pop the default_pvid of VLAN-aware bridge ports from tagged frames.
  348. * Useful if the switch cannot preserve the VLAN tag as seen on the
  349. * wire for user port ingress, and chooses to send all frames as
  350. * VLAN-tagged to the CPU, including those which were originally
  351. * untagged.
  352. */
  353. u32 untag_vlan_aware_bridge_pvid:1;
  354. /* Let DSA manage the FDB entries towards the
  355. * CPU, based on the software bridge database.
  356. */
  357. u32 assisted_learning_on_cpu_port:1;
  358. /* In case vlan_filtering_is_global is set, the VLAN awareness state
  359. * should be retrieved from here and not from the per-port settings.
  360. */
  361. u32 vlan_filtering:1;
  362. /* For switches that only have the MRU configurable. To ensure the
  363. * configured MTU is not exceeded, normalization of MRU on all bridged
  364. * interfaces is needed.
  365. */
  366. u32 mtu_enforcement_ingress:1;
  367. /* Drivers that isolate the FDBs of multiple bridges must set this
  368. * to true to receive the bridge as an argument in .port_fdb_{add,del}
  369. * and .port_mdb_{add,del}. Otherwise, the bridge.num will always be
  370. * passed as zero.
  371. */
  372. u32 fdb_isolation:1;
  373. /* Drivers that have global DSCP mapping settings must set this to
  374. * true to automatically apply the settings to all ports.
  375. */
  376. u32 dscp_prio_mapping_is_global:1;
  377. /* Listener for switch fabric events */
  378. struct notifier_block nb;
  379. /*
  380. * Give the switch driver somewhere to hang its private data
  381. * structure.
  382. */
  383. void *priv;
  384. void *tagger_data;
  385. /*
  386. * Configuration data for this switch.
  387. */
  388. struct dsa_chip_data *cd;
  389. /*
  390. * The switch operations.
  391. */
  392. const struct dsa_switch_ops *ops;
  393. /*
  394. * Allow a DSA switch driver to override the phylink MAC ops
  395. */
  396. const struct phylink_mac_ops *phylink_mac_ops;
  397. /*
  398. * User mii_bus and devices for the individual ports.
  399. */
  400. u32 phys_mii_mask;
  401. struct mii_bus *user_mii_bus;
  402. /* Ageing Time limits in msecs */
  403. unsigned int ageing_time_min;
  404. unsigned int ageing_time_max;
  405. /* Storage for drivers using tag_8021q */
  406. struct dsa_8021q_context *tag_8021q_ctx;
  407. /* devlink used to represent this switch device */
  408. struct devlink *devlink;
  409. /* Number of switch port queues */
  410. unsigned int num_tx_queues;
  411. /* Drivers that benefit from having an ID associated with each
  412. * offloaded LAG should set this to the maximum number of
  413. * supported IDs. DSA will then maintain a mapping of _at
  414. * least_ these many IDs, accessible to drivers via
  415. * dsa_lag_id().
  416. */
  417. unsigned int num_lag_ids;
  418. /* Drivers that support bridge forwarding offload or FDB isolation
  419. * should set this to the maximum number of bridges spanning the same
  420. * switch tree (or all trees, in the case of cross-tree bridging
  421. * support) that can be offloaded.
  422. */
  423. unsigned int max_num_bridges;
  424. unsigned int num_ports;
  425. };
  426. static inline struct dsa_port *dsa_to_port(struct dsa_switch *ds, int p)
  427. {
  428. struct dsa_switch_tree *dst = ds->dst;
  429. struct dsa_port *dp;
  430. list_for_each_entry(dp, &dst->ports, list)
  431. if (dp->ds == ds && dp->index == p)
  432. return dp;
  433. return NULL;
  434. }
  435. static inline bool dsa_port_is_dsa(struct dsa_port *port)
  436. {
  437. return port->type == DSA_PORT_TYPE_DSA;
  438. }
  439. static inline bool dsa_port_is_cpu(struct dsa_port *port)
  440. {
  441. return port->type == DSA_PORT_TYPE_CPU;
  442. }
  443. static inline bool dsa_port_is_user(struct dsa_port *dp)
  444. {
  445. return dp->type == DSA_PORT_TYPE_USER;
  446. }
  447. static inline bool dsa_port_is_unused(struct dsa_port *dp)
  448. {
  449. return dp->type == DSA_PORT_TYPE_UNUSED;
  450. }
  451. static inline bool dsa_port_conduit_is_operational(struct dsa_port *dp)
  452. {
  453. return dsa_port_is_cpu(dp) && dp->conduit_admin_up &&
  454. dp->conduit_oper_up;
  455. }
  456. static inline bool dsa_is_unused_port(struct dsa_switch *ds, int p)
  457. {
  458. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_UNUSED;
  459. }
  460. static inline bool dsa_is_cpu_port(struct dsa_switch *ds, int p)
  461. {
  462. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_CPU;
  463. }
  464. static inline bool dsa_is_dsa_port(struct dsa_switch *ds, int p)
  465. {
  466. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_DSA;
  467. }
  468. static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
  469. {
  470. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
  471. }
  472. #define dsa_tree_for_each_user_port(_dp, _dst) \
  473. list_for_each_entry((_dp), &(_dst)->ports, list) \
  474. if (dsa_port_is_user((_dp)))
  475. #define dsa_tree_for_each_user_port_continue_reverse(_dp, _dst) \
  476. list_for_each_entry_continue_reverse((_dp), &(_dst)->ports, list) \
  477. if (dsa_port_is_user((_dp)))
  478. #define dsa_tree_for_each_cpu_port(_dp, _dst) \
  479. list_for_each_entry((_dp), &(_dst)->ports, list) \
  480. if (dsa_port_is_cpu((_dp)))
  481. #define dsa_switch_for_each_port(_dp, _ds) \
  482. list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
  483. if ((_dp)->ds == (_ds))
  484. #define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
  485. list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
  486. if ((_dp)->ds == (_ds))
  487. #define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
  488. list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
  489. if ((_dp)->ds == (_ds))
  490. #define dsa_switch_for_each_available_port(_dp, _ds) \
  491. dsa_switch_for_each_port((_dp), (_ds)) \
  492. if (!dsa_port_is_unused((_dp)))
  493. #define dsa_switch_for_each_user_port(_dp, _ds) \
  494. dsa_switch_for_each_port((_dp), (_ds)) \
  495. if (dsa_port_is_user((_dp)))
  496. #define dsa_switch_for_each_user_port_continue_reverse(_dp, _ds) \
  497. dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
  498. if (dsa_port_is_user((_dp)))
  499. #define dsa_switch_for_each_cpu_port(_dp, _ds) \
  500. dsa_switch_for_each_port((_dp), (_ds)) \
  501. if (dsa_port_is_cpu((_dp)))
  502. #define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
  503. dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
  504. if (dsa_port_is_cpu((_dp)))
  505. static inline u32 dsa_user_ports(struct dsa_switch *ds)
  506. {
  507. struct dsa_port *dp;
  508. u32 mask = 0;
  509. dsa_switch_for_each_user_port(dp, ds)
  510. mask |= BIT(dp->index);
  511. return mask;
  512. }
  513. static inline u32 dsa_cpu_ports(struct dsa_switch *ds)
  514. {
  515. struct dsa_port *cpu_dp;
  516. u32 mask = 0;
  517. dsa_switch_for_each_cpu_port(cpu_dp, ds)
  518. mask |= BIT(cpu_dp->index);
  519. return mask;
  520. }
  521. /* Return the local port used to reach an arbitrary switch device */
  522. static inline unsigned int dsa_routing_port(struct dsa_switch *ds, int device)
  523. {
  524. struct dsa_switch_tree *dst = ds->dst;
  525. struct dsa_link *dl;
  526. list_for_each_entry(dl, &dst->rtable, list)
  527. if (dl->dp->ds == ds && dl->link_dp->ds->index == device)
  528. return dl->dp->index;
  529. return ds->num_ports;
  530. }
  531. /* Return the local port used to reach an arbitrary switch port */
  532. static inline unsigned int dsa_towards_port(struct dsa_switch *ds, int device,
  533. int port)
  534. {
  535. if (device == ds->index)
  536. return port;
  537. else
  538. return dsa_routing_port(ds, device);
  539. }
  540. /* Return the local port used to reach the dedicated CPU port */
  541. static inline unsigned int dsa_upstream_port(struct dsa_switch *ds, int port)
  542. {
  543. const struct dsa_port *dp = dsa_to_port(ds, port);
  544. const struct dsa_port *cpu_dp = dp->cpu_dp;
  545. if (!cpu_dp)
  546. return port;
  547. return dsa_towards_port(ds, cpu_dp->ds->index, cpu_dp->index);
  548. }
  549. /* Return true if this is the local port used to reach the CPU port */
  550. static inline bool dsa_is_upstream_port(struct dsa_switch *ds, int port)
  551. {
  552. if (dsa_is_unused_port(ds, port))
  553. return false;
  554. return port == dsa_upstream_port(ds, port);
  555. }
  556. /* Return true if this is a DSA port leading away from the CPU */
  557. static inline bool dsa_is_downstream_port(struct dsa_switch *ds, int port)
  558. {
  559. return dsa_is_dsa_port(ds, port) && !dsa_is_upstream_port(ds, port);
  560. }
  561. /* Return the local port used to reach the CPU port */
  562. static inline unsigned int dsa_switch_upstream_port(struct dsa_switch *ds)
  563. {
  564. struct dsa_port *dp;
  565. dsa_switch_for_each_available_port(dp, ds) {
  566. return dsa_upstream_port(ds, dp->index);
  567. }
  568. return ds->num_ports;
  569. }
  570. /* Return true if @upstream_ds is an upstream switch of @downstream_ds, meaning
  571. * that the routing port from @downstream_ds to @upstream_ds is also the port
  572. * which @downstream_ds uses to reach its dedicated CPU.
  573. */
  574. static inline bool dsa_switch_is_upstream_of(struct dsa_switch *upstream_ds,
  575. struct dsa_switch *downstream_ds)
  576. {
  577. int routing_port;
  578. if (upstream_ds == downstream_ds)
  579. return true;
  580. routing_port = dsa_routing_port(downstream_ds, upstream_ds->index);
  581. return dsa_is_upstream_port(downstream_ds, routing_port);
  582. }
  583. static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
  584. {
  585. const struct dsa_switch *ds = dp->ds;
  586. if (ds->vlan_filtering_is_global)
  587. return ds->vlan_filtering;
  588. else
  589. return dp->vlan_filtering;
  590. }
  591. static inline unsigned int dsa_port_lag_id_get(struct dsa_port *dp)
  592. {
  593. return dp->lag ? dp->lag->id : 0;
  594. }
  595. static inline struct net_device *dsa_port_lag_dev_get(struct dsa_port *dp)
  596. {
  597. return dp->lag ? dp->lag->dev : NULL;
  598. }
  599. static inline bool dsa_port_offloads_lag(struct dsa_port *dp,
  600. const struct dsa_lag *lag)
  601. {
  602. return dsa_port_lag_dev_get(dp) == lag->dev;
  603. }
  604. static inline struct net_device *dsa_port_to_conduit(const struct dsa_port *dp)
  605. {
  606. if (dp->cpu_port_in_lag)
  607. return dsa_port_lag_dev_get(dp->cpu_dp);
  608. return dp->cpu_dp->conduit;
  609. }
  610. static inline
  611. struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
  612. {
  613. if (!dp->bridge)
  614. return NULL;
  615. if (dp->lag)
  616. return dp->lag->dev;
  617. else if (dp->hsr_dev)
  618. return dp->hsr_dev;
  619. return dp->user;
  620. }
  621. static inline struct net_device *
  622. dsa_port_bridge_dev_get(const struct dsa_port *dp)
  623. {
  624. return dp->bridge ? dp->bridge->dev : NULL;
  625. }
  626. static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
  627. {
  628. return dp->bridge ? dp->bridge->num : 0;
  629. }
  630. static inline bool dsa_port_bridge_same(const struct dsa_port *a,
  631. const struct dsa_port *b)
  632. {
  633. struct net_device *br_a = dsa_port_bridge_dev_get(a);
  634. struct net_device *br_b = dsa_port_bridge_dev_get(b);
  635. /* Standalone ports are not in the same bridge with one another */
  636. return (!br_a || !br_b) ? false : (br_a == br_b);
  637. }
  638. static inline bool dsa_port_offloads_bridge_port(struct dsa_port *dp,
  639. const struct net_device *dev)
  640. {
  641. return dsa_port_to_bridge_port(dp) == dev;
  642. }
  643. static inline bool
  644. dsa_port_offloads_bridge_dev(struct dsa_port *dp,
  645. const struct net_device *bridge_dev)
  646. {
  647. /* DSA ports connected to a bridge, and event was emitted
  648. * for the bridge.
  649. */
  650. return dsa_port_bridge_dev_get(dp) == bridge_dev;
  651. }
  652. static inline bool dsa_port_offloads_bridge(struct dsa_port *dp,
  653. const struct dsa_bridge *bridge)
  654. {
  655. return dsa_port_bridge_dev_get(dp) == bridge->dev;
  656. }
  657. /* Returns true if any port of this tree offloads the given net_device */
  658. static inline bool dsa_tree_offloads_bridge_port(struct dsa_switch_tree *dst,
  659. const struct net_device *dev)
  660. {
  661. struct dsa_port *dp;
  662. list_for_each_entry(dp, &dst->ports, list)
  663. if (dsa_port_offloads_bridge_port(dp, dev))
  664. return true;
  665. return false;
  666. }
  667. /* Returns true if any port of this tree offloads the given bridge */
  668. static inline bool
  669. dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
  670. const struct net_device *bridge_dev)
  671. {
  672. struct dsa_port *dp;
  673. list_for_each_entry(dp, &dst->ports, list)
  674. if (dsa_port_offloads_bridge_dev(dp, bridge_dev))
  675. return true;
  676. return false;
  677. }
  678. static inline bool dsa_port_tree_same(const struct dsa_port *a,
  679. const struct dsa_port *b)
  680. {
  681. return a->ds->dst == b->ds->dst;
  682. }
  683. typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
  684. bool is_static, void *data);
  685. struct dsa_switch_ops {
  686. /*
  687. * Tagging protocol helpers called for the CPU ports and DSA links.
  688. * @get_tag_protocol retrieves the initial tagging protocol and is
  689. * mandatory. Switches which can operate using multiple tagging
  690. * protocols should implement @change_tag_protocol and report in
  691. * @get_tag_protocol the tagger in current use.
  692. */
  693. enum dsa_tag_protocol (*get_tag_protocol)(struct dsa_switch *ds,
  694. int port,
  695. enum dsa_tag_protocol mprot);
  696. int (*change_tag_protocol)(struct dsa_switch *ds,
  697. enum dsa_tag_protocol proto);
  698. /*
  699. * Method for switch drivers to connect to the tagging protocol driver
  700. * in current use. The switch driver can provide handlers for certain
  701. * types of packets for switch management.
  702. */
  703. int (*connect_tag_protocol)(struct dsa_switch *ds,
  704. enum dsa_tag_protocol proto);
  705. int (*port_change_conduit)(struct dsa_switch *ds, int port,
  706. struct net_device *conduit,
  707. struct netlink_ext_ack *extack);
  708. /* Optional switch-wide initialization and destruction methods */
  709. int (*setup)(struct dsa_switch *ds);
  710. void (*teardown)(struct dsa_switch *ds);
  711. /* Per-port initialization and destruction methods. Mandatory if the
  712. * driver registers devlink port regions, optional otherwise.
  713. */
  714. int (*port_setup)(struct dsa_switch *ds, int port);
  715. void (*port_teardown)(struct dsa_switch *ds, int port);
  716. u32 (*get_phy_flags)(struct dsa_switch *ds, int port);
  717. /*
  718. * Access to the switch's PHY registers.
  719. */
  720. int (*phy_read)(struct dsa_switch *ds, int port, int regnum);
  721. int (*phy_write)(struct dsa_switch *ds, int port,
  722. int regnum, u16 val);
  723. /*
  724. * PHYLINK integration
  725. */
  726. void (*phylink_get_caps)(struct dsa_switch *ds, int port,
  727. struct phylink_config *config);
  728. void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
  729. struct phylink_link_state *state);
  730. /*
  731. * Port statistics counters.
  732. */
  733. void (*get_strings)(struct dsa_switch *ds, int port,
  734. u32 stringset, uint8_t *data);
  735. void (*get_ethtool_stats)(struct dsa_switch *ds,
  736. int port, uint64_t *data);
  737. int (*get_sset_count)(struct dsa_switch *ds, int port, int sset);
  738. void (*get_ethtool_phy_stats)(struct dsa_switch *ds,
  739. int port, uint64_t *data);
  740. void (*get_eth_phy_stats)(struct dsa_switch *ds, int port,
  741. struct ethtool_eth_phy_stats *phy_stats);
  742. void (*get_eth_mac_stats)(struct dsa_switch *ds, int port,
  743. struct ethtool_eth_mac_stats *mac_stats);
  744. void (*get_eth_ctrl_stats)(struct dsa_switch *ds, int port,
  745. struct ethtool_eth_ctrl_stats *ctrl_stats);
  746. void (*get_rmon_stats)(struct dsa_switch *ds, int port,
  747. struct ethtool_rmon_stats *rmon_stats,
  748. const struct ethtool_rmon_hist_range **ranges);
  749. void (*get_ts_stats)(struct dsa_switch *ds, int port,
  750. struct ethtool_ts_stats *ts_stats);
  751. void (*get_stats64)(struct dsa_switch *ds, int port,
  752. struct rtnl_link_stats64 *s);
  753. void (*get_pause_stats)(struct dsa_switch *ds, int port,
  754. struct ethtool_pause_stats *pause_stats);
  755. void (*self_test)(struct dsa_switch *ds, int port,
  756. struct ethtool_test *etest, u64 *data);
  757. /*
  758. * ethtool Wake-on-LAN
  759. */
  760. void (*get_wol)(struct dsa_switch *ds, int port,
  761. struct ethtool_wolinfo *w);
  762. int (*set_wol)(struct dsa_switch *ds, int port,
  763. struct ethtool_wolinfo *w);
  764. /*
  765. * ethtool timestamp info
  766. */
  767. int (*get_ts_info)(struct dsa_switch *ds, int port,
  768. struct kernel_ethtool_ts_info *ts);
  769. /*
  770. * ethtool MAC merge layer
  771. */
  772. int (*get_mm)(struct dsa_switch *ds, int port,
  773. struct ethtool_mm_state *state);
  774. int (*set_mm)(struct dsa_switch *ds, int port,
  775. struct ethtool_mm_cfg *cfg,
  776. struct netlink_ext_ack *extack);
  777. void (*get_mm_stats)(struct dsa_switch *ds, int port,
  778. struct ethtool_mm_stats *stats);
  779. /*
  780. * DCB ops
  781. */
  782. int (*port_get_default_prio)(struct dsa_switch *ds, int port);
  783. int (*port_set_default_prio)(struct dsa_switch *ds, int port,
  784. u8 prio);
  785. int (*port_get_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp);
  786. int (*port_add_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
  787. u8 prio);
  788. int (*port_del_dscp_prio)(struct dsa_switch *ds, int port, u8 dscp,
  789. u8 prio);
  790. int (*port_set_apptrust)(struct dsa_switch *ds, int port,
  791. const u8 *sel, int nsel);
  792. int (*port_get_apptrust)(struct dsa_switch *ds, int port, u8 *sel,
  793. int *nsel);
  794. /*
  795. * Suspend and resume
  796. */
  797. int (*suspend)(struct dsa_switch *ds);
  798. int (*resume)(struct dsa_switch *ds);
  799. /*
  800. * Port enable/disable
  801. */
  802. int (*port_enable)(struct dsa_switch *ds, int port,
  803. struct phy_device *phy);
  804. void (*port_disable)(struct dsa_switch *ds, int port);
  805. /*
  806. * Notification for MAC address changes on user ports. Drivers can
  807. * currently only veto operations. They should not use the method to
  808. * program the hardware, since the operation is not rolled back in case
  809. * of other errors.
  810. */
  811. int (*port_set_mac_address)(struct dsa_switch *ds, int port,
  812. const unsigned char *addr);
  813. /*
  814. * Compatibility between device trees defining multiple CPU ports and
  815. * drivers which are not OK to use by default the numerically smallest
  816. * CPU port of a switch for its local ports. This can return NULL,
  817. * meaning "don't know/don't care".
  818. */
  819. struct dsa_port *(*preferred_default_local_cpu_port)(struct dsa_switch *ds);
  820. /*
  821. * Port's MAC EEE settings
  822. */
  823. bool (*support_eee)(struct dsa_switch *ds, int port);
  824. int (*set_mac_eee)(struct dsa_switch *ds, int port,
  825. struct ethtool_keee *e);
  826. /* EEPROM access */
  827. int (*get_eeprom_len)(struct dsa_switch *ds);
  828. int (*get_eeprom)(struct dsa_switch *ds,
  829. struct ethtool_eeprom *eeprom, u8 *data);
  830. int (*set_eeprom)(struct dsa_switch *ds,
  831. struct ethtool_eeprom *eeprom, u8 *data);
  832. /*
  833. * Register access.
  834. */
  835. int (*get_regs_len)(struct dsa_switch *ds, int port);
  836. void (*get_regs)(struct dsa_switch *ds, int port,
  837. struct ethtool_regs *regs, void *p);
  838. /*
  839. * Upper device tracking.
  840. */
  841. int (*port_prechangeupper)(struct dsa_switch *ds, int port,
  842. struct netdev_notifier_changeupper_info *info);
  843. /*
  844. * Bridge integration
  845. */
  846. int (*set_ageing_time)(struct dsa_switch *ds, unsigned int msecs);
  847. int (*port_bridge_join)(struct dsa_switch *ds, int port,
  848. struct dsa_bridge bridge,
  849. bool *tx_fwd_offload,
  850. struct netlink_ext_ack *extack);
  851. void (*port_bridge_leave)(struct dsa_switch *ds, int port,
  852. struct dsa_bridge bridge);
  853. void (*port_stp_state_set)(struct dsa_switch *ds, int port,
  854. u8 state);
  855. int (*port_mst_state_set)(struct dsa_switch *ds, int port,
  856. const struct switchdev_mst_state *state);
  857. void (*port_fast_age)(struct dsa_switch *ds, int port);
  858. int (*port_vlan_fast_age)(struct dsa_switch *ds, int port, u16 vid);
  859. int (*port_pre_bridge_flags)(struct dsa_switch *ds, int port,
  860. struct switchdev_brport_flags flags,
  861. struct netlink_ext_ack *extack);
  862. int (*port_bridge_flags)(struct dsa_switch *ds, int port,
  863. struct switchdev_brport_flags flags,
  864. struct netlink_ext_ack *extack);
  865. void (*port_set_host_flood)(struct dsa_switch *ds, int port,
  866. bool uc, bool mc);
  867. /*
  868. * VLAN support
  869. */
  870. int (*port_vlan_filtering)(struct dsa_switch *ds, int port,
  871. bool vlan_filtering,
  872. struct netlink_ext_ack *extack);
  873. int (*port_vlan_add)(struct dsa_switch *ds, int port,
  874. const struct switchdev_obj_port_vlan *vlan,
  875. struct netlink_ext_ack *extack);
  876. int (*port_vlan_del)(struct dsa_switch *ds, int port,
  877. const struct switchdev_obj_port_vlan *vlan);
  878. int (*vlan_msti_set)(struct dsa_switch *ds, struct dsa_bridge bridge,
  879. const struct switchdev_vlan_msti *msti);
  880. /*
  881. * Forwarding database
  882. */
  883. int (*port_fdb_add)(struct dsa_switch *ds, int port,
  884. const unsigned char *addr, u16 vid,
  885. struct dsa_db db);
  886. int (*port_fdb_del)(struct dsa_switch *ds, int port,
  887. const unsigned char *addr, u16 vid,
  888. struct dsa_db db);
  889. int (*port_fdb_dump)(struct dsa_switch *ds, int port,
  890. dsa_fdb_dump_cb_t *cb, void *data);
  891. int (*lag_fdb_add)(struct dsa_switch *ds, struct dsa_lag lag,
  892. const unsigned char *addr, u16 vid,
  893. struct dsa_db db);
  894. int (*lag_fdb_del)(struct dsa_switch *ds, struct dsa_lag lag,
  895. const unsigned char *addr, u16 vid,
  896. struct dsa_db db);
  897. /*
  898. * Multicast database
  899. */
  900. int (*port_mdb_add)(struct dsa_switch *ds, int port,
  901. const struct switchdev_obj_port_mdb *mdb,
  902. struct dsa_db db);
  903. int (*port_mdb_del)(struct dsa_switch *ds, int port,
  904. const struct switchdev_obj_port_mdb *mdb,
  905. struct dsa_db db);
  906. /*
  907. * RXNFC
  908. */
  909. int (*get_rxnfc)(struct dsa_switch *ds, int port,
  910. struct ethtool_rxnfc *nfc, u32 *rule_locs);
  911. int (*set_rxnfc)(struct dsa_switch *ds, int port,
  912. struct ethtool_rxnfc *nfc);
  913. /*
  914. * TC integration
  915. */
  916. int (*cls_flower_add)(struct dsa_switch *ds, int port,
  917. struct flow_cls_offload *cls, bool ingress);
  918. int (*cls_flower_del)(struct dsa_switch *ds, int port,
  919. struct flow_cls_offload *cls, bool ingress);
  920. int (*cls_flower_stats)(struct dsa_switch *ds, int port,
  921. struct flow_cls_offload *cls, bool ingress);
  922. int (*port_mirror_add)(struct dsa_switch *ds, int port,
  923. struct dsa_mall_mirror_tc_entry *mirror,
  924. bool ingress, struct netlink_ext_ack *extack);
  925. void (*port_mirror_del)(struct dsa_switch *ds, int port,
  926. struct dsa_mall_mirror_tc_entry *mirror);
  927. int (*port_policer_add)(struct dsa_switch *ds, int port,
  928. const struct flow_action_police *policer);
  929. void (*port_policer_del)(struct dsa_switch *ds, int port);
  930. int (*port_setup_tc)(struct dsa_switch *ds, int port,
  931. enum tc_setup_type type, void *type_data);
  932. /*
  933. * Cross-chip operations
  934. */
  935. int (*crosschip_bridge_join)(struct dsa_switch *ds, int tree_index,
  936. int sw_index, int port,
  937. struct dsa_bridge bridge,
  938. struct netlink_ext_ack *extack);
  939. void (*crosschip_bridge_leave)(struct dsa_switch *ds, int tree_index,
  940. int sw_index, int port,
  941. struct dsa_bridge bridge);
  942. int (*crosschip_lag_change)(struct dsa_switch *ds, int sw_index,
  943. int port);
  944. int (*crosschip_lag_join)(struct dsa_switch *ds, int sw_index,
  945. int port, struct dsa_lag lag,
  946. struct netdev_lag_upper_info *info,
  947. struct netlink_ext_ack *extack);
  948. int (*crosschip_lag_leave)(struct dsa_switch *ds, int sw_index,
  949. int port, struct dsa_lag lag);
  950. /*
  951. * PTP functionality
  952. */
  953. int (*port_hwtstamp_get)(struct dsa_switch *ds, int port,
  954. struct kernel_hwtstamp_config *config);
  955. int (*port_hwtstamp_set)(struct dsa_switch *ds, int port,
  956. struct kernel_hwtstamp_config *config,
  957. struct netlink_ext_ack *extack);
  958. void (*port_txtstamp)(struct dsa_switch *ds, int port,
  959. struct sk_buff *skb);
  960. bool (*port_rxtstamp)(struct dsa_switch *ds, int port,
  961. struct sk_buff *skb, unsigned int type);
  962. /* Devlink parameters, etc */
  963. int (*devlink_param_get)(struct dsa_switch *ds, u32 id,
  964. struct devlink_param_gset_ctx *ctx);
  965. int (*devlink_param_set)(struct dsa_switch *ds, u32 id,
  966. struct devlink_param_gset_ctx *ctx);
  967. int (*devlink_info_get)(struct dsa_switch *ds,
  968. struct devlink_info_req *req,
  969. struct netlink_ext_ack *extack);
  970. int (*devlink_sb_pool_get)(struct dsa_switch *ds,
  971. unsigned int sb_index, u16 pool_index,
  972. struct devlink_sb_pool_info *pool_info);
  973. int (*devlink_sb_pool_set)(struct dsa_switch *ds, unsigned int sb_index,
  974. u16 pool_index, u32 size,
  975. enum devlink_sb_threshold_type threshold_type,
  976. struct netlink_ext_ack *extack);
  977. int (*devlink_sb_port_pool_get)(struct dsa_switch *ds, int port,
  978. unsigned int sb_index, u16 pool_index,
  979. u32 *p_threshold);
  980. int (*devlink_sb_port_pool_set)(struct dsa_switch *ds, int port,
  981. unsigned int sb_index, u16 pool_index,
  982. u32 threshold,
  983. struct netlink_ext_ack *extack);
  984. int (*devlink_sb_tc_pool_bind_get)(struct dsa_switch *ds, int port,
  985. unsigned int sb_index, u16 tc_index,
  986. enum devlink_sb_pool_type pool_type,
  987. u16 *p_pool_index, u32 *p_threshold);
  988. int (*devlink_sb_tc_pool_bind_set)(struct dsa_switch *ds, int port,
  989. unsigned int sb_index, u16 tc_index,
  990. enum devlink_sb_pool_type pool_type,
  991. u16 pool_index, u32 threshold,
  992. struct netlink_ext_ack *extack);
  993. int (*devlink_sb_occ_snapshot)(struct dsa_switch *ds,
  994. unsigned int sb_index);
  995. int (*devlink_sb_occ_max_clear)(struct dsa_switch *ds,
  996. unsigned int sb_index);
  997. int (*devlink_sb_occ_port_pool_get)(struct dsa_switch *ds, int port,
  998. unsigned int sb_index, u16 pool_index,
  999. u32 *p_cur, u32 *p_max);
  1000. int (*devlink_sb_occ_tc_port_bind_get)(struct dsa_switch *ds, int port,
  1001. unsigned int sb_index, u16 tc_index,
  1002. enum devlink_sb_pool_type pool_type,
  1003. u32 *p_cur, u32 *p_max);
  1004. /*
  1005. * MTU change functionality. Switches can also adjust their MRU through
  1006. * this method. By MTU, one understands the SDU (L2 payload) length.
  1007. * If the switch needs to account for the DSA tag on the CPU port, this
  1008. * method needs to do so privately.
  1009. */
  1010. int (*port_change_mtu)(struct dsa_switch *ds, int port,
  1011. int new_mtu);
  1012. int (*port_max_mtu)(struct dsa_switch *ds, int port);
  1013. /*
  1014. * LAG integration
  1015. */
  1016. int (*port_lag_change)(struct dsa_switch *ds, int port);
  1017. int (*port_lag_join)(struct dsa_switch *ds, int port,
  1018. struct dsa_lag lag,
  1019. struct netdev_lag_upper_info *info,
  1020. struct netlink_ext_ack *extack);
  1021. int (*port_lag_leave)(struct dsa_switch *ds, int port,
  1022. struct dsa_lag lag);
  1023. /*
  1024. * HSR integration
  1025. */
  1026. int (*port_hsr_join)(struct dsa_switch *ds, int port,
  1027. struct net_device *hsr,
  1028. struct netlink_ext_ack *extack);
  1029. int (*port_hsr_leave)(struct dsa_switch *ds, int port,
  1030. struct net_device *hsr);
  1031. /*
  1032. * MRP integration
  1033. */
  1034. int (*port_mrp_add)(struct dsa_switch *ds, int port,
  1035. const struct switchdev_obj_mrp *mrp);
  1036. int (*port_mrp_del)(struct dsa_switch *ds, int port,
  1037. const struct switchdev_obj_mrp *mrp);
  1038. int (*port_mrp_add_ring_role)(struct dsa_switch *ds, int port,
  1039. const struct switchdev_obj_ring_role_mrp *mrp);
  1040. int (*port_mrp_del_ring_role)(struct dsa_switch *ds, int port,
  1041. const struct switchdev_obj_ring_role_mrp *mrp);
  1042. /*
  1043. * tag_8021q operations
  1044. */
  1045. int (*tag_8021q_vlan_add)(struct dsa_switch *ds, int port, u16 vid,
  1046. u16 flags);
  1047. int (*tag_8021q_vlan_del)(struct dsa_switch *ds, int port, u16 vid);
  1048. /*
  1049. * DSA conduit tracking operations
  1050. */
  1051. void (*conduit_state_change)(struct dsa_switch *ds,
  1052. const struct net_device *conduit,
  1053. bool operational);
  1054. };
  1055. #define DSA_DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes) \
  1056. DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, \
  1057. dsa_devlink_param_get, dsa_devlink_param_set, NULL)
  1058. int dsa_devlink_param_get(struct devlink *dl, u32 id,
  1059. struct devlink_param_gset_ctx *ctx,
  1060. struct netlink_ext_ack *extack);
  1061. int dsa_devlink_param_set(struct devlink *dl, u32 id,
  1062. struct devlink_param_gset_ctx *ctx,
  1063. struct netlink_ext_ack *extack);
  1064. int dsa_devlink_params_register(struct dsa_switch *ds,
  1065. const struct devlink_param *params,
  1066. size_t params_count);
  1067. void dsa_devlink_params_unregister(struct dsa_switch *ds,
  1068. const struct devlink_param *params,
  1069. size_t params_count);
  1070. int dsa_devlink_resource_register(struct dsa_switch *ds,
  1071. const char *resource_name,
  1072. u64 resource_size,
  1073. u64 resource_id,
  1074. u64 parent_resource_id,
  1075. const struct devlink_resource_size_params *size_params);
  1076. void dsa_devlink_resources_unregister(struct dsa_switch *ds);
  1077. void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
  1078. u64 resource_id,
  1079. devlink_resource_occ_get_t *occ_get,
  1080. void *occ_get_priv);
  1081. void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
  1082. u64 resource_id);
  1083. struct devlink_region *
  1084. dsa_devlink_region_create(struct dsa_switch *ds,
  1085. const struct devlink_region_ops *ops,
  1086. u32 region_max_snapshots, u64 region_size);
  1087. struct devlink_region *
  1088. dsa_devlink_port_region_create(struct dsa_switch *ds,
  1089. int port,
  1090. const struct devlink_port_region_ops *ops,
  1091. u32 region_max_snapshots, u64 region_size);
  1092. void dsa_devlink_region_destroy(struct devlink_region *region);
  1093. struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
  1094. struct dsa_devlink_priv {
  1095. struct dsa_switch *ds;
  1096. };
  1097. static inline struct dsa_switch *dsa_devlink_to_ds(struct devlink *dl)
  1098. {
  1099. struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
  1100. return dl_priv->ds;
  1101. }
  1102. static inline
  1103. struct dsa_switch *dsa_devlink_port_to_ds(struct devlink_port *port)
  1104. {
  1105. struct devlink *dl = port->devlink;
  1106. struct dsa_devlink_priv *dl_priv = devlink_priv(dl);
  1107. return dl_priv->ds;
  1108. }
  1109. static inline int dsa_devlink_port_to_port(struct devlink_port *port)
  1110. {
  1111. return port->index;
  1112. }
  1113. bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
  1114. const unsigned char *addr, u16 vid,
  1115. struct dsa_db db);
  1116. bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
  1117. const struct switchdev_obj_port_mdb *mdb,
  1118. struct dsa_db db);
  1119. int dsa_port_simple_hsr_validate(struct dsa_switch *ds, int port,
  1120. struct net_device *hsr,
  1121. struct netlink_ext_ack *extack);
  1122. int dsa_port_simple_hsr_join(struct dsa_switch *ds, int port,
  1123. struct net_device *hsr,
  1124. struct netlink_ext_ack *extack);
  1125. int dsa_port_simple_hsr_leave(struct dsa_switch *ds, int port,
  1126. struct net_device *hsr);
  1127. /* Keep inline for faster access in hot path */
  1128. static inline bool netdev_uses_dsa(const struct net_device *dev)
  1129. {
  1130. #if IS_ENABLED(CONFIG_NET_DSA)
  1131. return dev->dsa_ptr && dev->dsa_ptr->rcv;
  1132. #endif
  1133. return false;
  1134. }
  1135. /* All DSA tags that push the EtherType to the right (basically all except tail
  1136. * tags, which don't break dissection) can be treated the same from the
  1137. * perspective of the flow dissector.
  1138. *
  1139. * We need to return:
  1140. * - offset: the (B - A) difference between:
  1141. * A. the position of the real EtherType and
  1142. * B. the current skb->data (aka ETH_HLEN bytes into the frame, aka 2 bytes
  1143. * after the normal EtherType was supposed to be)
  1144. * The offset in bytes is exactly equal to the tagger overhead (and half of
  1145. * that, in __be16 shorts).
  1146. *
  1147. * - proto: the value of the real EtherType.
  1148. */
  1149. static inline void dsa_tag_generic_flow_dissect(const struct sk_buff *skb,
  1150. __be16 *proto, int *offset)
  1151. {
  1152. #if IS_ENABLED(CONFIG_NET_DSA)
  1153. const struct dsa_device_ops *ops = skb->dev->dsa_ptr->tag_ops;
  1154. int tag_len = ops->needed_headroom;
  1155. *offset = tag_len;
  1156. *proto = ((__be16 *)skb->data)[(tag_len / 2) - 1];
  1157. #endif
  1158. }
  1159. void dsa_unregister_switch(struct dsa_switch *ds);
  1160. int dsa_register_switch(struct dsa_switch *ds);
  1161. void dsa_switch_shutdown(struct dsa_switch *ds);
  1162. struct dsa_switch *dsa_switch_find(int tree_index, int sw_index);
  1163. void dsa_flush_workqueue(void);
  1164. #ifdef CONFIG_PM_SLEEP
  1165. int dsa_switch_suspend(struct dsa_switch *ds);
  1166. int dsa_switch_resume(struct dsa_switch *ds);
  1167. #else
  1168. static inline int dsa_switch_suspend(struct dsa_switch *ds)
  1169. {
  1170. return 0;
  1171. }
  1172. static inline int dsa_switch_resume(struct dsa_switch *ds)
  1173. {
  1174. return 0;
  1175. }
  1176. #endif /* CONFIG_PM_SLEEP */
  1177. #if IS_ENABLED(CONFIG_NET_DSA)
  1178. bool dsa_user_dev_check(const struct net_device *dev);
  1179. #else
  1180. static inline bool dsa_user_dev_check(const struct net_device *dev)
  1181. {
  1182. return false;
  1183. }
  1184. #endif
  1185. netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
  1186. void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
  1187. bool dsa_supports_eee(struct dsa_switch *ds, int port);
  1188. #endif