dsa.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * net/dsa/dsa.c - Hardware switch handling
  3. * Copyright (c) 2008 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 device *parent, struct dsa_platform_data *pd,
  56. struct mii_bus *bus, struct net_device *dev)
  57. {
  58. struct dsa_switch *ds;
  59. int ret;
  60. struct dsa_switch_driver *drv;
  61. char *name;
  62. int i;
  63. /*
  64. * Probe for switch model.
  65. */
  66. drv = dsa_switch_probe(bus, pd->sw_addr, &name);
  67. if (drv == NULL) {
  68. printk(KERN_ERR "%s: could not detect attached switch\n",
  69. dev->name);
  70. return ERR_PTR(-EINVAL);
  71. }
  72. printk(KERN_INFO "%s: detected a %s switch\n", dev->name, name);
  73. /*
  74. * Allocate and initialise switch state.
  75. */
  76. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  77. if (ds == NULL)
  78. return ERR_PTR(-ENOMEM);
  79. ds->pd = pd;
  80. ds->master_netdev = dev;
  81. ds->master_mii_bus = bus;
  82. ds->drv = drv;
  83. ds->tag_protocol = drv->tag_protocol;
  84. /*
  85. * Validate supplied switch configuration.
  86. */
  87. ds->cpu_port = -1;
  88. for (i = 0; i < DSA_MAX_PORTS; i++) {
  89. char *name;
  90. name = pd->port_names[i];
  91. if (name == NULL)
  92. continue;
  93. if (!strcmp(name, "cpu")) {
  94. if (ds->cpu_port != -1) {
  95. printk(KERN_ERR "multiple cpu ports?!\n");
  96. ret = -EINVAL;
  97. goto out;
  98. }
  99. ds->cpu_port = i;
  100. } else {
  101. ds->valid_port_mask |= 1 << i;
  102. }
  103. }
  104. if (ds->cpu_port == -1) {
  105. printk(KERN_ERR "no cpu port?!\n");
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. /*
  110. * If we use a tagging format that doesn't have an ethertype
  111. * field, make sure that all packets from this point on get
  112. * sent to the tag format's receive function. (Which will
  113. * discard received packets until we set ds->ports[] below.)
  114. */
  115. wmb();
  116. dev->dsa_ptr = (void *)ds;
  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, dev->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. wmb();
  139. for (i = 0; i < DSA_MAX_PORTS; i++) {
  140. struct net_device *slave_dev;
  141. if (!(ds->valid_port_mask & (1 << i)))
  142. continue;
  143. slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  144. if (slave_dev == NULL) {
  145. printk(KERN_ERR "%s: can't create dsa slave "
  146. "device for port %d(%s)\n",
  147. dev->name, 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. dev->dsa_ptr = NULL;
  157. kfree(ds);
  158. return ERR_PTR(ret);
  159. }
  160. static void dsa_switch_destroy(struct dsa_switch *ds)
  161. {
  162. }
  163. /* hooks for ethertype-less tagging formats *********************************/
  164. /*
  165. * The original DSA tag format and some other tag formats have no
  166. * ethertype, which means that we need to add a little hack to the
  167. * networking receive path to make sure that received frames get
  168. * the right ->protocol assigned to them when one of those tag
  169. * formats is in use.
  170. */
  171. bool dsa_uses_dsa_tags(void *dsa_ptr)
  172. {
  173. struct dsa_switch *ds = dsa_ptr;
  174. return !!(ds->tag_protocol == htons(ETH_P_DSA));
  175. }
  176. /* link polling *************************************************************/
  177. static void dsa_link_poll_work(struct work_struct *ugly)
  178. {
  179. struct dsa_switch *ds;
  180. ds = container_of(ugly, struct dsa_switch, link_poll_work);
  181. ds->drv->poll_link(ds);
  182. mod_timer(&ds->link_poll_timer, round_jiffies(jiffies + HZ));
  183. }
  184. static void dsa_link_poll_timer(unsigned long _ds)
  185. {
  186. struct dsa_switch *ds = (void *)_ds;
  187. schedule_work(&ds->link_poll_work);
  188. }
  189. /* platform driver init and cleanup *****************************************/
  190. static int dev_is_class(struct device *dev, void *class)
  191. {
  192. if (dev->class != NULL && !strcmp(dev->class->name, class))
  193. return 1;
  194. return 0;
  195. }
  196. static struct device *dev_find_class(struct device *parent, char *class)
  197. {
  198. if (dev_is_class(parent, class)) {
  199. get_device(parent);
  200. return parent;
  201. }
  202. return device_find_child(parent, class, dev_is_class);
  203. }
  204. static struct mii_bus *dev_to_mii_bus(struct device *dev)
  205. {
  206. struct device *d;
  207. d = dev_find_class(dev, "mdio_bus");
  208. if (d != NULL) {
  209. struct mii_bus *bus;
  210. bus = to_mii_bus(d);
  211. put_device(d);
  212. return bus;
  213. }
  214. return NULL;
  215. }
  216. static struct net_device *dev_to_net_device(struct device *dev)
  217. {
  218. struct device *d;
  219. d = dev_find_class(dev, "net");
  220. if (d != NULL) {
  221. struct net_device *nd;
  222. nd = to_net_dev(d);
  223. dev_hold(nd);
  224. put_device(d);
  225. return nd;
  226. }
  227. return NULL;
  228. }
  229. static int dsa_probe(struct platform_device *pdev)
  230. {
  231. static int dsa_version_printed;
  232. struct dsa_platform_data *pd = pdev->dev.platform_data;
  233. struct net_device *dev;
  234. struct mii_bus *bus;
  235. struct dsa_switch *ds;
  236. if (!dsa_version_printed++)
  237. printk(KERN_NOTICE "Distributed Switch Architecture "
  238. "driver version %s\n", dsa_driver_version);
  239. if (pd == NULL || pd->mii_bus == NULL || pd->netdev == NULL)
  240. return -EINVAL;
  241. bus = dev_to_mii_bus(pd->mii_bus);
  242. if (bus == NULL)
  243. return -EINVAL;
  244. dev = dev_to_net_device(pd->netdev);
  245. if (dev == NULL)
  246. return -EINVAL;
  247. if (dev->dsa_ptr != NULL) {
  248. dev_put(dev);
  249. return -EEXIST;
  250. }
  251. ds = dsa_switch_setup(&pdev->dev, pd, bus, dev);
  252. if (IS_ERR(ds)) {
  253. dev_put(dev);
  254. return PTR_ERR(ds);
  255. }
  256. if (ds->drv->poll_link != NULL) {
  257. INIT_WORK(&ds->link_poll_work, dsa_link_poll_work);
  258. init_timer(&ds->link_poll_timer);
  259. ds->link_poll_timer.data = (unsigned long)ds;
  260. ds->link_poll_timer.function = dsa_link_poll_timer;
  261. ds->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  262. add_timer(&ds->link_poll_timer);
  263. }
  264. platform_set_drvdata(pdev, ds);
  265. return 0;
  266. }
  267. static int dsa_remove(struct platform_device *pdev)
  268. {
  269. struct dsa_switch *ds = platform_get_drvdata(pdev);
  270. if (ds->drv->poll_link != NULL)
  271. del_timer_sync(&ds->link_poll_timer);
  272. flush_scheduled_work();
  273. dsa_switch_destroy(ds);
  274. return 0;
  275. }
  276. static void dsa_shutdown(struct platform_device *pdev)
  277. {
  278. }
  279. static struct platform_driver dsa_driver = {
  280. .probe = dsa_probe,
  281. .remove = dsa_remove,
  282. .shutdown = dsa_shutdown,
  283. .driver = {
  284. .name = "dsa",
  285. .owner = THIS_MODULE,
  286. },
  287. };
  288. static int __init dsa_init_module(void)
  289. {
  290. return platform_driver_register(&dsa_driver);
  291. }
  292. module_init(dsa_init_module);
  293. static void __exit dsa_cleanup_module(void)
  294. {
  295. platform_driver_unregister(&dsa_driver);
  296. }
  297. module_exit(dsa_cleanup_module);
  298. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>")
  299. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  300. MODULE_LICENSE("GPL");
  301. MODULE_ALIAS("platform:dsa");