switch.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * switch.h: Switch configuration API
  3. *
  4. * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #ifndef _LINUX_SWITCH_H
  17. #define _LINUX_SWITCH_H
  18. #include <net/genetlink.h>
  19. #include <uapi/linux/switch.h>
  20. struct switch_dev;
  21. struct switch_op;
  22. struct switch_val;
  23. struct switch_attr;
  24. struct switch_attrlist;
  25. struct switch_led_trigger;
  26. int register_switch(struct switch_dev *dev, struct net_device *netdev);
  27. void unregister_switch(struct switch_dev *dev);
  28. /**
  29. * struct switch_attrlist - attribute list
  30. *
  31. * @n_attr: number of attributes
  32. * @attr: pointer to the attributes array
  33. */
  34. struct switch_attrlist {
  35. int n_attr;
  36. const struct switch_attr *attr;
  37. };
  38. enum switch_port_speed {
  39. SWITCH_PORT_SPEED_UNKNOWN = 0,
  40. SWITCH_PORT_SPEED_10 = 10,
  41. SWITCH_PORT_SPEED_100 = 100,
  42. SWITCH_PORT_SPEED_1000 = 1000,
  43. };
  44. struct switch_port_link {
  45. bool link;
  46. bool duplex;
  47. bool aneg;
  48. bool tx_flow;
  49. bool rx_flow;
  50. enum switch_port_speed speed;
  51. /* in ethtool adv_t format */
  52. u32 eee;
  53. };
  54. struct switch_port_stats {
  55. unsigned long long tx_bytes;
  56. unsigned long long rx_bytes;
  57. };
  58. /**
  59. * struct switch_dev_ops - switch driver operations
  60. *
  61. * @attr_global: global switch attribute list
  62. * @attr_port: port attribute list
  63. * @attr_vlan: vlan attribute list
  64. *
  65. * Callbacks:
  66. *
  67. * @get_vlan_ports: read the port list of a VLAN
  68. * @set_vlan_ports: set the port list of a VLAN
  69. *
  70. * @get_port_pvid: get the primary VLAN ID of a port
  71. * @set_port_pvid: set the primary VLAN ID of a port
  72. *
  73. * @apply_config: apply all changed settings to the switch
  74. * @reset_switch: resetting the switch
  75. */
  76. struct switch_dev_ops {
  77. struct switch_attrlist attr_global, attr_port, attr_vlan;
  78. int (*get_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  79. int (*set_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  80. int (*get_port_pvid)(struct switch_dev *dev, int port, int *val);
  81. int (*set_port_pvid)(struct switch_dev *dev, int port, int val);
  82. int (*apply_config)(struct switch_dev *dev);
  83. int (*reset_switch)(struct switch_dev *dev);
  84. int (*get_port_link)(struct switch_dev *dev, int port,
  85. struct switch_port_link *link);
  86. int (*set_port_link)(struct switch_dev *dev, int port,
  87. struct switch_port_link *link);
  88. int (*get_port_stats)(struct switch_dev *dev, int port,
  89. struct switch_port_stats *stats);
  90. int (*phy_read16)(struct switch_dev *dev, int addr, u8 reg, u16 *value);
  91. int (*phy_write16)(struct switch_dev *dev, int addr, u8 reg, u16 value);
  92. };
  93. struct switch_dev {
  94. struct device_node *of_node;
  95. const struct switch_dev_ops *ops;
  96. /* will be automatically filled */
  97. char devname[IFNAMSIZ];
  98. const char *name;
  99. /* NB: either alias or netdev must be set */
  100. const char *alias;
  101. struct net_device *netdev;
  102. unsigned int ports;
  103. unsigned int vlans;
  104. unsigned int cpu_port;
  105. /* the following fields are internal for swconfig */
  106. unsigned int id;
  107. struct list_head dev_list;
  108. unsigned long def_global, def_port, def_vlan;
  109. struct mutex sw_mutex;
  110. struct switch_port *portbuf;
  111. struct switch_portmap *portmap;
  112. struct switch_port_link linkbuf;
  113. char buf[128];
  114. #ifdef CONFIG_SWCONFIG_LEDS
  115. struct switch_led_trigger *led_trigger;
  116. #endif
  117. };
  118. struct switch_port {
  119. u32 id;
  120. u32 flags;
  121. };
  122. struct switch_portmap {
  123. u32 virt;
  124. const char *s;
  125. };
  126. struct switch_val {
  127. const struct switch_attr *attr;
  128. unsigned int port_vlan;
  129. unsigned int len;
  130. union {
  131. const char *s;
  132. u32 i;
  133. struct switch_port *ports;
  134. struct switch_port_link *link;
  135. } value;
  136. };
  137. struct switch_attr {
  138. int disabled;
  139. int type;
  140. const char *name;
  141. const char *description;
  142. int (*set)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
  143. int (*get)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
  144. /* for driver internal use */
  145. int id;
  146. int ofs;
  147. int max;
  148. };
  149. int switch_generic_set_link(struct switch_dev *dev, int port,
  150. struct switch_port_link *link);
  151. #endif /* _LINUX_SWITCH_H */