dsa.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. bool dsa_uses_trailer_tags(void *dsa_ptr)
  177. {
  178. struct dsa_switch *ds = dsa_ptr;
  179. return !!(ds->tag_protocol == htons(ETH_P_TRAILER));
  180. }
  181. /* link polling *************************************************************/
  182. static void dsa_link_poll_work(struct work_struct *ugly)
  183. {
  184. struct dsa_switch *ds;
  185. ds = container_of(ugly, struct dsa_switch, link_poll_work);
  186. ds->drv->poll_link(ds);
  187. mod_timer(&ds->link_poll_timer, round_jiffies(jiffies + HZ));
  188. }
  189. static void dsa_link_poll_timer(unsigned long _ds)
  190. {
  191. struct dsa_switch *ds = (void *)_ds;
  192. schedule_work(&ds->link_poll_work);
  193. }
  194. /* platform driver init and cleanup *****************************************/
  195. static int dev_is_class(struct device *dev, void *class)
  196. {
  197. if (dev->class != NULL && !strcmp(dev->class->name, class))
  198. return 1;
  199. return 0;
  200. }
  201. static struct device *dev_find_class(struct device *parent, char *class)
  202. {
  203. if (dev_is_class(parent, class)) {
  204. get_device(parent);
  205. return parent;
  206. }
  207. return device_find_child(parent, class, dev_is_class);
  208. }
  209. static struct mii_bus *dev_to_mii_bus(struct device *dev)
  210. {
  211. struct device *d;
  212. d = dev_find_class(dev, "mdio_bus");
  213. if (d != NULL) {
  214. struct mii_bus *bus;
  215. bus = to_mii_bus(d);
  216. put_device(d);
  217. return bus;
  218. }
  219. return NULL;
  220. }
  221. static struct net_device *dev_to_net_device(struct device *dev)
  222. {
  223. struct device *d;
  224. d = dev_find_class(dev, "net");
  225. if (d != NULL) {
  226. struct net_device *nd;
  227. nd = to_net_dev(d);
  228. dev_hold(nd);
  229. put_device(d);
  230. return nd;
  231. }
  232. return NULL;
  233. }
  234. static int dsa_probe(struct platform_device *pdev)
  235. {
  236. static int dsa_version_printed;
  237. struct dsa_platform_data *pd = pdev->dev.platform_data;
  238. struct net_device *dev;
  239. struct mii_bus *bus;
  240. struct dsa_switch *ds;
  241. if (!dsa_version_printed++)
  242. printk(KERN_NOTICE "Distributed Switch Architecture "
  243. "driver version %s\n", dsa_driver_version);
  244. if (pd == NULL || pd->mii_bus == NULL || pd->netdev == NULL)
  245. return -EINVAL;
  246. bus = dev_to_mii_bus(pd->mii_bus);
  247. if (bus == 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. ds = dsa_switch_setup(&pdev->dev, pd, bus, dev);
  257. if (IS_ERR(ds)) {
  258. dev_put(dev);
  259. return PTR_ERR(ds);
  260. }
  261. if (ds->drv->poll_link != NULL) {
  262. INIT_WORK(&ds->link_poll_work, dsa_link_poll_work);
  263. init_timer(&ds->link_poll_timer);
  264. ds->link_poll_timer.data = (unsigned long)ds;
  265. ds->link_poll_timer.function = dsa_link_poll_timer;
  266. ds->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  267. add_timer(&ds->link_poll_timer);
  268. }
  269. platform_set_drvdata(pdev, ds);
  270. return 0;
  271. }
  272. static int dsa_remove(struct platform_device *pdev)
  273. {
  274. struct dsa_switch *ds = platform_get_drvdata(pdev);
  275. if (ds->drv->poll_link != NULL)
  276. del_timer_sync(&ds->link_poll_timer);
  277. flush_scheduled_work();
  278. dsa_switch_destroy(ds);
  279. return 0;
  280. }
  281. static void dsa_shutdown(struct platform_device *pdev)
  282. {
  283. }
  284. static struct platform_driver dsa_driver = {
  285. .probe = dsa_probe,
  286. .remove = dsa_remove,
  287. .shutdown = dsa_shutdown,
  288. .driver = {
  289. .name = "dsa",
  290. .owner = THIS_MODULE,
  291. },
  292. };
  293. static int __init dsa_init_module(void)
  294. {
  295. return platform_driver_register(&dsa_driver);
  296. }
  297. module_init(dsa_init_module);
  298. static void __exit dsa_cleanup_module(void)
  299. {
  300. platform_driver_unregister(&dsa_driver);
  301. }
  302. module_exit(dsa_cleanup_module);
  303. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>")
  304. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  305. MODULE_LICENSE("GPL");
  306. MODULE_ALIAS("platform:dsa");