dsa.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * net/dsa/dsa.c - Hardware switch handling
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/list.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/platform_device.h>
  13. #include <net/dsa.h>
  14. #include "dsa_priv.h"
  15. char dsa_driver_version[] = "0.1";
  16. /* switch driver registration ***********************************************/
  17. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  18. static LIST_HEAD(dsa_switch_drivers);
  19. void register_switch_driver(struct dsa_switch_driver *drv)
  20. {
  21. mutex_lock(&dsa_switch_drivers_mutex);
  22. list_add_tail(&drv->list, &dsa_switch_drivers);
  23. mutex_unlock(&dsa_switch_drivers_mutex);
  24. }
  25. void unregister_switch_driver(struct dsa_switch_driver *drv)
  26. {
  27. mutex_lock(&dsa_switch_drivers_mutex);
  28. list_del_init(&drv->list);
  29. mutex_unlock(&dsa_switch_drivers_mutex);
  30. }
  31. static struct dsa_switch_driver *
  32. dsa_switch_probe(struct mii_bus *bus, int sw_addr, char **_name)
  33. {
  34. struct dsa_switch_driver *ret;
  35. struct list_head *list;
  36. char *name;
  37. ret = NULL;
  38. name = NULL;
  39. mutex_lock(&dsa_switch_drivers_mutex);
  40. list_for_each(list, &dsa_switch_drivers) {
  41. struct dsa_switch_driver *drv;
  42. drv = list_entry(list, struct dsa_switch_driver, list);
  43. name = drv->probe(bus, sw_addr);
  44. if (name != NULL) {
  45. ret = drv;
  46. break;
  47. }
  48. }
  49. mutex_unlock(&dsa_switch_drivers_mutex);
  50. *_name = name;
  51. return ret;
  52. }
  53. /* basic switch operations **************************************************/
  54. static struct dsa_switch *
  55. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  56. struct device *parent, struct mii_bus *bus)
  57. {
  58. struct dsa_chip_data *pd = dst->pd->chip + index;
  59. struct dsa_switch_driver *drv;
  60. struct dsa_switch *ds;
  61. int ret;
  62. char *name;
  63. int i;
  64. /*
  65. * Probe for switch model.
  66. */
  67. drv = dsa_switch_probe(bus, pd->sw_addr, &name);
  68. if (drv == NULL) {
  69. printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
  70. dst->master_netdev->name, index);
  71. return ERR_PTR(-EINVAL);
  72. }
  73. printk(KERN_INFO "%s[%d]: detected a %s switch\n",
  74. dst->master_netdev->name, index, name);
  75. /*
  76. * Allocate and initialise switch state.
  77. */
  78. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  79. if (ds == NULL)
  80. return ERR_PTR(-ENOMEM);
  81. ds->dst = dst;
  82. ds->index = index;
  83. ds->pd = dst->pd->chip + index;
  84. ds->drv = drv;
  85. ds->master_mii_bus = bus;
  86. /*
  87. * Validate supplied switch configuration.
  88. */
  89. for (i = 0; i < DSA_MAX_PORTS; i++) {
  90. char *name;
  91. name = pd->port_names[i];
  92. if (name == NULL)
  93. continue;
  94. if (!strcmp(name, "cpu")) {
  95. if (dst->cpu_switch != -1) {
  96. printk(KERN_ERR "multiple cpu ports?!\n");
  97. ret = -EINVAL;
  98. goto out;
  99. }
  100. dst->cpu_switch = index;
  101. dst->cpu_port = i;
  102. } else if (!strcmp(name, "dsa")) {
  103. ds->dsa_port_mask |= 1 << i;
  104. } else {
  105. ds->phys_port_mask |= 1 << i;
  106. }
  107. }
  108. /*
  109. * If the CPU connects to this switch, set the switch tree
  110. * tagging protocol to the preferred tagging format of this
  111. * switch.
  112. */
  113. if (ds->dst->cpu_switch == index)
  114. ds->dst->tag_protocol = drv->tag_protocol;
  115. /*
  116. * Do basic register setup.
  117. */
  118. ret = drv->setup(ds);
  119. if (ret < 0)
  120. goto out;
  121. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  122. if (ret < 0)
  123. goto out;
  124. ds->slave_mii_bus = mdiobus_alloc();
  125. if (ds->slave_mii_bus == NULL) {
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. dsa_slave_mii_bus_init(ds);
  130. ret = mdiobus_register(ds->slave_mii_bus);
  131. if (ret < 0)
  132. goto out_free;
  133. /*
  134. * Create network devices for physical switch ports.
  135. */
  136. for (i = 0; i < DSA_MAX_PORTS; i++) {
  137. struct net_device *slave_dev;
  138. if (!(ds->phys_port_mask & (1 << i)))
  139. continue;
  140. slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  141. if (slave_dev == NULL) {
  142. printk(KERN_ERR "%s[%d]: can't create dsa "
  143. "slave device for port %d(%s)\n",
  144. dst->master_netdev->name,
  145. index, i, pd->port_names[i]);
  146. continue;
  147. }
  148. ds->ports[i] = slave_dev;
  149. }
  150. return ds;
  151. out_free:
  152. mdiobus_free(ds->slave_mii_bus);
  153. out:
  154. kfree(ds);
  155. return ERR_PTR(ret);
  156. }
  157. static void dsa_switch_destroy(struct dsa_switch *ds)
  158. {
  159. }
  160. /* hooks for ethertype-less tagging formats *********************************/
  161. /*
  162. * The original DSA tag format and some other tag formats have no
  163. * ethertype, which means that we need to add a little hack to the
  164. * networking receive path to make sure that received frames get
  165. * the right ->protocol assigned to them when one of those tag
  166. * formats is in use.
  167. */
  168. bool dsa_uses_dsa_tags(void *dsa_ptr)
  169. {
  170. struct dsa_switch_tree *dst = dsa_ptr;
  171. return !!(dst->tag_protocol == htons(ETH_P_DSA));
  172. }
  173. bool dsa_uses_trailer_tags(void *dsa_ptr)
  174. {
  175. struct dsa_switch_tree *dst = dsa_ptr;
  176. return !!(dst->tag_protocol == htons(ETH_P_TRAILER));
  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. static int dsa_probe(struct platform_device *pdev)
  237. {
  238. static int dsa_version_printed;
  239. struct dsa_platform_data *pd = pdev->dev.platform_data;
  240. struct net_device *dev;
  241. struct dsa_switch_tree *dst;
  242. int i;
  243. if (!dsa_version_printed++)
  244. printk(KERN_NOTICE "Distributed Switch Architecture "
  245. "driver version %s\n", dsa_driver_version);
  246. if (pd == NULL || pd->netdev == NULL)
  247. return -EINVAL;
  248. dev = dev_to_net_device(pd->netdev);
  249. if (dev == NULL)
  250. return -EINVAL;
  251. if (dev->dsa_ptr != NULL) {
  252. dev_put(dev);
  253. return -EEXIST;
  254. }
  255. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  256. if (dst == NULL) {
  257. dev_put(dev);
  258. return -ENOMEM;
  259. }
  260. platform_set_drvdata(pdev, dst);
  261. dst->pd = pd;
  262. dst->master_netdev = dev;
  263. dst->cpu_switch = -1;
  264. dst->cpu_port = -1;
  265. for (i = 0; i < pd->nr_chips; i++) {
  266. struct mii_bus *bus;
  267. struct dsa_switch *ds;
  268. bus = dev_to_mii_bus(pd->chip[i].mii_bus);
  269. if (bus == NULL) {
  270. printk(KERN_ERR "%s[%d]: no mii bus found for "
  271. "dsa switch\n", dev->name, i);
  272. continue;
  273. }
  274. ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
  275. if (IS_ERR(ds)) {
  276. printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
  277. "instance (error %ld)\n", dev->name, i,
  278. PTR_ERR(ds));
  279. continue;
  280. }
  281. dst->ds[i] = ds;
  282. if (ds->drv->poll_link != NULL)
  283. dst->link_poll_needed = 1;
  284. }
  285. /*
  286. * If we use a tagging format that doesn't have an ethertype
  287. * field, make sure that all packets from this point on get
  288. * sent to the tag format's receive function.
  289. */
  290. wmb();
  291. dev->dsa_ptr = (void *)dst;
  292. if (dst->link_poll_needed) {
  293. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  294. init_timer(&dst->link_poll_timer);
  295. dst->link_poll_timer.data = (unsigned long)dst;
  296. dst->link_poll_timer.function = dsa_link_poll_timer;
  297. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  298. add_timer(&dst->link_poll_timer);
  299. }
  300. return 0;
  301. }
  302. static int dsa_remove(struct platform_device *pdev)
  303. {
  304. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  305. int i;
  306. if (dst->link_poll_needed)
  307. del_timer_sync(&dst->link_poll_timer);
  308. flush_scheduled_work();
  309. for (i = 0; i < dst->pd->nr_chips; i++) {
  310. struct dsa_switch *ds = dst->ds[i];
  311. if (ds != NULL)
  312. dsa_switch_destroy(ds);
  313. }
  314. return 0;
  315. }
  316. static void dsa_shutdown(struct platform_device *pdev)
  317. {
  318. }
  319. static struct platform_driver dsa_driver = {
  320. .probe = dsa_probe,
  321. .remove = dsa_remove,
  322. .shutdown = dsa_shutdown,
  323. .driver = {
  324. .name = "dsa",
  325. .owner = THIS_MODULE,
  326. },
  327. };
  328. static int __init dsa_init_module(void)
  329. {
  330. return platform_driver_register(&dsa_driver);
  331. }
  332. module_init(dsa_init_module);
  333. static void __exit dsa_cleanup_module(void)
  334. {
  335. platform_driver_unregister(&dsa_driver);
  336. }
  337. module_exit(dsa_cleanup_module);
  338. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>")
  339. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  340. MODULE_LICENSE("GPL");
  341. MODULE_ALIAS("platform:dsa");