a381fd006c4417eab3a86acac5578e220b1fbc20
[openwrt/staging/wigyori.git] / target / linux / generic / files / include / linux / switch.h
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
19 #include <net/genetlink.h>
20 #include <uapi/linux/switch.h>
21
22 struct switch_dev;
23 struct switch_op;
24 struct switch_val;
25 struct switch_attr;
26 struct switch_attrlist;
27 struct switch_led_trigger;
28
29 int register_switch(struct switch_dev *dev, struct net_device *netdev);
30 void unregister_switch(struct switch_dev *dev);
31
32 /**
33 * struct switch_attrlist - attribute list
34 *
35 * @n_attr: number of attributes
36 * @attr: pointer to the attributes array
37 */
38 struct switch_attrlist {
39 int n_attr;
40 const struct switch_attr *attr;
41 };
42
43 enum switch_port_speed {
44 SWITCH_PORT_SPEED_UNKNOWN = 0,
45 SWITCH_PORT_SPEED_10 = 10,
46 SWITCH_PORT_SPEED_100 = 100,
47 SWITCH_PORT_SPEED_1000 = 1000,
48 };
49
50 struct switch_port_link {
51 bool link;
52 bool duplex;
53 bool aneg;
54 bool tx_flow;
55 bool rx_flow;
56 enum switch_port_speed speed;
57 /* in ethtool adv_t format */
58 u32 eee;
59 };
60
61 struct switch_port_stats {
62 unsigned long tx_bytes;
63 unsigned long rx_bytes;
64 };
65
66 /**
67 * struct switch_dev_ops - switch driver operations
68 *
69 * @attr_global: global switch attribute list
70 * @attr_port: port attribute list
71 * @attr_vlan: vlan attribute list
72 *
73 * Callbacks:
74 *
75 * @get_vlan_ports: read the port list of a VLAN
76 * @set_vlan_ports: set the port list of a VLAN
77 *
78 * @get_port_pvid: get the primary VLAN ID of a port
79 * @set_port_pvid: set the primary VLAN ID of a port
80 *
81 * @apply_config: apply all changed settings to the switch
82 * @reset_switch: resetting the switch
83 */
84 struct switch_dev_ops {
85 struct switch_attrlist attr_global, attr_port, attr_vlan;
86
87 int (*get_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
88 int (*set_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
89
90 int (*get_port_pvid)(struct switch_dev *dev, int port, int *val);
91 int (*set_port_pvid)(struct switch_dev *dev, int port, int val);
92
93 int (*apply_config)(struct switch_dev *dev);
94 int (*reset_switch)(struct switch_dev *dev);
95
96 int (*get_port_link)(struct switch_dev *dev, int port,
97 struct switch_port_link *link);
98 int (*set_port_link)(struct switch_dev *dev, int port,
99 struct switch_port_link *link);
100 int (*get_port_stats)(struct switch_dev *dev, int port,
101 struct switch_port_stats *stats);
102
103 int (*phy_read16)(struct switch_dev *dev, int addr, u8 reg, u16 *value);
104 int (*phy_write16)(struct switch_dev *dev, int addr, u8 reg, u16 value);
105 };
106
107 struct switch_dev {
108 const struct switch_dev_ops *ops;
109 /* will be automatically filled */
110 char devname[IFNAMSIZ];
111
112 const char *name;
113 /* NB: either alias or netdev must be set */
114 const char *alias;
115 struct net_device *netdev;
116
117 int ports;
118 int vlans;
119 int cpu_port;
120
121 /* the following fields are internal for swconfig */
122 int id;
123 struct list_head dev_list;
124 unsigned long def_global, def_port, def_vlan;
125
126 struct mutex sw_mutex;
127 struct switch_port *portbuf;
128 struct switch_port_link linkbuf;
129
130 char buf[128];
131
132 #ifdef CONFIG_SWCONFIG_LEDS
133 struct switch_led_trigger *led_trigger;
134 #endif
135 };
136
137 struct switch_port {
138 u32 id;
139 u32 flags;
140 };
141
142 struct switch_val {
143 const struct switch_attr *attr;
144 int port_vlan;
145 int len;
146 union {
147 const char *s;
148 u32 i;
149 struct switch_port *ports;
150 struct switch_port_link *link;
151 } value;
152 };
153
154 struct switch_attr {
155 int disabled;
156 int type;
157 const char *name;
158 const char *description;
159
160 int (*set)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
161 int (*get)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
162
163 /* for driver internal use */
164 int id;
165 int ofs;
166 int max;
167 };
168
169 int switch_generic_set_link(struct switch_dev *dev, int port,
170 struct switch_port_link *link);
171
172 #endif /* _LINUX_SWITCH_H */