dsa.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 *parent, struct device *host_dev, int sw_addr,
  48. char **_name, void **priv)
  49. {
  50. struct dsa_switch_driver *ret;
  51. struct list_head *list;
  52. char *name;
  53. ret = NULL;
  54. name = NULL;
  55. mutex_lock(&dsa_switch_drivers_mutex);
  56. list_for_each(list, &dsa_switch_drivers) {
  57. struct dsa_switch_driver *drv;
  58. drv = list_entry(list, struct dsa_switch_driver, list);
  59. name = drv->probe(parent, host_dev, sw_addr, priv);
  60. if (name != NULL) {
  61. ret = drv;
  62. break;
  63. }
  64. }
  65. mutex_unlock(&dsa_switch_drivers_mutex);
  66. *_name = name;
  67. return ret;
  68. }
  69. /* hwmon support ************************************************************/
  70. #ifdef CONFIG_NET_DSA_HWMON
  71. static ssize_t temp1_input_show(struct device *dev,
  72. struct device_attribute *attr, char *buf)
  73. {
  74. struct dsa_switch *ds = dev_get_drvdata(dev);
  75. int temp, ret;
  76. ret = ds->drv->get_temp(ds, &temp);
  77. if (ret < 0)
  78. return ret;
  79. return sprintf(buf, "%d\n", temp * 1000);
  80. }
  81. static DEVICE_ATTR_RO(temp1_input);
  82. static ssize_t temp1_max_show(struct device *dev,
  83. struct device_attribute *attr, char *buf)
  84. {
  85. struct dsa_switch *ds = dev_get_drvdata(dev);
  86. int temp, ret;
  87. ret = ds->drv->get_temp_limit(ds, &temp);
  88. if (ret < 0)
  89. return ret;
  90. return sprintf(buf, "%d\n", temp * 1000);
  91. }
  92. static ssize_t temp1_max_store(struct device *dev,
  93. struct device_attribute *attr, const char *buf,
  94. size_t count)
  95. {
  96. struct dsa_switch *ds = dev_get_drvdata(dev);
  97. int temp, ret;
  98. ret = kstrtoint(buf, 0, &temp);
  99. if (ret < 0)
  100. return ret;
  101. ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
  102. if (ret < 0)
  103. return ret;
  104. return count;
  105. }
  106. static DEVICE_ATTR_RW(temp1_max);
  107. static ssize_t temp1_max_alarm_show(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. struct dsa_switch *ds = dev_get_drvdata(dev);
  111. bool alarm;
  112. int ret;
  113. ret = ds->drv->get_temp_alarm(ds, &alarm);
  114. if (ret < 0)
  115. return ret;
  116. return sprintf(buf, "%d\n", alarm);
  117. }
  118. static DEVICE_ATTR_RO(temp1_max_alarm);
  119. static struct attribute *dsa_hwmon_attrs[] = {
  120. &dev_attr_temp1_input.attr, /* 0 */
  121. &dev_attr_temp1_max.attr, /* 1 */
  122. &dev_attr_temp1_max_alarm.attr, /* 2 */
  123. NULL
  124. };
  125. static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
  126. struct attribute *attr, int index)
  127. {
  128. struct device *dev = container_of(kobj, struct device, kobj);
  129. struct dsa_switch *ds = dev_get_drvdata(dev);
  130. struct dsa_switch_driver *drv = ds->drv;
  131. umode_t mode = attr->mode;
  132. if (index == 1) {
  133. if (!drv->get_temp_limit)
  134. mode = 0;
  135. else if (!drv->set_temp_limit)
  136. mode &= ~S_IWUSR;
  137. } else if (index == 2 && !drv->get_temp_alarm) {
  138. mode = 0;
  139. }
  140. return mode;
  141. }
  142. static const struct attribute_group dsa_hwmon_group = {
  143. .attrs = dsa_hwmon_attrs,
  144. .is_visible = dsa_hwmon_attrs_visible,
  145. };
  146. __ATTRIBUTE_GROUPS(dsa_hwmon);
  147. #endif /* CONFIG_NET_DSA_HWMON */
  148. /* basic switch operations **************************************************/
  149. static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
  150. {
  151. struct dsa_chip_data *cd = ds->pd;
  152. struct device_node *port_dn;
  153. struct phy_device *phydev;
  154. int ret, port, mode;
  155. for (port = 0; port < DSA_MAX_PORTS; port++) {
  156. if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
  157. continue;
  158. port_dn = cd->port_dn[port];
  159. if (of_phy_is_fixed_link(port_dn)) {
  160. ret = of_phy_register_fixed_link(port_dn);
  161. if (ret) {
  162. netdev_err(master,
  163. "failed to register fixed PHY\n");
  164. return ret;
  165. }
  166. phydev = of_phy_find_device(port_dn);
  167. mode = of_get_phy_mode(port_dn);
  168. if (mode < 0)
  169. mode = PHY_INTERFACE_MODE_NA;
  170. phydev->interface = mode;
  171. genphy_config_init(phydev);
  172. genphy_read_status(phydev);
  173. if (ds->drv->adjust_link)
  174. ds->drv->adjust_link(ds, port, phydev);
  175. }
  176. }
  177. return 0;
  178. }
  179. static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  180. {
  181. struct dsa_switch_driver *drv = ds->drv;
  182. struct dsa_switch_tree *dst = ds->dst;
  183. struct dsa_chip_data *pd = ds->pd;
  184. bool valid_name_found = false;
  185. int index = ds->index;
  186. int i, ret;
  187. /*
  188. * Validate supplied switch configuration.
  189. */
  190. for (i = 0; i < DSA_MAX_PORTS; i++) {
  191. char *name;
  192. name = pd->port_names[i];
  193. if (name == NULL)
  194. continue;
  195. if (!strcmp(name, "cpu")) {
  196. if (dst->cpu_switch != -1) {
  197. netdev_err(dst->master_netdev,
  198. "multiple cpu ports?!\n");
  199. ret = -EINVAL;
  200. goto out;
  201. }
  202. dst->cpu_switch = index;
  203. dst->cpu_port = i;
  204. } else if (!strcmp(name, "dsa")) {
  205. ds->dsa_port_mask |= 1 << i;
  206. } else {
  207. ds->enabled_port_mask |= 1 << i;
  208. }
  209. valid_name_found = true;
  210. }
  211. if (!valid_name_found && i == DSA_MAX_PORTS) {
  212. ret = -EINVAL;
  213. goto out;
  214. }
  215. /* Make the built-in MII bus mask match the number of ports,
  216. * switch drivers can override this later
  217. */
  218. ds->phys_mii_mask = ds->enabled_port_mask;
  219. /*
  220. * If the CPU connects to this switch, set the switch tree
  221. * tagging protocol to the preferred tagging format of this
  222. * switch.
  223. */
  224. if (dst->cpu_switch == index) {
  225. switch (ds->tag_protocol) {
  226. #ifdef CONFIG_NET_DSA_TAG_DSA
  227. case DSA_TAG_PROTO_DSA:
  228. dst->rcv = dsa_netdev_ops.rcv;
  229. break;
  230. #endif
  231. #ifdef CONFIG_NET_DSA_TAG_EDSA
  232. case DSA_TAG_PROTO_EDSA:
  233. dst->rcv = edsa_netdev_ops.rcv;
  234. break;
  235. #endif
  236. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  237. case DSA_TAG_PROTO_TRAILER:
  238. dst->rcv = trailer_netdev_ops.rcv;
  239. break;
  240. #endif
  241. #ifdef CONFIG_NET_DSA_TAG_BRCM
  242. case DSA_TAG_PROTO_BRCM:
  243. dst->rcv = brcm_netdev_ops.rcv;
  244. break;
  245. #endif
  246. case DSA_TAG_PROTO_NONE:
  247. break;
  248. default:
  249. ret = -ENOPROTOOPT;
  250. goto out;
  251. }
  252. dst->tag_protocol = ds->tag_protocol;
  253. }
  254. /*
  255. * Do basic register setup.
  256. */
  257. ret = drv->setup(ds);
  258. if (ret < 0)
  259. goto out;
  260. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  261. if (ret < 0)
  262. goto out;
  263. ds->slave_mii_bus = devm_mdiobus_alloc(parent);
  264. if (ds->slave_mii_bus == NULL) {
  265. ret = -ENOMEM;
  266. goto out;
  267. }
  268. dsa_slave_mii_bus_init(ds);
  269. ret = mdiobus_register(ds->slave_mii_bus);
  270. if (ret < 0)
  271. goto out;
  272. /*
  273. * Create network devices for physical switch ports.
  274. */
  275. for (i = 0; i < DSA_MAX_PORTS; i++) {
  276. if (!(ds->enabled_port_mask & (1 << i)))
  277. continue;
  278. ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  279. if (ret < 0) {
  280. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
  281. index, i, pd->port_names[i], ret);
  282. ret = 0;
  283. }
  284. }
  285. /* Perform configuration of the CPU and DSA ports */
  286. ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
  287. if (ret < 0) {
  288. netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
  289. index);
  290. ret = 0;
  291. }
  292. #ifdef CONFIG_NET_DSA_HWMON
  293. /* If the switch provides a temperature sensor,
  294. * register with hardware monitoring subsystem.
  295. * Treat registration error as non-fatal and ignore it.
  296. */
  297. if (drv->get_temp) {
  298. const char *netname = netdev_name(dst->master_netdev);
  299. char hname[IFNAMSIZ + 1];
  300. int i, j;
  301. /* Create valid hwmon 'name' attribute */
  302. for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
  303. if (isalnum(netname[i]))
  304. hname[j++] = netname[i];
  305. }
  306. hname[j] = '\0';
  307. scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
  308. hname, index);
  309. ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
  310. ds->hwmon_name, ds, dsa_hwmon_groups);
  311. if (IS_ERR(ds->hwmon_dev))
  312. ds->hwmon_dev = NULL;
  313. }
  314. #endif /* CONFIG_NET_DSA_HWMON */
  315. return ret;
  316. out:
  317. return ret;
  318. }
  319. static struct dsa_switch *
  320. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  321. struct device *parent, struct device *host_dev)
  322. {
  323. struct dsa_chip_data *pd = dst->pd->chip + index;
  324. struct dsa_switch_driver *drv;
  325. struct dsa_switch *ds;
  326. int ret;
  327. char *name;
  328. void *priv;
  329. /*
  330. * Probe for switch model.
  331. */
  332. drv = dsa_switch_probe(parent, host_dev, pd->sw_addr, &name, &priv);
  333. if (drv == NULL) {
  334. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  335. index);
  336. return ERR_PTR(-EINVAL);
  337. }
  338. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  339. index, name);
  340. /*
  341. * Allocate and initialise switch state.
  342. */
  343. ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
  344. if (ds == NULL)
  345. return ERR_PTR(-ENOMEM);
  346. ds->dst = dst;
  347. ds->index = index;
  348. ds->pd = pd;
  349. ds->drv = drv;
  350. ds->priv = priv;
  351. ds->tag_protocol = drv->tag_protocol;
  352. ds->master_dev = host_dev;
  353. ret = dsa_switch_setup_one(ds, parent);
  354. if (ret)
  355. return ERR_PTR(ret);
  356. return ds;
  357. }
  358. static void dsa_switch_destroy(struct dsa_switch *ds)
  359. {
  360. struct device_node *port_dn;
  361. struct phy_device *phydev;
  362. struct dsa_chip_data *cd = ds->pd;
  363. int port;
  364. #ifdef CONFIG_NET_DSA_HWMON
  365. if (ds->hwmon_dev)
  366. hwmon_device_unregister(ds->hwmon_dev);
  367. #endif
  368. /* Destroy network devices for physical switch ports. */
  369. for (port = 0; port < DSA_MAX_PORTS; port++) {
  370. if (!(ds->enabled_port_mask & (1 << port)))
  371. continue;
  372. if (!ds->ports[port])
  373. continue;
  374. dsa_slave_destroy(ds->ports[port]);
  375. }
  376. /* Remove any fixed link PHYs */
  377. for (port = 0; port < DSA_MAX_PORTS; port++) {
  378. port_dn = cd->port_dn[port];
  379. if (of_phy_is_fixed_link(port_dn)) {
  380. phydev = of_phy_find_device(port_dn);
  381. if (phydev) {
  382. phy_device_free(phydev);
  383. of_node_put(port_dn);
  384. fixed_phy_unregister(phydev);
  385. }
  386. }
  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. dst->master_netdev->dsa_ptr = NULL;
  770. /* If we used a tagging format that doesn't have an ethertype
  771. * field, make sure that all packets from this point get sent
  772. * without the tag and go through the regular receive path.
  773. */
  774. wmb();
  775. for (i = 0; i < dst->pd->nr_chips; i++) {
  776. struct dsa_switch *ds = dst->ds[i];
  777. if (ds)
  778. dsa_switch_destroy(ds);
  779. }
  780. dev_put(dst->master_netdev);
  781. }
  782. static int dsa_remove(struct platform_device *pdev)
  783. {
  784. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  785. dsa_remove_dst(dst);
  786. dsa_of_remove(&pdev->dev);
  787. return 0;
  788. }
  789. static void dsa_shutdown(struct platform_device *pdev)
  790. {
  791. }
  792. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  793. struct packet_type *pt, struct net_device *orig_dev)
  794. {
  795. struct dsa_switch_tree *dst = dev->dsa_ptr;
  796. if (unlikely(dst == NULL)) {
  797. kfree_skb(skb);
  798. return 0;
  799. }
  800. return dst->rcv(skb, dev, pt, orig_dev);
  801. }
  802. static struct packet_type dsa_pack_type __read_mostly = {
  803. .type = cpu_to_be16(ETH_P_XDSA),
  804. .func = dsa_switch_rcv,
  805. };
  806. static struct notifier_block dsa_netdevice_nb __read_mostly = {
  807. .notifier_call = dsa_slave_netdevice_event,
  808. };
  809. #ifdef CONFIG_PM_SLEEP
  810. static int dsa_suspend(struct device *d)
  811. {
  812. struct platform_device *pdev = to_platform_device(d);
  813. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  814. int i, ret = 0;
  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");