dsa2.c 14 KB

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