dsa.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
  146. {
  147. struct dsa_chip_data *cd = ds->pd;
  148. struct device_node *port_dn;
  149. struct phy_device *phydev;
  150. int ret, port, mode;
  151. for (port = 0; port < DSA_MAX_PORTS; port++) {
  152. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  153. continue;
  154. port_dn = cd->port_dn[port];
  155. if (of_phy_is_fixed_link(port_dn)) {
  156. ret = of_phy_register_fixed_link(port_dn);
  157. if (ret) {
  158. netdev_err(master,
  159. "failed to register fixed PHY\n");
  160. return ret;
  161. }
  162. phydev = of_phy_find_device(port_dn);
  163. mode = of_get_phy_mode(port_dn);
  164. if (mode < 0)
  165. mode = PHY_INTERFACE_MODE_NA;
  166. phydev->interface = mode;
  167. genphy_config_init(phydev);
  168. genphy_read_status(phydev);
  169. if (ds->drv->adjust_link)
  170. ds->drv->adjust_link(ds, port, phydev);
  171. }
  172. }
  173. return 0;
  174. }
  175. static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  176. {
  177. struct dsa_switch_driver *drv = ds->drv;
  178. struct dsa_switch_tree *dst = ds->dst;
  179. struct dsa_chip_data *pd = ds->pd;
  180. bool valid_name_found = false;
  181. int index = ds->index;
  182. int i, ret;
  183. /*
  184. * Validate supplied switch configuration.
  185. */
  186. for (i = 0; i < DSA_MAX_PORTS; i++) {
  187. char *name;
  188. name = pd->port_names[i];
  189. if (name == NULL)
  190. continue;
  191. if (!strcmp(name, "cpu")) {
  192. if (dst->cpu_switch != -1) {
  193. netdev_err(dst->master_netdev,
  194. "multiple cpu ports?!\n");
  195. ret = -EINVAL;
  196. goto out;
  197. }
  198. dst->cpu_switch = index;
  199. dst->cpu_port = i;
  200. } else if (!strcmp(name, "dsa")) {
  201. ds->dsa_port_mask |= 1 << i;
  202. } else {
  203. ds->phys_port_mask |= 1 << i;
  204. }
  205. valid_name_found = true;
  206. }
  207. if (!valid_name_found && i == DSA_MAX_PORTS) {
  208. ret = -EINVAL;
  209. goto out;
  210. }
  211. /* Make the built-in MII bus mask match the number of ports,
  212. * switch drivers can override this later
  213. */
  214. ds->phys_mii_mask = ds->phys_port_mask;
  215. /*
  216. * If the CPU connects to this switch, set the switch tree
  217. * tagging protocol to the preferred tagging format of this
  218. * switch.
  219. */
  220. if (dst->cpu_switch == index) {
  221. switch (ds->tag_protocol) {
  222. #ifdef CONFIG_NET_DSA_TAG_DSA
  223. case DSA_TAG_PROTO_DSA:
  224. dst->rcv = dsa_netdev_ops.rcv;
  225. break;
  226. #endif
  227. #ifdef CONFIG_NET_DSA_TAG_EDSA
  228. case DSA_TAG_PROTO_EDSA:
  229. dst->rcv = edsa_netdev_ops.rcv;
  230. break;
  231. #endif
  232. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  233. case DSA_TAG_PROTO_TRAILER:
  234. dst->rcv = trailer_netdev_ops.rcv;
  235. break;
  236. #endif
  237. #ifdef CONFIG_NET_DSA_TAG_BRCM
  238. case DSA_TAG_PROTO_BRCM:
  239. dst->rcv = brcm_netdev_ops.rcv;
  240. break;
  241. #endif
  242. case DSA_TAG_PROTO_NONE:
  243. break;
  244. default:
  245. ret = -ENOPROTOOPT;
  246. goto out;
  247. }
  248. dst->tag_protocol = ds->tag_protocol;
  249. }
  250. /*
  251. * Do basic register setup.
  252. */
  253. ret = drv->setup(ds);
  254. if (ret < 0)
  255. goto out;
  256. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  257. if (ret < 0)
  258. goto out;
  259. ds->slave_mii_bus = mdiobus_alloc();
  260. if (ds->slave_mii_bus == NULL) {
  261. ret = -ENOMEM;
  262. goto out;
  263. }
  264. dsa_slave_mii_bus_init(ds);
  265. ret = mdiobus_register(ds->slave_mii_bus);
  266. if (ret < 0)
  267. goto out_free;
  268. /*
  269. * Create network devices for physical switch ports.
  270. */
  271. for (i = 0; i < DSA_MAX_PORTS; i++) {
  272. if (!(ds->phys_port_mask & (1 << i)))
  273. continue;
  274. ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  275. if (ret < 0) {
  276. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
  277. index, i, pd->port_names[i]);
  278. ret = 0;
  279. }
  280. }
  281. /* Perform configuration of the CPU and DSA ports */
  282. ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
  283. if (ret < 0) {
  284. netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
  285. index);
  286. ret = 0;
  287. }
  288. #ifdef CONFIG_NET_DSA_HWMON
  289. /* If the switch provides a temperature sensor,
  290. * register with hardware monitoring subsystem.
  291. * Treat registration error as non-fatal and ignore it.
  292. */
  293. if (drv->get_temp) {
  294. const char *netname = netdev_name(dst->master_netdev);
  295. char hname[IFNAMSIZ + 1];
  296. int i, j;
  297. /* Create valid hwmon 'name' attribute */
  298. for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
  299. if (isalnum(netname[i]))
  300. hname[j++] = netname[i];
  301. }
  302. hname[j] = '\0';
  303. scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
  304. hname, index);
  305. ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
  306. ds->hwmon_name, ds, dsa_hwmon_groups);
  307. if (IS_ERR(ds->hwmon_dev))
  308. ds->hwmon_dev = NULL;
  309. }
  310. #endif /* CONFIG_NET_DSA_HWMON */
  311. return ret;
  312. out_free:
  313. mdiobus_free(ds->slave_mii_bus);
  314. out:
  315. kfree(ds);
  316. return ret;
  317. }
  318. static struct dsa_switch *
  319. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  320. struct device *parent, struct device *host_dev)
  321. {
  322. struct dsa_chip_data *pd = dst->pd->chip + index;
  323. struct dsa_switch_driver *drv;
  324. struct dsa_switch *ds;
  325. int ret;
  326. char *name;
  327. /*
  328. * Probe for switch model.
  329. */
  330. drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
  331. if (drv == NULL) {
  332. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  333. index);
  334. return ERR_PTR(-EINVAL);
  335. }
  336. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  337. index, name);
  338. /*
  339. * Allocate and initialise switch state.
  340. */
  341. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  342. if (ds == NULL)
  343. return ERR_PTR(-ENOMEM);
  344. ds->dst = dst;
  345. ds->index = index;
  346. ds->pd = pd;
  347. ds->drv = drv;
  348. ds->tag_protocol = drv->tag_protocol;
  349. ds->master_dev = host_dev;
  350. ret = dsa_switch_setup_one(ds, parent);
  351. if (ret)
  352. return ERR_PTR(ret);
  353. return ds;
  354. }
  355. static void dsa_switch_destroy(struct dsa_switch *ds)
  356. {
  357. #ifdef CONFIG_NET_DSA_HWMON
  358. if (ds->hwmon_dev)
  359. hwmon_device_unregister(ds->hwmon_dev);
  360. #endif
  361. }
  362. #ifdef CONFIG_PM_SLEEP
  363. static int dsa_switch_suspend(struct dsa_switch *ds)
  364. {
  365. int i, ret = 0;
  366. /* Suspend slave network devices */
  367. for (i = 0; i < DSA_MAX_PORTS; i++) {
  368. if (!dsa_is_port_initialized(ds, i))
  369. continue;
  370. ret = dsa_slave_suspend(ds->ports[i]);
  371. if (ret)
  372. return ret;
  373. }
  374. if (ds->drv->suspend)
  375. ret = ds->drv->suspend(ds);
  376. return ret;
  377. }
  378. static int dsa_switch_resume(struct dsa_switch *ds)
  379. {
  380. int i, ret = 0;
  381. if (ds->drv->resume)
  382. ret = ds->drv->resume(ds);
  383. if (ret)
  384. return ret;
  385. /* Resume slave network devices */
  386. for (i = 0; i < DSA_MAX_PORTS; i++) {
  387. if (!dsa_is_port_initialized(ds, i))
  388. continue;
  389. ret = dsa_slave_resume(ds->ports[i]);
  390. if (ret)
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. #endif
  396. /* link polling *************************************************************/
  397. static void dsa_link_poll_work(struct work_struct *ugly)
  398. {
  399. struct dsa_switch_tree *dst;
  400. int i;
  401. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  402. for (i = 0; i < dst->pd->nr_chips; i++) {
  403. struct dsa_switch *ds = dst->ds[i];
  404. if (ds != NULL && ds->drv->poll_link != NULL)
  405. ds->drv->poll_link(ds);
  406. }
  407. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  408. }
  409. static void dsa_link_poll_timer(unsigned long _dst)
  410. {
  411. struct dsa_switch_tree *dst = (void *)_dst;
  412. schedule_work(&dst->link_poll_work);
  413. }
  414. /* platform driver init and cleanup *****************************************/
  415. static int dev_is_class(struct device *dev, void *class)
  416. {
  417. if (dev->class != NULL && !strcmp(dev->class->name, class))
  418. return 1;
  419. return 0;
  420. }
  421. static struct device *dev_find_class(struct device *parent, char *class)
  422. {
  423. if (dev_is_class(parent, class)) {
  424. get_device(parent);
  425. return parent;
  426. }
  427. return device_find_child(parent, class, dev_is_class);
  428. }
  429. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  430. {
  431. struct device *d;
  432. d = dev_find_class(dev, "mdio_bus");
  433. if (d != NULL) {
  434. struct mii_bus *bus;
  435. bus = to_mii_bus(d);
  436. put_device(d);
  437. return bus;
  438. }
  439. return NULL;
  440. }
  441. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  442. static struct net_device *dev_to_net_device(struct device *dev)
  443. {
  444. struct device *d;
  445. d = dev_find_class(dev, "net");
  446. if (d != NULL) {
  447. struct net_device *nd;
  448. nd = to_net_dev(d);
  449. dev_hold(nd);
  450. put_device(d);
  451. return nd;
  452. }
  453. return NULL;
  454. }
  455. #ifdef CONFIG_OF
  456. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  457. struct dsa_chip_data *cd,
  458. int chip_index, int port_index,
  459. struct device_node *link)
  460. {
  461. const __be32 *reg;
  462. int link_sw_addr;
  463. struct device_node *parent_sw;
  464. int len;
  465. parent_sw = of_get_parent(link);
  466. if (!parent_sw)
  467. return -EINVAL;
  468. reg = of_get_property(parent_sw, "reg", &len);
  469. if (!reg || (len != sizeof(*reg) * 2))
  470. return -EINVAL;
  471. /*
  472. * Get the destination switch number from the second field of its 'reg'
  473. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  474. */
  475. link_sw_addr = be32_to_cpup(reg + 1);
  476. if (link_sw_addr >= pd->nr_chips)
  477. return -EINVAL;
  478. /* First time routing table allocation */
  479. if (!cd->rtable) {
  480. cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
  481. GFP_KERNEL);
  482. if (!cd->rtable)
  483. return -ENOMEM;
  484. /* default to no valid uplink/downlink */
  485. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  486. }
  487. cd->rtable[link_sw_addr] = port_index;
  488. return 0;
  489. }
  490. static int dsa_of_probe_links(struct dsa_platform_data *pd,
  491. struct dsa_chip_data *cd,
  492. int chip_index, int port_index,
  493. struct device_node *port,
  494. const char *port_name)
  495. {
  496. struct device_node *link;
  497. int link_index;
  498. int ret;
  499. for (link_index = 0;; link_index++) {
  500. link = of_parse_phandle(port, "link", link_index);
  501. if (!link)
  502. break;
  503. if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
  504. ret = dsa_of_setup_routing_table(pd, cd, chip_index,
  505. port_index, link);
  506. if (ret)
  507. return ret;
  508. }
  509. }
  510. return 0;
  511. }
  512. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  513. {
  514. int i;
  515. int port_index;
  516. for (i = 0; i < pd->nr_chips; i++) {
  517. port_index = 0;
  518. while (port_index < DSA_MAX_PORTS) {
  519. kfree(pd->chip[i].port_names[port_index]);
  520. port_index++;
  521. }
  522. kfree(pd->chip[i].rtable);
  523. }
  524. kfree(pd->chip);
  525. }
  526. static int dsa_of_probe(struct device *dev)
  527. {
  528. struct device_node *np = dev->of_node;
  529. struct device_node *child, *mdio, *ethernet, *port;
  530. struct mii_bus *mdio_bus, *mdio_bus_switch;
  531. struct net_device *ethernet_dev;
  532. struct dsa_platform_data *pd;
  533. struct dsa_chip_data *cd;
  534. const char *port_name;
  535. int chip_index, port_index;
  536. const unsigned int *sw_addr, *port_reg;
  537. u32 eeprom_len;
  538. int ret;
  539. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  540. if (!mdio)
  541. return -EINVAL;
  542. mdio_bus = of_mdio_find_bus(mdio);
  543. if (!mdio_bus)
  544. return -EPROBE_DEFER;
  545. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  546. if (!ethernet)
  547. return -EINVAL;
  548. ethernet_dev = of_find_net_device_by_node(ethernet);
  549. if (!ethernet_dev)
  550. return -EPROBE_DEFER;
  551. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  552. if (!pd)
  553. return -ENOMEM;
  554. dev->platform_data = pd;
  555. pd->of_netdev = ethernet_dev;
  556. pd->nr_chips = of_get_available_child_count(np);
  557. if (pd->nr_chips > DSA_MAX_SWITCHES)
  558. pd->nr_chips = DSA_MAX_SWITCHES;
  559. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  560. GFP_KERNEL);
  561. if (!pd->chip) {
  562. ret = -ENOMEM;
  563. goto out_free;
  564. }
  565. chip_index = -1;
  566. for_each_available_child_of_node(np, child) {
  567. chip_index++;
  568. cd = &pd->chip[chip_index];
  569. cd->of_node = child;
  570. cd->host_dev = &mdio_bus->dev;
  571. sw_addr = of_get_property(child, "reg", NULL);
  572. if (!sw_addr)
  573. continue;
  574. cd->sw_addr = be32_to_cpup(sw_addr);
  575. if (cd->sw_addr >= PHY_MAX_ADDR)
  576. continue;
  577. if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
  578. cd->eeprom_len = eeprom_len;
  579. mdio = of_parse_phandle(child, "mii-bus", 0);
  580. if (mdio) {
  581. mdio_bus_switch = of_mdio_find_bus(mdio);
  582. if (!mdio_bus_switch) {
  583. ret = -EPROBE_DEFER;
  584. goto out_free_chip;
  585. }
  586. cd->host_dev = &mdio_bus_switch->dev;
  587. }
  588. for_each_available_child_of_node(child, port) {
  589. port_reg = of_get_property(port, "reg", NULL);
  590. if (!port_reg)
  591. continue;
  592. port_index = be32_to_cpup(port_reg);
  593. if (port_index >= DSA_MAX_PORTS)
  594. break;
  595. port_name = of_get_property(port, "label", NULL);
  596. if (!port_name)
  597. continue;
  598. cd->port_dn[port_index] = port;
  599. cd->port_names[port_index] = kstrdup(port_name,
  600. GFP_KERNEL);
  601. if (!cd->port_names[port_index]) {
  602. ret = -ENOMEM;
  603. goto out_free_chip;
  604. }
  605. ret = dsa_of_probe_links(pd, cd, chip_index,
  606. port_index, port, port_name);
  607. if (ret)
  608. goto out_free_chip;
  609. }
  610. }
  611. return 0;
  612. out_free_chip:
  613. dsa_of_free_platform_data(pd);
  614. out_free:
  615. kfree(pd);
  616. dev->platform_data = NULL;
  617. return ret;
  618. }
  619. static void dsa_of_remove(struct device *dev)
  620. {
  621. struct dsa_platform_data *pd = dev->platform_data;
  622. if (!dev->of_node)
  623. return;
  624. dsa_of_free_platform_data(pd);
  625. kfree(pd);
  626. }
  627. #else
  628. static inline int dsa_of_probe(struct device *dev)
  629. {
  630. return 0;
  631. }
  632. static inline void dsa_of_remove(struct device *dev)
  633. {
  634. }
  635. #endif
  636. static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
  637. struct device *parent, struct dsa_platform_data *pd)
  638. {
  639. int i;
  640. dst->pd = pd;
  641. dst->master_netdev = dev;
  642. dst->cpu_switch = -1;
  643. dst->cpu_port = -1;
  644. for (i = 0; i < pd->nr_chips; i++) {
  645. struct dsa_switch *ds;
  646. ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
  647. if (IS_ERR(ds)) {
  648. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  649. i, PTR_ERR(ds));
  650. continue;
  651. }
  652. dst->ds[i] = ds;
  653. if (ds->drv->poll_link != NULL)
  654. dst->link_poll_needed = 1;
  655. }
  656. /*
  657. * If we use a tagging format that doesn't have an ethertype
  658. * field, make sure that all packets from this point on get
  659. * sent to the tag format's receive function.
  660. */
  661. wmb();
  662. dev->dsa_ptr = (void *)dst;
  663. if (dst->link_poll_needed) {
  664. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  665. init_timer(&dst->link_poll_timer);
  666. dst->link_poll_timer.data = (unsigned long)dst;
  667. dst->link_poll_timer.function = dsa_link_poll_timer;
  668. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  669. add_timer(&dst->link_poll_timer);
  670. }
  671. }
  672. static int dsa_probe(struct platform_device *pdev)
  673. {
  674. struct dsa_platform_data *pd = pdev->dev.platform_data;
  675. struct net_device *dev;
  676. struct dsa_switch_tree *dst;
  677. int ret;
  678. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  679. dsa_driver_version);
  680. if (pdev->dev.of_node) {
  681. ret = dsa_of_probe(&pdev->dev);
  682. if (ret)
  683. return ret;
  684. pd = pdev->dev.platform_data;
  685. }
  686. if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
  687. return -EINVAL;
  688. if (pd->of_netdev) {
  689. dev = pd->of_netdev;
  690. dev_hold(dev);
  691. } else {
  692. dev = dev_to_net_device(pd->netdev);
  693. }
  694. if (dev == NULL) {
  695. ret = -EPROBE_DEFER;
  696. goto out;
  697. }
  698. if (dev->dsa_ptr != NULL) {
  699. dev_put(dev);
  700. ret = -EEXIST;
  701. goto out;
  702. }
  703. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  704. if (dst == NULL) {
  705. dev_put(dev);
  706. ret = -ENOMEM;
  707. goto out;
  708. }
  709. platform_set_drvdata(pdev, dst);
  710. dsa_setup_dst(dst, dev, &pdev->dev, pd);
  711. return 0;
  712. out:
  713. dsa_of_remove(&pdev->dev);
  714. return ret;
  715. }
  716. static void dsa_remove_dst(struct dsa_switch_tree *dst)
  717. {
  718. int i;
  719. if (dst->link_poll_needed)
  720. del_timer_sync(&dst->link_poll_timer);
  721. flush_work(&dst->link_poll_work);
  722. for (i = 0; i < dst->pd->nr_chips; i++) {
  723. struct dsa_switch *ds = dst->ds[i];
  724. if (ds != NULL)
  725. dsa_switch_destroy(ds);
  726. }
  727. }
  728. static int dsa_remove(struct platform_device *pdev)
  729. {
  730. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  731. dsa_remove_dst(dst);
  732. dsa_of_remove(&pdev->dev);
  733. return 0;
  734. }
  735. static void dsa_shutdown(struct platform_device *pdev)
  736. {
  737. }
  738. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  739. struct packet_type *pt, struct net_device *orig_dev)
  740. {
  741. struct dsa_switch_tree *dst = dev->dsa_ptr;
  742. if (unlikely(dst == NULL)) {
  743. kfree_skb(skb);
  744. return 0;
  745. }
  746. return dst->rcv(skb, dev, pt, orig_dev);
  747. }
  748. static struct packet_type dsa_pack_type __read_mostly = {
  749. .type = cpu_to_be16(ETH_P_XDSA),
  750. .func = dsa_switch_rcv,
  751. };
  752. static struct notifier_block dsa_netdevice_nb __read_mostly = {
  753. .notifier_call = dsa_slave_netdevice_event,
  754. };
  755. #ifdef CONFIG_PM_SLEEP
  756. static int dsa_suspend(struct device *d)
  757. {
  758. struct platform_device *pdev = to_platform_device(d);
  759. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  760. int i, ret = 0;
  761. for (i = 0; i < dst->pd->nr_chips; i++) {
  762. struct dsa_switch *ds = dst->ds[i];
  763. if (ds != NULL)
  764. ret = dsa_switch_suspend(ds);
  765. }
  766. return ret;
  767. }
  768. static int dsa_resume(struct device *d)
  769. {
  770. struct platform_device *pdev = to_platform_device(d);
  771. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  772. int i, ret = 0;
  773. for (i = 0; i < dst->pd->nr_chips; i++) {
  774. struct dsa_switch *ds = dst->ds[i];
  775. if (ds != NULL)
  776. ret = dsa_switch_resume(ds);
  777. }
  778. return ret;
  779. }
  780. #endif
  781. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  782. static const struct of_device_id dsa_of_match_table[] = {
  783. { .compatible = "brcm,bcm7445-switch-v4.0" },
  784. { .compatible = "marvell,dsa", },
  785. {}
  786. };
  787. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  788. static struct platform_driver dsa_driver = {
  789. .probe = dsa_probe,
  790. .remove = dsa_remove,
  791. .shutdown = dsa_shutdown,
  792. .driver = {
  793. .name = "dsa",
  794. .of_match_table = dsa_of_match_table,
  795. .pm = &dsa_pm_ops,
  796. },
  797. };
  798. static int __init dsa_init_module(void)
  799. {
  800. int rc;
  801. register_netdevice_notifier(&dsa_netdevice_nb);
  802. rc = platform_driver_register(&dsa_driver);
  803. if (rc)
  804. return rc;
  805. dev_add_pack(&dsa_pack_type);
  806. return 0;
  807. }
  808. module_init(dsa_init_module);
  809. static void __exit dsa_cleanup_module(void)
  810. {
  811. unregister_netdevice_notifier(&dsa_netdevice_nb);
  812. dev_remove_pack(&dsa_pack_type);
  813. platform_driver_unregister(&dsa_driver);
  814. }
  815. module_exit(dsa_cleanup_module);
  816. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  817. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  818. MODULE_LICENSE("GPL");
  819. MODULE_ALIAS("platform:dsa");