dsa.c 21 KB

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