dsa.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /* link polling *************************************************************/
  163. static void dsa_link_poll_work(struct work_struct *ugly)
  164. {
  165. struct dsa_switch_tree *dst;
  166. int i;
  167. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  168. for (i = 0; i < dst->pd->nr_chips; i++) {
  169. struct dsa_switch *ds = dst->ds[i];
  170. if (ds != NULL && ds->drv->poll_link != NULL)
  171. ds->drv->poll_link(ds);
  172. }
  173. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  174. }
  175. static void dsa_link_poll_timer(unsigned long _dst)
  176. {
  177. struct dsa_switch_tree *dst = (void *)_dst;
  178. schedule_work(&dst->link_poll_work);
  179. }
  180. /* platform driver init and cleanup *****************************************/
  181. static int dev_is_class(struct device *dev, void *class)
  182. {
  183. if (dev->class != NULL && !strcmp(dev->class->name, class))
  184. return 1;
  185. return 0;
  186. }
  187. static struct device *dev_find_class(struct device *parent, char *class)
  188. {
  189. if (dev_is_class(parent, class)) {
  190. get_device(parent);
  191. return parent;
  192. }
  193. return device_find_child(parent, class, dev_is_class);
  194. }
  195. static struct mii_bus *dev_to_mii_bus(struct device *dev)
  196. {
  197. struct device *d;
  198. d = dev_find_class(dev, "mdio_bus");
  199. if (d != NULL) {
  200. struct mii_bus *bus;
  201. bus = to_mii_bus(d);
  202. put_device(d);
  203. return bus;
  204. }
  205. return NULL;
  206. }
  207. static struct net_device *dev_to_net_device(struct device *dev)
  208. {
  209. struct device *d;
  210. d = dev_find_class(dev, "net");
  211. if (d != NULL) {
  212. struct net_device *nd;
  213. nd = to_net_dev(d);
  214. dev_hold(nd);
  215. put_device(d);
  216. return nd;
  217. }
  218. return NULL;
  219. }
  220. static int dsa_probe(struct platform_device *pdev)
  221. {
  222. static int dsa_version_printed;
  223. struct dsa_platform_data *pd = pdev->dev.platform_data;
  224. struct net_device *dev;
  225. struct dsa_switch_tree *dst;
  226. int i;
  227. if (!dsa_version_printed++)
  228. printk(KERN_NOTICE "Distributed Switch Architecture "
  229. "driver version %s\n", dsa_driver_version);
  230. if (pd == NULL || pd->netdev == NULL)
  231. return -EINVAL;
  232. dev = dev_to_net_device(pd->netdev);
  233. if (dev == NULL)
  234. return -EINVAL;
  235. if (dev->dsa_ptr != NULL) {
  236. dev_put(dev);
  237. return -EEXIST;
  238. }
  239. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  240. if (dst == NULL) {
  241. dev_put(dev);
  242. return -ENOMEM;
  243. }
  244. platform_set_drvdata(pdev, dst);
  245. dst->pd = pd;
  246. dst->master_netdev = dev;
  247. dst->cpu_switch = -1;
  248. dst->cpu_port = -1;
  249. for (i = 0; i < pd->nr_chips; i++) {
  250. struct mii_bus *bus;
  251. struct dsa_switch *ds;
  252. bus = dev_to_mii_bus(pd->chip[i].mii_bus);
  253. if (bus == NULL) {
  254. printk(KERN_ERR "%s[%d]: no mii bus found for "
  255. "dsa switch\n", dev->name, i);
  256. continue;
  257. }
  258. ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
  259. if (IS_ERR(ds)) {
  260. printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
  261. "instance (error %ld)\n", dev->name, i,
  262. PTR_ERR(ds));
  263. continue;
  264. }
  265. dst->ds[i] = ds;
  266. if (ds->drv->poll_link != NULL)
  267. dst->link_poll_needed = 1;
  268. }
  269. /*
  270. * If we use a tagging format that doesn't have an ethertype
  271. * field, make sure that all packets from this point on get
  272. * sent to the tag format's receive function.
  273. */
  274. wmb();
  275. dev->dsa_ptr = (void *)dst;
  276. if (dst->link_poll_needed) {
  277. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  278. init_timer(&dst->link_poll_timer);
  279. dst->link_poll_timer.data = (unsigned long)dst;
  280. dst->link_poll_timer.function = dsa_link_poll_timer;
  281. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  282. add_timer(&dst->link_poll_timer);
  283. }
  284. return 0;
  285. }
  286. static int dsa_remove(struct platform_device *pdev)
  287. {
  288. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  289. int i;
  290. if (dst->link_poll_needed)
  291. del_timer_sync(&dst->link_poll_timer);
  292. flush_work_sync(&dst->link_poll_work);
  293. for (i = 0; i < dst->pd->nr_chips; i++) {
  294. struct dsa_switch *ds = dst->ds[i];
  295. if (ds != NULL)
  296. dsa_switch_destroy(ds);
  297. }
  298. return 0;
  299. }
  300. static void dsa_shutdown(struct platform_device *pdev)
  301. {
  302. }
  303. static struct platform_driver dsa_driver = {
  304. .probe = dsa_probe,
  305. .remove = dsa_remove,
  306. .shutdown = dsa_shutdown,
  307. .driver = {
  308. .name = "dsa",
  309. .owner = THIS_MODULE,
  310. },
  311. };
  312. static int __init dsa_init_module(void)
  313. {
  314. return platform_driver_register(&dsa_driver);
  315. }
  316. module_init(dsa_init_module);
  317. static void __exit dsa_cleanup_module(void)
  318. {
  319. platform_driver_unregister(&dsa_driver);
  320. }
  321. module_exit(dsa_cleanup_module);
  322. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  323. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  324. MODULE_LICENSE("GPL");
  325. MODULE_ALIAS("platform:dsa");