dsa.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. struct phy_device *phydev;
  423. if (of_phy_is_fixed_link(port_dn)) {
  424. phydev = of_phy_find_device(port_dn);
  425. if (phydev) {
  426. fixed_phy_unregister(phydev);
  427. put_device(&phydev->mdio.dev);
  428. phy_device_free(phydev);
  429. }
  430. }
  431. }
  432. static void dsa_switch_destroy(struct dsa_switch *ds)
  433. {
  434. int port;
  435. #ifdef CONFIG_NET_DSA_HWMON
  436. if (ds->hwmon_dev)
  437. hwmon_device_unregister(ds->hwmon_dev);
  438. #endif
  439. /* Destroy network devices for physical switch ports. */
  440. for (port = 0; port < DSA_MAX_PORTS; port++) {
  441. if (!(ds->enabled_port_mask & (1 << port)))
  442. continue;
  443. if (!ds->ports[port].netdev)
  444. continue;
  445. dsa_slave_destroy(ds->ports[port].netdev);
  446. }
  447. /* Disable configuration of the CPU and DSA ports */
  448. for (port = 0; port < DSA_MAX_PORTS; port++) {
  449. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  450. continue;
  451. dsa_cpu_dsa_destroy(ds->ports[port].dn);
  452. /* Clearing a bit which is not set does no harm */
  453. ds->cpu_port_mask |= ~(1 << port);
  454. ds->dsa_port_mask |= ~(1 << port);
  455. }
  456. if (ds->slave_mii_bus && ds->ops->phy_read)
  457. mdiobus_unregister(ds->slave_mii_bus);
  458. }
  459. #ifdef CONFIG_PM_SLEEP
  460. int dsa_switch_suspend(struct dsa_switch *ds)
  461. {
  462. int i, ret = 0;
  463. /* Suspend slave network devices */
  464. for (i = 0; i < DSA_MAX_PORTS; i++) {
  465. if (!dsa_is_port_initialized(ds, i))
  466. continue;
  467. ret = dsa_slave_suspend(ds->ports[i].netdev);
  468. if (ret)
  469. return ret;
  470. }
  471. if (ds->ops->suspend)
  472. ret = ds->ops->suspend(ds);
  473. return ret;
  474. }
  475. EXPORT_SYMBOL_GPL(dsa_switch_suspend);
  476. int dsa_switch_resume(struct dsa_switch *ds)
  477. {
  478. int i, ret = 0;
  479. if (ds->ops->resume)
  480. ret = ds->ops->resume(ds);
  481. if (ret)
  482. return ret;
  483. /* Resume slave network devices */
  484. for (i = 0; i < DSA_MAX_PORTS; i++) {
  485. if (!dsa_is_port_initialized(ds, i))
  486. continue;
  487. ret = dsa_slave_resume(ds->ports[i].netdev);
  488. if (ret)
  489. return ret;
  490. }
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_GPL(dsa_switch_resume);
  494. #endif
  495. /* platform driver init and cleanup *****************************************/
  496. static int dev_is_class(struct device *dev, void *class)
  497. {
  498. if (dev->class != NULL && !strcmp(dev->class->name, class))
  499. return 1;
  500. return 0;
  501. }
  502. static struct device *dev_find_class(struct device *parent, char *class)
  503. {
  504. if (dev_is_class(parent, class)) {
  505. get_device(parent);
  506. return parent;
  507. }
  508. return device_find_child(parent, class, dev_is_class);
  509. }
  510. struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
  511. {
  512. struct device *d;
  513. d = dev_find_class(dev, "mdio_bus");
  514. if (d != NULL) {
  515. struct mii_bus *bus;
  516. bus = to_mii_bus(d);
  517. put_device(d);
  518. return bus;
  519. }
  520. return NULL;
  521. }
  522. EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
  523. static struct net_device *dev_to_net_device(struct device *dev)
  524. {
  525. struct device *d;
  526. d = dev_find_class(dev, "net");
  527. if (d != NULL) {
  528. struct net_device *nd;
  529. nd = to_net_dev(d);
  530. dev_hold(nd);
  531. put_device(d);
  532. return nd;
  533. }
  534. return NULL;
  535. }
  536. #ifdef CONFIG_OF
  537. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  538. struct dsa_chip_data *cd,
  539. int chip_index, int port_index,
  540. struct device_node *link)
  541. {
  542. const __be32 *reg;
  543. int link_sw_addr;
  544. struct device_node *parent_sw;
  545. int len;
  546. parent_sw = of_get_parent(link);
  547. if (!parent_sw)
  548. return -EINVAL;
  549. reg = of_get_property(parent_sw, "reg", &len);
  550. if (!reg || (len != sizeof(*reg) * 2))
  551. return -EINVAL;
  552. /*
  553. * Get the destination switch number from the second field of its 'reg'
  554. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  555. */
  556. link_sw_addr = be32_to_cpup(reg + 1);
  557. if (link_sw_addr >= pd->nr_chips)
  558. return -EINVAL;
  559. cd->rtable[link_sw_addr] = port_index;
  560. return 0;
  561. }
  562. static int dsa_of_probe_links(struct dsa_platform_data *pd,
  563. struct dsa_chip_data *cd,
  564. int chip_index, int port_index,
  565. struct device_node *port,
  566. const char *port_name)
  567. {
  568. struct device_node *link;
  569. int link_index;
  570. int ret;
  571. for (link_index = 0;; link_index++) {
  572. link = of_parse_phandle(port, "link", link_index);
  573. if (!link)
  574. break;
  575. if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
  576. ret = dsa_of_setup_routing_table(pd, cd, chip_index,
  577. port_index, link);
  578. if (ret)
  579. return ret;
  580. }
  581. }
  582. return 0;
  583. }
  584. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  585. {
  586. int i;
  587. int port_index;
  588. for (i = 0; i < pd->nr_chips; i++) {
  589. port_index = 0;
  590. while (port_index < DSA_MAX_PORTS) {
  591. kfree(pd->chip[i].port_names[port_index]);
  592. port_index++;
  593. }
  594. /* Drop our reference to the MDIO bus device */
  595. if (pd->chip[i].host_dev)
  596. put_device(pd->chip[i].host_dev);
  597. }
  598. kfree(pd->chip);
  599. }
  600. static int dsa_of_probe(struct device *dev)
  601. {
  602. struct device_node *np = dev->of_node;
  603. struct device_node *child, *mdio, *ethernet, *port;
  604. struct mii_bus *mdio_bus, *mdio_bus_switch;
  605. struct net_device *ethernet_dev;
  606. struct dsa_platform_data *pd;
  607. struct dsa_chip_data *cd;
  608. const char *port_name;
  609. int chip_index, port_index;
  610. const unsigned int *sw_addr, *port_reg;
  611. u32 eeprom_len;
  612. int ret;
  613. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  614. if (!mdio)
  615. return -EINVAL;
  616. mdio_bus = of_mdio_find_bus(mdio);
  617. if (!mdio_bus)
  618. return -EPROBE_DEFER;
  619. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  620. if (!ethernet) {
  621. ret = -EINVAL;
  622. goto out_put_mdio;
  623. }
  624. ethernet_dev = of_find_net_device_by_node(ethernet);
  625. if (!ethernet_dev) {
  626. ret = -EPROBE_DEFER;
  627. goto out_put_mdio;
  628. }
  629. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  630. if (!pd) {
  631. ret = -ENOMEM;
  632. goto out_put_ethernet;
  633. }
  634. dev->platform_data = pd;
  635. pd->of_netdev = ethernet_dev;
  636. pd->nr_chips = of_get_available_child_count(np);
  637. if (pd->nr_chips > DSA_MAX_SWITCHES)
  638. pd->nr_chips = DSA_MAX_SWITCHES;
  639. pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
  640. GFP_KERNEL);
  641. if (!pd->chip) {
  642. ret = -ENOMEM;
  643. goto out_free;
  644. }
  645. chip_index = -1;
  646. for_each_available_child_of_node(np, child) {
  647. int i;
  648. chip_index++;
  649. cd = &pd->chip[chip_index];
  650. cd->of_node = child;
  651. /* Initialize the routing table */
  652. for (i = 0; i < DSA_MAX_SWITCHES; ++i)
  653. cd->rtable[i] = DSA_RTABLE_NONE;
  654. /* When assigning the host device, increment its refcount */
  655. cd->host_dev = get_device(&mdio_bus->dev);
  656. sw_addr = of_get_property(child, "reg", NULL);
  657. if (!sw_addr)
  658. continue;
  659. cd->sw_addr = be32_to_cpup(sw_addr);
  660. if (cd->sw_addr >= PHY_MAX_ADDR)
  661. continue;
  662. if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
  663. cd->eeprom_len = eeprom_len;
  664. mdio = of_parse_phandle(child, "mii-bus", 0);
  665. if (mdio) {
  666. mdio_bus_switch = of_mdio_find_bus(mdio);
  667. if (!mdio_bus_switch) {
  668. ret = -EPROBE_DEFER;
  669. goto out_free_chip;
  670. }
  671. /* Drop the mdio_bus device ref, replacing the host
  672. * device with the mdio_bus_switch device, keeping
  673. * the refcount from of_mdio_find_bus() above.
  674. */
  675. put_device(cd->host_dev);
  676. cd->host_dev = &mdio_bus_switch->dev;
  677. }
  678. for_each_available_child_of_node(child, port) {
  679. port_reg = of_get_property(port, "reg", NULL);
  680. if (!port_reg)
  681. continue;
  682. port_index = be32_to_cpup(port_reg);
  683. if (port_index >= DSA_MAX_PORTS)
  684. break;
  685. port_name = of_get_property(port, "label", NULL);
  686. if (!port_name)
  687. continue;
  688. cd->port_dn[port_index] = port;
  689. cd->port_names[port_index] = kstrdup(port_name,
  690. GFP_KERNEL);
  691. if (!cd->port_names[port_index]) {
  692. ret = -ENOMEM;
  693. goto out_free_chip;
  694. }
  695. ret = dsa_of_probe_links(pd, cd, chip_index,
  696. port_index, port, port_name);
  697. if (ret)
  698. goto out_free_chip;
  699. }
  700. }
  701. /* The individual chips hold their own refcount on the mdio bus,
  702. * so drop ours */
  703. put_device(&mdio_bus->dev);
  704. return 0;
  705. out_free_chip:
  706. dsa_of_free_platform_data(pd);
  707. out_free:
  708. kfree(pd);
  709. dev->platform_data = NULL;
  710. out_put_ethernet:
  711. put_device(&ethernet_dev->dev);
  712. out_put_mdio:
  713. put_device(&mdio_bus->dev);
  714. return ret;
  715. }
  716. static void dsa_of_remove(struct device *dev)
  717. {
  718. struct dsa_platform_data *pd = dev->platform_data;
  719. if (!dev->of_node)
  720. return;
  721. dsa_of_free_platform_data(pd);
  722. put_device(&pd->of_netdev->dev);
  723. kfree(pd);
  724. }
  725. #else
  726. static inline int dsa_of_probe(struct device *dev)
  727. {
  728. return 0;
  729. }
  730. static inline void dsa_of_remove(struct device *dev)
  731. {
  732. }
  733. #endif
  734. static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
  735. struct device *parent, struct dsa_platform_data *pd)
  736. {
  737. int i;
  738. unsigned configured = 0;
  739. dst->pd = pd;
  740. dst->master_netdev = dev;
  741. dst->cpu_switch = -1;
  742. dst->cpu_port = -1;
  743. for (i = 0; i < pd->nr_chips; i++) {
  744. struct dsa_switch *ds;
  745. ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
  746. if (IS_ERR(ds)) {
  747. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  748. i, PTR_ERR(ds));
  749. continue;
  750. }
  751. dst->ds[i] = ds;
  752. ++configured;
  753. }
  754. /*
  755. * If no switch was found, exit cleanly
  756. */
  757. if (!configured)
  758. return -EPROBE_DEFER;
  759. /*
  760. * If we use a tagging format that doesn't have an ethertype
  761. * field, make sure that all packets from this point on get
  762. * sent to the tag format's receive function.
  763. */
  764. wmb();
  765. dev->dsa_ptr = (void *)dst;
  766. return 0;
  767. }
  768. static int dsa_probe(struct platform_device *pdev)
  769. {
  770. struct dsa_platform_data *pd = pdev->dev.platform_data;
  771. struct net_device *dev;
  772. struct dsa_switch_tree *dst;
  773. int ret;
  774. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  775. dsa_driver_version);
  776. if (pdev->dev.of_node) {
  777. ret = dsa_of_probe(&pdev->dev);
  778. if (ret)
  779. return ret;
  780. pd = pdev->dev.platform_data;
  781. }
  782. if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
  783. return -EINVAL;
  784. if (pd->of_netdev) {
  785. dev = pd->of_netdev;
  786. dev_hold(dev);
  787. } else {
  788. dev = dev_to_net_device(pd->netdev);
  789. }
  790. if (dev == NULL) {
  791. ret = -EPROBE_DEFER;
  792. goto out;
  793. }
  794. if (dev->dsa_ptr != NULL) {
  795. dev_put(dev);
  796. ret = -EEXIST;
  797. goto out;
  798. }
  799. dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  800. if (dst == NULL) {
  801. dev_put(dev);
  802. ret = -ENOMEM;
  803. goto out;
  804. }
  805. platform_set_drvdata(pdev, dst);
  806. ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
  807. if (ret) {
  808. dev_put(dev);
  809. goto out;
  810. }
  811. return 0;
  812. out:
  813. dsa_of_remove(&pdev->dev);
  814. return ret;
  815. }
  816. static void dsa_remove_dst(struct dsa_switch_tree *dst)
  817. {
  818. int i;
  819. dst->master_netdev->dsa_ptr = NULL;
  820. /* If we used a tagging format that doesn't have an ethertype
  821. * field, make sure that all packets from this point get sent
  822. * without the tag and go through the regular receive path.
  823. */
  824. wmb();
  825. for (i = 0; i < dst->pd->nr_chips; i++) {
  826. struct dsa_switch *ds = dst->ds[i];
  827. if (ds)
  828. dsa_switch_destroy(ds);
  829. }
  830. dsa_cpu_port_ethtool_restore(dst->ds[0]);
  831. dev_put(dst->master_netdev);
  832. }
  833. static int dsa_remove(struct platform_device *pdev)
  834. {
  835. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  836. dsa_remove_dst(dst);
  837. dsa_of_remove(&pdev->dev);
  838. return 0;
  839. }
  840. static void dsa_shutdown(struct platform_device *pdev)
  841. {
  842. }
  843. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  844. struct packet_type *pt, struct net_device *orig_dev)
  845. {
  846. struct dsa_switch_tree *dst = dev->dsa_ptr;
  847. if (unlikely(dst == NULL)) {
  848. kfree_skb(skb);
  849. return 0;
  850. }
  851. return dst->rcv(skb, dev, pt, orig_dev);
  852. }
  853. static struct packet_type dsa_pack_type __read_mostly = {
  854. .type = cpu_to_be16(ETH_P_XDSA),
  855. .func = dsa_switch_rcv,
  856. };
  857. static struct notifier_block dsa_netdevice_nb __read_mostly = {
  858. .notifier_call = dsa_slave_netdevice_event,
  859. };
  860. #ifdef CONFIG_PM_SLEEP
  861. static int dsa_suspend(struct device *d)
  862. {
  863. struct platform_device *pdev = to_platform_device(d);
  864. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  865. int i, ret = 0;
  866. for (i = 0; i < dst->pd->nr_chips; i++) {
  867. struct dsa_switch *ds = dst->ds[i];
  868. if (ds != NULL)
  869. ret = dsa_switch_suspend(ds);
  870. }
  871. return ret;
  872. }
  873. static int dsa_resume(struct device *d)
  874. {
  875. struct platform_device *pdev = to_platform_device(d);
  876. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  877. int i, ret = 0;
  878. for (i = 0; i < dst->pd->nr_chips; i++) {
  879. struct dsa_switch *ds = dst->ds[i];
  880. if (ds != NULL)
  881. ret = dsa_switch_resume(ds);
  882. }
  883. return ret;
  884. }
  885. #endif
  886. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  887. static const struct of_device_id dsa_of_match_table[] = {
  888. { .compatible = "marvell,dsa", },
  889. {}
  890. };
  891. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  892. static struct platform_driver dsa_driver = {
  893. .probe = dsa_probe,
  894. .remove = dsa_remove,
  895. .shutdown = dsa_shutdown,
  896. .driver = {
  897. .name = "dsa",
  898. .of_match_table = dsa_of_match_table,
  899. .pm = &dsa_pm_ops,
  900. },
  901. };
  902. static int __init dsa_init_module(void)
  903. {
  904. int rc;
  905. register_netdevice_notifier(&dsa_netdevice_nb);
  906. rc = platform_driver_register(&dsa_driver);
  907. if (rc)
  908. return rc;
  909. dev_add_pack(&dsa_pack_type);
  910. return 0;
  911. }
  912. module_init(dsa_init_module);
  913. static void __exit dsa_cleanup_module(void)
  914. {
  915. unregister_netdevice_notifier(&dsa_netdevice_nb);
  916. dev_remove_pack(&dsa_pack_type);
  917. platform_driver_unregister(&dsa_driver);
  918. }
  919. module_exit(dsa_cleanup_module);
  920. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  921. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  922. MODULE_LICENSE("GPL");
  923. MODULE_ALIAS("platform:dsa");