switch.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. const struct switchdev_obj_port_fdb *fdb = info->fdb;
  69. struct switchdev_trans *trans = info->trans;
  70. /* Do not care yet about other switch chips of the fabric */
  71. if (ds->index != info->sw_index)
  72. return 0;
  73. if (switchdev_trans_ph_prepare(trans)) {
  74. if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
  75. return -EOPNOTSUPP;
  76. return ds->ops->port_fdb_prepare(ds, info->port, fdb, trans);
  77. }
  78. ds->ops->port_fdb_add(ds, info->port, fdb, trans);
  79. return 0;
  80. }
  81. static int dsa_switch_fdb_del(struct dsa_switch *ds,
  82. struct dsa_notifier_fdb_info *info)
  83. {
  84. const struct switchdev_obj_port_fdb *fdb = info->fdb;
  85. /* Do not care yet about other switch chips of the fabric */
  86. if (ds->index != info->sw_index)
  87. return 0;
  88. if (!ds->ops->port_fdb_del)
  89. return -EOPNOTSUPP;
  90. return ds->ops->port_fdb_del(ds, info->port, fdb);
  91. }
  92. static int dsa_switch_mdb_add(struct dsa_switch *ds,
  93. struct dsa_notifier_mdb_info *info)
  94. {
  95. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  96. struct switchdev_trans *trans = info->trans;
  97. /* Do not care yet about other switch chips of the fabric */
  98. if (ds->index != info->sw_index)
  99. return 0;
  100. if (switchdev_trans_ph_prepare(trans)) {
  101. if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
  102. return -EOPNOTSUPP;
  103. return ds->ops->port_mdb_prepare(ds, info->port, mdb, trans);
  104. }
  105. ds->ops->port_mdb_add(ds, info->port, mdb, trans);
  106. return 0;
  107. }
  108. static int dsa_switch_mdb_del(struct dsa_switch *ds,
  109. struct dsa_notifier_mdb_info *info)
  110. {
  111. const struct switchdev_obj_port_mdb *mdb = info->mdb;
  112. /* Do not care yet about other switch chips of the fabric */
  113. if (ds->index != info->sw_index)
  114. return 0;
  115. if (!ds->ops->port_mdb_del)
  116. return -EOPNOTSUPP;
  117. return ds->ops->port_mdb_del(ds, info->port, mdb);
  118. }
  119. static int dsa_switch_vlan_add(struct dsa_switch *ds,
  120. struct dsa_notifier_vlan_info *info)
  121. {
  122. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  123. struct switchdev_trans *trans = info->trans;
  124. /* Do not care yet about other switch chips of the fabric */
  125. if (ds->index != info->sw_index)
  126. return 0;
  127. if (switchdev_trans_ph_prepare(trans)) {
  128. if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
  129. return -EOPNOTSUPP;
  130. return ds->ops->port_vlan_prepare(ds, info->port, vlan, trans);
  131. }
  132. ds->ops->port_vlan_add(ds, info->port, vlan, trans);
  133. return 0;
  134. }
  135. static int dsa_switch_vlan_del(struct dsa_switch *ds,
  136. struct dsa_notifier_vlan_info *info)
  137. {
  138. const struct switchdev_obj_port_vlan *vlan = info->vlan;
  139. /* Do not care yet about other switch chips of the fabric */
  140. if (ds->index != info->sw_index)
  141. return 0;
  142. if (!ds->ops->port_vlan_del)
  143. return -EOPNOTSUPP;
  144. return ds->ops->port_vlan_del(ds, info->port, vlan);
  145. }
  146. static int dsa_switch_event(struct notifier_block *nb,
  147. unsigned long event, void *info)
  148. {
  149. struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
  150. int err;
  151. switch (event) {
  152. case DSA_NOTIFIER_AGEING_TIME:
  153. err = dsa_switch_ageing_time(ds, info);
  154. break;
  155. case DSA_NOTIFIER_BRIDGE_JOIN:
  156. err = dsa_switch_bridge_join(ds, info);
  157. break;
  158. case DSA_NOTIFIER_BRIDGE_LEAVE:
  159. err = dsa_switch_bridge_leave(ds, info);
  160. break;
  161. case DSA_NOTIFIER_FDB_ADD:
  162. err = dsa_switch_fdb_add(ds, info);
  163. break;
  164. case DSA_NOTIFIER_FDB_DEL:
  165. err = dsa_switch_fdb_del(ds, info);
  166. break;
  167. case DSA_NOTIFIER_MDB_ADD:
  168. err = dsa_switch_mdb_add(ds, info);
  169. break;
  170. case DSA_NOTIFIER_MDB_DEL:
  171. err = dsa_switch_mdb_del(ds, info);
  172. break;
  173. case DSA_NOTIFIER_VLAN_ADD:
  174. err = dsa_switch_vlan_add(ds, info);
  175. break;
  176. case DSA_NOTIFIER_VLAN_DEL:
  177. err = dsa_switch_vlan_del(ds, info);
  178. break;
  179. default:
  180. err = -EOPNOTSUPP;
  181. break;
  182. }
  183. /* Non-switchdev operations cannot be rolled back. If a DSA driver
  184. * returns an error during the chained call, switch chips may be in an
  185. * inconsistent state.
  186. */
  187. if (err)
  188. dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
  189. event, err);
  190. return notifier_from_errno(err);
  191. }
  192. int dsa_switch_register_notifier(struct dsa_switch *ds)
  193. {
  194. ds->nb.notifier_call = dsa_switch_event;
  195. return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
  196. }
  197. void dsa_switch_unregister_notifier(struct dsa_switch *ds)
  198. {
  199. int err;
  200. err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
  201. if (err)
  202. dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
  203. }