dsa2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 < ds->num_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 < ds->num_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. err = dsa_switch_register_notifier(ds);
  242. if (err)
  243. return err;
  244. if (ds->ops->set_addr) {
  245. err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
  246. if (err < 0)
  247. return err;
  248. }
  249. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  250. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  251. if (!ds->slave_mii_bus)
  252. return -ENOMEM;
  253. dsa_slave_mii_bus_init(ds);
  254. err = mdiobus_register(ds->slave_mii_bus);
  255. if (err < 0)
  256. return err;
  257. }
  258. for (index = 0; index < ds->num_ports; index++) {
  259. port = &ds->ports[index];
  260. if (!dsa_port_is_valid(port))
  261. continue;
  262. if (dsa_port_is_dsa(port)) {
  263. err = dsa_dsa_port_apply(port, index, ds);
  264. if (err)
  265. return err;
  266. continue;
  267. }
  268. if (dsa_port_is_cpu(port)) {
  269. err = dsa_cpu_port_apply(port, index, ds);
  270. if (err)
  271. return err;
  272. continue;
  273. }
  274. err = dsa_user_port_apply(port, index, ds);
  275. if (err)
  276. continue;
  277. }
  278. return 0;
  279. }
  280. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  281. {
  282. struct dsa_port *port;
  283. u32 index;
  284. for (index = 0; index < ds->num_ports; index++) {
  285. port = &ds->ports[index];
  286. if (!dsa_port_is_valid(port))
  287. continue;
  288. if (dsa_port_is_dsa(port)) {
  289. dsa_dsa_port_unapply(port, index, ds);
  290. continue;
  291. }
  292. if (dsa_port_is_cpu(port)) {
  293. dsa_cpu_port_unapply(port, index, ds);
  294. continue;
  295. }
  296. dsa_user_port_unapply(port, index, ds);
  297. }
  298. if (ds->slave_mii_bus && ds->ops->phy_read)
  299. mdiobus_unregister(ds->slave_mii_bus);
  300. dsa_switch_unregister_notifier(ds);
  301. }
  302. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  303. {
  304. struct dsa_switch *ds;
  305. u32 index;
  306. int err;
  307. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  308. ds = dst->ds[index];
  309. if (!ds)
  310. continue;
  311. err = dsa_ds_apply(dst, ds);
  312. if (err)
  313. return err;
  314. }
  315. if (dst->cpu_switch) {
  316. err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
  317. if (err)
  318. return err;
  319. }
  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. if (dst->cpu_switch)
  348. dsa_cpu_port_ethtool_restore(dst->cpu_switch);
  349. pr_info("DSA: tree %d unapplied\n", dst->tree);
  350. dst->applied = false;
  351. }
  352. static int dsa_cpu_parse(struct dsa_port *port, u32 index,
  353. struct dsa_switch_tree *dst,
  354. struct dsa_switch *ds)
  355. {
  356. enum dsa_tag_protocol tag_protocol;
  357. struct net_device *ethernet_dev;
  358. struct device_node *ethernet;
  359. ethernet = of_parse_phandle(port->dn, "ethernet", 0);
  360. if (!ethernet)
  361. return -EINVAL;
  362. ethernet_dev = of_find_net_device_by_node(ethernet);
  363. if (!ethernet_dev)
  364. return -EPROBE_DEFER;
  365. if (!ds->master_netdev)
  366. ds->master_netdev = ethernet_dev;
  367. if (!dst->master_netdev)
  368. dst->master_netdev = ethernet_dev;
  369. if (!dst->cpu_switch) {
  370. dst->cpu_switch = ds;
  371. dst->cpu_port = index;
  372. }
  373. tag_protocol = ds->ops->get_tag_protocol(ds);
  374. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  375. if (IS_ERR(dst->tag_ops)) {
  376. dev_warn(ds->dev, "No tagger for this switch\n");
  377. return PTR_ERR(dst->tag_ops);
  378. }
  379. dst->rcv = dst->tag_ops->rcv;
  380. return 0;
  381. }
  382. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  383. {
  384. struct dsa_port *port;
  385. u32 index;
  386. int err;
  387. for (index = 0; index < ds->num_ports; index++) {
  388. port = &ds->ports[index];
  389. if (!dsa_port_is_valid(port))
  390. continue;
  391. if (dsa_port_is_cpu(port)) {
  392. err = dsa_cpu_parse(port, index, dst, ds);
  393. if (err)
  394. return err;
  395. }
  396. }
  397. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  398. return 0;
  399. }
  400. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  401. {
  402. struct dsa_switch *ds;
  403. u32 index;
  404. int err;
  405. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  406. ds = dst->ds[index];
  407. if (!ds)
  408. continue;
  409. err = dsa_ds_parse(dst, ds);
  410. if (err)
  411. return err;
  412. }
  413. if (!dst->master_netdev) {
  414. pr_warn("Tree has no master device\n");
  415. return -EINVAL;
  416. }
  417. pr_info("DSA: tree %d parsed\n", dst->tree);
  418. return 0;
  419. }
  420. static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
  421. {
  422. struct device_node *port;
  423. int err;
  424. u32 reg;
  425. for_each_available_child_of_node(ports, port) {
  426. err = of_property_read_u32(port, "reg", &reg);
  427. if (err)
  428. return err;
  429. if (reg >= ds->num_ports)
  430. return -EINVAL;
  431. ds->ports[reg].dn = port;
  432. /* Initialize enabled_port_mask now for ops->setup()
  433. * to have access to a correct value, just like what
  434. * net/dsa/dsa.c::dsa_switch_setup_one does.
  435. */
  436. if (!dsa_port_is_cpu(&ds->ports[reg]))
  437. ds->enabled_port_mask |= 1 << reg;
  438. }
  439. return 0;
  440. }
  441. static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
  442. {
  443. int err;
  444. *tree = *index = 0;
  445. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  446. if (err) {
  447. /* Does not exist, but it is optional */
  448. if (err == -EINVAL)
  449. return 0;
  450. return err;
  451. }
  452. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  453. if (err)
  454. return err;
  455. if (*index >= DSA_MAX_SWITCHES)
  456. return -EINVAL;
  457. return 0;
  458. }
  459. static struct device_node *dsa_get_ports(struct dsa_switch *ds,
  460. struct device_node *np)
  461. {
  462. struct device_node *ports;
  463. ports = of_get_child_by_name(np, "ports");
  464. if (!ports) {
  465. dev_err(ds->dev, "no ports child node found\n");
  466. return ERR_PTR(-EINVAL);
  467. }
  468. return ports;
  469. }
  470. static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
  471. {
  472. struct device_node *np = dev->of_node;
  473. struct dsa_switch_tree *dst;
  474. struct device_node *ports;
  475. u32 tree, index;
  476. int i, err;
  477. err = dsa_parse_member_dn(np, &tree, &index);
  478. if (err)
  479. return err;
  480. ports = dsa_get_ports(ds, np);
  481. if (IS_ERR(ports))
  482. return PTR_ERR(ports);
  483. err = dsa_parse_ports_dn(ports, ds);
  484. if (err)
  485. return err;
  486. dst = dsa_get_dst(tree);
  487. if (!dst) {
  488. dst = dsa_add_dst(tree);
  489. if (!dst)
  490. return -ENOMEM;
  491. }
  492. if (dst->ds[index]) {
  493. err = -EBUSY;
  494. goto out;
  495. }
  496. ds->dst = dst;
  497. ds->index = index;
  498. /* Initialize the routing table */
  499. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  500. ds->rtable[i] = DSA_RTABLE_NONE;
  501. dsa_dst_add_ds(dst, ds, index);
  502. err = dsa_dst_complete(dst);
  503. if (err < 0)
  504. goto out_del_dst;
  505. if (err == 1) {
  506. /* Not all switches registered yet */
  507. err = 0;
  508. goto out;
  509. }
  510. if (dst->applied) {
  511. pr_info("DSA: Disjoint trees?\n");
  512. return -EINVAL;
  513. }
  514. err = dsa_dst_parse(dst);
  515. if (err) {
  516. if (err == -EPROBE_DEFER) {
  517. dsa_dst_del_ds(dst, ds, ds->index);
  518. return err;
  519. }
  520. goto out_del_dst;
  521. }
  522. err = dsa_dst_apply(dst);
  523. if (err) {
  524. dsa_dst_unapply(dst);
  525. goto out_del_dst;
  526. }
  527. dsa_put_dst(dst);
  528. return 0;
  529. out_del_dst:
  530. dsa_dst_del_ds(dst, ds, ds->index);
  531. out:
  532. dsa_put_dst(dst);
  533. return err;
  534. }
  535. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
  536. {
  537. size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
  538. struct dsa_switch *ds;
  539. int i;
  540. ds = devm_kzalloc(dev, size, GFP_KERNEL);
  541. if (!ds)
  542. return NULL;
  543. ds->dev = dev;
  544. ds->num_ports = n;
  545. for (i = 0; i < ds->num_ports; ++i) {
  546. ds->ports[i].index = i;
  547. ds->ports[i].ds = ds;
  548. }
  549. return ds;
  550. }
  551. EXPORT_SYMBOL_GPL(dsa_switch_alloc);
  552. int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
  553. {
  554. int err;
  555. mutex_lock(&dsa2_mutex);
  556. err = _dsa_register_switch(ds, dev);
  557. mutex_unlock(&dsa2_mutex);
  558. return err;
  559. }
  560. EXPORT_SYMBOL_GPL(dsa_register_switch);
  561. static void _dsa_unregister_switch(struct dsa_switch *ds)
  562. {
  563. struct dsa_switch_tree *dst = ds->dst;
  564. dsa_dst_unapply(dst);
  565. dsa_dst_del_ds(dst, ds, ds->index);
  566. }
  567. void dsa_unregister_switch(struct dsa_switch *ds)
  568. {
  569. mutex_lock(&dsa2_mutex);
  570. _dsa_unregister_switch(ds);
  571. mutex_unlock(&dsa2_mutex);
  572. }
  573. EXPORT_SYMBOL_GPL(dsa_unregister_switch);