1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #ifndef __NFP_FLOWER_H__ 5 #define __NFP_FLOWER_H__ 1 6 7 #include "cmsg.h" 8 9 #include <linux/circ_buf.h> 10 #include <linux/hashtable.h> 11 #include <linux/rhashtable.h> 12 #include <linux/time64.h> 13 #include <linux/types.h> 14 #include <net/pkt_cls.h> 15 #include <net/tcp.h> 16 #include <linux/workqueue.h> 17 #include <linux/idr.h> 18 19 struct nfp_fl_pre_lag; 20 struct net_device; 21 struct nfp_app; 22 23 #define NFP_FL_STAT_ID_MU_NUM GENMASK(31, 22) 24 #define NFP_FL_STAT_ID_STAT GENMASK(21, 0) 25 26 #define NFP_FL_STATS_ELEM_RS FIELD_SIZEOF(struct nfp_fl_stats_id, \ 27 init_unalloc) 28 #define NFP_FLOWER_MASK_ENTRY_RS 256 29 #define NFP_FLOWER_MASK_ELEMENT_RS 1 30 #define NFP_FLOWER_MASK_HASH_BITS 10 31 32 #define NFP_FL_META_FLAG_MANAGE_MASK BIT(7) 33 34 #define NFP_FL_MASK_REUSE_TIME_NS 40000 35 #define NFP_FL_MASK_ID_LOCATION 1 36 37 /* Extra features bitmap. */ 38 #define NFP_FL_FEATS_GENEVE BIT(0) 39 #define NFP_FL_NBI_MTU_SETTING BIT(1) 40 #define NFP_FL_FEATS_GENEVE_OPT BIT(2) 41 #define NFP_FL_FEATS_VLAN_PCP BIT(3) 42 #define NFP_FL_FEATS_FLOW_MOD BIT(5) 43 #define NFP_FL_FEATS_FLOW_MERGE BIT(30) 44 #define NFP_FL_FEATS_LAG BIT(31) 45 46 struct nfp_fl_mask_id { 47 struct circ_buf mask_id_free_list; 48 ktime_t *last_used; 49 u8 init_unallocated; 50 }; 51 52 struct nfp_fl_stats_id { 53 struct circ_buf free_list; 54 u32 init_unalloc; 55 u8 repeated_em_count; 56 }; 57 58 /** 59 * struct nfp_fl_tunnel_offloads - priv data for tunnel offloads 60 * @offloaded_macs: Hashtable of the offloaded MAC addresses 61 * @ipv4_off_list: List of IPv4 addresses to offload 62 * @neigh_off_list: List of neighbour offloads 63 * @ipv4_off_lock: Lock for the IPv4 address list 64 * @neigh_off_lock: Lock for the neighbour address list 65 * @mac_off_ids: IDA to manage id assignment for offloaded MACs 66 * @neigh_nb: Notifier to monitor neighbour state 67 */ 68 struct nfp_fl_tunnel_offloads { 69 struct rhashtable offloaded_macs; 70 struct list_head ipv4_off_list; 71 struct list_head neigh_off_list; 72 struct mutex ipv4_off_lock; 73 spinlock_t neigh_off_lock; 74 struct ida mac_off_ids; 75 struct notifier_block neigh_nb; 76 }; 77 78 /** 79 * struct nfp_mtu_conf - manage MTU setting 80 * @portnum: NFP port number of repr with requested MTU change 81 * @requested_val: MTU value requested for repr 82 * @ack: Received ack that MTU has been correctly set 83 * @wait_q: Wait queue for MTU acknowledgements 84 * @lock: Lock for setting/reading MTU variables 85 */ 86 struct nfp_mtu_conf { 87 u32 portnum; 88 unsigned int requested_val; 89 bool ack; 90 wait_queue_head_t wait_q; 91 spinlock_t lock; 92 }; 93 94 /** 95 * struct nfp_fl_lag - Flower APP priv data for link aggregation 96 * @work: Work queue for writing configs to the HW 97 * @lock: Lock to protect lag_group_list 98 * @group_list: List of all master/slave groups offloaded 99 * @ida_handle: IDA to handle group ids 100 * @pkt_num: Incremented for each config packet sent 101 * @batch_ver: Incremented for each batch of config packets 102 * @global_inst: Instance allocator for groups 103 * @rst_cfg: Marker to reset HW LAG config 104 * @retrans_skbs: Cmsgs that could not be processed by HW and require 105 * retransmission 106 */ 107 struct nfp_fl_lag { 108 struct delayed_work work; 109 struct mutex lock; 110 struct list_head group_list; 111 struct ida ida_handle; 112 unsigned int pkt_num; 113 unsigned int batch_ver; 114 u8 global_inst; 115 bool rst_cfg; 116 struct sk_buff_head retrans_skbs; 117 }; 118 119 /** 120 * struct nfp_fl_internal_ports - Flower APP priv data for additional ports 121 * @port_ids: Assignment of ids to any additional ports 122 * @lock: Lock for extra ports list 123 */ 124 struct nfp_fl_internal_ports { 125 struct idr port_ids; 126 spinlock_t lock; 127 }; 128 129 /** 130 * struct nfp_flower_priv - Flower APP per-vNIC priv data 131 * @app: Back pointer to app 132 * @nn: Pointer to vNIC 133 * @mask_id_seed: Seed used for mask hash table 134 * @flower_version: HW version of flower 135 * @flower_ext_feats: Bitmap of extra features the HW supports 136 * @stats_ids: List of free stats ids 137 * @mask_ids: List of free mask ids 138 * @mask_table: Hash table used to store masks 139 * @stats_ring_size: Maximum number of allowed stats ids 140 * @flow_table: Hash table used to store flower rules 141 * @stats: Stored stats updates for flower rules 142 * @stats_lock: Lock for flower rule stats updates 143 * @cmsg_work: Workqueue for control messages processing 144 * @cmsg_skbs_high: List of higher priority skbs for control message 145 * processing 146 * @cmsg_skbs_low: List of lower priority skbs for control message 147 * processing 148 * @tun: Tunnel offload data 149 * @reify_replies: atomically stores the number of replies received 150 * from firmware for repr reify 151 * @reify_wait_queue: wait queue for repr reify response counting 152 * @mtu_conf: Configuration of repr MTU value 153 * @nfp_lag: Link aggregation data block 154 * @indr_block_cb_priv: List of priv data passed to indirect block cbs 155 * @non_repr_priv: List of offloaded non-repr ports and their priv data 156 * @active_mem_unit: Current active memory unit for flower rules 157 * @total_mem_units: Total number of available memory units for flower rules 158 * @internal_ports: Internal port ids used in offloaded rules 159 */ 160 struct nfp_flower_priv { 161 struct nfp_app *app; 162 struct nfp_net *nn; 163 u32 mask_id_seed; 164 u64 flower_version; 165 u64 flower_ext_feats; 166 struct nfp_fl_stats_id stats_ids; 167 struct nfp_fl_mask_id mask_ids; 168 DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS); 169 u32 stats_ring_size; 170 struct rhashtable flow_table; 171 struct nfp_fl_stats *stats; 172 spinlock_t stats_lock; /* lock stats */ 173 struct work_struct cmsg_work; 174 struct sk_buff_head cmsg_skbs_high; 175 struct sk_buff_head cmsg_skbs_low; 176 struct nfp_fl_tunnel_offloads tun; 177 atomic_t reify_replies; 178 wait_queue_head_t reify_wait_queue; 179 struct nfp_mtu_conf mtu_conf; 180 struct nfp_fl_lag nfp_lag; 181 struct list_head indr_block_cb_priv; 182 struct list_head non_repr_priv; 183 unsigned int active_mem_unit; 184 unsigned int total_mem_units; 185 struct nfp_fl_internal_ports internal_ports; 186 }; 187 188 /** 189 * struct nfp_flower_repr_priv - Flower APP per-repr priv data 190 * @nfp_repr: Back pointer to nfp_repr 191 * @lag_port_flags: Extended port flags to record lag state of repr 192 * @mac_offloaded: Flag indicating a MAC address is offloaded for repr 193 * @offloaded_mac_addr: MAC address that has been offloaded for repr 194 * @mac_list: List entry of reprs that share the same offloaded MAC 195 */ 196 struct nfp_flower_repr_priv { 197 struct nfp_repr *nfp_repr; 198 unsigned long lag_port_flags; 199 bool mac_offloaded; 200 u8 offloaded_mac_addr[ETH_ALEN]; 201 struct list_head mac_list; 202 }; 203 204 /** 205 * struct nfp_flower_non_repr_priv - Priv data for non-repr offloaded ports 206 * @list: List entry of offloaded reprs 207 * @netdev: Pointer to non-repr net_device 208 * @ref_count: Number of references held for this priv data 209 * @mac_offloaded: Flag indicating a MAC address is offloaded for device 210 * @offloaded_mac_addr: MAC address that has been offloaded for dev 211 */ 212 struct nfp_flower_non_repr_priv { 213 struct list_head list; 214 struct net_device *netdev; 215 int ref_count; 216 bool mac_offloaded; 217 u8 offloaded_mac_addr[ETH_ALEN]; 218 }; 219 220 struct nfp_fl_key_ls { 221 u32 key_layer_two; 222 u8 key_layer; 223 int key_size; 224 }; 225 226 struct nfp_fl_rule_metadata { 227 u8 key_len; 228 u8 mask_len; 229 u8 act_len; 230 u8 flags; 231 __be32 host_ctx_id; 232 __be64 host_cookie __packed; 233 __be64 flow_version __packed; 234 __be32 shortcut; 235 }; 236 237 struct nfp_fl_stats { 238 u64 pkts; 239 u64 bytes; 240 u64 used; 241 }; 242 243 struct nfp_fl_payload { 244 struct nfp_fl_rule_metadata meta; 245 unsigned long tc_flower_cookie; 246 struct rhash_head fl_node; 247 struct rcu_head rcu; 248 __be32 nfp_tun_ipv4_addr; 249 struct net_device *ingress_dev; 250 char *unmasked_data; 251 char *mask_data; 252 char *action_data; 253 }; 254 255 extern const struct rhashtable_params nfp_flower_table_params; 256 257 struct nfp_fl_stats_frame { 258 __be32 stats_con_id; 259 __be32 pkt_count; 260 __be64 byte_count; 261 __be64 stats_cookie; 262 }; 263 264 static inline bool 265 nfp_flower_internal_port_can_offload(struct nfp_app *app, 266 struct net_device *netdev) 267 { 268 struct nfp_flower_priv *app_priv = app->priv; 269 270 if (!(app_priv->flower_ext_feats & NFP_FL_FEATS_FLOW_MERGE)) 271 return false; 272 if (!netdev->rtnl_link_ops) 273 return false; 274 if (!strcmp(netdev->rtnl_link_ops->kind, "openvswitch")) 275 return true; 276 277 return false; 278 } 279 280 int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count, 281 unsigned int host_ctx_split); 282 void nfp_flower_metadata_cleanup(struct nfp_app *app); 283 284 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev, 285 enum tc_setup_type type, void *type_data); 286 int nfp_flower_compile_flow_match(struct nfp_app *app, 287 struct tc_cls_flower_offload *flow, 288 struct nfp_fl_key_ls *key_ls, 289 struct net_device *netdev, 290 struct nfp_fl_payload *nfp_flow, 291 enum nfp_flower_tun_type tun_type); 292 int nfp_flower_compile_action(struct nfp_app *app, 293 struct tc_cls_flower_offload *flow, 294 struct net_device *netdev, 295 struct nfp_fl_payload *nfp_flow); 296 int nfp_compile_flow_metadata(struct nfp_app *app, 297 struct tc_cls_flower_offload *flow, 298 struct nfp_fl_payload *nfp_flow, 299 struct net_device *netdev); 300 int nfp_modify_flow_metadata(struct nfp_app *app, 301 struct nfp_fl_payload *nfp_flow); 302 303 struct nfp_fl_payload * 304 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie, 305 struct net_device *netdev); 306 struct nfp_fl_payload * 307 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie); 308 309 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb); 310 311 int nfp_tunnel_config_start(struct nfp_app *app); 312 void nfp_tunnel_config_stop(struct nfp_app *app); 313 int nfp_tunnel_mac_event_handler(struct nfp_app *app, 314 struct net_device *netdev, 315 unsigned long event, void *ptr); 316 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4); 317 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4); 318 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb); 319 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb); 320 void nfp_flower_lag_init(struct nfp_fl_lag *lag); 321 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag); 322 int nfp_flower_lag_reset(struct nfp_fl_lag *lag); 323 int nfp_flower_lag_netdev_event(struct nfp_flower_priv *priv, 324 struct net_device *netdev, 325 unsigned long event, void *ptr); 326 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb); 327 int nfp_flower_lag_populate_pre_action(struct nfp_app *app, 328 struct net_device *master, 329 struct nfp_fl_pre_lag *pre_act); 330 int nfp_flower_lag_get_output_id(struct nfp_app *app, 331 struct net_device *master); 332 int nfp_flower_reg_indir_block_handler(struct nfp_app *app, 333 struct net_device *netdev, 334 unsigned long event); 335 336 void 337 __nfp_flower_non_repr_priv_get(struct nfp_flower_non_repr_priv *non_repr_priv); 338 struct nfp_flower_non_repr_priv * 339 nfp_flower_non_repr_priv_get(struct nfp_app *app, struct net_device *netdev); 340 void 341 __nfp_flower_non_repr_priv_put(struct nfp_flower_non_repr_priv *non_repr_priv); 342 void 343 nfp_flower_non_repr_priv_put(struct nfp_app *app, struct net_device *netdev); 344 u32 nfp_flower_get_port_id_from_netdev(struct nfp_app *app, 345 struct net_device *netdev); 346 #endif 347