dsa.c 23 KB

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