dsa.c 24 KB

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