dsa2.c 16 KB

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