mlxsw: core: Add mlxsw specific workqueue and use it for FDB notif. processing
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlxsw / core.h
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/core.h
3  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5  * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6  * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #ifndef _MLXSW_CORE_H
38 #define _MLXSW_CORE_H
39
40 #include <linux/module.h>
41 #include <linux/device.h>
42 #include <linux/slab.h>
43 #include <linux/gfp.h>
44 #include <linux/types.h>
45 #include <linux/skbuff.h>
46 #include <linux/workqueue.h>
47 #include <net/devlink.h>
48
49 #include "trap.h"
50 #include "reg.h"
51
52 #include "cmd.h"
53
54 #define MLXSW_MODULE_ALIAS_PREFIX "mlxsw-driver-"
55 #define MODULE_MLXSW_DRIVER_ALIAS(kind) \
56         MODULE_ALIAS(MLXSW_MODULE_ALIAS_PREFIX kind)
57
58 #define MLXSW_DEVICE_KIND_SWITCHX2 "switchx2"
59 #define MLXSW_DEVICE_KIND_SPECTRUM "spectrum"
60
61 struct mlxsw_core;
62 struct mlxsw_driver;
63 struct mlxsw_bus;
64 struct mlxsw_bus_info;
65
66 void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core);
67
68 int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver);
69 void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver);
70
71 int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
72                                    const struct mlxsw_bus *mlxsw_bus,
73                                    void *bus_priv);
74 void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core);
75
76 struct mlxsw_tx_info {
77         u8 local_port;
78         bool is_emad;
79 };
80
81 bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core,
82                                   const struct mlxsw_tx_info *tx_info);
83 int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
84                             const struct mlxsw_tx_info *tx_info);
85
86 struct mlxsw_rx_listener {
87         void (*func)(struct sk_buff *skb, u8 local_port, void *priv);
88         u8 local_port;
89         u16 trap_id;
90 };
91
92 struct mlxsw_event_listener {
93         void (*func)(const struct mlxsw_reg_info *reg,
94                      char *payload, void *priv);
95         enum mlxsw_event_trap_id trap_id;
96 };
97
98 int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core,
99                                     const struct mlxsw_rx_listener *rxl,
100                                     void *priv);
101 void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core,
102                                        const struct mlxsw_rx_listener *rxl,
103                                        void *priv);
104
105 int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core,
106                                        const struct mlxsw_event_listener *el,
107                                        void *priv);
108 void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core,
109                                           const struct mlxsw_event_listener *el,
110                                           void *priv);
111
112 int mlxsw_reg_query(struct mlxsw_core *mlxsw_core,
113                     const struct mlxsw_reg_info *reg, char *payload);
114 int mlxsw_reg_write(struct mlxsw_core *mlxsw_core,
115                     const struct mlxsw_reg_info *reg, char *payload);
116
117 struct mlxsw_rx_info {
118         bool is_lag;
119         union {
120                 u16 sys_port;
121                 u16 lag_id;
122         } u;
123         u8 lag_port_index;
124         int trap_id;
125 };
126
127 void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
128                             struct mlxsw_rx_info *rx_info);
129
130 void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core,
131                                 u16 lag_id, u8 port_index, u8 local_port);
132 u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core,
133                               u16 lag_id, u8 port_index);
134 void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
135                                   u16 lag_id, u8 local_port);
136
137 struct mlxsw_core_port {
138         struct devlink_port devlink_port;
139 };
140
141 static inline void *
142 mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port)
143 {
144         /* mlxsw_core_port is ensured to always be the first field in driver
145          * port structure.
146          */
147         return mlxsw_core_port;
148 }
149
150 int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core,
151                          struct mlxsw_core_port *mlxsw_core_port, u8 local_port,
152                          struct net_device *dev, bool split, u32 split_group);
153 void mlxsw_core_port_fini(struct mlxsw_core_port *mlxsw_core_port);
154
155 int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay);
156
157 #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8
158
159 struct mlxsw_swid_config {
160         u8      used_type:1,
161                 used_properties:1;
162         u8      type;
163         u8      properties;
164 };
165
166 struct mlxsw_config_profile {
167         u16     used_max_vepa_channels:1,
168                 used_max_lag:1,
169                 used_max_port_per_lag:1,
170                 used_max_mid:1,
171                 used_max_pgt:1,
172                 used_max_system_port:1,
173                 used_max_vlan_groups:1,
174                 used_max_regions:1,
175                 used_flood_tables:1,
176                 used_flood_mode:1,
177                 used_max_ib_mc:1,
178                 used_max_pkey:1,
179                 used_ar_sec:1,
180                 used_adaptive_routing_group_cap:1;
181         u8      max_vepa_channels;
182         u16     max_lag;
183         u16     max_port_per_lag;
184         u16     max_mid;
185         u16     max_pgt;
186         u16     max_system_port;
187         u16     max_vlan_groups;
188         u16     max_regions;
189         u8      max_flood_tables;
190         u8      max_vid_flood_tables;
191         u8      flood_mode;
192         u8      max_fid_offset_flood_tables;
193         u16     fid_offset_flood_table_size;
194         u8      max_fid_flood_tables;
195         u16     fid_flood_table_size;
196         u16     max_ib_mc;
197         u16     max_pkey;
198         u8      ar_sec;
199         u16     adaptive_routing_group_cap;
200         u8      arn;
201         struct mlxsw_swid_config swid_config[MLXSW_CONFIG_PROFILE_SWID_COUNT];
202 };
203
204 struct mlxsw_driver {
205         struct list_head list;
206         const char *kind;
207         struct module *owner;
208         size_t priv_size;
209         int (*init)(struct mlxsw_core *mlxsw_core,
210                     const struct mlxsw_bus_info *mlxsw_bus_info);
211         void (*fini)(struct mlxsw_core *mlxsw_core);
212         int (*port_split)(struct mlxsw_core *mlxsw_core, u8 local_port,
213                           unsigned int count);
214         int (*port_unsplit)(struct mlxsw_core *mlxsw_core, u8 local_port);
215         int (*sb_pool_get)(struct mlxsw_core *mlxsw_core,
216                            unsigned int sb_index, u16 pool_index,
217                            struct devlink_sb_pool_info *pool_info);
218         int (*sb_pool_set)(struct mlxsw_core *mlxsw_core,
219                            unsigned int sb_index, u16 pool_index, u32 size,
220                            enum devlink_sb_threshold_type threshold_type);
221         int (*sb_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
222                                 unsigned int sb_index, u16 pool_index,
223                                 u32 *p_threshold);
224         int (*sb_port_pool_set)(struct mlxsw_core_port *mlxsw_core_port,
225                                 unsigned int sb_index, u16 pool_index,
226                                 u32 threshold);
227         int (*sb_tc_pool_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
228                                    unsigned int sb_index, u16 tc_index,
229                                    enum devlink_sb_pool_type pool_type,
230                                    u16 *p_pool_index, u32 *p_threshold);
231         int (*sb_tc_pool_bind_set)(struct mlxsw_core_port *mlxsw_core_port,
232                                    unsigned int sb_index, u16 tc_index,
233                                    enum devlink_sb_pool_type pool_type,
234                                    u16 pool_index, u32 threshold);
235         int (*sb_occ_snapshot)(struct mlxsw_core *mlxsw_core,
236                                unsigned int sb_index);
237         int (*sb_occ_max_clear)(struct mlxsw_core *mlxsw_core,
238                                 unsigned int sb_index);
239         int (*sb_occ_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
240                                     unsigned int sb_index, u16 pool_index,
241                                     u32 *p_cur, u32 *p_max);
242         int (*sb_occ_tc_port_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
243                                        unsigned int sb_index, u16 tc_index,
244                                        enum devlink_sb_pool_type pool_type,
245                                        u32 *p_cur, u32 *p_max);
246         void (*txhdr_construct)(struct sk_buff *skb,
247                                 const struct mlxsw_tx_info *tx_info);
248         u8 txhdr_len;
249         const struct mlxsw_config_profile *profile;
250 };
251
252 struct mlxsw_bus {
253         const char *kind;
254         int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core,
255                     const struct mlxsw_config_profile *profile);
256         void (*fini)(void *bus_priv);
257         bool (*skb_transmit_busy)(void *bus_priv,
258                                   const struct mlxsw_tx_info *tx_info);
259         int (*skb_transmit)(void *bus_priv, struct sk_buff *skb,
260                             const struct mlxsw_tx_info *tx_info);
261         int (*cmd_exec)(void *bus_priv, u16 opcode, u8 opcode_mod,
262                         u32 in_mod, bool out_mbox_direct,
263                         char *in_mbox, size_t in_mbox_size,
264                         char *out_mbox, size_t out_mbox_size,
265                         u8 *p_status);
266 };
267
268 struct mlxsw_bus_info {
269         const char *device_kind;
270         const char *device_name;
271         struct device *dev;
272         struct {
273                 u16 major;
274                 u16 minor;
275                 u16 subminor;
276         } fw_rev;
277         u8 vsd[MLXSW_CMD_BOARDINFO_VSD_LEN];
278         u8 psid[MLXSW_CMD_BOARDINFO_PSID_LEN];
279 };
280
281 struct mlxsw_hwmon;
282
283 #ifdef CONFIG_MLXSW_CORE_HWMON
284
285 int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
286                      const struct mlxsw_bus_info *mlxsw_bus_info,
287                      struct mlxsw_hwmon **p_hwmon);
288 void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon);
289
290 #else
291
292 static inline int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
293                                    const struct mlxsw_bus_info *mlxsw_bus_info,
294                                    struct mlxsw_hwmon **p_hwmon)
295 {
296         return 0;
297 }
298
299 #endif
300
301 #endif