dsa2.c 16 KB

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