dsa.c 9.2 KB

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