dsa.c 7.5 KB

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