dsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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/sysfs.h>
  24. #include "dsa_priv.h"
  25. char dsa_driver_version[] = "0.1";
  26. /* switch driver registration ***********************************************/
  27. static DEFINE_MUTEX(dsa_switch_drivers_mutex);
  28. static LIST_HEAD(dsa_switch_drivers);
  29. void register_switch_driver(struct dsa_switch_driver *drv)
  30. {
  31. mutex_lock(&dsa_switch_drivers_mutex);
  32. list_add_tail(&drv->list, &dsa_switch_drivers);
  33. mutex_unlock(&dsa_switch_drivers_mutex);
  34. }
  35. EXPORT_SYMBOL_GPL(register_switch_driver);
  36. void unregister_switch_driver(struct dsa_switch_driver *drv)
  37. {
  38. mutex_lock(&dsa_switch_drivers_mutex);
  39. list_del_init(&drv->list);
  40. mutex_unlock(&dsa_switch_drivers_mutex);
  41. }
  42. EXPORT_SYMBOL_GPL(unregister_switch_driver);
  43. static struct dsa_switch_driver *
  44. dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
  45. {
  46. struct dsa_switch_driver *ret;
  47. struct list_head *list;
  48. char *name;
  49. ret = NULL;
  50. name = NULL;
  51. mutex_lock(&dsa_switch_drivers_mutex);
  52. list_for_each(list, &dsa_switch_drivers) {
  53. struct dsa_switch_driver *drv;
  54. drv = list_entry(list, struct dsa_switch_driver, list);
  55. name = drv->probe(host_dev, sw_addr);
  56. if (name != NULL) {
  57. ret = drv;
  58. break;
  59. }
  60. }
  61. mutex_unlock(&dsa_switch_drivers_mutex);
  62. *_name = name;
  63. return ret;
  64. }
  65. /* hwmon support ************************************************************/
  66. #ifdef CONFIG_NET_DSA_HWMON
  67. static ssize_t temp1_input_show(struct device *dev,
  68. struct device_attribute *attr, char *buf)
  69. {
  70. struct dsa_switch *ds = dev_get_drvdata(dev);
  71. int temp, ret;
  72. ret = ds->drv->get_temp(ds, &temp);
  73. if (ret < 0)
  74. return ret;
  75. return sprintf(buf, "%d\n", temp * 1000);
  76. }
  77. static DEVICE_ATTR_RO(temp1_input);
  78. static ssize_t temp1_max_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct dsa_switch *ds = dev_get_drvdata(dev);
  82. int temp, ret;
  83. ret = ds->drv->get_temp_limit(ds, &temp);
  84. if (ret < 0)
  85. return ret;
  86. return sprintf(buf, "%d\n", temp * 1000);
  87. }
  88. static ssize_t temp1_max_store(struct device *dev,
  89. struct device_attribute *attr, const char *buf,
  90. size_t count)
  91. {
  92. struct dsa_switch *ds = dev_get_drvdata(dev);
  93. int temp, ret;
  94. ret = kstrtoint(buf, 0, &temp);
  95. if (ret < 0)
  96. return ret;
  97. ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
  98. if (ret < 0)
  99. return ret;
  100. return count;
  101. }
  102. static DEVICE_ATTR_RW(temp1_max);
  103. static ssize_t temp1_max_alarm_show(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct dsa_switch *ds = dev_get_drvdata(dev);
  107. bool alarm;
  108. int ret;
  109. ret = ds->drv->get_temp_alarm(ds, &alarm);
  110. if (ret < 0)
  111. return ret;
  112. return sprintf(buf, "%d\n", alarm);
  113. }
  114. static DEVICE_ATTR_RO(temp1_max_alarm);
  115. static struct attribute *dsa_hwmon_attrs[] = {
  116. &dev_attr_temp1_input.attr, /* 0 */
  117. &dev_attr_temp1_max.attr, /* 1 */
  118. &dev_attr_temp1_max_alarm.attr, /* 2 */
  119. NULL
  120. };
  121. static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
  122. struct attribute *attr, int index)
  123. {
  124. struct device *dev = container_of(kobj, struct device, kobj);
  125. struct dsa_switch *ds = dev_get_drvdata(dev);
  126. struct dsa_switch_driver *drv = ds->drv;
  127. umode_t mode = attr->mode;
  128. if (index == 1) {
  129. if (!drv->get_temp_limit)
  130. mode = 0;
  131. else if (!drv->set_temp_limit)
  132. mode &= ~S_IWUSR;
  133. } else if (index == 2 && !drv->get_temp_alarm) {
  134. mode = 0;
  135. }
  136. return mode;
  137. }
  138. static const struct attribute_group dsa_hwmon_group = {
  139. .attrs = dsa_hwmon_attrs,
  140. .is_visible = dsa_hwmon_attrs_visible,
  141. };
  142. __ATTRIBUTE_GROUPS(dsa_hwmon);
  143. #endif /* CONFIG_NET_DSA_HWMON */
  144. /* basic switch operations **************************************************/
  145. static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  146. {
  147. struct dsa_switch_driver *drv = ds->drv;
  148. struct dsa_switch_tree *dst = ds->dst;
  149. struct dsa_chip_data *pd = ds->pd;
  150. bool valid_name_found = false;
  151. int index = ds->index;
  152. int i, ret;
  153. /*
  154. * Validate supplied switch configuration.
  155. */
  156. for (i = 0; i < DSA_MAX_PORTS; i++) {
  157. char *name;
  158. name = pd->port_names[i];
  159. if (name == NULL)
  160. continue;
  161. if (!strcmp(name, "cpu")) {
  162. if (dst->cpu_switch != -1) {
  163. netdev_err(dst->master_netdev,
  164. "multiple cpu ports?!\n");
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. dst->cpu_switch = index;
  169. dst->cpu_port = i;
  170. } else if (!strcmp(name, "dsa")) {
  171. ds->dsa_port_mask |= 1 << i;
  172. } else {
  173. ds->phys_port_mask |= 1 << i;
  174. }
  175. valid_name_found = true;
  176. }
  177. if (!valid_name_found && i == DSA_MAX_PORTS) {
  178. ret = -EINVAL;
  179. goto out;
  180. }
  181. /* Make the built-in MII bus mask match the number of ports,
  182. * switch drivers can override this later
  183. */
  184. ds->phys_mii_mask = ds->phys_port_mask;
  185. /*
  186. * If the CPU connects to this switch, set the switch tree
  187. * tagging protocol to the preferred tagging format of this
  188. * switch.
  189. */
  190. if (dst->cpu_switch == index) {
  191. switch (ds->tag_protocol) {
  192. #ifdef CONFIG_NET_DSA_TAG_DSA
  193. case DSA_TAG_PROTO_DSA:
  194. dst->rcv = dsa_netdev_ops.rcv;
  195. break;
  196. #endif
  197. #ifdef CONFIG_NET_DSA_TAG_EDSA
  198. case DSA_TAG_PROTO_EDSA:
  199. dst->rcv = edsa_netdev_ops.rcv;
  200. break;
  201. #endif
  202. #ifdef CONFIG_NET_DSA_TAG_TRAILER
  203. case DSA_TAG_PROTO_TRAILER:
  204. dst->rcv = trailer_netdev_ops.rcv;
  205. break;
  206. #endif
  207. #ifdef CONFIG_NET_DSA_TAG_BRCM
  208. case DSA_TAG_PROTO_BRCM:
  209. dst->rcv = brcm_netdev_ops.rcv;
  210. break;
  211. #endif
  212. case DSA_TAG_PROTO_NONE:
  213. break;
  214. default:
  215. ret = -ENOPROTOOPT;
  216. goto out;
  217. }
  218. dst->tag_protocol = ds->tag_protocol;
  219. }
  220. /*
  221. * Do basic register setup.
  222. */
  223. ret = drv->setup(ds);
  224. if (ret < 0)
  225. goto out;
  226. ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
  227. if (ret < 0)
  228. goto out;
  229. ds->slave_mii_bus = mdiobus_alloc();
  230. if (ds->slave_mii_bus == NULL) {
  231. ret = -ENOMEM;
  232. goto out;
  233. }
  234. dsa_slave_mii_bus_init(ds);
  235. ret = mdiobus_register(ds->slave_mii_bus);
  236. if (ret < 0)
  237. goto out_free;
  238. /*
  239. * Create network devices for physical switch ports.
  240. */
  241. for (i = 0; i < DSA_MAX_PORTS; i++) {
  242. if (!(ds->phys_port_mask & (1 << i)))
  243. continue;
  244. ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
  245. if (ret < 0) {
  246. netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
  247. index, i, pd->port_names[i]);
  248. ret = 0;
  249. }
  250. }
  251. #ifdef CONFIG_NET_DSA_HWMON
  252. /* If the switch provides a temperature sensor,
  253. * register with hardware monitoring subsystem.
  254. * Treat registration error as non-fatal and ignore it.
  255. */
  256. if (drv->get_temp) {
  257. const char *netname = netdev_name(dst->master_netdev);
  258. char hname[IFNAMSIZ + 1];
  259. int i, j;
  260. /* Create valid hwmon 'name' attribute */
  261. for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
  262. if (isalnum(netname[i]))
  263. hname[j++] = netname[i];
  264. }
  265. hname[j] = '\0';
  266. scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
  267. hname, index);
  268. ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
  269. ds->hwmon_name, ds, dsa_hwmon_groups);
  270. if (IS_ERR(ds->hwmon_dev))
  271. ds->hwmon_dev = NULL;
  272. }
  273. #endif /* CONFIG_NET_DSA_HWMON */
  274. return ret;
  275. out_free:
  276. mdiobus_free(ds->slave_mii_bus);
  277. out:
  278. kfree(ds);
  279. return ret;
  280. }
  281. static struct dsa_switch *
  282. dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  283. struct device *parent, struct device *host_dev)
  284. {
  285. struct dsa_chip_data *pd = dst->pd->chip + index;
  286. struct dsa_switch_driver *drv;
  287. struct dsa_switch *ds;
  288. int ret;
  289. char *name;
  290. /*
  291. * Probe for switch model.
  292. */
  293. drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
  294. if (drv == NULL) {
  295. netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
  296. index);
  297. return ERR_PTR(-EINVAL);
  298. }
  299. netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
  300. index, name);
  301. /*
  302. * Allocate and initialise switch state.
  303. */
  304. ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  305. if (ds == NULL)
  306. return ERR_PTR(-ENOMEM);
  307. ds->dst = dst;
  308. ds->index = index;
  309. ds->pd = pd;
  310. ds->drv = drv;
  311. ds->tag_protocol = drv->tag_protocol;
  312. ds->master_dev = host_dev;
  313. ret = dsa_switch_setup_one(ds, parent);
  314. if (ret)
  315. return ERR_PTR(ret);
  316. return ds;
  317. }
  318. static void dsa_switch_destroy(struct dsa_switch *ds)
  319. {
  320. #ifdef CONFIG_NET_DSA_HWMON
  321. if (ds->hwmon_dev)
  322. hwmon_device_unregister(ds->hwmon_dev);
  323. #endif
  324. }
  325. #ifdef CONFIG_PM_SLEEP
  326. static int dsa_switch_suspend(struct dsa_switch *ds)
  327. {
  328. int i, ret = 0;
  329. /* Suspend slave network devices */
  330. for (i = 0; i < DSA_MAX_PORTS; i++) {
  331. if (!dsa_is_port_initialized(ds, i))
  332. continue;
  333. ret = dsa_slave_suspend(ds->ports[i]);
  334. if (ret)
  335. return ret;
  336. }
  337. if (ds->drv->suspend)
  338. ret = ds->drv->suspend(ds);
  339. return ret;
  340. }
  341. static int dsa_switch_resume(struct dsa_switch *ds)
  342. {
  343. int i, ret = 0;
  344. if (ds->drv->resume)
  345. ret = ds->drv->resume(ds);
  346. if (ret)
  347. return ret;
  348. /* Resume slave network devices */
  349. for (i = 0; i < DSA_MAX_PORTS; i++) {
  350. if (!dsa_is_port_initialized(ds, i))
  351. continue;
  352. ret = dsa_slave_resume(ds->ports[i]);
  353. if (ret)
  354. return ret;
  355. }
  356. return 0;
  357. }
  358. #endif
  359. /* link polling *************************************************************/
  360. static void dsa_link_poll_work(struct work_struct *ugly)
  361. {
  362. struct dsa_switch_tree *dst;
  363. int i;
  364. dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
  365. for (i = 0; i < dst->pd->nr_chips; i++) {
  366. struct dsa_switch *ds = dst->ds[i];
  367. if (ds != NULL && ds->drv->poll_link != NULL)
  368. ds->drv->poll_link(ds);
  369. }
  370. mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
  371. }
  372. static void dsa_link_poll_timer(unsigned long _dst)
  373. {
  374. struct dsa_switch_tree *dst = (void *)_dst;
  375. schedule_work(&dst->link_poll_work);
  376. }
  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. static struct net_device *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. #ifdef CONFIG_OF
  419. static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
  420. struct dsa_chip_data *cd,
  421. int chip_index, int port_index,
  422. struct device_node *link)
  423. {
  424. const __be32 *reg;
  425. int link_sw_addr;
  426. struct device_node *parent_sw;
  427. int len;
  428. parent_sw = of_get_parent(link);
  429. if (!parent_sw)
  430. return -EINVAL;
  431. reg = of_get_property(parent_sw, "reg", &len);
  432. if (!reg || (len != sizeof(*reg) * 2))
  433. return -EINVAL;
  434. /*
  435. * Get the destination switch number from the second field of its 'reg'
  436. * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
  437. */
  438. link_sw_addr = be32_to_cpup(reg + 1);
  439. if (link_sw_addr >= pd->nr_chips)
  440. return -EINVAL;
  441. /* First time routing table allocation */
  442. if (!cd->rtable) {
  443. cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
  444. GFP_KERNEL);
  445. if (!cd->rtable)
  446. return -ENOMEM;
  447. /* default to no valid uplink/downlink */
  448. memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
  449. }
  450. cd->rtable[link_sw_addr] = port_index;
  451. return 0;
  452. }
  453. static int dsa_of_probe_links(struct dsa_platform_data *pd,
  454. struct dsa_chip_data *cd,
  455. int chip_index, int port_index,
  456. struct device_node *port,
  457. const char *port_name)
  458. {
  459. struct device_node *link;
  460. int link_index;
  461. int ret;
  462. for (link_index = 0;; link_index++) {
  463. link = of_parse_phandle(port, "link", link_index);
  464. if (!link)
  465. break;
  466. if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
  467. ret = dsa_of_setup_routing_table(pd, cd, chip_index,
  468. port_index, link);
  469. if (ret)
  470. return ret;
  471. }
  472. }
  473. return 0;
  474. }
  475. static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
  476. {
  477. int i;
  478. int port_index;
  479. for (i = 0; i < pd->nr_chips; i++) {
  480. port_index = 0;
  481. while (port_index < DSA_MAX_PORTS) {
  482. kfree(pd->chip[i].port_names[port_index]);
  483. port_index++;
  484. }
  485. kfree(pd->chip[i].rtable);
  486. }
  487. kfree(pd->chip);
  488. }
  489. static int dsa_of_probe(struct device *dev)
  490. {
  491. struct device_node *np = dev->of_node;
  492. struct device_node *child, *mdio, *ethernet, *port;
  493. struct mii_bus *mdio_bus, *mdio_bus_switch;
  494. struct net_device *ethernet_dev;
  495. struct dsa_platform_data *pd;
  496. struct dsa_chip_data *cd;
  497. const char *port_name;
  498. int chip_index, port_index;
  499. const unsigned int *sw_addr, *port_reg;
  500. u32 eeprom_len;
  501. int ret;
  502. mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
  503. if (!mdio)
  504. return -EINVAL;
  505. mdio_bus = of_mdio_find_bus(mdio);
  506. if (!mdio_bus)
  507. return -EPROBE_DEFER;
  508. ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
  509. if (!ethernet)
  510. return -EINVAL;
  511. ethernet_dev = of_find_net_device_by_node(ethernet);
  512. if (!ethernet_dev)
  513. return -EPROBE_DEFER;
  514. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  515. if (!pd)
  516. return -ENOMEM;
  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. chip_index++;
  531. cd = &pd->chip[chip_index];
  532. cd->of_node = child;
  533. cd->host_dev = &mdio_bus->dev;
  534. sw_addr = of_get_property(child, "reg", NULL);
  535. if (!sw_addr)
  536. continue;
  537. cd->sw_addr = be32_to_cpup(sw_addr);
  538. if (cd->sw_addr >= PHY_MAX_ADDR)
  539. continue;
  540. if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
  541. cd->eeprom_len = eeprom_len;
  542. mdio = of_parse_phandle(child, "mii-bus", 0);
  543. if (mdio) {
  544. mdio_bus_switch = of_mdio_find_bus(mdio);
  545. if (!mdio_bus_switch) {
  546. ret = -EPROBE_DEFER;
  547. goto out_free_chip;
  548. }
  549. cd->host_dev = &mdio_bus_switch->dev;
  550. }
  551. for_each_available_child_of_node(child, port) {
  552. port_reg = of_get_property(port, "reg", NULL);
  553. if (!port_reg)
  554. continue;
  555. port_index = be32_to_cpup(port_reg);
  556. if (port_index >= DSA_MAX_PORTS)
  557. break;
  558. port_name = of_get_property(port, "label", NULL);
  559. if (!port_name)
  560. continue;
  561. cd->port_dn[port_index] = port;
  562. cd->port_names[port_index] = kstrdup(port_name,
  563. GFP_KERNEL);
  564. if (!cd->port_names[port_index]) {
  565. ret = -ENOMEM;
  566. goto out_free_chip;
  567. }
  568. ret = dsa_of_probe_links(pd, cd, chip_index,
  569. port_index, port, port_name);
  570. if (ret)
  571. goto out_free_chip;
  572. }
  573. }
  574. return 0;
  575. out_free_chip:
  576. dsa_of_free_platform_data(pd);
  577. out_free:
  578. kfree(pd);
  579. dev->platform_data = NULL;
  580. return ret;
  581. }
  582. static void dsa_of_remove(struct device *dev)
  583. {
  584. struct dsa_platform_data *pd = dev->platform_data;
  585. if (!dev->of_node)
  586. return;
  587. dsa_of_free_platform_data(pd);
  588. kfree(pd);
  589. }
  590. #else
  591. static inline int dsa_of_probe(struct device *dev)
  592. {
  593. return 0;
  594. }
  595. static inline void dsa_of_remove(struct device *dev)
  596. {
  597. }
  598. #endif
  599. static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
  600. struct device *parent, struct dsa_platform_data *pd)
  601. {
  602. int i;
  603. dst->pd = pd;
  604. dst->master_netdev = dev;
  605. dst->cpu_switch = -1;
  606. dst->cpu_port = -1;
  607. for (i = 0; i < pd->nr_chips; i++) {
  608. struct dsa_switch *ds;
  609. ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
  610. if (IS_ERR(ds)) {
  611. netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
  612. i, PTR_ERR(ds));
  613. continue;
  614. }
  615. dst->ds[i] = ds;
  616. if (ds->drv->poll_link != NULL)
  617. dst->link_poll_needed = 1;
  618. }
  619. /*
  620. * If we use a tagging format that doesn't have an ethertype
  621. * field, make sure that all packets from this point on get
  622. * sent to the tag format's receive function.
  623. */
  624. wmb();
  625. dev->dsa_ptr = (void *)dst;
  626. if (dst->link_poll_needed) {
  627. INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
  628. init_timer(&dst->link_poll_timer);
  629. dst->link_poll_timer.data = (unsigned long)dst;
  630. dst->link_poll_timer.function = dsa_link_poll_timer;
  631. dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
  632. add_timer(&dst->link_poll_timer);
  633. }
  634. }
  635. static int dsa_probe(struct platform_device *pdev)
  636. {
  637. struct dsa_platform_data *pd = pdev->dev.platform_data;
  638. struct net_device *dev;
  639. struct dsa_switch_tree *dst;
  640. int ret;
  641. pr_notice_once("Distributed Switch Architecture driver version %s\n",
  642. dsa_driver_version);
  643. if (pdev->dev.of_node) {
  644. ret = dsa_of_probe(&pdev->dev);
  645. if (ret)
  646. return ret;
  647. pd = pdev->dev.platform_data;
  648. }
  649. if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
  650. return -EINVAL;
  651. if (pd->of_netdev) {
  652. dev = pd->of_netdev;
  653. dev_hold(dev);
  654. } else {
  655. dev = dev_to_net_device(pd->netdev);
  656. }
  657. if (dev == NULL) {
  658. ret = -EPROBE_DEFER;
  659. goto out;
  660. }
  661. if (dev->dsa_ptr != NULL) {
  662. dev_put(dev);
  663. ret = -EEXIST;
  664. goto out;
  665. }
  666. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  667. if (dst == NULL) {
  668. dev_put(dev);
  669. ret = -ENOMEM;
  670. goto out;
  671. }
  672. platform_set_drvdata(pdev, dst);
  673. dsa_setup_dst(dst, dev, &pdev->dev, pd);
  674. return 0;
  675. out:
  676. dsa_of_remove(&pdev->dev);
  677. return ret;
  678. }
  679. static void dsa_remove_dst(struct dsa_switch_tree *dst)
  680. {
  681. int i;
  682. if (dst->link_poll_needed)
  683. del_timer_sync(&dst->link_poll_timer);
  684. flush_work(&dst->link_poll_work);
  685. for (i = 0; i < dst->pd->nr_chips; i++) {
  686. struct dsa_switch *ds = dst->ds[i];
  687. if (ds != NULL)
  688. dsa_switch_destroy(ds);
  689. }
  690. }
  691. static int dsa_remove(struct platform_device *pdev)
  692. {
  693. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  694. dsa_remove_dst(dst);
  695. dsa_of_remove(&pdev->dev);
  696. return 0;
  697. }
  698. static void dsa_shutdown(struct platform_device *pdev)
  699. {
  700. }
  701. static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
  702. struct packet_type *pt, struct net_device *orig_dev)
  703. {
  704. struct dsa_switch_tree *dst = dev->dsa_ptr;
  705. if (unlikely(dst == NULL)) {
  706. kfree_skb(skb);
  707. return 0;
  708. }
  709. return dst->rcv(skb, dev, pt, orig_dev);
  710. }
  711. static struct packet_type dsa_pack_type __read_mostly = {
  712. .type = cpu_to_be16(ETH_P_XDSA),
  713. .func = dsa_switch_rcv,
  714. };
  715. static struct notifier_block dsa_netdevice_nb __read_mostly = {
  716. .notifier_call = dsa_slave_netdevice_event,
  717. };
  718. #ifdef CONFIG_PM_SLEEP
  719. static int dsa_suspend(struct device *d)
  720. {
  721. struct platform_device *pdev = to_platform_device(d);
  722. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  723. int i, ret = 0;
  724. for (i = 0; i < dst->pd->nr_chips; i++) {
  725. struct dsa_switch *ds = dst->ds[i];
  726. if (ds != NULL)
  727. ret = dsa_switch_suspend(ds);
  728. }
  729. return ret;
  730. }
  731. static int dsa_resume(struct device *d)
  732. {
  733. struct platform_device *pdev = to_platform_device(d);
  734. struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
  735. int i, ret = 0;
  736. for (i = 0; i < dst->pd->nr_chips; i++) {
  737. struct dsa_switch *ds = dst->ds[i];
  738. if (ds != NULL)
  739. ret = dsa_switch_resume(ds);
  740. }
  741. return ret;
  742. }
  743. #endif
  744. static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
  745. static const struct of_device_id dsa_of_match_table[] = {
  746. { .compatible = "brcm,bcm7445-switch-v4.0" },
  747. { .compatible = "marvell,dsa", },
  748. {}
  749. };
  750. MODULE_DEVICE_TABLE(of, dsa_of_match_table);
  751. static struct platform_driver dsa_driver = {
  752. .probe = dsa_probe,
  753. .remove = dsa_remove,
  754. .shutdown = dsa_shutdown,
  755. .driver = {
  756. .name = "dsa",
  757. .of_match_table = dsa_of_match_table,
  758. .pm = &dsa_pm_ops,
  759. },
  760. };
  761. static int __init dsa_init_module(void)
  762. {
  763. int rc;
  764. register_netdevice_notifier(&dsa_netdevice_nb);
  765. rc = platform_driver_register(&dsa_driver);
  766. if (rc)
  767. return rc;
  768. dev_add_pack(&dsa_pack_type);
  769. return 0;
  770. }
  771. module_init(dsa_init_module);
  772. static void __exit dsa_cleanup_module(void)
  773. {
  774. unregister_netdevice_notifier(&dsa_netdevice_nb);
  775. dev_remove_pack(&dsa_pack_type);
  776. platform_driver_unregister(&dsa_driver);
  777. }
  778. module_exit(dsa_cleanup_module);
  779. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  780. MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
  781. MODULE_LICENSE("GPL");
  782. MODULE_ALIAS("platform:dsa");