dsa.c 24 KB

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