port.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING;
  58. struct dsa_switch *ds = dp->ds;
  59. int port = dp->index;
  60. int err;
  61. if (ds->ops->port_enable) {
  62. err = ds->ops->port_enable(ds, port, phy);
  63. if (err)
  64. return err;
  65. }
  66. dsa_port_set_state_now(dp, stp_state);
  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. dsa_port_set_state_now(dp, BR_STATE_DISABLED);
  74. if (ds->ops->port_disable)
  75. ds->ops->port_disable(ds, port, phy);
  76. }
  77. int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
  78. {
  79. struct dsa_notifier_bridge_info info = {
  80. .sw_index = dp->ds->index,
  81. .port = dp->index,
  82. .br = br,
  83. };
  84. int err;
  85. /* Here the port is already bridged. Reflect the current configuration
  86. * so that drivers can program their chips accordingly.
  87. */
  88. dp->bridge_dev = br;
  89. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
  90. /* The bridging is rolled back on error */
  91. if (err)
  92. dp->bridge_dev = NULL;
  93. return err;
  94. }
  95. void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
  96. {
  97. struct dsa_notifier_bridge_info info = {
  98. .sw_index = dp->ds->index,
  99. .port = dp->index,
  100. .br = br,
  101. };
  102. int err;
  103. /* Here the port is already unbridged. Reflect the current configuration
  104. * so that drivers can program their chips accordingly.
  105. */
  106. dp->bridge_dev = NULL;
  107. err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
  108. if (err)
  109. pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
  110. /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
  111. * so allow it to be in BR_STATE_FORWARDING to be kept functional
  112. */
  113. dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  114. }
  115. int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
  116. struct switchdev_trans *trans)
  117. {
  118. struct dsa_switch *ds = dp->ds;
  119. /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
  120. if (switchdev_trans_ph_prepare(trans))
  121. return 0;
  122. if (ds->ops->port_vlan_filtering)
  123. return ds->ops->port_vlan_filtering(ds, dp->index,
  124. vlan_filtering);
  125. return 0;
  126. }
  127. int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
  128. struct switchdev_trans *trans)
  129. {
  130. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
  131. unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
  132. struct dsa_notifier_ageing_time_info info = {
  133. .ageing_time = ageing_time,
  134. .trans = trans,
  135. };
  136. if (switchdev_trans_ph_prepare(trans))
  137. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  138. dp->ageing_time = ageing_time;
  139. return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
  140. }
  141. int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
  142. u16 vid)
  143. {
  144. struct dsa_notifier_fdb_info info = {
  145. .sw_index = dp->ds->index,
  146. .port = dp->index,
  147. .addr = addr,
  148. .vid = vid,
  149. };
  150. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
  151. }
  152. int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
  153. u16 vid)
  154. {
  155. struct dsa_notifier_fdb_info info = {
  156. .sw_index = dp->ds->index,
  157. .port = dp->index,
  158. .addr = addr,
  159. .vid = vid,
  160. };
  161. return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
  162. }
  163. int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
  164. {
  165. struct dsa_switch *ds = dp->ds;
  166. int port = dp->index;
  167. if (!ds->ops->port_fdb_dump)
  168. return -EOPNOTSUPP;
  169. return ds->ops->port_fdb_dump(ds, port, cb, data);
  170. }
  171. int dsa_port_mdb_add(const struct dsa_port *dp,
  172. const struct switchdev_obj_port_mdb *mdb,
  173. struct switchdev_trans *trans)
  174. {
  175. struct dsa_notifier_mdb_info info = {
  176. .sw_index = dp->ds->index,
  177. .port = dp->index,
  178. .trans = trans,
  179. .mdb = mdb,
  180. };
  181. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
  182. }
  183. int dsa_port_mdb_del(const struct dsa_port *dp,
  184. const struct switchdev_obj_port_mdb *mdb)
  185. {
  186. struct dsa_notifier_mdb_info info = {
  187. .sw_index = dp->ds->index,
  188. .port = dp->index,
  189. .mdb = mdb,
  190. };
  191. return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
  192. }
  193. int dsa_port_vlan_add(struct dsa_port *dp,
  194. const struct switchdev_obj_port_vlan *vlan,
  195. struct switchdev_trans *trans)
  196. {
  197. struct dsa_notifier_vlan_info info = {
  198. .sw_index = dp->ds->index,
  199. .port = dp->index,
  200. .trans = trans,
  201. .vlan = vlan,
  202. };
  203. if (br_vlan_enabled(dp->bridge_dev))
  204. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
  205. return 0;
  206. }
  207. int dsa_port_vlan_del(struct dsa_port *dp,
  208. const struct switchdev_obj_port_vlan *vlan)
  209. {
  210. struct dsa_notifier_vlan_info info = {
  211. .sw_index = dp->ds->index,
  212. .port = dp->index,
  213. .vlan = vlan,
  214. };
  215. if (br_vlan_enabled(dp->bridge_dev))
  216. return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
  217. return 0;
  218. }
  219. int dsa_port_fixed_link_register_of(struct dsa_port *dp)
  220. {
  221. struct device_node *dn = dp->dn;
  222. struct dsa_switch *ds = dp->ds;
  223. struct phy_device *phydev;
  224. int port = dp->index;
  225. int mode;
  226. int err;
  227. if (of_phy_is_fixed_link(dn)) {
  228. err = of_phy_register_fixed_link(dn);
  229. if (err) {
  230. dev_err(ds->dev,
  231. "failed to register the fixed PHY of port %d\n",
  232. port);
  233. return err;
  234. }
  235. phydev = of_phy_find_device(dn);
  236. mode = of_get_phy_mode(dn);
  237. if (mode < 0)
  238. mode = PHY_INTERFACE_MODE_NA;
  239. phydev->interface = mode;
  240. genphy_config_init(phydev);
  241. genphy_read_status(phydev);
  242. if (ds->ops->adjust_link)
  243. ds->ops->adjust_link(ds, port, phydev);
  244. put_device(&phydev->mdio.dev);
  245. }
  246. return 0;
  247. }
  248. void dsa_port_fixed_link_unregister_of(struct dsa_port *dp)
  249. {
  250. struct device_node *dn = dp->dn;
  251. if (of_phy_is_fixed_link(dn))
  252. of_phy_deregister_fixed_link(dn);
  253. }