dsa.c 23 KB

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