dsa.h 42 KB

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