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