switch.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Handling of a single switch chip, part of a switch fabric
  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/netdevice.h>
  13. #include <linux/notifier.h>
  14. #include <net/switchdev.h>
  15. #include "dsa_priv.h"
  16. static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
  17. unsigned int ageing_time)
  18. {
  19. int i;
  20. for (i = 0; i < ds->num_ports; ++i) {
  21. struct dsa_port *dp = &ds->ports[i];
  22. if (dp->ageing_time && dp->ageing_time < ageing_time)
  23. ageing_time = dp->ageing_time;
  24. }
  25. return ageing_time;
  26. }
  27. static int dsa_switch_ageing_time(struct dsa_switch *ds,
  28. struct dsa_notifier_ageing_time_info *info)
  29. {
  30. unsigned int ageing_time = info->ageing_time;
  31. struct switchdev_trans *trans = info->trans;
  32. if (switchdev_trans_ph_prepare(trans)) {
  33. if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
  34. return -ERANGE;
  35. if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
  36. return -ERANGE;
  37. return 0;
  38. }
  39. /* Program the fastest ageing time in case of multiple bridges */
  40. ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
  41. if (ds->ops->set_ageing_time)
  42. return ds->ops->set_ageing_time(ds, ageing_time);
  43. return 0;
  44. }
  45. static int dsa_switch_bridge_join(struct dsa_switch *ds,
  46. struct dsa_notifier_bridge_info *info)
  47. {
  48. if (ds->index == info->sw_index && ds->ops->port_bridge_join)
  49. return ds->ops->port_bridge_join(ds, info->port, info->br);
  50. if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
  51. return ds->ops->crosschip_bridge_join(ds, info->sw_index,
  52. info->port, info->br);
  53. return 0;
  54. }
  55. static int dsa_switch_bridge_leave(struct dsa_switch *ds,
  56. struct dsa_notifier_bridge_info *info)
  57. {
  58. if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
  59. ds->ops->port_bridge_leave(ds, info->port, info->br);
  60. if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
  61. ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
  62. info->br);
  63. return 0;
  64. }
  65. static int dsa_switch_fdb_add(struct dsa_switch *ds,
  66. struct dsa_notifier_fdb_info *info)
  67. {
  68. int port = dsa_towards_port(ds, info->sw_index, info->port);
  69. if (!ds->ops->port_fdb_add)
  70. return -EOPNOTSUPP;
  71. return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
  72. }
  73. static int dsa_switch_fdb_del(struct dsa_switch *ds,
  74. struct dsa_notifier_fdb_info *info)
  75. {
  76. int port = dsa_towards_port(ds, info->sw_index, info->port);
  77. if (!ds->ops->port_fdb_del)
  78. return -EOPNOTSUPP;
  79. return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
  80. }
  81. static int
  82. dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
  83. const struct switchdev_obj_port_mdb *mdb,
  84. const unsigned long *bitmap)
  85. {
  86. int port, err;
  87. if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
  88. return -EOPNOTSUPP;
  89. for_each_set_bit(port, bitmap, ds->num_ports) {
  90. err = ds->ops->port_mdb_prepare(ds, port, mdb);
  91. if (err)
  92. return err;
  93. }
  94. return 0;
  95. }
  96. static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
  97. const struct switchdev_obj_port_mdb *mdb,
  98. const unsigned long *bitmap)
  99. {
  100. int port;
  101. for_each_set_bit(port, bitmap, ds->num_ports)
  102. ds->ops->port_mdb_add(ds, port, mdb);
  103. }
  104. static int dsa_switch_mdb_add(struct dsa_switch *ds,
  105. struct dsa_notifier_mdb_info *info)
  106. {
  107. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  108. struct switchdev_trans *trans = info->trans;
  109. int port;
  110. /* Build a mask of Multicast group members */
  111. bitmap_zero(ds->bitmap, ds->num_ports);
  112. if (ds->index == info->sw_index)
  113. set_bit(info->port, ds->bitmap);
  114. for (port = 0; port < ds->num_ports; port++)
  115. if (dsa_is_dsa_port(ds, port))
  116. set_bit(port, ds->bitmap);
  117. if (switchdev_trans_ph_prepare(trans))
  118. return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
  119. dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
  120. return 0;
  121. }
  122. static int dsa_switch_mdb_del(struct dsa_switch *ds,
  123. struct dsa_notifier_mdb_info *info)
  124. {
  125. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  126. if (!ds->ops->port_mdb_del)
  127. return -EOPNOTSUPP;
  128. if (ds->index == info->sw_index)
  129. return ds->ops->port_mdb_del(ds, info->port, mdb);
  130. return 0;
  131. }
  132. static int
  133. dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
  134. const struct switchdev_obj_port_vlan *vlan,
  135. const unsigned long *bitmap)
  136. {
  137. int port, err;
  138. if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
  139. return -EOPNOTSUPP;
  140. for_each_set_bit(port, bitmap, ds->num_ports) {
  141. err = ds->ops->port_vlan_prepare(ds, port, vlan);
  142. if (err)
  143. return err;
  144. }
  145. return 0;
  146. }
  147. static void
  148. dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
  149. const struct switchdev_obj_port_vlan *vlan,
  150. const unsigned long *bitmap)
  151. {
  152. int port;
  153. for_each_set_bit(port, bitmap, ds->num_ports)
  154. ds->ops->port_vlan_add(ds, port, vlan);
  155. }
  156. static int dsa_switch_vlan_add(struct dsa_switch *ds,
  157. struct dsa_notifier_vlan_info *info)
  158. {
  159. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  160. struct switchdev_trans *trans = info->trans;
  161. int port;
  162. /* Build a mask of VLAN members */
  163. bitmap_zero(ds->bitmap, ds->num_ports);
  164. if (ds->index == info->sw_index)
  165. set_bit(info->port, ds->bitmap);
  166. for (port = 0; port < ds->num_ports; port++)
  167. if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
  168. set_bit(port, ds->bitmap);
  169. if (switchdev_trans_ph_prepare(trans))
  170. return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
  171. dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
  172. return 0;
  173. }
  174. static int dsa_switch_vlan_del(struct dsa_switch *ds,
  175. struct dsa_notifier_vlan_info *info)
  176. {
  177. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  178. if (!ds->ops->port_vlan_del)
  179. return -EOPNOTSUPP;
  180. if (ds->index == info->sw_index)
  181. return ds->ops->port_vlan_del(ds, info->port, vlan);
  182. return 0;
  183. }
  184. static int dsa_switch_event(struct notifier_block *nb,
  185. unsigned long event, void *info)
  186. {
  187. struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
  188. int err;
  189. switch (event) {
  190. case DSA_NOTIFIER_AGEING_TIME:
  191. err = dsa_switch_ageing_time(ds, info);
  192. break;
  193. case DSA_NOTIFIER_BRIDGE_JOIN:
  194. err = dsa_switch_bridge_join(ds, info);
  195. break;
  196. case DSA_NOTIFIER_BRIDGE_LEAVE:
  197. err = dsa_switch_bridge_leave(ds, info);
  198. break;
  199. case DSA_NOTIFIER_FDB_ADD:
  200. err = dsa_switch_fdb_add(ds, info);
  201. break;
  202. case DSA_NOTIFIER_FDB_DEL:
  203. err = dsa_switch_fdb_del(ds, info);
  204. break;
  205. case DSA_NOTIFIER_MDB_ADD:
  206. err = dsa_switch_mdb_add(ds, info);
  207. break;
  208. case DSA_NOTIFIER_MDB_DEL:
  209. err = dsa_switch_mdb_del(ds, info);
  210. break;
  211. case DSA_NOTIFIER_VLAN_ADD:
  212. err = dsa_switch_vlan_add(ds, info);
  213. break;
  214. case DSA_NOTIFIER_VLAN_DEL:
  215. err = dsa_switch_vlan_del(ds, info);
  216. break;
  217. default:
  218. err = -EOPNOTSUPP;
  219. break;
  220. }
  221. /* Non-switchdev operations cannot be rolled back. If a DSA driver
  222. * returns an error during the chained call, switch chips may be in an
  223. * inconsistent state.
  224. */
  225. if (err)
  226. dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
  227. event, err);
  228. return notifier_from_errno(err);
  229. }
  230. int dsa_switch_register_notifier(struct dsa_switch *ds)
  231. {
  232. ds->nb.notifier_call = dsa_switch_event;
  233. return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
  234. }
  235. void dsa_switch_unregister_notifier(struct dsa_switch *ds)
  236. {
  237. int err;
  238. err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
  239. if (err)
  240. dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
  241. }