dsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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/list.h>
  12. #include <linux/netdevice.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 "dsa_priv.h"
  21. char dsa_driver_version[] = "0.1";
  22. /* switch driver registration ***********************************************/
  23. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  24. static LIST_HEAD(dsa_switch_drivers);
  25. void register_switch_driver(struct dsa_switch_driver *drv)
  26. {
  27. mutex_lock(&dsa_switch_drivers_mutex);
  28. list_add_tail(&drv->list, &dsa_switch_drivers);
  29. mutex_unlock(&dsa_switch_drivers_mutex);
  30. }
  31. EXPORT_SYMBOL_GPL(register_switch_driver);
  32. void unregister_switch_driver(struct dsa_switch_driver *drv)
  33. {
  34. mutex_lock(&dsa_switch_drivers_mutex);
  35. list_del_init(&drv->list);
  36. mutex_unlock(&dsa_switch_drivers_mutex);
  37. }
  38. EXPORT_SYMBOL_GPL(unregister_switch_driver);
  39. static struct dsa_switch_driver *
  40. dsa_switch_probe(struct mii_bus *bus, int sw_addr, char **_name)
  41. {
  42. struct dsa_switch_driver *ret;
  43. struct list_head *list;
  44. char *name;
  45. ret = NULL;
  46. name = NULL;
  47. mutex_lock(&dsa_switch_drivers_mutex);
  48. list_for_each(list, &dsa_switch_drivers) {
  49. struct dsa_switch_driver *drv;
  50. drv = list_entry(list, struct dsa_switch_driver, list);
  51. name = drv->probe(bus, sw_addr);
  52. if (name != NULL) {
  53. ret = drv;
  54. break;
  55. }
  56. }
  57. mutex_unlock(&dsa_switch_drivers_mutex);
  58. *_name = name;
  59. return ret;
  60. }
  61. /* basic switch operations **************************************************/
  62. static struct dsa_switch *
  63. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  64. struct device *parent, struct mii_bus *bus)
  65. {
  66. struct dsa_chip_data *pd = dst->pd->chip + index;
  67. struct dsa_switch_driver *drv;
  68. struct dsa_switch *ds;
  69. int ret;
  70. char *name;
  71. int i;
  72. bool valid_name_found = false;
  73. /*
  74. * Probe for switch model.
  75. */
  76. drv = dsa_switch_probe(bus, pd->sw_addr, &name);
  77. if (drv == NULL) {
  78. printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
  79. dst->master_netdev->name, index);
  80. return ERR_PTR(-EINVAL);
  81. }
  82. printk(KERN_INFO "%s[%d]: detected a %s switch\n",
  83. dst->master_netdev->name, index, name);
  84. /*
  85. * Allocate and initialise switch state.
  86. */
  87. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  88. if (ds == NULL)
  89. return ERR_PTR(-ENOMEM);
  90. ds->dst = dst;
  91. ds->index = index;
  92. ds->pd = dst->pd->chip + index;
  93. ds->drv = drv;
  94. ds->master_mii_bus = bus;
  95. /*
  96. * Validate supplied switch configuration.
  97. */
  98. for (i = 0; i < DSA_MAX_PORTS; i++) {
  99. char *name;
  100. name = pd->port_names[i];
  101. if (name == NULL)
  102. continue;
  103. if (!strcmp(name, "cpu")) {
  104. if (dst->cpu_switch != -1) {
  105. printk(KERN_ERR "multiple cpu ports?!\n");
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. dst->cpu_switch = index;
  110. dst->cpu_port = i;
  111. } else if (!strcmp(name, "dsa")) {
  112. ds->dsa_port_mask |= 1 << i;
  113. } else {
  114. ds->phys_port_mask |= 1 << i;
  115. }
  116. valid_name_found = true;
  117. }
  118. if (!valid_name_found && i == DSA_MAX_PORTS) {
  119. ret = -EINVAL;
  120. goto out;
  121. }
  122. /*
  123. * If the CPU connects to this switch, set the switch tree
  124. * tagging protocol to the preferred tagging format of this
  125. * switch.
  126. */
  127. if (ds->dst->cpu_switch == index)
  128. ds->dst->tag_protocol = drv->tag_protocol;
  129. /*
  130. * Do basic register setup.
  131. */
  132. ret = drv->setup(ds);
  133. if (ret < 0)
  134. goto out;
  135. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  136. if (ret < 0)
  137. goto out;
  138. ds->slave_mii_bus = mdiobus_alloc();
  139. if (ds->slave_mii_bus == NULL) {
  140. ret = -ENOMEM;
  141. goto out;
  142. }
  143. dsa_slave_mii_bus_init(ds);
  144. ret = mdiobus_register(ds->slave_mii_bus);
  145. if (ret < 0)
  146. goto out_free;
  147. /*
  148. * Create network devices for physical switch ports.
  149. */
  150. for (i = 0; i < DSA_MAX_PORTS; i++) {
  151. struct net_device *slave_dev;
  152. if (!(ds->phys_port_mask & (1 << i)))
  153. continue;
  154. slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  155. if (slave_dev == NULL) {
  156. printk(KERN_ERR "%s[%d]: can't create dsa "
  157. "slave device for port %d(%s)\n",
  158. dst->master_netdev->name,
  159. index, i, pd->port_names[i]);
  160. continue;
  161. }
  162. ds->ports[i] = slave_dev;
  163. }
  164. return ds;
  165. out_free:
  166. mdiobus_free(ds->slave_mii_bus);
  167. out:
  168. kfree(ds);
  169. return ERR_PTR(ret);
  170. }
  171. static void dsa_switch_destroy(struct dsa_switch *ds)
  172. {
  173. }
  174. /* link polling *************************************************************/
  175. static void dsa_link_poll_work(struct work_struct *ugly)
  176. {
  177. struct dsa_switch_tree *dst;
  178. int i;
  179. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  180. for (i = 0; i < dst->pd->nr_chips; i++) {
  181. struct dsa_switch *ds = dst->ds[i];
  182. if (ds != NULL && ds->drv->poll_link != NULL)
  183. ds->drv->poll_link(ds);
  184. }
  185. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  186. }
  187. static void dsa_link_poll_timer(unsigned long _dst)
  188. {
  189. struct dsa_switch_tree *dst = (void *)_dst;
  190. schedule_work(&dst->link_poll_work);
  191. }
  192. /* platform driver init and cleanup *****************************************/
  193. static int dev_is_class(struct device *dev, void *class)
  194. {
  195. if (dev->class != NULL && !strcmp(dev->class->name, class))
  196. return 1;
  197. return 0;
  198. }
  199. static struct device *dev_find_class(struct device *parent, char *class)
  200. {
  201. if (dev_is_class(parent, class)) {
  202. get_device(parent);
  203. return parent;
  204. }
  205. return device_find_child(parent, class, dev_is_class);
  206. }
  207. static struct mii_bus *dev_to_mii_bus(struct device *dev)
  208. {
  209. struct device *d;
  210. d = dev_find_class(dev, "mdio_bus");
  211. if (d != NULL) {
  212. struct mii_bus *bus;
  213. bus = to_mii_bus(d);
  214. put_device(d);
  215. return bus;
  216. }
  217. return NULL;
  218. }
  219. static struct net_device *dev_to_net_device(struct device *dev)
  220. {
  221. struct device *d;
  222. d = dev_find_class(dev, "net");
  223. if (d != NULL) {
  224. struct net_device *nd;
  225. nd = to_net_dev(d);
  226. dev_hold(nd);
  227. put_device(d);
  228. return nd;
  229. }
  230. return NULL;
  231. }
  232. #ifdef CONFIG_OF
  233. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  234. struct dsa_chip_data *cd,
  235. int chip_index,
  236. struct device_node *link)
  237. {
  238. int ret;
  239. const __be32 *reg;
  240. int link_port_addr;
  241. int link_sw_addr;
  242. struct device_node *parent_sw;
  243. int len;
  244. parent_sw = of_get_parent(link);
  245. if (!parent_sw)
  246. return -EINVAL;
  247. reg = of_get_property(parent_sw, "reg", &len);
  248. if (!reg || (len != sizeof(*reg) * 2))
  249. return -EINVAL;
  250. link_sw_addr = be32_to_cpup(reg + 1);
  251. if (link_sw_addr >= pd->nr_chips)
  252. return -EINVAL;
  253. /* First time routing table allocation */
  254. if (!cd->rtable) {
  255. cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL);
  256. if (!cd->rtable)
  257. return -ENOMEM;
  258. /* default to no valid uplink/downlink */
  259. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  260. }
  261. reg = of_get_property(link, "reg", NULL);
  262. if (!reg) {
  263. ret = -EINVAL;
  264. goto out;
  265. }
  266. link_port_addr = be32_to_cpup(reg);
  267. cd->rtable[link_sw_addr] = link_port_addr;
  268. return 0;
  269. out:
  270. kfree(cd->rtable);
  271. return ret;
  272. }
  273. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  274. {
  275. int i;
  276. int port_index;
  277. for (i = 0; i < pd->nr_chips; i++) {
  278. port_index = 0;
  279. while (port_index < DSA_MAX_PORTS) {
  280. if (pd->chip[i].port_names[port_index])
  281. kfree(pd->chip[i].port_names[port_index]);
  282. port_index++;
  283. }
  284. kfree(pd->chip[i].rtable);
  285. }
  286. kfree(pd->chip);
  287. }
  288. static int dsa_of_probe(struct platform_device *pdev)
  289. {
  290. struct device_node *np = pdev->dev.of_node;
  291. struct device_node *child, *mdio, *ethernet, *port, *link;
  292. struct mii_bus *mdio_bus;
  293. struct platform_device *ethernet_dev;
  294. struct dsa_platform_data *pd;
  295. struct dsa_chip_data *cd;
  296. const char *port_name;
  297. int chip_index, port_index;
  298. const unsigned int *sw_addr, *port_reg;
  299. int ret;
  300. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  301. if (!mdio)
  302. return -EINVAL;
  303. mdio_bus = of_mdio_find_bus(mdio);
  304. if (!mdio_bus)
  305. return -EINVAL;
  306. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  307. if (!ethernet)
  308. return -EINVAL;
  309. ethernet_dev = of_find_device_by_node(ethernet);
  310. if (!ethernet_dev)
  311. return -ENODEV;
  312. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  313. if (!pd)
  314. return -ENOMEM;
  315. pdev->dev.platform_data = pd;
  316. pd->netdev = &ethernet_dev->dev;
  317. pd->nr_chips = of_get_child_count(np);
  318. if (pd->nr_chips > DSA_MAX_SWITCHES)
  319. pd->nr_chips = DSA_MAX_SWITCHES;
  320. pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data),
  321. GFP_KERNEL);
  322. if (!pd->chip) {
  323. ret = -ENOMEM;
  324. goto out_free;
  325. }
  326. chip_index = 0;
  327. for_each_available_child_of_node(np, child) {
  328. cd = &pd->chip[chip_index];
  329. cd->mii_bus = &mdio_bus->dev;
  330. sw_addr = of_get_property(child, "reg", NULL);
  331. if (!sw_addr)
  332. continue;
  333. cd->sw_addr = be32_to_cpup(sw_addr);
  334. if (cd->sw_addr > PHY_MAX_ADDR)
  335. continue;
  336. for_each_available_child_of_node(child, port) {
  337. port_reg = of_get_property(port, "reg", NULL);
  338. if (!port_reg)
  339. continue;
  340. port_index = be32_to_cpup(port_reg);
  341. port_name = of_get_property(port, "label", NULL);
  342. if (!port_name)
  343. continue;
  344. cd->port_names[port_index] = kstrdup(port_name,
  345. GFP_KERNEL);
  346. if (!cd->port_names[port_index]) {
  347. ret = -ENOMEM;
  348. goto out_free_chip;
  349. }
  350. link = of_parse_phandle(port, "link", 0);
  351. if (!strcmp(port_name, "dsa") && link &&
  352. pd->nr_chips > 1) {
  353. ret = dsa_of_setup_routing_table(pd, cd,
  354. chip_index, link);
  355. if (ret)
  356. goto out_free_chip;
  357. }
  358. if (port_index == DSA_MAX_PORTS)
  359. break;
  360. }
  361. }
  362. return 0;
  363. out_free_chip:
  364. dsa_of_free_platform_data(pd);
  365. out_free:
  366. kfree(pd);
  367. pdev->dev.platform_data = NULL;
  368. return ret;
  369. }
  370. static void dsa_of_remove(struct platform_device *pdev)
  371. {
  372. struct dsa_platform_data *pd = pdev->dev.platform_data;
  373. if (!pdev->dev.of_node)
  374. return;
  375. dsa_of_free_platform_data(pd);
  376. kfree(pd);
  377. }
  378. #else
  379. static inline int dsa_of_probe(struct platform_device *pdev)
  380. {
  381. return 0;
  382. }
  383. static inline void dsa_of_remove(struct platform_device *pdev)
  384. {
  385. }
  386. #endif
  387. static int dsa_probe(struct platform_device *pdev)
  388. {
  389. static int dsa_version_printed;
  390. struct dsa_platform_data *pd = pdev->dev.platform_data;
  391. struct net_device *dev;
  392. struct dsa_switch_tree *dst;
  393. int i, ret;
  394. if (!dsa_version_printed++)
  395. printk(KERN_NOTICE "Distributed Switch Architecture "
  396. "driver version %s\n", dsa_driver_version);
  397. if (pdev->dev.of_node) {
  398. ret = dsa_of_probe(pdev);
  399. if (ret)
  400. return ret;
  401. pd = pdev->dev.platform_data;
  402. }
  403. if (pd == NULL || pd->netdev == NULL)
  404. return -EINVAL;
  405. dev = dev_to_net_device(pd->netdev);
  406. if (dev == NULL) {
  407. ret = -EINVAL;
  408. goto out;
  409. }
  410. if (dev->dsa_ptr != NULL) {
  411. dev_put(dev);
  412. ret = -EEXIST;
  413. goto out;
  414. }
  415. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  416. if (dst == NULL) {
  417. dev_put(dev);
  418. ret = -ENOMEM;
  419. goto out;
  420. }
  421. platform_set_drvdata(pdev, dst);
  422. dst->pd = pd;
  423. dst->master_netdev = dev;
  424. dst->cpu_switch = -1;
  425. dst->cpu_port = -1;
  426. for (i = 0; i < pd->nr_chips; i++) {
  427. struct mii_bus *bus;
  428. struct dsa_switch *ds;
  429. bus = dev_to_mii_bus(pd->chip[i].mii_bus);
  430. if (bus == NULL) {
  431. printk(KERN_ERR "%s[%d]: no mii bus found for "
  432. "dsa switch\n", dev->name, i);
  433. continue;
  434. }
  435. ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
  436. if (IS_ERR(ds)) {
  437. printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
  438. "instance (error %ld)\n", dev->name, i,
  439. PTR_ERR(ds));
  440. continue;
  441. }
  442. dst->ds[i] = ds;
  443. if (ds->drv->poll_link != NULL)
  444. dst->link_poll_needed = 1;
  445. }
  446. /*
  447. * If we use a tagging format that doesn't have an ethertype
  448. * field, make sure that all packets from this point on get
  449. * sent to the tag format's receive function.
  450. */
  451. wmb();
  452. dev->dsa_ptr = (void *)dst;
  453. if (dst->link_poll_needed) {
  454. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  455. init_timer(&dst->link_poll_timer);
  456. dst->link_poll_timer.data = (unsigned long)dst;
  457. dst->link_poll_timer.function = dsa_link_poll_timer;
  458. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  459. add_timer(&dst->link_poll_timer);
  460. }
  461. return 0;
  462. out:
  463. dsa_of_remove(pdev);
  464. return ret;
  465. }
  466. static int dsa_remove(struct platform_device *pdev)
  467. {
  468. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  469. int i;
  470. if (dst->link_poll_needed)
  471. del_timer_sync(&dst->link_poll_timer);
  472. flush_work(&dst->link_poll_work);
  473. for (i = 0; i < dst->pd->nr_chips; i++) {
  474. struct dsa_switch *ds = dst->ds[i];
  475. if (ds != NULL)
  476. dsa_switch_destroy(ds);
  477. }
  478. dsa_of_remove(pdev);
  479. return 0;
  480. }
  481. static void dsa_shutdown(struct platform_device *pdev)
  482. {
  483. }
  484. static const struct of_device_id dsa_of_match_table[] = {
  485. { .compatible = "marvell,dsa", },
  486. {}
  487. };
  488. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  489. static struct platform_driver dsa_driver = {
  490. .probe = dsa_probe,
  491. .remove = dsa_remove,
  492. .shutdown = dsa_shutdown,
  493. .driver = {
  494. .name = "dsa",
  495. .owner = THIS_MODULE,
  496. .of_match_table = dsa_of_match_table,
  497. },
  498. };
  499. static int __init dsa_init_module(void)
  500. {
  501. int rc;
  502. rc = platform_driver_register(&dsa_driver);
  503. if (rc)
  504. return rc;
  505. #ifdef CONFIG_NET_DSA_TAG_DSA
  506. dev_add_pack(&dsa_packet_type);
  507. #endif
  508. #ifdef CONFIG_NET_DSA_TAG_EDSA
  509. dev_add_pack(&edsa_packet_type);
  510. #endif
  511. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  512. dev_add_pack(&trailer_packet_type);
  513. #endif
  514. return 0;
  515. }
  516. module_init(dsa_init_module);
  517. static void __exit dsa_cleanup_module(void)
  518. {
  519. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  520. dev_remove_pack(&trailer_packet_type);
  521. #endif
  522. #ifdef CONFIG_NET_DSA_TAG_EDSA
  523. dev_remove_pack(&edsa_packet_type);
  524. #endif
  525. #ifdef CONFIG_NET_DSA_TAG_DSA
  526. dev_remove_pack(&dsa_packet_type);
  527. #endif
  528. platform_driver_unregister(&dsa_driver);
  529. }
  530. module_exit(dsa_cleanup_module);
  531. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  532. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  533. MODULE_LICENSE("GPL");
  534. MODULE_ALIAS("platform:dsa");