dsa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * net/dsa/dsa.c - Hardware switch handling
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/ctype.h>
  12. #include <linux/device.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/list.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <net/dsa.h>
  19. #include <linux/of.h>
  20. #include <linux/of_mdio.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/sysfs.h>
  23. #include "dsa_priv.h"
  24. char dsa_driver_version[] = "0.1";
  25. /* switch driver registration ***********************************************/
  26. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  27. static LIST_HEAD(dsa_switch_drivers);
  28. void register_switch_driver(struct dsa_switch_driver *drv)
  29. {
  30. mutex_lock(&dsa_switch_drivers_mutex);
  31. list_add_tail(&drv->list, &dsa_switch_drivers);
  32. mutex_unlock(&dsa_switch_drivers_mutex);
  33. }
  34. EXPORT_SYMBOL_GPL(register_switch_driver);
  35. void unregister_switch_driver(struct dsa_switch_driver *drv)
  36. {
  37. mutex_lock(&dsa_switch_drivers_mutex);
  38. list_del_init(&drv->list);
  39. mutex_unlock(&dsa_switch_drivers_mutex);
  40. }
  41. EXPORT_SYMBOL_GPL(unregister_switch_driver);
  42. static struct dsa_switch_driver *
  43. dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
  44. {
  45. struct dsa_switch_driver *ret;
  46. struct list_head *list;
  47. char *name;
  48. ret = NULL;
  49. name = NULL;
  50. mutex_lock(&dsa_switch_drivers_mutex);
  51. list_for_each(list, &dsa_switch_drivers) {
  52. struct dsa_switch_driver *drv;
  53. drv = list_entry(list, struct dsa_switch_driver, list);
  54. name = drv->probe(host_dev, sw_addr);
  55. if (name != NULL) {
  56. ret = drv;
  57. break;
  58. }
  59. }
  60. mutex_unlock(&dsa_switch_drivers_mutex);
  61. *_name = name;
  62. return ret;
  63. }
  64. /* hwmon support ************************************************************/
  65. #ifdef CONFIG_NET_DSA_HWMON
  66. static ssize_t temp1_input_show(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct dsa_switch *ds = dev_get_drvdata(dev);
  70. int temp, ret;
  71. ret = ds->drv->get_temp(ds, &temp);
  72. if (ret < 0)
  73. return ret;
  74. return sprintf(buf, "%d\n", temp * 1000);
  75. }
  76. static DEVICE_ATTR_RO(temp1_input);
  77. static ssize_t temp1_max_show(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct dsa_switch *ds = dev_get_drvdata(dev);
  81. int temp, ret;
  82. ret = ds->drv->get_temp_limit(ds, &temp);
  83. if (ret < 0)
  84. return ret;
  85. return sprintf(buf, "%d\n", temp * 1000);
  86. }
  87. static ssize_t temp1_max_store(struct device *dev,
  88. struct device_attribute *attr, const char *buf,
  89. size_t count)
  90. {
  91. struct dsa_switch *ds = dev_get_drvdata(dev);
  92. int temp, ret;
  93. ret = kstrtoint(buf, 0, &temp);
  94. if (ret < 0)
  95. return ret;
  96. ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
  97. if (ret < 0)
  98. return ret;
  99. return count;
  100. }
  101. static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store);
  102. static ssize_t temp1_max_alarm_show(struct device *dev,
  103. struct device_attribute *attr, char *buf)
  104. {
  105. struct dsa_switch *ds = dev_get_drvdata(dev);
  106. bool alarm;
  107. int ret;
  108. ret = ds->drv->get_temp_alarm(ds, &alarm);
  109. if (ret < 0)
  110. return ret;
  111. return sprintf(buf, "%d\n", alarm);
  112. }
  113. static DEVICE_ATTR_RO(temp1_max_alarm);
  114. static struct attribute *dsa_hwmon_attrs[] = {
  115. &dev_attr_temp1_input.attr, /* 0 */
  116. &dev_attr_temp1_max.attr, /* 1 */
  117. &dev_attr_temp1_max_alarm.attr, /* 2 */
  118. NULL
  119. };
  120. static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
  121. struct attribute *attr, int index)
  122. {
  123. struct device *dev = container_of(kobj, struct device, kobj);
  124. struct dsa_switch *ds = dev_get_drvdata(dev);
  125. struct dsa_switch_driver *drv = ds->drv;
  126. umode_t mode = attr->mode;
  127. if (index == 1) {
  128. if (!drv->get_temp_limit)
  129. mode = 0;
  130. else if (drv->set_temp_limit)
  131. mode |= S_IWUSR;
  132. } else if (index == 2 && !drv->get_temp_alarm) {
  133. mode = 0;
  134. }
  135. return mode;
  136. }
  137. static const struct attribute_group dsa_hwmon_group = {
  138. .attrs = dsa_hwmon_attrs,
  139. .is_visible = dsa_hwmon_attrs_visible,
  140. };
  141. __ATTRIBUTE_GROUPS(dsa_hwmon);
  142. #endif /* CONFIG_NET_DSA_HWMON */
  143. /* basic switch operations **************************************************/
  144. static struct dsa_switch *
  145. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  146. struct device *parent, struct device *host_dev)
  147. {
  148. struct dsa_chip_data *pd = dst->pd->chip + index;
  149. struct dsa_switch_driver *drv;
  150. struct dsa_switch *ds;
  151. int ret;
  152. char *name;
  153. int i;
  154. bool valid_name_found = false;
  155. /*
  156. * Probe for switch model.
  157. */
  158. drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
  159. if (drv == NULL) {
  160. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  161. index);
  162. return ERR_PTR(-EINVAL);
  163. }
  164. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  165. index, name);
  166. /*
  167. * Allocate and initialise switch state.
  168. */
  169. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  170. if (ds == NULL)
  171. return ERR_PTR(-ENOMEM);
  172. ds->dst = dst;
  173. ds->index = index;
  174. ds->pd = dst->pd->chip + index;
  175. ds->drv = drv;
  176. ds->master_dev = host_dev;
  177. /*
  178. * Validate supplied switch configuration.
  179. */
  180. for (i = 0; i < DSA_MAX_PORTS; i++) {
  181. char *name;
  182. name = pd->port_names[i];
  183. if (name == NULL)
  184. continue;
  185. if (!strcmp(name, "cpu")) {
  186. if (dst->cpu_switch != -1) {
  187. netdev_err(dst->master_netdev,
  188. "multiple cpu ports?!\n");
  189. ret = -EINVAL;
  190. goto out;
  191. }
  192. dst->cpu_switch = index;
  193. dst->cpu_port = i;
  194. } else if (!strcmp(name, "dsa")) {
  195. ds->dsa_port_mask |= 1 << i;
  196. } else {
  197. ds->phys_port_mask |= 1 << i;
  198. }
  199. valid_name_found = true;
  200. }
  201. if (!valid_name_found && i == DSA_MAX_PORTS) {
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. /* Make the built-in MII bus mask match the number of ports,
  206. * switch drivers can override this later
  207. */
  208. ds->phys_mii_mask = ds->phys_port_mask;
  209. /*
  210. * If the CPU connects to this switch, set the switch tree
  211. * tagging protocol to the preferred tagging format of this
  212. * switch.
  213. */
  214. if (dst->cpu_switch == index) {
  215. switch (drv->tag_protocol) {
  216. #ifdef CONFIG_NET_DSA_TAG_DSA
  217. case DSA_TAG_PROTO_DSA:
  218. dst->rcv = dsa_netdev_ops.rcv;
  219. break;
  220. #endif
  221. #ifdef CONFIG_NET_DSA_TAG_EDSA
  222. case DSA_TAG_PROTO_EDSA:
  223. dst->rcv = edsa_netdev_ops.rcv;
  224. break;
  225. #endif
  226. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  227. case DSA_TAG_PROTO_TRAILER:
  228. dst->rcv = trailer_netdev_ops.rcv;
  229. break;
  230. #endif
  231. #ifdef CONFIG_NET_DSA_TAG_BRCM
  232. case DSA_TAG_PROTO_BRCM:
  233. dst->rcv = brcm_netdev_ops.rcv;
  234. break;
  235. #endif
  236. case DSA_TAG_PROTO_NONE:
  237. break;
  238. default:
  239. ret = -ENOPROTOOPT;
  240. goto out;
  241. }
  242. dst->tag_protocol = drv->tag_protocol;
  243. }
  244. /*
  245. * Do basic register setup.
  246. */
  247. ret = drv->setup(ds);
  248. if (ret < 0)
  249. goto out;
  250. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  251. if (ret < 0)
  252. goto out;
  253. ds->slave_mii_bus = mdiobus_alloc();
  254. if (ds->slave_mii_bus == NULL) {
  255. ret = -ENOMEM;
  256. goto out;
  257. }
  258. dsa_slave_mii_bus_init(ds);
  259. ret = mdiobus_register(ds->slave_mii_bus);
  260. if (ret < 0)
  261. goto out_free;
  262. /*
  263. * Create network devices for physical switch ports.
  264. */
  265. for (i = 0; i < DSA_MAX_PORTS; i++) {
  266. struct net_device *slave_dev;
  267. if (!(ds->phys_port_mask & (1 << i)))
  268. continue;
  269. slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  270. if (slave_dev == NULL) {
  271. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
  272. index, i, pd->port_names[i]);
  273. continue;
  274. }
  275. ds->ports[i] = slave_dev;
  276. }
  277. #ifdef CONFIG_NET_DSA_HWMON
  278. /* If the switch provides a temperature sensor,
  279. * register with hardware monitoring subsystem.
  280. * Treat registration error as non-fatal and ignore it.
  281. */
  282. if (drv->get_temp) {
  283. const char *netname = netdev_name(dst->master_netdev);
  284. char hname[IFNAMSIZ + 1];
  285. int i, j;
  286. /* Create valid hwmon 'name' attribute */
  287. for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
  288. if (isalnum(netname[i]))
  289. hname[j++] = netname[i];
  290. }
  291. hname[j] = '\0';
  292. scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
  293. hname, index);
  294. ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
  295. ds->hwmon_name, ds, dsa_hwmon_groups);
  296. if (IS_ERR(ds->hwmon_dev))
  297. ds->hwmon_dev = NULL;
  298. }
  299. #endif /* CONFIG_NET_DSA_HWMON */
  300. return ds;
  301. out_free:
  302. mdiobus_free(ds->slave_mii_bus);
  303. out:
  304. kfree(ds);
  305. return ERR_PTR(ret);
  306. }
  307. static void dsa_switch_destroy(struct dsa_switch *ds)
  308. {
  309. #ifdef CONFIG_NET_DSA_HWMON
  310. if (ds->hwmon_dev)
  311. hwmon_device_unregister(ds->hwmon_dev);
  312. #endif
  313. }
  314. #ifdef CONFIG_PM_SLEEP
  315. static int dsa_switch_suspend(struct dsa_switch *ds)
  316. {
  317. int i, ret = 0;
  318. /* Suspend slave network devices */
  319. for (i = 0; i < DSA_MAX_PORTS; i++) {
  320. if (!(ds->phys_port_mask & (1 << i)))
  321. continue;
  322. ret = dsa_slave_suspend(ds->ports[i]);
  323. if (ret)
  324. return ret;
  325. }
  326. if (ds->drv->suspend)
  327. ret = ds->drv->suspend(ds);
  328. return ret;
  329. }
  330. static int dsa_switch_resume(struct dsa_switch *ds)
  331. {
  332. int i, ret = 0;
  333. if (ds->drv->resume)
  334. ret = ds->drv->resume(ds);
  335. if (ret)
  336. return ret;
  337. /* Resume slave network devices */
  338. for (i = 0; i < DSA_MAX_PORTS; i++) {
  339. if (!(ds->phys_port_mask & (1 << i)))
  340. continue;
  341. ret = dsa_slave_resume(ds->ports[i]);
  342. if (ret)
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. #endif
  348. /* link polling *************************************************************/
  349. static void dsa_link_poll_work(struct work_struct *ugly)
  350. {
  351. struct dsa_switch_tree *dst;
  352. int i;
  353. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  354. for (i = 0; i < dst->pd->nr_chips; i++) {
  355. struct dsa_switch *ds = dst->ds[i];
  356. if (ds != NULL && ds->drv->poll_link != NULL)
  357. ds->drv->poll_link(ds);
  358. }
  359. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  360. }
  361. static void dsa_link_poll_timer(unsigned long _dst)
  362. {
  363. struct dsa_switch_tree *dst = (void *)_dst;
  364. schedule_work(&dst->link_poll_work);
  365. }
  366. /* platform driver init and cleanup *****************************************/
  367. static int dev_is_class(struct device *dev, void *class)
  368. {
  369. if (dev->class != NULL && !strcmp(dev->class->name, class))
  370. return 1;
  371. return 0;
  372. }
  373. static struct device *dev_find_class(struct device *parent, char *class)
  374. {
  375. if (dev_is_class(parent, class)) {
  376. get_device(parent);
  377. return parent;
  378. }
  379. return device_find_child(parent, class, dev_is_class);
  380. }
  381. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  382. {
  383. struct device *d;
  384. d = dev_find_class(dev, "mdio_bus");
  385. if (d != NULL) {
  386. struct mii_bus *bus;
  387. bus = to_mii_bus(d);
  388. put_device(d);
  389. return bus;
  390. }
  391. return NULL;
  392. }
  393. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  394. static struct net_device *dev_to_net_device(struct device *dev)
  395. {
  396. struct device *d;
  397. d = dev_find_class(dev, "net");
  398. if (d != NULL) {
  399. struct net_device *nd;
  400. nd = to_net_dev(d);
  401. dev_hold(nd);
  402. put_device(d);
  403. return nd;
  404. }
  405. return NULL;
  406. }
  407. #ifdef CONFIG_OF
  408. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  409. struct dsa_chip_data *cd,
  410. int chip_index,
  411. struct device_node *link)
  412. {
  413. int ret;
  414. const __be32 *reg;
  415. int link_port_addr;
  416. int link_sw_addr;
  417. struct device_node *parent_sw;
  418. int len;
  419. parent_sw = of_get_parent(link);
  420. if (!parent_sw)
  421. return -EINVAL;
  422. reg = of_get_property(parent_sw, "reg", &len);
  423. if (!reg || (len != sizeof(*reg) * 2))
  424. return -EINVAL;
  425. link_sw_addr = be32_to_cpup(reg + 1);
  426. if (link_sw_addr >= pd->nr_chips)
  427. return -EINVAL;
  428. /* First time routing table allocation */
  429. if (!cd->rtable) {
  430. cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
  431. GFP_KERNEL);
  432. if (!cd->rtable)
  433. return -ENOMEM;
  434. /* default to no valid uplink/downlink */
  435. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  436. }
  437. reg = of_get_property(link, "reg", NULL);
  438. if (!reg) {
  439. ret = -EINVAL;
  440. goto out;
  441. }
  442. link_port_addr = be32_to_cpup(reg);
  443. cd->rtable[link_sw_addr] = link_port_addr;
  444. return 0;
  445. out:
  446. kfree(cd->rtable);
  447. return ret;
  448. }
  449. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  450. {
  451. int i;
  452. int port_index;
  453. for (i = 0; i < pd->nr_chips; i++) {
  454. port_index = 0;
  455. while (port_index < DSA_MAX_PORTS) {
  456. kfree(pd->chip[i].port_names[port_index]);
  457. port_index++;
  458. }
  459. kfree(pd->chip[i].rtable);
  460. }
  461. kfree(pd->chip);
  462. }
  463. static int dsa_of_probe(struct platform_device *pdev)
  464. {
  465. struct device_node *np = pdev->dev.of_node;
  466. struct device_node *child, *mdio, *ethernet, *port, *link;
  467. struct mii_bus *mdio_bus;
  468. struct platform_device *ethernet_dev;
  469. struct dsa_platform_data *pd;
  470. struct dsa_chip_data *cd;
  471. const char *port_name;
  472. int chip_index, port_index;
  473. const unsigned int *sw_addr, *port_reg;
  474. u32 eeprom_len;
  475. int ret;
  476. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  477. if (!mdio)
  478. return -EINVAL;
  479. mdio_bus = of_mdio_find_bus(mdio);
  480. if (!mdio_bus)
  481. return -EINVAL;
  482. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  483. if (!ethernet)
  484. return -EINVAL;
  485. ethernet_dev = of_find_device_by_node(ethernet);
  486. if (!ethernet_dev)
  487. return -ENODEV;
  488. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  489. if (!pd)
  490. return -ENOMEM;
  491. pdev->dev.platform_data = pd;
  492. pd->netdev = &ethernet_dev->dev;
  493. pd->nr_chips = of_get_available_child_count(np);
  494. if (pd->nr_chips > DSA_MAX_SWITCHES)
  495. pd->nr_chips = DSA_MAX_SWITCHES;
  496. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  497. GFP_KERNEL);
  498. if (!pd->chip) {
  499. ret = -ENOMEM;
  500. goto out_free;
  501. }
  502. chip_index = -1;
  503. for_each_available_child_of_node(np, child) {
  504. chip_index++;
  505. cd = &pd->chip[chip_index];
  506. cd->of_node = child;
  507. cd->host_dev = &mdio_bus->dev;
  508. sw_addr = of_get_property(child, "reg", NULL);
  509. if (!sw_addr)
  510. continue;
  511. cd->sw_addr = be32_to_cpup(sw_addr);
  512. if (cd->sw_addr > PHY_MAX_ADDR)
  513. continue;
  514. if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
  515. cd->eeprom_len = eeprom_len;
  516. for_each_available_child_of_node(child, port) {
  517. port_reg = of_get_property(port, "reg", NULL);
  518. if (!port_reg)
  519. continue;
  520. port_index = be32_to_cpup(port_reg);
  521. port_name = of_get_property(port, "label", NULL);
  522. if (!port_name)
  523. continue;
  524. cd->port_dn[port_index] = port;
  525. cd->port_names[port_index] = kstrdup(port_name,
  526. GFP_KERNEL);
  527. if (!cd->port_names[port_index]) {
  528. ret = -ENOMEM;
  529. goto out_free_chip;
  530. }
  531. link = of_parse_phandle(port, "link", 0);
  532. if (!strcmp(port_name, "dsa") && link &&
  533. pd->nr_chips > 1) {
  534. ret = dsa_of_setup_routing_table(pd, cd,
  535. chip_index, link);
  536. if (ret)
  537. goto out_free_chip;
  538. }
  539. if (port_index == DSA_MAX_PORTS)
  540. break;
  541. }
  542. }
  543. return 0;
  544. out_free_chip:
  545. dsa_of_free_platform_data(pd);
  546. out_free:
  547. kfree(pd);
  548. pdev->dev.platform_data = NULL;
  549. return ret;
  550. }
  551. static void dsa_of_remove(struct platform_device *pdev)
  552. {
  553. struct dsa_platform_data *pd = pdev->dev.platform_data;
  554. if (!pdev->dev.of_node)
  555. return;
  556. dsa_of_free_platform_data(pd);
  557. kfree(pd);
  558. }
  559. #else
  560. static inline int dsa_of_probe(struct platform_device *pdev)
  561. {
  562. return 0;
  563. }
  564. static inline void dsa_of_remove(struct platform_device *pdev)
  565. {
  566. }
  567. #endif
  568. static int dsa_probe(struct platform_device *pdev)
  569. {
  570. struct dsa_platform_data *pd = pdev->dev.platform_data;
  571. struct net_device *dev;
  572. struct dsa_switch_tree *dst;
  573. int i, ret;
  574. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  575. dsa_driver_version);
  576. if (pdev->dev.of_node) {
  577. ret = dsa_of_probe(pdev);
  578. if (ret)
  579. return ret;
  580. pd = pdev->dev.platform_data;
  581. }
  582. if (pd == NULL || pd->netdev == NULL)
  583. return -EINVAL;
  584. dev = dev_to_net_device(pd->netdev);
  585. if (dev == NULL) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. if (dev->dsa_ptr != NULL) {
  590. dev_put(dev);
  591. ret = -EEXIST;
  592. goto out;
  593. }
  594. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  595. if (dst == NULL) {
  596. dev_put(dev);
  597. ret = -ENOMEM;
  598. goto out;
  599. }
  600. platform_set_drvdata(pdev, dst);
  601. dst->pd = pd;
  602. dst->master_netdev = dev;
  603. dst->cpu_switch = -1;
  604. dst->cpu_port = -1;
  605. for (i = 0; i < pd->nr_chips; i++) {
  606. struct dsa_switch *ds;
  607. ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
  608. if (IS_ERR(ds)) {
  609. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  610. i, PTR_ERR(ds));
  611. continue;
  612. }
  613. dst->ds[i] = ds;
  614. if (ds->drv->poll_link != NULL)
  615. dst->link_poll_needed = 1;
  616. }
  617. /*
  618. * If we use a tagging format that doesn't have an ethertype
  619. * field, make sure that all packets from this point on get
  620. * sent to the tag format's receive function.
  621. */
  622. wmb();
  623. dev->dsa_ptr = (void *)dst;
  624. if (dst->link_poll_needed) {
  625. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  626. init_timer(&dst->link_poll_timer);
  627. dst->link_poll_timer.data = (unsigned long)dst;
  628. dst->link_poll_timer.function = dsa_link_poll_timer;
  629. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  630. add_timer(&dst->link_poll_timer);
  631. }
  632. return 0;
  633. out:
  634. dsa_of_remove(pdev);
  635. return ret;
  636. }
  637. static int dsa_remove(struct platform_device *pdev)
  638. {
  639. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  640. int i;
  641. if (dst->link_poll_needed)
  642. del_timer_sync(&dst->link_poll_timer);
  643. flush_work(&dst->link_poll_work);
  644. for (i = 0; i < dst->pd->nr_chips; i++) {
  645. struct dsa_switch *ds = dst->ds[i];
  646. if (ds != NULL)
  647. dsa_switch_destroy(ds);
  648. }
  649. dsa_of_remove(pdev);
  650. return 0;
  651. }
  652. static void dsa_shutdown(struct platform_device *pdev)
  653. {
  654. }
  655. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  656. struct packet_type *pt, struct net_device *orig_dev)
  657. {
  658. struct dsa_switch_tree *dst = dev->dsa_ptr;
  659. if (unlikely(dst == NULL)) {
  660. kfree_skb(skb);
  661. return 0;
  662. }
  663. return dst->rcv(skb, dev, pt, orig_dev);
  664. }
  665. static struct packet_type dsa_pack_type __read_mostly = {
  666. .type = cpu_to_be16(ETH_P_XDSA),
  667. .func = dsa_switch_rcv,
  668. };
  669. #ifdef CONFIG_PM_SLEEP
  670. static int dsa_suspend(struct device *d)
  671. {
  672. struct platform_device *pdev = to_platform_device(d);
  673. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  674. int i, ret = 0;
  675. for (i = 0; i < dst->pd->nr_chips; i++) {
  676. struct dsa_switch *ds = dst->ds[i];
  677. if (ds != NULL)
  678. ret = dsa_switch_suspend(ds);
  679. }
  680. return ret;
  681. }
  682. static int dsa_resume(struct device *d)
  683. {
  684. struct platform_device *pdev = to_platform_device(d);
  685. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  686. int i, ret = 0;
  687. for (i = 0; i < dst->pd->nr_chips; i++) {
  688. struct dsa_switch *ds = dst->ds[i];
  689. if (ds != NULL)
  690. ret = dsa_switch_resume(ds);
  691. }
  692. return ret;
  693. }
  694. #endif
  695. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  696. static const struct of_device_id dsa_of_match_table[] = {
  697. { .compatible = "brcm,bcm7445-switch-v4.0" },
  698. { .compatible = "marvell,dsa", },
  699. {}
  700. };
  701. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  702. static struct platform_driver dsa_driver = {
  703. .probe = dsa_probe,
  704. .remove = dsa_remove,
  705. .shutdown = dsa_shutdown,
  706. .driver = {
  707. .name = "dsa",
  708. .of_match_table = dsa_of_match_table,
  709. .pm = &dsa_pm_ops,
  710. },
  711. };
  712. static int __init dsa_init_module(void)
  713. {
  714. int rc;
  715. rc = platform_driver_register(&dsa_driver);
  716. if (rc)
  717. return rc;
  718. dev_add_pack(&dsa_pack_type);
  719. return 0;
  720. }
  721. module_init(dsa_init_module);
  722. static void __exit dsa_cleanup_module(void)
  723. {
  724. dev_remove_pack(&dsa_pack_type);
  725. platform_driver_unregister(&dsa_driver);
  726. }
  727. module_exit(dsa_cleanup_module);
  728. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  729. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  730. MODULE_LICENSE("GPL");
  731. MODULE_ALIAS("platform:dsa");