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 drv->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->drv->setup(ds);
  244. if (err < 0)
  245. return err;
  246. err = ds->drv->set_addr(ds, dst->master_netdev->dev_addr);
  247. if (err < 0)
  248. return err;
  249. err = ds->drv->set_addr(ds, dst->master_netdev->dev_addr);
  250. if (err < 0)
  251. return err;
  252. if (!ds->slave_mii_bus && ds->drv->phy_read) {
  253. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  254. if (!ds->slave_mii_bus)
  255. return -ENOMEM;
  256. dsa_slave_mii_bus_init(ds);
  257. err = mdiobus_register(ds->slave_mii_bus);
  258. if (err < 0)
  259. return err;
  260. }
  261. for (index = 0; index < DSA_MAX_PORTS; index++) {
  262. port = ds->ports[index].dn;
  263. if (!port)
  264. continue;
  265. if (dsa_port_is_dsa(port)) {
  266. err = dsa_dsa_port_apply(port, index, ds);
  267. if (err)
  268. return err;
  269. continue;
  270. }
  271. if (dsa_port_is_cpu(port)) {
  272. err = dsa_cpu_port_apply(port, index, ds);
  273. if (err)
  274. return err;
  275. continue;
  276. }
  277. err = dsa_user_port_apply(port, index, ds);
  278. if (err)
  279. continue;
  280. }
  281. return 0;
  282. }
  283. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  284. {
  285. struct device_node *port;
  286. u32 index;
  287. for (index = 0; index < DSA_MAX_PORTS; index++) {
  288. port = ds->ports[index].dn;
  289. if (!port)
  290. continue;
  291. if (dsa_port_is_dsa(port)) {
  292. dsa_dsa_port_unapply(port, index, ds);
  293. continue;
  294. }
  295. if (dsa_port_is_cpu(port)) {
  296. dsa_cpu_port_unapply(port, index, ds);
  297. continue;
  298. }
  299. dsa_user_port_unapply(port, index, ds);
  300. }
  301. if (ds->slave_mii_bus && ds->drv->phy_read)
  302. mdiobus_unregister(ds->slave_mii_bus);
  303. }
  304. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  305. {
  306. struct dsa_switch *ds;
  307. u32 index;
  308. int err;
  309. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  310. ds = dst->ds[index];
  311. if (!ds)
  312. continue;
  313. err = dsa_ds_apply(dst, ds);
  314. if (err)
  315. return err;
  316. }
  317. err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
  318. if (err)
  319. return err;
  320. /* If we use a tagging format that doesn't have an ethertype
  321. * field, make sure that all packets from this point on get
  322. * sent to the tag format's receive function.
  323. */
  324. wmb();
  325. dst->master_netdev->dsa_ptr = (void *)dst;
  326. dst->applied = true;
  327. return 0;
  328. }
  329. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  330. {
  331. struct dsa_switch *ds;
  332. u32 index;
  333. if (!dst->applied)
  334. return;
  335. dst->master_netdev->dsa_ptr = NULL;
  336. /* If we used a tagging format that doesn't have an ethertype
  337. * field, make sure that all packets from this point get sent
  338. * without the tag and go through the regular receive path.
  339. */
  340. wmb();
  341. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  342. ds = dst->ds[index];
  343. if (!ds)
  344. continue;
  345. dsa_ds_unapply(dst, ds);
  346. }
  347. dsa_cpu_port_ethtool_restore(dst->ds[0]);
  348. pr_info("DSA: tree %d unapplied\n", dst->tree);
  349. dst->applied = false;
  350. }
  351. static int dsa_cpu_parse(struct device_node *port, u32 index,
  352. struct dsa_switch_tree *dst,
  353. struct dsa_switch *ds)
  354. {
  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. dst->tag_ops = dsa_resolve_tag_protocol(ds->drv->tag_protocol);
  372. if (IS_ERR(dst->tag_ops)) {
  373. dev_warn(ds->dev, "No tagger for this switch\n");
  374. return PTR_ERR(dst->tag_ops);
  375. }
  376. dst->rcv = dst->tag_ops->rcv;
  377. return 0;
  378. }
  379. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  380. {
  381. struct device_node *port;
  382. u32 index;
  383. int err;
  384. for (index = 0; index < DSA_MAX_PORTS; index++) {
  385. port = ds->ports[index].dn;
  386. if (!port)
  387. continue;
  388. if (dsa_port_is_cpu(port)) {
  389. err = dsa_cpu_parse(port, index, dst, ds);
  390. if (err)
  391. return err;
  392. }
  393. }
  394. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  395. return 0;
  396. }
  397. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  398. {
  399. struct dsa_switch *ds;
  400. u32 index;
  401. int err;
  402. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  403. ds = dst->ds[index];
  404. if (!ds)
  405. continue;
  406. err = dsa_ds_parse(dst, ds);
  407. if (err)
  408. return err;
  409. }
  410. if (!dst->master_netdev) {
  411. pr_warn("Tree has no master device\n");
  412. return -EINVAL;
  413. }
  414. pr_info("DSA: tree %d parsed\n", dst->tree);
  415. return 0;
  416. }
  417. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  418. {
  419. struct device_node *port;
  420. int err;
  421. u32 reg;
  422. for_each_available_child_of_node(ports, port) {
  423. err = of_property_read_u32(port, "reg", &reg);
  424. if (err)
  425. return err;
  426. if (reg >= DSA_MAX_PORTS)
  427. return -EINVAL;
  428. ds->ports[reg].dn = port;
  429. /* Initialize enabled_port_mask now for drv->setup()
  430. * to have access to a correct value, just like what
  431. * net/dsa/dsa.c::dsa_switch_setup_one does.
  432. */
  433. if (!dsa_port_is_cpu(port))
  434. ds->enabled_port_mask |= 1 << reg;
  435. }
  436. return 0;
  437. }
  438. static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
  439. {
  440. int err;
  441. *tree = *index = 0;
  442. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  443. if (err) {
  444. /* Does not exist, but it is optional */
  445. if (err == -EINVAL)
  446. return 0;
  447. return err;
  448. }
  449. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  450. if (err)
  451. return err;
  452. if (*index >= DSA_MAX_SWITCHES)
  453. return -EINVAL;
  454. return 0;
  455. }
  456. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  457. struct device_node *np)
  458. {
  459. struct device_node *ports;
  460. ports = of_get_child_by_name(np, "ports");
  461. if (!ports) {
  462. dev_err(ds->dev, "no ports child node found\n");
  463. return ERR_PTR(-EINVAL);
  464. }
  465. return ports;
  466. }
  467. static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  468. {
  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(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. goto out_del_dst;
  512. err = dsa_dst_apply(dst);
  513. if (err) {
  514. dsa_dst_unapply(dst);
  515. goto out_del_dst;
  516. }
  517. dsa_put_dst(dst);
  518. return 0;
  519. out_del_dst:
  520. dsa_dst_del_ds(dst, ds, ds->index);
  521. out:
  522. dsa_put_dst(dst);
  523. return err;
  524. }
  525. int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
  526. {
  527. int err;
  528. mutex_lock(&dsa2_mutex);
  529. err = _dsa_register_switch(ds, np);
  530. mutex_unlock(&dsa2_mutex);
  531. return err;
  532. }
  533. EXPORT_SYMBOL_GPL(dsa_register_switch);
  534. static void _dsa_unregister_switch(struct dsa_switch *ds)
  535. {
  536. struct dsa_switch_tree *dst = ds->dst;
  537. dsa_dst_unapply(dst);
  538. dsa_dst_del_ds(dst, ds, ds->index);
  539. }
  540. void dsa_unregister_switch(struct dsa_switch *ds)
  541. {
  542. mutex_lock(&dsa2_mutex);
  543. _dsa_unregister_switch(ds);
  544. mutex_unlock(&dsa2_mutex);
  545. }
  546. EXPORT_SYMBOL_GPL(dsa_unregister_switch);