dsa2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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/netdevice.h>
  16. #include <linux/slab.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include "dsa_priv.h"
  21. static LIST_HEAD(dsa_tree_list);
  22. static DEFINE_MUTEX(dsa2_mutex);
  23. static const struct devlink_ops dsa_devlink_ops = {
  24. };
  25. static struct dsa_switch_tree *dsa_tree_find(int index)
  26. {
  27. struct dsa_switch_tree *dst;
  28. list_for_each_entry(dst, &dsa_tree_list, list)
  29. if (dst->index == index)
  30. return dst;
  31. return NULL;
  32. }
  33. static struct dsa_switch_tree *dsa_tree_alloc(int index)
  34. {
  35. struct dsa_switch_tree *dst;
  36. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  37. if (!dst)
  38. return NULL;
  39. dst->index = index;
  40. INIT_LIST_HEAD(&dst->list);
  41. list_add_tail(&dsa_tree_list, &dst->list);
  42. /* Initialize the reference counter to the number of switches, not 1 */
  43. kref_init(&dst->refcount);
  44. refcount_set(&dst->refcount.refcount, 0);
  45. return dst;
  46. }
  47. static void dsa_tree_free(struct dsa_switch_tree *dst)
  48. {
  49. list_del(&dst->list);
  50. kfree(dst);
  51. }
  52. static struct dsa_switch_tree *dsa_tree_touch(int index)
  53. {
  54. struct dsa_switch_tree *dst;
  55. dst = dsa_tree_find(index);
  56. if (!dst)
  57. dst = dsa_tree_alloc(index);
  58. return dst;
  59. }
  60. static void dsa_tree_get(struct dsa_switch_tree *dst)
  61. {
  62. kref_get(&dst->refcount);
  63. }
  64. static void dsa_tree_release(struct kref *ref)
  65. {
  66. struct dsa_switch_tree *dst;
  67. dst = container_of(ref, struct dsa_switch_tree, refcount);
  68. dsa_tree_free(dst);
  69. }
  70. static void dsa_tree_put(struct dsa_switch_tree *dst)
  71. {
  72. kref_put(&dst->refcount, dsa_tree_release);
  73. }
  74. static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
  75. struct dsa_switch *ds, u32 index)
  76. {
  77. dsa_tree_get(dst);
  78. dst->ds[index] = ds;
  79. }
  80. static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
  81. struct dsa_switch *ds, u32 index)
  82. {
  83. dst->ds[index] = NULL;
  84. dsa_tree_put(dst);
  85. }
  86. /* For platform data configurations, we need to have a valid name argument to
  87. * differentiate a disabled port from an enabled one
  88. */
  89. static bool dsa_port_is_valid(struct dsa_port *port)
  90. {
  91. return port->type != DSA_PORT_TYPE_UNUSED;
  92. }
  93. static bool dsa_port_is_dsa(struct dsa_port *port)
  94. {
  95. return port->type == DSA_PORT_TYPE_DSA;
  96. }
  97. static bool dsa_port_is_cpu(struct dsa_port *port)
  98. {
  99. return port->type == DSA_PORT_TYPE_CPU;
  100. }
  101. static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
  102. struct device_node *port)
  103. {
  104. u32 index;
  105. for (index = 0; index < ds->num_ports; index++)
  106. if (ds->ports[index].dn == port)
  107. return true;
  108. return false;
  109. }
  110. static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
  111. struct device_node *port)
  112. {
  113. struct dsa_switch *ds;
  114. u32 index;
  115. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  116. ds = dst->ds[index];
  117. if (!ds)
  118. continue;
  119. if (dsa_ds_find_port_dn(ds, port))
  120. return ds;
  121. }
  122. return NULL;
  123. }
  124. static int dsa_port_complete(struct dsa_switch_tree *dst,
  125. struct dsa_switch *src_ds,
  126. struct dsa_port *port,
  127. u32 src_port)
  128. {
  129. struct device_node *link;
  130. int index;
  131. struct dsa_switch *dst_ds;
  132. for (index = 0;; index++) {
  133. link = of_parse_phandle(port->dn, "link", index);
  134. if (!link)
  135. break;
  136. dst_ds = dsa_dst_find_port_dn(dst, link);
  137. of_node_put(link);
  138. if (!dst_ds)
  139. return 1;
  140. src_ds->rtable[dst_ds->index] = src_port;
  141. }
  142. return 0;
  143. }
  144. /* A switch is complete if all the DSA ports phandles point to ports
  145. * known in the tree. A return value of 1 means the tree is not
  146. * complete. This is not an error condition. A value of 0 is
  147. * success.
  148. */
  149. static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  150. {
  151. struct dsa_port *port;
  152. u32 index;
  153. int err;
  154. for (index = 0; index < ds->num_ports; index++) {
  155. port = &ds->ports[index];
  156. if (!dsa_port_is_valid(port))
  157. continue;
  158. if (!dsa_port_is_dsa(port))
  159. continue;
  160. err = dsa_port_complete(dst, ds, port, index);
  161. if (err != 0)
  162. return err;
  163. }
  164. return 0;
  165. }
  166. /* A tree is complete if all the DSA ports phandles point to ports
  167. * known in the tree. A return value of 1 means the tree is not
  168. * complete. This is not an error condition. A value of 0 is
  169. * success.
  170. */
  171. static int dsa_dst_complete(struct dsa_switch_tree *dst)
  172. {
  173. struct dsa_switch *ds;
  174. u32 index;
  175. int err;
  176. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  177. ds = dst->ds[index];
  178. if (!ds)
  179. continue;
  180. err = dsa_ds_complete(dst, ds);
  181. if (err != 0)
  182. return err;
  183. }
  184. return 0;
  185. }
  186. static int dsa_dsa_port_apply(struct dsa_port *port)
  187. {
  188. struct dsa_switch *ds = port->ds;
  189. int err;
  190. err = dsa_port_fixed_link_register_of(port);
  191. if (err) {
  192. dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
  193. port->index, err);
  194. return err;
  195. }
  196. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  197. return devlink_port_register(ds->devlink, &port->devlink_port,
  198. port->index);
  199. }
  200. static void dsa_dsa_port_unapply(struct dsa_port *port)
  201. {
  202. devlink_port_unregister(&port->devlink_port);
  203. dsa_port_fixed_link_unregister_of(port);
  204. }
  205. static int dsa_cpu_port_apply(struct dsa_port *port)
  206. {
  207. struct dsa_switch *ds = port->ds;
  208. int err;
  209. err = dsa_port_fixed_link_register_of(port);
  210. if (err) {
  211. dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
  212. port->index, err);
  213. return err;
  214. }
  215. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  216. err = devlink_port_register(ds->devlink, &port->devlink_port,
  217. port->index);
  218. return err;
  219. }
  220. static void dsa_cpu_port_unapply(struct dsa_port *port)
  221. {
  222. devlink_port_unregister(&port->devlink_port);
  223. dsa_port_fixed_link_unregister_of(port);
  224. }
  225. static int dsa_user_port_apply(struct dsa_port *port)
  226. {
  227. struct dsa_switch *ds = port->ds;
  228. int err;
  229. err = dsa_slave_create(port);
  230. if (err) {
  231. dev_warn(ds->dev, "Failed to create slave %d: %d\n",
  232. port->index, err);
  233. port->slave = NULL;
  234. return err;
  235. }
  236. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  237. err = devlink_port_register(ds->devlink, &port->devlink_port,
  238. port->index);
  239. if (err)
  240. return err;
  241. devlink_port_type_eth_set(&port->devlink_port, port->slave);
  242. return 0;
  243. }
  244. static void dsa_user_port_unapply(struct dsa_port *port)
  245. {
  246. devlink_port_unregister(&port->devlink_port);
  247. if (port->slave) {
  248. dsa_slave_destroy(port->slave);
  249. port->slave = NULL;
  250. }
  251. }
  252. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  253. {
  254. struct dsa_port *port;
  255. u32 index;
  256. int err;
  257. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  258. * driver and before ops->setup() has run, since the switch drivers and
  259. * the slave MDIO bus driver rely on these values for probing PHY
  260. * devices or not
  261. */
  262. ds->phys_mii_mask |= dsa_user_ports(ds);
  263. /* Add the switch to devlink before calling setup, so that setup can
  264. * add dpipe tables
  265. */
  266. ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
  267. if (!ds->devlink)
  268. return -ENOMEM;
  269. err = devlink_register(ds->devlink, ds->dev);
  270. if (err)
  271. return err;
  272. err = ds->ops->setup(ds);
  273. if (err < 0)
  274. return err;
  275. err = dsa_switch_register_notifier(ds);
  276. if (err)
  277. return err;
  278. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  279. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  280. if (!ds->slave_mii_bus)
  281. return -ENOMEM;
  282. dsa_slave_mii_bus_init(ds);
  283. err = mdiobus_register(ds->slave_mii_bus);
  284. if (err < 0)
  285. return err;
  286. }
  287. for (index = 0; index < ds->num_ports; index++) {
  288. port = &ds->ports[index];
  289. if (!dsa_port_is_valid(port))
  290. continue;
  291. if (dsa_port_is_dsa(port)) {
  292. err = dsa_dsa_port_apply(port);
  293. if (err)
  294. return err;
  295. continue;
  296. }
  297. if (dsa_port_is_cpu(port)) {
  298. err = dsa_cpu_port_apply(port);
  299. if (err)
  300. return err;
  301. continue;
  302. }
  303. err = dsa_user_port_apply(port);
  304. if (err)
  305. continue;
  306. }
  307. return 0;
  308. }
  309. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  310. {
  311. struct dsa_port *port;
  312. u32 index;
  313. for (index = 0; index < ds->num_ports; index++) {
  314. port = &ds->ports[index];
  315. if (!dsa_port_is_valid(port))
  316. continue;
  317. if (dsa_port_is_dsa(port)) {
  318. dsa_dsa_port_unapply(port);
  319. continue;
  320. }
  321. if (dsa_port_is_cpu(port)) {
  322. dsa_cpu_port_unapply(port);
  323. continue;
  324. }
  325. dsa_user_port_unapply(port);
  326. }
  327. if (ds->slave_mii_bus && ds->ops->phy_read)
  328. mdiobus_unregister(ds->slave_mii_bus);
  329. dsa_switch_unregister_notifier(ds);
  330. if (ds->devlink) {
  331. devlink_unregister(ds->devlink);
  332. devlink_free(ds->devlink);
  333. ds->devlink = NULL;
  334. }
  335. }
  336. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  337. {
  338. struct dsa_switch *ds;
  339. u32 index;
  340. int err;
  341. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  342. ds = dst->ds[index];
  343. if (!ds)
  344. continue;
  345. err = dsa_ds_apply(dst, ds);
  346. if (err)
  347. return err;
  348. }
  349. /* If we use a tagging format that doesn't have an ethertype
  350. * field, make sure that all packets from this point on get
  351. * sent to the tag format's receive function.
  352. */
  353. wmb();
  354. dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
  355. err = dsa_master_ethtool_setup(dst->cpu_dp->master);
  356. if (err)
  357. return err;
  358. dst->applied = true;
  359. return 0;
  360. }
  361. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  362. {
  363. struct dsa_switch *ds;
  364. u32 index;
  365. if (!dst->applied)
  366. return;
  367. dsa_master_ethtool_restore(dst->cpu_dp->master);
  368. dst->cpu_dp->master->dsa_ptr = NULL;
  369. /* If we used a tagging format that doesn't have an ethertype
  370. * field, make sure that all packets from this point get sent
  371. * without the tag and go through the regular receive path.
  372. */
  373. wmb();
  374. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  375. ds = dst->ds[index];
  376. if (!ds)
  377. continue;
  378. dsa_ds_unapply(dst, ds);
  379. }
  380. dst->cpu_dp = NULL;
  381. pr_info("DSA: tree %d unapplied\n", dst->index);
  382. dst->applied = false;
  383. }
  384. static int dsa_cpu_parse(struct dsa_port *port, u32 index,
  385. struct dsa_switch_tree *dst,
  386. struct dsa_switch *ds)
  387. {
  388. const struct dsa_device_ops *tag_ops;
  389. enum dsa_tag_protocol tag_protocol;
  390. if (!dst->cpu_dp)
  391. dst->cpu_dp = port;
  392. tag_protocol = ds->ops->get_tag_protocol(ds);
  393. tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  394. if (IS_ERR(tag_ops)) {
  395. dev_warn(ds->dev, "No tagger for this switch\n");
  396. return PTR_ERR(tag_ops);
  397. }
  398. dst->cpu_dp->tag_ops = tag_ops;
  399. /* Make a few copies for faster access in master receive hot path */
  400. dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
  401. dst->cpu_dp->dst = dst;
  402. return 0;
  403. }
  404. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  405. {
  406. struct dsa_port *port;
  407. u32 index;
  408. int err;
  409. for (index = 0; index < ds->num_ports; index++) {
  410. port = &ds->ports[index];
  411. if (!dsa_port_is_valid(port) ||
  412. dsa_port_is_dsa(port))
  413. continue;
  414. if (dsa_port_is_cpu(port)) {
  415. err = dsa_cpu_parse(port, index, dst, ds);
  416. if (err)
  417. return err;
  418. }
  419. }
  420. pr_info("DSA: switch %d %d parsed\n", dst->index, ds->index);
  421. return 0;
  422. }
  423. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  424. {
  425. struct dsa_switch *ds;
  426. struct dsa_port *dp;
  427. u32 index;
  428. int port;
  429. int err;
  430. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  431. ds = dst->ds[index];
  432. if (!ds)
  433. continue;
  434. err = dsa_ds_parse(dst, ds);
  435. if (err)
  436. return err;
  437. }
  438. if (!dst->cpu_dp) {
  439. pr_warn("Tree has no master device\n");
  440. return -EINVAL;
  441. }
  442. /* Assign the default CPU port to all ports of the fabric */
  443. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  444. ds = dst->ds[index];
  445. if (!ds)
  446. continue;
  447. for (port = 0; port < ds->num_ports; port++) {
  448. dp = &ds->ports[port];
  449. if (!dsa_port_is_valid(dp) ||
  450. dsa_port_is_dsa(dp) ||
  451. dsa_port_is_cpu(dp))
  452. continue;
  453. dp->cpu_dp = dst->cpu_dp;
  454. }
  455. }
  456. pr_info("DSA: tree %d parsed\n", dst->index);
  457. return 0;
  458. }
  459. static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
  460. {
  461. struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
  462. struct device_node *link = of_parse_phandle(dn, "link", 0);
  463. const char *name = of_get_property(dn, "label", NULL);
  464. if (ethernet) {
  465. struct net_device *master;
  466. master = of_find_net_device_by_node(ethernet);
  467. if (!master)
  468. return -EPROBE_DEFER;
  469. dp->type = DSA_PORT_TYPE_CPU;
  470. dp->master = master;
  471. } else if (link) {
  472. dp->type = DSA_PORT_TYPE_DSA;
  473. } else {
  474. if (!name)
  475. name = "eth%d";
  476. dp->type = DSA_PORT_TYPE_USER;
  477. dp->name = name;
  478. }
  479. dp->dn = dn;
  480. return 0;
  481. }
  482. static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
  483. {
  484. struct device_node *ports, *port;
  485. struct dsa_port *dp;
  486. u32 reg;
  487. int err;
  488. ports = of_get_child_by_name(dn, "ports");
  489. if (!ports) {
  490. dev_err(ds->dev, "no ports child node found\n");
  491. return -EINVAL;
  492. }
  493. for_each_available_child_of_node(ports, port) {
  494. err = of_property_read_u32(port, "reg", &reg);
  495. if (err)
  496. return err;
  497. if (reg >= ds->num_ports)
  498. return -EINVAL;
  499. dp = &ds->ports[reg];
  500. err = dsa_port_parse_of(dp, port);
  501. if (err)
  502. return err;
  503. }
  504. return 0;
  505. }
  506. static int dsa_port_parse(struct dsa_port *dp, const char *name,
  507. struct device *dev)
  508. {
  509. if (!strcmp(name, "cpu")) {
  510. struct net_device *master;
  511. master = dsa_dev_to_net_device(dev);
  512. if (!master)
  513. return -EPROBE_DEFER;
  514. dev_put(master);
  515. dp->type = DSA_PORT_TYPE_CPU;
  516. dp->master = master;
  517. } else if (!strcmp(name, "dsa")) {
  518. dp->type = DSA_PORT_TYPE_DSA;
  519. } else {
  520. dp->type = DSA_PORT_TYPE_USER;
  521. }
  522. dp->name = name;
  523. return 0;
  524. }
  525. static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
  526. {
  527. bool valid_name_found = false;
  528. struct dsa_port *dp;
  529. struct device *dev;
  530. const char *name;
  531. unsigned int i;
  532. int err;
  533. for (i = 0; i < DSA_MAX_PORTS; i++) {
  534. name = cd->port_names[i];
  535. dev = cd->netdev[i];
  536. dp = &ds->ports[i];
  537. if (!name)
  538. continue;
  539. err = dsa_port_parse(dp, name, dev);
  540. if (err)
  541. return err;
  542. valid_name_found = true;
  543. }
  544. if (!valid_name_found && i == DSA_MAX_PORTS)
  545. return -EINVAL;
  546. return 0;
  547. }
  548. static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
  549. {
  550. int err;
  551. *tree = *index = 0;
  552. err = of_property_read_u32_index(np, "dsa,member", 0, tree);
  553. if (err) {
  554. /* Does not exist, but it is optional */
  555. if (err == -EINVAL)
  556. return 0;
  557. return err;
  558. }
  559. err = of_property_read_u32_index(np, "dsa,member", 1, index);
  560. if (err)
  561. return err;
  562. if (*index >= DSA_MAX_SWITCHES)
  563. return -EINVAL;
  564. return 0;
  565. }
  566. static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
  567. {
  568. if (!pd)
  569. return -ENODEV;
  570. /* We do not support complex trees with dsa_chip_data */
  571. *tree = 0;
  572. *index = 0;
  573. return 0;
  574. }
  575. static int _dsa_register_switch(struct dsa_switch *ds)
  576. {
  577. struct dsa_chip_data *pdata = ds->dev->platform_data;
  578. struct device_node *np = ds->dev->of_node;
  579. struct dsa_switch_tree *dst;
  580. u32 tree, index;
  581. int i, err;
  582. if (np) {
  583. err = dsa_parse_member_dn(np, &tree, &index);
  584. if (err)
  585. return err;
  586. err = dsa_parse_ports_of(np, ds);
  587. if (err)
  588. return err;
  589. } else {
  590. err = dsa_parse_member(pdata, &tree, &index);
  591. if (err)
  592. return err;
  593. err = dsa_parse_ports(pdata, ds);
  594. if (err)
  595. return err;
  596. }
  597. dst = dsa_tree_touch(tree);
  598. if (!dst)
  599. return -ENOMEM;
  600. if (dst->ds[index])
  601. return -EBUSY;
  602. ds->dst = dst;
  603. ds->index = index;
  604. ds->cd = pdata;
  605. /* Initialize the routing table */
  606. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  607. ds->rtable[i] = DSA_RTABLE_NONE;
  608. dsa_dst_add_ds(dst, ds, index);
  609. err = dsa_dst_complete(dst);
  610. if (err < 0)
  611. goto out_del_dst;
  612. /* Not all switches registered yet */
  613. if (err == 1)
  614. return 0;
  615. if (dst->applied) {
  616. pr_info("DSA: Disjoint trees?\n");
  617. return -EINVAL;
  618. }
  619. err = dsa_dst_parse(dst);
  620. if (err)
  621. goto out_del_dst;
  622. err = dsa_dst_apply(dst);
  623. if (err) {
  624. dsa_dst_unapply(dst);
  625. goto out_del_dst;
  626. }
  627. return 0;
  628. out_del_dst:
  629. dsa_dst_del_ds(dst, ds, ds->index);
  630. return err;
  631. }
  632. struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
  633. {
  634. size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
  635. struct dsa_switch *ds;
  636. int i;
  637. ds = devm_kzalloc(dev, size, GFP_KERNEL);
  638. if (!ds)
  639. return NULL;
  640. ds->dev = dev;
  641. ds->num_ports = n;
  642. for (i = 0; i < ds->num_ports; ++i) {
  643. ds->ports[i].index = i;
  644. ds->ports[i].ds = ds;
  645. }
  646. return ds;
  647. }
  648. EXPORT_SYMBOL_GPL(dsa_switch_alloc);
  649. int dsa_register_switch(struct dsa_switch *ds)
  650. {
  651. int err;
  652. mutex_lock(&dsa2_mutex);
  653. err = _dsa_register_switch(ds);
  654. mutex_unlock(&dsa2_mutex);
  655. return err;
  656. }
  657. EXPORT_SYMBOL_GPL(dsa_register_switch);
  658. static void _dsa_unregister_switch(struct dsa_switch *ds)
  659. {
  660. struct dsa_switch_tree *dst = ds->dst;
  661. dsa_dst_unapply(dst);
  662. dsa_dst_del_ds(dst, ds, ds->index);
  663. }
  664. void dsa_unregister_switch(struct dsa_switch *ds)
  665. {
  666. mutex_lock(&dsa2_mutex);
  667. _dsa_unregister_switch(ds);
  668. mutex_unlock(&dsa2_mutex);
  669. }
  670. EXPORT_SYMBOL_GPL(dsa_unregister_switch);