switch.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Handling of a single switch chip, part of a switch fabric
  3. *
  4. * Copyright (c) 2017 Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/netdevice.h>
  12. #include <linux/notifier.h>
  13. #include <net/dsa.h>
  14. static int dsa_switch_bridge_join(struct dsa_switch *ds,
  15. struct dsa_notifier_bridge_info *info)
  16. {
  17. if (ds->index == info->sw_index && ds->ops->port_bridge_join)
  18. return ds->ops->port_bridge_join(ds, info->port, info->br);
  19. if (ds->index != info->sw_index)
  20. dev_dbg(ds->dev, "crosschip DSA port %d.%d bridged to %s\n",
  21. info->sw_index, info->port, netdev_name(info->br));
  22. return 0;
  23. }
  24. static int dsa_switch_bridge_leave(struct dsa_switch *ds,
  25. struct dsa_notifier_bridge_info *info)
  26. {
  27. if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
  28. ds->ops->port_bridge_leave(ds, info->port, info->br);
  29. if (ds->index != info->sw_index)
  30. dev_dbg(ds->dev, "crosschip DSA port %d.%d unbridged from %s\n",
  31. info->sw_index, info->port, netdev_name(info->br));
  32. return 0;
  33. }
  34. static int dsa_switch_event(struct notifier_block *nb,
  35. unsigned long event, void *info)
  36. {
  37. struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
  38. int err;
  39. switch (event) {
  40. case DSA_NOTIFIER_BRIDGE_JOIN:
  41. err = dsa_switch_bridge_join(ds, info);
  42. break;
  43. case DSA_NOTIFIER_BRIDGE_LEAVE:
  44. err = dsa_switch_bridge_leave(ds, info);
  45. break;
  46. default:
  47. err = -EOPNOTSUPP;
  48. break;
  49. }
  50. /* Non-switchdev operations cannot be rolled back. If a DSA driver
  51. * returns an error during the chained call, switch chips may be in an
  52. * inconsistent state.
  53. */
  54. if (err)
  55. dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
  56. event, err);
  57. return notifier_from_errno(err);
  58. }
  59. int dsa_switch_register_notifier(struct dsa_switch *ds)
  60. {
  61. ds->nb.notifier_call = dsa_switch_event;
  62. return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
  63. }
  64. void dsa_switch_unregister_notifier(struct dsa_switch *ds)
  65. {
  66. int err;
  67. err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
  68. if (err)
  69. dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
  70. }