kernel: swconfig: add get_port_{link,stats} callbacks
[openwrt.git] / target / linux / generic / files / include / linux / switch.h
1 /*
2  * switch.h: Switch configuration API
3  *
4  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
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
17 #ifndef __LINUX_SWITCH_H
18 #define __LINUX_SWITCH_H
19
20 #include <linux/types.h>
21 #include <linux/netdevice.h>
22 #include <linux/netlink.h>
23 #include <linux/genetlink.h>
24 #ifndef __KERNEL__
25 #include <netlink/netlink.h>
26 #include <netlink/genl/genl.h>
27 #include <netlink/genl/ctrl.h>
28 #else
29 #include <net/genetlink.h>
30 #endif
31
32 /* main attributes */
33 enum {
34         SWITCH_ATTR_UNSPEC,
35         /* global */
36         SWITCH_ATTR_TYPE,
37         /* device */
38         SWITCH_ATTR_ID,
39         SWITCH_ATTR_DEV_NAME,
40         SWITCH_ATTR_ALIAS,
41         SWITCH_ATTR_NAME,
42         SWITCH_ATTR_VLANS,
43         SWITCH_ATTR_PORTS,
44         SWITCH_ATTR_CPU_PORT,
45         /* attributes */
46         SWITCH_ATTR_OP_ID,
47         SWITCH_ATTR_OP_TYPE,
48         SWITCH_ATTR_OP_NAME,
49         SWITCH_ATTR_OP_PORT,
50         SWITCH_ATTR_OP_VLAN,
51         SWITCH_ATTR_OP_VALUE_INT,
52         SWITCH_ATTR_OP_VALUE_STR,
53         SWITCH_ATTR_OP_VALUE_PORTS,
54         SWITCH_ATTR_OP_DESCRIPTION,
55         /* port lists */
56         SWITCH_ATTR_PORT,
57         SWITCH_ATTR_MAX
58 };
59
60 /* commands */
61 enum {
62         SWITCH_CMD_UNSPEC,
63         SWITCH_CMD_GET_SWITCH,
64         SWITCH_CMD_NEW_ATTR,
65         SWITCH_CMD_LIST_GLOBAL,
66         SWITCH_CMD_GET_GLOBAL,
67         SWITCH_CMD_SET_GLOBAL,
68         SWITCH_CMD_LIST_PORT,
69         SWITCH_CMD_GET_PORT,
70         SWITCH_CMD_SET_PORT,
71         SWITCH_CMD_LIST_VLAN,
72         SWITCH_CMD_GET_VLAN,
73         SWITCH_CMD_SET_VLAN
74 };
75
76 /* data types */
77 enum switch_val_type {
78         SWITCH_TYPE_UNSPEC,
79         SWITCH_TYPE_INT,
80         SWITCH_TYPE_STRING,
81         SWITCH_TYPE_PORTS,
82         SWITCH_TYPE_NOVAL,
83 };
84
85 /* port nested attributes */
86 enum {
87         SWITCH_PORT_UNSPEC,
88         SWITCH_PORT_ID,
89         SWITCH_PORT_FLAG_TAGGED,
90         SWITCH_PORT_ATTR_MAX
91 };
92
93 #define SWITCH_ATTR_DEFAULTS_OFFSET     0x1000
94
95 #ifdef __KERNEL__
96
97 struct switch_dev;
98 struct switch_op;
99 struct switch_val;
100 struct switch_attr;
101 struct switch_attrlist;
102
103 int register_switch(struct switch_dev *dev, struct net_device *netdev);
104 void unregister_switch(struct switch_dev *dev);
105
106 /**
107  * struct switch_attrlist - attribute list
108  *
109  * @n_attr: number of attributes
110  * @attr: pointer to the attributes array
111  */
112 struct switch_attrlist {
113         int n_attr;
114         const struct switch_attr *attr;
115 };
116
117 enum switch_port_speed {
118         SWITCH_PORT_SPEED_UNKNOWN = 0,
119         SWITCH_PORT_SPEED_10 = 10,
120         SWITCH_PORT_SPEED_100 = 100,
121         SWITCH_PORT_SPEED_1000 = 1000,
122 };
123
124 struct switch_port_link {
125         bool link;
126         bool duplex;
127         bool aneg;
128         bool tx_flow;
129         bool rx_flow;
130         enum switch_port_speed speed;
131 };
132
133 struct switch_port_stats {
134         unsigned long tx_bytes;
135         unsigned long rx_bytes;
136 };
137
138 /**
139  * struct switch_dev_ops - switch driver operations
140  *
141  * @attr_global: global switch attribute list
142  * @attr_port: port attribute list
143  * @attr_vlan: vlan attribute list
144  *
145  * Callbacks:
146  *
147  * @get_vlan_ports: read the port list of a VLAN
148  * @set_vlan_ports: set the port list of a VLAN
149  *
150  * @get_port_pvid: get the primary VLAN ID of a port
151  * @set_port_pvid: set the primary VLAN ID of a port
152  *
153  * @apply_config: apply all changed settings to the switch
154  * @reset_switch: resetting the switch
155  */
156 struct switch_dev_ops {
157         struct switch_attrlist attr_global, attr_port, attr_vlan;
158
159         int (*get_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
160         int (*set_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
161
162         int (*get_port_pvid)(struct switch_dev *dev, int port, int *val);
163         int (*set_port_pvid)(struct switch_dev *dev, int port, int val);
164
165         int (*apply_config)(struct switch_dev *dev);
166         int (*reset_switch)(struct switch_dev *dev);
167
168         int (*get_port_link)(struct switch_dev *dev, int port,
169                              struct switch_port_link *link);
170         int (*get_port_stats)(struct switch_dev *dev, int port,
171                               struct switch_port_stats *stats);
172 };
173
174 struct switch_dev {
175         const struct switch_dev_ops *ops;
176         /* will be automatically filled */
177         char devname[IFNAMSIZ];
178
179         const char *name;
180         /* NB: either alias or netdev must be set */
181         const char *alias;
182         struct net_device *netdev;
183
184         int ports;
185         int vlans;
186         int cpu_port;
187
188         /* the following fields are internal for swconfig */
189         int id;
190         struct list_head dev_list;
191         unsigned long def_global, def_port, def_vlan;
192
193         spinlock_t lock;
194         struct switch_port *portbuf;
195 };
196
197 struct switch_port {
198         u32 id;
199         u32 flags;
200 };
201
202 struct switch_val {
203         const struct switch_attr *attr;
204         int port_vlan;
205         int len;
206         union {
207                 const char *s;
208                 u32 i;
209                 struct switch_port *ports;
210         } value;
211 };
212
213 struct switch_attr {
214         int disabled;
215         int type;
216         const char *name;
217         const char *description;
218
219         int (*set)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
220         int (*get)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
221
222         /* for driver internal use */
223         int id;
224         int ofs;
225         int max;
226 };
227
228 #endif
229
230 #endif