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