switch.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. if (!ds->ops->port_mdb_add)
  102. return;
  103. for_each_set_bit(port, bitmap, ds->num_ports)
  104. ds->ops->port_mdb_add(ds, port, mdb);
  105. }
  106. static int dsa_switch_mdb_add(struct dsa_switch *ds,
  107. struct dsa_notifier_mdb_info *info)
  108. {
  109. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  110. struct switchdev_trans *trans = info->trans;
  111. int port;
  112. /* Build a mask of Multicast group members */
  113. bitmap_zero(ds->bitmap, ds->num_ports);
  114. if (ds->index == info->sw_index)
  115. set_bit(info->port, ds->bitmap);
  116. for (port = 0; port < ds->num_ports; port++)
  117. if (dsa_is_dsa_port(ds, port))
  118. set_bit(port, ds->bitmap);
  119. if (switchdev_trans_ph_prepare(trans))
  120. return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
  121. dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
  122. return 0;
  123. }
  124. static int dsa_switch_mdb_del(struct dsa_switch *ds,
  125. struct dsa_notifier_mdb_info *info)
  126. {
  127. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  128. if (!ds->ops->port_mdb_del)
  129. return -EOPNOTSUPP;
  130. if (ds->index == info->sw_index)
  131. return ds->ops->port_mdb_del(ds, info->port, mdb);
  132. return 0;
  133. }
  134. static int
  135. dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
  136. const struct switchdev_obj_port_vlan *vlan,
  137. const unsigned long *bitmap)
  138. {
  139. int port, err;
  140. if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
  141. return -EOPNOTSUPP;
  142. for_each_set_bit(port, bitmap, ds->num_ports) {
  143. err = ds->ops->port_vlan_prepare(ds, port, vlan);
  144. if (err)
  145. return err;
  146. }
  147. return 0;
  148. }
  149. static void
  150. dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
  151. const struct switchdev_obj_port_vlan *vlan,
  152. const unsigned long *bitmap)
  153. {
  154. int port;
  155. for_each_set_bit(port, bitmap, ds->num_ports)
  156. ds->ops->port_vlan_add(ds, port, vlan);
  157. }
  158. static int dsa_switch_vlan_add(struct dsa_switch *ds,
  159. struct dsa_notifier_vlan_info *info)
  160. {
  161. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  162. struct switchdev_trans *trans = info->trans;
  163. int port;
  164. /* Build a mask of VLAN members */
  165. bitmap_zero(ds->bitmap, ds->num_ports);
  166. if (ds->index == info->sw_index)
  167. set_bit(info->port, ds->bitmap);
  168. for (port = 0; port < ds->num_ports; port++)
  169. if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
  170. set_bit(port, ds->bitmap);
  171. if (switchdev_trans_ph_prepare(trans))
  172. return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
  173. dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
  174. return 0;
  175. }
  176. static int dsa_switch_vlan_del(struct dsa_switch *ds,
  177. struct dsa_notifier_vlan_info *info)
  178. {
  179. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  180. if (!ds->ops->port_vlan_del)
  181. return -EOPNOTSUPP;
  182. if (ds->index == info->sw_index)
  183. return ds->ops->port_vlan_del(ds, info->port, vlan);
  184. return 0;
  185. }
  186. static int dsa_switch_event(struct notifier_block *nb,
  187. unsigned long event, void *info)
  188. {
  189. struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
  190. int err;
  191. switch (event) {
  192. case DSA_NOTIFIER_AGEING_TIME:
  193. err = dsa_switch_ageing_time(ds, info);
  194. break;
  195. case DSA_NOTIFIER_BRIDGE_JOIN:
  196. err = dsa_switch_bridge_join(ds, info);
  197. break;
  198. case DSA_NOTIFIER_BRIDGE_LEAVE:
  199. err = dsa_switch_bridge_leave(ds, info);
  200. break;
  201. case DSA_NOTIFIER_FDB_ADD:
  202. err = dsa_switch_fdb_add(ds, info);
  203. break;
  204. case DSA_NOTIFIER_FDB_DEL:
  205. err = dsa_switch_fdb_del(ds, info);
  206. break;
  207. case DSA_NOTIFIER_MDB_ADD:
  208. err = dsa_switch_mdb_add(ds, info);
  209. break;
  210. case DSA_NOTIFIER_MDB_DEL:
  211. err = dsa_switch_mdb_del(ds, info);
  212. break;
  213. case DSA_NOTIFIER_VLAN_ADD:
  214. err = dsa_switch_vlan_add(ds, info);
  215. break;
  216. case DSA_NOTIFIER_VLAN_DEL:
  217. err = dsa_switch_vlan_del(ds, info);
  218. break;
  219. default:
  220. err = -EOPNOTSUPP;
  221. break;
  222. }
  223. /* Non-switchdev operations cannot be rolled back. If a DSA driver
  224. * returns an error during the chained call, switch chips may be in an
  225. * inconsistent state.
  226. */
  227. if (err)
  228. dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
  229. event, err);
  230. return notifier_from_errno(err);
  231. }
  232. int dsa_switch_register_notifier(struct dsa_switch *ds)
  233. {
  234. ds->nb.notifier_call = dsa_switch_event;
  235. return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
  236. }
  237. void dsa_switch_unregister_notifier(struct dsa_switch *ds)
  238. {
  239. int err;
  240. err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
  241. if (err)
  242. dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
  243. }