dsa.c 21 KB

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