dsa2.c 17 KB

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