dsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. /* Make the built-in MII bus mask match the number of ports,
  123. * switch drivers can override this later
  124. */
  125. ds->phys_mii_mask = ds->phys_port_mask;
  126. /*
  127. * If the CPU connects to this switch, set the switch tree
  128. * tagging protocol to the preferred tagging format of this
  129. * switch.
  130. */
  131. if (ds->dst->cpu_switch == index)
  132. ds->dst->tag_protocol = drv->tag_protocol;
  133. /*
  134. * Do basic register setup.
  135. */
  136. ret = drv->setup(ds);
  137. if (ret < 0)
  138. goto out;
  139. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  140. if (ret < 0)
  141. goto out;
  142. ds->slave_mii_bus = mdiobus_alloc();
  143. if (ds->slave_mii_bus == NULL) {
  144. ret = -ENOMEM;
  145. goto out;
  146. }
  147. dsa_slave_mii_bus_init(ds);
  148. ret = mdiobus_register(ds->slave_mii_bus);
  149. if (ret < 0)
  150. goto out_free;
  151. /*
  152. * Create network devices for physical switch ports.
  153. */
  154. for (i = 0; i < DSA_MAX_PORTS; i++) {
  155. struct net_device *slave_dev;
  156. if (!(ds->phys_port_mask & (1 << i)))
  157. continue;
  158. slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  159. if (slave_dev == NULL) {
  160. printk(KERN_ERR "%s[%d]: can't create dsa "
  161. "slave device for port %d(%s)\n",
  162. dst->master_netdev->name,
  163. index, i, pd->port_names[i]);
  164. continue;
  165. }
  166. ds->ports[i] = slave_dev;
  167. }
  168. return ds;
  169. out_free:
  170. mdiobus_free(ds->slave_mii_bus);
  171. out:
  172. kfree(ds);
  173. return ERR_PTR(ret);
  174. }
  175. static void dsa_switch_destroy(struct dsa_switch *ds)
  176. {
  177. }
  178. /* link polling *************************************************************/
  179. static void dsa_link_poll_work(struct work_struct *ugly)
  180. {
  181. struct dsa_switch_tree *dst;
  182. int i;
  183. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  184. for (i = 0; i < dst->pd->nr_chips; i++) {
  185. struct dsa_switch *ds = dst->ds[i];
  186. if (ds != NULL && ds->drv->poll_link != NULL)
  187. ds->drv->poll_link(ds);
  188. }
  189. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  190. }
  191. static void dsa_link_poll_timer(unsigned long _dst)
  192. {
  193. struct dsa_switch_tree *dst = (void *)_dst;
  194. schedule_work(&dst->link_poll_work);
  195. }
  196. /* platform driver init and cleanup *****************************************/
  197. static int dev_is_class(struct device *dev, void *class)
  198. {
  199. if (dev->class != NULL && !strcmp(dev->class->name, class))
  200. return 1;
  201. return 0;
  202. }
  203. static struct device *dev_find_class(struct device *parent, char *class)
  204. {
  205. if (dev_is_class(parent, class)) {
  206. get_device(parent);
  207. return parent;
  208. }
  209. return device_find_child(parent, class, dev_is_class);
  210. }
  211. static struct mii_bus *dev_to_mii_bus(struct device *dev)
  212. {
  213. struct device *d;
  214. d = dev_find_class(dev, "mdio_bus");
  215. if (d != NULL) {
  216. struct mii_bus *bus;
  217. bus = to_mii_bus(d);
  218. put_device(d);
  219. return bus;
  220. }
  221. return NULL;
  222. }
  223. static struct net_device *dev_to_net_device(struct device *dev)
  224. {
  225. struct device *d;
  226. d = dev_find_class(dev, "net");
  227. if (d != NULL) {
  228. struct net_device *nd;
  229. nd = to_net_dev(d);
  230. dev_hold(nd);
  231. put_device(d);
  232. return nd;
  233. }
  234. return NULL;
  235. }
  236. #ifdef CONFIG_OF
  237. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  238. struct dsa_chip_data *cd,
  239. int chip_index,
  240. struct device_node *link)
  241. {
  242. int ret;
  243. const __be32 *reg;
  244. int link_port_addr;
  245. int link_sw_addr;
  246. struct device_node *parent_sw;
  247. int len;
  248. parent_sw = of_get_parent(link);
  249. if (!parent_sw)
  250. return -EINVAL;
  251. reg = of_get_property(parent_sw, "reg", &len);
  252. if (!reg || (len != sizeof(*reg) * 2))
  253. return -EINVAL;
  254. link_sw_addr = be32_to_cpup(reg + 1);
  255. if (link_sw_addr >= pd->nr_chips)
  256. return -EINVAL;
  257. /* First time routing table allocation */
  258. if (!cd->rtable) {
  259. cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL);
  260. if (!cd->rtable)
  261. return -ENOMEM;
  262. /* default to no valid uplink/downlink */
  263. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  264. }
  265. reg = of_get_property(link, "reg", NULL);
  266. if (!reg) {
  267. ret = -EINVAL;
  268. goto out;
  269. }
  270. link_port_addr = be32_to_cpup(reg);
  271. cd->rtable[link_sw_addr] = link_port_addr;
  272. return 0;
  273. out:
  274. kfree(cd->rtable);
  275. return ret;
  276. }
  277. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  278. {
  279. int i;
  280. int port_index;
  281. for (i = 0; i < pd->nr_chips; i++) {
  282. port_index = 0;
  283. while (port_index < DSA_MAX_PORTS) {
  284. kfree(pd->chip[i].port_names[port_index]);
  285. port_index++;
  286. }
  287. kfree(pd->chip[i].rtable);
  288. }
  289. kfree(pd->chip);
  290. }
  291. static int dsa_of_probe(struct platform_device *pdev)
  292. {
  293. struct device_node *np = pdev->dev.of_node;
  294. struct device_node *child, *mdio, *ethernet, *port, *link;
  295. struct mii_bus *mdio_bus;
  296. struct platform_device *ethernet_dev;
  297. struct dsa_platform_data *pd;
  298. struct dsa_chip_data *cd;
  299. const char *port_name;
  300. int chip_index, port_index;
  301. const unsigned int *sw_addr, *port_reg;
  302. int ret;
  303. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  304. if (!mdio)
  305. return -EINVAL;
  306. mdio_bus = of_mdio_find_bus(mdio);
  307. if (!mdio_bus)
  308. return -EINVAL;
  309. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  310. if (!ethernet)
  311. return -EINVAL;
  312. ethernet_dev = of_find_device_by_node(ethernet);
  313. if (!ethernet_dev)
  314. return -ENODEV;
  315. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  316. if (!pd)
  317. return -ENOMEM;
  318. pdev->dev.platform_data = pd;
  319. pd->netdev = &ethernet_dev->dev;
  320. pd->nr_chips = of_get_child_count(np);
  321. if (pd->nr_chips > DSA_MAX_SWITCHES)
  322. pd->nr_chips = DSA_MAX_SWITCHES;
  323. pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data),
  324. GFP_KERNEL);
  325. if (!pd->chip) {
  326. ret = -ENOMEM;
  327. goto out_free;
  328. }
  329. chip_index = -1;
  330. for_each_available_child_of_node(np, child) {
  331. chip_index++;
  332. cd = &pd->chip[chip_index];
  333. cd->of_node = child;
  334. cd->mii_bus = &mdio_bus->dev;
  335. sw_addr = of_get_property(child, "reg", NULL);
  336. if (!sw_addr)
  337. continue;
  338. cd->sw_addr = be32_to_cpup(sw_addr);
  339. if (cd->sw_addr > PHY_MAX_ADDR)
  340. continue;
  341. for_each_available_child_of_node(child, port) {
  342. port_reg = of_get_property(port, "reg", NULL);
  343. if (!port_reg)
  344. continue;
  345. port_index = be32_to_cpup(port_reg);
  346. port_name = of_get_property(port, "label", NULL);
  347. if (!port_name)
  348. continue;
  349. cd->port_dn[port_index] = port;
  350. cd->port_names[port_index] = kstrdup(port_name,
  351. GFP_KERNEL);
  352. if (!cd->port_names[port_index]) {
  353. ret = -ENOMEM;
  354. goto out_free_chip;
  355. }
  356. link = of_parse_phandle(port, "link", 0);
  357. if (!strcmp(port_name, "dsa") && link &&
  358. pd->nr_chips > 1) {
  359. ret = dsa_of_setup_routing_table(pd, cd,
  360. chip_index, link);
  361. if (ret)
  362. goto out_free_chip;
  363. }
  364. if (port_index == DSA_MAX_PORTS)
  365. break;
  366. }
  367. }
  368. return 0;
  369. out_free_chip:
  370. dsa_of_free_platform_data(pd);
  371. out_free:
  372. kfree(pd);
  373. pdev->dev.platform_data = NULL;
  374. return ret;
  375. }
  376. static void dsa_of_remove(struct platform_device *pdev)
  377. {
  378. struct dsa_platform_data *pd = pdev->dev.platform_data;
  379. if (!pdev->dev.of_node)
  380. return;
  381. dsa_of_free_platform_data(pd);
  382. kfree(pd);
  383. }
  384. #else
  385. static inline int dsa_of_probe(struct platform_device *pdev)
  386. {
  387. return 0;
  388. }
  389. static inline void dsa_of_remove(struct platform_device *pdev)
  390. {
  391. }
  392. #endif
  393. static int dsa_probe(struct platform_device *pdev)
  394. {
  395. static int dsa_version_printed;
  396. struct dsa_platform_data *pd = pdev->dev.platform_data;
  397. struct net_device *dev;
  398. struct dsa_switch_tree *dst;
  399. int i, ret;
  400. if (!dsa_version_printed++)
  401. printk(KERN_NOTICE "Distributed Switch Architecture "
  402. "driver version %s\n", dsa_driver_version);
  403. if (pdev->dev.of_node) {
  404. ret = dsa_of_probe(pdev);
  405. if (ret)
  406. return ret;
  407. pd = pdev->dev.platform_data;
  408. }
  409. if (pd == NULL || pd->netdev == NULL)
  410. return -EINVAL;
  411. dev = dev_to_net_device(pd->netdev);
  412. if (dev == NULL) {
  413. ret = -EINVAL;
  414. goto out;
  415. }
  416. if (dev->dsa_ptr != NULL) {
  417. dev_put(dev);
  418. ret = -EEXIST;
  419. goto out;
  420. }
  421. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  422. if (dst == NULL) {
  423. dev_put(dev);
  424. ret = -ENOMEM;
  425. goto out;
  426. }
  427. platform_set_drvdata(pdev, dst);
  428. dst->pd = pd;
  429. dst->master_netdev = dev;
  430. dst->cpu_switch = -1;
  431. dst->cpu_port = -1;
  432. for (i = 0; i < pd->nr_chips; i++) {
  433. struct mii_bus *bus;
  434. struct dsa_switch *ds;
  435. bus = dev_to_mii_bus(pd->chip[i].mii_bus);
  436. if (bus == NULL) {
  437. printk(KERN_ERR "%s[%d]: no mii bus found for "
  438. "dsa switch\n", dev->name, i);
  439. continue;
  440. }
  441. ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
  442. if (IS_ERR(ds)) {
  443. printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
  444. "instance (error %ld)\n", dev->name, i,
  445. PTR_ERR(ds));
  446. continue;
  447. }
  448. dst->ds[i] = ds;
  449. if (ds->drv->poll_link != NULL)
  450. dst->link_poll_needed = 1;
  451. }
  452. /*
  453. * If we use a tagging format that doesn't have an ethertype
  454. * field, make sure that all packets from this point on get
  455. * sent to the tag format's receive function.
  456. */
  457. wmb();
  458. dev->dsa_ptr = (void *)dst;
  459. if (dst->link_poll_needed) {
  460. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  461. init_timer(&dst->link_poll_timer);
  462. dst->link_poll_timer.data = (unsigned long)dst;
  463. dst->link_poll_timer.function = dsa_link_poll_timer;
  464. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  465. add_timer(&dst->link_poll_timer);
  466. }
  467. return 0;
  468. out:
  469. dsa_of_remove(pdev);
  470. return ret;
  471. }
  472. static int dsa_remove(struct platform_device *pdev)
  473. {
  474. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  475. int i;
  476. if (dst->link_poll_needed)
  477. del_timer_sync(&dst->link_poll_timer);
  478. flush_work(&dst->link_poll_work);
  479. for (i = 0; i < dst->pd->nr_chips; i++) {
  480. struct dsa_switch *ds = dst->ds[i];
  481. if (ds != NULL)
  482. dsa_switch_destroy(ds);
  483. }
  484. dsa_of_remove(pdev);
  485. return 0;
  486. }
  487. static void dsa_shutdown(struct platform_device *pdev)
  488. {
  489. }
  490. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  491. struct packet_type *pt, struct net_device *orig_dev)
  492. {
  493. struct dsa_switch_tree *dst = dev->dsa_ptr;
  494. if (unlikely(dst == NULL)) {
  495. kfree_skb(skb);
  496. return 0;
  497. }
  498. return dst->ops->rcv(skb, dev, pt, orig_dev);
  499. }
  500. static struct packet_type dsa_pack_type __read_mostly = {
  501. .type = cpu_to_be16(ETH_P_XDSA),
  502. .func = dsa_switch_rcv,
  503. };
  504. static const struct of_device_id dsa_of_match_table[] = {
  505. { .compatible = "brcm,bcm7445-switch-v4.0" },
  506. { .compatible = "marvell,dsa", },
  507. {}
  508. };
  509. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  510. static struct platform_driver dsa_driver = {
  511. .probe = dsa_probe,
  512. .remove = dsa_remove,
  513. .shutdown = dsa_shutdown,
  514. .driver = {
  515. .name = "dsa",
  516. .owner = THIS_MODULE,
  517. .of_match_table = dsa_of_match_table,
  518. },
  519. };
  520. static int __init dsa_init_module(void)
  521. {
  522. int rc;
  523. rc = platform_driver_register(&dsa_driver);
  524. if (rc)
  525. return rc;
  526. dev_add_pack(&dsa_pack_type);
  527. return 0;
  528. }
  529. module_init(dsa_init_module);
  530. static void __exit dsa_cleanup_module(void)
  531. {
  532. dev_remove_pack(&dsa_pack_type);
  533. platform_driver_unregister(&dsa_driver);
  534. }
  535. module_exit(dsa_cleanup_module);
  536. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  537. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  538. MODULE_LICENSE("GPL");
  539. MODULE_ALIAS("platform:dsa");