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