dsa.c 24 KB

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