dsa.c 9.2 KB

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