dsa.c 14 KB

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