port.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Handling of a single switch port
  3. *
  4. * Copyright (c) 2017 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/if_bridge.h>
  13. #include <linux/notifier.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/of_net.h>
  16. #include "dsa_priv.h"
  17. static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
  18. {
  19. struct raw_notifier_head *nh = &dp->ds->dst->nh;
  20. int err;
  21. err = raw_notifier_call_chain(nh, e, v);
  22. return notifier_to_errno(err);
  23. }
  24. int dsa_port_set_state(struct dsa_port *dp, u8 state,
  25. struct switchdev_trans *trans)
  26. {
  27. struct dsa_switch *ds = dp->ds;
  28. int port = dp->index;
  29. if (switchdev_trans_ph_prepare(trans))
  30. return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
  31. if (ds->ops->port_stp_state_set)
  32. ds->ops->port_stp_state_set(ds, port, state);
  33. if (ds->ops->port_fast_age) {
  34. /* Fast age FDB entries or flush appropriate forwarding database
  35. * for the given port, if we are moving it from Learning or
  36. * Forwarding state, to Disabled or Blocking or Listening state.
  37. */
  38. if ((dp->stp_state == BR_STATE_LEARNING ||
  39. dp->stp_state == BR_STATE_FORWARDING) &&
  40. (state == BR_STATE_DISABLED ||
  41. state == BR_STATE_BLOCKING ||
  42. state == BR_STATE_LISTENING))
  43. ds->ops->port_fast_age(ds, port);
  44. }
  45. dp->stp_state = state;
  46. return 0;
  47. }
  48. static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
  49. {
  50. int err;
  51. err = dsa_port_set_state(dp, state, NULL);
  52. if (err)
  53. pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
  54. }
  55. int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
  56. {
  57. struct dsa_switch *ds = dp->ds;
  58. int port = dp->index;
  59. int err;
  60. if (ds->ops->port_enable) {
  61. err = ds->ops->port_enable(ds, port, phy);
  62. if (err)
  63. return err;
  64. }
  65. if (!dp->bridge_dev)
  66. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  67. return 0;
  68. }
  69. void dsa_port_disable(struct dsa_port *dp, struct phy_device *phy)
  70. {
  71. struct dsa_switch *ds = dp->ds;
  72. int port = dp->index;
  73. if (!dp->bridge_dev)
  74. dsa_port_set_state_now(dp, BR_STATE_DISABLED);
  75. if (ds->ops->port_disable)
  76. ds->ops->port_disable(ds, port, phy);
  77. }
  78. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
  79. {
  80. struct dsa_notifier_bridge_info info = {
  81. .sw_index = dp->ds->index,
  82. .port = dp->index,
  83. .br = br,
  84. };
  85. int err;
  86. /* Here the port is already bridged. Reflect the current configuration
  87. * so that drivers can program their chips accordingly.
  88. */
  89. dp->bridge_dev = br;
  90. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
  91. /* The bridging is rolled back on error */
  92. if (err)
  93. dp->bridge_dev = NULL;
  94. return err;
  95. }
  96. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  97. {
  98. struct dsa_notifier_bridge_info info = {
  99. .sw_index = dp->ds->index,
  100. .port = dp->index,
  101. .br = br,
  102. };
  103. int err;
  104. /* Here the port is already unbridged. Reflect the current configuration
  105. * so that drivers can program their chips accordingly.
  106. */
  107. dp->bridge_dev = NULL;
  108. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  109. if (err)
  110. pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
  111. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  112. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  113. */
  114. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  115. }
  116. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  117. struct switchdev_trans *trans)
  118. {
  119. struct dsa_switch *ds = dp->ds;
  120. /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
  121. if (switchdev_trans_ph_prepare(trans))
  122. return 0;
  123. if (ds->ops->port_vlan_filtering)
  124. return ds->ops->port_vlan_filtering(ds, dp->index,
  125. vlan_filtering);
  126. return 0;
  127. }
  128. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
  129. struct switchdev_trans *trans)
  130. {
  131. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  132. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  133. struct dsa_notifier_ageing_time_info info = {
  134. .ageing_time = ageing_time,
  135. .trans = trans,
  136. };
  137. if (switchdev_trans_ph_prepare(trans))
  138. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  139. dp->ageing_time = ageing_time;
  140. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  141. }
  142. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  143. u16 vid)
  144. {
  145. struct dsa_notifier_fdb_info info = {
  146. .sw_index = dp->ds->index,
  147. .port = dp->index,
  148. .addr = addr,
  149. .vid = vid,
  150. };
  151. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  152. }
  153. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  154. u16 vid)
  155. {
  156. struct dsa_notifier_fdb_info info = {
  157. .sw_index = dp->ds->index,
  158. .port = dp->index,
  159. .addr = addr,
  160. .vid = vid,
  161. };
  162. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  163. }
  164. int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
  165. {
  166. struct dsa_switch *ds = dp->ds;
  167. int port = dp->index;
  168. if (!ds->ops->port_fdb_dump)
  169. return -EOPNOTSUPP;
  170. return ds->ops->port_fdb_dump(ds, port, cb, data);
  171. }
  172. int dsa_port_mdb_add(const struct dsa_port *dp,
  173. const struct switchdev_obj_port_mdb *mdb,
  174. struct switchdev_trans *trans)
  175. {
  176. struct dsa_notifier_mdb_info info = {
  177. .sw_index = dp->ds->index,
  178. .port = dp->index,
  179. .trans = trans,
  180. .mdb = mdb,
  181. };
  182. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  183. }
  184. int dsa_port_mdb_del(const struct dsa_port *dp,
  185. const struct switchdev_obj_port_mdb *mdb)
  186. {
  187. struct dsa_notifier_mdb_info info = {
  188. .sw_index = dp->ds->index,
  189. .port = dp->index,
  190. .mdb = mdb,
  191. };
  192. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  193. }
  194. int dsa_port_vlan_add(struct dsa_port *dp,
  195. const struct switchdev_obj_port_vlan *vlan,
  196. struct switchdev_trans *trans)
  197. {
  198. struct dsa_notifier_vlan_info info = {
  199. .sw_index = dp->ds->index,
  200. .port = dp->index,
  201. .trans = trans,
  202. .vlan = vlan,
  203. };
  204. if (netif_is_bridge_master(vlan->obj.orig_dev))
  205. return -EOPNOTSUPP;
  206. if (br_vlan_enabled(dp->bridge_dev))
  207. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  208. return 0;
  209. }
  210. int dsa_port_vlan_del(struct dsa_port *dp,
  211. const struct switchdev_obj_port_vlan *vlan)
  212. {
  213. struct dsa_notifier_vlan_info info = {
  214. .sw_index = dp->ds->index,
  215. .port = dp->index,
  216. .vlan = vlan,
  217. };
  218. if (netif_is_bridge_master(vlan->obj.orig_dev))
  219. return -EOPNOTSUPP;
  220. if (br_vlan_enabled(dp->bridge_dev))
  221. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  222. return 0;
  223. }
  224. static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
  225. {
  226. struct device_node *phy_dn;
  227. struct phy_device *phydev;
  228. phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
  229. if (!phy_dn)
  230. return NULL;
  231. phydev = of_phy_find_device(phy_dn);
  232. if (!phydev) {
  233. of_node_put(phy_dn);
  234. return ERR_PTR(-EPROBE_DEFER);
  235. }
  236. return phydev;
  237. }
  238. static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
  239. {
  240. struct dsa_switch *ds = dp->ds;
  241. struct phy_device *phydev;
  242. int port = dp->index;
  243. int err = 0;
  244. phydev = dsa_port_get_phy_device(dp);
  245. if (!phydev)
  246. return 0;
  247. if (IS_ERR(phydev))
  248. return PTR_ERR(phydev);
  249. if (enable) {
  250. err = genphy_config_init(phydev);
  251. if (err < 0)
  252. goto err_put_dev;
  253. err = genphy_resume(phydev);
  254. if (err < 0)
  255. goto err_put_dev;
  256. err = genphy_read_status(phydev);
  257. if (err < 0)
  258. goto err_put_dev;
  259. } else {
  260. err = genphy_suspend(phydev);
  261. if (err < 0)
  262. goto err_put_dev;
  263. }
  264. if (ds->ops->adjust_link)
  265. ds->ops->adjust_link(ds, port, phydev);
  266. dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
  267. err_put_dev:
  268. put_device(&phydev->mdio.dev);
  269. return err;
  270. }
  271. static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
  272. {
  273. struct device_node *dn = dp->dn;
  274. struct dsa_switch *ds = dp->ds;
  275. struct phy_device *phydev;
  276. int port = dp->index;
  277. int mode;
  278. int err;
  279. err = of_phy_register_fixed_link(dn);
  280. if (err) {
  281. dev_err(ds->dev,
  282. "failed to register the fixed PHY of port %d\n",
  283. port);
  284. return err;
  285. }
  286. phydev = of_phy_find_device(dn);
  287. mode = of_get_phy_mode(dn);
  288. if (mode < 0)
  289. mode = PHY_INTERFACE_MODE_NA;
  290. phydev->interface = mode;
  291. genphy_config_init(phydev);
  292. genphy_read_status(phydev);
  293. if (ds->ops->adjust_link)
  294. ds->ops->adjust_link(ds, port, phydev);
  295. put_device(&phydev->mdio.dev);
  296. return 0;
  297. }
  298. int dsa_port_link_register_of(struct dsa_port *dp)
  299. {
  300. if (of_phy_is_fixed_link(dp->dn))
  301. return dsa_port_fixed_link_register_of(dp);
  302. else
  303. return dsa_port_setup_phy_of(dp, true);
  304. }
  305. void dsa_port_link_unregister_of(struct dsa_port *dp)
  306. {
  307. if (of_phy_is_fixed_link(dp->dn))
  308. of_phy_deregister_fixed_link(dp->dn);
  309. else
  310. dsa_port_setup_phy_of(dp, false);
  311. }
  312. int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
  313. {
  314. struct phy_device *phydev;
  315. int ret = -EOPNOTSUPP;
  316. if (of_phy_is_fixed_link(dp->dn))
  317. return ret;
  318. phydev = dsa_port_get_phy_device(dp);
  319. if (IS_ERR_OR_NULL(phydev))
  320. return ret;
  321. ret = phy_ethtool_get_strings(phydev, data);
  322. put_device(&phydev->mdio.dev);
  323. return ret;
  324. }
  325. EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
  326. int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
  327. {
  328. struct phy_device *phydev;
  329. int ret = -EOPNOTSUPP;
  330. if (of_phy_is_fixed_link(dp->dn))
  331. return ret;
  332. phydev = dsa_port_get_phy_device(dp);
  333. if (IS_ERR_OR_NULL(phydev))
  334. return ret;
  335. ret = phy_ethtool_get_stats(phydev, NULL, data);
  336. put_device(&phydev->mdio.dev);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
  340. int dsa_port_get_phy_sset_count(struct dsa_port *dp)
  341. {
  342. struct phy_device *phydev;
  343. int ret = -EOPNOTSUPP;
  344. if (of_phy_is_fixed_link(dp->dn))
  345. return ret;
  346. phydev = dsa_port_get_phy_device(dp);
  347. if (IS_ERR_OR_NULL(phydev))
  348. return ret;
  349. ret = phy_ethtool_get_sset_count(phydev);
  350. put_device(&phydev->mdio.dev);
  351. return ret;
  352. }
  353. EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);