dsa.c 20 KB

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