hsr_main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, [email protected]
  6. *
  7. * Event handling for HSR and PRP devices.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <net/rtnetlink.h>
  11. #include <linux/rculist.h>
  12. #include <linux/timer.h>
  13. #include <linux/etherdevice.h>
  14. #include "hsr_main.h"
  15. #include "hsr_device.h"
  16. #include "hsr_netlink.h"
  17. #include "hsr_framereg.h"
  18. #include "hsr_slave.h"
  19. static bool hsr_slave_empty(struct hsr_priv *hsr)
  20. {
  21. struct hsr_port *port;
  22. hsr_for_each_port(hsr, port)
  23. if (port->type != HSR_PT_MASTER)
  24. return false;
  25. return true;
  26. }
  27. static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
  28. void *ptr)
  29. {
  30. struct hsr_port *port, *master;
  31. struct net_device *dev;
  32. struct hsr_priv *hsr;
  33. LIST_HEAD(list_kill);
  34. int mtu_max;
  35. int res;
  36. dev = netdev_notifier_info_to_dev(ptr);
  37. port = hsr_port_get_rtnl(dev);
  38. if (!port) {
  39. if (!is_hsr_master(dev))
  40. return NOTIFY_DONE; /* Not an HSR device */
  41. hsr = netdev_priv(dev);
  42. port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  43. if (!port) {
  44. /* Resend of notification concerning removed device? */
  45. return NOTIFY_DONE;
  46. }
  47. } else {
  48. hsr = port->hsr;
  49. }
  50. switch (event) {
  51. case NETDEV_UP: /* Administrative state DOWN */
  52. case NETDEV_DOWN: /* Administrative state UP */
  53. case NETDEV_CHANGE: /* Link (carrier) state changes */
  54. hsr_check_carrier_and_operstate(hsr);
  55. break;
  56. case NETDEV_CHANGENAME:
  57. if (is_hsr_master(dev))
  58. hsr_debugfs_rename(dev);
  59. break;
  60. case NETDEV_CHANGEADDR:
  61. if (port->type == HSR_PT_MASTER) {
  62. /* This should not happen since there's no
  63. * ndo_set_mac_address() for HSR devices - i.e. not
  64. * supported.
  65. */
  66. break;
  67. }
  68. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  69. if (port->type == HSR_PT_SLAVE_A) {
  70. eth_hw_addr_set(master->dev, dev->dev_addr);
  71. call_netdevice_notifiers(NETDEV_CHANGEADDR,
  72. master->dev);
  73. }
  74. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  75. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  76. res = hsr_create_self_node(hsr,
  77. master->dev->dev_addr,
  78. port ?
  79. port->dev->dev_addr :
  80. master->dev->dev_addr);
  81. if (res)
  82. netdev_warn(master->dev,
  83. "Could not update HSR node address.\n");
  84. break;
  85. case NETDEV_CHANGEMTU:
  86. if (port->type == HSR_PT_MASTER)
  87. break; /* Handled in ndo_change_mtu() */
  88. mtu_max = hsr_get_max_mtu(port->hsr);
  89. master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
  90. master->dev->mtu = mtu_max;
  91. break;
  92. case NETDEV_UNREGISTER:
  93. if (!is_hsr_master(dev)) {
  94. master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
  95. hsr_del_port(port);
  96. if (hsr_slave_empty(master->hsr)) {
  97. const struct rtnl_link_ops *ops;
  98. ops = master->dev->rtnl_link_ops;
  99. ops->dellink(master->dev, &list_kill);
  100. unregister_netdevice_many(&list_kill);
  101. }
  102. }
  103. break;
  104. case NETDEV_PRE_TYPE_CHANGE:
  105. /* HSR works only on Ethernet devices. Refuse slave to change
  106. * its type.
  107. */
  108. return NOTIFY_BAD;
  109. }
  110. return NOTIFY_DONE;
  111. }
  112. struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
  113. {
  114. struct hsr_port *port;
  115. hsr_for_each_port(hsr, port)
  116. if (port->type == pt)
  117. return port;
  118. return NULL;
  119. }
  120. int hsr_get_version(struct net_device *dev, enum hsr_version *ver)
  121. {
  122. struct hsr_priv *hsr;
  123. hsr = netdev_priv(dev);
  124. *ver = hsr->prot_version;
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(hsr_get_version);
  128. static struct notifier_block hsr_nb = {
  129. .notifier_call = hsr_netdev_notify, /* Slave event notifications */
  130. };
  131. static int __init hsr_init(void)
  132. {
  133. int res;
  134. BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
  135. register_netdevice_notifier(&hsr_nb);
  136. res = hsr_netlink_init();
  137. return res;
  138. }
  139. static void __exit hsr_exit(void)
  140. {
  141. hsr_netlink_exit();
  142. hsr_debugfs_remove_root();
  143. unregister_netdevice_notifier(&hsr_nb);
  144. }
  145. module_init(hsr_init);
  146. module_exit(hsr_exit);
  147. MODULE_LICENSE("GPL");