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. err = dsa_slave_create(port, name);
  215. if (err) {
  216. dev_warn(ds->dev, "Failed to create slave %d: %d\n",
  217. port->index, err);
  218. port->slave = NULL;
  219. return err;
  220. }
  221. memset(&port->devlink_port, 0, sizeof(port->devlink_port));
  222. err = devlink_port_register(ds->devlink, &port->devlink_port,
  223. port->index);
  224. if (err)
  225. return err;
  226. devlink_port_type_eth_set(&port->devlink_port, port->slave);
  227. return 0;
  228. }
  229. static void dsa_user_port_unapply(struct dsa_port *port)
  230. {
  231. devlink_port_unregister(&port->devlink_port);
  232. if (port->slave) {
  233. dsa_slave_destroy(port->slave);
  234. port->slave = NULL;
  235. }
  236. }
  237. static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  238. {
  239. struct dsa_port *port;
  240. u32 index;
  241. int err;
  242. /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
  243. * driver and before ops->setup() has run, since the switch drivers and
  244. * the slave MDIO bus driver rely on these values for probing PHY
  245. * devices or not
  246. */
  247. ds->phys_mii_mask |= dsa_user_ports(ds);
  248. /* Add the switch to devlink before calling setup, so that setup can
  249. * add dpipe tables
  250. */
  251. ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
  252. if (!ds->devlink)
  253. return -ENOMEM;
  254. err = devlink_register(ds->devlink, ds->dev);
  255. if (err)
  256. return err;
  257. err = ds->ops->setup(ds);
  258. if (err < 0)
  259. return err;
  260. err = dsa_switch_register_notifier(ds);
  261. if (err)
  262. return err;
  263. if (!ds->slave_mii_bus && ds->ops->phy_read) {
  264. ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
  265. if (!ds->slave_mii_bus)
  266. return -ENOMEM;
  267. dsa_slave_mii_bus_init(ds);
  268. err = mdiobus_register(ds->slave_mii_bus);
  269. if (err < 0)
  270. return err;
  271. }
  272. for (index = 0; index < ds->num_ports; index++) {
  273. port = &ds->ports[index];
  274. if (!dsa_port_is_valid(port))
  275. continue;
  276. if (dsa_port_is_dsa(port)) {
  277. err = dsa_dsa_port_apply(port);
  278. if (err)
  279. return err;
  280. continue;
  281. }
  282. if (dsa_port_is_cpu(port)) {
  283. err = dsa_cpu_port_apply(port);
  284. if (err)
  285. return err;
  286. continue;
  287. }
  288. err = dsa_user_port_apply(port);
  289. if (err)
  290. continue;
  291. }
  292. return 0;
  293. }
  294. static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  295. {
  296. struct dsa_port *port;
  297. u32 index;
  298. for (index = 0; index < ds->num_ports; index++) {
  299. port = &ds->ports[index];
  300. if (!dsa_port_is_valid(port))
  301. continue;
  302. if (dsa_port_is_dsa(port)) {
  303. dsa_dsa_port_unapply(port);
  304. continue;
  305. }
  306. if (dsa_port_is_cpu(port)) {
  307. dsa_cpu_port_unapply(port);
  308. continue;
  309. }
  310. dsa_user_port_unapply(port);
  311. }
  312. if (ds->slave_mii_bus && ds->ops->phy_read)
  313. mdiobus_unregister(ds->slave_mii_bus);
  314. dsa_switch_unregister_notifier(ds);
  315. if (ds->devlink) {
  316. devlink_unregister(ds->devlink);
  317. devlink_free(ds->devlink);
  318. ds->devlink = NULL;
  319. }
  320. }
  321. static int dsa_dst_apply(struct dsa_switch_tree *dst)
  322. {
  323. struct dsa_switch *ds;
  324. u32 index;
  325. int err;
  326. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  327. ds = dst->ds[index];
  328. if (!ds)
  329. continue;
  330. err = dsa_ds_apply(dst, ds);
  331. if (err)
  332. return err;
  333. }
  334. /* If we use a tagging format that doesn't have an ethertype
  335. * field, make sure that all packets from this point on get
  336. * sent to the tag format's receive function.
  337. */
  338. wmb();
  339. dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
  340. err = dsa_master_ethtool_setup(dst->cpu_dp->master);
  341. if (err)
  342. return err;
  343. dst->applied = true;
  344. return 0;
  345. }
  346. static void dsa_dst_unapply(struct dsa_switch_tree *dst)
  347. {
  348. struct dsa_switch *ds;
  349. u32 index;
  350. if (!dst->applied)
  351. return;
  352. dsa_master_ethtool_restore(dst->cpu_dp->master);
  353. dst->cpu_dp->master->dsa_ptr = NULL;
  354. /* If we used a tagging format that doesn't have an ethertype
  355. * field, make sure that all packets from this point get sent
  356. * without the tag and go through the regular receive path.
  357. */
  358. wmb();
  359. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  360. ds = dst->ds[index];
  361. if (!ds)
  362. continue;
  363. dsa_ds_unapply(dst, ds);
  364. }
  365. dst->cpu_dp = NULL;
  366. pr_info("DSA: tree %d unapplied\n", dst->tree);
  367. dst->applied = false;
  368. }
  369. static int dsa_cpu_parse(struct dsa_port *port, u32 index,
  370. struct dsa_switch_tree *dst,
  371. struct dsa_switch *ds)
  372. {
  373. const struct dsa_device_ops *tag_ops;
  374. enum dsa_tag_protocol tag_protocol;
  375. if (!dst->cpu_dp)
  376. dst->cpu_dp = port;
  377. tag_protocol = ds->ops->get_tag_protocol(ds);
  378. tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  379. if (IS_ERR(tag_ops)) {
  380. dev_warn(ds->dev, "No tagger for this switch\n");
  381. return PTR_ERR(tag_ops);
  382. }
  383. dst->cpu_dp->tag_ops = tag_ops;
  384. /* Make a few copies for faster access in master receive hot path */
  385. dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
  386. dst->cpu_dp->dst = dst;
  387. return 0;
  388. }
  389. static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
  390. {
  391. struct dsa_port *port;
  392. u32 index;
  393. int err;
  394. for (index = 0; index < ds->num_ports; index++) {
  395. port = &ds->ports[index];
  396. if (!dsa_port_is_valid(port) ||
  397. dsa_port_is_dsa(port))
  398. continue;
  399. if (dsa_port_is_cpu(port)) {
  400. err = dsa_cpu_parse(port, index, dst, ds);
  401. if (err)
  402. return err;
  403. }
  404. }
  405. pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
  406. return 0;
  407. }
  408. static int dsa_dst_parse(struct dsa_switch_tree *dst)
  409. {
  410. struct dsa_switch *ds;
  411. struct dsa_port *dp;
  412. u32 index;
  413. int port;
  414. int err;
  415. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  416. ds = dst->ds[index];
  417. if (!ds)
  418. continue;
  419. err = dsa_ds_parse(dst, ds);
  420. if (err)
  421. return err;
  422. }
  423. if (!dst->cpu_dp) {
  424. pr_warn("Tree has no master device\n");
  425. return -EINVAL;
  426. }
  427. /* Assign the default CPU port to all ports of the fabric */
  428. for (index = 0; index < DSA_MAX_SWITCHES; index++) {
  429. ds = dst->ds[index];
  430. if (!ds)
  431. continue;
  432. for (port = 0; port < ds->num_ports; port++) {
  433. dp = &ds->ports[port];
  434. if (!dsa_port_is_valid(dp) ||
  435. dsa_port_is_dsa(dp) ||
  436. dsa_port_is_cpu(dp))
  437. continue;
  438. dp->cpu_dp = dst->cpu_dp;
  439. }
  440. }
  441. pr_info("DSA: tree %d parsed\n", dst->tree);
  442. return 0;
  443. }
  444. static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
  445. {
  446. struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
  447. struct device_node *link = of_parse_phandle(dn, "link", 0);
  448. const char *name = of_get_property(dn, "label", NULL);
  449. if (ethernet) {
  450. struct net_device *master;
  451. master = of_find_net_device_by_node(ethernet);
  452. if (!master)
  453. return -EPROBE_DEFER;
  454. dp->type = DSA_PORT_TYPE_CPU;
  455. dp->master = master;
  456. } else if (link) {
  457. dp->type = DSA_PORT_TYPE_DSA;
  458. } else {
  459. if (!name)
  460. name = "eth%d";
  461. dp->type = DSA_PORT_TYPE_USER;
  462. dp->name = name;
  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);