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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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>
52 #include "resources.h"
55 struct mlxsw_core_port
;
58 struct mlxsw_bus_info
;
60 void *mlxsw_core_driver_priv(struct mlxsw_core
*mlxsw_core
);
62 int mlxsw_core_driver_register(struct mlxsw_driver
*mlxsw_driver
);
63 void mlxsw_core_driver_unregister(struct mlxsw_driver
*mlxsw_driver
);
65 int mlxsw_core_bus_device_register(const struct mlxsw_bus_info
*mlxsw_bus_info
,
66 const struct mlxsw_bus
*mlxsw_bus
,
68 void mlxsw_core_bus_device_unregister(struct mlxsw_core
*mlxsw_core
);
70 struct mlxsw_tx_info
{
75 bool mlxsw_core_skb_transmit_busy(struct mlxsw_core
*mlxsw_core
,
76 const struct mlxsw_tx_info
*tx_info
);
77 int mlxsw_core_skb_transmit(struct mlxsw_core
*mlxsw_core
, struct sk_buff
*skb
,
78 const struct mlxsw_tx_info
*tx_info
);
80 struct mlxsw_rx_listener
{
81 void (*func
)(struct sk_buff
*skb
, u8 local_port
, void *priv
);
84 enum mlxsw_reg_hpkt_action action
;
87 struct mlxsw_event_listener
{
88 void (*func
)(const struct mlxsw_reg_info
*reg
,
89 char *payload
, void *priv
);
90 enum mlxsw_event_trap_id trap_id
;
93 struct mlxsw_listener
{
96 struct mlxsw_rx_listener rx_listener
;
97 struct mlxsw_event_listener event_listener
;
99 enum mlxsw_reg_hpkt_action action
;
100 enum mlxsw_reg_hpkt_action unreg_action
;
102 bool is_ctrl
; /* should go via control buffer or not */
106 #define MLXSW_RXL(_func, _trap_id, _action, _is_ctrl, _trap_group, \
109 .trap_id = MLXSW_TRAP_ID_##_trap_id, \
113 .local_port = MLXSW_PORT_DONT_CARE, \
114 .trap_id = MLXSW_TRAP_ID_##_trap_id, \
116 .action = MLXSW_REG_HPKT_ACTION_##_action, \
117 .unreg_action = MLXSW_REG_HPKT_ACTION_##_unreg_action, \
118 .trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group, \
119 .is_ctrl = _is_ctrl, \
123 #define MLXSW_EVENTL(_func, _trap_id, _trap_group) \
125 .trap_id = MLXSW_TRAP_ID_##_trap_id, \
126 .u.event_listener = \
129 .trap_id = MLXSW_TRAP_ID_##_trap_id, \
131 .action = MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU, \
132 .trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group, \
137 int mlxsw_core_rx_listener_register(struct mlxsw_core
*mlxsw_core
,
138 const struct mlxsw_rx_listener
*rxl
,
140 void mlxsw_core_rx_listener_unregister(struct mlxsw_core
*mlxsw_core
,
141 const struct mlxsw_rx_listener
*rxl
,
144 int mlxsw_core_event_listener_register(struct mlxsw_core
*mlxsw_core
,
145 const struct mlxsw_event_listener
*el
,
147 void mlxsw_core_event_listener_unregister(struct mlxsw_core
*mlxsw_core
,
148 const struct mlxsw_event_listener
*el
,
151 int mlxsw_core_trap_register(struct mlxsw_core
*mlxsw_core
,
152 const struct mlxsw_listener
*listener
,
154 void mlxsw_core_trap_unregister(struct mlxsw_core
*mlxsw_core
,
155 const struct mlxsw_listener
*listener
,
158 typedef void mlxsw_reg_trans_cb_t(struct mlxsw_core
*mlxsw_core
, char *payload
,
159 size_t payload_len
, unsigned long cb_priv
);
161 int mlxsw_reg_trans_query(struct mlxsw_core
*mlxsw_core
,
162 const struct mlxsw_reg_info
*reg
, char *payload
,
163 struct list_head
*bulk_list
,
164 mlxsw_reg_trans_cb_t
*cb
, unsigned long cb_priv
);
165 int mlxsw_reg_trans_write(struct mlxsw_core
*mlxsw_core
,
166 const struct mlxsw_reg_info
*reg
, char *payload
,
167 struct list_head
*bulk_list
,
168 mlxsw_reg_trans_cb_t
*cb
, unsigned long cb_priv
);
169 int mlxsw_reg_trans_bulk_wait(struct list_head
*bulk_list
);
171 int mlxsw_reg_query(struct mlxsw_core
*mlxsw_core
,
172 const struct mlxsw_reg_info
*reg
, char *payload
);
173 int mlxsw_reg_write(struct mlxsw_core
*mlxsw_core
,
174 const struct mlxsw_reg_info
*reg
, char *payload
);
176 struct mlxsw_rx_info
{
186 void mlxsw_core_skb_receive(struct mlxsw_core
*mlxsw_core
, struct sk_buff
*skb
,
187 struct mlxsw_rx_info
*rx_info
);
189 void mlxsw_core_lag_mapping_set(struct mlxsw_core
*mlxsw_core
,
190 u16 lag_id
, u8 port_index
, u8 local_port
);
191 u8
mlxsw_core_lag_mapping_get(struct mlxsw_core
*mlxsw_core
,
192 u16 lag_id
, u8 port_index
);
193 void mlxsw_core_lag_mapping_clear(struct mlxsw_core
*mlxsw_core
,
194 u16 lag_id
, u8 local_port
);
196 void *mlxsw_core_port_driver_priv(struct mlxsw_core_port
*mlxsw_core_port
);
197 int mlxsw_core_port_init(struct mlxsw_core
*mlxsw_core
, u8 local_port
);
198 void mlxsw_core_port_fini(struct mlxsw_core
*mlxsw_core
, u8 local_port
);
199 void mlxsw_core_port_eth_set(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
200 void *port_driver_priv
, struct net_device
*dev
,
201 bool split
, u32 split_group
);
202 void mlxsw_core_port_ib_set(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
203 void *port_driver_priv
);
204 void mlxsw_core_port_clear(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
205 void *port_driver_priv
);
206 enum devlink_port_type
mlxsw_core_port_type_get(struct mlxsw_core
*mlxsw_core
,
209 int mlxsw_core_schedule_dw(struct delayed_work
*dwork
, unsigned long delay
);
210 int mlxsw_core_schedule_odw(struct delayed_work
*dwork
, unsigned long delay
);
211 void mlxsw_core_flush_owq(void);
213 #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8
215 struct mlxsw_swid_config
{
222 struct mlxsw_config_profile
{
223 u16 used_max_vepa_channels
:1,
226 used_max_system_port
:1,
227 used_max_vlan_groups
:1,
234 used_adaptive_routing_group_cap
:1,
235 used_kvd_split_data
:1; /* indicate for the kvd's values */
237 u8 max_vepa_channels
;
244 u8 max_vid_flood_tables
;
246 u8 max_fid_offset_flood_tables
;
247 u16 fid_offset_flood_table_size
;
248 u8 max_fid_flood_tables
;
249 u16 fid_flood_table_size
;
253 u16 adaptive_routing_group_cap
;
256 u16 kvd_hash_granularity
;
257 u8 kvd_hash_single_parts
;
258 u8 kvd_hash_double_parts
;
259 u8 resource_query_enable
;
260 struct mlxsw_swid_config swid_config
[MLXSW_CONFIG_PROFILE_SWID_COUNT
];
263 struct mlxsw_driver
{
264 struct list_head list
;
267 int (*init
)(struct mlxsw_core
*mlxsw_core
,
268 const struct mlxsw_bus_info
*mlxsw_bus_info
);
269 void (*fini
)(struct mlxsw_core
*mlxsw_core
);
270 int (*basic_trap_groups_set
)(struct mlxsw_core
*mlxsw_core
);
271 int (*port_type_set
)(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
272 enum devlink_port_type new_type
);
273 int (*port_split
)(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
275 int (*port_unsplit
)(struct mlxsw_core
*mlxsw_core
, u8 local_port
);
276 int (*sb_pool_get
)(struct mlxsw_core
*mlxsw_core
,
277 unsigned int sb_index
, u16 pool_index
,
278 struct devlink_sb_pool_info
*pool_info
);
279 int (*sb_pool_set
)(struct mlxsw_core
*mlxsw_core
,
280 unsigned int sb_index
, u16 pool_index
, u32 size
,
281 enum devlink_sb_threshold_type threshold_type
);
282 int (*sb_port_pool_get
)(struct mlxsw_core_port
*mlxsw_core_port
,
283 unsigned int sb_index
, u16 pool_index
,
285 int (*sb_port_pool_set
)(struct mlxsw_core_port
*mlxsw_core_port
,
286 unsigned int sb_index
, u16 pool_index
,
288 int (*sb_tc_pool_bind_get
)(struct mlxsw_core_port
*mlxsw_core_port
,
289 unsigned int sb_index
, u16 tc_index
,
290 enum devlink_sb_pool_type pool_type
,
291 u16
*p_pool_index
, u32
*p_threshold
);
292 int (*sb_tc_pool_bind_set
)(struct mlxsw_core_port
*mlxsw_core_port
,
293 unsigned int sb_index
, u16 tc_index
,
294 enum devlink_sb_pool_type pool_type
,
295 u16 pool_index
, u32 threshold
);
296 int (*sb_occ_snapshot
)(struct mlxsw_core
*mlxsw_core
,
297 unsigned int sb_index
);
298 int (*sb_occ_max_clear
)(struct mlxsw_core
*mlxsw_core
,
299 unsigned int sb_index
);
300 int (*sb_occ_port_pool_get
)(struct mlxsw_core_port
*mlxsw_core_port
,
301 unsigned int sb_index
, u16 pool_index
,
302 u32
*p_cur
, u32
*p_max
);
303 int (*sb_occ_tc_port_bind_get
)(struct mlxsw_core_port
*mlxsw_core_port
,
304 unsigned int sb_index
, u16 tc_index
,
305 enum devlink_sb_pool_type pool_type
,
306 u32
*p_cur
, u32
*p_max
);
307 void (*txhdr_construct
)(struct sk_buff
*skb
,
308 const struct mlxsw_tx_info
*tx_info
);
310 const struct mlxsw_config_profile
*profile
;
313 bool mlxsw_core_res_valid(struct mlxsw_core
*mlxsw_core
,
314 enum mlxsw_res_id res_id
);
316 #define MLXSW_CORE_RES_VALID(res, short_res_id) \
317 mlxsw_core_res_valid(res, MLXSW_RES_ID_##short_res_id)
319 u64
mlxsw_core_res_get(struct mlxsw_core
*mlxsw_core
,
320 enum mlxsw_res_id res_id
);
322 #define MLXSW_CORE_RES_GET(res, short_res_id) \
323 mlxsw_core_res_get(res, MLXSW_RES_ID_##short_res_id)
325 #define MLXSW_BUS_F_TXRX BIT(0)
329 int (*init
)(void *bus_priv
, struct mlxsw_core
*mlxsw_core
,
330 const struct mlxsw_config_profile
*profile
,
331 struct mlxsw_res
*res
);
332 void (*fini
)(void *bus_priv
);
333 bool (*skb_transmit_busy
)(void *bus_priv
,
334 const struct mlxsw_tx_info
*tx_info
);
335 int (*skb_transmit
)(void *bus_priv
, struct sk_buff
*skb
,
336 const struct mlxsw_tx_info
*tx_info
);
337 int (*cmd_exec
)(void *bus_priv
, u16 opcode
, u8 opcode_mod
,
338 u32 in_mod
, bool out_mbox_direct
,
339 char *in_mbox
, size_t in_mbox_size
,
340 char *out_mbox
, size_t out_mbox_size
,
345 struct mlxsw_bus_info
{
346 const char *device_kind
;
347 const char *device_name
;
354 u8 vsd
[MLXSW_CMD_BOARDINFO_VSD_LEN
];
355 u8 psid
[MLXSW_CMD_BOARDINFO_PSID_LEN
];
360 #ifdef CONFIG_MLXSW_CORE_HWMON
362 int mlxsw_hwmon_init(struct mlxsw_core
*mlxsw_core
,
363 const struct mlxsw_bus_info
*mlxsw_bus_info
,
364 struct mlxsw_hwmon
**p_hwmon
);
365 void mlxsw_hwmon_fini(struct mlxsw_hwmon
*mlxsw_hwmon
);
369 static inline int mlxsw_hwmon_init(struct mlxsw_core
*mlxsw_core
,
370 const struct mlxsw_bus_info
*mlxsw_bus_info
,
371 struct mlxsw_hwmon
**p_hwmon
)
378 struct mlxsw_thermal
;
380 #ifdef CONFIG_MLXSW_CORE_THERMAL
382 int mlxsw_thermal_init(struct mlxsw_core
*mlxsw_core
,
383 const struct mlxsw_bus_info
*mlxsw_bus_info
,
384 struct mlxsw_thermal
**p_thermal
);
385 void mlxsw_thermal_fini(struct mlxsw_thermal
*thermal
);
389 static inline int mlxsw_thermal_init(struct mlxsw_core
*mlxsw_core
,
390 const struct mlxsw_bus_info
*mlxsw_bus_info
,
391 struct mlxsw_thermal
**p_thermal
)
396 static inline void mlxsw_thermal_fini(struct mlxsw_thermal
*thermal
)