dsa.c 23 KB

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