1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3 
4 #ifndef _MLXSW_CORE_H
5 #define _MLXSW_CORE_H
6 
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/slab.h>
10 #include <linux/gfp.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/workqueue.h>
14 #include <linux/net_namespace.h>
15 #include <net/devlink.h>
16 
17 #include "trap.h"
18 #include "reg.h"
19 #include "cmd.h"
20 #include "resources.h"
21 
22 struct mlxsw_core;
23 struct mlxsw_core_port;
24 struct mlxsw_driver;
25 struct mlxsw_bus;
26 struct mlxsw_bus_info;
27 struct mlxsw_fw_rev;
28 
29 unsigned int mlxsw_core_max_ports(const struct mlxsw_core *mlxsw_core);
30 
31 void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core);
32 
33 bool mlxsw_core_res_query_enabled(const struct mlxsw_core *mlxsw_core);
34 
35 bool
36 mlxsw_core_fw_rev_minor_subminor_validate(const struct mlxsw_fw_rev *rev,
37 					  const struct mlxsw_fw_rev *req_rev);
38 
39 int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver);
40 void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver);
41 
42 int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
43 				   const struct mlxsw_bus *mlxsw_bus,
44 				   void *bus_priv, bool reload,
45 				   struct devlink *devlink,
46 				   struct netlink_ext_ack *extack);
47 void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core, bool reload);
48 
49 struct mlxsw_tx_info {
50 	u8 local_port;
51 	bool is_emad;
52 };
53 
54 bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core,
55 				  const struct mlxsw_tx_info *tx_info);
56 int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
57 			    const struct mlxsw_tx_info *tx_info);
58 void mlxsw_core_ptp_transmitted(struct mlxsw_core *mlxsw_core,
59 				struct sk_buff *skb, u8 local_port);
60 
61 struct mlxsw_rx_listener {
62 	void (*func)(struct sk_buff *skb, u8 local_port, void *priv);
63 	u8 local_port;
64 	u8 mirror_reason;
65 	u16 trap_id;
66 };
67 
68 struct mlxsw_event_listener {
69 	void (*func)(const struct mlxsw_reg_info *reg,
70 		     char *payload, void *priv);
71 	enum mlxsw_event_trap_id trap_id;
72 };
73 
74 struct mlxsw_listener {
75 	u16 trap_id;
76 	union {
77 		struct mlxsw_rx_listener rx_listener;
78 		struct mlxsw_event_listener event_listener;
79 	};
80 	enum mlxsw_reg_hpkt_action en_action; /* Action when enabled */
81 	enum mlxsw_reg_hpkt_action dis_action; /* Action when disabled */
82 	u8 en_trap_group; /* Trap group when enabled */
83 	u8 dis_trap_group; /* Trap group when disabled */
84 	u8 is_ctrl:1, /* should go via control buffer or not */
85 	   is_event:1,
86 	   enabled_on_register:1; /* Trap should be enabled when listener
87 				   * is registered.
88 				   */
89 };
90 
91 #define __MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group,	\
92 		    _dis_action, _enabled_on_register, _dis_trap_group)		\
93 	{									\
94 		.trap_id = MLXSW_TRAP_ID_##_trap_id,				\
95 		.rx_listener =							\
96 		{								\
97 			.func = _func,						\
98 			.local_port = MLXSW_PORT_DONT_CARE,			\
99 			.trap_id = MLXSW_TRAP_ID_##_trap_id,			\
100 		},								\
101 		.en_action = MLXSW_REG_HPKT_ACTION_##_en_action,		\
102 		.dis_action = MLXSW_REG_HPKT_ACTION_##_dis_action,		\
103 		.en_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_en_trap_group,	\
104 		.dis_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_dis_trap_group,	\
105 		.is_ctrl = _is_ctrl,						\
106 		.enabled_on_register = _enabled_on_register,			\
107 	}
108 
109 #define MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _trap_group,		\
110 		  _dis_action)							\
111 	__MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _trap_group,		\
112 		    _dis_action, true, _trap_group)
113 
114 #define MLXSW_RXL_DIS(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group,	\
115 		      _dis_action, _dis_trap_group)				\
116 	__MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group,	\
117 		    _dis_action, false, _dis_trap_group)
118 
119 #define MLXSW_EVENTL(_func, _trap_id, _trap_group)				\
120 	{									\
121 		.trap_id = MLXSW_TRAP_ID_##_trap_id,				\
122 		.event_listener =						\
123 		{								\
124 			.func = _func,						\
125 			.trap_id = MLXSW_TRAP_ID_##_trap_id,			\
126 		},								\
127 		.en_action = MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU,			\
128 		.en_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group,	\
129 		.is_event = true,						\
130 		.enabled_on_register = true,					\
131 	}
132 
133 int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core,
134 				    const struct mlxsw_rx_listener *rxl,
135 				    void *priv, bool enabled);
136 void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core,
137 				       const struct mlxsw_rx_listener *rxl);
138 
139 int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core,
140 				       const struct mlxsw_event_listener *el,
141 				       void *priv);
142 void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core,
143 					  const struct mlxsw_event_listener *el);
144 
145 int mlxsw_core_trap_register(struct mlxsw_core *mlxsw_core,
146 			     const struct mlxsw_listener *listener,
147 			     void *priv);
148 void mlxsw_core_trap_unregister(struct mlxsw_core *mlxsw_core,
149 				const struct mlxsw_listener *listener,
150 				void *priv);
151 int mlxsw_core_trap_state_set(struct mlxsw_core *mlxsw_core,
152 			      const struct mlxsw_listener *listener,
153 			      bool enabled);
154 
155 typedef void mlxsw_reg_trans_cb_t(struct mlxsw_core *mlxsw_core, char *payload,
156 				  size_t payload_len, unsigned long cb_priv);
157 
158 int mlxsw_reg_trans_query(struct mlxsw_core *mlxsw_core,
159 			  const struct mlxsw_reg_info *reg, char *payload,
160 			  struct list_head *bulk_list,
161 			  mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
162 int mlxsw_reg_trans_write(struct mlxsw_core *mlxsw_core,
163 			  const struct mlxsw_reg_info *reg, char *payload,
164 			  struct list_head *bulk_list,
165 			  mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
166 int mlxsw_reg_trans_bulk_wait(struct list_head *bulk_list);
167 
168 int mlxsw_reg_query(struct mlxsw_core *mlxsw_core,
169 		    const struct mlxsw_reg_info *reg, char *payload);
170 int mlxsw_reg_write(struct mlxsw_core *mlxsw_core,
171 		    const struct mlxsw_reg_info *reg, char *payload);
172 
173 struct mlxsw_rx_info {
174 	bool is_lag;
175 	union {
176 		u16 sys_port;
177 		u16 lag_id;
178 	} u;
179 	u8 lag_port_index;
180 	u8 mirror_reason;
181 	int trap_id;
182 };
183 
184 void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
185 			    struct mlxsw_rx_info *rx_info);
186 
187 void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core,
188 				u16 lag_id, u8 port_index, u8 local_port);
189 u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core,
190 			      u16 lag_id, u8 port_index);
191 void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
192 				  u16 lag_id, u8 local_port);
193 
194 void *mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port);
195 int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core, u8 local_port,
196 			 u32 port_number, bool split, u32 split_port_subnumber,
197 			 bool splittable, u32 lanes,
198 			 const unsigned char *switch_id,
199 			 unsigned char switch_id_len);
200 void mlxsw_core_port_fini(struct mlxsw_core *mlxsw_core, u8 local_port);
201 int mlxsw_core_cpu_port_init(struct mlxsw_core *mlxsw_core,
202 			     void *port_driver_priv,
203 			     const unsigned char *switch_id,
204 			     unsigned char switch_id_len);
205 void mlxsw_core_cpu_port_fini(struct mlxsw_core *mlxsw_core);
206 void mlxsw_core_port_eth_set(struct mlxsw_core *mlxsw_core, u8 local_port,
207 			     void *port_driver_priv, struct net_device *dev);
208 void mlxsw_core_port_ib_set(struct mlxsw_core *mlxsw_core, u8 local_port,
209 			    void *port_driver_priv);
210 void mlxsw_core_port_clear(struct mlxsw_core *mlxsw_core, u8 local_port,
211 			   void *port_driver_priv);
212 enum devlink_port_type mlxsw_core_port_type_get(struct mlxsw_core *mlxsw_core,
213 						u8 local_port);
214 struct devlink_port *
215 mlxsw_core_port_devlink_port_get(struct mlxsw_core *mlxsw_core,
216 				 u8 local_port);
217 int mlxsw_core_module_max_width(struct mlxsw_core *mlxsw_core, u8 module);
218 
219 int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay);
220 bool mlxsw_core_schedule_work(struct work_struct *work);
221 void mlxsw_core_flush_owq(void);
222 int mlxsw_core_resources_query(struct mlxsw_core *mlxsw_core, char *mbox,
223 			       struct mlxsw_res *res);
224 
225 #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8
226 
227 struct mlxsw_swid_config {
228 	u8	used_type:1,
229 		used_properties:1;
230 	u8	type;
231 	u8	properties;
232 };
233 
234 struct mlxsw_config_profile {
235 	u16	used_max_vepa_channels:1,
236 		used_max_mid:1,
237 		used_max_pgt:1,
238 		used_max_system_port:1,
239 		used_max_vlan_groups:1,
240 		used_max_regions:1,
241 		used_flood_tables:1,
242 		used_flood_mode:1,
243 		used_max_ib_mc:1,
244 		used_max_pkey:1,
245 		used_ar_sec:1,
246 		used_adaptive_routing_group_cap:1,
247 		used_kvd_sizes:1;
248 	u8	max_vepa_channels;
249 	u16	max_mid;
250 	u16	max_pgt;
251 	u16	max_system_port;
252 	u16	max_vlan_groups;
253 	u16	max_regions;
254 	u8	max_flood_tables;
255 	u8	max_vid_flood_tables;
256 	u8	flood_mode;
257 	u8	max_fid_offset_flood_tables;
258 	u16	fid_offset_flood_table_size;
259 	u8	max_fid_flood_tables;
260 	u16	fid_flood_table_size;
261 	u16	max_ib_mc;
262 	u16	max_pkey;
263 	u8	ar_sec;
264 	u16	adaptive_routing_group_cap;
265 	u8	arn;
266 	u32	kvd_linear_size;
267 	u8	kvd_hash_single_parts;
268 	u8	kvd_hash_double_parts;
269 	struct mlxsw_swid_config swid_config[MLXSW_CONFIG_PROFILE_SWID_COUNT];
270 };
271 
272 struct mlxsw_driver {
273 	struct list_head list;
274 	const char *kind;
275 	size_t priv_size;
276 	int (*init)(struct mlxsw_core *mlxsw_core,
277 		    const struct mlxsw_bus_info *mlxsw_bus_info,
278 		    struct netlink_ext_ack *extack);
279 	void (*fini)(struct mlxsw_core *mlxsw_core);
280 	int (*basic_trap_groups_set)(struct mlxsw_core *mlxsw_core);
281 	int (*port_type_set)(struct mlxsw_core *mlxsw_core, u8 local_port,
282 			     enum devlink_port_type new_type);
283 	int (*port_split)(struct mlxsw_core *mlxsw_core, u8 local_port,
284 			  unsigned int count, struct netlink_ext_ack *extack);
285 	int (*port_unsplit)(struct mlxsw_core *mlxsw_core, u8 local_port,
286 			    struct netlink_ext_ack *extack);
287 	int (*sb_pool_get)(struct mlxsw_core *mlxsw_core,
288 			   unsigned int sb_index, u16 pool_index,
289 			   struct devlink_sb_pool_info *pool_info);
290 	int (*sb_pool_set)(struct mlxsw_core *mlxsw_core,
291 			   unsigned int sb_index, u16 pool_index, u32 size,
292 			   enum devlink_sb_threshold_type threshold_type,
293 			   struct netlink_ext_ack *extack);
294 	int (*sb_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
295 				unsigned int sb_index, u16 pool_index,
296 				u32 *p_threshold);
297 	int (*sb_port_pool_set)(struct mlxsw_core_port *mlxsw_core_port,
298 				unsigned int sb_index, u16 pool_index,
299 				u32 threshold, struct netlink_ext_ack *extack);
300 	int (*sb_tc_pool_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
301 				   unsigned int sb_index, u16 tc_index,
302 				   enum devlink_sb_pool_type pool_type,
303 				   u16 *p_pool_index, u32 *p_threshold);
304 	int (*sb_tc_pool_bind_set)(struct mlxsw_core_port *mlxsw_core_port,
305 				   unsigned int sb_index, u16 tc_index,
306 				   enum devlink_sb_pool_type pool_type,
307 				   u16 pool_index, u32 threshold,
308 				   struct netlink_ext_ack *extack);
309 	int (*sb_occ_snapshot)(struct mlxsw_core *mlxsw_core,
310 			       unsigned int sb_index);
311 	int (*sb_occ_max_clear)(struct mlxsw_core *mlxsw_core,
312 				unsigned int sb_index);
313 	int (*sb_occ_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
314 				    unsigned int sb_index, u16 pool_index,
315 				    u32 *p_cur, u32 *p_max);
316 	int (*sb_occ_tc_port_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
317 				       unsigned int sb_index, u16 tc_index,
318 				       enum devlink_sb_pool_type pool_type,
319 				       u32 *p_cur, u32 *p_max);
320 	int (*flash_update)(struct mlxsw_core *mlxsw_core,
321 			    const char *file_name, const char *component,
322 			    struct netlink_ext_ack *extack);
323 	int (*trap_init)(struct mlxsw_core *mlxsw_core,
324 			 const struct devlink_trap *trap, void *trap_ctx);
325 	void (*trap_fini)(struct mlxsw_core *mlxsw_core,
326 			  const struct devlink_trap *trap, void *trap_ctx);
327 	int (*trap_action_set)(struct mlxsw_core *mlxsw_core,
328 			       const struct devlink_trap *trap,
329 			       enum devlink_trap_action action);
330 	int (*trap_group_init)(struct mlxsw_core *mlxsw_core,
331 			       const struct devlink_trap_group *group);
332 	int (*trap_group_set)(struct mlxsw_core *mlxsw_core,
333 			      const struct devlink_trap_group *group,
334 			      const struct devlink_trap_policer *policer);
335 	int (*trap_policer_init)(struct mlxsw_core *mlxsw_core,
336 				 const struct devlink_trap_policer *policer);
337 	void (*trap_policer_fini)(struct mlxsw_core *mlxsw_core,
338 				  const struct devlink_trap_policer *policer);
339 	int (*trap_policer_set)(struct mlxsw_core *mlxsw_core,
340 				const struct devlink_trap_policer *policer,
341 				u64 rate, u64 burst,
342 				struct netlink_ext_ack *extack);
343 	int (*trap_policer_counter_get)(struct mlxsw_core *mlxsw_core,
344 					const struct devlink_trap_policer *policer,
345 					u64 *p_drops);
346 	void (*txhdr_construct)(struct sk_buff *skb,
347 				const struct mlxsw_tx_info *tx_info);
348 	int (*resources_register)(struct mlxsw_core *mlxsw_core);
349 	int (*kvd_sizes_get)(struct mlxsw_core *mlxsw_core,
350 			     const struct mlxsw_config_profile *profile,
351 			     u64 *p_single_size, u64 *p_double_size,
352 			     u64 *p_linear_size);
353 	int (*params_register)(struct mlxsw_core *mlxsw_core);
354 	void (*params_unregister)(struct mlxsw_core *mlxsw_core);
355 
356 	/* Notify a driver that a timestamped packet was transmitted. Driver
357 	 * is responsible for freeing the passed-in SKB.
358 	 */
359 	void (*ptp_transmitted)(struct mlxsw_core *mlxsw_core,
360 				struct sk_buff *skb, u8 local_port);
361 
362 	u8 txhdr_len;
363 	const struct mlxsw_config_profile *profile;
364 	bool res_query_enabled;
365 };
366 
367 int mlxsw_core_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
368 			     const struct mlxsw_config_profile *profile,
369 			     u64 *p_single_size, u64 *p_double_size,
370 			     u64 *p_linear_size);
371 
372 void mlxsw_core_fw_flash_start(struct mlxsw_core *mlxsw_core);
373 void mlxsw_core_fw_flash_end(struct mlxsw_core *mlxsw_core);
374 
375 u32 mlxsw_core_read_frc_h(struct mlxsw_core *mlxsw_core);
376 u32 mlxsw_core_read_frc_l(struct mlxsw_core *mlxsw_core);
377 
378 void mlxsw_core_emad_string_tlv_enable(struct mlxsw_core *mlxsw_core);
379 
380 bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core,
381 			  enum mlxsw_res_id res_id);
382 
383 #define MLXSW_CORE_RES_VALID(mlxsw_core, short_res_id)			\
384 	mlxsw_core_res_valid(mlxsw_core, MLXSW_RES_ID_##short_res_id)
385 
386 u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core,
387 		       enum mlxsw_res_id res_id);
388 
389 #define MLXSW_CORE_RES_GET(mlxsw_core, short_res_id)			\
390 	mlxsw_core_res_get(mlxsw_core, MLXSW_RES_ID_##short_res_id)
391 
392 static inline struct net *mlxsw_core_net(struct mlxsw_core *mlxsw_core)
393 {
394 	return devlink_net(priv_to_devlink(mlxsw_core));
395 }
396 
397 #define MLXSW_BUS_F_TXRX	BIT(0)
398 #define MLXSW_BUS_F_RESET	BIT(1)
399 
400 struct mlxsw_bus {
401 	const char *kind;
402 	int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core,
403 		    const struct mlxsw_config_profile *profile,
404 		    struct mlxsw_res *res);
405 	void (*fini)(void *bus_priv);
406 	bool (*skb_transmit_busy)(void *bus_priv,
407 				  const struct mlxsw_tx_info *tx_info);
408 	int (*skb_transmit)(void *bus_priv, struct sk_buff *skb,
409 			    const struct mlxsw_tx_info *tx_info);
410 	int (*cmd_exec)(void *bus_priv, u16 opcode, u8 opcode_mod,
411 			u32 in_mod, bool out_mbox_direct,
412 			char *in_mbox, size_t in_mbox_size,
413 			char *out_mbox, size_t out_mbox_size,
414 			u8 *p_status);
415 	u32 (*read_frc_h)(void *bus_priv);
416 	u32 (*read_frc_l)(void *bus_priv);
417 	u8 features;
418 };
419 
420 struct mlxsw_fw_rev {
421 	u16 major;
422 	u16 minor;
423 	u16 subminor;
424 	u16 can_reset_minor;
425 };
426 
427 struct mlxsw_bus_info {
428 	const char *device_kind;
429 	const char *device_name;
430 	struct device *dev;
431 	struct mlxsw_fw_rev fw_rev;
432 	u8 vsd[MLXSW_CMD_BOARDINFO_VSD_LEN];
433 	u8 psid[MLXSW_CMD_BOARDINFO_PSID_LEN];
434 	u8 low_frequency:1,
435 	   read_frc_capable:1;
436 };
437 
438 struct mlxsw_hwmon;
439 
440 #ifdef CONFIG_MLXSW_CORE_HWMON
441 
442 int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
443 		     const struct mlxsw_bus_info *mlxsw_bus_info,
444 		     struct mlxsw_hwmon **p_hwmon);
445 void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon);
446 
447 #else
448 
449 static inline int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
450 				   const struct mlxsw_bus_info *mlxsw_bus_info,
451 				   struct mlxsw_hwmon **p_hwmon)
452 {
453 	return 0;
454 }
455 
456 static inline void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon)
457 {
458 }
459 
460 #endif
461 
462 struct mlxsw_thermal;
463 
464 #ifdef CONFIG_MLXSW_CORE_THERMAL
465 
466 int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core,
467 		       const struct mlxsw_bus_info *mlxsw_bus_info,
468 		       struct mlxsw_thermal **p_thermal);
469 void mlxsw_thermal_fini(struct mlxsw_thermal *thermal);
470 
471 #else
472 
473 static inline int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core,
474 				     const struct mlxsw_bus_info *mlxsw_bus_info,
475 				     struct mlxsw_thermal **p_thermal)
476 {
477 	return 0;
478 }
479 
480 static inline void mlxsw_thermal_fini(struct mlxsw_thermal *thermal)
481 {
482 }
483 
484 #endif
485 
486 enum mlxsw_devlink_param_id {
487 	MLXSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
488 	MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL,
489 };
490 
491 struct mlxsw_skb_cb {
492 	union {
493 		struct mlxsw_tx_info tx_info;
494 		u32 cookie_index; /* Only used during receive */
495 	};
496 };
497 
498 static inline struct mlxsw_skb_cb *mlxsw_skb_cb(struct sk_buff *skb)
499 {
500 	BUILD_BUG_ON(sizeof(mlxsw_skb_cb) > sizeof(skb->cb));
501 	return (struct mlxsw_skb_cb *) skb->cb;
502 }
503 
504 #endif
505