port.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Handling of a single switch port
  4. *
  5. * Copyright (c) 2017 Savoir-faire Linux Inc.
  6. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  7. */
  8. #include <linux/if_bridge.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/notifier.h>
  11. #include <linux/of_mdio.h>
  12. #include <linux/of_net.h>
  13. #include "dsa.h"
  14. #include "port.h"
  15. #include "switch.h"
  16. #include "tag_8021q.h"
  17. #include "user.h"
  18. /**
  19. * dsa_port_notify - Notify the switching fabric of changes to a port
  20. * @dp: port on which change occurred
  21. * @e: event, must be of type DSA_NOTIFIER_*
  22. * @v: event-specific value.
  23. *
  24. * Notify all switches in the DSA tree that this port's switch belongs to,
  25. * including this switch itself, of an event. Allows the other switches to
  26. * reconfigure themselves for cross-chip operations. Can also be used to
  27. * reconfigure ports without net_devices (CPU ports, DSA links) whenever
  28. * a user port's state changes.
  29. */
  30. static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
  31. {
  32. return dsa_tree_notify(dp->ds->dst, e, v);
  33. }
  34. static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp, u16 vid)
  35. {
  36. struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
  37. struct switchdev_notifier_fdb_info info = {
  38. .vid = vid,
  39. };
  40. /* When the port becomes standalone it has already left the bridge.
  41. * Don't notify the bridge in that case.
  42. */
  43. if (!brport_dev)
  44. return;
  45. call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
  46. brport_dev, &info.info, NULL);
  47. }
  48. static void dsa_port_fast_age(const struct dsa_port *dp)
  49. {
  50. struct dsa_switch *ds = dp->ds;
  51. if (!ds->ops->port_fast_age)
  52. return;
  53. ds->ops->port_fast_age(ds, dp->index);
  54. /* flush all VLANs */
  55. dsa_port_notify_bridge_fdb_flush(dp, 0);
  56. }
  57. static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
  58. {
  59. struct dsa_switch *ds = dp->ds;
  60. int err;
  61. if (!ds->ops->port_vlan_fast_age)
  62. return -EOPNOTSUPP;
  63. err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
  64. if (!err)
  65. dsa_port_notify_bridge_fdb_flush(dp, vid);
  66. return err;
  67. }
  68. static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
  69. {
  70. DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
  71. int err, vid;
  72. err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
  73. if (err)
  74. return err;
  75. for_each_set_bit(vid, vids, VLAN_N_VID) {
  76. err = dsa_port_vlan_fast_age(dp, vid);
  77. if (err)
  78. return err;
  79. }
  80. return 0;
  81. }
  82. static bool dsa_port_can_configure_learning(struct dsa_port *dp)
  83. {
  84. struct switchdev_brport_flags flags = {
  85. .mask = BR_LEARNING,
  86. };
  87. struct dsa_switch *ds = dp->ds;
  88. int err;
  89. if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
  90. return false;
  91. err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
  92. return !err;
  93. }
  94. bool dsa_port_supports_hwtstamp(struct dsa_port *dp)
  95. {
  96. struct dsa_switch *ds = dp->ds;
  97. struct ifreq ifr = {};
  98. int err;
  99. if (!ds->ops->port_hwtstamp_get || !ds->ops->port_hwtstamp_set)
  100. return false;
  101. /* "See through" shim implementations of the "get" method.
  102. * Since we can't cook up a complete ioctl request structure, this will
  103. * fail in copy_to_user() with -EFAULT, which hopefully is enough to
  104. * detect a valid implementation.
  105. */
  106. err = ds->ops->port_hwtstamp_get(ds, dp->index, &ifr);
  107. return err != -EOPNOTSUPP;
  108. }
  109. int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
  110. {
  111. struct dsa_switch *ds = dp->ds;
  112. int port = dp->index;
  113. if (!ds->ops->port_stp_state_set)
  114. return -EOPNOTSUPP;
  115. ds->ops->port_stp_state_set(ds, port, state);
  116. if (!dsa_port_can_configure_learning(dp) ||
  117. (do_fast_age && dp->learning)) {
  118. /* Fast age FDB entries or flush appropriate forwarding database
  119. * for the given port, if we are moving it from Learning or
  120. * Forwarding state, to Disabled or Blocking or Listening state.
  121. * Ports that were standalone before the STP state change don't
  122. * need to fast age the FDB, since address learning is off in
  123. * standalone mode.
  124. */
  125. if ((dp->stp_state == BR_STATE_LEARNING ||
  126. dp->stp_state == BR_STATE_FORWARDING) &&
  127. (state == BR_STATE_DISABLED ||
  128. state == BR_STATE_BLOCKING ||
  129. state == BR_STATE_LISTENING))
  130. dsa_port_fast_age(dp);
  131. }
  132. dp->stp_state = state;
  133. return 0;
  134. }
  135. static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
  136. bool do_fast_age)
  137. {
  138. struct dsa_switch *ds = dp->ds;
  139. int err;
  140. err = dsa_port_set_state(dp, state, do_fast_age);
  141. if (err && err != -EOPNOTSUPP) {
  142. dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
  143. dp->index, state, ERR_PTR(err));
  144. }
  145. }
  146. int dsa_port_set_mst_state(struct dsa_port *dp,
  147. const struct switchdev_mst_state *state,
  148. struct netlink_ext_ack *extack)
  149. {
  150. struct dsa_switch *ds = dp->ds;
  151. u8 prev_state;
  152. int err;
  153. if (!ds->ops->port_mst_state_set)
  154. return -EOPNOTSUPP;
  155. err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
  156. &prev_state);
  157. if (err)
  158. return err;
  159. err = ds->ops->port_mst_state_set(ds, dp->index, state);
  160. if (err)
  161. return err;
  162. if (!(dp->learning &&
  163. (prev_state == BR_STATE_LEARNING ||
  164. prev_state == BR_STATE_FORWARDING) &&
  165. (state->state == BR_STATE_DISABLED ||
  166. state->state == BR_STATE_BLOCKING ||
  167. state->state == BR_STATE_LISTENING)))
  168. return 0;
  169. err = dsa_port_msti_fast_age(dp, state->msti);
  170. if (err)
  171. NL_SET_ERR_MSG_MOD(extack,
  172. "Unable to flush associated VLANs");
  173. return 0;
  174. }
  175. int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
  176. {
  177. struct dsa_switch *ds = dp->ds;
  178. int port = dp->index;
  179. int err;
  180. if (ds->ops->port_enable) {
  181. err = ds->ops->port_enable(ds, port, phy);
  182. if (err)
  183. return err;
  184. }
  185. if (!dp->bridge)
  186. dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
  187. if (dp->pl)
  188. phylink_start(dp->pl);
  189. return 0;
  190. }
  191. int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
  192. {
  193. int err;
  194. rtnl_lock();
  195. err = dsa_port_enable_rt(dp, phy);
  196. rtnl_unlock();
  197. return err;
  198. }
  199. void dsa_port_disable_rt(struct dsa_port *dp)
  200. {
  201. struct dsa_switch *ds = dp->ds;
  202. int port = dp->index;
  203. if (dp->pl)
  204. phylink_stop(dp->pl);
  205. if (!dp->bridge)
  206. dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
  207. if (ds->ops->port_disable)
  208. ds->ops->port_disable(ds, port);
  209. }
  210. void dsa_port_disable(struct dsa_port *dp)
  211. {
  212. rtnl_lock();
  213. dsa_port_disable_rt(dp);
  214. rtnl_unlock();
  215. }
  216. static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
  217. struct dsa_bridge bridge)
  218. {
  219. struct netlink_ext_ack extack = {0};
  220. bool change_vlan_filtering = false;
  221. struct dsa_switch *ds = dp->ds;
  222. struct dsa_port *other_dp;
  223. bool vlan_filtering;
  224. int err;
  225. if (ds->needs_standalone_vlan_filtering &&
  226. !br_vlan_enabled(bridge.dev)) {
  227. change_vlan_filtering = true;
  228. vlan_filtering = true;
  229. } else if (!ds->needs_standalone_vlan_filtering &&
  230. br_vlan_enabled(bridge.dev)) {
  231. change_vlan_filtering = true;
  232. vlan_filtering = false;
  233. }
  234. /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
  235. * event for changing vlan_filtering setting upon user ports leaving
  236. * it. That is a good thing, because that lets us handle it and also
  237. * handle the case where the switch's vlan_filtering setting is global
  238. * (not per port). When that happens, the correct moment to trigger the
  239. * vlan_filtering callback is only when the last port leaves the last
  240. * VLAN-aware bridge.
  241. */
  242. if (change_vlan_filtering && ds->vlan_filtering_is_global) {
  243. dsa_switch_for_each_port(other_dp, ds) {
  244. struct net_device *br = dsa_port_bridge_dev_get(other_dp);
  245. if (br && br_vlan_enabled(br)) {
  246. change_vlan_filtering = false;
  247. break;
  248. }
  249. }
  250. }
  251. if (!change_vlan_filtering)
  252. return;
  253. err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
  254. if (extack._msg) {
  255. dev_err(ds->dev, "port %d: %s\n", dp->index,
  256. extack._msg);
  257. }
  258. if (err && err != -EOPNOTSUPP) {
  259. dev_err(ds->dev,
  260. "port %d failed to reset VLAN filtering to %d: %pe\n",
  261. dp->index, vlan_filtering, ERR_PTR(err));
  262. }
  263. }
  264. static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
  265. struct netlink_ext_ack *extack)
  266. {
  267. const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
  268. BR_BCAST_FLOOD | BR_PORT_LOCKED;
  269. struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
  270. int flag, err;
  271. for_each_set_bit(flag, &mask, 32) {
  272. struct switchdev_brport_flags flags = {0};
  273. flags.mask = BIT(flag);
  274. if (br_port_flag_is_set(brport_dev, BIT(flag)))
  275. flags.val = BIT(flag);
  276. err = dsa_port_bridge_flags(dp, flags, extack);
  277. if (err && err != -EOPNOTSUPP)
  278. return err;
  279. }
  280. return 0;
  281. }
  282. static void dsa_port_clear_brport_flags(struct dsa_port *dp)
  283. {
  284. const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
  285. const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
  286. BR_BCAST_FLOOD | BR_PORT_LOCKED;
  287. int flag, err;
  288. for_each_set_bit(flag, &mask, 32) {
  289. struct switchdev_brport_flags flags = {0};
  290. flags.mask = BIT(flag);
  291. flags.val = val & BIT(flag);
  292. err = dsa_port_bridge_flags(dp, flags, NULL);
  293. if (err && err != -EOPNOTSUPP)
  294. dev_err(dp->ds->dev,
  295. "failed to clear bridge port flag %lu: %pe\n",
  296. flags.val, ERR_PTR(err));
  297. }
  298. }
  299. static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
  300. struct netlink_ext_ack *extack)
  301. {
  302. struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
  303. struct net_device *br = dsa_port_bridge_dev_get(dp);
  304. int err;
  305. err = dsa_port_inherit_brport_flags(dp, extack);
  306. if (err)
  307. return err;
  308. err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
  309. if (err && err != -EOPNOTSUPP)
  310. return err;
  311. err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
  312. if (err && err != -EOPNOTSUPP)
  313. return err;
  314. err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
  315. if (err && err != -EOPNOTSUPP)
  316. return err;
  317. return 0;
  318. }
  319. static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
  320. struct dsa_bridge bridge)
  321. {
  322. /* Configure the port for standalone mode (no address learning,
  323. * flood everything).
  324. * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
  325. * when the user requests it through netlink or sysfs, but not
  326. * automatically at port join or leave, so we need to handle resetting
  327. * the brport flags ourselves. But we even prefer it that way, because
  328. * otherwise, some setups might never get the notification they need,
  329. * for example, when a port leaves a LAG that offloads the bridge,
  330. * it becomes standalone, but as far as the bridge is concerned, no
  331. * port ever left.
  332. */
  333. dsa_port_clear_brport_flags(dp);
  334. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  335. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  336. */
  337. dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
  338. dsa_port_reset_vlan_filtering(dp, bridge);
  339. /* Ageing time may be global to the switch chip, so don't change it
  340. * here because we have no good reason (or value) to change it to.
  341. */
  342. }
  343. static int dsa_port_bridge_create(struct dsa_port *dp,
  344. struct net_device *br,
  345. struct netlink_ext_ack *extack)
  346. {
  347. struct dsa_switch *ds = dp->ds;
  348. struct dsa_bridge *bridge;
  349. bridge = dsa_tree_bridge_find(ds->dst, br);
  350. if (bridge) {
  351. refcount_inc(&bridge->refcount);
  352. dp->bridge = bridge;
  353. return 0;
  354. }
  355. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  356. if (!bridge)
  357. return -ENOMEM;
  358. refcount_set(&bridge->refcount, 1);
  359. bridge->dev = br;
  360. bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
  361. if (ds->max_num_bridges && !bridge->num) {
  362. NL_SET_ERR_MSG_MOD(extack,
  363. "Range of offloadable bridges exceeded");
  364. kfree(bridge);
  365. return -EOPNOTSUPP;
  366. }
  367. dp->bridge = bridge;
  368. return 0;
  369. }
  370. static void dsa_port_bridge_destroy(struct dsa_port *dp,
  371. const struct net_device *br)
  372. {
  373. struct dsa_bridge *bridge = dp->bridge;
  374. dp->bridge = NULL;
  375. if (!refcount_dec_and_test(&bridge->refcount))
  376. return;
  377. if (bridge->num)
  378. dsa_bridge_num_put(br, bridge->num);
  379. kfree(bridge);
  380. }
  381. static bool dsa_port_supports_mst(struct dsa_port *dp)
  382. {
  383. struct dsa_switch *ds = dp->ds;
  384. return ds->ops->vlan_msti_set &&
  385. ds->ops->port_mst_state_set &&
  386. ds->ops->port_vlan_fast_age &&
  387. dsa_port_can_configure_learning(dp);
  388. }
  389. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
  390. struct netlink_ext_ack *extack)
  391. {
  392. struct dsa_notifier_bridge_info info = {
  393. .dp = dp,
  394. .extack = extack,
  395. };
  396. struct net_device *dev = dp->user;
  397. struct net_device *brport_dev;
  398. int err;
  399. if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
  400. return -EOPNOTSUPP;
  401. /* Here the interface is already bridged. Reflect the current
  402. * configuration so that drivers can program their chips accordingly.
  403. */
  404. err = dsa_port_bridge_create(dp, br, extack);
  405. if (err)
  406. return err;
  407. brport_dev = dsa_port_to_bridge_port(dp);
  408. info.bridge = *dp->bridge;
  409. err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
  410. if (err)
  411. goto out_rollback;
  412. /* Drivers which support bridge TX forwarding should set this */
  413. dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
  414. err = switchdev_bridge_port_offload(brport_dev, dev, dp,
  415. &dsa_user_switchdev_notifier,
  416. &dsa_user_switchdev_blocking_notifier,
  417. dp->bridge->tx_fwd_offload, extack);
  418. if (err)
  419. goto out_rollback_unbridge;
  420. err = dsa_port_switchdev_sync_attrs(dp, extack);
  421. if (err)
  422. goto out_rollback_unoffload;
  423. return 0;
  424. out_rollback_unoffload:
  425. switchdev_bridge_port_unoffload(brport_dev, dp,
  426. &dsa_user_switchdev_notifier,
  427. &dsa_user_switchdev_blocking_notifier);
  428. dsa_flush_workqueue();
  429. out_rollback_unbridge:
  430. dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  431. out_rollback:
  432. dsa_port_bridge_destroy(dp, br);
  433. return err;
  434. }
  435. void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
  436. {
  437. struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
  438. /* Don't try to unoffload something that is not offloaded */
  439. if (!brport_dev)
  440. return;
  441. switchdev_bridge_port_unoffload(brport_dev, dp,
  442. &dsa_user_switchdev_notifier,
  443. &dsa_user_switchdev_blocking_notifier);
  444. dsa_flush_workqueue();
  445. }
  446. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  447. {
  448. struct dsa_notifier_bridge_info info = {
  449. .dp = dp,
  450. };
  451. int err;
  452. /* If the port could not be offloaded to begin with, then
  453. * there is nothing to do.
  454. */
  455. if (!dp->bridge)
  456. return;
  457. info.bridge = *dp->bridge;
  458. /* Here the port is already unbridged. Reflect the current configuration
  459. * so that drivers can program their chips accordingly.
  460. */
  461. dsa_port_bridge_destroy(dp, br);
  462. err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  463. if (err)
  464. dev_err(dp->ds->dev,
  465. "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
  466. dp->index, ERR_PTR(err));
  467. dsa_port_switchdev_unsync_attrs(dp, info.bridge);
  468. }
  469. int dsa_port_lag_change(struct dsa_port *dp,
  470. struct netdev_lag_lower_state_info *linfo)
  471. {
  472. struct dsa_notifier_lag_info info = {
  473. .dp = dp,
  474. };
  475. bool tx_enabled;
  476. if (!dp->lag)
  477. return 0;
  478. /* On statically configured aggregates (e.g. loadbalance
  479. * without LACP) ports will always be tx_enabled, even if the
  480. * link is down. Thus we require both link_up and tx_enabled
  481. * in order to include it in the tx set.
  482. */
  483. tx_enabled = linfo->link_up && linfo->tx_enabled;
  484. if (tx_enabled == dp->lag_tx_enabled)
  485. return 0;
  486. dp->lag_tx_enabled = tx_enabled;
  487. return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
  488. }
  489. static int dsa_port_lag_create(struct dsa_port *dp,
  490. struct net_device *lag_dev)
  491. {
  492. struct dsa_switch *ds = dp->ds;
  493. struct dsa_lag *lag;
  494. lag = dsa_tree_lag_find(ds->dst, lag_dev);
  495. if (lag) {
  496. refcount_inc(&lag->refcount);
  497. dp->lag = lag;
  498. return 0;
  499. }
  500. lag = kzalloc(sizeof(*lag), GFP_KERNEL);
  501. if (!lag)
  502. return -ENOMEM;
  503. refcount_set(&lag->refcount, 1);
  504. mutex_init(&lag->fdb_lock);
  505. INIT_LIST_HEAD(&lag->fdbs);
  506. lag->dev = lag_dev;
  507. dsa_lag_map(ds->dst, lag);
  508. dp->lag = lag;
  509. return 0;
  510. }
  511. static void dsa_port_lag_destroy(struct dsa_port *dp)
  512. {
  513. struct dsa_lag *lag = dp->lag;
  514. dp->lag = NULL;
  515. dp->lag_tx_enabled = false;
  516. if (!refcount_dec_and_test(&lag->refcount))
  517. return;
  518. WARN_ON(!list_empty(&lag->fdbs));
  519. dsa_lag_unmap(dp->ds->dst, lag);
  520. kfree(lag);
  521. }
  522. int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
  523. struct netdev_lag_upper_info *uinfo,
  524. struct netlink_ext_ack *extack)
  525. {
  526. struct dsa_notifier_lag_info info = {
  527. .dp = dp,
  528. .info = uinfo,
  529. .extack = extack,
  530. };
  531. struct net_device *bridge_dev;
  532. int err;
  533. err = dsa_port_lag_create(dp, lag_dev);
  534. if (err)
  535. goto err_lag_create;
  536. info.lag = *dp->lag;
  537. err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
  538. if (err)
  539. goto err_lag_join;
  540. bridge_dev = netdev_master_upper_dev_get(lag_dev);
  541. if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
  542. return 0;
  543. err = dsa_port_bridge_join(dp, bridge_dev, extack);
  544. if (err)
  545. goto err_bridge_join;
  546. return 0;
  547. err_bridge_join:
  548. dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
  549. err_lag_join:
  550. dsa_port_lag_destroy(dp);
  551. err_lag_create:
  552. return err;
  553. }
  554. void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
  555. {
  556. struct net_device *br = dsa_port_bridge_dev_get(dp);
  557. if (br)
  558. dsa_port_pre_bridge_leave(dp, br);
  559. }
  560. void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
  561. {
  562. struct net_device *br = dsa_port_bridge_dev_get(dp);
  563. struct dsa_notifier_lag_info info = {
  564. .dp = dp,
  565. };
  566. int err;
  567. if (!dp->lag)
  568. return;
  569. /* Port might have been part of a LAG that in turn was
  570. * attached to a bridge.
  571. */
  572. if (br)
  573. dsa_port_bridge_leave(dp, br);
  574. info.lag = *dp->lag;
  575. dsa_port_lag_destroy(dp);
  576. err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
  577. if (err)
  578. dev_err(dp->ds->dev,
  579. "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
  580. dp->index, ERR_PTR(err));
  581. }
  582. /* Must be called under rcu_read_lock() */
  583. static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
  584. bool vlan_filtering,
  585. struct netlink_ext_ack *extack)
  586. {
  587. struct dsa_switch *ds = dp->ds;
  588. struct dsa_port *other_dp;
  589. int err;
  590. /* VLAN awareness was off, so the question is "can we turn it on".
  591. * We may have had 8021q uppers, those need to go. Make sure we don't
  592. * enter an inconsistent state: deny changing the VLAN awareness state
  593. * as long as we have 8021q uppers.
  594. */
  595. if (vlan_filtering && dsa_port_is_user(dp)) {
  596. struct net_device *br = dsa_port_bridge_dev_get(dp);
  597. struct net_device *upper_dev, *user = dp->user;
  598. struct list_head *iter;
  599. netdev_for_each_upper_dev_rcu(user, upper_dev, iter) {
  600. struct bridge_vlan_info br_info;
  601. u16 vid;
  602. if (!is_vlan_dev(upper_dev))
  603. continue;
  604. vid = vlan_dev_vlan_id(upper_dev);
  605. /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
  606. * device, respectively the VID is not found, returning
  607. * 0 means success, which is a failure for us here.
  608. */
  609. err = br_vlan_get_info(br, vid, &br_info);
  610. if (err == 0) {
  611. NL_SET_ERR_MSG_MOD(extack,
  612. "Must first remove VLAN uppers having VIDs also present in bridge");
  613. return false;
  614. }
  615. }
  616. }
  617. if (!ds->vlan_filtering_is_global)
  618. return true;
  619. /* For cases where enabling/disabling VLAN awareness is global to the
  620. * switch, we need to handle the case where multiple bridges span
  621. * different ports of the same switch device and one of them has a
  622. * different setting than what is being requested.
  623. */
  624. dsa_switch_for_each_port(other_dp, ds) {
  625. struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
  626. /* If it's the same bridge, it also has same
  627. * vlan_filtering setting => no need to check
  628. */
  629. if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
  630. continue;
  631. if (br_vlan_enabled(other_br) != vlan_filtering) {
  632. NL_SET_ERR_MSG_MOD(extack,
  633. "VLAN filtering is a global setting");
  634. return false;
  635. }
  636. }
  637. return true;
  638. }
  639. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  640. struct netlink_ext_ack *extack)
  641. {
  642. bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
  643. struct dsa_switch *ds = dp->ds;
  644. bool apply;
  645. int err;
  646. if (!ds->ops->port_vlan_filtering)
  647. return -EOPNOTSUPP;
  648. /* We are called from dsa_user_switchdev_blocking_event(),
  649. * which is not under rcu_read_lock(), unlike
  650. * dsa_user_switchdev_event().
  651. */
  652. rcu_read_lock();
  653. apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
  654. rcu_read_unlock();
  655. if (!apply)
  656. return -EINVAL;
  657. if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
  658. return 0;
  659. err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
  660. extack);
  661. if (err)
  662. return err;
  663. if (ds->vlan_filtering_is_global) {
  664. struct dsa_port *other_dp;
  665. ds->vlan_filtering = vlan_filtering;
  666. dsa_switch_for_each_user_port(other_dp, ds) {
  667. struct net_device *user = other_dp->user;
  668. /* We might be called in the unbind path, so not
  669. * all user devices might still be registered.
  670. */
  671. if (!user)
  672. continue;
  673. err = dsa_user_manage_vlan_filtering(user,
  674. vlan_filtering);
  675. if (err)
  676. goto restore;
  677. }
  678. } else {
  679. dp->vlan_filtering = vlan_filtering;
  680. err = dsa_user_manage_vlan_filtering(dp->user,
  681. vlan_filtering);
  682. if (err)
  683. goto restore;
  684. }
  685. return 0;
  686. restore:
  687. ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
  688. if (ds->vlan_filtering_is_global)
  689. ds->vlan_filtering = old_vlan_filtering;
  690. else
  691. dp->vlan_filtering = old_vlan_filtering;
  692. return err;
  693. }
  694. /* This enforces legacy behavior for switch drivers which assume they can't
  695. * receive VLAN configuration when joining a bridge with vlan_filtering=0
  696. */
  697. bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
  698. {
  699. struct net_device *br = dsa_port_bridge_dev_get(dp);
  700. struct dsa_switch *ds = dp->ds;
  701. if (!br)
  702. return false;
  703. return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
  704. }
  705. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
  706. {
  707. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  708. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  709. struct dsa_notifier_ageing_time_info info;
  710. int err;
  711. info.ageing_time = ageing_time;
  712. err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  713. if (err)
  714. return err;
  715. dp->ageing_time = ageing_time;
  716. return 0;
  717. }
  718. int dsa_port_mst_enable(struct dsa_port *dp, bool on,
  719. struct netlink_ext_ack *extack)
  720. {
  721. if (on && !dsa_port_supports_mst(dp)) {
  722. NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
  728. struct switchdev_brport_flags flags,
  729. struct netlink_ext_ack *extack)
  730. {
  731. struct dsa_switch *ds = dp->ds;
  732. if (!ds->ops->port_pre_bridge_flags)
  733. return -EINVAL;
  734. return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
  735. }
  736. int dsa_port_bridge_flags(struct dsa_port *dp,
  737. struct switchdev_brport_flags flags,
  738. struct netlink_ext_ack *extack)
  739. {
  740. struct dsa_switch *ds = dp->ds;
  741. int err;
  742. if (!ds->ops->port_bridge_flags)
  743. return -EOPNOTSUPP;
  744. err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
  745. if (err)
  746. return err;
  747. if (flags.mask & BR_LEARNING) {
  748. bool learning = flags.val & BR_LEARNING;
  749. if (learning == dp->learning)
  750. return 0;
  751. if ((dp->learning && !learning) &&
  752. (dp->stp_state == BR_STATE_LEARNING ||
  753. dp->stp_state == BR_STATE_FORWARDING))
  754. dsa_port_fast_age(dp);
  755. dp->learning = learning;
  756. }
  757. return 0;
  758. }
  759. void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
  760. {
  761. struct dsa_switch *ds = dp->ds;
  762. if (ds->ops->port_set_host_flood)
  763. ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
  764. }
  765. int dsa_port_vlan_msti(struct dsa_port *dp,
  766. const struct switchdev_vlan_msti *msti)
  767. {
  768. struct dsa_switch *ds = dp->ds;
  769. if (!ds->ops->vlan_msti_set)
  770. return -EOPNOTSUPP;
  771. return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
  772. }
  773. int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
  774. {
  775. struct dsa_notifier_mtu_info info = {
  776. .dp = dp,
  777. .mtu = new_mtu,
  778. };
  779. return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
  780. }
  781. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  782. u16 vid)
  783. {
  784. struct dsa_notifier_fdb_info info = {
  785. .dp = dp,
  786. .addr = addr,
  787. .vid = vid,
  788. .db = {
  789. .type = DSA_DB_BRIDGE,
  790. .bridge = *dp->bridge,
  791. },
  792. };
  793. /* Refcounting takes bridge.num as a key, and should be global for all
  794. * bridges in the absence of FDB isolation, and per bridge otherwise.
  795. * Force the bridge.num to zero here in the absence of FDB isolation.
  796. */
  797. if (!dp->ds->fdb_isolation)
  798. info.db.bridge.num = 0;
  799. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  800. }
  801. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  802. u16 vid)
  803. {
  804. struct dsa_notifier_fdb_info info = {
  805. .dp = dp,
  806. .addr = addr,
  807. .vid = vid,
  808. .db = {
  809. .type = DSA_DB_BRIDGE,
  810. .bridge = *dp->bridge,
  811. },
  812. };
  813. if (!dp->ds->fdb_isolation)
  814. info.db.bridge.num = 0;
  815. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  816. }
  817. static int dsa_port_host_fdb_add(struct dsa_port *dp,
  818. const unsigned char *addr, u16 vid,
  819. struct dsa_db db)
  820. {
  821. struct dsa_notifier_fdb_info info = {
  822. .dp = dp,
  823. .addr = addr,
  824. .vid = vid,
  825. .db = db,
  826. };
  827. return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
  828. }
  829. int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
  830. const unsigned char *addr, u16 vid)
  831. {
  832. struct dsa_db db = {
  833. .type = DSA_DB_PORT,
  834. .dp = dp,
  835. };
  836. return dsa_port_host_fdb_add(dp, addr, vid, db);
  837. }
  838. int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
  839. const unsigned char *addr, u16 vid)
  840. {
  841. struct net_device *conduit = dsa_port_to_conduit(dp);
  842. struct dsa_db db = {
  843. .type = DSA_DB_BRIDGE,
  844. .bridge = *dp->bridge,
  845. };
  846. int err;
  847. if (!dp->ds->fdb_isolation)
  848. db.bridge.num = 0;
  849. /* Avoid a call to __dev_set_promiscuity() on the conduit, which
  850. * requires rtnl_lock(), since we can't guarantee that is held here,
  851. * and we can't take it either.
  852. */
  853. if (conduit->priv_flags & IFF_UNICAST_FLT) {
  854. err = dev_uc_add(conduit, addr);
  855. if (err)
  856. return err;
  857. }
  858. return dsa_port_host_fdb_add(dp, addr, vid, db);
  859. }
  860. static int dsa_port_host_fdb_del(struct dsa_port *dp,
  861. const unsigned char *addr, u16 vid,
  862. struct dsa_db db)
  863. {
  864. struct dsa_notifier_fdb_info info = {
  865. .dp = dp,
  866. .addr = addr,
  867. .vid = vid,
  868. .db = db,
  869. };
  870. return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
  871. }
  872. int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
  873. const unsigned char *addr, u16 vid)
  874. {
  875. struct dsa_db db = {
  876. .type = DSA_DB_PORT,
  877. .dp = dp,
  878. };
  879. return dsa_port_host_fdb_del(dp, addr, vid, db);
  880. }
  881. int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
  882. const unsigned char *addr, u16 vid)
  883. {
  884. struct net_device *conduit = dsa_port_to_conduit(dp);
  885. struct dsa_db db = {
  886. .type = DSA_DB_BRIDGE,
  887. .bridge = *dp->bridge,
  888. };
  889. int err;
  890. if (!dp->ds->fdb_isolation)
  891. db.bridge.num = 0;
  892. if (conduit->priv_flags & IFF_UNICAST_FLT) {
  893. err = dev_uc_del(conduit, addr);
  894. if (err)
  895. return err;
  896. }
  897. return dsa_port_host_fdb_del(dp, addr, vid, db);
  898. }
  899. int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  900. u16 vid)
  901. {
  902. struct dsa_notifier_lag_fdb_info info = {
  903. .lag = dp->lag,
  904. .addr = addr,
  905. .vid = vid,
  906. .db = {
  907. .type = DSA_DB_BRIDGE,
  908. .bridge = *dp->bridge,
  909. },
  910. };
  911. if (!dp->ds->fdb_isolation)
  912. info.db.bridge.num = 0;
  913. return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
  914. }
  915. int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  916. u16 vid)
  917. {
  918. struct dsa_notifier_lag_fdb_info info = {
  919. .lag = dp->lag,
  920. .addr = addr,
  921. .vid = vid,
  922. .db = {
  923. .type = DSA_DB_BRIDGE,
  924. .bridge = *dp->bridge,
  925. },
  926. };
  927. if (!dp->ds->fdb_isolation)
  928. info.db.bridge.num = 0;
  929. return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
  930. }
  931. int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
  932. {
  933. struct dsa_switch *ds = dp->ds;
  934. int port = dp->index;
  935. if (!ds->ops->port_fdb_dump)
  936. return -EOPNOTSUPP;
  937. return ds->ops->port_fdb_dump(ds, port, cb, data);
  938. }
  939. int dsa_port_mdb_add(const struct dsa_port *dp,
  940. const struct switchdev_obj_port_mdb *mdb)
  941. {
  942. struct dsa_notifier_mdb_info info = {
  943. .dp = dp,
  944. .mdb = mdb,
  945. .db = {
  946. .type = DSA_DB_BRIDGE,
  947. .bridge = *dp->bridge,
  948. },
  949. };
  950. if (!dp->ds->fdb_isolation)
  951. info.db.bridge.num = 0;
  952. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  953. }
  954. int dsa_port_mdb_del(const struct dsa_port *dp,
  955. const struct switchdev_obj_port_mdb *mdb)
  956. {
  957. struct dsa_notifier_mdb_info info = {
  958. .dp = dp,
  959. .mdb = mdb,
  960. .db = {
  961. .type = DSA_DB_BRIDGE,
  962. .bridge = *dp->bridge,
  963. },
  964. };
  965. if (!dp->ds->fdb_isolation)
  966. info.db.bridge.num = 0;
  967. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  968. }
  969. static int dsa_port_host_mdb_add(const struct dsa_port *dp,
  970. const struct switchdev_obj_port_mdb *mdb,
  971. struct dsa_db db)
  972. {
  973. struct dsa_notifier_mdb_info info = {
  974. .dp = dp,
  975. .mdb = mdb,
  976. .db = db,
  977. };
  978. return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
  979. }
  980. int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
  981. const struct switchdev_obj_port_mdb *mdb)
  982. {
  983. struct dsa_db db = {
  984. .type = DSA_DB_PORT,
  985. .dp = dp,
  986. };
  987. return dsa_port_host_mdb_add(dp, mdb, db);
  988. }
  989. int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
  990. const struct switchdev_obj_port_mdb *mdb)
  991. {
  992. struct net_device *conduit = dsa_port_to_conduit(dp);
  993. struct dsa_db db = {
  994. .type = DSA_DB_BRIDGE,
  995. .bridge = *dp->bridge,
  996. };
  997. int err;
  998. if (!dp->ds->fdb_isolation)
  999. db.bridge.num = 0;
  1000. err = dev_mc_add(conduit, mdb->addr);
  1001. if (err)
  1002. return err;
  1003. return dsa_port_host_mdb_add(dp, mdb, db);
  1004. }
  1005. static int dsa_port_host_mdb_del(const struct dsa_port *dp,
  1006. const struct switchdev_obj_port_mdb *mdb,
  1007. struct dsa_db db)
  1008. {
  1009. struct dsa_notifier_mdb_info info = {
  1010. .dp = dp,
  1011. .mdb = mdb,
  1012. .db = db,
  1013. };
  1014. return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
  1015. }
  1016. int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
  1017. const struct switchdev_obj_port_mdb *mdb)
  1018. {
  1019. struct dsa_db db = {
  1020. .type = DSA_DB_PORT,
  1021. .dp = dp,
  1022. };
  1023. return dsa_port_host_mdb_del(dp, mdb, db);
  1024. }
  1025. int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
  1026. const struct switchdev_obj_port_mdb *mdb)
  1027. {
  1028. struct net_device *conduit = dsa_port_to_conduit(dp);
  1029. struct dsa_db db = {
  1030. .type = DSA_DB_BRIDGE,
  1031. .bridge = *dp->bridge,
  1032. };
  1033. int err;
  1034. if (!dp->ds->fdb_isolation)
  1035. db.bridge.num = 0;
  1036. err = dev_mc_del(conduit, mdb->addr);
  1037. if (err)
  1038. return err;
  1039. return dsa_port_host_mdb_del(dp, mdb, db);
  1040. }
  1041. int dsa_port_vlan_add(struct dsa_port *dp,
  1042. const struct switchdev_obj_port_vlan *vlan,
  1043. struct netlink_ext_ack *extack)
  1044. {
  1045. struct dsa_notifier_vlan_info info = {
  1046. .dp = dp,
  1047. .vlan = vlan,
  1048. .extack = extack,
  1049. };
  1050. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  1051. }
  1052. int dsa_port_vlan_del(struct dsa_port *dp,
  1053. const struct switchdev_obj_port_vlan *vlan)
  1054. {
  1055. struct dsa_notifier_vlan_info info = {
  1056. .dp = dp,
  1057. .vlan = vlan,
  1058. };
  1059. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  1060. }
  1061. int dsa_port_host_vlan_add(struct dsa_port *dp,
  1062. const struct switchdev_obj_port_vlan *vlan,
  1063. struct netlink_ext_ack *extack)
  1064. {
  1065. struct net_device *conduit = dsa_port_to_conduit(dp);
  1066. struct dsa_notifier_vlan_info info = {
  1067. .dp = dp,
  1068. .vlan = vlan,
  1069. .extack = extack,
  1070. };
  1071. int err;
  1072. err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
  1073. if (err && err != -EOPNOTSUPP)
  1074. return err;
  1075. vlan_vid_add(conduit, htons(ETH_P_8021Q), vlan->vid);
  1076. return err;
  1077. }
  1078. int dsa_port_host_vlan_del(struct dsa_port *dp,
  1079. const struct switchdev_obj_port_vlan *vlan)
  1080. {
  1081. struct net_device *conduit = dsa_port_to_conduit(dp);
  1082. struct dsa_notifier_vlan_info info = {
  1083. .dp = dp,
  1084. .vlan = vlan,
  1085. };
  1086. int err;
  1087. err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
  1088. if (err && err != -EOPNOTSUPP)
  1089. return err;
  1090. vlan_vid_del(conduit, htons(ETH_P_8021Q), vlan->vid);
  1091. return err;
  1092. }
  1093. int dsa_port_mrp_add(const struct dsa_port *dp,
  1094. const struct switchdev_obj_mrp *mrp)
  1095. {
  1096. struct dsa_switch *ds = dp->ds;
  1097. if (!ds->ops->port_mrp_add)
  1098. return -EOPNOTSUPP;
  1099. return ds->ops->port_mrp_add(ds, dp->index, mrp);
  1100. }
  1101. int dsa_port_mrp_del(const struct dsa_port *dp,
  1102. const struct switchdev_obj_mrp *mrp)
  1103. {
  1104. struct dsa_switch *ds = dp->ds;
  1105. if (!ds->ops->port_mrp_del)
  1106. return -EOPNOTSUPP;
  1107. return ds->ops->port_mrp_del(ds, dp->index, mrp);
  1108. }
  1109. int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
  1110. const struct switchdev_obj_ring_role_mrp *mrp)
  1111. {
  1112. struct dsa_switch *ds = dp->ds;
  1113. if (!ds->ops->port_mrp_add_ring_role)
  1114. return -EOPNOTSUPP;
  1115. return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
  1116. }
  1117. int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
  1118. const struct switchdev_obj_ring_role_mrp *mrp)
  1119. {
  1120. struct dsa_switch *ds = dp->ds;
  1121. if (!ds->ops->port_mrp_del_ring_role)
  1122. return -EOPNOTSUPP;
  1123. return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
  1124. }
  1125. static int dsa_port_assign_conduit(struct dsa_port *dp,
  1126. struct net_device *conduit,
  1127. struct netlink_ext_ack *extack,
  1128. bool fail_on_err)
  1129. {
  1130. struct dsa_switch *ds = dp->ds;
  1131. int port = dp->index, err;
  1132. err = ds->ops->port_change_conduit(ds, port, conduit, extack);
  1133. if (err && !fail_on_err)
  1134. dev_err(ds->dev, "port %d failed to assign conduit %s: %pe\n",
  1135. port, conduit->name, ERR_PTR(err));
  1136. if (err && fail_on_err)
  1137. return err;
  1138. dp->cpu_dp = conduit->dsa_ptr;
  1139. dp->cpu_port_in_lag = netif_is_lag_master(conduit);
  1140. return 0;
  1141. }
  1142. /* Change the dp->cpu_dp affinity for a user port. Note that both cross-chip
  1143. * notifiers and drivers have implicit assumptions about user-to-CPU-port
  1144. * mappings, so we unfortunately cannot delay the deletion of the objects
  1145. * (switchdev, standalone addresses, standalone VLANs) on the old CPU port
  1146. * until the new CPU port has been set up. So we need to completely tear down
  1147. * the old CPU port before changing it, and restore it on errors during the
  1148. * bringup of the new one.
  1149. */
  1150. int dsa_port_change_conduit(struct dsa_port *dp, struct net_device *conduit,
  1151. struct netlink_ext_ack *extack)
  1152. {
  1153. struct net_device *bridge_dev = dsa_port_bridge_dev_get(dp);
  1154. struct net_device *old_conduit = dsa_port_to_conduit(dp);
  1155. struct net_device *dev = dp->user;
  1156. struct dsa_switch *ds = dp->ds;
  1157. bool vlan_filtering;
  1158. int err, tmp;
  1159. /* Bridges may hold host FDB, MDB and VLAN objects. These need to be
  1160. * migrated, so dynamically unoffload and later reoffload the bridge
  1161. * port.
  1162. */
  1163. if (bridge_dev) {
  1164. dsa_port_pre_bridge_leave(dp, bridge_dev);
  1165. dsa_port_bridge_leave(dp, bridge_dev);
  1166. }
  1167. /* The port might still be VLAN filtering even if it's no longer
  1168. * under a bridge, either due to ds->vlan_filtering_is_global or
  1169. * ds->needs_standalone_vlan_filtering. In turn this means VLANs
  1170. * on the CPU port.
  1171. */
  1172. vlan_filtering = dsa_port_is_vlan_filtering(dp);
  1173. if (vlan_filtering) {
  1174. err = dsa_user_manage_vlan_filtering(dev, false);
  1175. if (err) {
  1176. NL_SET_ERR_MSG_MOD(extack,
  1177. "Failed to remove standalone VLANs");
  1178. goto rewind_old_bridge;
  1179. }
  1180. }
  1181. /* Standalone addresses, and addresses of upper interfaces like
  1182. * VLAN, LAG, HSR need to be migrated.
  1183. */
  1184. dsa_user_unsync_ha(dev);
  1185. /* If live-changing, we also need to uninstall the user device address
  1186. * from the port FDB and the conduit interface.
  1187. */
  1188. if (dev->flags & IFF_UP)
  1189. dsa_user_host_uc_uninstall(dev);
  1190. err = dsa_port_assign_conduit(dp, conduit, extack, true);
  1191. if (err)
  1192. goto rewind_old_addrs;
  1193. /* If the port doesn't have its own MAC address and relies on the DSA
  1194. * conduit's one, inherit it again from the new DSA conduit.
  1195. */
  1196. if (is_zero_ether_addr(dp->mac))
  1197. eth_hw_addr_inherit(dev, conduit);
  1198. /* If live-changing, we need to install the user device address to the
  1199. * port FDB and the conduit interface.
  1200. */
  1201. if (dev->flags & IFF_UP) {
  1202. err = dsa_user_host_uc_install(dev, dev->dev_addr);
  1203. if (err) {
  1204. NL_SET_ERR_MSG_MOD(extack,
  1205. "Failed to install host UC address");
  1206. goto rewind_addr_inherit;
  1207. }
  1208. }
  1209. dsa_user_sync_ha(dev);
  1210. if (vlan_filtering) {
  1211. err = dsa_user_manage_vlan_filtering(dev, true);
  1212. if (err) {
  1213. NL_SET_ERR_MSG_MOD(extack,
  1214. "Failed to restore standalone VLANs");
  1215. goto rewind_new_addrs;
  1216. }
  1217. }
  1218. if (bridge_dev) {
  1219. err = dsa_port_bridge_join(dp, bridge_dev, extack);
  1220. if (err && err == -EOPNOTSUPP) {
  1221. NL_SET_ERR_MSG_MOD(extack,
  1222. "Failed to reoffload bridge");
  1223. goto rewind_new_vlan;
  1224. }
  1225. }
  1226. return 0;
  1227. rewind_new_vlan:
  1228. if (vlan_filtering)
  1229. dsa_user_manage_vlan_filtering(dev, false);
  1230. rewind_new_addrs:
  1231. dsa_user_unsync_ha(dev);
  1232. if (dev->flags & IFF_UP)
  1233. dsa_user_host_uc_uninstall(dev);
  1234. rewind_addr_inherit:
  1235. if (is_zero_ether_addr(dp->mac))
  1236. eth_hw_addr_inherit(dev, old_conduit);
  1237. dsa_port_assign_conduit(dp, old_conduit, NULL, false);
  1238. /* Restore the objects on the old CPU port */
  1239. rewind_old_addrs:
  1240. if (dev->flags & IFF_UP) {
  1241. tmp = dsa_user_host_uc_install(dev, dev->dev_addr);
  1242. if (tmp) {
  1243. dev_err(ds->dev,
  1244. "port %d failed to restore host UC address: %pe\n",
  1245. dp->index, ERR_PTR(tmp));
  1246. }
  1247. }
  1248. dsa_user_sync_ha(dev);
  1249. if (vlan_filtering) {
  1250. tmp = dsa_user_manage_vlan_filtering(dev, true);
  1251. if (tmp) {
  1252. dev_err(ds->dev,
  1253. "port %d failed to restore standalone VLANs: %pe\n",
  1254. dp->index, ERR_PTR(tmp));
  1255. }
  1256. }
  1257. rewind_old_bridge:
  1258. if (bridge_dev) {
  1259. tmp = dsa_port_bridge_join(dp, bridge_dev, extack);
  1260. if (tmp) {
  1261. dev_err(ds->dev,
  1262. "port %d failed to rejoin bridge %s: %pe\n",
  1263. dp->index, bridge_dev->name, ERR_PTR(tmp));
  1264. }
  1265. }
  1266. return err;
  1267. }
  1268. void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
  1269. const struct dsa_device_ops *tag_ops)
  1270. {
  1271. cpu_dp->rcv = tag_ops->rcv;
  1272. cpu_dp->tag_ops = tag_ops;
  1273. }
  1274. static struct phylink_pcs *
  1275. dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
  1276. phy_interface_t interface)
  1277. {
  1278. struct dsa_port *dp = dsa_phylink_to_port(config);
  1279. struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
  1280. struct dsa_switch *ds = dp->ds;
  1281. if (ds->ops->phylink_mac_select_pcs)
  1282. pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
  1283. return pcs;
  1284. }
  1285. /* dsa_supports_eee - indicate that EEE is supported
  1286. * @ds: pointer to &struct dsa_switch
  1287. * @port: port index
  1288. *
  1289. * A default implementation for the .support_eee() DSA operations member,
  1290. * which drivers can use to indicate that they support EEE on all of their
  1291. * user ports.
  1292. *
  1293. * Returns: true
  1294. */
  1295. bool dsa_supports_eee(struct dsa_switch *ds, int port)
  1296. {
  1297. return true;
  1298. }
  1299. EXPORT_SYMBOL_GPL(dsa_supports_eee);
  1300. static void dsa_port_phylink_mac_config(struct phylink_config *config,
  1301. unsigned int mode,
  1302. const struct phylink_link_state *state)
  1303. {
  1304. struct dsa_port *dp = dsa_phylink_to_port(config);
  1305. struct dsa_switch *ds = dp->ds;
  1306. if (!ds->ops->phylink_mac_config)
  1307. return;
  1308. ds->ops->phylink_mac_config(ds, dp->index, mode, state);
  1309. }
  1310. static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
  1311. unsigned int mode,
  1312. phy_interface_t interface)
  1313. {
  1314. struct dsa_port *dp = dsa_phylink_to_port(config);
  1315. struct dsa_switch *ds = dp->ds;
  1316. if (!ds->ops->phylink_mac_link_down)
  1317. return;
  1318. ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
  1319. }
  1320. static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
  1321. struct phy_device *phydev,
  1322. unsigned int mode,
  1323. phy_interface_t interface,
  1324. int speed, int duplex,
  1325. bool tx_pause, bool rx_pause)
  1326. {
  1327. struct dsa_port *dp = dsa_phylink_to_port(config);
  1328. struct dsa_switch *ds = dp->ds;
  1329. if (!ds->ops->phylink_mac_link_up)
  1330. return;
  1331. ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
  1332. speed, duplex, tx_pause, rx_pause);
  1333. }
  1334. static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
  1335. .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
  1336. .mac_config = dsa_port_phylink_mac_config,
  1337. .mac_link_down = dsa_port_phylink_mac_link_down,
  1338. .mac_link_up = dsa_port_phylink_mac_link_up,
  1339. };
  1340. int dsa_port_phylink_create(struct dsa_port *dp)
  1341. {
  1342. const struct phylink_mac_ops *mac_ops;
  1343. struct dsa_switch *ds = dp->ds;
  1344. phy_interface_t mode;
  1345. struct phylink *pl;
  1346. int err;
  1347. err = of_get_phy_mode(dp->dn, &mode);
  1348. if (err)
  1349. mode = PHY_INTERFACE_MODE_NA;
  1350. if (ds->ops->phylink_get_caps) {
  1351. ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
  1352. } else {
  1353. /* For legacy drivers */
  1354. if (mode != PHY_INTERFACE_MODE_NA) {
  1355. __set_bit(mode, dp->pl_config.supported_interfaces);
  1356. } else {
  1357. __set_bit(PHY_INTERFACE_MODE_INTERNAL,
  1358. dp->pl_config.supported_interfaces);
  1359. __set_bit(PHY_INTERFACE_MODE_GMII,
  1360. dp->pl_config.supported_interfaces);
  1361. }
  1362. }
  1363. mac_ops = &dsa_port_phylink_mac_ops;
  1364. if (ds->phylink_mac_ops)
  1365. mac_ops = ds->phylink_mac_ops;
  1366. pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), mode,
  1367. mac_ops);
  1368. if (IS_ERR(pl)) {
  1369. pr_err("error creating PHYLINK: %ld\n", PTR_ERR(pl));
  1370. return PTR_ERR(pl);
  1371. }
  1372. dp->pl = pl;
  1373. return 0;
  1374. }
  1375. void dsa_port_phylink_destroy(struct dsa_port *dp)
  1376. {
  1377. phylink_destroy(dp->pl);
  1378. dp->pl = NULL;
  1379. }
  1380. static int dsa_shared_port_phylink_register(struct dsa_port *dp)
  1381. {
  1382. struct dsa_switch *ds = dp->ds;
  1383. struct device_node *port_dn = dp->dn;
  1384. int err;
  1385. dp->pl_config.dev = ds->dev;
  1386. dp->pl_config.type = PHYLINK_DEV;
  1387. err = dsa_port_phylink_create(dp);
  1388. if (err)
  1389. return err;
  1390. err = phylink_of_phy_connect(dp->pl, port_dn, 0);
  1391. if (err && err != -ENODEV) {
  1392. pr_err("could not attach to PHY: %d\n", err);
  1393. goto err_phy_connect;
  1394. }
  1395. return 0;
  1396. err_phy_connect:
  1397. dsa_port_phylink_destroy(dp);
  1398. return err;
  1399. }
  1400. /* During the initial DSA driver migration to OF, port nodes were sometimes
  1401. * added to device trees with no indication of how they should operate from a
  1402. * link management perspective (phy-handle, fixed-link, etc). Additionally, the
  1403. * phy-mode may be absent. The interpretation of these port OF nodes depends on
  1404. * their type.
  1405. *
  1406. * User ports with no phy-handle or fixed-link are expected to connect to an
  1407. * internal PHY located on the ds->user_mii_bus at an MDIO address equal to
  1408. * the port number. This description is still actively supported.
  1409. *
  1410. * Shared (CPU and DSA) ports with no phy-handle or fixed-link are expected to
  1411. * operate at the maximum speed that their phy-mode is capable of. If the
  1412. * phy-mode is absent, they are expected to operate using the phy-mode
  1413. * supported by the port that gives the highest link speed. It is unspecified
  1414. * if the port should use flow control or not, half duplex or full duplex, or
  1415. * if the phy-mode is a SERDES link, whether in-band autoneg is expected to be
  1416. * enabled or not.
  1417. *
  1418. * In the latter case of shared ports, omitting the link management description
  1419. * from the firmware node is deprecated and strongly discouraged. DSA uses
  1420. * phylink, which rejects the firmware nodes of these ports for lacking
  1421. * required properties.
  1422. *
  1423. * For switches in this table, DSA will skip enforcing validation and will
  1424. * later omit registering a phylink instance for the shared ports, if they lack
  1425. * a fixed-link, a phy-handle, or a managed = "in-band-status" property.
  1426. * It becomes the responsibility of the driver to ensure that these ports
  1427. * operate at the maximum speed (whatever this means) and will interoperate
  1428. * with the DSA conduit or other cascade port, since phylink methods will not be
  1429. * invoked for them.
  1430. *
  1431. * If you are considering expanding this table for newly introduced switches,
  1432. * think again. It is OK to remove switches from this table if there aren't DT
  1433. * blobs in circulation which rely on defaulting the shared ports.
  1434. */
  1435. static const char * const dsa_switches_apply_workarounds[] = {
  1436. #if IS_ENABLED(CONFIG_NET_DSA_XRS700X)
  1437. "arrow,xrs7003e",
  1438. "arrow,xrs7003f",
  1439. "arrow,xrs7004e",
  1440. "arrow,xrs7004f",
  1441. #endif
  1442. #if IS_ENABLED(CONFIG_B53)
  1443. "brcm,bcm5325",
  1444. "brcm,bcm53115",
  1445. "brcm,bcm53125",
  1446. "brcm,bcm53128",
  1447. "brcm,bcm5365",
  1448. "brcm,bcm5389",
  1449. "brcm,bcm5395",
  1450. "brcm,bcm5397",
  1451. "brcm,bcm5398",
  1452. "brcm,bcm53010-srab",
  1453. "brcm,bcm53011-srab",
  1454. "brcm,bcm53012-srab",
  1455. "brcm,bcm53018-srab",
  1456. "brcm,bcm53019-srab",
  1457. "brcm,bcm5301x-srab",
  1458. "brcm,bcm11360-srab",
  1459. "brcm,bcm58522-srab",
  1460. "brcm,bcm58525-srab",
  1461. "brcm,bcm58535-srab",
  1462. "brcm,bcm58622-srab",
  1463. "brcm,bcm58623-srab",
  1464. "brcm,bcm58625-srab",
  1465. "brcm,bcm88312-srab",
  1466. "brcm,cygnus-srab",
  1467. "brcm,nsp-srab",
  1468. "brcm,omega-srab",
  1469. "brcm,bcm3384-switch",
  1470. "brcm,bcm6328-switch",
  1471. "brcm,bcm6368-switch",
  1472. "brcm,bcm63xx-switch",
  1473. #endif
  1474. #if IS_ENABLED(CONFIG_NET_DSA_BCM_SF2)
  1475. "brcm,bcm7445-switch-v4.0",
  1476. "brcm,bcm7278-switch-v4.0",
  1477. "brcm,bcm7278-switch-v4.8",
  1478. #endif
  1479. #if IS_ENABLED(CONFIG_NET_DSA_LANTIQ_GSWIP)
  1480. "lantiq,xrx200-gswip",
  1481. "lantiq,xrx300-gswip",
  1482. "lantiq,xrx330-gswip",
  1483. #endif
  1484. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6060)
  1485. "marvell,mv88e6060",
  1486. #endif
  1487. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6XXX)
  1488. "marvell,mv88e6085",
  1489. "marvell,mv88e6190",
  1490. "marvell,mv88e6250",
  1491. #endif
  1492. #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON)
  1493. "microchip,ksz8765",
  1494. "microchip,ksz8794",
  1495. "microchip,ksz8795",
  1496. "microchip,ksz8863",
  1497. "microchip,ksz8873",
  1498. "microchip,ksz9477",
  1499. "microchip,ksz9897",
  1500. "microchip,ksz9893",
  1501. "microchip,ksz9563",
  1502. "microchip,ksz8563",
  1503. "microchip,ksz9567",
  1504. #endif
  1505. #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_MDIO)
  1506. "smsc,lan9303-mdio",
  1507. #endif
  1508. #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_I2C)
  1509. "smsc,lan9303-i2c",
  1510. #endif
  1511. NULL,
  1512. };
  1513. static void dsa_shared_port_validate_of(struct dsa_port *dp,
  1514. bool *missing_phy_mode,
  1515. bool *missing_link_description)
  1516. {
  1517. struct device_node *dn = dp->dn, *phy_np;
  1518. struct dsa_switch *ds = dp->ds;
  1519. phy_interface_t mode;
  1520. *missing_phy_mode = false;
  1521. *missing_link_description = false;
  1522. if (of_get_phy_mode(dn, &mode)) {
  1523. *missing_phy_mode = true;
  1524. dev_err(ds->dev,
  1525. "OF node %pOF of %s port %d lacks the required \"phy-mode\" property\n",
  1526. dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
  1527. }
  1528. /* Note: of_phy_is_fixed_link() also returns true for
  1529. * managed = "in-band-status"
  1530. */
  1531. if (of_phy_is_fixed_link(dn))
  1532. return;
  1533. phy_np = of_parse_phandle(dn, "phy-handle", 0);
  1534. if (phy_np) {
  1535. of_node_put(phy_np);
  1536. return;
  1537. }
  1538. *missing_link_description = true;
  1539. dev_err(ds->dev,
  1540. "OF node %pOF of %s port %d lacks the required \"phy-handle\", \"fixed-link\" or \"managed\" properties\n",
  1541. dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
  1542. }
  1543. static void dsa_shared_port_link_down(struct dsa_port *dp)
  1544. {
  1545. struct dsa_switch *ds = dp->ds;
  1546. if (ds->phylink_mac_ops && ds->phylink_mac_ops->mac_link_down)
  1547. ds->phylink_mac_ops->mac_link_down(&dp->pl_config, MLO_AN_FIXED,
  1548. PHY_INTERFACE_MODE_NA);
  1549. else if (ds->ops->phylink_mac_link_down)
  1550. ds->ops->phylink_mac_link_down(ds, dp->index, MLO_AN_FIXED,
  1551. PHY_INTERFACE_MODE_NA);
  1552. }
  1553. int dsa_shared_port_link_register_of(struct dsa_port *dp)
  1554. {
  1555. struct dsa_switch *ds = dp->ds;
  1556. bool missing_link_description;
  1557. bool missing_phy_mode;
  1558. dsa_shared_port_validate_of(dp, &missing_phy_mode,
  1559. &missing_link_description);
  1560. if ((missing_phy_mode || missing_link_description) &&
  1561. !of_device_compatible_match(ds->dev->of_node,
  1562. dsa_switches_apply_workarounds))
  1563. return -EINVAL;
  1564. if (missing_link_description) {
  1565. dev_warn(ds->dev,
  1566. "Skipping phylink registration for %s port %d\n",
  1567. dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
  1568. } else {
  1569. dsa_shared_port_link_down(dp);
  1570. return dsa_shared_port_phylink_register(dp);
  1571. }
  1572. return 0;
  1573. }
  1574. void dsa_shared_port_link_unregister_of(struct dsa_port *dp)
  1575. {
  1576. if (dp->pl) {
  1577. rtnl_lock();
  1578. phylink_disconnect_phy(dp->pl);
  1579. rtnl_unlock();
  1580. dsa_port_phylink_destroy(dp);
  1581. return;
  1582. }
  1583. }
  1584. int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr,
  1585. struct netlink_ext_ack *extack)
  1586. {
  1587. struct dsa_switch *ds = dp->ds;
  1588. int err;
  1589. if (!ds->ops->port_hsr_join)
  1590. return -EOPNOTSUPP;
  1591. dp->hsr_dev = hsr;
  1592. err = ds->ops->port_hsr_join(ds, dp->index, hsr, extack);
  1593. if (err)
  1594. dp->hsr_dev = NULL;
  1595. return err;
  1596. }
  1597. void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
  1598. {
  1599. struct dsa_switch *ds = dp->ds;
  1600. int err;
  1601. dp->hsr_dev = NULL;
  1602. if (ds->ops->port_hsr_leave) {
  1603. err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
  1604. if (err)
  1605. dev_err(dp->ds->dev,
  1606. "port %d failed to leave HSR %s: %pe\n",
  1607. dp->index, hsr->name, ERR_PTR(err));
  1608. }
  1609. }
  1610. int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
  1611. {
  1612. struct dsa_notifier_tag_8021q_vlan_info info = {
  1613. .dp = dp,
  1614. .vid = vid,
  1615. };
  1616. if (broadcast)
  1617. return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
  1618. return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
  1619. }
  1620. void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
  1621. {
  1622. struct dsa_notifier_tag_8021q_vlan_info info = {
  1623. .dp = dp,
  1624. .vid = vid,
  1625. };
  1626. int err;
  1627. if (broadcast)
  1628. err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
  1629. else
  1630. err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
  1631. if (err)
  1632. dev_err(dp->ds->dev,
  1633. "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
  1634. dp->index, vid, ERR_PTR(err));
  1635. }