dsa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. if (!(ds->phys_port_mask & (1 << i)))
  267. continue;
  268. ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  269. if (ret < 0) {
  270. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
  271. index, i, pd->port_names[i]);
  272. ret = 0;
  273. }
  274. }
  275. #ifdef CONFIG_NET_DSA_HWMON
  276. /* If the switch provides a temperature sensor,
  277. * register with hardware monitoring subsystem.
  278. * Treat registration error as non-fatal and ignore it.
  279. */
  280. if (drv->get_temp) {
  281. const char *netname = netdev_name(dst->master_netdev);
  282. char hname[IFNAMSIZ + 1];
  283. int i, j;
  284. /* Create valid hwmon 'name' attribute */
  285. for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
  286. if (isalnum(netname[i]))
  287. hname[j++] = netname[i];
  288. }
  289. hname[j] = '\0';
  290. scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
  291. hname, index);
  292. ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
  293. ds->hwmon_name, ds, dsa_hwmon_groups);
  294. if (IS_ERR(ds->hwmon_dev))
  295. ds->hwmon_dev = NULL;
  296. }
  297. #endif /* CONFIG_NET_DSA_HWMON */
  298. return ds;
  299. out_free:
  300. mdiobus_free(ds->slave_mii_bus);
  301. out:
  302. kfree(ds);
  303. return ERR_PTR(ret);
  304. }
  305. static void dsa_switch_destroy(struct dsa_switch *ds)
  306. {
  307. #ifdef CONFIG_NET_DSA_HWMON
  308. if (ds->hwmon_dev)
  309. hwmon_device_unregister(ds->hwmon_dev);
  310. #endif
  311. }
  312. #ifdef CONFIG_PM_SLEEP
  313. static int dsa_switch_suspend(struct dsa_switch *ds)
  314. {
  315. int i, ret = 0;
  316. /* Suspend slave network devices */
  317. for (i = 0; i < DSA_MAX_PORTS; i++) {
  318. if (!dsa_is_port_initialized(ds, i))
  319. continue;
  320. ret = dsa_slave_suspend(ds->ports[i]);
  321. if (ret)
  322. return ret;
  323. }
  324. if (ds->drv->suspend)
  325. ret = ds->drv->suspend(ds);
  326. return ret;
  327. }
  328. static int dsa_switch_resume(struct dsa_switch *ds)
  329. {
  330. int i, ret = 0;
  331. if (ds->drv->resume)
  332. ret = ds->drv->resume(ds);
  333. if (ret)
  334. return ret;
  335. /* Resume slave network devices */
  336. for (i = 0; i < DSA_MAX_PORTS; i++) {
  337. if (!dsa_is_port_initialized(ds, i))
  338. continue;
  339. ret = dsa_slave_resume(ds->ports[i]);
  340. if (ret)
  341. return ret;
  342. }
  343. return 0;
  344. }
  345. #endif
  346. /* link polling *************************************************************/
  347. static void dsa_link_poll_work(struct work_struct *ugly)
  348. {
  349. struct dsa_switch_tree *dst;
  350. int i;
  351. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  352. for (i = 0; i < dst->pd->nr_chips; i++) {
  353. struct dsa_switch *ds = dst->ds[i];
  354. if (ds != NULL && ds->drv->poll_link != NULL)
  355. ds->drv->poll_link(ds);
  356. }
  357. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  358. }
  359. static void dsa_link_poll_timer(unsigned long _dst)
  360. {
  361. struct dsa_switch_tree *dst = (void *)_dst;
  362. schedule_work(&dst->link_poll_work);
  363. }
  364. /* platform driver init and cleanup *****************************************/
  365. static int dev_is_class(struct device *dev, void *class)
  366. {
  367. if (dev->class != NULL && !strcmp(dev->class->name, class))
  368. return 1;
  369. return 0;
  370. }
  371. static struct device *dev_find_class(struct device *parent, char *class)
  372. {
  373. if (dev_is_class(parent, class)) {
  374. get_device(parent);
  375. return parent;
  376. }
  377. return device_find_child(parent, class, dev_is_class);
  378. }
  379. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  380. {
  381. struct device *d;
  382. d = dev_find_class(dev, "mdio_bus");
  383. if (d != NULL) {
  384. struct mii_bus *bus;
  385. bus = to_mii_bus(d);
  386. put_device(d);
  387. return bus;
  388. }
  389. return NULL;
  390. }
  391. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  392. static struct net_device *dev_to_net_device(struct device *dev)
  393. {
  394. struct device *d;
  395. d = dev_find_class(dev, "net");
  396. if (d != NULL) {
  397. struct net_device *nd;
  398. nd = to_net_dev(d);
  399. dev_hold(nd);
  400. put_device(d);
  401. return nd;
  402. }
  403. return NULL;
  404. }
  405. #ifdef CONFIG_OF
  406. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  407. struct dsa_chip_data *cd,
  408. int chip_index,
  409. struct device_node *link)
  410. {
  411. int ret;
  412. const __be32 *reg;
  413. int link_port_addr;
  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. link_sw_addr = be32_to_cpup(reg + 1);
  424. if (link_sw_addr >= pd->nr_chips)
  425. return -EINVAL;
  426. /* First time routing table allocation */
  427. if (!cd->rtable) {
  428. cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
  429. GFP_KERNEL);
  430. if (!cd->rtable)
  431. return -ENOMEM;
  432. /* default to no valid uplink/downlink */
  433. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  434. }
  435. reg = of_get_property(link, "reg", NULL);
  436. if (!reg) {
  437. ret = -EINVAL;
  438. goto out;
  439. }
  440. link_port_addr = be32_to_cpup(reg);
  441. cd->rtable[link_sw_addr] = link_port_addr;
  442. return 0;
  443. out:
  444. kfree(cd->rtable);
  445. return ret;
  446. }
  447. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  448. {
  449. int i;
  450. int port_index;
  451. for (i = 0; i < pd->nr_chips; i++) {
  452. port_index = 0;
  453. while (port_index < DSA_MAX_PORTS) {
  454. kfree(pd->chip[i].port_names[port_index]);
  455. port_index++;
  456. }
  457. kfree(pd->chip[i].rtable);
  458. }
  459. kfree(pd->chip);
  460. }
  461. static int dsa_of_probe(struct device *dev)
  462. {
  463. struct device_node *np = dev->of_node;
  464. struct device_node *child, *mdio, *ethernet, *port, *link;
  465. struct mii_bus *mdio_bus;
  466. struct platform_device *ethernet_dev;
  467. struct dsa_platform_data *pd;
  468. struct dsa_chip_data *cd;
  469. const char *port_name;
  470. int chip_index, port_index;
  471. const unsigned int *sw_addr, *port_reg;
  472. u32 eeprom_len;
  473. int ret;
  474. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  475. if (!mdio)
  476. return -EINVAL;
  477. mdio_bus = of_mdio_find_bus(mdio);
  478. if (!mdio_bus)
  479. return -EPROBE_DEFER;
  480. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  481. if (!ethernet)
  482. return -EINVAL;
  483. ethernet_dev = of_find_device_by_node(ethernet);
  484. if (!ethernet_dev)
  485. return -EPROBE_DEFER;
  486. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  487. if (!pd)
  488. return -ENOMEM;
  489. dev->platform_data = pd;
  490. pd->netdev = &ethernet_dev->dev;
  491. pd->nr_chips = of_get_available_child_count(np);
  492. if (pd->nr_chips > DSA_MAX_SWITCHES)
  493. pd->nr_chips = DSA_MAX_SWITCHES;
  494. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  495. GFP_KERNEL);
  496. if (!pd->chip) {
  497. ret = -ENOMEM;
  498. goto out_free;
  499. }
  500. chip_index = -1;
  501. for_each_available_child_of_node(np, child) {
  502. chip_index++;
  503. cd = &pd->chip[chip_index];
  504. cd->of_node = child;
  505. cd->host_dev = &mdio_bus->dev;
  506. sw_addr = of_get_property(child, "reg", NULL);
  507. if (!sw_addr)
  508. continue;
  509. cd->sw_addr = be32_to_cpup(sw_addr);
  510. if (cd->sw_addr > PHY_MAX_ADDR)
  511. continue;
  512. if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
  513. cd->eeprom_len = eeprom_len;
  514. for_each_available_child_of_node(child, port) {
  515. port_reg = of_get_property(port, "reg", NULL);
  516. if (!port_reg)
  517. continue;
  518. port_index = be32_to_cpup(port_reg);
  519. port_name = of_get_property(port, "label", NULL);
  520. if (!port_name)
  521. continue;
  522. cd->port_dn[port_index] = port;
  523. cd->port_names[port_index] = kstrdup(port_name,
  524. GFP_KERNEL);
  525. if (!cd->port_names[port_index]) {
  526. ret = -ENOMEM;
  527. goto out_free_chip;
  528. }
  529. link = of_parse_phandle(port, "link", 0);
  530. if (!strcmp(port_name, "dsa") && link &&
  531. pd->nr_chips > 1) {
  532. ret = dsa_of_setup_routing_table(pd, cd,
  533. chip_index, link);
  534. if (ret)
  535. goto out_free_chip;
  536. }
  537. if (port_index == DSA_MAX_PORTS)
  538. break;
  539. }
  540. }
  541. return 0;
  542. out_free_chip:
  543. dsa_of_free_platform_data(pd);
  544. out_free:
  545. kfree(pd);
  546. dev->platform_data = NULL;
  547. return ret;
  548. }
  549. static void dsa_of_remove(struct device *dev)
  550. {
  551. struct dsa_platform_data *pd = dev->platform_data;
  552. if (!dev->of_node)
  553. return;
  554. dsa_of_free_platform_data(pd);
  555. kfree(pd);
  556. }
  557. #else
  558. static inline int dsa_of_probe(struct device *dev)
  559. {
  560. return 0;
  561. }
  562. static inline void dsa_of_remove(struct device *dev)
  563. {
  564. }
  565. #endif
  566. static int dsa_probe(struct platform_device *pdev)
  567. {
  568. struct dsa_platform_data *pd = pdev->dev.platform_data;
  569. struct net_device *dev;
  570. struct dsa_switch_tree *dst;
  571. int i, ret;
  572. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  573. dsa_driver_version);
  574. if (pdev->dev.of_node) {
  575. ret = dsa_of_probe(&pdev->dev);
  576. if (ret)
  577. return ret;
  578. pd = pdev->dev.platform_data;
  579. }
  580. if (pd == NULL || pd->netdev == NULL)
  581. return -EINVAL;
  582. dev = dev_to_net_device(pd->netdev);
  583. if (dev == NULL) {
  584. ret = -EPROBE_DEFER;
  585. goto out;
  586. }
  587. if (dev->dsa_ptr != NULL) {
  588. dev_put(dev);
  589. ret = -EEXIST;
  590. goto out;
  591. }
  592. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  593. if (dst == NULL) {
  594. dev_put(dev);
  595. ret = -ENOMEM;
  596. goto out;
  597. }
  598. platform_set_drvdata(pdev, dst);
  599. dst->pd = pd;
  600. dst->master_netdev = dev;
  601. dst->cpu_switch = -1;
  602. dst->cpu_port = -1;
  603. for (i = 0; i < pd->nr_chips; i++) {
  604. struct dsa_switch *ds;
  605. ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
  606. if (IS_ERR(ds)) {
  607. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  608. i, PTR_ERR(ds));
  609. continue;
  610. }
  611. dst->ds[i] = ds;
  612. if (ds->drv->poll_link != NULL)
  613. dst->link_poll_needed = 1;
  614. }
  615. /*
  616. * If we use a tagging format that doesn't have an ethertype
  617. * field, make sure that all packets from this point on get
  618. * sent to the tag format's receive function.
  619. */
  620. wmb();
  621. dev->dsa_ptr = (void *)dst;
  622. if (dst->link_poll_needed) {
  623. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  624. init_timer(&dst->link_poll_timer);
  625. dst->link_poll_timer.data = (unsigned long)dst;
  626. dst->link_poll_timer.function = dsa_link_poll_timer;
  627. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  628. add_timer(&dst->link_poll_timer);
  629. }
  630. return 0;
  631. out:
  632. dsa_of_remove(&pdev->dev);
  633. return ret;
  634. }
  635. static int dsa_remove(struct platform_device *pdev)
  636. {
  637. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  638. int i;
  639. if (dst->link_poll_needed)
  640. del_timer_sync(&dst->link_poll_timer);
  641. flush_work(&dst->link_poll_work);
  642. for (i = 0; i < dst->pd->nr_chips; i++) {
  643. struct dsa_switch *ds = dst->ds[i];
  644. if (ds != NULL)
  645. dsa_switch_destroy(ds);
  646. }
  647. dsa_of_remove(&pdev->dev);
  648. return 0;
  649. }
  650. static void dsa_shutdown(struct platform_device *pdev)
  651. {
  652. }
  653. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  654. struct packet_type *pt, struct net_device *orig_dev)
  655. {
  656. struct dsa_switch_tree *dst = dev->dsa_ptr;
  657. if (unlikely(dst == NULL)) {
  658. kfree_skb(skb);
  659. return 0;
  660. }
  661. return dst->rcv(skb, dev, pt, orig_dev);
  662. }
  663. static struct packet_type dsa_pack_type __read_mostly = {
  664. .type = cpu_to_be16(ETH_P_XDSA),
  665. .func = dsa_switch_rcv,
  666. };
  667. static struct notifier_block dsa_netdevice_nb __read_mostly = {
  668. .notifier_call = dsa_slave_netdevice_event,
  669. };
  670. #ifdef CONFIG_PM_SLEEP
  671. static int dsa_suspend(struct device *d)
  672. {
  673. struct platform_device *pdev = to_platform_device(d);
  674. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  675. int i, ret = 0;
  676. for (i = 0; i < dst->pd->nr_chips; i++) {
  677. struct dsa_switch *ds = dst->ds[i];
  678. if (ds != NULL)
  679. ret = dsa_switch_suspend(ds);
  680. }
  681. return ret;
  682. }
  683. static int dsa_resume(struct device *d)
  684. {
  685. struct platform_device *pdev = to_platform_device(d);
  686. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  687. int i, ret = 0;
  688. for (i = 0; i < dst->pd->nr_chips; i++) {
  689. struct dsa_switch *ds = dst->ds[i];
  690. if (ds != NULL)
  691. ret = dsa_switch_resume(ds);
  692. }
  693. return ret;
  694. }
  695. #endif
  696. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  697. static const struct of_device_id dsa_of_match_table[] = {
  698. { .compatible = "brcm,bcm7445-switch-v4.0" },
  699. { .compatible = "marvell,dsa", },
  700. {}
  701. };
  702. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  703. static struct platform_driver dsa_driver = {
  704. .probe = dsa_probe,
  705. .remove = dsa_remove,
  706. .shutdown = dsa_shutdown,
  707. .driver = {
  708. .name = "dsa",
  709. .of_match_table = dsa_of_match_table,
  710. .pm = &dsa_pm_ops,
  711. },
  712. };
  713. static int __init dsa_init_module(void)
  714. {
  715. int rc;
  716. register_netdevice_notifier(&dsa_netdevice_nb);
  717. rc = platform_driver_register(&dsa_driver);
  718. if (rc)
  719. return rc;
  720. dev_add_pack(&dsa_pack_type);
  721. return 0;
  722. }
  723. module_init(dsa_init_module);
  724. static void __exit dsa_cleanup_module(void)
  725. {
  726. unregister_netdevice_notifier(&dsa_netdevice_nb);
  727. dev_remove_pack(&dsa_pack_type);
  728. platform_driver_unregister(&dsa_driver);
  729. }
  730. module_exit(dsa_cleanup_module);
  731. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  732. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  733. MODULE_LICENSE("GPL");
  734. MODULE_ALIAS("platform:dsa");