1 /*- 2 * Copyright (c) 2013-2017, Mellanox Technologies, Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #ifndef _MLX5_FS_CORE_ 27 #define _MLX5_FS_CORE_ 28 29 #include <asm/atomic.h> 30 #include <linux/completion.h> 31 #include <linux/mutex.h> 32 #include <dev/mlx5/fs.h> 33 34 enum fs_type { 35 FS_TYPE_NAMESPACE, 36 FS_TYPE_PRIO, 37 FS_TYPE_FLOW_TABLE, 38 FS_TYPE_FLOW_GROUP, 39 FS_TYPE_FLOW_ENTRY, 40 FS_TYPE_FLOW_DEST 41 }; 42 43 enum fs_ft_type { 44 FS_FT_NIC_RX = 0x0, 45 FS_FT_ESW_EGRESS_ACL = 0x2, 46 FS_FT_ESW_INGRESS_ACL = 0x3, 47 FS_FT_FDB = 0X4, 48 FS_FT_SNIFFER_RX = 0x5, 49 FS_FT_SNIFFER_TX = 0x6 50 }; 51 52 enum fs_fte_status { 53 FS_FTE_STATUS_EXISTING = 1UL << 0, 54 }; 55 56 /* Should always be the first variable in the struct */ 57 struct fs_base { 58 struct list_head list; 59 struct fs_base *parent; 60 enum fs_type type; 61 struct kref refcount; 62 /* lock the node for writing and traversing */ 63 struct mutex lock; 64 struct completion complete; 65 atomic_t users_refcount; 66 const char *name; 67 }; 68 69 struct mlx5_flow_rule { 70 struct fs_base base; 71 struct mlx5_flow_destination dest_attr; 72 struct list_head clients_data; 73 /*protect clients lits*/ 74 struct mutex clients_lock; 75 }; 76 77 struct fs_fte { 78 struct fs_base base; 79 u32 val[MLX5_ST_SZ_DW(fte_match_param)]; 80 uint32_t dests_size; 81 uint32_t flow_tag; 82 struct list_head dests; 83 uint32_t index; /* index in ft */ 84 u8 action; /* MLX5_FLOW_CONTEXT_ACTION */ 85 enum fs_fte_status status; 86 }; 87 88 struct fs_star_rule { 89 struct mlx5_flow_group *fg; 90 struct fs_fte *fte; 91 }; 92 93 struct mlx5_flow_table { 94 struct fs_base base; 95 /* sorted list by start_index */ 96 struct list_head fgs; 97 struct { 98 bool active; 99 unsigned int max_types; 100 unsigned int num_types; 101 } autogroup; 102 unsigned int max_fte; 103 unsigned int level; 104 uint32_t id; 105 u16 vport; 106 enum fs_ft_type type; 107 struct fs_star_rule star_rule; 108 unsigned int shared_refcount; 109 }; 110 111 enum fs_prio_flags { 112 MLX5_CORE_FS_PRIO_SHARED = 1 113 }; 114 115 struct fs_prio { 116 struct fs_base base; 117 struct list_head objs; /* each object is a namespace or ft */ 118 unsigned int max_ft; 119 unsigned int num_ft; 120 unsigned int max_ns; 121 unsigned int prio; 122 /*When create shared flow table, this lock should be taken*/ 123 struct mutex shared_lock; 124 u8 flags; 125 }; 126 127 struct mlx5_flow_namespace { 128 /* parent == NULL => root ns */ 129 struct fs_base base; 130 /* sorted by priority number */ 131 struct list_head prios; /* list of fs_prios */ 132 struct list_head list_notifiers; 133 struct rw_semaphore notifiers_rw_sem; 134 struct rw_semaphore dests_rw_sem; 135 }; 136 137 struct mlx5_flow_root_namespace { 138 struct mlx5_flow_namespace ns; 139 struct mlx5_flow_table *ft_level_0; 140 enum fs_ft_type table_type; 141 struct mlx5_core_dev *dev; 142 struct mlx5_flow_table *root_ft; 143 /* When chaining flow-tables, this lock should be taken */ 144 struct mutex fs_chain_lock; 145 }; 146 147 struct mlx5_flow_group { 148 struct fs_base base; 149 struct list_head ftes; 150 struct mlx5_core_fs_mask mask; 151 uint32_t start_index; 152 uint32_t max_ftes; 153 uint32_t num_ftes; 154 uint32_t id; 155 }; 156 157 struct mlx5_flow_handler { 158 struct list_head list; 159 rule_event_fn add_dst_cb; 160 rule_event_fn del_dst_cb; 161 void *client_context; 162 struct mlx5_flow_namespace *ns; 163 }; 164 165 struct fs_client_priv_data { 166 struct mlx5_flow_handler *fs_handler; 167 struct list_head list; 168 void *client_dst_data; 169 }; 170 171 struct mlx5_modify_hdr { 172 enum mlx5_flow_namespace_type ns_type; 173 u32 id; 174 }; 175 176 void _fs_remove_node(struct kref *kref); 177 #define fs_get_obj(v, _base) {v = container_of((_base), typeof(*v), base); } 178 #define fs_get_parent(v, child) {v = (child)->base.parent ? \ 179 container_of((child)->base.parent, \ 180 typeof(*v), base) : NULL; } 181 182 #define fs_list_for_each_entry(pos, cond, root) \ 183 list_for_each_entry(pos, root, base.list) \ 184 if (!(cond)) {} else 185 186 #define fs_list_for_each_entry_continue(pos, cond, root) \ 187 list_for_each_entry_continue(pos, root, base.list) \ 188 if (!(cond)) {} else 189 190 #define fs_list_for_each_entry_reverse(pos, cond, root) \ 191 list_for_each_entry_reverse(pos, root, base.list) \ 192 if (!(cond)) {} else 193 194 #define fs_list_for_each_entry_continue_reverse(pos, cond, root) \ 195 list_for_each_entry_continue_reverse(pos, root, base.list) \ 196 if (!(cond)) {} else 197 198 #define fs_for_each_ft(pos, prio) \ 199 fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_TABLE, \ 200 &(prio)->objs) 201 202 #define fs_for_each_ft_reverse(pos, prio) \ 203 fs_list_for_each_entry_reverse(pos, \ 204 (pos)->base.type == FS_TYPE_FLOW_TABLE, \ 205 &(prio)->objs) 206 207 #define fs_for_each_ns(pos, prio) \ 208 fs_list_for_each_entry(pos, \ 209 (pos)->base.type == FS_TYPE_NAMESPACE, \ 210 &(prio)->objs) 211 212 #define fs_for_each_ns_or_ft_reverse(pos, prio) \ 213 list_for_each_entry_reverse(pos, &(prio)->objs, list) \ 214 if (!((pos)->type == FS_TYPE_NAMESPACE || \ 215 (pos)->type == FS_TYPE_FLOW_TABLE)) {} else 216 217 #define fs_for_each_ns_or_ft(pos, prio) \ 218 list_for_each_entry(pos, &(prio)->objs, list) \ 219 if (!((pos)->type == FS_TYPE_NAMESPACE || \ 220 (pos)->type == FS_TYPE_FLOW_TABLE)) {} else 221 222 #define fs_for_each_ns_or_ft_continue_reverse(pos, prio) \ 223 list_for_each_entry_continue_reverse(pos, &(prio)->objs, list) \ 224 if (!((pos)->type == FS_TYPE_NAMESPACE || \ 225 (pos)->type == FS_TYPE_FLOW_TABLE)) {} else 226 227 #define fs_for_each_ns_or_ft_continue(pos, prio) \ 228 list_for_each_entry_continue(pos, &(prio)->objs, list) \ 229 if (!((pos)->type == FS_TYPE_NAMESPACE || \ 230 (pos)->type == FS_TYPE_FLOW_TABLE)) {} else 231 232 #define fs_for_each_prio(pos, ns) \ 233 fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_PRIO, \ 234 &(ns)->prios) 235 236 #define fs_for_each_prio_reverse(pos, ns) \ 237 fs_list_for_each_entry_reverse(pos, (pos)->base.type == FS_TYPE_PRIO, \ 238 &(ns)->prios) 239 240 #define fs_for_each_prio_continue(pos, ns) \ 241 fs_list_for_each_entry_continue(pos, (pos)->base.type == FS_TYPE_PRIO, \ 242 &(ns)->prios) 243 244 #define fs_for_each_prio_continue_reverse(pos, ns) \ 245 fs_list_for_each_entry_continue_reverse(pos, \ 246 (pos)->base.type == FS_TYPE_PRIO, \ 247 &(ns)->prios) 248 249 #define fs_for_each_fg(pos, ft) \ 250 fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_GROUP, \ 251 &(ft)->fgs) 252 253 #define fs_for_each_fte(pos, fg) \ 254 fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_ENTRY, \ 255 &(fg)->ftes) 256 #define fs_for_each_dst(pos, fte) \ 257 fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_DEST, \ 258 &(fte)->dests) 259 260 int mlx5_cmd_fs_create_ft(struct mlx5_core_dev *dev, 261 u16 vport, 262 enum fs_ft_type type, unsigned int level, 263 unsigned int log_size, unsigned int *table_id); 264 265 int mlx5_cmd_fs_destroy_ft(struct mlx5_core_dev *dev, 266 u16 vport, 267 enum fs_ft_type type, unsigned int table_id); 268 269 int mlx5_cmd_fs_create_fg(struct mlx5_core_dev *dev, 270 u32 *in, 271 u16 vport, 272 enum fs_ft_type type, unsigned int table_id, 273 unsigned int *group_id); 274 275 int mlx5_cmd_fs_destroy_fg(struct mlx5_core_dev *dev, 276 u16 vport, 277 enum fs_ft_type type, unsigned int table_id, 278 unsigned int group_id); 279 280 281 int mlx5_cmd_fs_set_fte(struct mlx5_core_dev *dev, 282 u16 vport, 283 enum fs_fte_status *fte_status, 284 u32 *match_val, 285 enum fs_ft_type type, unsigned int table_id, 286 unsigned int index, unsigned int group_id, 287 unsigned int flow_tag, 288 unsigned short action, int dest_size, 289 struct list_head *dests); /* mlx5_flow_desination */ 290 291 int mlx5_cmd_fs_delete_fte(struct mlx5_core_dev *dev, 292 u16 vport, 293 enum fs_fte_status *fte_status, 294 enum fs_ft_type type, unsigned int table_id, 295 unsigned int index); 296 297 int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev, 298 enum fs_ft_type type, 299 unsigned int id); 300 301 int mlx5_init_fs(struct mlx5_core_dev *dev); 302 void mlx5_cleanup_fs(struct mlx5_core_dev *dev); 303 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev, 304 unsigned long interval); 305 306 int mlx5_cmd_modify_header_alloc(struct mlx5_core_dev *dev, 307 enum mlx5_flow_namespace_type namespace, 308 u8 num_actions, 309 void *modify_actions, 310 struct mlx5_modify_hdr *modify_hdr); 311 void mlx5_cmd_modify_header_dealloc(struct mlx5_core_dev *dev, 312 struct mlx5_modify_hdr *modify_hdr); 313 #endif 314