dsa2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * net/dsa/dsa2.c - Hardware switch handling, binding version 2
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
  5. * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/dsa.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include "dsa_priv.h"
  21. static LIST_HEAD(dsa_switch_trees);
  22. static DEFINE_MUTEX(dsa2_mutex);
  23. static struct dsa_switch_tree *dsa_get_dst(u32 tree)
  24. {
  25. struct dsa_switch_tree *dst;
  26. list_for_each_entry(dst, &dsa_switch_trees, list)
  27. if (dst->tree == tree) {
  28. kref_get(&dst->refcount);
  29. return dst;
  30. }
  31. return NULL;
  32. }
  33. static void dsa_free_dst(struct kref *ref)
  34. {
  35. struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
  36. refcount);
  37. list_del(&dst->list);
  38. kfree(dst);
  39. }
  40. static void dsa_put_dst(struct dsa_switch_tree *dst)
  41. {
  42. kref_put(&dst->refcount, dsa_free_dst);
  43. }
  44. static struct dsa_switch_tree *dsa_add_dst(u32 tree)
  45. {
  46. struct dsa_switch_tree *dst;
  47. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  48. if (!dst)
  49. return NULL;
  50. dst->tree = tree;
  51. dst->cpu_switch = -1;
  52. INIT_LIST_HEAD(&dst->list);
  53. list_add_tail(&dsa_switch_trees, &dst->list);
  54. kref_init(&dst->refcount);
  55. return dst;
  56. }
  57. static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
  58. struct dsa_switch *ds, u32 index)
  59. {
  60. kref_get(&dst->refcount);
  61. dst->ds[index] = ds;
  62. }
  63. static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
  64. struct dsa_switch *ds, u32 index)
  65. {
  66. dst->ds[index] = NULL;
  67. kref_put(&dst->refcount, dsa_free_dst);
  68. }
  69. static bool dsa_port_is_dsa(struct device_node *port)
  70. {
  71. return !!of_parse_phandle(port, "link", 0);
  72. }
  73. static bool dsa_port_is_cpu(struct device_node *port)
  74. {
  75. return !!of_parse_phandle(port, "ethernet", 0);
  76. }
  77. static bool dsa_ds_find_port(struct dsa_switch *ds,
  78. struct device_node *port)
  79. {
  80. u32 index;
  81. for (index = 0; index < DSA_MAX_PORTS; index++)
  82. if (ds->ports[index].dn == port)
  83. return true;
  84. return false;
  85. }
  86. static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
  87. struct device_node *port)
  88. {
  89. struct dsa_switch *ds;
  90. u32 index;
  91. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  92. ds = dst->ds[index];
  93. if (!ds)
  94. continue;
  95. if (dsa_ds_find_port(ds, port))
  96. return ds;
  97. }
  98. return NULL;
  99. }
  100. static int dsa_port_complete(struct dsa_switch_tree *dst,
  101. struct dsa_switch *src_ds,
  102. struct device_node *port,
  103. u32 src_port)
  104. {
  105. struct device_node *link;
  106. int index;
  107. struct dsa_switch *dst_ds;
  108. for (index = 0;; index++) {
  109. link = of_parse_phandle(port, "link", index);
  110. if (!link)
  111. break;
  112. dst_ds = dsa_dst_find_port(dst, link);
  113. of_node_put(link);
  114. if (!dst_ds)
  115. return 1;
  116. src_ds->rtable[dst_ds->index] = src_port;
  117. }
  118. return 0;
  119. }
  120. /* A switch is complete if all the DSA ports phandles point to ports
  121. * known in the tree. A return value of 1 means the tree is not
  122. * complete. This is not an error condition. A value of 0 is
  123. * success.
  124. */
  125. static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  126. {
  127. struct device_node *port;
  128. u32 index;
  129. int err;
  130. for (index = 0; index < DSA_MAX_PORTS; index++) {
  131. port = ds->ports[index].dn;
  132. if (!port)
  133. continue;
  134. if (!dsa_port_is_dsa(port))
  135. continue;
  136. err = dsa_port_complete(dst, ds, port, index);
  137. if (err != 0)
  138. return err;
  139. ds->dsa_port_mask |= BIT(index);
  140. }
  141. return 0;
  142. }
  143. /* A tree is complete if all the DSA ports phandles point to ports
  144. * known in the tree. A return value of 1 means the tree is not
  145. * complete. This is not an error condition. A value of 0 is
  146. * success.
  147. */
  148. static int dsa_dst_complete(struct dsa_switch_tree *dst)
  149. {
  150. struct dsa_switch *ds;
  151. u32 index;
  152. int err;
  153. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  154. ds = dst->ds[index];
  155. if (!ds)
  156. continue;
  157. err = dsa_ds_complete(dst, ds);
  158. if (err != 0)
  159. return err;
  160. }
  161. return 0;
  162. }
  163. static int dsa_dsa_port_apply(struct device_node *port, u32 index,
  164. struct dsa_switch *ds)
  165. {
  166. int err;
  167. err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
  168. if (err) {
  169. dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
  170. index, err);
  171. return err;
  172. }
  173. return 0;
  174. }
  175. static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
  176. struct dsa_switch *ds)
  177. {
  178. dsa_cpu_dsa_destroy(port);
  179. }
  180. static int dsa_cpu_port_apply(struct device_node *port, u32 index,
  181. struct dsa_switch *ds)
  182. {
  183. int err;
  184. err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
  185. if (err) {
  186. dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
  187. index, err);
  188. return err;
  189. }
  190. ds->cpu_port_mask |= BIT(index);
  191. return 0;
  192. }
  193. static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
  194. struct dsa_switch *ds)
  195. {
  196. dsa_cpu_dsa_destroy(port);
  197. ds->cpu_port_mask &= ~BIT(index);
  198. }
  199. static int dsa_user_port_apply(struct device_node *port, u32 index,
  200. struct dsa_switch *ds)
  201. {
  202. const char *name;
  203. int err;
  204. name = of_get_property(port, "label", NULL);
  205. if (!name)
  206. name = "eth%d";
  207. err = dsa_slave_create(ds, ds->dev, index, name);
  208. if (err) {
  209. dev_warn(ds->dev, "Failed to create slave %d: %d\n",
  210. index, err);
  211. return err;
  212. }
  213. return 0;
  214. }
  215. static void dsa_user_port_unapply(struct device_node *port, u32 index,
  216. struct dsa_switch *ds)
  217. {
  218. if (ds->ports[index].netdev) {
  219. dsa_slave_destroy(ds->ports[index].netdev);
  220. ds->ports[index].netdev = NULL;
  221. ds->enabled_port_mask &= ~(1 << index);
  222. }
  223. }
  224. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  225. {
  226. struct device_node *port;
  227. u32 index;
  228. int err;
  229. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  230. * driver and before ops->setup() has run, since the switch drivers and
  231. * the slave MDIO bus driver rely on these values for probing PHY
  232. * devices or not
  233. */
  234. ds->phys_mii_mask = ds->enabled_port_mask;
  235. err = ds->ops->setup(ds);
  236. if (err < 0)
  237. return err;
  238. if (ds->ops->set_addr) {
  239. err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
  240. if (err < 0)
  241. return err;
  242. }
  243. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  244. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  245. if (!ds->slave_mii_bus)
  246. return -ENOMEM;
  247. dsa_slave_mii_bus_init(ds);
  248. err = mdiobus_register(ds->slave_mii_bus);
  249. if (err < 0)
  250. return err;
  251. }
  252. for (index = 0; index < DSA_MAX_PORTS; index++) {
  253. port = ds->ports[index].dn;
  254. if (!port)
  255. continue;
  256. if (dsa_port_is_dsa(port)) {
  257. err = dsa_dsa_port_apply(port, index, ds);
  258. if (err)
  259. return err;
  260. continue;
  261. }
  262. if (dsa_port_is_cpu(port)) {
  263. err = dsa_cpu_port_apply(port, index, ds);
  264. if (err)
  265. return err;
  266. continue;
  267. }
  268. err = dsa_user_port_apply(port, index, ds);
  269. if (err)
  270. continue;
  271. }
  272. return 0;
  273. }
  274. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  275. {
  276. struct device_node *port;
  277. u32 index;
  278. for (index = 0; index < DSA_MAX_PORTS; index++) {
  279. port = ds->ports[index].dn;
  280. if (!port)
  281. continue;
  282. if (dsa_port_is_dsa(port)) {
  283. dsa_dsa_port_unapply(port, index, ds);
  284. continue;
  285. }
  286. if (dsa_port_is_cpu(port)) {
  287. dsa_cpu_port_unapply(port, index, ds);
  288. continue;
  289. }
  290. dsa_user_port_unapply(port, index, ds);
  291. }
  292. if (ds->slave_mii_bus && ds->ops->phy_read)
  293. mdiobus_unregister(ds->slave_mii_bus);
  294. }
  295. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  296. {
  297. struct dsa_switch *ds;
  298. u32 index;
  299. int err;
  300. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  301. ds = dst->ds[index];
  302. if (!ds)
  303. continue;
  304. err = dsa_ds_apply(dst, ds);
  305. if (err)
  306. return err;
  307. }
  308. if (dst->ds[0]) {
  309. err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
  310. if (err)
  311. return err;
  312. }
  313. /* If we use a tagging format that doesn't have an ethertype
  314. * field, make sure that all packets from this point on get
  315. * sent to the tag format's receive function.
  316. */
  317. wmb();
  318. dst->master_netdev->dsa_ptr = (void *)dst;
  319. dst->applied = true;
  320. return 0;
  321. }
  322. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  323. {
  324. struct dsa_switch *ds;
  325. u32 index;
  326. if (!dst->applied)
  327. return;
  328. dst->master_netdev->dsa_ptr = NULL;
  329. /* If we used a tagging format that doesn't have an ethertype
  330. * field, make sure that all packets from this point get sent
  331. * without the tag and go through the regular receive path.
  332. */
  333. wmb();
  334. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  335. ds = dst->ds[index];
  336. if (!ds)
  337. continue;
  338. dsa_ds_unapply(dst, ds);
  339. }
  340. if (dst->ds[0])
  341. dsa_cpu_port_ethtool_restore(dst->ds[0]);
  342. pr_info("DSA: tree %d unapplied\n", dst->tree);
  343. dst->applied = false;
  344. }
  345. static int dsa_cpu_parse(struct device_node *port, u32 index,
  346. struct dsa_switch_tree *dst,
  347. struct dsa_switch *ds)
  348. {
  349. enum dsa_tag_protocol tag_protocol;
  350. struct net_device *ethernet_dev;
  351. struct device_node *ethernet;
  352. ethernet = of_parse_phandle(port, "ethernet", 0);
  353. if (!ethernet)
  354. return -EINVAL;
  355. ethernet_dev = of_find_net_device_by_node(ethernet);
  356. if (!ethernet_dev)
  357. return -EPROBE_DEFER;
  358. if (!ds->master_netdev)
  359. ds->master_netdev = ethernet_dev;
  360. if (!dst->master_netdev)
  361. dst->master_netdev = ethernet_dev;
  362. if (dst->cpu_switch == -1) {
  363. dst->cpu_switch = ds->index;
  364. dst->cpu_port = index;
  365. }
  366. tag_protocol = ds->ops->get_tag_protocol(ds);
  367. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  368. if (IS_ERR(dst->tag_ops)) {
  369. dev_warn(ds->dev, "No tagger for this switch\n");
  370. return PTR_ERR(dst->tag_ops);
  371. }
  372. dst->rcv = dst->tag_ops->rcv;
  373. return 0;
  374. }
  375. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  376. {
  377. struct device_node *port;
  378. u32 index;
  379. int err;
  380. for (index = 0; index < DSA_MAX_PORTS; index++) {
  381. port = ds->ports[index].dn;
  382. if (!port)
  383. continue;
  384. if (dsa_port_is_cpu(port)) {
  385. err = dsa_cpu_parse(port, index, dst, ds);
  386. if (err)
  387. return err;
  388. }
  389. }
  390. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  391. return 0;
  392. }
  393. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  394. {
  395. struct dsa_switch *ds;
  396. u32 index;
  397. int err;
  398. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  399. ds = dst->ds[index];
  400. if (!ds)
  401. continue;
  402. err = dsa_ds_parse(dst, ds);
  403. if (err)
  404. return err;
  405. }
  406. if (!dst->master_netdev) {
  407. pr_warn("Tree has no master device\n");
  408. return -EINVAL;
  409. }
  410. pr_info("DSA: tree %d parsed\n", dst->tree);
  411. return 0;
  412. }
  413. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  414. {
  415. struct device_node *port;
  416. int err;
  417. u32 reg;
  418. for_each_available_child_of_node(ports, port) {
  419. err = of_property_read_u32(port, "reg", &reg);
  420. if (err)
  421. return err;
  422. if (reg >= DSA_MAX_PORTS)
  423. return -EINVAL;
  424. ds->ports[reg].dn = port;
  425. /* Initialize enabled_port_mask now for ops->setup()
  426. * to have access to a correct value, just like what
  427. * net/dsa/dsa.c::dsa_switch_setup_one does.
  428. */
  429. if (!dsa_port_is_cpu(port))
  430. ds->enabled_port_mask |= 1 << reg;
  431. }
  432. return 0;
  433. }
  434. static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
  435. {
  436. int err;
  437. *tree = *index = 0;
  438. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  439. if (err) {
  440. /* Does not exist, but it is optional */
  441. if (err == -EINVAL)
  442. return 0;
  443. return err;
  444. }
  445. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  446. if (err)
  447. return err;
  448. if (*index >= DSA_MAX_SWITCHES)
  449. return -EINVAL;
  450. return 0;
  451. }
  452. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  453. struct device_node *np)
  454. {
  455. struct device_node *ports;
  456. ports = of_get_child_by_name(np, "ports");
  457. if (!ports) {
  458. dev_err(ds->dev, "no ports child node found\n");
  459. return ERR_PTR(-EINVAL);
  460. }
  461. return ports;
  462. }
  463. static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  464. {
  465. struct device_node *ports = dsa_get_ports(ds, np);
  466. struct dsa_switch_tree *dst;
  467. u32 tree, index;
  468. int i, err;
  469. err = dsa_parse_member(np, &tree, &index);
  470. if (err)
  471. return err;
  472. if (IS_ERR(ports))
  473. return PTR_ERR(ports);
  474. err = dsa_parse_ports_dn(ports, ds);
  475. if (err)
  476. return err;
  477. dst = dsa_get_dst(tree);
  478. if (!dst) {
  479. dst = dsa_add_dst(tree);
  480. if (!dst)
  481. return -ENOMEM;
  482. }
  483. if (dst->ds[index]) {
  484. err = -EBUSY;
  485. goto out;
  486. }
  487. ds->dst = dst;
  488. ds->index = index;
  489. /* Initialize the routing table */
  490. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  491. ds->rtable[i] = DSA_RTABLE_NONE;
  492. dsa_dst_add_ds(dst, ds, index);
  493. err = dsa_dst_complete(dst);
  494. if (err < 0)
  495. goto out_del_dst;
  496. if (err == 1) {
  497. /* Not all switches registered yet */
  498. err = 0;
  499. goto out;
  500. }
  501. if (dst->applied) {
  502. pr_info("DSA: Disjoint trees?\n");
  503. return -EINVAL;
  504. }
  505. err = dsa_dst_parse(dst);
  506. if (err) {
  507. if (err == -EPROBE_DEFER) {
  508. dsa_dst_del_ds(dst, ds, ds->index);
  509. return err;
  510. }
  511. goto out_del_dst;
  512. }
  513. err = dsa_dst_apply(dst);
  514. if (err) {
  515. dsa_dst_unapply(dst);
  516. goto out_del_dst;
  517. }
  518. dsa_put_dst(dst);
  519. return 0;
  520. out_del_dst:
  521. dsa_dst_del_ds(dst, ds, ds->index);
  522. out:
  523. dsa_put_dst(dst);
  524. return err;
  525. }
  526. int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  527. {
  528. int err;
  529. mutex_lock(&dsa2_mutex);
  530. err = _dsa_register_switch(ds, np);
  531. mutex_unlock(&dsa2_mutex);
  532. return err;
  533. }
  534. EXPORT_SYMBOL_GPL(dsa_register_switch);
  535. static void _dsa_unregister_switch(struct dsa_switch *ds)
  536. {
  537. struct dsa_switch_tree *dst = ds->dst;
  538. dsa_dst_unapply(dst);
  539. dsa_dst_del_ds(dst, ds, ds->index);
  540. }
  541. void dsa_unregister_switch(struct dsa_switch *ds)
  542. {
  543. mutex_lock(&dsa2_mutex);
  544. _dsa_unregister_switch(ds);
  545. mutex_unlock(&dsa2_mutex);
  546. }
  547. EXPORT_SYMBOL_GPL(dsa_unregister_switch);