dsa2.c 17 KB

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