1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019, Mellanox Technologies */
3 
4 #ifndef _MLX5DR_H_
5 #define _MLX5DR_H_
6 
7 struct mlx5dr_domain;
8 struct mlx5dr_table;
9 struct mlx5dr_matcher;
10 struct mlx5dr_rule;
11 struct mlx5dr_action;
12 
13 enum mlx5dr_domain_type {
14 	MLX5DR_DOMAIN_TYPE_NIC_RX,
15 	MLX5DR_DOMAIN_TYPE_NIC_TX,
16 	MLX5DR_DOMAIN_TYPE_FDB,
17 };
18 
19 enum mlx5dr_domain_sync_flags {
20 	MLX5DR_DOMAIN_SYNC_FLAGS_SW = 1 << 0,
21 	MLX5DR_DOMAIN_SYNC_FLAGS_HW = 1 << 1,
22 };
23 
24 enum mlx5dr_action_reformat_type {
25 	DR_ACTION_REFORMAT_TYP_TNL_L2_TO_L2,
26 	DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L2,
27 	DR_ACTION_REFORMAT_TYP_TNL_L3_TO_L2,
28 	DR_ACTION_REFORMAT_TYP_L2_TO_TNL_L3,
29 	DR_ACTION_REFORMAT_TYP_INSERT_HDR,
30 };
31 
32 struct mlx5dr_match_parameters {
33 	size_t match_sz;
34 	u64 *match_buf; /* Device spec format */
35 };
36 
37 struct mlx5dr_action_dest {
38 	struct mlx5dr_action *dest;
39 	struct mlx5dr_action *reformat;
40 };
41 
42 struct mlx5dr_domain *
43 mlx5dr_domain_create(struct mlx5_core_dev *mdev, enum mlx5dr_domain_type type);
44 
45 int mlx5dr_domain_destroy(struct mlx5dr_domain *domain);
46 
47 int mlx5dr_domain_sync(struct mlx5dr_domain *domain, u32 flags);
48 
49 void mlx5dr_domain_set_peer(struct mlx5dr_domain *dmn,
50 			    struct mlx5dr_domain *peer_dmn);
51 
52 struct mlx5dr_table *
53 mlx5dr_table_create(struct mlx5dr_domain *domain, u32 level, u32 flags);
54 
55 int mlx5dr_table_destroy(struct mlx5dr_table *table);
56 
57 u32 mlx5dr_table_get_id(struct mlx5dr_table *table);
58 
59 struct mlx5dr_matcher *
60 mlx5dr_matcher_create(struct mlx5dr_table *table,
61 		      u32 priority,
62 		      u8 match_criteria_enable,
63 		      struct mlx5dr_match_parameters *mask);
64 
65 int mlx5dr_matcher_destroy(struct mlx5dr_matcher *matcher);
66 
67 struct mlx5dr_rule *
68 mlx5dr_rule_create(struct mlx5dr_matcher *matcher,
69 		   struct mlx5dr_match_parameters *value,
70 		   size_t num_actions,
71 		   struct mlx5dr_action *actions[],
72 		   u32 flow_source);
73 
74 int mlx5dr_rule_destroy(struct mlx5dr_rule *rule);
75 
76 int mlx5dr_table_set_miss_action(struct mlx5dr_table *tbl,
77 				 struct mlx5dr_action *action);
78 
79 struct mlx5dr_action *
80 mlx5dr_action_create_dest_table_num(struct mlx5dr_domain *dmn, u32 table_num);
81 
82 struct mlx5dr_action *
83 mlx5dr_action_create_dest_table(struct mlx5dr_table *table);
84 
85 struct mlx5dr_action *
86 mlx5dr_action_create_dest_flow_fw_table(struct mlx5dr_domain *domain,
87 					struct mlx5_flow_table *ft);
88 
89 struct mlx5dr_action *
90 mlx5dr_action_create_dest_vport(struct mlx5dr_domain *domain,
91 				u32 vport, u8 vhca_id_valid,
92 				u16 vhca_id);
93 
94 struct mlx5dr_action *
95 mlx5dr_action_create_mult_dest_tbl(struct mlx5dr_domain *dmn,
96 				   struct mlx5dr_action_dest *dests,
97 				   u32 num_of_dests);
98 
99 struct mlx5dr_action *mlx5dr_action_create_drop(void);
100 
101 struct mlx5dr_action *mlx5dr_action_create_tag(u32 tag_value);
102 
103 struct mlx5dr_action *
104 mlx5dr_action_create_flow_sampler(struct mlx5dr_domain *dmn, u32 sampler_id);
105 
106 struct mlx5dr_action *
107 mlx5dr_action_create_flow_counter(u32 counter_id);
108 
109 struct mlx5dr_action *
110 mlx5dr_action_create_packet_reformat(struct mlx5dr_domain *dmn,
111 				     enum mlx5dr_action_reformat_type reformat_type,
112 				     u8 reformat_param_0,
113 				     u8 reformat_param_1,
114 				     size_t data_sz,
115 				     void *data);
116 
117 struct mlx5dr_action *
118 mlx5dr_action_create_modify_header(struct mlx5dr_domain *domain,
119 				   u32 flags,
120 				   size_t actions_sz,
121 				   __be64 actions[]);
122 
123 struct mlx5dr_action *mlx5dr_action_create_pop_vlan(void);
124 
125 struct mlx5dr_action *
126 mlx5dr_action_create_push_vlan(struct mlx5dr_domain *domain, __be32 vlan_hdr);
127 
128 int mlx5dr_action_destroy(struct mlx5dr_action *action);
129 
130 static inline bool
131 mlx5dr_is_supported(struct mlx5_core_dev *dev)
132 {
133 	return MLX5_CAP_GEN(dev, roce) &&
134 	       (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, sw_owner) ||
135 		(MLX5_CAP_ESW_FLOWTABLE_FDB(dev, sw_owner_v2) &&
136 		 (MLX5_CAP_GEN(dev, steering_format_version) <=
137 		  MLX5_STEERING_FORMAT_CONNECTX_6DX)));
138 }
139 
140 /* buddy functions & structure */
141 
142 struct mlx5dr_icm_mr;
143 
144 struct mlx5dr_icm_buddy_mem {
145 	unsigned long		**bitmap;
146 	unsigned int		*num_free;
147 	u32			max_order;
148 	struct list_head	list_node;
149 	struct mlx5dr_icm_mr	*icm_mr;
150 	struct mlx5dr_icm_pool	*pool;
151 
152 	/* This is the list of used chunks. HW may be accessing this memory */
153 	struct list_head	used_list;
154 	u64			used_memory;
155 
156 	/* Hardware may be accessing this memory but at some future,
157 	 * undetermined time, it might cease to do so.
158 	 * sync_ste command sets them free.
159 	 */
160 	struct list_head	hot_list;
161 };
162 
163 int mlx5dr_buddy_init(struct mlx5dr_icm_buddy_mem *buddy,
164 		      unsigned int max_order);
165 void mlx5dr_buddy_cleanup(struct mlx5dr_icm_buddy_mem *buddy);
166 int mlx5dr_buddy_alloc_mem(struct mlx5dr_icm_buddy_mem *buddy,
167 			   unsigned int order,
168 			   unsigned int *segment);
169 void mlx5dr_buddy_free_mem(struct mlx5dr_icm_buddy_mem *buddy,
170 			   unsigned int seg, unsigned int order);
171 
172 #endif /* _MLX5DR_H_ */
173