dsa2.c 14 KB

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