dsa.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DSA topology and switch handling
  4. *
  5. * Copyright (c) 2008-2009 Marvell Semiconductor
  6. * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
  7. * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/if_hsr.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/slab.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/of.h>
  18. #include <linux/of_net.h>
  19. #include <net/dsa_stubs.h>
  20. #include <net/sch_generic.h>
  21. #include "conduit.h"
  22. #include "devlink.h"
  23. #include "dsa.h"
  24. #include "netlink.h"
  25. #include "port.h"
  26. #include "switch.h"
  27. #include "tag.h"
  28. #include "user.h"
  29. #define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
  30. static DEFINE_MUTEX(dsa2_mutex);
  31. LIST_HEAD(dsa_tree_list);
  32. static struct workqueue_struct *dsa_owq;
  33. /* Track the bridges with forwarding offload enabled */
  34. static unsigned long dsa_fwd_offloading_bridges;
  35. bool dsa_schedule_work(struct work_struct *work)
  36. {
  37. return queue_work(dsa_owq, work);
  38. }
  39. void dsa_flush_workqueue(void)
  40. {
  41. flush_workqueue(dsa_owq);
  42. }
  43. EXPORT_SYMBOL_GPL(dsa_flush_workqueue);
  44. /**
  45. * dsa_lag_map() - Map LAG structure to a linear LAG array
  46. * @dst: Tree in which to record the mapping.
  47. * @lag: LAG structure that is to be mapped to the tree's array.
  48. *
  49. * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
  50. * two spaces. The size of the mapping space is determined by the
  51. * driver by setting ds->num_lag_ids. It is perfectly legal to leave
  52. * it unset if it is not needed, in which case these functions become
  53. * no-ops.
  54. */
  55. void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
  56. {
  57. unsigned int id;
  58. for (id = 1; id <= dst->lags_len; id++) {
  59. if (!dsa_lag_by_id(dst, id)) {
  60. dst->lags[id - 1] = lag;
  61. lag->id = id;
  62. return;
  63. }
  64. }
  65. /* No IDs left, which is OK. Some drivers do not need it. The
  66. * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
  67. * returns an error for this device when joining the LAG. The
  68. * driver can then return -EOPNOTSUPP back to DSA, which will
  69. * fall back to a software LAG.
  70. */
  71. }
  72. /**
  73. * dsa_lag_unmap() - Remove a LAG ID mapping
  74. * @dst: Tree in which the mapping is recorded.
  75. * @lag: LAG structure that was mapped.
  76. *
  77. * As there may be multiple users of the mapping, it is only removed
  78. * if there are no other references to it.
  79. */
  80. void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
  81. {
  82. unsigned int id;
  83. dsa_lags_foreach_id(id, dst) {
  84. if (dsa_lag_by_id(dst, id) == lag) {
  85. dst->lags[id - 1] = NULL;
  86. lag->id = 0;
  87. break;
  88. }
  89. }
  90. }
  91. struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
  92. const struct net_device *lag_dev)
  93. {
  94. struct dsa_port *dp;
  95. list_for_each_entry(dp, &dst->ports, list)
  96. if (dsa_port_lag_dev_get(dp) == lag_dev)
  97. return dp->lag;
  98. return NULL;
  99. }
  100. struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
  101. const struct net_device *br)
  102. {
  103. struct dsa_port *dp;
  104. list_for_each_entry(dp, &dst->ports, list)
  105. if (dsa_port_bridge_dev_get(dp) == br)
  106. return dp->bridge;
  107. return NULL;
  108. }
  109. static int dsa_bridge_num_find(const struct net_device *bridge_dev)
  110. {
  111. struct dsa_switch_tree *dst;
  112. list_for_each_entry(dst, &dsa_tree_list, list) {
  113. struct dsa_bridge *bridge;
  114. bridge = dsa_tree_bridge_find(dst, bridge_dev);
  115. if (bridge)
  116. return bridge->num;
  117. }
  118. return 0;
  119. }
  120. unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
  121. {
  122. unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
  123. /* Switches without FDB isolation support don't get unique
  124. * bridge numbering
  125. */
  126. if (!max)
  127. return 0;
  128. if (!bridge_num) {
  129. /* First port that requests FDB isolation or TX forwarding
  130. * offload for this bridge
  131. */
  132. bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
  133. DSA_MAX_NUM_OFFLOADING_BRIDGES,
  134. 1);
  135. if (bridge_num > max)
  136. return 0;
  137. set_bit(bridge_num, &dsa_fwd_offloading_bridges);
  138. }
  139. return bridge_num;
  140. }
  141. void dsa_bridge_num_put(const struct net_device *bridge_dev,
  142. unsigned int bridge_num)
  143. {
  144. /* Since we refcount bridges, we know that when we call this function
  145. * it is no longer in use, so we can just go ahead and remove it from
  146. * the bit mask.
  147. */
  148. clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
  149. }
  150. struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
  151. {
  152. struct dsa_switch_tree *dst;
  153. struct dsa_port *dp;
  154. list_for_each_entry(dst, &dsa_tree_list, list) {
  155. if (dst->index != tree_index)
  156. continue;
  157. list_for_each_entry(dp, &dst->ports, list) {
  158. if (dp->ds->index != sw_index)
  159. continue;
  160. return dp->ds;
  161. }
  162. }
  163. return NULL;
  164. }
  165. EXPORT_SYMBOL_GPL(dsa_switch_find);
  166. static struct dsa_switch_tree *dsa_tree_find(int index)
  167. {
  168. struct dsa_switch_tree *dst;
  169. list_for_each_entry(dst, &dsa_tree_list, list)
  170. if (dst->index == index)
  171. return dst;
  172. return NULL;
  173. }
  174. static struct dsa_switch_tree *dsa_tree_alloc(int index)
  175. {
  176. struct dsa_switch_tree *dst;
  177. dst = kzalloc_obj(*dst);
  178. if (!dst)
  179. return NULL;
  180. dst->index = index;
  181. INIT_LIST_HEAD(&dst->rtable);
  182. INIT_LIST_HEAD(&dst->ports);
  183. INIT_LIST_HEAD(&dst->list);
  184. list_add_tail(&dst->list, &dsa_tree_list);
  185. kref_init(&dst->refcount);
  186. return dst;
  187. }
  188. static void dsa_tree_free(struct dsa_switch_tree *dst)
  189. {
  190. if (dst->tag_ops)
  191. dsa_tag_driver_put(dst->tag_ops);
  192. list_del(&dst->list);
  193. kfree(dst);
  194. }
  195. static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
  196. {
  197. if (dst)
  198. kref_get(&dst->refcount);
  199. return dst;
  200. }
  201. static struct dsa_switch_tree *dsa_tree_touch(int index)
  202. {
  203. struct dsa_switch_tree *dst;
  204. dst = dsa_tree_find(index);
  205. if (dst)
  206. return dsa_tree_get(dst);
  207. else
  208. return dsa_tree_alloc(index);
  209. }
  210. static void dsa_tree_release(struct kref *ref)
  211. {
  212. struct dsa_switch_tree *dst;
  213. dst = container_of(ref, struct dsa_switch_tree, refcount);
  214. dsa_tree_free(dst);
  215. }
  216. static void dsa_tree_put(struct dsa_switch_tree *dst)
  217. {
  218. if (dst)
  219. kref_put(&dst->refcount, dsa_tree_release);
  220. }
  221. static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
  222. struct device_node *dn)
  223. {
  224. struct dsa_port *dp;
  225. list_for_each_entry(dp, &dst->ports, list)
  226. if (dp->dn == dn)
  227. return dp;
  228. return NULL;
  229. }
  230. static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
  231. struct dsa_port *link_dp)
  232. {
  233. struct dsa_switch *ds = dp->ds;
  234. struct dsa_switch_tree *dst;
  235. struct dsa_link *dl;
  236. dst = ds->dst;
  237. list_for_each_entry(dl, &dst->rtable, list)
  238. if (dl->dp == dp && dl->link_dp == link_dp)
  239. return dl;
  240. dl = kzalloc_obj(*dl);
  241. if (!dl)
  242. return NULL;
  243. dl->dp = dp;
  244. dl->link_dp = link_dp;
  245. INIT_LIST_HEAD(&dl->list);
  246. list_add_tail(&dl->list, &dst->rtable);
  247. return dl;
  248. }
  249. static bool dsa_port_setup_routing_table(struct dsa_port *dp)
  250. {
  251. struct dsa_switch *ds = dp->ds;
  252. struct dsa_switch_tree *dst = ds->dst;
  253. struct device_node *dn = dp->dn;
  254. struct of_phandle_iterator it;
  255. struct dsa_port *link_dp;
  256. struct dsa_link *dl;
  257. int err;
  258. of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
  259. link_dp = dsa_tree_find_port_by_node(dst, it.node);
  260. if (!link_dp) {
  261. of_node_put(it.node);
  262. return false;
  263. }
  264. dl = dsa_link_touch(dp, link_dp);
  265. if (!dl) {
  266. of_node_put(it.node);
  267. return false;
  268. }
  269. }
  270. return true;
  271. }
  272. static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
  273. {
  274. bool complete = true;
  275. struct dsa_port *dp;
  276. list_for_each_entry(dp, &dst->ports, list) {
  277. if (dsa_port_is_dsa(dp)) {
  278. complete = dsa_port_setup_routing_table(dp);
  279. if (!complete)
  280. break;
  281. }
  282. }
  283. return complete;
  284. }
  285. static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
  286. {
  287. struct dsa_port *dp;
  288. list_for_each_entry(dp, &dst->ports, list)
  289. if (dsa_port_is_cpu(dp))
  290. return dp;
  291. return NULL;
  292. }
  293. struct net_device *dsa_tree_find_first_conduit(struct dsa_switch_tree *dst)
  294. {
  295. struct dsa_port *cpu_dp;
  296. cpu_dp = dsa_tree_find_first_cpu(dst);
  297. return cpu_dp->conduit;
  298. }
  299. /* Assign the default CPU port (the first one in the tree) to all ports of the
  300. * fabric which don't already have one as part of their own switch.
  301. */
  302. static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
  303. {
  304. struct dsa_port *cpu_dp, *dp;
  305. cpu_dp = dsa_tree_find_first_cpu(dst);
  306. if (!cpu_dp) {
  307. pr_err("DSA: tree %d has no CPU port\n", dst->index);
  308. return -EINVAL;
  309. }
  310. list_for_each_entry(dp, &dst->ports, list) {
  311. if (dp->cpu_dp)
  312. continue;
  313. if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
  314. dp->cpu_dp = cpu_dp;
  315. }
  316. return 0;
  317. }
  318. static struct dsa_port *
  319. dsa_switch_preferred_default_local_cpu_port(struct dsa_switch *ds)
  320. {
  321. struct dsa_port *cpu_dp;
  322. if (!ds->ops->preferred_default_local_cpu_port)
  323. return NULL;
  324. cpu_dp = ds->ops->preferred_default_local_cpu_port(ds);
  325. if (!cpu_dp)
  326. return NULL;
  327. if (WARN_ON(!dsa_port_is_cpu(cpu_dp) || cpu_dp->ds != ds))
  328. return NULL;
  329. return cpu_dp;
  330. }
  331. /* Perform initial assignment of CPU ports to user ports and DSA links in the
  332. * fabric, giving preference to CPU ports local to each switch. Default to
  333. * using the first CPU port in the switch tree if the port does not have a CPU
  334. * port local to this switch.
  335. */
  336. static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
  337. {
  338. struct dsa_port *preferred_cpu_dp, *cpu_dp, *dp;
  339. list_for_each_entry(cpu_dp, &dst->ports, list) {
  340. if (!dsa_port_is_cpu(cpu_dp))
  341. continue;
  342. preferred_cpu_dp = dsa_switch_preferred_default_local_cpu_port(cpu_dp->ds);
  343. if (preferred_cpu_dp && preferred_cpu_dp != cpu_dp)
  344. continue;
  345. /* Prefer a local CPU port */
  346. dsa_switch_for_each_port(dp, cpu_dp->ds) {
  347. /* Prefer the first local CPU port found */
  348. if (dp->cpu_dp)
  349. continue;
  350. if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
  351. dp->cpu_dp = cpu_dp;
  352. }
  353. }
  354. return dsa_tree_setup_default_cpu(dst);
  355. }
  356. static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
  357. {
  358. struct dsa_port *dp;
  359. list_for_each_entry(dp, &dst->ports, list)
  360. if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
  361. dp->cpu_dp = NULL;
  362. }
  363. static int dsa_port_setup(struct dsa_port *dp)
  364. {
  365. bool dsa_port_link_registered = false;
  366. struct dsa_switch *ds = dp->ds;
  367. bool dsa_port_enabled = false;
  368. int err = 0;
  369. if (dp->setup)
  370. return 0;
  371. err = dsa_port_devlink_setup(dp);
  372. if (err)
  373. return err;
  374. switch (dp->type) {
  375. case DSA_PORT_TYPE_UNUSED:
  376. dsa_port_disable(dp);
  377. break;
  378. case DSA_PORT_TYPE_CPU:
  379. if (dp->dn) {
  380. err = dsa_shared_port_link_register_of(dp);
  381. if (err)
  382. break;
  383. dsa_port_link_registered = true;
  384. } else {
  385. dev_warn(ds->dev,
  386. "skipping link registration for CPU port %d\n",
  387. dp->index);
  388. }
  389. err = dsa_port_enable(dp, NULL);
  390. if (err)
  391. break;
  392. dsa_port_enabled = true;
  393. break;
  394. case DSA_PORT_TYPE_DSA:
  395. if (dp->dn) {
  396. err = dsa_shared_port_link_register_of(dp);
  397. if (err)
  398. break;
  399. dsa_port_link_registered = true;
  400. } else {
  401. dev_warn(ds->dev,
  402. "skipping link registration for DSA port %d\n",
  403. dp->index);
  404. }
  405. err = dsa_port_enable(dp, NULL);
  406. if (err)
  407. break;
  408. dsa_port_enabled = true;
  409. break;
  410. case DSA_PORT_TYPE_USER:
  411. of_get_mac_address(dp->dn, dp->mac);
  412. err = dsa_user_create(dp);
  413. break;
  414. }
  415. if (err && dsa_port_enabled)
  416. dsa_port_disable(dp);
  417. if (err && dsa_port_link_registered)
  418. dsa_shared_port_link_unregister_of(dp);
  419. if (err) {
  420. dsa_port_devlink_teardown(dp);
  421. return err;
  422. }
  423. dp->setup = true;
  424. return 0;
  425. }
  426. static void dsa_port_teardown(struct dsa_port *dp)
  427. {
  428. if (!dp->setup)
  429. return;
  430. switch (dp->type) {
  431. case DSA_PORT_TYPE_UNUSED:
  432. break;
  433. case DSA_PORT_TYPE_CPU:
  434. dsa_port_disable(dp);
  435. if (dp->dn)
  436. dsa_shared_port_link_unregister_of(dp);
  437. break;
  438. case DSA_PORT_TYPE_DSA:
  439. dsa_port_disable(dp);
  440. if (dp->dn)
  441. dsa_shared_port_link_unregister_of(dp);
  442. break;
  443. case DSA_PORT_TYPE_USER:
  444. if (dp->user) {
  445. dsa_user_destroy(dp->user);
  446. dp->user = NULL;
  447. }
  448. break;
  449. }
  450. dsa_port_devlink_teardown(dp);
  451. dp->setup = false;
  452. }
  453. static int dsa_port_setup_as_unused(struct dsa_port *dp)
  454. {
  455. dp->type = DSA_PORT_TYPE_UNUSED;
  456. return dsa_port_setup(dp);
  457. }
  458. static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
  459. {
  460. const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
  461. struct dsa_switch_tree *dst = ds->dst;
  462. int err;
  463. if (tag_ops->proto == dst->default_proto)
  464. goto connect;
  465. rtnl_lock();
  466. err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
  467. rtnl_unlock();
  468. if (err) {
  469. dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
  470. tag_ops->name, ERR_PTR(err));
  471. return err;
  472. }
  473. connect:
  474. if (tag_ops->connect) {
  475. err = tag_ops->connect(ds);
  476. if (err)
  477. return err;
  478. }
  479. if (ds->ops->connect_tag_protocol) {
  480. err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
  481. if (err) {
  482. dev_err(ds->dev,
  483. "Unable to connect to tag protocol \"%s\": %pe\n",
  484. tag_ops->name, ERR_PTR(err));
  485. goto disconnect;
  486. }
  487. }
  488. return 0;
  489. disconnect:
  490. if (tag_ops->disconnect)
  491. tag_ops->disconnect(ds);
  492. return err;
  493. }
  494. static void dsa_switch_teardown_tag_protocol(struct dsa_switch *ds)
  495. {
  496. const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
  497. if (tag_ops->disconnect)
  498. tag_ops->disconnect(ds);
  499. }
  500. static int dsa_switch_setup(struct dsa_switch *ds)
  501. {
  502. int err;
  503. if (ds->setup)
  504. return 0;
  505. /* Initialize ds->phys_mii_mask before registering the user MDIO bus
  506. * driver and before ops->setup() has run, since the switch drivers and
  507. * the user MDIO bus driver rely on these values for probing PHY
  508. * devices or not
  509. */
  510. ds->phys_mii_mask |= dsa_user_ports(ds);
  511. err = dsa_switch_devlink_alloc(ds);
  512. if (err)
  513. return err;
  514. err = dsa_switch_register_notifier(ds);
  515. if (err)
  516. goto devlink_free;
  517. ds->configure_vlan_while_not_filtering = true;
  518. err = ds->ops->setup(ds);
  519. if (err < 0)
  520. goto unregister_notifier;
  521. err = dsa_switch_setup_tag_protocol(ds);
  522. if (err)
  523. goto teardown;
  524. if (!ds->user_mii_bus && ds->ops->phy_read) {
  525. ds->user_mii_bus = mdiobus_alloc();
  526. if (!ds->user_mii_bus) {
  527. err = -ENOMEM;
  528. goto teardown;
  529. }
  530. dsa_user_mii_bus_init(ds);
  531. err = mdiobus_register(ds->user_mii_bus);
  532. if (err < 0)
  533. goto free_user_mii_bus;
  534. }
  535. dsa_switch_devlink_register(ds);
  536. ds->setup = true;
  537. return 0;
  538. free_user_mii_bus:
  539. if (ds->user_mii_bus && ds->ops->phy_read)
  540. mdiobus_free(ds->user_mii_bus);
  541. teardown:
  542. if (ds->ops->teardown)
  543. ds->ops->teardown(ds);
  544. unregister_notifier:
  545. dsa_switch_unregister_notifier(ds);
  546. devlink_free:
  547. dsa_switch_devlink_free(ds);
  548. return err;
  549. }
  550. static void dsa_switch_teardown(struct dsa_switch *ds)
  551. {
  552. if (!ds->setup)
  553. return;
  554. dsa_switch_devlink_unregister(ds);
  555. if (ds->user_mii_bus && ds->ops->phy_read) {
  556. mdiobus_unregister(ds->user_mii_bus);
  557. mdiobus_free(ds->user_mii_bus);
  558. ds->user_mii_bus = NULL;
  559. }
  560. dsa_switch_teardown_tag_protocol(ds);
  561. if (ds->ops->teardown)
  562. ds->ops->teardown(ds);
  563. dsa_switch_unregister_notifier(ds);
  564. dsa_switch_devlink_free(ds);
  565. ds->setup = false;
  566. }
  567. /* First tear down the non-shared, then the shared ports. This ensures that
  568. * all work items scheduled by our switchdev handlers for user ports have
  569. * completed before we destroy the refcounting kept on the shared ports.
  570. */
  571. static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
  572. {
  573. struct dsa_port *dp;
  574. list_for_each_entry(dp, &dst->ports, list)
  575. if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
  576. dsa_port_teardown(dp);
  577. dsa_flush_workqueue();
  578. list_for_each_entry(dp, &dst->ports, list)
  579. if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
  580. dsa_port_teardown(dp);
  581. }
  582. static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
  583. {
  584. struct dsa_port *dp;
  585. list_for_each_entry(dp, &dst->ports, list)
  586. dsa_switch_teardown(dp->ds);
  587. }
  588. /* Bring shared ports up first, then non-shared ports */
  589. static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
  590. {
  591. struct dsa_port *dp;
  592. int err = 0;
  593. list_for_each_entry(dp, &dst->ports, list) {
  594. if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
  595. err = dsa_port_setup(dp);
  596. if (err)
  597. goto teardown;
  598. }
  599. }
  600. list_for_each_entry(dp, &dst->ports, list) {
  601. if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
  602. err = dsa_port_setup(dp);
  603. if (err) {
  604. err = dsa_port_setup_as_unused(dp);
  605. if (err)
  606. goto teardown;
  607. }
  608. }
  609. }
  610. return 0;
  611. teardown:
  612. dsa_tree_teardown_ports(dst);
  613. return err;
  614. }
  615. static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
  616. {
  617. struct dsa_port *dp;
  618. int err = 0;
  619. list_for_each_entry(dp, &dst->ports, list) {
  620. err = dsa_switch_setup(dp->ds);
  621. if (err) {
  622. dsa_tree_teardown_switches(dst);
  623. break;
  624. }
  625. }
  626. return err;
  627. }
  628. static int dsa_tree_setup_conduit(struct dsa_switch_tree *dst)
  629. {
  630. struct dsa_port *cpu_dp;
  631. int err = 0;
  632. rtnl_lock();
  633. dsa_tree_for_each_cpu_port(cpu_dp, dst) {
  634. struct net_device *conduit = cpu_dp->conduit;
  635. bool admin_up = (conduit->flags & IFF_UP) &&
  636. !qdisc_tx_is_noop(conduit);
  637. err = dsa_conduit_setup(conduit, cpu_dp);
  638. if (err)
  639. break;
  640. /* Replay conduit state event */
  641. dsa_tree_conduit_admin_state_change(dst, conduit, admin_up);
  642. dsa_tree_conduit_oper_state_change(dst, conduit,
  643. netif_oper_up(conduit));
  644. }
  645. rtnl_unlock();
  646. return err;
  647. }
  648. static void dsa_tree_teardown_conduit(struct dsa_switch_tree *dst)
  649. {
  650. struct dsa_port *cpu_dp;
  651. rtnl_lock();
  652. dsa_tree_for_each_cpu_port(cpu_dp, dst) {
  653. struct net_device *conduit = cpu_dp->conduit;
  654. /* Synthesizing an "admin down" state is sufficient for
  655. * the switches to get a notification if the conduit is
  656. * currently up and running.
  657. */
  658. dsa_tree_conduit_admin_state_change(dst, conduit, false);
  659. dsa_conduit_teardown(conduit);
  660. }
  661. rtnl_unlock();
  662. }
  663. static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
  664. {
  665. unsigned int len = 0;
  666. struct dsa_port *dp;
  667. list_for_each_entry(dp, &dst->ports, list) {
  668. if (dp->ds->num_lag_ids > len)
  669. len = dp->ds->num_lag_ids;
  670. }
  671. if (!len)
  672. return 0;
  673. dst->lags = kzalloc_objs(*dst->lags, len);
  674. if (!dst->lags)
  675. return -ENOMEM;
  676. dst->lags_len = len;
  677. return 0;
  678. }
  679. static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
  680. {
  681. kfree(dst->lags);
  682. }
  683. static void dsa_tree_teardown_routing_table(struct dsa_switch_tree *dst)
  684. {
  685. struct dsa_link *dl, *next;
  686. list_for_each_entry_safe(dl, next, &dst->rtable, list) {
  687. list_del(&dl->list);
  688. kfree(dl);
  689. }
  690. }
  691. static int dsa_tree_setup(struct dsa_switch_tree *dst)
  692. {
  693. bool complete;
  694. int err;
  695. if (dst->setup) {
  696. pr_err("DSA: tree %d already setup! Disjoint trees?\n",
  697. dst->index);
  698. return -EEXIST;
  699. }
  700. complete = dsa_tree_setup_routing_table(dst);
  701. if (!complete)
  702. return 0;
  703. err = dsa_tree_setup_cpu_ports(dst);
  704. if (err)
  705. goto teardown_rtable;
  706. err = dsa_tree_setup_switches(dst);
  707. if (err)
  708. goto teardown_cpu_ports;
  709. err = dsa_tree_setup_ports(dst);
  710. if (err)
  711. goto teardown_switches;
  712. err = dsa_tree_setup_conduit(dst);
  713. if (err)
  714. goto teardown_ports;
  715. err = dsa_tree_setup_lags(dst);
  716. if (err)
  717. goto teardown_conduit;
  718. dst->setup = true;
  719. pr_info("DSA: tree %d setup\n", dst->index);
  720. return 0;
  721. teardown_conduit:
  722. dsa_tree_teardown_conduit(dst);
  723. teardown_ports:
  724. dsa_tree_teardown_ports(dst);
  725. teardown_switches:
  726. dsa_tree_teardown_switches(dst);
  727. teardown_cpu_ports:
  728. dsa_tree_teardown_cpu_ports(dst);
  729. teardown_rtable:
  730. dsa_tree_teardown_routing_table(dst);
  731. return err;
  732. }
  733. static void dsa_tree_teardown(struct dsa_switch_tree *dst)
  734. {
  735. if (!dst->setup)
  736. return;
  737. dsa_tree_teardown_lags(dst);
  738. dsa_tree_teardown_conduit(dst);
  739. dsa_tree_teardown_ports(dst);
  740. dsa_tree_teardown_switches(dst);
  741. dsa_tree_teardown_cpu_ports(dst);
  742. dsa_tree_teardown_routing_table(dst);
  743. pr_info("DSA: tree %d torn down\n", dst->index);
  744. dst->setup = false;
  745. }
  746. static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
  747. const struct dsa_device_ops *tag_ops)
  748. {
  749. const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
  750. struct dsa_notifier_tag_proto_info info;
  751. int err;
  752. dst->tag_ops = tag_ops;
  753. /* Notify the switches from this tree about the connection
  754. * to the new tagger
  755. */
  756. info.tag_ops = tag_ops;
  757. err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
  758. if (err && err != -EOPNOTSUPP)
  759. goto out_disconnect;
  760. /* Notify the old tagger about the disconnection from this tree */
  761. info.tag_ops = old_tag_ops;
  762. dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
  763. return 0;
  764. out_disconnect:
  765. info.tag_ops = tag_ops;
  766. dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
  767. dst->tag_ops = old_tag_ops;
  768. return err;
  769. }
  770. /* Since the dsa/tagging sysfs device attribute is per conduit, the assumption
  771. * is that all DSA switches within a tree share the same tagger, otherwise
  772. * they would have formed disjoint trees (different "dsa,member" values).
  773. */
  774. int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
  775. const struct dsa_device_ops *tag_ops,
  776. const struct dsa_device_ops *old_tag_ops)
  777. {
  778. struct dsa_notifier_tag_proto_info info;
  779. struct dsa_port *dp;
  780. int err = -EBUSY;
  781. if (!rtnl_trylock())
  782. return restart_syscall();
  783. /* At the moment we don't allow changing the tag protocol under
  784. * traffic. The rtnl_mutex also happens to serialize concurrent
  785. * attempts to change the tagging protocol. If we ever lift the IFF_UP
  786. * restriction, there needs to be another mutex which serializes this.
  787. */
  788. dsa_tree_for_each_user_port(dp, dst) {
  789. if (dsa_port_to_conduit(dp)->flags & IFF_UP)
  790. goto out_unlock;
  791. if (dp->user->flags & IFF_UP)
  792. goto out_unlock;
  793. }
  794. /* Notify the tag protocol change */
  795. info.tag_ops = tag_ops;
  796. err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
  797. if (err)
  798. goto out_unwind_tagger;
  799. err = dsa_tree_bind_tag_proto(dst, tag_ops);
  800. if (err)
  801. goto out_unwind_tagger;
  802. rtnl_unlock();
  803. return 0;
  804. out_unwind_tagger:
  805. info.tag_ops = old_tag_ops;
  806. dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
  807. out_unlock:
  808. rtnl_unlock();
  809. return err;
  810. }
  811. static void dsa_tree_conduit_state_change(struct dsa_switch_tree *dst,
  812. struct net_device *conduit)
  813. {
  814. struct dsa_notifier_conduit_state_info info;
  815. struct dsa_port *cpu_dp = conduit->dsa_ptr;
  816. info.conduit = conduit;
  817. info.operational = dsa_port_conduit_is_operational(cpu_dp);
  818. dsa_tree_notify(dst, DSA_NOTIFIER_CONDUIT_STATE_CHANGE, &info);
  819. }
  820. void dsa_tree_conduit_admin_state_change(struct dsa_switch_tree *dst,
  821. struct net_device *conduit,
  822. bool up)
  823. {
  824. struct dsa_port *cpu_dp = conduit->dsa_ptr;
  825. bool notify = false;
  826. /* Don't keep track of admin state on LAG DSA conduits,
  827. * but rather just of physical DSA conduits
  828. */
  829. if (netif_is_lag_master(conduit))
  830. return;
  831. if ((dsa_port_conduit_is_operational(cpu_dp)) !=
  832. (up && cpu_dp->conduit_oper_up))
  833. notify = true;
  834. cpu_dp->conduit_admin_up = up;
  835. if (notify)
  836. dsa_tree_conduit_state_change(dst, conduit);
  837. }
  838. void dsa_tree_conduit_oper_state_change(struct dsa_switch_tree *dst,
  839. struct net_device *conduit,
  840. bool up)
  841. {
  842. struct dsa_port *cpu_dp = conduit->dsa_ptr;
  843. bool notify = false;
  844. /* Don't keep track of oper state on LAG DSA conduits,
  845. * but rather just of physical DSA conduits
  846. */
  847. if (netif_is_lag_master(conduit))
  848. return;
  849. if ((dsa_port_conduit_is_operational(cpu_dp)) !=
  850. (cpu_dp->conduit_admin_up && up))
  851. notify = true;
  852. cpu_dp->conduit_oper_up = up;
  853. if (notify)
  854. dsa_tree_conduit_state_change(dst, conduit);
  855. }
  856. static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
  857. {
  858. struct dsa_switch_tree *dst = ds->dst;
  859. struct dsa_port *dp;
  860. dsa_switch_for_each_port(dp, ds)
  861. if (dp->index == index)
  862. return dp;
  863. dp = kzalloc_obj(*dp);
  864. if (!dp)
  865. return NULL;
  866. dp->ds = ds;
  867. dp->index = index;
  868. mutex_init(&dp->addr_lists_lock);
  869. mutex_init(&dp->vlans_lock);
  870. INIT_LIST_HEAD(&dp->fdbs);
  871. INIT_LIST_HEAD(&dp->mdbs);
  872. INIT_LIST_HEAD(&dp->vlans); /* also initializes &dp->user_vlans */
  873. INIT_LIST_HEAD(&dp->list);
  874. list_add_tail(&dp->list, &dst->ports);
  875. return dp;
  876. }
  877. static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
  878. {
  879. dp->type = DSA_PORT_TYPE_USER;
  880. dp->name = name;
  881. return 0;
  882. }
  883. static int dsa_port_parse_dsa(struct dsa_port *dp)
  884. {
  885. dp->type = DSA_PORT_TYPE_DSA;
  886. return 0;
  887. }
  888. static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
  889. struct net_device *conduit)
  890. {
  891. enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
  892. struct dsa_switch *mds, *ds = dp->ds;
  893. unsigned int mdp_upstream;
  894. struct dsa_port *mdp;
  895. /* It is possible to stack DSA switches onto one another when that
  896. * happens the switch driver may want to know if its tagging protocol
  897. * is going to work in such a configuration.
  898. */
  899. if (dsa_user_dev_check(conduit)) {
  900. mdp = dsa_user_to_port(conduit);
  901. mds = mdp->ds;
  902. mdp_upstream = dsa_upstream_port(mds, mdp->index);
  903. tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
  904. DSA_TAG_PROTO_NONE);
  905. }
  906. /* If the conduit device is not itself a DSA user in a disjoint DSA
  907. * tree, then return immediately.
  908. */
  909. return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
  910. }
  911. static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *conduit,
  912. const char *user_protocol)
  913. {
  914. const struct dsa_device_ops *tag_ops = NULL;
  915. struct dsa_switch *ds = dp->ds;
  916. struct dsa_switch_tree *dst = ds->dst;
  917. enum dsa_tag_protocol default_proto;
  918. /* Find out which protocol the switch would prefer. */
  919. default_proto = dsa_get_tag_protocol(dp, conduit);
  920. if (dst->default_proto) {
  921. if (dst->default_proto != default_proto) {
  922. dev_err(ds->dev,
  923. "A DSA switch tree can have only one tagging protocol\n");
  924. return -EINVAL;
  925. }
  926. } else {
  927. dst->default_proto = default_proto;
  928. }
  929. /* See if the user wants to override that preference. */
  930. if (user_protocol) {
  931. if (!ds->ops->change_tag_protocol) {
  932. dev_err(ds->dev, "Tag protocol cannot be modified\n");
  933. return -EINVAL;
  934. }
  935. tag_ops = dsa_tag_driver_get_by_name(user_protocol);
  936. if (IS_ERR(tag_ops)) {
  937. dev_warn(ds->dev,
  938. "Failed to find a tagging driver for protocol %s, using default\n",
  939. user_protocol);
  940. tag_ops = NULL;
  941. }
  942. }
  943. if (!tag_ops)
  944. tag_ops = dsa_tag_driver_get_by_id(default_proto);
  945. if (IS_ERR(tag_ops)) {
  946. if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
  947. return -EPROBE_DEFER;
  948. dev_warn(ds->dev, "No tagger for this switch\n");
  949. return PTR_ERR(tag_ops);
  950. }
  951. if (dst->tag_ops) {
  952. if (dst->tag_ops != tag_ops) {
  953. dev_err(ds->dev,
  954. "A DSA switch tree can have only one tagging protocol\n");
  955. dsa_tag_driver_put(tag_ops);
  956. return -EINVAL;
  957. }
  958. /* In the case of multiple CPU ports per switch, the tagging
  959. * protocol is still reference-counted only per switch tree.
  960. */
  961. dsa_tag_driver_put(tag_ops);
  962. } else {
  963. dst->tag_ops = tag_ops;
  964. }
  965. dp->conduit = conduit;
  966. dp->type = DSA_PORT_TYPE_CPU;
  967. dsa_port_set_tag_protocol(dp, dst->tag_ops);
  968. dp->dst = dst;
  969. /* At this point, the tree may be configured to use a different
  970. * tagger than the one chosen by the switch driver during
  971. * .setup, in the case when a user selects a custom protocol
  972. * through the DT.
  973. *
  974. * This is resolved by syncing the driver with the tree in
  975. * dsa_switch_setup_tag_protocol once .setup has run and the
  976. * driver is ready to accept calls to .change_tag_protocol. If
  977. * the driver does not support the custom protocol at that
  978. * point, the tree is wholly rejected, thereby ensuring that the
  979. * tree and driver are always in agreement on the protocol to
  980. * use.
  981. */
  982. return 0;
  983. }
  984. static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
  985. {
  986. struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
  987. const char *name = of_get_property(dn, "label", NULL);
  988. bool link = of_property_read_bool(dn, "link");
  989. dp->dn = dn;
  990. if (ethernet) {
  991. struct net_device *conduit;
  992. const char *user_protocol;
  993. int err;
  994. rtnl_lock();
  995. conduit = of_find_net_device_by_node(ethernet);
  996. of_node_put(ethernet);
  997. if (!conduit) {
  998. rtnl_unlock();
  999. return -EPROBE_DEFER;
  1000. }
  1001. netdev_hold(conduit, &dp->conduit_tracker, GFP_KERNEL);
  1002. put_device(&conduit->dev);
  1003. rtnl_unlock();
  1004. user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
  1005. err = dsa_port_parse_cpu(dp, conduit, user_protocol);
  1006. if (err)
  1007. netdev_put(conduit, &dp->conduit_tracker);
  1008. return err;
  1009. }
  1010. if (link)
  1011. return dsa_port_parse_dsa(dp);
  1012. return dsa_port_parse_user(dp, name);
  1013. }
  1014. static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
  1015. struct device_node *dn)
  1016. {
  1017. struct device_node *ports, *port;
  1018. struct dsa_port *dp;
  1019. int err = 0;
  1020. u32 reg;
  1021. ports = of_get_child_by_name(dn, "ports");
  1022. if (!ports) {
  1023. /* The second possibility is "ethernet-ports" */
  1024. ports = of_get_child_by_name(dn, "ethernet-ports");
  1025. if (!ports) {
  1026. dev_err(ds->dev, "no ports child node found\n");
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. for_each_available_child_of_node(ports, port) {
  1031. err = of_property_read_u32(port, "reg", &reg);
  1032. if (err) {
  1033. of_node_put(port);
  1034. goto out_put_node;
  1035. }
  1036. if (reg >= ds->num_ports) {
  1037. dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
  1038. port, reg, ds->num_ports);
  1039. of_node_put(port);
  1040. err = -EINVAL;
  1041. goto out_put_node;
  1042. }
  1043. dp = dsa_to_port(ds, reg);
  1044. err = dsa_port_parse_of(dp, port);
  1045. if (err) {
  1046. of_node_put(port);
  1047. goto out_put_node;
  1048. }
  1049. }
  1050. out_put_node:
  1051. of_node_put(ports);
  1052. return err;
  1053. }
  1054. static int dsa_switch_parse_member_of(struct dsa_switch *ds,
  1055. struct device_node *dn)
  1056. {
  1057. u32 m[2] = { 0, 0 };
  1058. int sz;
  1059. /* Don't error out if this optional property isn't found */
  1060. sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
  1061. if (sz < 0 && sz != -EINVAL)
  1062. return sz;
  1063. ds->index = m[1];
  1064. ds->dst = dsa_tree_touch(m[0]);
  1065. if (!ds->dst)
  1066. return -ENOMEM;
  1067. if (dsa_switch_find(ds->dst->index, ds->index)) {
  1068. dev_err(ds->dev,
  1069. "A DSA switch with index %d already exists in tree %d\n",
  1070. ds->index, ds->dst->index);
  1071. return -EEXIST;
  1072. }
  1073. if (ds->dst->last_switch < ds->index)
  1074. ds->dst->last_switch = ds->index;
  1075. return 0;
  1076. }
  1077. static int dsa_switch_touch_ports(struct dsa_switch *ds)
  1078. {
  1079. struct dsa_port *dp;
  1080. int port;
  1081. for (port = 0; port < ds->num_ports; port++) {
  1082. dp = dsa_port_touch(ds, port);
  1083. if (!dp)
  1084. return -ENOMEM;
  1085. }
  1086. return 0;
  1087. }
  1088. static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
  1089. {
  1090. int err;
  1091. err = dsa_switch_parse_member_of(ds, dn);
  1092. if (err)
  1093. return err;
  1094. err = dsa_switch_touch_ports(ds);
  1095. if (err)
  1096. return err;
  1097. return dsa_switch_parse_ports_of(ds, dn);
  1098. }
  1099. static int dev_is_class(struct device *dev, const void *class)
  1100. {
  1101. if (dev->class != NULL && !strcmp(dev->class->name, class))
  1102. return 1;
  1103. return 0;
  1104. }
  1105. static struct device *dev_find_class(struct device *parent, char *class)
  1106. {
  1107. if (dev_is_class(parent, class)) {
  1108. get_device(parent);
  1109. return parent;
  1110. }
  1111. return device_find_child(parent, class, dev_is_class);
  1112. }
  1113. static int dsa_port_parse(struct dsa_port *dp, const char *name,
  1114. struct device *dev)
  1115. {
  1116. if (!strcmp(name, "cpu")) {
  1117. struct net_device *conduit;
  1118. struct device *d;
  1119. int err;
  1120. rtnl_lock();
  1121. d = dev_find_class(dev, "net");
  1122. if (!d) {
  1123. rtnl_unlock();
  1124. return -EPROBE_DEFER;
  1125. }
  1126. conduit = to_net_dev(d);
  1127. netdev_hold(conduit, &dp->conduit_tracker, GFP_KERNEL);
  1128. put_device(d);
  1129. rtnl_unlock();
  1130. err = dsa_port_parse_cpu(dp, conduit, NULL);
  1131. if (err)
  1132. netdev_put(conduit, &dp->conduit_tracker);
  1133. return err;
  1134. }
  1135. if (!strcmp(name, "dsa"))
  1136. return dsa_port_parse_dsa(dp);
  1137. return dsa_port_parse_user(dp, name);
  1138. }
  1139. static int dsa_switch_parse_ports(struct dsa_switch *ds,
  1140. struct dsa_chip_data *cd)
  1141. {
  1142. bool valid_name_found = false;
  1143. struct dsa_port *dp;
  1144. struct device *dev;
  1145. const char *name;
  1146. unsigned int i;
  1147. int err;
  1148. for (i = 0; i < DSA_MAX_PORTS; i++) {
  1149. name = cd->port_names[i];
  1150. dev = cd->netdev[i];
  1151. dp = dsa_to_port(ds, i);
  1152. if (!name)
  1153. continue;
  1154. err = dsa_port_parse(dp, name, dev);
  1155. if (err)
  1156. return err;
  1157. valid_name_found = true;
  1158. }
  1159. if (!valid_name_found && i == DSA_MAX_PORTS)
  1160. return -EINVAL;
  1161. return 0;
  1162. }
  1163. static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
  1164. {
  1165. int err;
  1166. ds->cd = cd;
  1167. /* We don't support interconnected switches nor multiple trees via
  1168. * platform data, so this is the unique switch of the tree.
  1169. */
  1170. ds->index = 0;
  1171. ds->dst = dsa_tree_touch(0);
  1172. if (!ds->dst)
  1173. return -ENOMEM;
  1174. err = dsa_switch_touch_ports(ds);
  1175. if (err)
  1176. return err;
  1177. return dsa_switch_parse_ports(ds, cd);
  1178. }
  1179. static void dsa_switch_release_ports(struct dsa_switch *ds)
  1180. {
  1181. struct dsa_mac_addr *a, *tmp;
  1182. struct dsa_port *dp, *next;
  1183. struct dsa_vlan *v, *n;
  1184. dsa_switch_for_each_port_safe(dp, next, ds) {
  1185. if (dsa_port_is_cpu(dp) && dp->conduit)
  1186. netdev_put(dp->conduit, &dp->conduit_tracker);
  1187. /* These are either entries that upper layers lost track of
  1188. * (probably due to bugs), or installed through interfaces
  1189. * where one does not necessarily have to remove them, like
  1190. * ndo_dflt_fdb_add().
  1191. */
  1192. list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
  1193. dev_info(ds->dev,
  1194. "Cleaning up unicast address %pM vid %u from port %d\n",
  1195. a->addr, a->vid, dp->index);
  1196. list_del(&a->list);
  1197. kfree(a);
  1198. }
  1199. list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
  1200. dev_info(ds->dev,
  1201. "Cleaning up multicast address %pM vid %u from port %d\n",
  1202. a->addr, a->vid, dp->index);
  1203. list_del(&a->list);
  1204. kfree(a);
  1205. }
  1206. /* These are entries that upper layers have lost track of,
  1207. * probably due to bugs, but also due to dsa_port_do_vlan_del()
  1208. * having failed and the VLAN entry still lingering on.
  1209. */
  1210. list_for_each_entry_safe(v, n, &dp->vlans, list) {
  1211. dev_info(ds->dev,
  1212. "Cleaning up vid %u from port %d\n",
  1213. v->vid, dp->index);
  1214. list_del(&v->list);
  1215. kfree(v);
  1216. }
  1217. list_del(&dp->list);
  1218. kfree(dp);
  1219. }
  1220. }
  1221. static int dsa_switch_probe(struct dsa_switch *ds)
  1222. {
  1223. struct dsa_switch_tree *dst;
  1224. struct dsa_chip_data *pdata;
  1225. struct device_node *np;
  1226. int err;
  1227. if (!ds->dev)
  1228. return -ENODEV;
  1229. pdata = ds->dev->platform_data;
  1230. np = ds->dev->of_node;
  1231. if (!ds->num_ports)
  1232. return -EINVAL;
  1233. if (np) {
  1234. err = dsa_switch_parse_of(ds, np);
  1235. if (err)
  1236. dsa_switch_release_ports(ds);
  1237. } else if (pdata) {
  1238. err = dsa_switch_parse(ds, pdata);
  1239. if (err)
  1240. dsa_switch_release_ports(ds);
  1241. } else {
  1242. err = -ENODEV;
  1243. }
  1244. if (err)
  1245. return err;
  1246. dst = ds->dst;
  1247. dsa_tree_get(dst);
  1248. err = dsa_tree_setup(dst);
  1249. if (err) {
  1250. dsa_switch_release_ports(ds);
  1251. dsa_tree_put(dst);
  1252. }
  1253. return err;
  1254. }
  1255. int dsa_register_switch(struct dsa_switch *ds)
  1256. {
  1257. int err;
  1258. mutex_lock(&dsa2_mutex);
  1259. err = dsa_switch_probe(ds);
  1260. dsa_tree_put(ds->dst);
  1261. mutex_unlock(&dsa2_mutex);
  1262. return err;
  1263. }
  1264. EXPORT_SYMBOL_GPL(dsa_register_switch);
  1265. static void dsa_switch_remove(struct dsa_switch *ds)
  1266. {
  1267. struct dsa_switch_tree *dst = ds->dst;
  1268. dsa_tree_teardown(dst);
  1269. dsa_switch_release_ports(ds);
  1270. dsa_tree_put(dst);
  1271. }
  1272. void dsa_unregister_switch(struct dsa_switch *ds)
  1273. {
  1274. mutex_lock(&dsa2_mutex);
  1275. dsa_switch_remove(ds);
  1276. mutex_unlock(&dsa2_mutex);
  1277. }
  1278. EXPORT_SYMBOL_GPL(dsa_unregister_switch);
  1279. /* If the DSA conduit chooses to unregister its net_device on .shutdown, DSA is
  1280. * blocking that operation from completion, due to the dev_hold taken inside
  1281. * netdev_upper_dev_link. Unlink the DSA user interfaces from being uppers of
  1282. * the DSA conduit, so that the system can reboot successfully.
  1283. */
  1284. void dsa_switch_shutdown(struct dsa_switch *ds)
  1285. {
  1286. struct net_device *conduit, *user_dev;
  1287. LIST_HEAD(close_list);
  1288. struct dsa_port *dp;
  1289. mutex_lock(&dsa2_mutex);
  1290. if (!ds->setup)
  1291. goto out;
  1292. rtnl_lock();
  1293. dsa_switch_for_each_cpu_port(dp, ds)
  1294. list_add(&dp->conduit->close_list, &close_list);
  1295. netif_close_many(&close_list, true);
  1296. dsa_switch_for_each_user_port(dp, ds) {
  1297. conduit = dsa_port_to_conduit(dp);
  1298. user_dev = dp->user;
  1299. netif_device_detach(user_dev);
  1300. netdev_upper_dev_unlink(conduit, user_dev);
  1301. }
  1302. /* Disconnect from further netdevice notifiers on the conduit,
  1303. * since netdev_uses_dsa() will now return false.
  1304. */
  1305. dsa_switch_for_each_cpu_port(dp, ds) {
  1306. dp->conduit->dsa_ptr = NULL;
  1307. netdev_put(dp->conduit, &dp->conduit_tracker);
  1308. }
  1309. rtnl_unlock();
  1310. out:
  1311. mutex_unlock(&dsa2_mutex);
  1312. }
  1313. EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
  1314. #ifdef CONFIG_PM_SLEEP
  1315. static bool dsa_port_is_initialized(const struct dsa_port *dp)
  1316. {
  1317. return dp->type == DSA_PORT_TYPE_USER && dp->user;
  1318. }
  1319. int dsa_switch_suspend(struct dsa_switch *ds)
  1320. {
  1321. struct dsa_port *dp;
  1322. int ret = 0;
  1323. /* Suspend user network devices */
  1324. dsa_switch_for_each_port(dp, ds) {
  1325. if (!dsa_port_is_initialized(dp))
  1326. continue;
  1327. ret = dsa_user_suspend(dp->user);
  1328. if (ret)
  1329. return ret;
  1330. }
  1331. if (ds->ops->suspend)
  1332. ret = ds->ops->suspend(ds);
  1333. return ret;
  1334. }
  1335. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  1336. int dsa_switch_resume(struct dsa_switch *ds)
  1337. {
  1338. struct dsa_port *dp;
  1339. int ret = 0;
  1340. if (ds->ops->resume)
  1341. ret = ds->ops->resume(ds);
  1342. if (ret)
  1343. return ret;
  1344. /* Resume user network devices */
  1345. dsa_switch_for_each_port(dp, ds) {
  1346. if (!dsa_port_is_initialized(dp))
  1347. continue;
  1348. ret = dsa_user_resume(dp->user);
  1349. if (ret)
  1350. return ret;
  1351. }
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  1355. #endif
  1356. struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
  1357. {
  1358. if (!netdev || !dsa_user_dev_check(netdev))
  1359. return ERR_PTR(-ENODEV);
  1360. return dsa_user_to_port(netdev);
  1361. }
  1362. EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
  1363. bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
  1364. {
  1365. if (a->type != b->type)
  1366. return false;
  1367. switch (a->type) {
  1368. case DSA_DB_PORT:
  1369. return a->dp == b->dp;
  1370. case DSA_DB_LAG:
  1371. return a->lag.dev == b->lag.dev;
  1372. case DSA_DB_BRIDGE:
  1373. return a->bridge.num == b->bridge.num;
  1374. default:
  1375. WARN_ON(1);
  1376. return false;
  1377. }
  1378. }
  1379. bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
  1380. const unsigned char *addr, u16 vid,
  1381. struct dsa_db db)
  1382. {
  1383. struct dsa_port *dp = dsa_to_port(ds, port);
  1384. struct dsa_mac_addr *a;
  1385. lockdep_assert_held(&dp->addr_lists_lock);
  1386. list_for_each_entry(a, &dp->fdbs, list) {
  1387. if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
  1388. continue;
  1389. if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
  1390. return true;
  1391. }
  1392. return false;
  1393. }
  1394. EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
  1395. bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
  1396. const struct switchdev_obj_port_mdb *mdb,
  1397. struct dsa_db db)
  1398. {
  1399. struct dsa_port *dp = dsa_to_port(ds, port);
  1400. struct dsa_mac_addr *a;
  1401. lockdep_assert_held(&dp->addr_lists_lock);
  1402. list_for_each_entry(a, &dp->mdbs, list) {
  1403. if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
  1404. continue;
  1405. if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
  1406. return true;
  1407. }
  1408. return false;
  1409. }
  1410. EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
  1411. /* Helpers for switches without specific HSR offloads, but which can implement
  1412. * NETIF_F_HW_HSR_DUP because their tagger uses dsa_xmit_port_mask()
  1413. */
  1414. int dsa_port_simple_hsr_validate(struct dsa_switch *ds, int port,
  1415. struct net_device *hsr,
  1416. struct netlink_ext_ack *extack)
  1417. {
  1418. enum hsr_port_type type;
  1419. int err;
  1420. err = hsr_get_port_type(hsr, dsa_to_port(ds, port)->user, &type);
  1421. if (err)
  1422. return err;
  1423. if (type != HSR_PT_SLAVE_A && type != HSR_PT_SLAVE_B) {
  1424. NL_SET_ERR_MSG_MOD(extack,
  1425. "Only HSR slave ports can be offloaded");
  1426. return -EOPNOTSUPP;
  1427. }
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_validate);
  1431. int dsa_port_simple_hsr_join(struct dsa_switch *ds, int port,
  1432. struct net_device *hsr,
  1433. struct netlink_ext_ack *extack)
  1434. {
  1435. struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
  1436. int err;
  1437. err = dsa_port_simple_hsr_validate(ds, port, hsr, extack);
  1438. if (err)
  1439. return err;
  1440. dsa_hsr_foreach_port(other_dp, ds, hsr) {
  1441. if (other_dp != dp) {
  1442. dp->user->features |= NETIF_F_HW_HSR_DUP;
  1443. other_dp->user->features |= NETIF_F_HW_HSR_DUP;
  1444. break;
  1445. }
  1446. }
  1447. return 0;
  1448. }
  1449. EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_join);
  1450. int dsa_port_simple_hsr_leave(struct dsa_switch *ds, int port,
  1451. struct net_device *hsr)
  1452. {
  1453. struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
  1454. dsa_hsr_foreach_port(other_dp, ds, hsr) {
  1455. if (other_dp != dp) {
  1456. dp->user->features &= ~NETIF_F_HW_HSR_DUP;
  1457. other_dp->user->features &= ~NETIF_F_HW_HSR_DUP;
  1458. break;
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. EXPORT_SYMBOL_GPL(dsa_port_simple_hsr_leave);
  1464. static const struct dsa_stubs __dsa_stubs = {
  1465. .conduit_hwtstamp_validate = __dsa_conduit_hwtstamp_validate,
  1466. };
  1467. static void dsa_register_stubs(void)
  1468. {
  1469. dsa_stubs = &__dsa_stubs;
  1470. }
  1471. static void dsa_unregister_stubs(void)
  1472. {
  1473. dsa_stubs = NULL;
  1474. }
  1475. static int __init dsa_init_module(void)
  1476. {
  1477. int rc;
  1478. dsa_owq = alloc_ordered_workqueue("dsa_ordered",
  1479. WQ_MEM_RECLAIM);
  1480. if (!dsa_owq)
  1481. return -ENOMEM;
  1482. rc = dsa_user_register_notifier();
  1483. if (rc)
  1484. goto register_notifier_fail;
  1485. dev_add_pack(&dsa_pack_type);
  1486. rc = rtnl_link_register(&dsa_link_ops);
  1487. if (rc)
  1488. goto netlink_register_fail;
  1489. dsa_register_stubs();
  1490. return 0;
  1491. netlink_register_fail:
  1492. dsa_user_unregister_notifier();
  1493. dev_remove_pack(&dsa_pack_type);
  1494. register_notifier_fail:
  1495. destroy_workqueue(dsa_owq);
  1496. return rc;
  1497. }
  1498. module_init(dsa_init_module);
  1499. static void __exit dsa_cleanup_module(void)
  1500. {
  1501. dsa_unregister_stubs();
  1502. rtnl_link_unregister(&dsa_link_ops);
  1503. dsa_user_unregister_notifier();
  1504. dev_remove_pack(&dsa_pack_type);
  1505. destroy_workqueue(dsa_owq);
  1506. }
  1507. module_exit(dsa_cleanup_module);
  1508. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  1509. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  1510. MODULE_LICENSE("GPL");
  1511. MODULE_ALIAS("platform:dsa");
  1512. MODULE_IMPORT_NS("NETDEV_INTERNAL");