dsa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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, int port_index,
  411. struct device_node *link)
  412. {
  413. const __be32 *reg;
  414. int link_sw_addr;
  415. struct device_node *parent_sw;
  416. int len;
  417. parent_sw = of_get_parent(link);
  418. if (!parent_sw)
  419. return -EINVAL;
  420. reg = of_get_property(parent_sw, "reg", &len);
  421. if (!reg || (len != sizeof(*reg) * 2))
  422. return -EINVAL;
  423. /*
  424. * Get the destination switch number from the second field of its 'reg'
  425. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  426. */
  427. link_sw_addr = be32_to_cpup(reg + 1);
  428. if (link_sw_addr >= pd->nr_chips)
  429. return -EINVAL;
  430. /* First time routing table allocation */
  431. if (!cd->rtable) {
  432. cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
  433. GFP_KERNEL);
  434. if (!cd->rtable)
  435. return -ENOMEM;
  436. /* default to no valid uplink/downlink */
  437. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  438. }
  439. cd->rtable[link_sw_addr] = port_index;
  440. return 0;
  441. }
  442. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  443. {
  444. int i;
  445. int port_index;
  446. for (i = 0; i < pd->nr_chips; i++) {
  447. port_index = 0;
  448. while (port_index < DSA_MAX_PORTS) {
  449. kfree(pd->chip[i].port_names[port_index]);
  450. port_index++;
  451. }
  452. kfree(pd->chip[i].rtable);
  453. }
  454. kfree(pd->chip);
  455. }
  456. static int dsa_of_probe(struct platform_device *pdev)
  457. {
  458. struct device_node *np = pdev->dev.of_node;
  459. struct device_node *child, *mdio, *ethernet, *port, *link;
  460. struct mii_bus *mdio_bus;
  461. struct platform_device *ethernet_dev;
  462. struct dsa_platform_data *pd;
  463. struct dsa_chip_data *cd;
  464. const char *port_name;
  465. int chip_index, port_index;
  466. const unsigned int *sw_addr, *port_reg;
  467. u32 eeprom_len;
  468. int ret;
  469. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  470. if (!mdio)
  471. return -EINVAL;
  472. mdio_bus = of_mdio_find_bus(mdio);
  473. if (!mdio_bus)
  474. return -EINVAL;
  475. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  476. if (!ethernet)
  477. return -EINVAL;
  478. ethernet_dev = of_find_device_by_node(ethernet);
  479. if (!ethernet_dev)
  480. return -ENODEV;
  481. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  482. if (!pd)
  483. return -ENOMEM;
  484. pdev->dev.platform_data = pd;
  485. pd->netdev = &ethernet_dev->dev;
  486. pd->nr_chips = of_get_available_child_count(np);
  487. if (pd->nr_chips > DSA_MAX_SWITCHES)
  488. pd->nr_chips = DSA_MAX_SWITCHES;
  489. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  490. GFP_KERNEL);
  491. if (!pd->chip) {
  492. ret = -ENOMEM;
  493. goto out_free;
  494. }
  495. chip_index = -1;
  496. for_each_available_child_of_node(np, child) {
  497. chip_index++;
  498. cd = &pd->chip[chip_index];
  499. cd->of_node = child;
  500. cd->host_dev = &mdio_bus->dev;
  501. sw_addr = of_get_property(child, "reg", NULL);
  502. if (!sw_addr)
  503. continue;
  504. cd->sw_addr = be32_to_cpup(sw_addr);
  505. if (cd->sw_addr > PHY_MAX_ADDR)
  506. continue;
  507. if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
  508. cd->eeprom_len = eeprom_len;
  509. for_each_available_child_of_node(child, port) {
  510. port_reg = of_get_property(port, "reg", NULL);
  511. if (!port_reg)
  512. continue;
  513. port_index = be32_to_cpup(port_reg);
  514. port_name = of_get_property(port, "label", NULL);
  515. if (!port_name)
  516. continue;
  517. cd->port_dn[port_index] = port;
  518. cd->port_names[port_index] = kstrdup(port_name,
  519. GFP_KERNEL);
  520. if (!cd->port_names[port_index]) {
  521. ret = -ENOMEM;
  522. goto out_free_chip;
  523. }
  524. link = of_parse_phandle(port, "link", 0);
  525. if (!strcmp(port_name, "dsa") && link &&
  526. pd->nr_chips > 1) {
  527. ret = dsa_of_setup_routing_table(pd, cd,
  528. chip_index, port_index, link);
  529. if (ret)
  530. goto out_free_chip;
  531. }
  532. if (port_index == DSA_MAX_PORTS)
  533. break;
  534. }
  535. }
  536. return 0;
  537. out_free_chip:
  538. dsa_of_free_platform_data(pd);
  539. out_free:
  540. kfree(pd);
  541. pdev->dev.platform_data = NULL;
  542. return ret;
  543. }
  544. static void dsa_of_remove(struct platform_device *pdev)
  545. {
  546. struct dsa_platform_data *pd = pdev->dev.platform_data;
  547. if (!pdev->dev.of_node)
  548. return;
  549. dsa_of_free_platform_data(pd);
  550. kfree(pd);
  551. }
  552. #else
  553. static inline int dsa_of_probe(struct platform_device *pdev)
  554. {
  555. return 0;
  556. }
  557. static inline void dsa_of_remove(struct platform_device *pdev)
  558. {
  559. }
  560. #endif
  561. static int dsa_probe(struct platform_device *pdev)
  562. {
  563. struct dsa_platform_data *pd = pdev->dev.platform_data;
  564. struct net_device *dev;
  565. struct dsa_switch_tree *dst;
  566. int i, ret;
  567. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  568. dsa_driver_version);
  569. if (pdev->dev.of_node) {
  570. ret = dsa_of_probe(pdev);
  571. if (ret)
  572. return ret;
  573. pd = pdev->dev.platform_data;
  574. }
  575. if (pd == NULL || pd->netdev == NULL)
  576. return -EINVAL;
  577. dev = dev_to_net_device(pd->netdev);
  578. if (dev == NULL) {
  579. ret = -EINVAL;
  580. goto out;
  581. }
  582. if (dev->dsa_ptr != NULL) {
  583. dev_put(dev);
  584. ret = -EEXIST;
  585. goto out;
  586. }
  587. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  588. if (dst == NULL) {
  589. dev_put(dev);
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. platform_set_drvdata(pdev, dst);
  594. dst->pd = pd;
  595. dst->master_netdev = dev;
  596. dst->cpu_switch = -1;
  597. dst->cpu_port = -1;
  598. for (i = 0; i < pd->nr_chips; i++) {
  599. struct dsa_switch *ds;
  600. ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
  601. if (IS_ERR(ds)) {
  602. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  603. i, PTR_ERR(ds));
  604. continue;
  605. }
  606. dst->ds[i] = ds;
  607. if (ds->drv->poll_link != NULL)
  608. dst->link_poll_needed = 1;
  609. }
  610. /*
  611. * If we use a tagging format that doesn't have an ethertype
  612. * field, make sure that all packets from this point on get
  613. * sent to the tag format's receive function.
  614. */
  615. wmb();
  616. dev->dsa_ptr = (void *)dst;
  617. if (dst->link_poll_needed) {
  618. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  619. init_timer(&dst->link_poll_timer);
  620. dst->link_poll_timer.data = (unsigned long)dst;
  621. dst->link_poll_timer.function = dsa_link_poll_timer;
  622. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  623. add_timer(&dst->link_poll_timer);
  624. }
  625. return 0;
  626. out:
  627. dsa_of_remove(pdev);
  628. return ret;
  629. }
  630. static int dsa_remove(struct platform_device *pdev)
  631. {
  632. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  633. int i;
  634. if (dst->link_poll_needed)
  635. del_timer_sync(&dst->link_poll_timer);
  636. flush_work(&dst->link_poll_work);
  637. for (i = 0; i < dst->pd->nr_chips; i++) {
  638. struct dsa_switch *ds = dst->ds[i];
  639. if (ds != NULL)
  640. dsa_switch_destroy(ds);
  641. }
  642. dsa_of_remove(pdev);
  643. return 0;
  644. }
  645. static void dsa_shutdown(struct platform_device *pdev)
  646. {
  647. }
  648. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  649. struct packet_type *pt, struct net_device *orig_dev)
  650. {
  651. struct dsa_switch_tree *dst = dev->dsa_ptr;
  652. if (unlikely(dst == NULL)) {
  653. kfree_skb(skb);
  654. return 0;
  655. }
  656. return dst->rcv(skb, dev, pt, orig_dev);
  657. }
  658. static struct packet_type dsa_pack_type __read_mostly = {
  659. .type = cpu_to_be16(ETH_P_XDSA),
  660. .func = dsa_switch_rcv,
  661. };
  662. #ifdef CONFIG_PM_SLEEP
  663. static int dsa_suspend(struct device *d)
  664. {
  665. struct platform_device *pdev = to_platform_device(d);
  666. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  667. int i, ret = 0;
  668. for (i = 0; i < dst->pd->nr_chips; i++) {
  669. struct dsa_switch *ds = dst->ds[i];
  670. if (ds != NULL)
  671. ret = dsa_switch_suspend(ds);
  672. }
  673. return ret;
  674. }
  675. static int dsa_resume(struct device *d)
  676. {
  677. struct platform_device *pdev = to_platform_device(d);
  678. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  679. int i, ret = 0;
  680. for (i = 0; i < dst->pd->nr_chips; i++) {
  681. struct dsa_switch *ds = dst->ds[i];
  682. if (ds != NULL)
  683. ret = dsa_switch_resume(ds);
  684. }
  685. return ret;
  686. }
  687. #endif
  688. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  689. static const struct of_device_id dsa_of_match_table[] = {
  690. { .compatible = "brcm,bcm7445-switch-v4.0" },
  691. { .compatible = "marvell,dsa", },
  692. {}
  693. };
  694. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  695. static struct platform_driver dsa_driver = {
  696. .probe = dsa_probe,
  697. .remove = dsa_remove,
  698. .shutdown = dsa_shutdown,
  699. .driver = {
  700. .name = "dsa",
  701. .of_match_table = dsa_of_match_table,
  702. .pm = &dsa_pm_ops,
  703. },
  704. };
  705. static int __init dsa_init_module(void)
  706. {
  707. int rc;
  708. rc = platform_driver_register(&dsa_driver);
  709. if (rc)
  710. return rc;
  711. dev_add_pack(&dsa_pack_type);
  712. return 0;
  713. }
  714. module_init(dsa_init_module);
  715. static void __exit dsa_cleanup_module(void)
  716. {
  717. dev_remove_pack(&dsa_pack_type);
  718. platform_driver_unregister(&dsa_driver);
  719. }
  720. module_exit(dsa_cleanup_module);
  721. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  722. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  723. MODULE_LICENSE("GPL");
  724. MODULE_ALIAS("platform:dsa");