dsa2.c 17 KB

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