dsa.c 22 KB

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