dsa.c 23 KB

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