legacy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * net/dsa/legacy.c - Hardware switch handling
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/list.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_net.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/phy_fixed.h>
  23. #include <linux/etherdevice.h>
  24. #include "dsa_priv.h"
  25. /* switch driver registration ***********************************************/
  26. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  27. static LIST_HEAD(dsa_switch_drivers);
  28. void register_switch_driver(struct dsa_switch_driver *drv)
  29. {
  30. mutex_lock(&dsa_switch_drivers_mutex);
  31. list_add_tail(&drv->list, &dsa_switch_drivers);
  32. mutex_unlock(&dsa_switch_drivers_mutex);
  33. }
  34. EXPORT_SYMBOL_GPL(register_switch_driver);
  35. void unregister_switch_driver(struct dsa_switch_driver *drv)
  36. {
  37. mutex_lock(&dsa_switch_drivers_mutex);
  38. list_del_init(&drv->list);
  39. mutex_unlock(&dsa_switch_drivers_mutex);
  40. }
  41. EXPORT_SYMBOL_GPL(unregister_switch_driver);
  42. static const struct dsa_switch_ops *
  43. dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
  44. const char **_name, void **priv)
  45. {
  46. const struct dsa_switch_ops *ret;
  47. struct list_head *list;
  48. const char *name;
  49. ret = NULL;
  50. name = NULL;
  51. mutex_lock(&dsa_switch_drivers_mutex);
  52. list_for_each(list, &dsa_switch_drivers) {
  53. const struct dsa_switch_ops *ops;
  54. struct dsa_switch_driver *drv;
  55. drv = list_entry(list, struct dsa_switch_driver, list);
  56. ops = drv->ops;
  57. name = ops->probe(parent, host_dev, sw_addr, priv);
  58. if (name != NULL) {
  59. ret = ops;
  60. break;
  61. }
  62. }
  63. mutex_unlock(&dsa_switch_drivers_mutex);
  64. *_name = name;
  65. return ret;
  66. }
  67. /* basic switch operations **************************************************/
  68. static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
  69. {
  70. struct dsa_port *dport;
  71. int ret, port;
  72. for (port = 0; port < ds->num_ports; port++) {
  73. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  74. continue;
  75. dport = &ds->ports[port];
  76. ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
  77. if (ret)
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  83. {
  84. const struct dsa_switch_ops *ops = ds->ops;
  85. struct dsa_switch_tree *dst = ds->dst;
  86. struct dsa_chip_data *cd = ds->cd;
  87. bool valid_name_found = false;
  88. int index = ds->index;
  89. int i, ret;
  90. /*
  91. * Validate supplied switch configuration.
  92. */
  93. for (i = 0; i < ds->num_ports; i++) {
  94. char *name;
  95. name = cd->port_names[i];
  96. if (name == NULL)
  97. continue;
  98. if (!strcmp(name, "cpu")) {
  99. if (dst->cpu_dp) {
  100. netdev_err(dst->master_netdev,
  101. "multiple cpu ports?!\n");
  102. return -EINVAL;
  103. }
  104. dst->cpu_dp = &ds->ports[i];
  105. ds->cpu_port_mask |= 1 << i;
  106. } else if (!strcmp(name, "dsa")) {
  107. ds->dsa_port_mask |= 1 << i;
  108. } else {
  109. ds->enabled_port_mask |= 1 << i;
  110. }
  111. valid_name_found = true;
  112. }
  113. if (!valid_name_found && i == ds->num_ports)
  114. return -EINVAL;
  115. /* Make the built-in MII bus mask match the number of ports,
  116. * switch drivers can override this later
  117. */
  118. ds->phys_mii_mask = ds->enabled_port_mask;
  119. /*
  120. * If the CPU connects to this switch, set the switch tree
  121. * tagging protocol to the preferred tagging format of this
  122. * switch.
  123. */
  124. if (dst->cpu_dp->ds == ds) {
  125. enum dsa_tag_protocol tag_protocol;
  126. tag_protocol = ops->get_tag_protocol(ds);
  127. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  128. if (IS_ERR(dst->tag_ops))
  129. return PTR_ERR(dst->tag_ops);
  130. dst->rcv = dst->tag_ops->rcv;
  131. }
  132. memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
  133. /*
  134. * Do basic register setup.
  135. */
  136. ret = ops->setup(ds);
  137. if (ret < 0)
  138. return ret;
  139. ret = dsa_switch_register_notifier(ds);
  140. if (ret)
  141. return ret;
  142. if (ops->set_addr) {
  143. ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. if (!ds->slave_mii_bus && ops->phy_read) {
  148. ds->slave_mii_bus = devm_mdiobus_alloc(parent);
  149. if (!ds->slave_mii_bus)
  150. return -ENOMEM;
  151. dsa_slave_mii_bus_init(ds);
  152. ret = mdiobus_register(ds->slave_mii_bus);
  153. if (ret < 0)
  154. return ret;
  155. }
  156. /*
  157. * Create network devices for physical switch ports.
  158. */
  159. for (i = 0; i < ds->num_ports; i++) {
  160. ds->ports[i].dn = cd->port_dn[i];
  161. if (!(ds->enabled_port_mask & (1 << i)))
  162. continue;
  163. ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
  164. if (ret < 0)
  165. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
  166. index, i, cd->port_names[i], ret);
  167. }
  168. /* Perform configuration of the CPU and DSA ports */
  169. ret = dsa_cpu_dsa_setups(ds, parent);
  170. if (ret < 0)
  171. netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
  172. index);
  173. ret = dsa_cpu_port_ethtool_setup(ds);
  174. if (ret)
  175. return ret;
  176. return 0;
  177. }
  178. static struct dsa_switch *
  179. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  180. struct device *parent, struct device *host_dev)
  181. {
  182. struct dsa_chip_data *cd = dst->pd->chip + index;
  183. const struct dsa_switch_ops *ops;
  184. struct dsa_switch *ds;
  185. int ret;
  186. const char *name;
  187. void *priv;
  188. /*
  189. * Probe for switch model.
  190. */
  191. ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
  192. if (!ops) {
  193. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  194. index);
  195. return ERR_PTR(-EINVAL);
  196. }
  197. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  198. index, name);
  199. /*
  200. * Allocate and initialise switch state.
  201. */
  202. ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
  203. if (!ds)
  204. return ERR_PTR(-ENOMEM);
  205. ds->dst = dst;
  206. ds->index = index;
  207. ds->cd = cd;
  208. ds->ops = ops;
  209. ds->priv = priv;
  210. ret = dsa_switch_setup_one(ds, parent);
  211. if (ret)
  212. return ERR_PTR(ret);
  213. return ds;
  214. }
  215. static void dsa_switch_destroy(struct dsa_switch *ds)
  216. {
  217. int port;
  218. /* Destroy network devices for physical switch ports. */
  219. for (port = 0; port < ds->num_ports; port++) {
  220. if (!(ds->enabled_port_mask & (1 << port)))
  221. continue;
  222. if (!ds->ports[port].netdev)
  223. continue;
  224. dsa_slave_destroy(ds->ports[port].netdev);
  225. }
  226. /* Disable configuration of the CPU and DSA ports */
  227. for (port = 0; port < ds->num_ports; port++) {
  228. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  229. continue;
  230. dsa_cpu_dsa_destroy(&ds->ports[port]);
  231. /* Clearing a bit which is not set does no harm */
  232. ds->cpu_port_mask |= ~(1 << port);
  233. ds->dsa_port_mask |= ~(1 << port);
  234. }
  235. if (ds->slave_mii_bus && ds->ops->phy_read)
  236. mdiobus_unregister(ds->slave_mii_bus);
  237. dsa_switch_unregister_notifier(ds);
  238. }
  239. #ifdef CONFIG_PM_SLEEP
  240. int dsa_switch_suspend(struct dsa_switch *ds)
  241. {
  242. int i, ret = 0;
  243. /* Suspend slave network devices */
  244. for (i = 0; i < ds->num_ports; i++) {
  245. if (!dsa_is_port_initialized(ds, i))
  246. continue;
  247. ret = dsa_slave_suspend(ds->ports[i].netdev);
  248. if (ret)
  249. return ret;
  250. }
  251. if (ds->ops->suspend)
  252. ret = ds->ops->suspend(ds);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  256. int dsa_switch_resume(struct dsa_switch *ds)
  257. {
  258. int i, ret = 0;
  259. if (ds->ops->resume)
  260. ret = ds->ops->resume(ds);
  261. if (ret)
  262. return ret;
  263. /* Resume slave network devices */
  264. for (i = 0; i < ds->num_ports; i++) {
  265. if (!dsa_is_port_initialized(ds, i))
  266. continue;
  267. ret = dsa_slave_resume(ds->ports[i].netdev);
  268. if (ret)
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  274. #endif
  275. /* platform driver init and cleanup *****************************************/
  276. static int dev_is_class(struct device *dev, void *class)
  277. {
  278. if (dev->class != NULL && !strcmp(dev->class->name, class))
  279. return 1;
  280. return 0;
  281. }
  282. static struct device *dev_find_class(struct device *parent, char *class)
  283. {
  284. if (dev_is_class(parent, class)) {
  285. get_device(parent);
  286. return parent;
  287. }
  288. return device_find_child(parent, class, dev_is_class);
  289. }
  290. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  291. {
  292. struct device *d;
  293. d = dev_find_class(dev, "mdio_bus");
  294. if (d != NULL) {
  295. struct mii_bus *bus;
  296. bus = to_mii_bus(d);
  297. put_device(d);
  298. return bus;
  299. }
  300. return NULL;
  301. }
  302. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  303. #ifdef CONFIG_OF
  304. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  305. struct dsa_chip_data *cd,
  306. int chip_index, int port_index,
  307. struct device_node *link)
  308. {
  309. const __be32 *reg;
  310. int link_sw_addr;
  311. struct device_node *parent_sw;
  312. int len;
  313. parent_sw = of_get_parent(link);
  314. if (!parent_sw)
  315. return -EINVAL;
  316. reg = of_get_property(parent_sw, "reg", &len);
  317. if (!reg || (len != sizeof(*reg) * 2))
  318. return -EINVAL;
  319. /*
  320. * Get the destination switch number from the second field of its 'reg'
  321. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  322. */
  323. link_sw_addr = be32_to_cpup(reg + 1);
  324. if (link_sw_addr >= pd->nr_chips)
  325. return -EINVAL;
  326. cd->rtable[link_sw_addr] = port_index;
  327. return 0;
  328. }
  329. static int dsa_of_probe_links(struct dsa_platform_data *pd,
  330. struct dsa_chip_data *cd,
  331. int chip_index, int port_index,
  332. struct device_node *port,
  333. const char *port_name)
  334. {
  335. struct device_node *link;
  336. int link_index;
  337. int ret;
  338. for (link_index = 0;; link_index++) {
  339. link = of_parse_phandle(port, "link", link_index);
  340. if (!link)
  341. break;
  342. if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
  343. ret = dsa_of_setup_routing_table(pd, cd, chip_index,
  344. port_index, link);
  345. if (ret)
  346. return ret;
  347. }
  348. }
  349. return 0;
  350. }
  351. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  352. {
  353. int i;
  354. int port_index;
  355. for (i = 0; i < pd->nr_chips; i++) {
  356. port_index = 0;
  357. while (port_index < DSA_MAX_PORTS) {
  358. kfree(pd->chip[i].port_names[port_index]);
  359. port_index++;
  360. }
  361. /* Drop our reference to the MDIO bus device */
  362. if (pd->chip[i].host_dev)
  363. put_device(pd->chip[i].host_dev);
  364. }
  365. kfree(pd->chip);
  366. }
  367. static int dsa_of_probe(struct device *dev)
  368. {
  369. struct device_node *np = dev->of_node;
  370. struct device_node *child, *mdio, *ethernet, *port;
  371. struct mii_bus *mdio_bus, *mdio_bus_switch;
  372. struct net_device *ethernet_dev;
  373. struct dsa_platform_data *pd;
  374. struct dsa_chip_data *cd;
  375. const char *port_name;
  376. int chip_index, port_index;
  377. const unsigned int *sw_addr, *port_reg;
  378. u32 eeprom_len;
  379. int ret;
  380. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  381. if (!mdio)
  382. return -EINVAL;
  383. mdio_bus = of_mdio_find_bus(mdio);
  384. if (!mdio_bus)
  385. return -EPROBE_DEFER;
  386. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  387. if (!ethernet) {
  388. ret = -EINVAL;
  389. goto out_put_mdio;
  390. }
  391. ethernet_dev = of_find_net_device_by_node(ethernet);
  392. if (!ethernet_dev) {
  393. ret = -EPROBE_DEFER;
  394. goto out_put_mdio;
  395. }
  396. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  397. if (!pd) {
  398. ret = -ENOMEM;
  399. goto out_put_ethernet;
  400. }
  401. dev->platform_data = pd;
  402. pd->of_netdev = ethernet_dev;
  403. pd->nr_chips = of_get_available_child_count(np);
  404. if (pd->nr_chips > DSA_MAX_SWITCHES)
  405. pd->nr_chips = DSA_MAX_SWITCHES;
  406. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  407. GFP_KERNEL);
  408. if (!pd->chip) {
  409. ret = -ENOMEM;
  410. goto out_free;
  411. }
  412. chip_index = -1;
  413. for_each_available_child_of_node(np, child) {
  414. int i;
  415. chip_index++;
  416. cd = &pd->chip[chip_index];
  417. cd->of_node = child;
  418. /* Initialize the routing table */
  419. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  420. cd->rtable[i] = DSA_RTABLE_NONE;
  421. /* When assigning the host device, increment its refcount */
  422. cd->host_dev = get_device(&mdio_bus->dev);
  423. sw_addr = of_get_property(child, "reg", NULL);
  424. if (!sw_addr)
  425. continue;
  426. cd->sw_addr = be32_to_cpup(sw_addr);
  427. if (cd->sw_addr >= PHY_MAX_ADDR)
  428. continue;
  429. if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
  430. cd->eeprom_len = eeprom_len;
  431. mdio = of_parse_phandle(child, "mii-bus", 0);
  432. if (mdio) {
  433. mdio_bus_switch = of_mdio_find_bus(mdio);
  434. if (!mdio_bus_switch) {
  435. ret = -EPROBE_DEFER;
  436. goto out_free_chip;
  437. }
  438. /* Drop the mdio_bus device ref, replacing the host
  439. * device with the mdio_bus_switch device, keeping
  440. * the refcount from of_mdio_find_bus() above.
  441. */
  442. put_device(cd->host_dev);
  443. cd->host_dev = &mdio_bus_switch->dev;
  444. }
  445. for_each_available_child_of_node(child, port) {
  446. port_reg = of_get_property(port, "reg", NULL);
  447. if (!port_reg)
  448. continue;
  449. port_index = be32_to_cpup(port_reg);
  450. if (port_index >= DSA_MAX_PORTS)
  451. break;
  452. port_name = of_get_property(port, "label", NULL);
  453. if (!port_name)
  454. continue;
  455. cd->port_dn[port_index] = port;
  456. cd->port_names[port_index] = kstrdup(port_name,
  457. GFP_KERNEL);
  458. if (!cd->port_names[port_index]) {
  459. ret = -ENOMEM;
  460. goto out_free_chip;
  461. }
  462. ret = dsa_of_probe_links(pd, cd, chip_index,
  463. port_index, port, port_name);
  464. if (ret)
  465. goto out_free_chip;
  466. }
  467. }
  468. /* The individual chips hold their own refcount on the mdio bus,
  469. * so drop ours */
  470. put_device(&mdio_bus->dev);
  471. return 0;
  472. out_free_chip:
  473. dsa_of_free_platform_data(pd);
  474. out_free:
  475. kfree(pd);
  476. dev->platform_data = NULL;
  477. out_put_ethernet:
  478. put_device(&ethernet_dev->dev);
  479. out_put_mdio:
  480. put_device(&mdio_bus->dev);
  481. return ret;
  482. }
  483. static void dsa_of_remove(struct device *dev)
  484. {
  485. struct dsa_platform_data *pd = dev->platform_data;
  486. if (!dev->of_node)
  487. return;
  488. dsa_of_free_platform_data(pd);
  489. put_device(&pd->of_netdev->dev);
  490. kfree(pd);
  491. }
  492. #else
  493. static inline int dsa_of_probe(struct device *dev)
  494. {
  495. return 0;
  496. }
  497. static inline void dsa_of_remove(struct device *dev)
  498. {
  499. }
  500. #endif
  501. static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
  502. struct device *parent, struct dsa_platform_data *pd)
  503. {
  504. int i;
  505. unsigned configured = 0;
  506. dst->pd = pd;
  507. dst->master_netdev = dev;
  508. for (i = 0; i < pd->nr_chips; i++) {
  509. struct dsa_switch *ds;
  510. ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
  511. if (IS_ERR(ds)) {
  512. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  513. i, PTR_ERR(ds));
  514. continue;
  515. }
  516. dst->ds[i] = ds;
  517. ++configured;
  518. }
  519. /*
  520. * If no switch was found, exit cleanly
  521. */
  522. if (!configured)
  523. return -EPROBE_DEFER;
  524. /*
  525. * If we use a tagging format that doesn't have an ethertype
  526. * field, make sure that all packets from this point on get
  527. * sent to the tag format's receive function.
  528. */
  529. wmb();
  530. dev->dsa_ptr = (void *)dst;
  531. return 0;
  532. }
  533. static int dsa_probe(struct platform_device *pdev)
  534. {
  535. struct dsa_platform_data *pd = pdev->dev.platform_data;
  536. struct net_device *dev;
  537. struct dsa_switch_tree *dst;
  538. int ret;
  539. if (pdev->dev.of_node) {
  540. ret = dsa_of_probe(&pdev->dev);
  541. if (ret)
  542. return ret;
  543. pd = pdev->dev.platform_data;
  544. }
  545. if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
  546. return -EINVAL;
  547. if (pd->of_netdev) {
  548. dev = pd->of_netdev;
  549. dev_hold(dev);
  550. } else {
  551. dev = dsa_dev_to_net_device(pd->netdev);
  552. }
  553. if (dev == NULL) {
  554. ret = -EPROBE_DEFER;
  555. goto out;
  556. }
  557. if (dev->dsa_ptr != NULL) {
  558. dev_put(dev);
  559. ret = -EEXIST;
  560. goto out;
  561. }
  562. dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  563. if (dst == NULL) {
  564. dev_put(dev);
  565. ret = -ENOMEM;
  566. goto out;
  567. }
  568. platform_set_drvdata(pdev, dst);
  569. ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
  570. if (ret) {
  571. dev_put(dev);
  572. goto out;
  573. }
  574. return 0;
  575. out:
  576. dsa_of_remove(&pdev->dev);
  577. return ret;
  578. }
  579. static void dsa_remove_dst(struct dsa_switch_tree *dst)
  580. {
  581. int i;
  582. dst->master_netdev->dsa_ptr = NULL;
  583. /* If we used a tagging format that doesn't have an ethertype
  584. * field, make sure that all packets from this point get sent
  585. * without the tag and go through the regular receive path.
  586. */
  587. wmb();
  588. for (i = 0; i < dst->pd->nr_chips; i++) {
  589. struct dsa_switch *ds = dst->ds[i];
  590. if (ds)
  591. dsa_switch_destroy(ds);
  592. }
  593. dsa_cpu_port_ethtool_restore(dst->cpu_dp->ds);
  594. dev_put(dst->master_netdev);
  595. }
  596. static int dsa_remove(struct platform_device *pdev)
  597. {
  598. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  599. dsa_remove_dst(dst);
  600. dsa_of_remove(&pdev->dev);
  601. return 0;
  602. }
  603. static void dsa_shutdown(struct platform_device *pdev)
  604. {
  605. }
  606. #ifdef CONFIG_PM_SLEEP
  607. static int dsa_suspend(struct device *d)
  608. {
  609. struct platform_device *pdev = to_platform_device(d);
  610. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  611. int i, ret = 0;
  612. for (i = 0; i < dst->pd->nr_chips; i++) {
  613. struct dsa_switch *ds = dst->ds[i];
  614. if (ds != NULL)
  615. ret = dsa_switch_suspend(ds);
  616. }
  617. return ret;
  618. }
  619. static int dsa_resume(struct device *d)
  620. {
  621. struct platform_device *pdev = to_platform_device(d);
  622. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  623. int i, ret = 0;
  624. for (i = 0; i < dst->pd->nr_chips; i++) {
  625. struct dsa_switch *ds = dst->ds[i];
  626. if (ds != NULL)
  627. ret = dsa_switch_resume(ds);
  628. }
  629. return ret;
  630. }
  631. #endif
  632. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  633. static const struct of_device_id dsa_of_match_table[] = {
  634. { .compatible = "marvell,dsa", },
  635. {}
  636. };
  637. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  638. static struct platform_driver dsa_driver = {
  639. .probe = dsa_probe,
  640. .remove = dsa_remove,
  641. .shutdown = dsa_shutdown,
  642. .driver = {
  643. .name = "dsa",
  644. .of_match_table = dsa_of_match_table,
  645. .pm = &dsa_pm_ops,
  646. },
  647. };
  648. int dsa_legacy_register(void)
  649. {
  650. return platform_driver_register(&dsa_driver);
  651. }
  652. void dsa_legacy_unregister(void)
  653. {
  654. platform_driver_unregister(&dsa_driver);
  655. }