dsa2.c 14 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. INIT_LIST_HEAD(&dst->list);
  52. list_add_tail(&dsa_switch_trees, &dst->list);
  53. kref_init(&dst->refcount);
  54. return dst;
  55. }
  56. static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
  57. struct dsa_switch *ds, u32 index)
  58. {
  59. kref_get(&dst->refcount);
  60. dst->ds[index] = ds;
  61. }
  62. static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
  63. struct dsa_switch *ds, u32 index)
  64. {
  65. dst->ds[index] = NULL;
  66. kref_put(&dst->refcount, dsa_free_dst);
  67. }
  68. static bool dsa_port_is_dsa(struct device_node *port)
  69. {
  70. return !!of_parse_phandle(port, "link", 0);
  71. }
  72. static bool dsa_port_is_cpu(struct device_node *port)
  73. {
  74. return !!of_parse_phandle(port, "ethernet", 0);
  75. }
  76. static bool dsa_ds_find_port(struct dsa_switch *ds,
  77. struct device_node *port)
  78. {
  79. u32 index;
  80. for (index = 0; index < DSA_MAX_PORTS; index++)
  81. if (ds->ports[index].dn == port)
  82. return true;
  83. return false;
  84. }
  85. static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
  86. struct device_node *port)
  87. {
  88. struct dsa_switch *ds;
  89. u32 index;
  90. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  91. ds = dst->ds[index];
  92. if (!ds)
  93. continue;
  94. if (dsa_ds_find_port(ds, port))
  95. return ds;
  96. }
  97. return NULL;
  98. }
  99. static int dsa_port_complete(struct dsa_switch_tree *dst,
  100. struct dsa_switch *src_ds,
  101. struct device_node *port,
  102. u32 src_port)
  103. {
  104. struct device_node *link;
  105. int index;
  106. struct dsa_switch *dst_ds;
  107. for (index = 0;; index++) {
  108. link = of_parse_phandle(port, "link", index);
  109. if (!link)
  110. break;
  111. dst_ds = dsa_dst_find_port(dst, link);
  112. of_node_put(link);
  113. if (!dst_ds)
  114. return 1;
  115. src_ds->rtable[dst_ds->index] = src_port;
  116. }
  117. return 0;
  118. }
  119. /* A switch is complete if all the DSA ports phandles point to ports
  120. * known in the tree. A return value of 1 means the tree is not
  121. * complete. This is not an error condition. A value of 0 is
  122. * success.
  123. */
  124. static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  125. {
  126. struct device_node *port;
  127. u32 index;
  128. int err;
  129. for (index = 0; index < DSA_MAX_PORTS; index++) {
  130. port = ds->ports[index].dn;
  131. if (!port)
  132. continue;
  133. if (!dsa_port_is_dsa(port))
  134. continue;
  135. err = dsa_port_complete(dst, ds, port, index);
  136. if (err != 0)
  137. return err;
  138. ds->dsa_port_mask |= BIT(index);
  139. }
  140. return 0;
  141. }
  142. /* A tree is complete if all the DSA ports phandles point to ports
  143. * known in the tree. A return value of 1 means the tree is not
  144. * complete. This is not an error condition. A value of 0 is
  145. * success.
  146. */
  147. static int dsa_dst_complete(struct dsa_switch_tree *dst)
  148. {
  149. struct dsa_switch *ds;
  150. u32 index;
  151. int err;
  152. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  153. ds = dst->ds[index];
  154. if (!ds)
  155. continue;
  156. err = dsa_ds_complete(dst, ds);
  157. if (err != 0)
  158. return err;
  159. }
  160. return 0;
  161. }
  162. static int dsa_dsa_port_apply(struct device_node *port, u32 index,
  163. struct dsa_switch *ds)
  164. {
  165. int err;
  166. err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
  167. if (err) {
  168. dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
  169. index, err);
  170. return err;
  171. }
  172. return 0;
  173. }
  174. static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
  175. struct dsa_switch *ds)
  176. {
  177. dsa_cpu_dsa_destroy(port);
  178. }
  179. static int dsa_cpu_port_apply(struct device_node *port, u32 index,
  180. struct dsa_switch *ds)
  181. {
  182. int err;
  183. err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
  184. if (err) {
  185. dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
  186. index, err);
  187. return err;
  188. }
  189. ds->cpu_port_mask |= BIT(index);
  190. return 0;
  191. }
  192. static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
  193. struct dsa_switch *ds)
  194. {
  195. dsa_cpu_dsa_destroy(port);
  196. ds->cpu_port_mask &= ~BIT(index);
  197. }
  198. static int dsa_user_port_apply(struct device_node *port, u32 index,
  199. struct dsa_switch *ds)
  200. {
  201. const char *name;
  202. int err;
  203. name = of_get_property(port, "label", NULL);
  204. if (!name)
  205. name = "eth%d";
  206. err = dsa_slave_create(ds, ds->dev, index, name);
  207. if (err) {
  208. dev_warn(ds->dev, "Failed to create slave %d: %d\n",
  209. index, err);
  210. return err;
  211. }
  212. return 0;
  213. }
  214. static void dsa_user_port_unapply(struct device_node *port, u32 index,
  215. struct dsa_switch *ds)
  216. {
  217. if (ds->ports[index].netdev) {
  218. dsa_slave_destroy(ds->ports[index].netdev);
  219. ds->ports[index].netdev = NULL;
  220. ds->enabled_port_mask &= ~(1 << index);
  221. }
  222. }
  223. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  224. {
  225. struct device_node *port;
  226. u32 index;
  227. int err;
  228. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  229. * driver and before ops->setup() has run, since the switch drivers and
  230. * the slave MDIO bus driver rely on these values for probing PHY
  231. * devices or not
  232. */
  233. ds->phys_mii_mask = ds->enabled_port_mask;
  234. err = ds->ops->setup(ds);
  235. if (err < 0)
  236. return err;
  237. if (ds->ops->set_addr) {
  238. err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
  239. if (err < 0)
  240. return err;
  241. }
  242. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  243. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  244. if (!ds->slave_mii_bus)
  245. return -ENOMEM;
  246. dsa_slave_mii_bus_init(ds);
  247. err = mdiobus_register(ds->slave_mii_bus);
  248. if (err < 0)
  249. return err;
  250. }
  251. for (index = 0; index < DSA_MAX_PORTS; index++) {
  252. port = ds->ports[index].dn;
  253. if (!port)
  254. continue;
  255. if (dsa_port_is_dsa(port)) {
  256. err = dsa_dsa_port_apply(port, index, ds);
  257. if (err)
  258. return err;
  259. continue;
  260. }
  261. if (dsa_port_is_cpu(port)) {
  262. err = dsa_cpu_port_apply(port, index, ds);
  263. if (err)
  264. return err;
  265. continue;
  266. }
  267. err = dsa_user_port_apply(port, index, ds);
  268. if (err)
  269. continue;
  270. }
  271. return 0;
  272. }
  273. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  274. {
  275. struct device_node *port;
  276. u32 index;
  277. for (index = 0; index < DSA_MAX_PORTS; index++) {
  278. port = ds->ports[index].dn;
  279. if (!port)
  280. continue;
  281. if (dsa_port_is_dsa(port)) {
  282. dsa_dsa_port_unapply(port, index, ds);
  283. continue;
  284. }
  285. if (dsa_port_is_cpu(port)) {
  286. dsa_cpu_port_unapply(port, index, ds);
  287. continue;
  288. }
  289. dsa_user_port_unapply(port, index, ds);
  290. }
  291. if (ds->slave_mii_bus && ds->ops->phy_read)
  292. mdiobus_unregister(ds->slave_mii_bus);
  293. }
  294. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  295. {
  296. struct dsa_switch *ds;
  297. u32 index;
  298. int err;
  299. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  300. ds = dst->ds[index];
  301. if (!ds)
  302. continue;
  303. err = dsa_ds_apply(dst, ds);
  304. if (err)
  305. return err;
  306. }
  307. if (dst->cpu_switch) {
  308. err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
  309. if (err)
  310. return err;
  311. }
  312. /* If we use a tagging format that doesn't have an ethertype
  313. * field, make sure that all packets from this point on get
  314. * sent to the tag format's receive function.
  315. */
  316. wmb();
  317. dst->master_netdev->dsa_ptr = (void *)dst;
  318. dst->applied = true;
  319. return 0;
  320. }
  321. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  322. {
  323. struct dsa_switch *ds;
  324. u32 index;
  325. if (!dst->applied)
  326. return;
  327. dst->master_netdev->dsa_ptr = NULL;
  328. /* If we used a tagging format that doesn't have an ethertype
  329. * field, make sure that all packets from this point get sent
  330. * without the tag and go through the regular receive path.
  331. */
  332. wmb();
  333. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  334. ds = dst->ds[index];
  335. if (!ds)
  336. continue;
  337. dsa_ds_unapply(dst, ds);
  338. }
  339. if (dst->cpu_switch)
  340. dsa_cpu_port_ethtool_restore(dst->cpu_switch);
  341. pr_info("DSA: tree %d unapplied\n", dst->tree);
  342. dst->applied = false;
  343. }
  344. static int dsa_cpu_parse(struct device_node *port, u32 index,
  345. struct dsa_switch_tree *dst,
  346. struct dsa_switch *ds)
  347. {
  348. enum dsa_tag_protocol tag_protocol;
  349. struct net_device *ethernet_dev;
  350. struct device_node *ethernet;
  351. ethernet = of_parse_phandle(port, "ethernet", 0);
  352. if (!ethernet)
  353. return -EINVAL;
  354. ethernet_dev = of_find_net_device_by_node(ethernet);
  355. if (!ethernet_dev)
  356. return -EPROBE_DEFER;
  357. if (!ds->master_netdev)
  358. ds->master_netdev = ethernet_dev;
  359. if (!dst->master_netdev)
  360. dst->master_netdev = ethernet_dev;
  361. if (!dst->cpu_switch) {
  362. dst->cpu_switch = ds;
  363. dst->cpu_port = index;
  364. }
  365. tag_protocol = ds->ops->get_tag_protocol(ds);
  366. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  367. if (IS_ERR(dst->tag_ops)) {
  368. dev_warn(ds->dev, "No tagger for this switch\n");
  369. return PTR_ERR(dst->tag_ops);
  370. }
  371. dst->rcv = dst->tag_ops->rcv;
  372. return 0;
  373. }
  374. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  375. {
  376. struct device_node *port;
  377. u32 index;
  378. int err;
  379. for (index = 0; index < DSA_MAX_PORTS; index++) {
  380. port = ds->ports[index].dn;
  381. if (!port)
  382. continue;
  383. if (dsa_port_is_cpu(port)) {
  384. err = dsa_cpu_parse(port, index, dst, ds);
  385. if (err)
  386. return err;
  387. }
  388. }
  389. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  390. return 0;
  391. }
  392. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  393. {
  394. struct dsa_switch *ds;
  395. u32 index;
  396. int err;
  397. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  398. ds = dst->ds[index];
  399. if (!ds)
  400. continue;
  401. err = dsa_ds_parse(dst, ds);
  402. if (err)
  403. return err;
  404. }
  405. if (!dst->master_netdev) {
  406. pr_warn("Tree has no master device\n");
  407. return -EINVAL;
  408. }
  409. pr_info("DSA: tree %d parsed\n", dst->tree);
  410. return 0;
  411. }
  412. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  413. {
  414. struct device_node *port;
  415. int err;
  416. u32 reg;
  417. for_each_available_child_of_node(ports, port) {
  418. err = of_property_read_u32(port, "reg", &reg);
  419. if (err)
  420. return err;
  421. if (reg >= DSA_MAX_PORTS)
  422. return -EINVAL;
  423. ds->ports[reg].dn = port;
  424. /* Initialize enabled_port_mask now for ops->setup()
  425. * to have access to a correct value, just like what
  426. * net/dsa/dsa.c::dsa_switch_setup_one does.
  427. */
  428. if (!dsa_port_is_cpu(port))
  429. ds->enabled_port_mask |= 1 << reg;
  430. }
  431. return 0;
  432. }
  433. static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
  434. {
  435. int err;
  436. *tree = *index = 0;
  437. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  438. if (err) {
  439. /* Does not exist, but it is optional */
  440. if (err == -EINVAL)
  441. return 0;
  442. return err;
  443. }
  444. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  445. if (err)
  446. return err;
  447. if (*index >= DSA_MAX_SWITCHES)
  448. return -EINVAL;
  449. return 0;
  450. }
  451. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  452. struct device_node *np)
  453. {
  454. struct device_node *ports;
  455. ports = of_get_child_by_name(np, "ports");
  456. if (!ports) {
  457. dev_err(ds->dev, "no ports child node found\n");
  458. return ERR_PTR(-EINVAL);
  459. }
  460. return ports;
  461. }
  462. static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
  463. {
  464. struct device_node *np = dev->of_node;
  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 *dev)
  527. {
  528. int err;
  529. mutex_lock(&dsa2_mutex);
  530. err = _dsa_register_switch(ds, dev);
  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);