1 /* 2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 11 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 13 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 14 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 15 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 16 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 17 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 18 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 19 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 20 * SUCH DAMAGE. 21 */ 22 23 #ifndef _MLX5_FS_CORE_ 24 #define _MLX5_FS_CORE_ 25 26 #include <dev/mlx5/fs.h> 27 28 #define FDB_TC_MAX_CHAIN 3 29 #define FDB_FT_CHAIN (FDB_TC_MAX_CHAIN + 1) 30 #define FDB_TC_SLOW_PATH_CHAIN (FDB_FT_CHAIN + 1) 31 32 /* The index of the last real chain (FT) + 1 as chain zero is valid as well */ 33 #define FDB_NUM_CHAINS (FDB_FT_CHAIN + 1) 34 35 #define FDB_TC_MAX_PRIO 16 36 #define FDB_TC_LEVELS_PER_PRIO 2 37 38 struct mlx5_flow_definer { 39 enum mlx5_flow_namespace_type ns_type; 40 u32 id; 41 }; 42 43 struct mlx5_modify_hdr { 44 enum mlx5_flow_namespace_type ns_type; 45 union { 46 u32 id; 47 }; 48 }; 49 50 struct mlx5_pkt_reformat { 51 enum mlx5_flow_namespace_type ns_type; 52 int reformat_type; /* from mlx5_ifc */ 53 union { 54 u32 id; 55 }; 56 }; 57 58 /* FS_TYPE_PRIO_CHAINS is a PRIO that will have namespaces only, 59 * and those are in parallel to one another when going over them to connect 60 * a new flow table. Meaning the last flow table in a TYPE_PRIO prio in one 61 * parallel namespace will not automatically connect to the first flow table 62 * found in any prio in any next namespace, but skip the entire containing 63 * TYPE_PRIO_CHAINS prio. 64 * 65 * This is used to implement tc chains, each chain of prios is a different 66 * namespace inside a containing TYPE_PRIO_CHAINS prio. 67 */ 68 69 enum fs_node_type { 70 FS_TYPE_NAMESPACE, 71 FS_TYPE_PRIO, 72 FS_TYPE_PRIO_CHAINS, 73 FS_TYPE_FLOW_TABLE, 74 FS_TYPE_FLOW_GROUP, 75 FS_TYPE_FLOW_ENTRY, 76 FS_TYPE_FLOW_DEST 77 }; 78 79 /**********************************************************************************************************/ 80 81 82 #define fs_ft_type fs_flow_table_type 83 /************************************************************************************************************/ 84 enum fs_flow_table_type { 85 FS_FT_NIC_RX = 0x0, 86 FS_FT_NIC_TX = 0x1, 87 FS_FT_ESW_EGRESS_ACL = 0x2, 88 FS_FT_ESW_INGRESS_ACL = 0x3, 89 FS_FT_FDB = 0X4, 90 FS_FT_SNIFFER_RX = 0X5, 91 FS_FT_SNIFFER_TX = 0X6, 92 FS_FT_RDMA_RX = 0X7, 93 FS_FT_RDMA_TX = 0X8, 94 FS_FT_PORT_SEL = 0X9, 95 FS_FT_MAX_TYPE = FS_FT_PORT_SEL, 96 }; 97 98 enum fs_flow_table_op_mod { 99 FS_FT_OP_MOD_NORMAL, 100 FS_FT_OP_MOD_LAG_DEMUX, 101 }; 102 103 enum fs_fte_status { 104 FS_FTE_STATUS_EXISTING = 1UL << 0, 105 }; 106 107 enum mlx5_flow_steering_mode { 108 MLX5_FLOW_STEERING_MODE_DMFS, 109 MLX5_FLOW_STEERING_MODE_SMFS 110 }; 111 112 enum mlx5_flow_steering_capabilty { 113 MLX5_FLOW_STEERING_CAP_VLAN_PUSH_ON_RX = 1UL << 0, 114 MLX5_FLOW_STEERING_CAP_VLAN_POP_ON_TX = 1UL << 1, 115 MLX5_FLOW_STEERING_CAP_MATCH_RANGES = 1UL << 2, 116 }; 117 118 struct mlx5_flow_steering { 119 struct mlx5_core_dev *dev; 120 enum mlx5_flow_steering_mode mode; 121 struct kmem_cache *fgs_cache; 122 struct kmem_cache *ftes_cache; 123 struct mlx5_flow_root_namespace *root_ns; 124 struct mlx5_flow_root_namespace *fdb_root_ns; 125 struct mlx5_flow_namespace **fdb_sub_ns; 126 struct mlx5_flow_root_namespace **esw_egress_root_ns; 127 struct mlx5_flow_root_namespace **esw_ingress_root_ns; 128 struct mlx5_flow_root_namespace *sniffer_tx_root_ns; 129 struct mlx5_flow_root_namespace *sniffer_rx_root_ns; 130 struct mlx5_flow_root_namespace *rdma_rx_root_ns; 131 struct mlx5_flow_root_namespace *rdma_tx_root_ns; 132 struct mlx5_flow_root_namespace *egress_root_ns; 133 struct mlx5_flow_root_namespace *port_sel_root_ns; 134 int esw_egress_acl_vports; 135 int esw_ingress_acl_vports; 136 }; 137 138 struct fs_node { 139 struct list_head list; 140 struct list_head children; 141 enum fs_node_type type; 142 struct fs_node *parent; 143 struct fs_node *root; 144 /* lock the node for writing and traversing */ 145 struct rw_semaphore lock; 146 refcount_t refcount; 147 bool active; 148 void (*del_hw_func)(struct fs_node *); 149 void (*del_sw_func)(struct fs_node *); 150 atomic_t version; 151 }; 152 153 struct mlx5_flow_rule { 154 struct fs_node node; 155 struct mlx5_flow_table *ft; 156 struct mlx5_flow_destination dest_attr; 157 /* next_ft should be accessed under chain_lock and only of 158 * destination type is FWD_NEXT_fT. 159 */ 160 struct list_head next_ft; 161 u32 sw_action; 162 }; 163 164 struct mlx5_flow_handle { 165 int num_rules; 166 struct mlx5_flow_rule *rule[]; 167 }; 168 169 /* Type of children is mlx5_flow_group */ 170 struct mlx5_flow_table { 171 struct fs_node node; 172 u32 id; 173 u16 vport; 174 unsigned int max_fte; 175 unsigned int level; 176 enum fs_flow_table_type type; 177 enum fs_flow_table_op_mod op_mod; 178 struct { 179 bool active; 180 unsigned int required_groups; 181 unsigned int group_size; 182 unsigned int num_groups; 183 unsigned int max_fte; 184 } autogroup; 185 /* Protect fwd_rules */ 186 struct mutex lock; 187 /* FWD rules that point on this flow table */ 188 struct list_head fwd_rules; 189 u32 flags; 190 struct xarray fgs_xa; 191 enum mlx5_flow_table_miss_action def_miss_action; 192 struct mlx5_flow_namespace *ns; 193 }; 194 195 struct mlx5_ft_underlay_qp { 196 struct list_head list; 197 u32 qpn; 198 }; 199 200 #define MLX5_FTE_MATCH_PARAM_RESERVED reserved_at_e00 201 /* Calculate the fte_match_param length and without the reserved length. 202 * Make sure the reserved field is the last. 203 */ 204 #define MLX5_ST_SZ_DW_MATCH_PARAM \ 205 ((MLX5_BYTE_OFF(fte_match_param, MLX5_FTE_MATCH_PARAM_RESERVED) / sizeof(u32)) + \ 206 BUILD_BUG_ON_ZERO(MLX5_ST_SZ_BYTES(fte_match_param) != \ 207 MLX5_FLD_SZ_BYTES(fte_match_param, \ 208 MLX5_FTE_MATCH_PARAM_RESERVED) +\ 209 MLX5_BYTE_OFF(fte_match_param, \ 210 MLX5_FTE_MATCH_PARAM_RESERVED))) 211 212 /* Type of children is mlx5_flow_rule */ 213 struct fs_fte { 214 struct fs_node node; 215 u32 val[MLX5_ST_SZ_DW_MATCH_PARAM]; 216 u32 dests_size; 217 u32 fwd_dests; 218 u32 index; 219 struct mlx5_flow_context flow_context; 220 struct mlx5_flow_act action; 221 enum fs_fte_status status; 222 struct mlx5_fc *counter; 223 int modify_mask; 224 }; 225 226 /* Type of children is mlx5_flow_table/namespace */ 227 struct fs_prio { 228 struct fs_node node; 229 unsigned int num_levels; 230 unsigned int start_level; 231 unsigned int prio; 232 unsigned int num_ft; 233 }; 234 235 /* Type of children is fs_prio */ 236 struct mlx5_flow_namespace { 237 /* parent == NULL => root ns */ 238 struct fs_node node; 239 enum mlx5_flow_table_miss_action def_miss_action; 240 }; 241 242 struct mlx5_flow_group_mask { 243 u8 match_criteria_enable; 244 u32 match_criteria[MLX5_ST_SZ_DW_MATCH_PARAM]; 245 }; 246 247 /* Type of children is fs_fte */ 248 struct mlx5_flow_group { 249 struct fs_node node; 250 struct mlx5_flow_group_mask mask; 251 u32 start_index; 252 u32 max_ftes; 253 struct ida fte_allocator; 254 u32 id; 255 struct xarray ftes_xa; 256 }; 257 258 struct mlx5_flow_root_namespace { 259 struct mlx5_flow_namespace ns; 260 enum mlx5_flow_steering_mode mode; 261 enum fs_flow_table_type table_type; 262 struct mlx5_core_dev *dev; 263 struct mlx5_flow_table *root_ft; 264 /* Should be held when chaining flow tables */ 265 struct mutex chain_lock; 266 struct list_head underlay_qpns; 267 const struct mlx5_flow_cmds *cmds; 268 }; 269 270 int mlx5_init_fc_stats(struct mlx5_core_dev *dev); 271 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev); 272 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev, 273 struct delayed_work *dwork, 274 unsigned long delay); 275 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev, 276 unsigned long interval); 277 278 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_fw_cmds(void); 279 280 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns, 281 struct mlx5_flow_root_namespace *peer_ns); 282 283 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns, 284 enum mlx5_flow_steering_mode mode); 285 286 int mlx5_fs_core_alloc(struct mlx5_core_dev *dev); 287 void mlx5_fs_core_free(struct mlx5_core_dev *dev); 288 int mlx5_fs_core_init(struct mlx5_core_dev *dev); 289 void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev); 290 291 int mlx5_fs_egress_acls_init(struct mlx5_core_dev *dev, int total_vports); 292 void mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev *dev); 293 int mlx5_fs_ingress_acls_init(struct mlx5_core_dev *dev, int total_vports); 294 void mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev *dev); 295 296 u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type); 297 298 struct mlx5_flow_root_namespace *find_root(struct fs_node *node); 299 300 #define fs_get_obj(v, _node) {v = container_of((_node), typeof(*v), node); } 301 302 #define fs_list_for_each_entry(pos, root) \ 303 list_for_each_entry(pos, root, node.list) 304 305 #define fs_list_for_each_entry_safe(pos, tmp, root) \ 306 list_for_each_entry_safe(pos, tmp, root, node.list) 307 308 #define fs_for_each_ns_or_ft_reverse(pos, prio) \ 309 list_for_each_entry_reverse(pos, &(prio)->node.children, list) 310 311 #define fs_for_each_ns_or_ft(pos, prio) \ 312 list_for_each_entry(pos, (&(prio)->node.children), list) 313 314 #define fs_for_each_prio(pos, ns) \ 315 fs_list_for_each_entry(pos, &(ns)->node.children) 316 317 #define fs_for_each_ns(pos, prio) \ 318 fs_list_for_each_entry(pos, &(prio)->node.children) 319 320 #define fs_for_each_ft(pos, prio) \ 321 fs_list_for_each_entry(pos, &(prio)->node.children) 322 323 #define fs_for_each_ft_safe(pos, tmp, prio) \ 324 fs_list_for_each_entry_safe(pos, tmp, &(prio)->node.children) 325 326 #define fs_for_each_fg(pos, ft) \ 327 fs_list_for_each_entry(pos, &(ft)->node.children) 328 329 #define fs_for_each_fte(pos, fg) \ 330 fs_list_for_each_entry(pos, &(fg)->node.children) 331 332 #define fs_for_each_dst(pos, fte) \ 333 fs_list_for_each_entry(pos, &(fte)->node.children) 334 335 #define MLX5_CAP_FLOWTABLE_TYPE(mdev, cap, type) ( \ 336 (type == FS_FT_NIC_RX) ? MLX5_CAP_FLOWTABLE_NIC_RX(mdev, cap) : \ 337 (type == FS_FT_NIC_TX) ? MLX5_CAP_FLOWTABLE_NIC_TX(mdev, cap) : \ 338 (type == FS_FT_ESW_EGRESS_ACL) ? MLX5_CAP_ESW_EGRESS_ACL(mdev, cap) : \ 339 (type == FS_FT_ESW_INGRESS_ACL) ? MLX5_CAP_ESW_INGRESS_ACL(mdev, cap) : \ 340 (type == FS_FT_FDB) ? MLX5_CAP_ESW_FLOWTABLE_FDB(mdev, cap) : \ 341 (type == FS_FT_SNIFFER_RX) ? MLX5_CAP_FLOWTABLE_SNIFFER_RX(mdev, cap) : \ 342 (type == FS_FT_SNIFFER_TX) ? MLX5_CAP_FLOWTABLE_SNIFFER_TX(mdev, cap) : \ 343 (type == FS_FT_RDMA_RX) ? MLX5_CAP_FLOWTABLE_RDMA_RX(mdev, cap) : \ 344 (type == FS_FT_RDMA_TX) ? MLX5_CAP_FLOWTABLE_RDMA_TX(mdev, cap) : \ 345 (type == FS_FT_PORT_SEL) ? MLX5_CAP_FLOWTABLE_PORT_SELECTION(mdev, cap) : \ 346 (BUILD_BUG_ON_ZERO(FS_FT_PORT_SEL != FS_FT_MAX_TYPE))\ 347 ) 348 349 #endif 350