dsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * net/dsa/dsa.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/of_gpio.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/phy_fixed.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <net/dsa.h>
  26. #include "dsa_priv.h"
  27. static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
  28. struct net_device *dev)
  29. {
  30. /* Just return the original SKB */
  31. return skb;
  32. }
  33. static const struct dsa_device_ops none_ops = {
  34. .xmit = dsa_slave_notag_xmit,
  35. .rcv = NULL,
  36. };
  37. const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
  38. #ifdef CONFIG_NET_DSA_TAG_DSA
  39. [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
  40. #endif
  41. #ifdef CONFIG_NET_DSA_TAG_EDSA
  42. [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
  43. #endif
  44. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  45. [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
  46. #endif
  47. #ifdef CONFIG_NET_DSA_TAG_BRCM
  48. [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
  49. #endif
  50. #ifdef CONFIG_NET_DSA_TAG_QCA
  51. [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
  52. #endif
  53. [DSA_TAG_PROTO_NONE] = &none_ops,
  54. };
  55. /* switch driver registration ***********************************************/
  56. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  57. static LIST_HEAD(dsa_switch_drivers);
  58. void register_switch_driver(struct dsa_switch_driver *drv)
  59. {
  60. mutex_lock(&dsa_switch_drivers_mutex);
  61. list_add_tail(&drv->list, &dsa_switch_drivers);
  62. mutex_unlock(&dsa_switch_drivers_mutex);
  63. }
  64. EXPORT_SYMBOL_GPL(register_switch_driver);
  65. void unregister_switch_driver(struct dsa_switch_driver *drv)
  66. {
  67. mutex_lock(&dsa_switch_drivers_mutex);
  68. list_del_init(&drv->list);
  69. mutex_unlock(&dsa_switch_drivers_mutex);
  70. }
  71. EXPORT_SYMBOL_GPL(unregister_switch_driver);
  72. static const struct dsa_switch_ops *
  73. dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
  74. const char **_name, void **priv)
  75. {
  76. const struct dsa_switch_ops *ret;
  77. struct list_head *list;
  78. const char *name;
  79. ret = NULL;
  80. name = NULL;
  81. mutex_lock(&dsa_switch_drivers_mutex);
  82. list_for_each(list, &dsa_switch_drivers) {
  83. const struct dsa_switch_ops *ops;
  84. struct dsa_switch_driver *drv;
  85. drv = list_entry(list, struct dsa_switch_driver, list);
  86. ops = drv->ops;
  87. name = ops->probe(parent, host_dev, sw_addr, priv);
  88. if (name != NULL) {
  89. ret = ops;
  90. break;
  91. }
  92. }
  93. mutex_unlock(&dsa_switch_drivers_mutex);
  94. *_name = name;
  95. return ret;
  96. }
  97. /* basic switch operations **************************************************/
  98. int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
  99. struct dsa_port *dport, int port)
  100. {
  101. struct device_node *port_dn = dport->dn;
  102. struct phy_device *phydev;
  103. int ret, mode;
  104. if (of_phy_is_fixed_link(port_dn)) {
  105. ret = of_phy_register_fixed_link(port_dn);
  106. if (ret) {
  107. dev_err(dev, "failed to register fixed PHY\n");
  108. return ret;
  109. }
  110. phydev = of_phy_find_device(port_dn);
  111. mode = of_get_phy_mode(port_dn);
  112. if (mode < 0)
  113. mode = PHY_INTERFACE_MODE_NA;
  114. phydev->interface = mode;
  115. genphy_config_init(phydev);
  116. genphy_read_status(phydev);
  117. if (ds->ops->adjust_link)
  118. ds->ops->adjust_link(ds, port, phydev);
  119. put_device(&phydev->mdio.dev);
  120. }
  121. return 0;
  122. }
  123. static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
  124. {
  125. struct dsa_port *dport;
  126. int ret, port;
  127. for (port = 0; port < ds->num_ports; port++) {
  128. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  129. continue;
  130. dport = &ds->ports[port];
  131. ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
  132. if (ret)
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
  138. {
  139. const struct dsa_device_ops *ops;
  140. if (tag_protocol >= DSA_TAG_LAST)
  141. return ERR_PTR(-EINVAL);
  142. ops = dsa_device_ops[tag_protocol];
  143. if (!ops)
  144. return ERR_PTR(-ENOPROTOOPT);
  145. return ops;
  146. }
  147. int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
  148. {
  149. struct net_device *master;
  150. struct ethtool_ops *cpu_ops;
  151. master = ds->dst->master_netdev;
  152. if (ds->master_netdev)
  153. master = ds->master_netdev;
  154. cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
  155. if (!cpu_ops)
  156. return -ENOMEM;
  157. memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
  158. sizeof(struct ethtool_ops));
  159. ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
  160. memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
  161. sizeof(struct ethtool_ops));
  162. dsa_cpu_port_ethtool_init(cpu_ops);
  163. master->ethtool_ops = cpu_ops;
  164. return 0;
  165. }
  166. void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
  167. {
  168. struct net_device *master;
  169. master = ds->dst->master_netdev;
  170. if (ds->master_netdev)
  171. master = ds->master_netdev;
  172. master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
  173. }
  174. static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  175. {
  176. const struct dsa_switch_ops *ops = ds->ops;
  177. struct dsa_switch_tree *dst = ds->dst;
  178. struct dsa_chip_data *cd = ds->cd;
  179. bool valid_name_found = false;
  180. int index = ds->index;
  181. int i, ret;
  182. /*
  183. * Validate supplied switch configuration.
  184. */
  185. for (i = 0; i < ds->num_ports; i++) {
  186. char *name;
  187. name = cd->port_names[i];
  188. if (name == NULL)
  189. continue;
  190. if (!strcmp(name, "cpu")) {
  191. if (dst->cpu_switch) {
  192. netdev_err(dst->master_netdev,
  193. "multiple cpu ports?!\n");
  194. return -EINVAL;
  195. }
  196. dst->cpu_switch = ds;
  197. dst->cpu_port = i;
  198. ds->cpu_port_mask |= 1 << i;
  199. } else if (!strcmp(name, "dsa")) {
  200. ds->dsa_port_mask |= 1 << i;
  201. } else {
  202. ds->enabled_port_mask |= 1 << i;
  203. }
  204. valid_name_found = true;
  205. }
  206. if (!valid_name_found && i == ds->num_ports)
  207. return -EINVAL;
  208. /* Make the built-in MII bus mask match the number of ports,
  209. * switch drivers can override this later
  210. */
  211. ds->phys_mii_mask = ds->enabled_port_mask;
  212. /*
  213. * If the CPU connects to this switch, set the switch tree
  214. * tagging protocol to the preferred tagging format of this
  215. * switch.
  216. */
  217. if (dst->cpu_switch == ds) {
  218. enum dsa_tag_protocol tag_protocol;
  219. tag_protocol = ops->get_tag_protocol(ds);
  220. dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
  221. if (IS_ERR(dst->tag_ops))
  222. return PTR_ERR(dst->tag_ops);
  223. dst->rcv = dst->tag_ops->rcv;
  224. }
  225. memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
  226. /*
  227. * Do basic register setup.
  228. */
  229. ret = ops->setup(ds);
  230. if (ret < 0)
  231. return ret;
  232. ret = dsa_switch_register_notifier(ds);
  233. if (ret)
  234. return ret;
  235. if (ops->set_addr) {
  236. ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
  237. if (ret < 0)
  238. return ret;
  239. }
  240. if (!ds->slave_mii_bus && ops->phy_read) {
  241. ds->slave_mii_bus = devm_mdiobus_alloc(parent);
  242. if (!ds->slave_mii_bus)
  243. return -ENOMEM;
  244. dsa_slave_mii_bus_init(ds);
  245. ret = mdiobus_register(ds->slave_mii_bus);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. /*
  250. * Create network devices for physical switch ports.
  251. */
  252. for (i = 0; i < ds->num_ports; i++) {
  253. ds->ports[i].dn = cd->port_dn[i];
  254. if (!(ds->enabled_port_mask & (1 << i)))
  255. continue;
  256. ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
  257. if (ret < 0)
  258. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
  259. index, i, cd->port_names[i], ret);
  260. }
  261. /* Perform configuration of the CPU and DSA ports */
  262. ret = dsa_cpu_dsa_setups(ds, parent);
  263. if (ret < 0)
  264. netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
  265. index);
  266. ret = dsa_cpu_port_ethtool_setup(ds);
  267. if (ret)
  268. return ret;
  269. return 0;
  270. }
  271. static struct dsa_switch *
  272. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  273. struct device *parent, struct device *host_dev)
  274. {
  275. struct dsa_chip_data *cd = dst->pd->chip + index;
  276. const struct dsa_switch_ops *ops;
  277. struct dsa_switch *ds;
  278. int ret;
  279. const char *name;
  280. void *priv;
  281. /*
  282. * Probe for switch model.
  283. */
  284. ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
  285. if (!ops) {
  286. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  287. index);
  288. return ERR_PTR(-EINVAL);
  289. }
  290. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  291. index, name);
  292. /*
  293. * Allocate and initialise switch state.
  294. */
  295. ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
  296. if (!ds)
  297. return ERR_PTR(-ENOMEM);
  298. ds->dst = dst;
  299. ds->index = index;
  300. ds->cd = cd;
  301. ds->ops = ops;
  302. ds->priv = priv;
  303. ret = dsa_switch_setup_one(ds, parent);
  304. if (ret)
  305. return ERR_PTR(ret);
  306. return ds;
  307. }
  308. void dsa_cpu_dsa_destroy(struct dsa_port *port)
  309. {
  310. struct device_node *port_dn = port->dn;
  311. if (of_phy_is_fixed_link(port_dn))
  312. of_phy_deregister_fixed_link(port_dn);
  313. }
  314. static void dsa_switch_destroy(struct dsa_switch *ds)
  315. {
  316. int port;
  317. /* Destroy network devices for physical switch ports. */
  318. for (port = 0; port < ds->num_ports; port++) {
  319. if (!(ds->enabled_port_mask & (1 << port)))
  320. continue;
  321. if (!ds->ports[port].netdev)
  322. continue;
  323. dsa_slave_destroy(ds->ports[port].netdev);
  324. }
  325. /* Disable configuration of the CPU and DSA ports */
  326. for (port = 0; port < ds->num_ports; port++) {
  327. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  328. continue;
  329. dsa_cpu_dsa_destroy(&ds->ports[port]);
  330. /* Clearing a bit which is not set does no harm */
  331. ds->cpu_port_mask |= ~(1 << port);
  332. ds->dsa_port_mask |= ~(1 << port);
  333. }
  334. if (ds->slave_mii_bus && ds->ops->phy_read)
  335. mdiobus_unregister(ds->slave_mii_bus);
  336. dsa_switch_unregister_notifier(ds);
  337. }
  338. #ifdef CONFIG_PM_SLEEP
  339. int dsa_switch_suspend(struct dsa_switch *ds)
  340. {
  341. int i, ret = 0;
  342. /* Suspend slave network devices */
  343. for (i = 0; i < ds->num_ports; i++) {
  344. if (!dsa_is_port_initialized(ds, i))
  345. continue;
  346. ret = dsa_slave_suspend(ds->ports[i].netdev);
  347. if (ret)
  348. return ret;
  349. }
  350. if (ds->ops->suspend)
  351. ret = ds->ops->suspend(ds);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  355. int dsa_switch_resume(struct dsa_switch *ds)
  356. {
  357. int i, ret = 0;
  358. if (ds->ops->resume)
  359. ret = ds->ops->resume(ds);
  360. if (ret)
  361. return ret;
  362. /* Resume slave network devices */
  363. for (i = 0; i < ds->num_ports; i++) {
  364. if (!dsa_is_port_initialized(ds, i))
  365. continue;
  366. ret = dsa_slave_resume(ds->ports[i].netdev);
  367. if (ret)
  368. return ret;
  369. }
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  373. #endif
  374. /* platform driver init and cleanup *****************************************/
  375. static int dev_is_class(struct device *dev, void *class)
  376. {
  377. if (dev->class != NULL && !strcmp(dev->class->name, class))
  378. return 1;
  379. return 0;
  380. }
  381. static struct device *dev_find_class(struct device *parent, char *class)
  382. {
  383. if (dev_is_class(parent, class)) {
  384. get_device(parent);
  385. return parent;
  386. }
  387. return device_find_child(parent, class, dev_is_class);
  388. }
  389. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  390. {
  391. struct device *d;
  392. d = dev_find_class(dev, "mdio_bus");
  393. if (d != NULL) {
  394. struct mii_bus *bus;
  395. bus = to_mii_bus(d);
  396. put_device(d);
  397. return bus;
  398. }
  399. return NULL;
  400. }
  401. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  402. struct net_device *dsa_dev_to_net_device(struct device *dev)
  403. {
  404. struct device *d;
  405. d = dev_find_class(dev, "net");
  406. if (d != NULL) {
  407. struct net_device *nd;
  408. nd = to_net_dev(d);
  409. dev_hold(nd);
  410. put_device(d);
  411. return nd;
  412. }
  413. return NULL;
  414. }
  415. EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
  416. #ifdef CONFIG_OF
  417. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  418. struct dsa_chip_data *cd,
  419. int chip_index, int port_index,
  420. struct device_node *link)
  421. {
  422. const __be32 *reg;
  423. int link_sw_addr;
  424. struct device_node *parent_sw;
  425. int len;
  426. parent_sw = of_get_parent(link);
  427. if (!parent_sw)
  428. return -EINVAL;
  429. reg = of_get_property(parent_sw, "reg", &len);
  430. if (!reg || (len != sizeof(*reg) * 2))
  431. return -EINVAL;
  432. /*
  433. * Get the destination switch number from the second field of its 'reg'
  434. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  435. */
  436. link_sw_addr = be32_to_cpup(reg + 1);
  437. if (link_sw_addr >= pd->nr_chips)
  438. return -EINVAL;
  439. cd->rtable[link_sw_addr] = port_index;
  440. return 0;
  441. }
  442. static int dsa_of_probe_links(struct dsa_platform_data *pd,
  443. struct dsa_chip_data *cd,
  444. int chip_index, int port_index,
  445. struct device_node *port,
  446. const char *port_name)
  447. {
  448. struct device_node *link;
  449. int link_index;
  450. int ret;
  451. for (link_index = 0;; link_index++) {
  452. link = of_parse_phandle(port, "link", link_index);
  453. if (!link)
  454. break;
  455. if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
  456. ret = dsa_of_setup_routing_table(pd, cd, chip_index,
  457. port_index, link);
  458. if (ret)
  459. return ret;
  460. }
  461. }
  462. return 0;
  463. }
  464. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  465. {
  466. int i;
  467. int port_index;
  468. for (i = 0; i < pd->nr_chips; i++) {
  469. port_index = 0;
  470. while (port_index < DSA_MAX_PORTS) {
  471. kfree(pd->chip[i].port_names[port_index]);
  472. port_index++;
  473. }
  474. /* Drop our reference to the MDIO bus device */
  475. if (pd->chip[i].host_dev)
  476. put_device(pd->chip[i].host_dev);
  477. }
  478. kfree(pd->chip);
  479. }
  480. static int dsa_of_probe(struct device *dev)
  481. {
  482. struct device_node *np = dev->of_node;
  483. struct device_node *child, *mdio, *ethernet, *port;
  484. struct mii_bus *mdio_bus, *mdio_bus_switch;
  485. struct net_device *ethernet_dev;
  486. struct dsa_platform_data *pd;
  487. struct dsa_chip_data *cd;
  488. const char *port_name;
  489. int chip_index, port_index;
  490. const unsigned int *sw_addr, *port_reg;
  491. u32 eeprom_len;
  492. int ret;
  493. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  494. if (!mdio)
  495. return -EINVAL;
  496. mdio_bus = of_mdio_find_bus(mdio);
  497. if (!mdio_bus)
  498. return -EPROBE_DEFER;
  499. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  500. if (!ethernet) {
  501. ret = -EINVAL;
  502. goto out_put_mdio;
  503. }
  504. ethernet_dev = of_find_net_device_by_node(ethernet);
  505. if (!ethernet_dev) {
  506. ret = -EPROBE_DEFER;
  507. goto out_put_mdio;
  508. }
  509. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  510. if (!pd) {
  511. ret = -ENOMEM;
  512. goto out_put_ethernet;
  513. }
  514. dev->platform_data = pd;
  515. pd->of_netdev = ethernet_dev;
  516. pd->nr_chips = of_get_available_child_count(np);
  517. if (pd->nr_chips > DSA_MAX_SWITCHES)
  518. pd->nr_chips = DSA_MAX_SWITCHES;
  519. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  520. GFP_KERNEL);
  521. if (!pd->chip) {
  522. ret = -ENOMEM;
  523. goto out_free;
  524. }
  525. chip_index = -1;
  526. for_each_available_child_of_node(np, child) {
  527. int i;
  528. chip_index++;
  529. cd = &pd->chip[chip_index];
  530. cd->of_node = child;
  531. /* Initialize the routing table */
  532. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  533. cd->rtable[i] = DSA_RTABLE_NONE;
  534. /* When assigning the host device, increment its refcount */
  535. cd->host_dev = get_device(&mdio_bus->dev);
  536. sw_addr = of_get_property(child, "reg", NULL);
  537. if (!sw_addr)
  538. continue;
  539. cd->sw_addr = be32_to_cpup(sw_addr);
  540. if (cd->sw_addr >= PHY_MAX_ADDR)
  541. continue;
  542. if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
  543. cd->eeprom_len = eeprom_len;
  544. mdio = of_parse_phandle(child, "mii-bus", 0);
  545. if (mdio) {
  546. mdio_bus_switch = of_mdio_find_bus(mdio);
  547. if (!mdio_bus_switch) {
  548. ret = -EPROBE_DEFER;
  549. goto out_free_chip;
  550. }
  551. /* Drop the mdio_bus device ref, replacing the host
  552. * device with the mdio_bus_switch device, keeping
  553. * the refcount from of_mdio_find_bus() above.
  554. */
  555. put_device(cd->host_dev);
  556. cd->host_dev = &mdio_bus_switch->dev;
  557. }
  558. for_each_available_child_of_node(child, port) {
  559. port_reg = of_get_property(port, "reg", NULL);
  560. if (!port_reg)
  561. continue;
  562. port_index = be32_to_cpup(port_reg);
  563. if (port_index >= DSA_MAX_PORTS)
  564. break;
  565. port_name = of_get_property(port, "label", NULL);
  566. if (!port_name)
  567. continue;
  568. cd->port_dn[port_index] = port;
  569. cd->port_names[port_index] = kstrdup(port_name,
  570. GFP_KERNEL);
  571. if (!cd->port_names[port_index]) {
  572. ret = -ENOMEM;
  573. goto out_free_chip;
  574. }
  575. ret = dsa_of_probe_links(pd, cd, chip_index,
  576. port_index, port, port_name);
  577. if (ret)
  578. goto out_free_chip;
  579. }
  580. }
  581. /* The individual chips hold their own refcount on the mdio bus,
  582. * so drop ours */
  583. put_device(&mdio_bus->dev);
  584. return 0;
  585. out_free_chip:
  586. dsa_of_free_platform_data(pd);
  587. out_free:
  588. kfree(pd);
  589. dev->platform_data = NULL;
  590. out_put_ethernet:
  591. put_device(&ethernet_dev->dev);
  592. out_put_mdio:
  593. put_device(&mdio_bus->dev);
  594. return ret;
  595. }
  596. static void dsa_of_remove(struct device *dev)
  597. {
  598. struct dsa_platform_data *pd = dev->platform_data;
  599. if (!dev->of_node)
  600. return;
  601. dsa_of_free_platform_data(pd);
  602. put_device(&pd->of_netdev->dev);
  603. kfree(pd);
  604. }
  605. #else
  606. static inline int dsa_of_probe(struct device *dev)
  607. {
  608. return 0;
  609. }
  610. static inline void dsa_of_remove(struct device *dev)
  611. {
  612. }
  613. #endif
  614. static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
  615. struct device *parent, struct dsa_platform_data *pd)
  616. {
  617. int i;
  618. unsigned configured = 0;
  619. dst->pd = pd;
  620. dst->master_netdev = dev;
  621. dst->cpu_port = -1;
  622. for (i = 0; i < pd->nr_chips; i++) {
  623. struct dsa_switch *ds;
  624. ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
  625. if (IS_ERR(ds)) {
  626. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  627. i, PTR_ERR(ds));
  628. continue;
  629. }
  630. dst->ds[i] = ds;
  631. ++configured;
  632. }
  633. /*
  634. * If no switch was found, exit cleanly
  635. */
  636. if (!configured)
  637. return -EPROBE_DEFER;
  638. /*
  639. * If we use a tagging format that doesn't have an ethertype
  640. * field, make sure that all packets from this point on get
  641. * sent to the tag format's receive function.
  642. */
  643. wmb();
  644. dev->dsa_ptr = (void *)dst;
  645. return 0;
  646. }
  647. static int dsa_probe(struct platform_device *pdev)
  648. {
  649. struct dsa_platform_data *pd = pdev->dev.platform_data;
  650. struct net_device *dev;
  651. struct dsa_switch_tree *dst;
  652. int ret;
  653. if (pdev->dev.of_node) {
  654. ret = dsa_of_probe(&pdev->dev);
  655. if (ret)
  656. return ret;
  657. pd = pdev->dev.platform_data;
  658. }
  659. if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
  660. return -EINVAL;
  661. if (pd->of_netdev) {
  662. dev = pd->of_netdev;
  663. dev_hold(dev);
  664. } else {
  665. dev = dsa_dev_to_net_device(pd->netdev);
  666. }
  667. if (dev == NULL) {
  668. ret = -EPROBE_DEFER;
  669. goto out;
  670. }
  671. if (dev->dsa_ptr != NULL) {
  672. dev_put(dev);
  673. ret = -EEXIST;
  674. goto out;
  675. }
  676. dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  677. if (dst == NULL) {
  678. dev_put(dev);
  679. ret = -ENOMEM;
  680. goto out;
  681. }
  682. platform_set_drvdata(pdev, dst);
  683. ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
  684. if (ret) {
  685. dev_put(dev);
  686. goto out;
  687. }
  688. return 0;
  689. out:
  690. dsa_of_remove(&pdev->dev);
  691. return ret;
  692. }
  693. static void dsa_remove_dst(struct dsa_switch_tree *dst)
  694. {
  695. int i;
  696. dst->master_netdev->dsa_ptr = NULL;
  697. /* If we used a tagging format that doesn't have an ethertype
  698. * field, make sure that all packets from this point get sent
  699. * without the tag and go through the regular receive path.
  700. */
  701. wmb();
  702. for (i = 0; i < dst->pd->nr_chips; i++) {
  703. struct dsa_switch *ds = dst->ds[i];
  704. if (ds)
  705. dsa_switch_destroy(ds);
  706. }
  707. dsa_cpu_port_ethtool_restore(dst->cpu_switch);
  708. dev_put(dst->master_netdev);
  709. }
  710. static int dsa_remove(struct platform_device *pdev)
  711. {
  712. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  713. dsa_remove_dst(dst);
  714. dsa_of_remove(&pdev->dev);
  715. return 0;
  716. }
  717. static void dsa_shutdown(struct platform_device *pdev)
  718. {
  719. }
  720. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  721. struct packet_type *pt, struct net_device *orig_dev)
  722. {
  723. struct dsa_switch_tree *dst = dev->dsa_ptr;
  724. if (unlikely(dst == NULL)) {
  725. kfree_skb(skb);
  726. return 0;
  727. }
  728. return dst->rcv(skb, dev, pt, orig_dev);
  729. }
  730. static struct packet_type dsa_pack_type __read_mostly = {
  731. .type = cpu_to_be16(ETH_P_XDSA),
  732. .func = dsa_switch_rcv,
  733. };
  734. #ifdef CONFIG_PM_SLEEP
  735. static int dsa_suspend(struct device *d)
  736. {
  737. struct platform_device *pdev = to_platform_device(d);
  738. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  739. int i, ret = 0;
  740. for (i = 0; i < dst->pd->nr_chips; i++) {
  741. struct dsa_switch *ds = dst->ds[i];
  742. if (ds != NULL)
  743. ret = dsa_switch_suspend(ds);
  744. }
  745. return ret;
  746. }
  747. static int dsa_resume(struct device *d)
  748. {
  749. struct platform_device *pdev = to_platform_device(d);
  750. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  751. int i, ret = 0;
  752. for (i = 0; i < dst->pd->nr_chips; i++) {
  753. struct dsa_switch *ds = dst->ds[i];
  754. if (ds != NULL)
  755. ret = dsa_switch_resume(ds);
  756. }
  757. return ret;
  758. }
  759. #endif
  760. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  761. static const struct of_device_id dsa_of_match_table[] = {
  762. { .compatible = "marvell,dsa", },
  763. {}
  764. };
  765. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  766. static struct platform_driver dsa_driver = {
  767. .probe = dsa_probe,
  768. .remove = dsa_remove,
  769. .shutdown = dsa_shutdown,
  770. .driver = {
  771. .name = "dsa",
  772. .of_match_table = dsa_of_match_table,
  773. .pm = &dsa_pm_ops,
  774. },
  775. };
  776. static int __init dsa_init_module(void)
  777. {
  778. int rc;
  779. rc = dsa_slave_register_notifier();
  780. if (rc)
  781. return rc;
  782. rc = platform_driver_register(&dsa_driver);
  783. if (rc)
  784. return rc;
  785. dev_add_pack(&dsa_pack_type);
  786. return 0;
  787. }
  788. module_init(dsa_init_module);
  789. static void __exit dsa_cleanup_module(void)
  790. {
  791. dsa_slave_unregister_notifier();
  792. dev_remove_pack(&dsa_pack_type);
  793. platform_driver_unregister(&dsa_driver);
  794. }
  795. module_exit(dsa_cleanup_module);
  796. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  797. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  798. MODULE_LICENSE("GPL");
  799. MODULE_ALIAS("platform:dsa");