dsa2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
  309. if (err)
  310. return err;
  311. /* If we use a tagging format that doesn't have an ethertype
  312. * field, make sure that all packets from this point on get
  313. * sent to the tag format's receive function.
  314. */
  315. wmb();
  316. dst->master_netdev->dsa_ptr = (void *)dst;
  317. dst->applied = true;
  318. return 0;
  319. }
  320. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  321. {
  322. struct dsa_switch *ds;
  323. u32 index;
  324. if (!dst->applied)
  325. return;
  326. dst->master_netdev->dsa_ptr = NULL;
  327. /* If we used a tagging format that doesn't have an ethertype
  328. * field, make sure that all packets from this point get sent
  329. * without the tag and go through the regular receive path.
  330. */
  331. wmb();
  332. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  333. ds = dst->ds[index];
  334. if (!ds)
  335. continue;
  336. dsa_ds_unapply(dst, ds);
  337. }
  338. dsa_cpu_port_ethtool_restore(dst->ds[0]);
  339. pr_info("DSA: tree %d unapplied\n", dst->tree);
  340. dst->applied = false;
  341. }
  342. static int dsa_cpu_parse(struct device_node *port, u32 index,
  343. struct dsa_switch_tree *dst,
  344. struct dsa_switch *ds)
  345. {
  346. enum dsa_tag_protocol tag_protocol;
  347. struct net_device *ethernet_dev;
  348. struct device_node *ethernet;
  349. ethernet = of_parse_phandle(port, "ethernet", 0);
  350. if (!ethernet)
  351. return -EINVAL;
  352. ethernet_dev = of_find_net_device_by_node(ethernet);
  353. if (!ethernet_dev)
  354. return -EPROBE_DEFER;
  355. if (!ds->master_netdev)
  356. ds->master_netdev = ethernet_dev;
  357. if (!dst->master_netdev)
  358. dst->master_netdev = ethernet_dev;
  359. if (dst->cpu_switch == -1) {
  360. dst->cpu_switch = ds->index;
  361. dst->cpu_port = index;
  362. }
  363. tag_protocol = ds->ops->get_tag_protocol(ds);
  364. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  365. if (IS_ERR(dst->tag_ops)) {
  366. dev_warn(ds->dev, "No tagger for this switch\n");
  367. return PTR_ERR(dst->tag_ops);
  368. }
  369. dst->rcv = dst->tag_ops->rcv;
  370. return 0;
  371. }
  372. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  373. {
  374. struct device_node *port;
  375. u32 index;
  376. int err;
  377. for (index = 0; index < DSA_MAX_PORTS; index++) {
  378. port = ds->ports[index].dn;
  379. if (!port)
  380. continue;
  381. if (dsa_port_is_cpu(port)) {
  382. err = dsa_cpu_parse(port, index, dst, ds);
  383. if (err)
  384. return err;
  385. }
  386. }
  387. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  388. return 0;
  389. }
  390. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  391. {
  392. struct dsa_switch *ds;
  393. u32 index;
  394. int err;
  395. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  396. ds = dst->ds[index];
  397. if (!ds)
  398. continue;
  399. err = dsa_ds_parse(dst, ds);
  400. if (err)
  401. return err;
  402. }
  403. if (!dst->master_netdev) {
  404. pr_warn("Tree has no master device\n");
  405. return -EINVAL;
  406. }
  407. pr_info("DSA: tree %d parsed\n", dst->tree);
  408. return 0;
  409. }
  410. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  411. {
  412. struct device_node *port;
  413. int err;
  414. u32 reg;
  415. for_each_available_child_of_node(ports, port) {
  416. err = of_property_read_u32(port, "reg", &reg);
  417. if (err)
  418. return err;
  419. if (reg >= DSA_MAX_PORTS)
  420. return -EINVAL;
  421. ds->ports[reg].dn = port;
  422. /* Initialize enabled_port_mask now for ops->setup()
  423. * to have access to a correct value, just like what
  424. * net/dsa/dsa.c::dsa_switch_setup_one does.
  425. */
  426. if (!dsa_port_is_cpu(port))
  427. ds->enabled_port_mask |= 1 << reg;
  428. }
  429. return 0;
  430. }
  431. static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
  432. {
  433. int err;
  434. *tree = *index = 0;
  435. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  436. if (err) {
  437. /* Does not exist, but it is optional */
  438. if (err == -EINVAL)
  439. return 0;
  440. return err;
  441. }
  442. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  443. if (err)
  444. return err;
  445. if (*index >= DSA_MAX_SWITCHES)
  446. return -EINVAL;
  447. return 0;
  448. }
  449. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  450. struct device_node *np)
  451. {
  452. struct device_node *ports;
  453. ports = of_get_child_by_name(np, "ports");
  454. if (!ports) {
  455. dev_err(ds->dev, "no ports child node found\n");
  456. return ERR_PTR(-EINVAL);
  457. }
  458. return ports;
  459. }
  460. static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  461. {
  462. struct device_node *ports = dsa_get_ports(ds, np);
  463. struct dsa_switch_tree *dst;
  464. u32 tree, index;
  465. int i, err;
  466. err = dsa_parse_member(np, &tree, &index);
  467. if (err)
  468. return err;
  469. if (IS_ERR(ports))
  470. return PTR_ERR(ports);
  471. err = dsa_parse_ports_dn(ports, ds);
  472. if (err)
  473. return err;
  474. dst = dsa_get_dst(tree);
  475. if (!dst) {
  476. dst = dsa_add_dst(tree);
  477. if (!dst)
  478. return -ENOMEM;
  479. }
  480. if (dst->ds[index]) {
  481. err = -EBUSY;
  482. goto out;
  483. }
  484. ds->dst = dst;
  485. ds->index = index;
  486. /* Initialize the routing table */
  487. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  488. ds->rtable[i] = DSA_RTABLE_NONE;
  489. dsa_dst_add_ds(dst, ds, index);
  490. err = dsa_dst_complete(dst);
  491. if (err < 0)
  492. goto out_del_dst;
  493. if (err == 1) {
  494. /* Not all switches registered yet */
  495. err = 0;
  496. goto out;
  497. }
  498. if (dst->applied) {
  499. pr_info("DSA: Disjoint trees?\n");
  500. return -EINVAL;
  501. }
  502. err = dsa_dst_parse(dst);
  503. if (err) {
  504. if (err == -EPROBE_DEFER) {
  505. dsa_dst_del_ds(dst, ds, ds->index);
  506. return err;
  507. }
  508. goto out_del_dst;
  509. }
  510. err = dsa_dst_apply(dst);
  511. if (err) {
  512. dsa_dst_unapply(dst);
  513. goto out_del_dst;
  514. }
  515. dsa_put_dst(dst);
  516. return 0;
  517. out_del_dst:
  518. dsa_dst_del_ds(dst, ds, ds->index);
  519. out:
  520. dsa_put_dst(dst);
  521. return err;
  522. }
  523. int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  524. {
  525. int err;
  526. mutex_lock(&dsa2_mutex);
  527. err = _dsa_register_switch(ds, np);
  528. mutex_unlock(&dsa2_mutex);
  529. return err;
  530. }
  531. EXPORT_SYMBOL_GPL(dsa_register_switch);
  532. static void _dsa_unregister_switch(struct dsa_switch *ds)
  533. {
  534. struct dsa_switch_tree *dst = ds->dst;
  535. dsa_dst_unapply(dst);
  536. dsa_dst_del_ds(dst, ds, ds->index);
  537. }
  538. void dsa_unregister_switch(struct dsa_switch *ds)
  539. {
  540. mutex_lock(&dsa2_mutex);
  541. _dsa_unregister_switch(ds);
  542. mutex_unlock(&dsa2_mutex);
  543. }
  544. EXPORT_SYMBOL_GPL(dsa_unregister_switch);