dsa2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. ds->ports[index].netdev = NULL;
  222. return err;
  223. }
  224. return 0;
  225. }
  226. static void dsa_user_port_unapply(struct device_node *port, u32 index,
  227. struct dsa_switch *ds)
  228. {
  229. if (ds->ports[index].netdev) {
  230. dsa_slave_destroy(ds->ports[index].netdev);
  231. ds->ports[index].netdev = NULL;
  232. ds->enabled_port_mask &= ~(1 << index);
  233. }
  234. }
  235. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  236. {
  237. struct device_node *port;
  238. u32 index;
  239. int err;
  240. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  241. * driver and before ops->setup() has run, since the switch drivers and
  242. * the slave MDIO bus driver rely on these values for probing PHY
  243. * devices or not
  244. */
  245. ds->phys_mii_mask = ds->enabled_port_mask;
  246. err = ds->ops->setup(ds);
  247. if (err < 0)
  248. return err;
  249. if (ds->ops->set_addr) {
  250. err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
  251. if (err < 0)
  252. return err;
  253. }
  254. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  255. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  256. if (!ds->slave_mii_bus)
  257. return -ENOMEM;
  258. dsa_slave_mii_bus_init(ds);
  259. err = mdiobus_register(ds->slave_mii_bus);
  260. if (err < 0)
  261. return err;
  262. }
  263. for (index = 0; index < DSA_MAX_PORTS; index++) {
  264. port = ds->ports[index].dn;
  265. if (!port)
  266. continue;
  267. if (dsa_port_is_dsa(port)) {
  268. err = dsa_dsa_port_apply(port, index, ds);
  269. if (err)
  270. return err;
  271. continue;
  272. }
  273. if (dsa_port_is_cpu(port)) {
  274. err = dsa_cpu_port_apply(port, index, ds);
  275. if (err)
  276. return err;
  277. continue;
  278. }
  279. err = dsa_user_port_apply(port, index, ds);
  280. if (err)
  281. continue;
  282. }
  283. return 0;
  284. }
  285. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  286. {
  287. struct device_node *port;
  288. u32 index;
  289. for (index = 0; index < DSA_MAX_PORTS; index++) {
  290. port = ds->ports[index].dn;
  291. if (!port)
  292. continue;
  293. if (dsa_port_is_dsa(port)) {
  294. dsa_dsa_port_unapply(port, index, ds);
  295. continue;
  296. }
  297. if (dsa_port_is_cpu(port)) {
  298. dsa_cpu_port_unapply(port, index, ds);
  299. continue;
  300. }
  301. dsa_user_port_unapply(port, index, ds);
  302. }
  303. if (ds->slave_mii_bus && ds->ops->phy_read)
  304. mdiobus_unregister(ds->slave_mii_bus);
  305. }
  306. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  307. {
  308. struct dsa_switch *ds;
  309. u32 index;
  310. int err;
  311. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  312. ds = dst->ds[index];
  313. if (!ds)
  314. continue;
  315. err = dsa_ds_apply(dst, ds);
  316. if (err)
  317. return err;
  318. }
  319. if (dst->ds[0]) {
  320. err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
  321. if (err)
  322. return err;
  323. }
  324. /* If we use a tagging format that doesn't have an ethertype
  325. * field, make sure that all packets from this point on get
  326. * sent to the tag format's receive function.
  327. */
  328. wmb();
  329. dst->master_netdev->dsa_ptr = (void *)dst;
  330. dst->applied = true;
  331. return 0;
  332. }
  333. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  334. {
  335. struct dsa_switch *ds;
  336. u32 index;
  337. if (!dst->applied)
  338. return;
  339. dst->master_netdev->dsa_ptr = NULL;
  340. /* If we used a tagging format that doesn't have an ethertype
  341. * field, make sure that all packets from this point get sent
  342. * without the tag and go through the regular receive path.
  343. */
  344. wmb();
  345. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  346. ds = dst->ds[index];
  347. if (!ds)
  348. continue;
  349. dsa_ds_unapply(dst, ds);
  350. }
  351. if (dst->ds[0])
  352. dsa_cpu_port_ethtool_restore(dst->ds[0]);
  353. pr_info("DSA: tree %d unapplied\n", dst->tree);
  354. dst->applied = false;
  355. }
  356. static int dsa_cpu_parse(struct device_node *port, u32 index,
  357. struct dsa_switch_tree *dst,
  358. struct dsa_switch *ds)
  359. {
  360. enum dsa_tag_protocol tag_protocol;
  361. struct net_device *ethernet_dev;
  362. struct device_node *ethernet;
  363. ethernet = of_parse_phandle(port, "ethernet", 0);
  364. if (!ethernet)
  365. return -EINVAL;
  366. ethernet_dev = of_find_net_device_by_node(ethernet);
  367. if (!ethernet_dev)
  368. return -EPROBE_DEFER;
  369. if (!ds->master_netdev)
  370. ds->master_netdev = ethernet_dev;
  371. if (!dst->master_netdev)
  372. dst->master_netdev = ethernet_dev;
  373. if (dst->cpu_switch == -1) {
  374. dst->cpu_switch = ds->index;
  375. dst->cpu_port = index;
  376. }
  377. tag_protocol = ds->ops->get_tag_protocol(ds);
  378. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  379. if (IS_ERR(dst->tag_ops)) {
  380. dev_warn(ds->dev, "No tagger for this switch\n");
  381. return PTR_ERR(dst->tag_ops);
  382. }
  383. dst->rcv = dst->tag_ops->rcv;
  384. return 0;
  385. }
  386. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  387. {
  388. struct device_node *port;
  389. u32 index;
  390. int err;
  391. for (index = 0; index < DSA_MAX_PORTS; index++) {
  392. port = ds->ports[index].dn;
  393. if (!port)
  394. continue;
  395. if (dsa_port_is_cpu(port)) {
  396. err = dsa_cpu_parse(port, index, dst, ds);
  397. if (err)
  398. return err;
  399. }
  400. }
  401. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  402. return 0;
  403. }
  404. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  405. {
  406. struct dsa_switch *ds;
  407. u32 index;
  408. int err;
  409. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  410. ds = dst->ds[index];
  411. if (!ds)
  412. continue;
  413. err = dsa_ds_parse(dst, ds);
  414. if (err)
  415. return err;
  416. }
  417. if (!dst->master_netdev) {
  418. pr_warn("Tree has no master device\n");
  419. return -EINVAL;
  420. }
  421. pr_info("DSA: tree %d parsed\n", dst->tree);
  422. return 0;
  423. }
  424. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  425. {
  426. struct device_node *port;
  427. int err;
  428. u32 reg;
  429. for_each_available_child_of_node(ports, port) {
  430. err = of_property_read_u32(port, "reg", &reg);
  431. if (err)
  432. return err;
  433. if (reg >= DSA_MAX_PORTS)
  434. return -EINVAL;
  435. ds->ports[reg].dn = port;
  436. /* Initialize enabled_port_mask now for ops->setup()
  437. * to have access to a correct value, just like what
  438. * net/dsa/dsa.c::dsa_switch_setup_one does.
  439. */
  440. if (!dsa_port_is_cpu(port))
  441. ds->enabled_port_mask |= 1 << reg;
  442. }
  443. return 0;
  444. }
  445. static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
  446. {
  447. int err;
  448. *tree = *index = 0;
  449. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  450. if (err) {
  451. /* Does not exist, but it is optional */
  452. if (err == -EINVAL)
  453. return 0;
  454. return err;
  455. }
  456. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  457. if (err)
  458. return err;
  459. if (*index >= DSA_MAX_SWITCHES)
  460. return -EINVAL;
  461. return 0;
  462. }
  463. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  464. struct device_node *np)
  465. {
  466. struct device_node *ports;
  467. ports = of_get_child_by_name(np, "ports");
  468. if (!ports) {
  469. dev_err(ds->dev, "no ports child node found\n");
  470. return ERR_PTR(-EINVAL);
  471. }
  472. return ports;
  473. }
  474. static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  475. {
  476. struct device_node *ports = dsa_get_ports(ds, np);
  477. struct dsa_switch_tree *dst;
  478. u32 tree, index;
  479. int i, err;
  480. err = dsa_parse_member(np, &tree, &index);
  481. if (err)
  482. return err;
  483. if (IS_ERR(ports))
  484. return PTR_ERR(ports);
  485. err = dsa_parse_ports_dn(ports, ds);
  486. if (err)
  487. return err;
  488. dst = dsa_get_dst(tree);
  489. if (!dst) {
  490. dst = dsa_add_dst(tree);
  491. if (!dst)
  492. return -ENOMEM;
  493. }
  494. if (dst->ds[index]) {
  495. err = -EBUSY;
  496. goto out;
  497. }
  498. ds->dst = dst;
  499. ds->index = index;
  500. /* Initialize the routing table */
  501. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  502. ds->rtable[i] = DSA_RTABLE_NONE;
  503. dsa_dst_add_ds(dst, ds, index);
  504. err = dsa_dst_complete(dst);
  505. if (err < 0)
  506. goto out_del_dst;
  507. if (err == 1) {
  508. /* Not all switches registered yet */
  509. err = 0;
  510. goto out;
  511. }
  512. if (dst->applied) {
  513. pr_info("DSA: Disjoint trees?\n");
  514. return -EINVAL;
  515. }
  516. err = dsa_dst_parse(dst);
  517. if (err)
  518. goto out_del_dst;
  519. err = dsa_dst_apply(dst);
  520. if (err) {
  521. dsa_dst_unapply(dst);
  522. goto out_del_dst;
  523. }
  524. dsa_put_dst(dst);
  525. return 0;
  526. out_del_dst:
  527. dsa_dst_del_ds(dst, ds, ds->index);
  528. out:
  529. dsa_put_dst(dst);
  530. return err;
  531. }
  532. int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  533. {
  534. int err;
  535. mutex_lock(&dsa2_mutex);
  536. err = _dsa_register_switch(ds, np);
  537. mutex_unlock(&dsa2_mutex);
  538. return err;
  539. }
  540. EXPORT_SYMBOL_GPL(dsa_register_switch);
  541. static void _dsa_unregister_switch(struct dsa_switch *ds)
  542. {
  543. struct dsa_switch_tree *dst = ds->dst;
  544. dsa_dst_unapply(dst);
  545. dsa_dst_del_ds(dst, ds, ds->index);
  546. }
  547. void dsa_unregister_switch(struct dsa_switch *ds)
  548. {
  549. mutex_lock(&dsa2_mutex);
  550. _dsa_unregister_switch(ds);
  551. mutex_unlock(&dsa2_mutex);
  552. }
  553. EXPORT_SYMBOL_GPL(dsa_unregister_switch);