dsa.c 24 KB

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