dsa.c 9.3 KB

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