dsa.c 23 KB

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