dsa2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. port->type = DSA_PORT_TYPE_DSA;
  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)
  178. {
  179. struct dsa_switch *ds = port->ds;
  180. int err;
  181. err = dsa_port_fixed_link_register_of(port);
  182. if (err) {
  183. dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
  184. port->index, err);
  185. return err;
  186. }
  187. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  188. return devlink_port_register(ds->devlink, &port->devlink_port,
  189. port->index);
  190. }
  191. static void dsa_dsa_port_unapply(struct dsa_port *port)
  192. {
  193. devlink_port_unregister(&port->devlink_port);
  194. dsa_port_fixed_link_unregister_of(port);
  195. }
  196. static int dsa_cpu_port_apply(struct dsa_port *port)
  197. {
  198. struct dsa_switch *ds = port->ds;
  199. int err;
  200. err = dsa_port_fixed_link_register_of(port);
  201. if (err) {
  202. dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
  203. port->index, err);
  204. return err;
  205. }
  206. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  207. err = devlink_port_register(ds->devlink, &port->devlink_port,
  208. port->index);
  209. return err;
  210. }
  211. static void dsa_cpu_port_unapply(struct dsa_port *port)
  212. {
  213. devlink_port_unregister(&port->devlink_port);
  214. dsa_port_fixed_link_unregister_of(port);
  215. }
  216. static int dsa_user_port_apply(struct dsa_port *port)
  217. {
  218. struct dsa_switch *ds = port->ds;
  219. const char *name = port->name;
  220. int err;
  221. if (port->dn)
  222. name = of_get_property(port->dn, "label", NULL);
  223. if (!name)
  224. name = "eth%d";
  225. err = dsa_slave_create(port, name);
  226. if (err) {
  227. dev_warn(ds->dev, "Failed to create slave %d: %d\n",
  228. port->index, err);
  229. port->slave = NULL;
  230. return err;
  231. }
  232. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  233. err = devlink_port_register(ds->devlink, &port->devlink_port,
  234. port->index);
  235. if (err)
  236. return err;
  237. devlink_port_type_eth_set(&port->devlink_port, port->slave);
  238. return 0;
  239. }
  240. static void dsa_user_port_unapply(struct dsa_port *port)
  241. {
  242. devlink_port_unregister(&port->devlink_port);
  243. if (port->slave) {
  244. dsa_slave_destroy(port->slave);
  245. port->slave = NULL;
  246. }
  247. }
  248. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  249. {
  250. struct dsa_port *port;
  251. u32 index;
  252. int err;
  253. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  254. * driver and before ops->setup() has run, since the switch drivers and
  255. * the slave MDIO bus driver rely on these values for probing PHY
  256. * devices or not
  257. */
  258. ds->phys_mii_mask |= dsa_user_ports(ds);
  259. /* Add the switch to devlink before calling setup, so that setup can
  260. * add dpipe tables
  261. */
  262. ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
  263. if (!ds->devlink)
  264. return -ENOMEM;
  265. err = devlink_register(ds->devlink, ds->dev);
  266. if (err)
  267. return err;
  268. err = ds->ops->setup(ds);
  269. if (err < 0)
  270. return err;
  271. err = dsa_switch_register_notifier(ds);
  272. if (err)
  273. return err;
  274. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  275. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  276. if (!ds->slave_mii_bus)
  277. return -ENOMEM;
  278. dsa_slave_mii_bus_init(ds);
  279. err = mdiobus_register(ds->slave_mii_bus);
  280. if (err < 0)
  281. return err;
  282. }
  283. for (index = 0; index < ds->num_ports; index++) {
  284. port = &ds->ports[index];
  285. if (!dsa_port_is_valid(port))
  286. continue;
  287. if (dsa_port_is_dsa(port)) {
  288. err = dsa_dsa_port_apply(port);
  289. if (err)
  290. return err;
  291. continue;
  292. }
  293. if (dsa_port_is_cpu(port)) {
  294. err = dsa_cpu_port_apply(port);
  295. if (err)
  296. return err;
  297. continue;
  298. }
  299. err = dsa_user_port_apply(port);
  300. if (err)
  301. continue;
  302. }
  303. return 0;
  304. }
  305. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  306. {
  307. struct dsa_port *port;
  308. u32 index;
  309. for (index = 0; index < ds->num_ports; index++) {
  310. port = &ds->ports[index];
  311. if (!dsa_port_is_valid(port))
  312. continue;
  313. if (dsa_port_is_dsa(port)) {
  314. dsa_dsa_port_unapply(port);
  315. continue;
  316. }
  317. if (dsa_port_is_cpu(port)) {
  318. dsa_cpu_port_unapply(port);
  319. continue;
  320. }
  321. dsa_user_port_unapply(port);
  322. }
  323. if (ds->slave_mii_bus && ds->ops->phy_read)
  324. mdiobus_unregister(ds->slave_mii_bus);
  325. dsa_switch_unregister_notifier(ds);
  326. if (ds->devlink) {
  327. devlink_unregister(ds->devlink);
  328. devlink_free(ds->devlink);
  329. ds->devlink = NULL;
  330. }
  331. }
  332. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  333. {
  334. struct dsa_switch *ds;
  335. u32 index;
  336. int err;
  337. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  338. ds = dst->ds[index];
  339. if (!ds)
  340. continue;
  341. err = dsa_ds_apply(dst, ds);
  342. if (err)
  343. return err;
  344. }
  345. /* If we use a tagging format that doesn't have an ethertype
  346. * field, make sure that all packets from this point on get
  347. * sent to the tag format's receive function.
  348. */
  349. wmb();
  350. dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
  351. err = dsa_master_ethtool_setup(dst->cpu_dp->master);
  352. if (err)
  353. return err;
  354. dst->applied = true;
  355. return 0;
  356. }
  357. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  358. {
  359. struct dsa_switch *ds;
  360. u32 index;
  361. if (!dst->applied)
  362. return;
  363. dsa_master_ethtool_restore(dst->cpu_dp->master);
  364. dst->cpu_dp->master->dsa_ptr = NULL;
  365. /* If we used a tagging format that doesn't have an ethertype
  366. * field, make sure that all packets from this point get sent
  367. * without the tag and go through the regular receive path.
  368. */
  369. wmb();
  370. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  371. ds = dst->ds[index];
  372. if (!ds)
  373. continue;
  374. dsa_ds_unapply(dst, ds);
  375. }
  376. dst->cpu_dp = NULL;
  377. pr_info("DSA: tree %d unapplied\n", dst->tree);
  378. dst->applied = false;
  379. }
  380. static int dsa_cpu_parse(struct dsa_port *port, u32 index,
  381. struct dsa_switch_tree *dst,
  382. struct dsa_switch *ds)
  383. {
  384. const struct dsa_device_ops *tag_ops;
  385. enum dsa_tag_protocol tag_protocol;
  386. struct net_device *ethernet_dev;
  387. struct device_node *ethernet;
  388. if (port->dn) {
  389. ethernet = of_parse_phandle(port->dn, "ethernet", 0);
  390. if (!ethernet)
  391. return -EINVAL;
  392. ethernet_dev = of_find_net_device_by_node(ethernet);
  393. if (!ethernet_dev)
  394. return -EPROBE_DEFER;
  395. } else {
  396. ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
  397. if (!ethernet_dev)
  398. return -EPROBE_DEFER;
  399. dev_put(ethernet_dev);
  400. }
  401. if (!dst->cpu_dp) {
  402. dst->cpu_dp = port;
  403. dst->cpu_dp->master = ethernet_dev;
  404. }
  405. port->type = DSA_PORT_TYPE_CPU;
  406. tag_protocol = ds->ops->get_tag_protocol(ds);
  407. tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  408. if (IS_ERR(tag_ops)) {
  409. dev_warn(ds->dev, "No tagger for this switch\n");
  410. return PTR_ERR(tag_ops);
  411. }
  412. dst->cpu_dp->tag_ops = tag_ops;
  413. /* Make a few copies for faster access in master receive hot path */
  414. dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
  415. dst->cpu_dp->dst = dst;
  416. return 0;
  417. }
  418. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  419. {
  420. struct dsa_port *port;
  421. u32 index;
  422. int err;
  423. for (index = 0; index < ds->num_ports; index++) {
  424. port = &ds->ports[index];
  425. if (!dsa_port_is_valid(port) ||
  426. dsa_port_is_dsa(port))
  427. continue;
  428. if (dsa_port_is_cpu(port)) {
  429. err = dsa_cpu_parse(port, index, dst, ds);
  430. if (err)
  431. return err;
  432. } else {
  433. port->type = DSA_PORT_TYPE_USER;
  434. }
  435. }
  436. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  437. return 0;
  438. }
  439. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  440. {
  441. struct dsa_switch *ds;
  442. struct dsa_port *dp;
  443. u32 index;
  444. int port;
  445. int err;
  446. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  447. ds = dst->ds[index];
  448. if (!ds)
  449. continue;
  450. err = dsa_ds_parse(dst, ds);
  451. if (err)
  452. return err;
  453. }
  454. if (!dst->cpu_dp) {
  455. pr_warn("Tree has no master device\n");
  456. return -EINVAL;
  457. }
  458. /* Assign the default CPU port to all ports of the fabric */
  459. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  460. ds = dst->ds[index];
  461. if (!ds)
  462. continue;
  463. for (port = 0; port < ds->num_ports; port++) {
  464. dp = &ds->ports[port];
  465. if (!dsa_port_is_valid(dp) ||
  466. dsa_port_is_dsa(dp) ||
  467. dsa_port_is_cpu(dp))
  468. continue;
  469. dp->cpu_dp = dst->cpu_dp;
  470. }
  471. }
  472. pr_info("DSA: tree %d parsed\n", dst->tree);
  473. return 0;
  474. }
  475. static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
  476. {
  477. dp->dn = dn;
  478. return 0;
  479. }
  480. static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
  481. {
  482. struct device_node *ports, *port;
  483. struct dsa_port *dp;
  484. u32 reg;
  485. int err;
  486. ports = of_get_child_by_name(dn, "ports");
  487. if (!ports) {
  488. dev_err(ds->dev, "no ports child node found\n");
  489. return -EINVAL;
  490. }
  491. for_each_available_child_of_node(ports, port) {
  492. err = of_property_read_u32(port, "reg", &reg);
  493. if (err)
  494. return err;
  495. if (reg >= ds->num_ports)
  496. return -EINVAL;
  497. dp = &ds->ports[reg];
  498. err = dsa_port_parse_of(dp, port);
  499. if (err)
  500. return err;
  501. }
  502. return 0;
  503. }
  504. static int dsa_port_parse(struct dsa_port *dp, const char *name,
  505. struct device *dev)
  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. if (err == -EPROBE_DEFER) {
  614. dsa_dst_del_ds(dst, ds, ds->index);
  615. return err;
  616. }
  617. goto out_del_dst;
  618. }
  619. err = dsa_dst_apply(dst);
  620. if (err) {
  621. dsa_dst_unapply(dst);
  622. goto out_del_dst;
  623. }
  624. dsa_put_dst(dst);
  625. return 0;
  626. out_del_dst:
  627. dsa_dst_del_ds(dst, ds, ds->index);
  628. out:
  629. dsa_put_dst(dst);
  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);