dsa.c 21 KB

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