Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
   4 */
   5
   6#include <rdma/ib_user_verbs.h>
   7#include <rdma/ib_verbs.h>
   8#include <rdma/uverbs_types.h>
   9#include <rdma/uverbs_ioctl.h>
  10#include <rdma/uverbs_std_types.h>
  11#include <rdma/mlx5_user_ioctl_cmds.h>
  12#include <rdma/mlx5_user_ioctl_verbs.h>
  13#include <rdma/ib_hdrs.h>
  14#include <rdma/ib_umem.h>
  15#include <linux/mlx5/driver.h>
  16#include <linux/mlx5/fs.h>
  17#include <linux/mlx5/fs_helpers.h>
 
  18#include <linux/mlx5/eswitch.h>
  19#include <net/inet_ecn.h>
  20#include "mlx5_ib.h"
  21#include "counters.h"
  22#include "devx.h"
  23#include "fs.h"
  24
  25#define UVERBS_MODULE_NAME mlx5_ib
  26#include <rdma/uverbs_named_ioctl.h>
  27
  28enum {
  29	MATCH_CRITERIA_ENABLE_OUTER_BIT,
  30	MATCH_CRITERIA_ENABLE_MISC_BIT,
  31	MATCH_CRITERIA_ENABLE_INNER_BIT,
  32	MATCH_CRITERIA_ENABLE_MISC2_BIT
  33};
  34
  35#define HEADER_IS_ZERO(match_criteria, headers)			           \
  36	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
  37		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
  38
  39static u8 get_match_criteria_enable(u32 *match_criteria)
  40{
  41	u8 match_criteria_enable;
  42
  43	match_criteria_enable =
  44		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
  45		MATCH_CRITERIA_ENABLE_OUTER_BIT;
  46	match_criteria_enable |=
  47		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
  48		MATCH_CRITERIA_ENABLE_MISC_BIT;
  49	match_criteria_enable |=
  50		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
  51		MATCH_CRITERIA_ENABLE_INNER_BIT;
  52	match_criteria_enable |=
  53		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
  54		MATCH_CRITERIA_ENABLE_MISC2_BIT;
  55
  56	return match_criteria_enable;
  57}
  58
  59static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
  60{
  61	u8 entry_mask;
  62	u8 entry_val;
  63	int err = 0;
  64
  65	if (!mask)
  66		goto out;
  67
  68	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
  69			      ip_protocol);
  70	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
  71			     ip_protocol);
  72	if (!entry_mask) {
  73		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
  74		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
  75		goto out;
  76	}
  77	/* Don't override existing ip protocol */
  78	if (mask != entry_mask || val != entry_val)
  79		err = -EINVAL;
  80out:
  81	return err;
  82}
  83
  84static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
  85			   bool inner)
  86{
  87	if (inner) {
  88		MLX5_SET(fte_match_set_misc,
  89			 misc_c, inner_ipv6_flow_label, mask);
  90		MLX5_SET(fte_match_set_misc,
  91			 misc_v, inner_ipv6_flow_label, val);
  92	} else {
  93		MLX5_SET(fte_match_set_misc,
  94			 misc_c, outer_ipv6_flow_label, mask);
  95		MLX5_SET(fte_match_set_misc,
  96			 misc_v, outer_ipv6_flow_label, val);
  97	}
  98}
  99
 100static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
 101{
 102	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
 103	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
 104	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
 105	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
 106}
 107
 108static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
 109{
 110	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
 111	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
 112		return -EOPNOTSUPP;
 113
 114	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
 115	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
 116		return -EOPNOTSUPP;
 117
 118	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
 119	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
 120		return -EOPNOTSUPP;
 121
 122	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
 123	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
 124		return -EOPNOTSUPP;
 125
 126	return 0;
 127}
 128
 129#define LAST_ETH_FIELD vlan_tag
 
 130#define LAST_IPV4_FIELD tos
 131#define LAST_IPV6_FIELD traffic_class
 132#define LAST_TCP_UDP_FIELD src_port
 133#define LAST_TUNNEL_FIELD tunnel_id
 134#define LAST_FLOW_TAG_FIELD tag_id
 135#define LAST_DROP_FIELD size
 136#define LAST_COUNTERS_FIELD counters
 137
 138/* Field is the last supported field */
 139#define FIELDS_NOT_SUPPORTED(filter, field)                                    \
 140	memchr_inv((void *)&filter.field + sizeof(filter.field), 0,            \
 141		   sizeof(filter) - offsetofend(typeof(filter), field))
 
 
 
 142
 143int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
 144			   bool is_egress,
 145			   struct mlx5_flow_act *action)
 146{
 147
 148	switch (maction->ib_action.type) {
 
 
 
 
 
 
 
 
 
 
 149	case IB_FLOW_ACTION_UNSPECIFIED:
 150		if (maction->flow_action_raw.sub_type ==
 151		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
 152			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
 153				return -EINVAL;
 154			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
 155			action->modify_hdr =
 156				maction->flow_action_raw.modify_hdr;
 157			return 0;
 158		}
 159		if (maction->flow_action_raw.sub_type ==
 160		    MLX5_IB_FLOW_ACTION_DECAP) {
 161			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
 162				return -EINVAL;
 163			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
 164			return 0;
 165		}
 166		if (maction->flow_action_raw.sub_type ==
 167		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
 168			if (action->action &
 169			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
 170				return -EINVAL;
 171			action->action |=
 172				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
 173			action->pkt_reformat =
 174				maction->flow_action_raw.pkt_reformat;
 175			return 0;
 176		}
 177		fallthrough;
 178	default:
 179		return -EOPNOTSUPP;
 180	}
 181}
 182
 183static int parse_flow_attr(struct mlx5_core_dev *mdev,
 184			   struct mlx5_flow_spec *spec,
 185			   const union ib_flow_spec *ib_spec,
 186			   const struct ib_flow_attr *flow_attr,
 187			   struct mlx5_flow_act *action, u32 prev_type)
 188{
 189	struct mlx5_flow_context *flow_context = &spec->flow_context;
 190	u32 *match_c = spec->match_criteria;
 191	u32 *match_v = spec->match_value;
 192	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
 193					   misc_parameters);
 194	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
 195					   misc_parameters);
 196	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
 197					    misc_parameters_2);
 198	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
 199					    misc_parameters_2);
 200	void *headers_c;
 201	void *headers_v;
 202	int match_ipv;
 203	int ret;
 204
 205	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
 206		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
 207					 inner_headers);
 208		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
 209					 inner_headers);
 210		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 211					ft_field_support.inner_ip_version);
 212	} else {
 213		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
 214					 outer_headers);
 215		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
 216					 outer_headers);
 217		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 218					ft_field_support.outer_ip_version);
 219	}
 220
 221	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
 222	case IB_FLOW_SPEC_ETH:
 223		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
 224			return -EOPNOTSUPP;
 225
 226		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 227					     dmac_47_16),
 228				ib_spec->eth.mask.dst_mac);
 229		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 230					     dmac_47_16),
 231				ib_spec->eth.val.dst_mac);
 232
 233		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 234					     smac_47_16),
 235				ib_spec->eth.mask.src_mac);
 236		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 237					     smac_47_16),
 238				ib_spec->eth.val.src_mac);
 239
 240		if (ib_spec->eth.mask.vlan_tag) {
 241			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 242				 cvlan_tag, 1);
 243			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 244				 cvlan_tag, 1);
 245
 246			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 247				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
 248			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 249				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
 250
 251			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 252				 first_cfi,
 253				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
 254			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 255				 first_cfi,
 256				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
 257
 258			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 259				 first_prio,
 260				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
 261			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 262				 first_prio,
 263				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
 264		}
 265		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 266			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
 267		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 268			 ethertype, ntohs(ib_spec->eth.val.ether_type));
 269		break;
 270	case IB_FLOW_SPEC_IPV4:
 271		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
 272			return -EOPNOTSUPP;
 273
 274		if (match_ipv) {
 275			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 276				 ip_version, 0xf);
 277			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 278				 ip_version, MLX5_FS_IPV4_VERSION);
 279		} else {
 280			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 281				 ethertype, 0xffff);
 282			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 283				 ethertype, ETH_P_IP);
 284		}
 285
 286		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 287				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
 288		       &ib_spec->ipv4.mask.src_ip,
 289		       sizeof(ib_spec->ipv4.mask.src_ip));
 290		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 291				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
 292		       &ib_spec->ipv4.val.src_ip,
 293		       sizeof(ib_spec->ipv4.val.src_ip));
 294		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 295				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
 296		       &ib_spec->ipv4.mask.dst_ip,
 297		       sizeof(ib_spec->ipv4.mask.dst_ip));
 298		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 299				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
 300		       &ib_spec->ipv4.val.dst_ip,
 301		       sizeof(ib_spec->ipv4.val.dst_ip));
 302
 303		set_tos(headers_c, headers_v,
 304			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
 305
 306		if (set_proto(headers_c, headers_v,
 307			      ib_spec->ipv4.mask.proto,
 308			      ib_spec->ipv4.val.proto))
 309			return -EINVAL;
 310		break;
 311	case IB_FLOW_SPEC_IPV6:
 312		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
 313			return -EOPNOTSUPP;
 314
 315		if (match_ipv) {
 316			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 317				 ip_version, 0xf);
 318			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 319				 ip_version, MLX5_FS_IPV6_VERSION);
 320		} else {
 321			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 322				 ethertype, 0xffff);
 323			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 324				 ethertype, ETH_P_IPV6);
 325		}
 326
 327		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 328				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
 329		       &ib_spec->ipv6.mask.src_ip,
 330		       sizeof(ib_spec->ipv6.mask.src_ip));
 331		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 332				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
 333		       &ib_spec->ipv6.val.src_ip,
 334		       sizeof(ib_spec->ipv6.val.src_ip));
 335		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 336				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
 337		       &ib_spec->ipv6.mask.dst_ip,
 338		       sizeof(ib_spec->ipv6.mask.dst_ip));
 339		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 340				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
 341		       &ib_spec->ipv6.val.dst_ip,
 342		       sizeof(ib_spec->ipv6.val.dst_ip));
 343
 344		set_tos(headers_c, headers_v,
 345			ib_spec->ipv6.mask.traffic_class,
 346			ib_spec->ipv6.val.traffic_class);
 347
 348		if (set_proto(headers_c, headers_v,
 349			      ib_spec->ipv6.mask.next_hdr,
 350			      ib_spec->ipv6.val.next_hdr))
 351			return -EINVAL;
 352
 353		set_flow_label(misc_params_c, misc_params_v,
 354			       ntohl(ib_spec->ipv6.mask.flow_label),
 355			       ntohl(ib_spec->ipv6.val.flow_label),
 356			       ib_spec->type & IB_FLOW_SPEC_INNER);
 357		break;
 358	case IB_FLOW_SPEC_ESP:
 359		return -EOPNOTSUPP;
 
 
 
 
 
 
 
 360	case IB_FLOW_SPEC_TCP:
 361		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
 362					 LAST_TCP_UDP_FIELD))
 363			return -EOPNOTSUPP;
 364
 365		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
 366			return -EINVAL;
 367
 368		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
 369			 ntohs(ib_spec->tcp_udp.mask.src_port));
 370		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
 371			 ntohs(ib_spec->tcp_udp.val.src_port));
 372
 373		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
 374			 ntohs(ib_spec->tcp_udp.mask.dst_port));
 375		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
 376			 ntohs(ib_spec->tcp_udp.val.dst_port));
 377		break;
 378	case IB_FLOW_SPEC_UDP:
 379		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
 380					 LAST_TCP_UDP_FIELD))
 381			return -EOPNOTSUPP;
 382
 383		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
 384			return -EINVAL;
 385
 386		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
 387			 ntohs(ib_spec->tcp_udp.mask.src_port));
 388		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
 389			 ntohs(ib_spec->tcp_udp.val.src_port));
 390
 391		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
 392			 ntohs(ib_spec->tcp_udp.mask.dst_port));
 393		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
 394			 ntohs(ib_spec->tcp_udp.val.dst_port));
 395		break;
 396	case IB_FLOW_SPEC_GRE:
 397		if (ib_spec->gre.mask.c_ks_res0_ver)
 398			return -EOPNOTSUPP;
 399
 400		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
 401			return -EINVAL;
 402
 403		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
 404			 0xff);
 405		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
 406			 IPPROTO_GRE);
 407
 408		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
 409			 ntohs(ib_spec->gre.mask.protocol));
 410		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
 411			 ntohs(ib_spec->gre.val.protocol));
 412
 413		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
 414				    gre_key.nvgre.hi),
 415		       &ib_spec->gre.mask.key,
 416		       sizeof(ib_spec->gre.mask.key));
 417		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
 418				    gre_key.nvgre.hi),
 419		       &ib_spec->gre.val.key,
 420		       sizeof(ib_spec->gre.val.key));
 421		break;
 422	case IB_FLOW_SPEC_MPLS:
 423		switch (prev_type) {
 424		case IB_FLOW_SPEC_UDP:
 425			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 426						   ft_field_support.outer_first_mpls_over_udp),
 427						   &ib_spec->mpls.mask.tag))
 428				return -EOPNOTSUPP;
 429
 430			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 431					    outer_first_mpls_over_udp),
 432			       &ib_spec->mpls.val.tag,
 433			       sizeof(ib_spec->mpls.val.tag));
 434			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 435					    outer_first_mpls_over_udp),
 436			       &ib_spec->mpls.mask.tag,
 437			       sizeof(ib_spec->mpls.mask.tag));
 438			break;
 439		case IB_FLOW_SPEC_GRE:
 440			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 441						   ft_field_support.outer_first_mpls_over_gre),
 442						   &ib_spec->mpls.mask.tag))
 443				return -EOPNOTSUPP;
 444
 445			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 446					    outer_first_mpls_over_gre),
 447			       &ib_spec->mpls.val.tag,
 448			       sizeof(ib_spec->mpls.val.tag));
 449			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 450					    outer_first_mpls_over_gre),
 451			       &ib_spec->mpls.mask.tag,
 452			       sizeof(ib_spec->mpls.mask.tag));
 453			break;
 454		default:
 455			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
 456				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 457							   ft_field_support.inner_first_mpls),
 458							   &ib_spec->mpls.mask.tag))
 459					return -EOPNOTSUPP;
 460
 461				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 462						    inner_first_mpls),
 463				       &ib_spec->mpls.val.tag,
 464				       sizeof(ib_spec->mpls.val.tag));
 465				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 466						    inner_first_mpls),
 467				       &ib_spec->mpls.mask.tag,
 468				       sizeof(ib_spec->mpls.mask.tag));
 469			} else {
 470				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 471							   ft_field_support.outer_first_mpls),
 472							   &ib_spec->mpls.mask.tag))
 473					return -EOPNOTSUPP;
 474
 475				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 476						    outer_first_mpls),
 477				       &ib_spec->mpls.val.tag,
 478				       sizeof(ib_spec->mpls.val.tag));
 479				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 480						    outer_first_mpls),
 481				       &ib_spec->mpls.mask.tag,
 482				       sizeof(ib_spec->mpls.mask.tag));
 483			}
 484		}
 485		break;
 486	case IB_FLOW_SPEC_VXLAN_TUNNEL:
 487		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
 488					 LAST_TUNNEL_FIELD))
 489			return -EOPNOTSUPP;
 490
 491		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
 492			 ntohl(ib_spec->tunnel.mask.tunnel_id));
 493		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
 494			 ntohl(ib_spec->tunnel.val.tunnel_id));
 495		break;
 496	case IB_FLOW_SPEC_ACTION_TAG:
 497		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
 498					 LAST_FLOW_TAG_FIELD))
 499			return -EOPNOTSUPP;
 500		if (ib_spec->flow_tag.tag_id >= BIT(24))
 501			return -EINVAL;
 502
 503		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
 504		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
 505		break;
 506	case IB_FLOW_SPEC_ACTION_DROP:
 507		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
 508					 LAST_DROP_FIELD))
 509			return -EOPNOTSUPP;
 510		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
 511		break;
 512	case IB_FLOW_SPEC_ACTION_HANDLE:
 513		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
 514			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
 515		if (ret)
 516			return ret;
 517		break;
 518	case IB_FLOW_SPEC_ACTION_COUNT:
 519		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
 520					 LAST_COUNTERS_FIELD))
 521			return -EOPNOTSUPP;
 522
 523		/* for now support only one counters spec per flow */
 524		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
 525			return -EINVAL;
 526
 527		action->counters = ib_spec->flow_count.counters;
 528		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
 529		break;
 530	default:
 531		return -EINVAL;
 532	}
 533
 534	return 0;
 535}
 536
 537/* If a flow could catch both multicast and unicast packets,
 538 * it won't fall into the multicast flow steering table and this rule
 539 * could steal other multicast packets.
 540 */
 541static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
 542{
 543	union ib_flow_spec *flow_spec;
 544
 545	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
 546	    ib_attr->num_of_specs < 1)
 547		return false;
 548
 549	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
 550	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
 551		struct ib_flow_spec_ipv4 *ipv4_spec;
 552
 553		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
 554		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
 555			return true;
 556
 557		return false;
 558	}
 559
 560	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
 561		struct ib_flow_spec_eth *eth_spec;
 562
 563		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
 564		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
 565		       is_multicast_ether_addr(eth_spec->val.dst_mac);
 566	}
 567
 568	return false;
 569}
 570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 571static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
 572			       const struct ib_flow_attr *flow_attr,
 573			       bool check_inner)
 574{
 575	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
 576	int match_ipv = check_inner ?
 577			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 578					ft_field_support.inner_ip_version) :
 579			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 580					ft_field_support.outer_ip_version);
 581	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
 582	bool ipv4_spec_valid, ipv6_spec_valid;
 583	unsigned int ip_spec_type = 0;
 584	bool has_ethertype = false;
 585	unsigned int spec_index;
 586	bool mask_valid = true;
 587	u16 eth_type = 0;
 588	bool type_valid;
 589
 590	/* Validate that ethertype is correct */
 591	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
 592		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
 593		    ib_spec->eth.mask.ether_type) {
 594			mask_valid = (ib_spec->eth.mask.ether_type ==
 595				      htons(0xffff));
 596			has_ethertype = true;
 597			eth_type = ntohs(ib_spec->eth.val.ether_type);
 598		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
 599			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
 600			ip_spec_type = ib_spec->type;
 601		}
 602		ib_spec = (void *)ib_spec + ib_spec->size;
 603	}
 604
 605	type_valid = (!has_ethertype) || (!ip_spec_type);
 606	if (!type_valid && mask_valid) {
 607		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
 608			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
 609		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
 610			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
 611
 612		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
 613			     (((eth_type == ETH_P_MPLS_UC) ||
 614			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
 615	}
 616
 617	return type_valid;
 618}
 619
 620static bool is_valid_attr(struct mlx5_core_dev *mdev,
 621			  const struct ib_flow_attr *flow_attr)
 622{
 623	return is_valid_ethertype(mdev, flow_attr, false) &&
 624	       is_valid_ethertype(mdev, flow_attr, true);
 625}
 626
 627static void put_flow_table(struct mlx5_ib_dev *dev,
 628			   struct mlx5_ib_flow_prio *prio, bool ft_added)
 629{
 630	prio->refcount -= !!ft_added;
 631	if (!prio->refcount) {
 632		mlx5_destroy_flow_table(prio->flow_table);
 633		prio->flow_table = NULL;
 634	}
 635}
 636
 637static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
 638{
 639	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
 640							  struct mlx5_ib_flow_handler,
 641							  ibflow);
 642	struct mlx5_ib_flow_handler *iter, *tmp;
 643	struct mlx5_ib_dev *dev = handler->dev;
 644
 645	mutex_lock(&dev->flow_db->lock);
 646
 647	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
 648		mlx5_del_flow_rules(iter->rule);
 649		put_flow_table(dev, iter->prio, true);
 650		list_del(&iter->list);
 651		kfree(iter);
 652	}
 653
 654	mlx5_del_flow_rules(handler->rule);
 655	put_flow_table(dev, handler->prio, true);
 656	mlx5_ib_counters_clear_description(handler->ibcounters);
 657	mutex_unlock(&dev->flow_db->lock);
 658	if (handler->flow_matcher)
 659		atomic_dec(&handler->flow_matcher->usecnt);
 660	kfree(handler);
 661
 662	return 0;
 663}
 664
 665static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
 666{
 667	priority *= 2;
 668	if (!dont_trap)
 669		priority++;
 670	return priority;
 671}
 672
 673enum flow_table_type {
 674	MLX5_IB_FT_RX,
 675	MLX5_IB_FT_TX
 676};
 677
 678#define MLX5_FS_MAX_TYPES	 6
 679#define MLX5_FS_MAX_ENTRIES	 BIT(16)
 680
 681static bool mlx5_ib_shared_ft_allowed(struct ib_device *device)
 682{
 683	struct mlx5_ib_dev *dev = to_mdev(device);
 684
 685	return MLX5_CAP_GEN(dev->mdev, shared_object_to_user_object_allowed);
 686}
 687
 688static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_ib_dev *dev,
 689					   struct mlx5_flow_namespace *ns,
 690					   struct mlx5_ib_flow_prio *prio,
 691					   int priority,
 692					   int num_entries, int num_groups,
 693					   u32 flags)
 694{
 695	struct mlx5_flow_table_attr ft_attr = {};
 696	struct mlx5_flow_table *ft;
 697
 698	ft_attr.prio = priority;
 699	ft_attr.max_fte = num_entries;
 700	ft_attr.flags = flags;
 701	ft_attr.autogroup.max_num_groups = num_groups;
 702	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
 703	if (IS_ERR(ft))
 704		return ERR_CAST(ft);
 705
 706	prio->flow_table = ft;
 707	prio->refcount = 0;
 708	return prio;
 709}
 710
 711static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
 712						struct ib_flow_attr *flow_attr,
 713						enum flow_table_type ft_type)
 714{
 715	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
 716	struct mlx5_flow_namespace *ns = NULL;
 717	enum mlx5_flow_namespace_type fn_type;
 718	struct mlx5_ib_flow_prio *prio;
 719	struct mlx5_flow_table *ft;
 720	int max_table_size;
 721	int num_entries;
 722	int num_groups;
 723	bool esw_encap;
 724	u32 flags = 0;
 725	int priority;
 726
 727	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 728						       log_max_ft_size));
 729	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
 730		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
 731	switch (flow_attr->type) {
 732	case IB_FLOW_ATTR_NORMAL:
 733		if (flow_is_multicast_only(flow_attr) && !dont_trap)
 
 
 734			priority = MLX5_IB_FLOW_MCAST_PRIO;
 735		else
 736			priority = ib_prio_to_core_prio(flow_attr->priority,
 737							dont_trap);
 738		if (ft_type == MLX5_IB_FT_RX) {
 739			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
 740			prio = &dev->flow_db->prios[priority];
 741			if (!dev->is_rep && !esw_encap &&
 742			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
 743				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
 744			if (!dev->is_rep && !esw_encap &&
 745			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 746						      reformat_l3_tunnel_to_l2))
 747				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
 748		} else {
 749			max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(
 750				dev->mdev, log_max_ft_size));
 
 751			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
 752			prio = &dev->flow_db->egress_prios[priority];
 753			if (!dev->is_rep && !esw_encap &&
 754			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
 755				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
 756		}
 757		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
 758		num_entries = MLX5_FS_MAX_ENTRIES;
 759		num_groups = MLX5_FS_MAX_TYPES;
 760		break;
 761	case IB_FLOW_ATTR_ALL_DEFAULT:
 762	case IB_FLOW_ATTR_MC_DEFAULT:
 763		ns = mlx5_get_flow_namespace(dev->mdev,
 764					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
 765		build_leftovers_ft_param(&priority, &num_entries, &num_groups);
 
 
 766		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
 767		break;
 768	case IB_FLOW_ATTR_SNIFFER:
 769		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
 770					allow_sniffer_and_nic_rx_shared_tir))
 771			return ERR_PTR(-EOPNOTSUPP);
 772
 773		ns = mlx5_get_flow_namespace(
 774			dev->mdev, ft_type == MLX5_IB_FT_RX ?
 775					   MLX5_FLOW_NAMESPACE_SNIFFER_RX :
 776					   MLX5_FLOW_NAMESPACE_SNIFFER_TX);
 777
 778		prio = &dev->flow_db->sniffer[ft_type];
 779		priority = 0;
 780		num_entries = 1;
 781		num_groups = 1;
 782		break;
 783	default:
 784		break;
 785	}
 786
 787	if (!ns)
 788		return ERR_PTR(-EOPNOTSUPP);
 789
 790	max_table_size = min_t(int, num_entries, max_table_size);
 791
 792	ft = prio->flow_table;
 793	if (!ft)
 794		return _get_prio(dev, ns, prio, priority, max_table_size,
 795				 num_groups, flags);
 796
 797	return prio;
 798}
 799
 800enum {
 801	RDMA_RX_ECN_OPCOUNTER_PRIO,
 802	RDMA_RX_CNP_OPCOUNTER_PRIO,
 803};
 804
 805enum {
 806	RDMA_TX_CNP_OPCOUNTER_PRIO,
 807};
 808
 809static int set_vhca_port_spec(struct mlx5_ib_dev *dev, u32 port_num,
 810			      struct mlx5_flow_spec *spec)
 811{
 812	if (!MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
 813					ft_field_support.source_vhca_port) ||
 814	    !MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
 815					ft_field_support.source_vhca_port))
 816		return -EOPNOTSUPP;
 817
 818	MLX5_SET_TO_ONES(fte_match_param, &spec->match_criteria,
 819			 misc_parameters.source_vhca_port);
 820	MLX5_SET(fte_match_param, &spec->match_value,
 821		 misc_parameters.source_vhca_port, port_num);
 822
 823	return 0;
 824}
 825
 826static int set_ecn_ce_spec(struct mlx5_ib_dev *dev, u32 port_num,
 827			   struct mlx5_flow_spec *spec, int ipv)
 828{
 829	if (!MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
 830					ft_field_support.outer_ip_version))
 831		return -EOPNOTSUPP;
 832
 833	if (mlx5_core_mp_enabled(dev->mdev) &&
 834	    set_vhca_port_spec(dev, port_num, spec))
 835		return -EOPNOTSUPP;
 836
 837	MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
 838			 outer_headers.ip_ecn);
 839	MLX5_SET(fte_match_param, spec->match_value, outer_headers.ip_ecn,
 840		 INET_ECN_CE);
 841	MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
 842			 outer_headers.ip_version);
 843	MLX5_SET(fte_match_param, spec->match_value, outer_headers.ip_version,
 844		 ipv);
 845
 846	spec->match_criteria_enable =
 847		get_match_criteria_enable(spec->match_criteria);
 848
 849	return 0;
 850}
 851
 852static int set_cnp_spec(struct mlx5_ib_dev *dev, u32 port_num,
 853			struct mlx5_flow_spec *spec)
 854{
 855	if (mlx5_core_mp_enabled(dev->mdev) &&
 856	    set_vhca_port_spec(dev, port_num, spec))
 857		return -EOPNOTSUPP;
 858
 859	MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
 860			 misc_parameters.bth_opcode);
 861	MLX5_SET(fte_match_param, spec->match_value, misc_parameters.bth_opcode,
 862		 IB_BTH_OPCODE_CNP);
 863
 864	spec->match_criteria_enable =
 865		get_match_criteria_enable(spec->match_criteria);
 866
 867	return 0;
 868}
 869
 870int mlx5_ib_fs_add_op_fc(struct mlx5_ib_dev *dev, u32 port_num,
 871			 struct mlx5_ib_op_fc *opfc,
 872			 enum mlx5_ib_optional_counter_type type)
 873{
 874	enum mlx5_flow_namespace_type fn_type;
 875	int priority, i, err, spec_num;
 876	struct mlx5_flow_act flow_act = {};
 877	struct mlx5_flow_destination dst;
 878	struct mlx5_flow_namespace *ns;
 879	struct mlx5_ib_flow_prio *prio;
 880	struct mlx5_flow_spec *spec;
 881
 882	spec = kcalloc(MAX_OPFC_RULES, sizeof(*spec), GFP_KERNEL);
 883	if (!spec)
 884		return -ENOMEM;
 885
 886	switch (type) {
 887	case MLX5_IB_OPCOUNTER_CC_RX_CE_PKTS:
 888		if (set_ecn_ce_spec(dev, port_num, &spec[0],
 889				    MLX5_FS_IPV4_VERSION) ||
 890		    set_ecn_ce_spec(dev, port_num, &spec[1],
 891				    MLX5_FS_IPV6_VERSION)) {
 892			err = -EOPNOTSUPP;
 893			goto free;
 894		}
 895		spec_num = 2;
 896		fn_type = MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS;
 897		priority = RDMA_RX_ECN_OPCOUNTER_PRIO;
 898		break;
 899
 900	case MLX5_IB_OPCOUNTER_CC_RX_CNP_PKTS:
 901		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
 902					ft_field_support_2_nic_receive_rdma.bth_opcode) ||
 903		    set_cnp_spec(dev, port_num, &spec[0])) {
 904			err = -EOPNOTSUPP;
 905			goto free;
 906		}
 907		spec_num = 1;
 908		fn_type = MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS;
 909		priority = RDMA_RX_CNP_OPCOUNTER_PRIO;
 910		break;
 911
 912	case MLX5_IB_OPCOUNTER_CC_TX_CNP_PKTS:
 913		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
 914					ft_field_support_2_nic_transmit_rdma.bth_opcode) ||
 915		    set_cnp_spec(dev, port_num, &spec[0])) {
 916			err = -EOPNOTSUPP;
 917			goto free;
 918		}
 919		spec_num = 1;
 920		fn_type = MLX5_FLOW_NAMESPACE_RDMA_TX_COUNTERS;
 921		priority = RDMA_TX_CNP_OPCOUNTER_PRIO;
 922		break;
 923
 924	default:
 925		err = -EOPNOTSUPP;
 926		goto free;
 927	}
 928
 929	ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
 930	if (!ns) {
 931		err = -EOPNOTSUPP;
 932		goto free;
 933	}
 934
 935	prio = &dev->flow_db->opfcs[type];
 936	if (!prio->flow_table) {
 937		prio = _get_prio(dev, ns, prio, priority,
 938				 dev->num_ports * MAX_OPFC_RULES, 1, 0);
 939		if (IS_ERR(prio)) {
 940			err = PTR_ERR(prio);
 941			goto free;
 942		}
 943	}
 944
 945	dst.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
 946	dst.counter_id = mlx5_fc_id(opfc->fc);
 947
 948	flow_act.action =
 949		MLX5_FLOW_CONTEXT_ACTION_COUNT | MLX5_FLOW_CONTEXT_ACTION_ALLOW;
 950
 951	for (i = 0; i < spec_num; i++) {
 952		opfc->rule[i] = mlx5_add_flow_rules(prio->flow_table, &spec[i],
 953						    &flow_act, &dst, 1);
 954		if (IS_ERR(opfc->rule[i])) {
 955			err = PTR_ERR(opfc->rule[i]);
 956			goto del_rules;
 957		}
 958	}
 959	prio->refcount += spec_num;
 960	kfree(spec);
 961
 962	return 0;
 963
 964del_rules:
 965	for (i -= 1; i >= 0; i--)
 966		mlx5_del_flow_rules(opfc->rule[i]);
 967	put_flow_table(dev, prio, false);
 968free:
 969	kfree(spec);
 970	return err;
 971}
 972
 973void mlx5_ib_fs_remove_op_fc(struct mlx5_ib_dev *dev,
 974			     struct mlx5_ib_op_fc *opfc,
 975			     enum mlx5_ib_optional_counter_type type)
 976{
 977	int i;
 978
 979	for (i = 0; i < MAX_OPFC_RULES && opfc->rule[i]; i++) {
 980		mlx5_del_flow_rules(opfc->rule[i]);
 981		put_flow_table(dev, &dev->flow_db->opfcs[type], true);
 982	}
 983}
 984
 985static void set_underlay_qp(struct mlx5_ib_dev *dev,
 986			    struct mlx5_flow_spec *spec,
 987			    u32 underlay_qpn)
 988{
 989	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
 990					   spec->match_criteria,
 991					   misc_parameters);
 992	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
 993					   misc_parameters);
 994
 995	if (underlay_qpn &&
 996	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 997				      ft_field_support.bth_dst_qp)) {
 998		MLX5_SET(fte_match_set_misc,
 999			 misc_params_v, bth_dst_qp, underlay_qpn);
1000		MLX5_SET(fte_match_set_misc,
1001			 misc_params_c, bth_dst_qp, 0xffffff);
1002	}
1003}
1004
1005static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
1006					 struct mlx5_flow_spec *spec,
1007					 struct mlx5_eswitch_rep *rep)
1008{
1009	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
1010	void *misc;
1011
1012	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1013		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1014				    misc_parameters_2);
1015
1016		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1017			 mlx5_eswitch_get_vport_metadata_for_match(rep->esw,
1018								   rep->vport));
1019		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1020				    misc_parameters_2);
1021
1022		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1023			 mlx5_eswitch_get_vport_metadata_mask());
1024	} else {
1025		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1026				    misc_parameters);
1027
1028		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
1029
1030		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1031				    misc_parameters);
1032
1033		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
1034	}
1035}
1036
1037static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
1038						      struct mlx5_ib_flow_prio *ft_prio,
1039						      const struct ib_flow_attr *flow_attr,
1040						      struct mlx5_flow_destination *dst,
1041						      u32 underlay_qpn,
1042						      struct mlx5_ib_create_flow *ucmd)
1043{
1044	struct mlx5_flow_table	*ft = ft_prio->flow_table;
1045	struct mlx5_ib_flow_handler *handler;
1046	struct mlx5_flow_act flow_act = {};
1047	struct mlx5_flow_spec *spec;
1048	struct mlx5_flow_destination dest_arr[2] = {};
1049	struct mlx5_flow_destination *rule_dst = dest_arr;
1050	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
1051	unsigned int spec_index;
1052	u32 prev_type = 0;
1053	int err = 0;
1054	int dest_num = 0;
1055	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
1056
1057	if (!is_valid_attr(dev->mdev, flow_attr))
1058		return ERR_PTR(-EINVAL);
1059
1060	if (dev->is_rep && is_egress)
1061		return ERR_PTR(-EINVAL);
1062
1063	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1064	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1065	if (!handler || !spec) {
1066		err = -ENOMEM;
1067		goto free;
1068	}
1069
1070	INIT_LIST_HEAD(&handler->list);
1071
1072	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1073		err = parse_flow_attr(dev->mdev, spec,
1074				      ib_flow, flow_attr, &flow_act,
1075				      prev_type);
1076		if (err < 0)
1077			goto free;
1078
1079		prev_type = ((union ib_flow_spec *)ib_flow)->type;
1080		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1081	}
1082
1083	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
1084		memcpy(&dest_arr[0], dst, sizeof(*dst));
1085		dest_num++;
1086	}
1087
1088	if (!flow_is_multicast_only(flow_attr))
1089		set_underlay_qp(dev, spec, underlay_qpn);
1090
1091	if (dev->is_rep && flow_attr->type != IB_FLOW_ATTR_SNIFFER) {
1092		struct mlx5_eswitch_rep *rep;
1093
1094		rep = dev->port[flow_attr->port - 1].rep;
1095		if (!rep) {
1096			err = -EINVAL;
1097			goto free;
1098		}
1099
1100		mlx5_ib_set_rule_source_port(dev, spec, rep);
1101	}
1102
1103	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
1104
 
 
 
 
 
 
1105	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1106		struct mlx5_ib_mcounters *mcounters;
1107
1108		err = mlx5_ib_flow_counters_set_data(flow_act.counters, ucmd);
1109		if (err)
1110			goto free;
1111
1112		mcounters = to_mcounters(flow_act.counters);
1113		handler->ibcounters = flow_act.counters;
1114		dest_arr[dest_num].type =
1115			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1116		dest_arr[dest_num].counter_id =
1117			mlx5_fc_id(mcounters->hw_cntrs_hndl);
1118		dest_num++;
1119	}
1120
1121	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
1122		if (!dest_num)
1123			rule_dst = NULL;
1124	} else {
1125		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)
1126			flow_act.action |=
1127				MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1128		if (is_egress)
1129			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1130		else if (dest_num)
1131			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1132	}
1133
1134	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
1135	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1136	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
1137		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
1138			     spec->flow_context.flow_tag, flow_attr->type);
1139		err = -EINVAL;
1140		goto free;
1141	}
1142	handler->rule = mlx5_add_flow_rules(ft, spec,
1143					    &flow_act,
1144					    rule_dst, dest_num);
1145
1146	if (IS_ERR(handler->rule)) {
1147		err = PTR_ERR(handler->rule);
1148		goto free;
1149	}
1150
1151	ft_prio->refcount++;
1152	handler->prio = ft_prio;
1153	handler->dev = dev;
1154
1155	ft_prio->flow_table = ft;
1156free:
1157	if (err && handler) {
1158		mlx5_ib_counters_clear_description(handler->ibcounters);
1159		kfree(handler);
1160	}
1161	kvfree(spec);
1162	return err ? ERR_PTR(err) : handler;
1163}
1164
1165static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1166						     struct mlx5_ib_flow_prio *ft_prio,
1167						     const struct ib_flow_attr *flow_attr,
1168						     struct mlx5_flow_destination *dst)
1169{
1170	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
1171}
1172
1173enum {
1174	LEFTOVERS_MC,
1175	LEFTOVERS_UC,
1176};
1177
1178static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1179							  struct mlx5_ib_flow_prio *ft_prio,
1180							  struct ib_flow_attr *flow_attr,
1181							  struct mlx5_flow_destination *dst)
1182{
1183	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1184	struct mlx5_ib_flow_handler *handler = NULL;
1185
1186	static struct {
1187		struct ib_flow_attr	flow_attr;
1188		struct ib_flow_spec_eth eth_flow;
1189	} leftovers_specs[] = {
1190		[LEFTOVERS_MC] = {
1191			.flow_attr = {
1192				.num_of_specs = 1,
1193				.size = sizeof(leftovers_specs[0])
1194			},
1195			.eth_flow = {
1196				.type = IB_FLOW_SPEC_ETH,
1197				.size = sizeof(struct ib_flow_spec_eth),
1198				.mask = {.dst_mac = {0x1} },
1199				.val =  {.dst_mac = {0x1} }
1200			}
1201		},
1202		[LEFTOVERS_UC] = {
1203			.flow_attr = {
1204				.num_of_specs = 1,
1205				.size = sizeof(leftovers_specs[0])
1206			},
1207			.eth_flow = {
1208				.type = IB_FLOW_SPEC_ETH,
1209				.size = sizeof(struct ib_flow_spec_eth),
1210				.mask = {.dst_mac = {0x1} },
1211				.val = {.dst_mac = {} }
1212			}
1213		}
1214	};
1215
1216	handler = create_flow_rule(dev, ft_prio,
1217				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
1218				   dst);
1219	if (!IS_ERR(handler) &&
1220	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
1221		handler_ucast = create_flow_rule(dev, ft_prio,
1222						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
1223						 dst);
1224		if (IS_ERR(handler_ucast)) {
1225			mlx5_del_flow_rules(handler->rule);
1226			ft_prio->refcount--;
1227			kfree(handler);
1228			handler = handler_ucast;
1229		} else {
1230			list_add(&handler_ucast->list, &handler->list);
1231		}
1232	}
1233
1234	return handler;
1235}
1236
1237static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
1238							struct mlx5_ib_flow_prio *ft_rx,
1239							struct mlx5_ib_flow_prio *ft_tx,
1240							struct mlx5_flow_destination *dst)
1241{
1242	struct mlx5_ib_flow_handler *handler_rx;
1243	struct mlx5_ib_flow_handler *handler_tx;
1244	int err;
1245	static const struct ib_flow_attr flow_attr  = {
1246		.num_of_specs = 0,
1247		.type = IB_FLOW_ATTR_SNIFFER,
1248		.size = sizeof(flow_attr)
1249	};
1250
1251	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
1252	if (IS_ERR(handler_rx)) {
1253		err = PTR_ERR(handler_rx);
1254		goto err;
1255	}
1256
1257	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
1258	if (IS_ERR(handler_tx)) {
1259		err = PTR_ERR(handler_tx);
1260		goto err_tx;
1261	}
1262
1263	list_add(&handler_tx->list, &handler_rx->list);
1264
1265	return handler_rx;
1266
1267err_tx:
1268	mlx5_del_flow_rules(handler_rx->rule);
1269	ft_rx->refcount--;
1270	kfree(handler_rx);
1271err:
1272	return ERR_PTR(err);
1273}
1274
 
1275static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
1276					   struct ib_flow_attr *flow_attr,
 
1277					   struct ib_udata *udata)
1278{
1279	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1280	struct mlx5_ib_qp *mqp = to_mqp(qp);
1281	struct mlx5_ib_flow_handler *handler = NULL;
1282	struct mlx5_flow_destination *dst = NULL;
1283	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
1284	struct mlx5_ib_flow_prio *ft_prio;
1285	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
1286	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
1287	size_t min_ucmd_sz, required_ucmd_sz;
1288	int err;
1289	int underlay_qpn;
1290
1291	if (udata && udata->inlen) {
1292		min_ucmd_sz = offsetofend(struct mlx5_ib_create_flow, reserved);
 
1293		if (udata->inlen < min_ucmd_sz)
1294			return ERR_PTR(-EOPNOTSUPP);
1295
1296		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
1297		if (err)
1298			return ERR_PTR(err);
1299
1300		/* currently supports only one counters data */
1301		if (ucmd_hdr.ncounters_data > 1)
1302			return ERR_PTR(-EINVAL);
1303
1304		required_ucmd_sz = min_ucmd_sz +
1305			sizeof(struct mlx5_ib_flow_counters_data) *
1306			ucmd_hdr.ncounters_data;
1307		if (udata->inlen > required_ucmd_sz &&
1308		    !ib_is_udata_cleared(udata, required_ucmd_sz,
1309					 udata->inlen - required_ucmd_sz))
1310			return ERR_PTR(-EOPNOTSUPP);
1311
1312		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
1313		if (!ucmd)
1314			return ERR_PTR(-ENOMEM);
1315
1316		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
1317		if (err)
1318			goto free_ucmd;
1319	}
1320
1321	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
1322		err = -ENOMEM;
1323		goto free_ucmd;
1324	}
1325
1326	if (flow_attr->flags &
1327	    ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP | IB_FLOW_ATTR_FLAGS_EGRESS)) {
 
 
1328		err = -EINVAL;
1329		goto free_ucmd;
1330	}
1331
1332	if (is_egress &&
1333	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1334	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
1335		err = -EINVAL;
1336		goto free_ucmd;
1337	}
1338
1339	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
1340	if (!dst) {
1341		err = -ENOMEM;
1342		goto free_ucmd;
1343	}
1344
1345	mutex_lock(&dev->flow_db->lock);
1346
1347	ft_prio = get_flow_table(dev, flow_attr,
1348				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
1349	if (IS_ERR(ft_prio)) {
1350		err = PTR_ERR(ft_prio);
1351		goto unlock;
1352	}
1353	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
1354		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
1355		if (IS_ERR(ft_prio_tx)) {
1356			err = PTR_ERR(ft_prio_tx);
1357			ft_prio_tx = NULL;
1358			goto destroy_ft;
1359		}
1360	}
1361
1362	if (is_egress) {
1363		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1364	} else {
1365		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1366		if (mqp->is_rss)
1367			dst->tir_num = mqp->rss_qp.tirn;
1368		else
1369			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
1370	}
1371
1372	switch (flow_attr->type) {
1373	case IB_FLOW_ATTR_NORMAL:
1374		underlay_qpn = (mqp->flags & IB_QP_CREATE_SOURCE_QPN) ?
1375				       mqp->underlay_qpn :
1376				       0;
1377		handler = _create_flow_rule(dev, ft_prio, flow_attr, dst,
1378					    underlay_qpn, ucmd);
1379		break;
1380	case IB_FLOW_ATTR_ALL_DEFAULT:
1381	case IB_FLOW_ATTR_MC_DEFAULT:
1382		handler = create_leftovers_rule(dev, ft_prio, flow_attr, dst);
1383		break;
1384	case IB_FLOW_ATTR_SNIFFER:
1385		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
1386		break;
1387	default:
1388		err = -EINVAL;
1389		goto destroy_ft;
1390	}
1391
1392	if (IS_ERR(handler)) {
1393		err = PTR_ERR(handler);
1394		handler = NULL;
1395		goto destroy_ft;
1396	}
1397
1398	mutex_unlock(&dev->flow_db->lock);
1399	kfree(dst);
1400	kfree(ucmd);
1401
1402	return &handler->ibflow;
1403
1404destroy_ft:
1405	put_flow_table(dev, ft_prio, false);
1406	if (ft_prio_tx)
1407		put_flow_table(dev, ft_prio_tx, false);
1408unlock:
1409	mutex_unlock(&dev->flow_db->lock);
1410	kfree(dst);
1411free_ucmd:
1412	kfree(ucmd);
1413	return ERR_PTR(err);
1414}
1415
1416static struct mlx5_ib_flow_prio *
1417_get_flow_table(struct mlx5_ib_dev *dev, u16 user_priority,
1418		enum mlx5_flow_namespace_type ns_type,
1419		bool mcast)
1420{
1421	struct mlx5_flow_namespace *ns = NULL;
1422	struct mlx5_ib_flow_prio *prio = NULL;
1423	int max_table_size = 0;
1424	bool esw_encap;
1425	u32 flags = 0;
1426	int priority;
1427
1428	if (mcast)
1429		priority = MLX5_IB_FLOW_MCAST_PRIO;
1430	else
1431		priority = ib_prio_to_core_prio(user_priority, false);
1432
1433	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
1434		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1435	switch (ns_type) {
1436	case MLX5_FLOW_NAMESPACE_BYPASS:
1437		max_table_size = BIT(
1438			MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, log_max_ft_size));
1439		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
1440			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
1441		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
1442					      reformat_l3_tunnel_to_l2) &&
1443		    !esw_encap)
1444			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1445		break;
1446	case MLX5_FLOW_NAMESPACE_EGRESS:
1447		max_table_size = BIT(
1448			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
1449		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) &&
1450		    !esw_encap)
1451			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1452		break;
1453	case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
1454		max_table_size = BIT(
1455			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
1456		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
1457			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
1458		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev,
1459					       reformat_l3_tunnel_to_l2) &&
1460		    esw_encap)
1461			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1462		priority = user_priority;
1463		break;
1464	case MLX5_FLOW_NAMESPACE_RDMA_RX:
1465		max_table_size = BIT(
1466			MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev, log_max_ft_size));
1467		priority = user_priority;
1468		break;
1469	case MLX5_FLOW_NAMESPACE_RDMA_TX:
1470		max_table_size = BIT(
1471			MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev, log_max_ft_size));
1472		priority = user_priority;
1473		break;
1474	default:
1475		break;
1476	}
1477
1478	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
1479
1480	ns = mlx5_get_flow_namespace(dev->mdev, ns_type);
1481	if (!ns)
1482		return ERR_PTR(-EOPNOTSUPP);
1483
1484	switch (ns_type) {
1485	case MLX5_FLOW_NAMESPACE_BYPASS:
1486		prio = &dev->flow_db->prios[priority];
1487		break;
1488	case MLX5_FLOW_NAMESPACE_EGRESS:
1489		prio = &dev->flow_db->egress_prios[priority];
1490		break;
1491	case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
1492		prio = &dev->flow_db->fdb[priority];
1493		break;
1494	case MLX5_FLOW_NAMESPACE_RDMA_RX:
1495		prio = &dev->flow_db->rdma_rx[priority];
1496		break;
1497	case MLX5_FLOW_NAMESPACE_RDMA_TX:
1498		prio = &dev->flow_db->rdma_tx[priority];
1499		break;
1500	default: return ERR_PTR(-EINVAL);
1501	}
1502
1503	if (!prio)
1504		return ERR_PTR(-EINVAL);
1505
1506	if (prio->flow_table)
1507		return prio;
1508
1509	return _get_prio(dev, ns, prio, priority, max_table_size,
1510			 MLX5_FS_MAX_TYPES, flags);
1511}
1512
1513static struct mlx5_ib_flow_handler *
1514_create_raw_flow_rule(struct mlx5_ib_dev *dev,
1515		      struct mlx5_ib_flow_prio *ft_prio,
1516		      struct mlx5_flow_destination *dst,
1517		      struct mlx5_ib_flow_matcher  *fs_matcher,
1518		      struct mlx5_flow_context *flow_context,
1519		      struct mlx5_flow_act *flow_act,
1520		      void *cmd_in, int inlen,
1521		      int dst_num)
1522{
1523	struct mlx5_ib_flow_handler *handler;
1524	struct mlx5_flow_spec *spec;
1525	struct mlx5_flow_table *ft = ft_prio->flow_table;
1526	int err = 0;
1527
1528	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1529	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1530	if (!handler || !spec) {
1531		err = -ENOMEM;
1532		goto free;
1533	}
1534
1535	INIT_LIST_HEAD(&handler->list);
1536
1537	memcpy(spec->match_value, cmd_in, inlen);
1538	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
1539	       fs_matcher->mask_len);
1540	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
1541	spec->flow_context = *flow_context;
1542
1543	handler->rule = mlx5_add_flow_rules(ft, spec,
1544					    flow_act, dst, dst_num);
1545
1546	if (IS_ERR(handler->rule)) {
1547		err = PTR_ERR(handler->rule);
1548		goto free;
1549	}
1550
1551	ft_prio->refcount++;
1552	handler->prio = ft_prio;
1553	handler->dev = dev;
1554	ft_prio->flow_table = ft;
1555
1556free:
1557	if (err)
1558		kfree(handler);
1559	kvfree(spec);
1560	return err ? ERR_PTR(err) : handler;
1561}
1562
1563static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
1564				void *match_v)
1565{
1566	void *match_c;
1567	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
1568	void *dmac, *dmac_mask;
1569	void *ipv4, *ipv4_mask;
1570
1571	if (!(fs_matcher->match_criteria_enable &
1572	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
1573		return false;
1574
1575	match_c = fs_matcher->matcher_mask.match_params;
1576	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
1577					   outer_headers);
1578	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
1579					   outer_headers);
1580
1581	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
1582			    dmac_47_16);
1583	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
1584				 dmac_47_16);
1585
1586	if (is_multicast_ether_addr(dmac) &&
1587	    is_multicast_ether_addr(dmac_mask))
1588		return true;
1589
1590	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
1591			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1592
1593	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
1594				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1595
1596	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
1597	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
1598		return true;
1599
1600	return false;
1601}
1602
1603static struct mlx5_ib_flow_handler *raw_fs_rule_add(
1604	struct mlx5_ib_dev *dev, struct mlx5_ib_flow_matcher *fs_matcher,
1605	struct mlx5_flow_context *flow_context, struct mlx5_flow_act *flow_act,
1606	u32 counter_id, void *cmd_in, int inlen, int dest_id, int dest_type)
1607{
1608	struct mlx5_flow_destination *dst;
1609	struct mlx5_ib_flow_prio *ft_prio;
1610	struct mlx5_ib_flow_handler *handler;
1611	int dst_num = 0;
1612	bool mcast;
1613	int err;
1614
1615	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
1616		return ERR_PTR(-EOPNOTSUPP);
1617
1618	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
1619		return ERR_PTR(-ENOMEM);
1620
1621	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
1622	if (!dst)
1623		return ERR_PTR(-ENOMEM);
1624
1625	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
1626	mutex_lock(&dev->flow_db->lock);
1627
1628	ft_prio = _get_flow_table(dev, fs_matcher->priority,
1629				  fs_matcher->ns_type, mcast);
1630	if (IS_ERR(ft_prio)) {
1631		err = PTR_ERR(ft_prio);
1632		goto unlock;
1633	}
1634
1635	switch (dest_type) {
1636	case MLX5_FLOW_DESTINATION_TYPE_TIR:
1637		dst[dst_num].type = dest_type;
1638		dst[dst_num++].tir_num = dest_id;
1639		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1640		break;
1641	case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
1642		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
1643		dst[dst_num++].ft_num = dest_id;
1644		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1645		break;
1646	case MLX5_FLOW_DESTINATION_TYPE_PORT:
1647		dst[dst_num++].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1648		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1649		break;
1650	default:
1651		break;
1652	}
1653
 
1654	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1655		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1656		dst[dst_num].counter_id = counter_id;
1657		dst_num++;
1658	}
1659
1660	handler = _create_raw_flow_rule(dev, ft_prio, dst_num ? dst : NULL,
1661					fs_matcher, flow_context, flow_act,
1662					cmd_in, inlen, dst_num);
1663
1664	if (IS_ERR(handler)) {
1665		err = PTR_ERR(handler);
1666		goto destroy_ft;
1667	}
1668
1669	mutex_unlock(&dev->flow_db->lock);
1670	atomic_inc(&fs_matcher->usecnt);
1671	handler->flow_matcher = fs_matcher;
1672
1673	kfree(dst);
1674
1675	return handler;
1676
1677destroy_ft:
1678	put_flow_table(dev, ft_prio, false);
1679unlock:
1680	mutex_unlock(&dev->flow_db->lock);
1681	kfree(dst);
1682
1683	return ERR_PTR(err);
1684}
1685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1686static void destroy_flow_action_raw(struct mlx5_ib_flow_action *maction)
1687{
1688	switch (maction->flow_action_raw.sub_type) {
1689	case MLX5_IB_FLOW_ACTION_MODIFY_HEADER:
1690		mlx5_modify_header_dealloc(maction->flow_action_raw.dev->mdev,
1691					   maction->flow_action_raw.modify_hdr);
1692		break;
1693	case MLX5_IB_FLOW_ACTION_PACKET_REFORMAT:
1694		mlx5_packet_reformat_dealloc(maction->flow_action_raw.dev->mdev,
1695					     maction->flow_action_raw.pkt_reformat);
1696		break;
1697	case MLX5_IB_FLOW_ACTION_DECAP:
1698		break;
1699	default:
1700		break;
1701	}
1702}
1703
1704static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
1705{
1706	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
1707
1708	switch (action->type) {
 
 
 
 
 
 
 
1709	case IB_FLOW_ACTION_UNSPECIFIED:
1710		destroy_flow_action_raw(maction);
1711		break;
1712	default:
1713		WARN_ON(true);
1714		break;
1715	}
1716
1717	kfree(maction);
1718	return 0;
1719}
1720
1721static int
1722mlx5_ib_ft_type_to_namespace(enum mlx5_ib_uapi_flow_table_type table_type,
1723			     enum mlx5_flow_namespace_type *namespace)
1724{
1725	switch (table_type) {
1726	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX:
1727		*namespace = MLX5_FLOW_NAMESPACE_BYPASS;
1728		break;
1729	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX:
1730		*namespace = MLX5_FLOW_NAMESPACE_EGRESS;
1731		break;
1732	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_FDB:
1733		*namespace = MLX5_FLOW_NAMESPACE_FDB_BYPASS;
1734		break;
1735	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_RDMA_RX:
1736		*namespace = MLX5_FLOW_NAMESPACE_RDMA_RX;
1737		break;
1738	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_RDMA_TX:
1739		*namespace = MLX5_FLOW_NAMESPACE_RDMA_TX;
1740		break;
1741	default:
1742		return -EINVAL;
1743	}
1744
1745	return 0;
1746}
1747
1748static const struct uverbs_attr_spec mlx5_ib_flow_type[] = {
1749	[MLX5_IB_FLOW_TYPE_NORMAL] = {
1750		.type = UVERBS_ATTR_TYPE_PTR_IN,
1751		.u.ptr = {
1752			.len = sizeof(u16), /* data is priority */
1753			.min_len = sizeof(u16),
1754		}
1755	},
1756	[MLX5_IB_FLOW_TYPE_SNIFFER] = {
1757		.type = UVERBS_ATTR_TYPE_PTR_IN,
1758		UVERBS_ATTR_NO_DATA(),
1759	},
1760	[MLX5_IB_FLOW_TYPE_ALL_DEFAULT] = {
1761		.type = UVERBS_ATTR_TYPE_PTR_IN,
1762		UVERBS_ATTR_NO_DATA(),
1763	},
1764	[MLX5_IB_FLOW_TYPE_MC_DEFAULT] = {
1765		.type = UVERBS_ATTR_TYPE_PTR_IN,
1766		UVERBS_ATTR_NO_DATA(),
1767	},
1768};
1769
1770static bool is_flow_dest(void *obj, int *dest_id, int *dest_type)
1771{
1772	struct devx_obj *devx_obj = obj;
1773	u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox, opcode);
1774
1775	switch (opcode) {
1776	case MLX5_CMD_OP_DESTROY_TIR:
1777		*dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1778		*dest_id = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox,
1779				    obj_id);
1780		return true;
1781
1782	case MLX5_CMD_OP_DESTROY_FLOW_TABLE:
1783		*dest_type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1784		*dest_id = MLX5_GET(destroy_flow_table_in, devx_obj->dinbox,
1785				    table_id);
1786		return true;
1787	default:
1788		return false;
1789	}
1790}
1791
1792static int get_dests(struct uverbs_attr_bundle *attrs,
1793		     struct mlx5_ib_flow_matcher *fs_matcher, int *dest_id,
1794		     int *dest_type, struct ib_qp **qp, u32 *flags)
1795{
1796	bool dest_devx, dest_qp;
1797	void *devx_obj;
1798	int err;
1799
1800	dest_devx = uverbs_attr_is_valid(attrs,
1801					 MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
1802	dest_qp = uverbs_attr_is_valid(attrs,
1803				       MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
1804
1805	*flags = 0;
1806	err = uverbs_get_flags32(flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_FLAGS,
1807				 MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS |
1808					 MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP);
1809	if (err)
1810		return err;
1811
1812	/* Both flags are not allowed */
1813	if (*flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS &&
1814	    *flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)
1815		return -EINVAL;
1816
1817	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
1818		if (dest_devx && (dest_qp || *flags))
1819			return -EINVAL;
1820		else if (dest_qp && *flags)
1821			return -EINVAL;
1822	}
1823
1824	/* Allow only DEVX object, drop as dest for FDB */
1825	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB_BYPASS &&
1826	    !(dest_devx || (*flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)))
1827		return -EINVAL;
1828
1829	/* Allow only DEVX object or QP as dest when inserting to RDMA_RX */
1830	if ((fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) &&
1831	    ((!dest_devx && !dest_qp) || (dest_devx && dest_qp)))
1832		return -EINVAL;
1833
1834	*qp = NULL;
1835	if (dest_devx) {
1836		devx_obj =
1837			uverbs_attr_get_obj(attrs,
1838					    MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
1839
1840		/* Verify that the given DEVX object is a flow
1841		 * steering destination.
1842		 */
1843		if (!is_flow_dest(devx_obj, dest_id, dest_type))
1844			return -EINVAL;
1845		/* Allow only flow table as dest when inserting to FDB or RDMA_RX */
1846		if ((fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB_BYPASS ||
1847		     fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) &&
1848		    *dest_type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1849			return -EINVAL;
1850	} else if (dest_qp) {
1851		struct mlx5_ib_qp *mqp;
1852
1853		*qp = uverbs_attr_get_obj(attrs,
1854					  MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
1855		if (IS_ERR(*qp))
1856			return PTR_ERR(*qp);
1857
1858		if ((*qp)->qp_type != IB_QPT_RAW_PACKET)
1859			return -EINVAL;
1860
1861		mqp = to_mqp(*qp);
1862		if (mqp->is_rss)
1863			*dest_id = mqp->rss_qp.tirn;
1864		else
1865			*dest_id = mqp->raw_packet_qp.rq.tirn;
1866		*dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1867	} else if ((fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS ||
1868		    fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) &&
1869		   !(*flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)) {
1870		*dest_type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1871	}
1872
1873	if (*dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1874	    (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS ||
1875	     fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX))
1876		return -EINVAL;
1877
1878	return 0;
1879}
1880
1881static bool is_flow_counter(void *obj, u32 offset, u32 *counter_id)
1882{
1883	struct devx_obj *devx_obj = obj;
1884	u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox, opcode);
1885
1886	if (opcode == MLX5_CMD_OP_DEALLOC_FLOW_COUNTER) {
1887
1888		if (offset && offset >= devx_obj->flow_counter_bulk_size)
1889			return false;
1890
1891		*counter_id = MLX5_GET(dealloc_flow_counter_in,
1892				       devx_obj->dinbox,
1893				       flow_counter_id);
1894		*counter_id += offset;
1895		return true;
1896	}
1897
1898	return false;
1899}
1900
1901#define MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS 2
1902static int UVERBS_HANDLER(MLX5_IB_METHOD_CREATE_FLOW)(
1903	struct uverbs_attr_bundle *attrs)
1904{
1905	struct mlx5_flow_context flow_context = {.flow_tag =
1906		MLX5_FS_DEFAULT_FLOW_TAG};
1907	u32 *offset_attr, offset = 0, counter_id = 0;
1908	int dest_id, dest_type = -1, inlen, len, ret, i;
1909	struct mlx5_ib_flow_handler *flow_handler;
1910	struct mlx5_ib_flow_matcher *fs_matcher;
1911	struct ib_uobject **arr_flow_actions;
1912	struct ib_uflow_resources *uflow_res;
1913	struct mlx5_flow_act flow_act = {};
1914	struct ib_qp *qp = NULL;
1915	void *devx_obj, *cmd_in;
1916	struct ib_uobject *uobj;
1917	struct mlx5_ib_dev *dev;
1918	u32 flags;
1919
1920	if (!capable(CAP_NET_RAW))
1921		return -EPERM;
1922
1923	fs_matcher = uverbs_attr_get_obj(attrs,
1924					 MLX5_IB_ATTR_CREATE_FLOW_MATCHER);
1925	uobj =  uverbs_attr_get_uobject(attrs, MLX5_IB_ATTR_CREATE_FLOW_HANDLE);
1926	dev = mlx5_udata_to_mdev(&attrs->driver_udata);
1927
1928	if (get_dests(attrs, fs_matcher, &dest_id, &dest_type, &qp, &flags))
1929		return -EINVAL;
1930
1931	if (flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS)
1932		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
1933
1934	if (flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)
1935		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1936
1937	len = uverbs_attr_get_uobjs_arr(attrs,
1938		MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX, &arr_flow_actions);
1939	if (len) {
1940		devx_obj = arr_flow_actions[0]->object;
1941
1942		if (uverbs_attr_is_valid(attrs,
1943					 MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET)) {
1944
1945			int num_offsets = uverbs_attr_ptr_get_array_size(
1946				attrs,
1947				MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET,
1948				sizeof(u32));
1949
1950			if (num_offsets != 1)
1951				return -EINVAL;
1952
1953			offset_attr = uverbs_attr_get_alloced_ptr(
1954				attrs,
1955				MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET);
1956			offset = *offset_attr;
1957		}
1958
1959		if (!is_flow_counter(devx_obj, offset, &counter_id))
1960			return -EINVAL;
1961
1962		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1963	}
1964
1965	cmd_in = uverbs_attr_get_alloced_ptr(
1966		attrs, MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
1967	inlen = uverbs_attr_get_len(attrs,
1968				    MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
1969
1970	uflow_res = flow_resources_alloc(MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS);
1971	if (!uflow_res)
1972		return -ENOMEM;
1973
1974	len = uverbs_attr_get_uobjs_arr(attrs,
1975		MLX5_IB_ATTR_CREATE_FLOW_ARR_FLOW_ACTIONS, &arr_flow_actions);
1976	for (i = 0; i < len; i++) {
1977		struct mlx5_ib_flow_action *maction =
1978			to_mflow_act(arr_flow_actions[i]->object);
1979
1980		ret = parse_flow_flow_action(maction, false, &flow_act);
1981		if (ret)
1982			goto err_out;
1983		flow_resources_add(uflow_res, IB_FLOW_SPEC_ACTION_HANDLE,
1984				   arr_flow_actions[i]->object);
1985	}
1986
1987	ret = uverbs_copy_from(&flow_context.flow_tag, attrs,
1988			       MLX5_IB_ATTR_CREATE_FLOW_TAG);
1989	if (!ret) {
1990		if (flow_context.flow_tag >= BIT(24)) {
1991			ret = -EINVAL;
1992			goto err_out;
1993		}
1994		flow_context.flags |= FLOW_CONTEXT_HAS_TAG;
1995	}
1996
1997	flow_handler =
1998		raw_fs_rule_add(dev, fs_matcher, &flow_context, &flow_act,
1999				counter_id, cmd_in, inlen, dest_id, dest_type);
2000	if (IS_ERR(flow_handler)) {
2001		ret = PTR_ERR(flow_handler);
2002		goto err_out;
2003	}
2004
2005	ib_set_flow(uobj, &flow_handler->ibflow, qp, &dev->ib_dev, uflow_res);
2006
2007	return 0;
2008err_out:
2009	ib_uverbs_flow_resources_free(uflow_res);
2010	return ret;
2011}
2012
2013static int flow_matcher_cleanup(struct ib_uobject *uobject,
2014				enum rdma_remove_reason why,
2015				struct uverbs_attr_bundle *attrs)
2016{
2017	struct mlx5_ib_flow_matcher *obj = uobject->object;
 
2018
2019	if (atomic_read(&obj->usecnt))
2020		return -EBUSY;
2021
2022	kfree(obj);
2023	return 0;
2024}
2025
2026static int steering_anchor_create_ft(struct mlx5_ib_dev *dev,
2027				     struct mlx5_ib_flow_prio *ft_prio,
2028				     enum mlx5_flow_namespace_type ns_type)
2029{
2030	struct mlx5_flow_table_attr ft_attr = {};
2031	struct mlx5_flow_namespace *ns;
2032	struct mlx5_flow_table *ft;
2033
2034	if (ft_prio->anchor.ft)
2035		return 0;
2036
2037	ns = mlx5_get_flow_namespace(dev->mdev, ns_type);
2038	if (!ns)
2039		return -EOPNOTSUPP;
2040
2041	ft_attr.flags = MLX5_FLOW_TABLE_UNMANAGED;
2042	ft_attr.uid = MLX5_SHARED_RESOURCE_UID;
2043	ft_attr.prio = 0;
2044	ft_attr.max_fte = 2;
2045	ft_attr.level = 1;
2046
2047	ft = mlx5_create_flow_table(ns, &ft_attr);
2048	if (IS_ERR(ft))
2049		return PTR_ERR(ft);
2050
2051	ft_prio->anchor.ft = ft;
2052
2053	return 0;
2054}
2055
2056static void steering_anchor_destroy_ft(struct mlx5_ib_flow_prio *ft_prio)
2057{
2058	if (ft_prio->anchor.ft) {
2059		mlx5_destroy_flow_table(ft_prio->anchor.ft);
2060		ft_prio->anchor.ft = NULL;
2061	}
2062}
2063
2064static int
2065steering_anchor_create_fg_drop(struct mlx5_ib_flow_prio *ft_prio)
2066{
2067	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2068	struct mlx5_flow_group *fg;
2069	void *flow_group_in;
2070	int err = 0;
2071
2072	if (ft_prio->anchor.fg_drop)
2073		return 0;
2074
2075	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2076	if (!flow_group_in)
2077		return -ENOMEM;
2078
2079	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 1);
2080	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 1);
2081
2082	fg = mlx5_create_flow_group(ft_prio->anchor.ft, flow_group_in);
2083	if (IS_ERR(fg)) {
2084		err = PTR_ERR(fg);
2085		goto out;
2086	}
2087
2088	ft_prio->anchor.fg_drop = fg;
2089
2090out:
2091	kvfree(flow_group_in);
2092
2093	return err;
2094}
2095
2096static void
2097steering_anchor_destroy_fg_drop(struct mlx5_ib_flow_prio *ft_prio)
2098{
2099	if (ft_prio->anchor.fg_drop) {
2100		mlx5_destroy_flow_group(ft_prio->anchor.fg_drop);
2101		ft_prio->anchor.fg_drop = NULL;
2102	}
2103}
2104
2105static int
2106steering_anchor_create_fg_goto_table(struct mlx5_ib_flow_prio *ft_prio)
2107{
2108	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2109	struct mlx5_flow_group *fg;
2110	void *flow_group_in;
2111	int err = 0;
2112
2113	if (ft_prio->anchor.fg_goto_table)
2114		return 0;
2115
2116	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2117	if (!flow_group_in)
2118		return -ENOMEM;
2119
2120	fg = mlx5_create_flow_group(ft_prio->anchor.ft, flow_group_in);
2121	if (IS_ERR(fg)) {
2122		err = PTR_ERR(fg);
2123		goto out;
2124	}
2125	ft_prio->anchor.fg_goto_table = fg;
2126
2127out:
2128	kvfree(flow_group_in);
2129
2130	return err;
2131}
2132
2133static void
2134steering_anchor_destroy_fg_goto_table(struct mlx5_ib_flow_prio *ft_prio)
2135{
2136	if (ft_prio->anchor.fg_goto_table) {
2137		mlx5_destroy_flow_group(ft_prio->anchor.fg_goto_table);
2138		ft_prio->anchor.fg_goto_table = NULL;
2139	}
2140}
2141
2142static int
2143steering_anchor_create_rule_drop(struct mlx5_ib_flow_prio *ft_prio)
2144{
2145	struct mlx5_flow_act flow_act = {};
2146	struct mlx5_flow_handle *handle;
2147
2148	if (ft_prio->anchor.rule_drop)
2149		return 0;
2150
2151	flow_act.fg = ft_prio->anchor.fg_drop;
2152	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2153
2154	handle = mlx5_add_flow_rules(ft_prio->anchor.ft, NULL, &flow_act,
2155				     NULL, 0);
2156	if (IS_ERR(handle))
2157		return PTR_ERR(handle);
2158
2159	ft_prio->anchor.rule_drop = handle;
2160
2161	return 0;
2162}
2163
2164static void steering_anchor_destroy_rule_drop(struct mlx5_ib_flow_prio *ft_prio)
2165{
2166	if (ft_prio->anchor.rule_drop) {
2167		mlx5_del_flow_rules(ft_prio->anchor.rule_drop);
2168		ft_prio->anchor.rule_drop = NULL;
2169	}
2170}
2171
2172static int
2173steering_anchor_create_rule_goto_table(struct mlx5_ib_flow_prio *ft_prio)
2174{
2175	struct mlx5_flow_destination dest = {};
2176	struct mlx5_flow_act flow_act = {};
2177	struct mlx5_flow_handle *handle;
2178
2179	if (ft_prio->anchor.rule_goto_table)
2180		return 0;
2181
2182	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2183	flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
2184	flow_act.fg = ft_prio->anchor.fg_goto_table;
2185
2186	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
2187	dest.ft = ft_prio->flow_table;
2188
2189	handle = mlx5_add_flow_rules(ft_prio->anchor.ft, NULL, &flow_act,
2190				     &dest, 1);
2191	if (IS_ERR(handle))
2192		return PTR_ERR(handle);
2193
2194	ft_prio->anchor.rule_goto_table = handle;
2195
2196	return 0;
2197}
2198
2199static void
2200steering_anchor_destroy_rule_goto_table(struct mlx5_ib_flow_prio *ft_prio)
2201{
2202	if (ft_prio->anchor.rule_goto_table) {
2203		mlx5_del_flow_rules(ft_prio->anchor.rule_goto_table);
2204		ft_prio->anchor.rule_goto_table = NULL;
2205	}
2206}
2207
2208static int steering_anchor_create_res(struct mlx5_ib_dev *dev,
2209				      struct mlx5_ib_flow_prio *ft_prio,
2210				      enum mlx5_flow_namespace_type ns_type)
2211{
2212	int err;
2213
2214	err = steering_anchor_create_ft(dev, ft_prio, ns_type);
2215	if (err)
2216		return err;
2217
2218	err = steering_anchor_create_fg_drop(ft_prio);
2219	if (err)
2220		goto destroy_ft;
2221
2222	err = steering_anchor_create_fg_goto_table(ft_prio);
2223	if (err)
2224		goto destroy_fg_drop;
2225
2226	err = steering_anchor_create_rule_drop(ft_prio);
2227	if (err)
2228		goto destroy_fg_goto_table;
2229
2230	err = steering_anchor_create_rule_goto_table(ft_prio);
2231	if (err)
2232		goto destroy_rule_drop;
2233
2234	return 0;
2235
2236destroy_rule_drop:
2237	steering_anchor_destroy_rule_drop(ft_prio);
2238destroy_fg_goto_table:
2239	steering_anchor_destroy_fg_goto_table(ft_prio);
2240destroy_fg_drop:
2241	steering_anchor_destroy_fg_drop(ft_prio);
2242destroy_ft:
2243	steering_anchor_destroy_ft(ft_prio);
2244
2245	return err;
2246}
2247
2248static void mlx5_steering_anchor_destroy_res(struct mlx5_ib_flow_prio *ft_prio)
2249{
2250	steering_anchor_destroy_rule_goto_table(ft_prio);
2251	steering_anchor_destroy_rule_drop(ft_prio);
2252	steering_anchor_destroy_fg_goto_table(ft_prio);
2253	steering_anchor_destroy_fg_drop(ft_prio);
2254	steering_anchor_destroy_ft(ft_prio);
2255}
2256
2257static int steering_anchor_cleanup(struct ib_uobject *uobject,
2258				   enum rdma_remove_reason why,
2259				   struct uverbs_attr_bundle *attrs)
2260{
2261	struct mlx5_ib_steering_anchor *obj = uobject->object;
2262
2263	if (atomic_read(&obj->usecnt))
2264		return -EBUSY;
2265
2266	mutex_lock(&obj->dev->flow_db->lock);
2267	if (!--obj->ft_prio->anchor.rule_goto_table_ref)
2268		steering_anchor_destroy_rule_goto_table(obj->ft_prio);
2269
2270	put_flow_table(obj->dev, obj->ft_prio, true);
2271	mutex_unlock(&obj->dev->flow_db->lock);
2272
2273	kfree(obj);
2274	return 0;
2275}
2276
2277static void fs_cleanup_anchor(struct mlx5_ib_flow_prio *prio,
2278			      int count)
2279{
2280	while (count--)
2281		mlx5_steering_anchor_destroy_res(&prio[count]);
2282}
2283
2284void mlx5_ib_fs_cleanup_anchor(struct mlx5_ib_dev *dev)
2285{
2286	fs_cleanup_anchor(dev->flow_db->prios, MLX5_IB_NUM_FLOW_FT);
2287	fs_cleanup_anchor(dev->flow_db->egress_prios, MLX5_IB_NUM_FLOW_FT);
2288	fs_cleanup_anchor(dev->flow_db->sniffer, MLX5_IB_NUM_SNIFFER_FTS);
2289	fs_cleanup_anchor(dev->flow_db->egress, MLX5_IB_NUM_EGRESS_FTS);
2290	fs_cleanup_anchor(dev->flow_db->fdb, MLX5_IB_NUM_FDB_FTS);
2291	fs_cleanup_anchor(dev->flow_db->rdma_rx, MLX5_IB_NUM_FLOW_FT);
2292	fs_cleanup_anchor(dev->flow_db->rdma_tx, MLX5_IB_NUM_FLOW_FT);
2293}
2294
2295static int mlx5_ib_matcher_ns(struct uverbs_attr_bundle *attrs,
2296			      struct mlx5_ib_flow_matcher *obj)
2297{
2298	enum mlx5_ib_uapi_flow_table_type ft_type =
2299		MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX;
2300	u32 flags;
2301	int err;
2302
2303	/* New users should use MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE and older
2304	 * users should switch to it. We leave this to not break userspace
2305	 */
2306	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE) &&
2307	    uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS))
2308		return -EINVAL;
2309
2310	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE)) {
2311		err = uverbs_get_const(&ft_type, attrs,
2312				       MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE);
2313		if (err)
2314			return err;
2315
2316		err = mlx5_ib_ft_type_to_namespace(ft_type, &obj->ns_type);
2317		if (err)
2318			return err;
2319
2320		return 0;
2321	}
2322
2323	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS)) {
2324		err = uverbs_get_flags32(&flags, attrs,
2325					 MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS,
2326					 IB_FLOW_ATTR_FLAGS_EGRESS);
2327		if (err)
2328			return err;
2329
2330		if (flags)
2331			return mlx5_ib_ft_type_to_namespace(
2332				MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX,
2333				&obj->ns_type);
 
 
2334	}
2335
2336	obj->ns_type = MLX5_FLOW_NAMESPACE_BYPASS;
2337
2338	return 0;
2339}
2340
2341static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_MATCHER_CREATE)(
2342	struct uverbs_attr_bundle *attrs)
2343{
2344	struct ib_uobject *uobj = uverbs_attr_get_uobject(
2345		attrs, MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE);
2346	struct mlx5_ib_dev *dev = mlx5_udata_to_mdev(&attrs->driver_udata);
2347	struct mlx5_ib_flow_matcher *obj;
2348	int err;
2349
2350	obj = kzalloc(sizeof(struct mlx5_ib_flow_matcher), GFP_KERNEL);
2351	if (!obj)
2352		return -ENOMEM;
2353
2354	obj->mask_len = uverbs_attr_get_len(
2355		attrs, MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
2356	err = uverbs_copy_from(&obj->matcher_mask,
2357			       attrs,
2358			       MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
2359	if (err)
2360		goto end;
2361
2362	obj->flow_type = uverbs_attr_get_enum_id(
2363		attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
2364
2365	if (obj->flow_type == MLX5_IB_FLOW_TYPE_NORMAL) {
2366		err = uverbs_copy_from(&obj->priority,
2367				       attrs,
2368				       MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
2369		if (err)
2370			goto end;
2371	}
2372
2373	err = uverbs_copy_from(&obj->match_criteria_enable,
2374			       attrs,
2375			       MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA);
2376	if (err)
2377		goto end;
2378
2379	err = mlx5_ib_matcher_ns(attrs, obj);
2380	if (err)
2381		goto end;
2382
2383	if (obj->ns_type == MLX5_FLOW_NAMESPACE_FDB_BYPASS &&
2384	    mlx5_eswitch_mode(dev->mdev) != MLX5_ESWITCH_OFFLOADS) {
2385		err = -EINVAL;
2386		goto end;
2387	}
2388
2389	uobj->object = obj;
2390	obj->mdev = dev->mdev;
2391	atomic_set(&obj->usecnt, 0);
2392	return 0;
2393
2394end:
2395	kfree(obj);
2396	return err;
2397}
2398
2399static int UVERBS_HANDLER(MLX5_IB_METHOD_STEERING_ANCHOR_CREATE)(
2400	struct uverbs_attr_bundle *attrs)
2401{
2402	struct ib_uobject *uobj = uverbs_attr_get_uobject(
2403		attrs, MLX5_IB_ATTR_STEERING_ANCHOR_CREATE_HANDLE);
2404	struct mlx5_ib_dev *dev = mlx5_udata_to_mdev(&attrs->driver_udata);
2405	enum mlx5_ib_uapi_flow_table_type ib_uapi_ft_type;
2406	enum mlx5_flow_namespace_type ns_type;
2407	struct mlx5_ib_steering_anchor *obj;
2408	struct mlx5_ib_flow_prio *ft_prio;
2409	u16 priority;
2410	u32 ft_id;
2411	int err;
2412
2413	if (!capable(CAP_NET_RAW))
2414		return -EPERM;
2415
2416	err = uverbs_get_const(&ib_uapi_ft_type, attrs,
2417			       MLX5_IB_ATTR_STEERING_ANCHOR_FT_TYPE);
2418	if (err)
2419		return err;
2420
2421	err = mlx5_ib_ft_type_to_namespace(ib_uapi_ft_type, &ns_type);
2422	if (err)
2423		return err;
2424
2425	err = uverbs_copy_from(&priority, attrs,
2426			       MLX5_IB_ATTR_STEERING_ANCHOR_PRIORITY);
2427	if (err)
2428		return err;
2429
2430	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2431	if (!obj)
2432		return -ENOMEM;
2433
2434	mutex_lock(&dev->flow_db->lock);
2435
2436	ft_prio = _get_flow_table(dev, priority, ns_type, 0);
2437	if (IS_ERR(ft_prio)) {
2438		err = PTR_ERR(ft_prio);
2439		goto free_obj;
2440	}
2441
2442	ft_prio->refcount++;
2443
2444	if (!ft_prio->anchor.rule_goto_table_ref) {
2445		err = steering_anchor_create_res(dev, ft_prio, ns_type);
2446		if (err)
2447			goto put_flow_table;
2448	}
2449
2450	ft_prio->anchor.rule_goto_table_ref++;
2451
2452	ft_id = mlx5_flow_table_id(ft_prio->anchor.ft);
2453
2454	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_STEERING_ANCHOR_FT_ID,
2455			     &ft_id, sizeof(ft_id));
2456	if (err)
2457		goto destroy_res;
2458
2459	mutex_unlock(&dev->flow_db->lock);
2460
2461	uobj->object = obj;
2462	obj->dev = dev;
2463	obj->ft_prio = ft_prio;
2464	atomic_set(&obj->usecnt, 0);
2465
2466	return 0;
2467
2468destroy_res:
2469	--ft_prio->anchor.rule_goto_table_ref;
2470	mlx5_steering_anchor_destroy_res(ft_prio);
2471put_flow_table:
2472	put_flow_table(dev, ft_prio, true);
2473free_obj:
2474	mutex_unlock(&dev->flow_db->lock);
2475	kfree(obj);
2476
2477	return err;
2478}
2479
2480static struct ib_flow_action *
2481mlx5_ib_create_modify_header(struct mlx5_ib_dev *dev,
2482			     enum mlx5_ib_uapi_flow_table_type ft_type,
2483			     u8 num_actions, void *in)
2484{
2485	enum mlx5_flow_namespace_type namespace;
2486	struct mlx5_ib_flow_action *maction;
2487	int ret;
2488
2489	ret = mlx5_ib_ft_type_to_namespace(ft_type, &namespace);
2490	if (ret)
2491		return ERR_PTR(-EINVAL);
2492
2493	maction = kzalloc(sizeof(*maction), GFP_KERNEL);
2494	if (!maction)
2495		return ERR_PTR(-ENOMEM);
2496
2497	maction->flow_action_raw.modify_hdr =
2498		mlx5_modify_header_alloc(dev->mdev, namespace, num_actions, in);
2499
2500	if (IS_ERR(maction->flow_action_raw.modify_hdr)) {
2501		ret = PTR_ERR(maction->flow_action_raw.modify_hdr);
2502		kfree(maction);
2503		return ERR_PTR(ret);
2504	}
2505	maction->flow_action_raw.sub_type =
2506		MLX5_IB_FLOW_ACTION_MODIFY_HEADER;
2507	maction->flow_action_raw.dev = dev;
2508
2509	return &maction->ib_action;
2510}
2511
2512static bool mlx5_ib_modify_header_supported(struct mlx5_ib_dev *dev)
2513{
2514	return MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2515					 max_modify_header_actions) ||
2516	       MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
2517					 max_modify_header_actions) ||
2518	       MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
2519					 max_modify_header_actions);
2520}
2521
2522static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER)(
2523	struct uverbs_attr_bundle *attrs)
2524{
2525	struct ib_uobject *uobj = uverbs_attr_get_uobject(
2526		attrs, MLX5_IB_ATTR_CREATE_MODIFY_HEADER_HANDLE);
2527	struct mlx5_ib_dev *mdev = mlx5_udata_to_mdev(&attrs->driver_udata);
2528	enum mlx5_ib_uapi_flow_table_type ft_type;
2529	struct ib_flow_action *action;
2530	int num_actions;
2531	void *in;
2532	int ret;
2533
2534	if (!mlx5_ib_modify_header_supported(mdev))
2535		return -EOPNOTSUPP;
2536
2537	in = uverbs_attr_get_alloced_ptr(attrs,
2538		MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM);
2539
2540	num_actions = uverbs_attr_ptr_get_array_size(
2541		attrs, MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM,
2542		MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto));
2543	if (num_actions < 0)
2544		return num_actions;
2545
2546	ret = uverbs_get_const(&ft_type, attrs,
2547			       MLX5_IB_ATTR_CREATE_MODIFY_HEADER_FT_TYPE);
2548	if (ret)
2549		return ret;
2550	action = mlx5_ib_create_modify_header(mdev, ft_type, num_actions, in);
2551	if (IS_ERR(action))
2552		return PTR_ERR(action);
2553
2554	uverbs_flow_action_fill_action(action, uobj, &mdev->ib_dev,
2555				       IB_FLOW_ACTION_UNSPECIFIED);
2556
2557	return 0;
2558}
2559
2560static bool mlx5_ib_flow_action_packet_reformat_valid(struct mlx5_ib_dev *ibdev,
2561						      u8 packet_reformat_type,
2562						      u8 ft_type)
2563{
2564	switch (packet_reformat_type) {
2565	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
2566		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX)
2567			return MLX5_CAP_FLOWTABLE(ibdev->mdev,
2568						  encap_general_header);
2569		break;
2570	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
2571		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX)
2572			return MLX5_CAP_FLOWTABLE_NIC_TX(ibdev->mdev,
2573				reformat_l2_to_l3_tunnel);
2574		break;
2575	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
2576		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX)
2577			return MLX5_CAP_FLOWTABLE_NIC_RX(ibdev->mdev,
2578				reformat_l3_tunnel_to_l2);
2579		break;
2580	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2:
2581		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX)
2582			return MLX5_CAP_FLOWTABLE_NIC_RX(ibdev->mdev, decap);
2583		break;
2584	default:
2585		break;
2586	}
2587
2588	return false;
2589}
2590
2591static int mlx5_ib_dv_to_prm_packet_reforamt_type(u8 dv_prt, u8 *prm_prt)
2592{
2593	switch (dv_prt) {
2594	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
2595		*prm_prt = MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL;
2596		break;
2597	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
2598		*prm_prt = MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
2599		break;
2600	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
2601		*prm_prt = MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
2602		break;
2603	default:
2604		return -EINVAL;
2605	}
2606
2607	return 0;
2608}
2609
2610static int mlx5_ib_flow_action_create_packet_reformat_ctx(
2611	struct mlx5_ib_dev *dev,
2612	struct mlx5_ib_flow_action *maction,
2613	u8 ft_type, u8 dv_prt,
2614	void *in, size_t len)
2615{
2616	struct mlx5_pkt_reformat_params reformat_params;
2617	enum mlx5_flow_namespace_type namespace;
2618	u8 prm_prt;
2619	int ret;
2620
2621	ret = mlx5_ib_ft_type_to_namespace(ft_type, &namespace);
2622	if (ret)
2623		return ret;
2624
2625	ret = mlx5_ib_dv_to_prm_packet_reforamt_type(dv_prt, &prm_prt);
2626	if (ret)
2627		return ret;
2628
2629	memset(&reformat_params, 0, sizeof(reformat_params));
2630	reformat_params.type = prm_prt;
2631	reformat_params.size = len;
2632	reformat_params.data = in;
2633	maction->flow_action_raw.pkt_reformat =
2634		mlx5_packet_reformat_alloc(dev->mdev, &reformat_params,
2635					   namespace);
2636	if (IS_ERR(maction->flow_action_raw.pkt_reformat)) {
2637		ret = PTR_ERR(maction->flow_action_raw.pkt_reformat);
2638		return ret;
2639	}
2640
2641	maction->flow_action_raw.sub_type =
2642		MLX5_IB_FLOW_ACTION_PACKET_REFORMAT;
2643	maction->flow_action_raw.dev = dev;
2644
2645	return 0;
2646}
2647
2648static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT)(
2649	struct uverbs_attr_bundle *attrs)
2650{
2651	struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs,
2652		MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_HANDLE);
2653	struct mlx5_ib_dev *mdev = mlx5_udata_to_mdev(&attrs->driver_udata);
2654	enum mlx5_ib_uapi_flow_action_packet_reformat_type dv_prt;
2655	enum mlx5_ib_uapi_flow_table_type ft_type;
2656	struct mlx5_ib_flow_action *maction;
2657	int ret;
2658
2659	ret = uverbs_get_const(&ft_type, attrs,
2660			       MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_FT_TYPE);
2661	if (ret)
2662		return ret;
2663
2664	ret = uverbs_get_const(&dv_prt, attrs,
2665			       MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_TYPE);
2666	if (ret)
2667		return ret;
2668
2669	if (!mlx5_ib_flow_action_packet_reformat_valid(mdev, dv_prt, ft_type))
2670		return -EOPNOTSUPP;
2671
2672	maction = kzalloc(sizeof(*maction), GFP_KERNEL);
2673	if (!maction)
2674		return -ENOMEM;
2675
2676	if (dv_prt ==
2677	    MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2) {
2678		maction->flow_action_raw.sub_type =
2679			MLX5_IB_FLOW_ACTION_DECAP;
2680		maction->flow_action_raw.dev = mdev;
2681	} else {
2682		void *in;
2683		int len;
2684
2685		in = uverbs_attr_get_alloced_ptr(attrs,
2686			MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF);
2687		if (IS_ERR(in)) {
2688			ret = PTR_ERR(in);
2689			goto free_maction;
2690		}
2691
2692		len = uverbs_attr_get_len(attrs,
2693			MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF);
2694
2695		ret = mlx5_ib_flow_action_create_packet_reformat_ctx(mdev,
2696			maction, ft_type, dv_prt, in, len);
2697		if (ret)
2698			goto free_maction;
2699	}
2700
2701	uverbs_flow_action_fill_action(&maction->ib_action, uobj, &mdev->ib_dev,
2702				       IB_FLOW_ACTION_UNSPECIFIED);
2703	return 0;
2704
2705free_maction:
2706	kfree(maction);
2707	return ret;
2708}
2709
2710DECLARE_UVERBS_NAMED_METHOD(
2711	MLX5_IB_METHOD_CREATE_FLOW,
2712	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
2713			UVERBS_OBJECT_FLOW,
2714			UVERBS_ACCESS_NEW,
2715			UA_MANDATORY),
2716	UVERBS_ATTR_PTR_IN(
2717		MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE,
2718		UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
2719		UA_MANDATORY,
2720		UA_ALLOC_AND_COPY),
2721	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_MATCHER,
2722			MLX5_IB_OBJECT_FLOW_MATCHER,
2723			UVERBS_ACCESS_READ,
2724			UA_MANDATORY),
2725	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_QP,
2726			UVERBS_OBJECT_QP,
2727			UVERBS_ACCESS_READ),
2728	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX,
2729			MLX5_IB_OBJECT_DEVX_OBJ,
2730			UVERBS_ACCESS_READ),
2731	UVERBS_ATTR_IDRS_ARR(MLX5_IB_ATTR_CREATE_FLOW_ARR_FLOW_ACTIONS,
2732			     UVERBS_OBJECT_FLOW_ACTION,
2733			     UVERBS_ACCESS_READ, 1,
2734			     MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS,
2735			     UA_OPTIONAL),
2736	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_TAG,
2737			   UVERBS_ATTR_TYPE(u32),
2738			   UA_OPTIONAL),
2739	UVERBS_ATTR_IDRS_ARR(MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX,
2740			     MLX5_IB_OBJECT_DEVX_OBJ,
2741			     UVERBS_ACCESS_READ, 1, 1,
2742			     UA_OPTIONAL),
2743	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET,
2744			   UVERBS_ATTR_MIN_SIZE(sizeof(u32)),
2745			   UA_OPTIONAL,
2746			   UA_ALLOC_AND_COPY),
2747	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_FLAGS,
2748			     enum mlx5_ib_create_flow_flags,
2749			     UA_OPTIONAL));
2750
2751DECLARE_UVERBS_NAMED_METHOD_DESTROY(
2752	MLX5_IB_METHOD_DESTROY_FLOW,
2753	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
2754			UVERBS_OBJECT_FLOW,
2755			UVERBS_ACCESS_DESTROY,
2756			UA_MANDATORY));
2757
2758ADD_UVERBS_METHODS(mlx5_ib_fs,
2759		   UVERBS_OBJECT_FLOW,
2760		   &UVERBS_METHOD(MLX5_IB_METHOD_CREATE_FLOW),
2761		   &UVERBS_METHOD(MLX5_IB_METHOD_DESTROY_FLOW));
2762
2763DECLARE_UVERBS_NAMED_METHOD(
2764	MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER,
2765	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_HANDLE,
2766			UVERBS_OBJECT_FLOW_ACTION,
2767			UVERBS_ACCESS_NEW,
2768			UA_MANDATORY),
2769	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM,
2770			   UVERBS_ATTR_MIN_SIZE(MLX5_UN_SZ_BYTES(
2771				   set_add_copy_action_in_auto)),
2772			   UA_MANDATORY,
2773			   UA_ALLOC_AND_COPY),
2774	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_FT_TYPE,
2775			     enum mlx5_ib_uapi_flow_table_type,
2776			     UA_MANDATORY));
2777
2778DECLARE_UVERBS_NAMED_METHOD(
2779	MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT,
2780	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_HANDLE,
2781			UVERBS_OBJECT_FLOW_ACTION,
2782			UVERBS_ACCESS_NEW,
2783			UA_MANDATORY),
2784	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF,
2785			   UVERBS_ATTR_MIN_SIZE(1),
2786			   UA_ALLOC_AND_COPY,
2787			   UA_OPTIONAL),
2788	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_TYPE,
2789			     enum mlx5_ib_uapi_flow_action_packet_reformat_type,
2790			     UA_MANDATORY),
2791	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_FT_TYPE,
2792			     enum mlx5_ib_uapi_flow_table_type,
2793			     UA_MANDATORY));
2794
2795ADD_UVERBS_METHODS(
2796	mlx5_ib_flow_actions,
2797	UVERBS_OBJECT_FLOW_ACTION,
2798	&UVERBS_METHOD(MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER),
2799	&UVERBS_METHOD(MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT));
2800
2801DECLARE_UVERBS_NAMED_METHOD(
2802	MLX5_IB_METHOD_FLOW_MATCHER_CREATE,
2803	UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE,
2804			MLX5_IB_OBJECT_FLOW_MATCHER,
2805			UVERBS_ACCESS_NEW,
2806			UA_MANDATORY),
2807	UVERBS_ATTR_PTR_IN(
2808		MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK,
2809		UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
2810		UA_MANDATORY),
2811	UVERBS_ATTR_ENUM_IN(MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE,
2812			    mlx5_ib_flow_type,
2813			    UA_MANDATORY),
2814	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA,
2815			   UVERBS_ATTR_TYPE(u8),
2816			   UA_MANDATORY),
2817	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS,
2818			     enum ib_flow_flags,
2819			     UA_OPTIONAL),
2820	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE,
2821			     enum mlx5_ib_uapi_flow_table_type,
2822			     UA_OPTIONAL));
2823
2824DECLARE_UVERBS_NAMED_METHOD_DESTROY(
2825	MLX5_IB_METHOD_FLOW_MATCHER_DESTROY,
2826	UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_DESTROY_HANDLE,
2827			MLX5_IB_OBJECT_FLOW_MATCHER,
2828			UVERBS_ACCESS_DESTROY,
2829			UA_MANDATORY));
2830
2831DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_FLOW_MATCHER,
2832			    UVERBS_TYPE_ALLOC_IDR(flow_matcher_cleanup),
2833			    &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_CREATE),
2834			    &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_DESTROY));
2835
2836DECLARE_UVERBS_NAMED_METHOD(
2837	MLX5_IB_METHOD_STEERING_ANCHOR_CREATE,
2838	UVERBS_ATTR_IDR(MLX5_IB_ATTR_STEERING_ANCHOR_CREATE_HANDLE,
2839			MLX5_IB_OBJECT_STEERING_ANCHOR,
2840			UVERBS_ACCESS_NEW,
2841			UA_MANDATORY),
2842	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_STEERING_ANCHOR_FT_TYPE,
2843			     enum mlx5_ib_uapi_flow_table_type,
2844			     UA_MANDATORY),
2845	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_STEERING_ANCHOR_PRIORITY,
2846			   UVERBS_ATTR_TYPE(u16),
2847			   UA_MANDATORY),
2848	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_STEERING_ANCHOR_FT_ID,
2849			   UVERBS_ATTR_TYPE(u32),
2850			   UA_MANDATORY));
2851
2852DECLARE_UVERBS_NAMED_METHOD_DESTROY(
2853	MLX5_IB_METHOD_STEERING_ANCHOR_DESTROY,
2854	UVERBS_ATTR_IDR(MLX5_IB_ATTR_STEERING_ANCHOR_DESTROY_HANDLE,
2855			MLX5_IB_OBJECT_STEERING_ANCHOR,
2856			UVERBS_ACCESS_DESTROY,
2857			UA_MANDATORY));
2858
2859DECLARE_UVERBS_NAMED_OBJECT(
2860	MLX5_IB_OBJECT_STEERING_ANCHOR,
2861	UVERBS_TYPE_ALLOC_IDR(steering_anchor_cleanup),
2862	&UVERBS_METHOD(MLX5_IB_METHOD_STEERING_ANCHOR_CREATE),
2863	&UVERBS_METHOD(MLX5_IB_METHOD_STEERING_ANCHOR_DESTROY));
2864
2865const struct uapi_definition mlx5_ib_flow_defs[] = {
2866	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(
2867		MLX5_IB_OBJECT_FLOW_MATCHER),
2868	UAPI_DEF_CHAIN_OBJ_TREE(
2869		UVERBS_OBJECT_FLOW,
2870		&mlx5_ib_fs),
2871	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
2872				&mlx5_ib_flow_actions),
2873	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(
2874		MLX5_IB_OBJECT_STEERING_ANCHOR,
2875		UAPI_DEF_IS_OBJ_SUPPORTED(mlx5_ib_shared_ft_allowed)),
2876	{},
2877};
2878
2879static const struct ib_device_ops flow_ops = {
2880	.create_flow = mlx5_ib_create_flow,
2881	.destroy_flow = mlx5_ib_destroy_flow,
2882	.destroy_flow_action = mlx5_ib_destroy_flow_action,
2883};
2884
 
 
 
 
 
2885int mlx5_ib_fs_init(struct mlx5_ib_dev *dev)
2886{
2887	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
2888
2889	if (!dev->flow_db)
2890		return -ENOMEM;
2891
2892	mutex_init(&dev->flow_db->lock);
2893
2894	ib_set_device_ops(&dev->ib_dev, &flow_ops);
 
 
 
 
2895	return 0;
2896}
v5.9
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
   4 */
   5
   6#include <rdma/ib_user_verbs.h>
   7#include <rdma/ib_verbs.h>
   8#include <rdma/uverbs_types.h>
   9#include <rdma/uverbs_ioctl.h>
  10#include <rdma/uverbs_std_types.h>
  11#include <rdma/mlx5_user_ioctl_cmds.h>
  12#include <rdma/mlx5_user_ioctl_verbs.h>
 
  13#include <rdma/ib_umem.h>
  14#include <linux/mlx5/driver.h>
  15#include <linux/mlx5/fs.h>
  16#include <linux/mlx5/fs_helpers.h>
  17#include <linux/mlx5/accel.h>
  18#include <linux/mlx5/eswitch.h>
 
  19#include "mlx5_ib.h"
  20#include "counters.h"
  21#include "devx.h"
  22#include "fs.h"
  23
  24#define UVERBS_MODULE_NAME mlx5_ib
  25#include <rdma/uverbs_named_ioctl.h>
  26
  27enum {
  28	MATCH_CRITERIA_ENABLE_OUTER_BIT,
  29	MATCH_CRITERIA_ENABLE_MISC_BIT,
  30	MATCH_CRITERIA_ENABLE_INNER_BIT,
  31	MATCH_CRITERIA_ENABLE_MISC2_BIT
  32};
  33
  34#define HEADER_IS_ZERO(match_criteria, headers)			           \
  35	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
  36		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
  37
  38static u8 get_match_criteria_enable(u32 *match_criteria)
  39{
  40	u8 match_criteria_enable;
  41
  42	match_criteria_enable =
  43		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
  44		MATCH_CRITERIA_ENABLE_OUTER_BIT;
  45	match_criteria_enable |=
  46		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
  47		MATCH_CRITERIA_ENABLE_MISC_BIT;
  48	match_criteria_enable |=
  49		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
  50		MATCH_CRITERIA_ENABLE_INNER_BIT;
  51	match_criteria_enable |=
  52		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
  53		MATCH_CRITERIA_ENABLE_MISC2_BIT;
  54
  55	return match_criteria_enable;
  56}
  57
  58static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
  59{
  60	u8 entry_mask;
  61	u8 entry_val;
  62	int err = 0;
  63
  64	if (!mask)
  65		goto out;
  66
  67	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
  68			      ip_protocol);
  69	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
  70			     ip_protocol);
  71	if (!entry_mask) {
  72		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
  73		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
  74		goto out;
  75	}
  76	/* Don't override existing ip protocol */
  77	if (mask != entry_mask || val != entry_val)
  78		err = -EINVAL;
  79out:
  80	return err;
  81}
  82
  83static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
  84			   bool inner)
  85{
  86	if (inner) {
  87		MLX5_SET(fte_match_set_misc,
  88			 misc_c, inner_ipv6_flow_label, mask);
  89		MLX5_SET(fte_match_set_misc,
  90			 misc_v, inner_ipv6_flow_label, val);
  91	} else {
  92		MLX5_SET(fte_match_set_misc,
  93			 misc_c, outer_ipv6_flow_label, mask);
  94		MLX5_SET(fte_match_set_misc,
  95			 misc_v, outer_ipv6_flow_label, val);
  96	}
  97}
  98
  99static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
 100{
 101	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
 102	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
 103	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
 104	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
 105}
 106
 107static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
 108{
 109	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
 110	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
 111		return -EOPNOTSUPP;
 112
 113	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
 114	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
 115		return -EOPNOTSUPP;
 116
 117	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
 118	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
 119		return -EOPNOTSUPP;
 120
 121	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
 122	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
 123		return -EOPNOTSUPP;
 124
 125	return 0;
 126}
 127
 128#define LAST_ETH_FIELD vlan_tag
 129#define LAST_IB_FIELD sl
 130#define LAST_IPV4_FIELD tos
 131#define LAST_IPV6_FIELD traffic_class
 132#define LAST_TCP_UDP_FIELD src_port
 133#define LAST_TUNNEL_FIELD tunnel_id
 134#define LAST_FLOW_TAG_FIELD tag_id
 135#define LAST_DROP_FIELD size
 136#define LAST_COUNTERS_FIELD counters
 137
 138/* Field is the last supported field */
 139#define FIELDS_NOT_SUPPORTED(filter, field)\
 140	memchr_inv((void *)&filter.field  +\
 141		   sizeof(filter.field), 0,\
 142		   sizeof(filter) -\
 143		   offsetof(typeof(filter), field) -\
 144		   sizeof(filter.field))
 145
 146int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
 147			   bool is_egress,
 148			   struct mlx5_flow_act *action)
 149{
 150
 151	switch (maction->ib_action.type) {
 152	case IB_FLOW_ACTION_ESP:
 153		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
 154				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
 155			return -EINVAL;
 156		/* Currently only AES_GCM keymat is supported by the driver */
 157		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
 158		action->action |= is_egress ?
 159			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
 160			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
 161		return 0;
 162	case IB_FLOW_ACTION_UNSPECIFIED:
 163		if (maction->flow_action_raw.sub_type ==
 164		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
 165			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
 166				return -EINVAL;
 167			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
 168			action->modify_hdr =
 169				maction->flow_action_raw.modify_hdr;
 170			return 0;
 171		}
 172		if (maction->flow_action_raw.sub_type ==
 173		    MLX5_IB_FLOW_ACTION_DECAP) {
 174			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
 175				return -EINVAL;
 176			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
 177			return 0;
 178		}
 179		if (maction->flow_action_raw.sub_type ==
 180		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
 181			if (action->action &
 182			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
 183				return -EINVAL;
 184			action->action |=
 185				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
 186			action->pkt_reformat =
 187				maction->flow_action_raw.pkt_reformat;
 188			return 0;
 189		}
 190		fallthrough;
 191	default:
 192		return -EOPNOTSUPP;
 193	}
 194}
 195
 196static int parse_flow_attr(struct mlx5_core_dev *mdev,
 197			   struct mlx5_flow_spec *spec,
 198			   const union ib_flow_spec *ib_spec,
 199			   const struct ib_flow_attr *flow_attr,
 200			   struct mlx5_flow_act *action, u32 prev_type)
 201{
 202	struct mlx5_flow_context *flow_context = &spec->flow_context;
 203	u32 *match_c = spec->match_criteria;
 204	u32 *match_v = spec->match_value;
 205	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
 206					   misc_parameters);
 207	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
 208					   misc_parameters);
 209	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
 210					    misc_parameters_2);
 211	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
 212					    misc_parameters_2);
 213	void *headers_c;
 214	void *headers_v;
 215	int match_ipv;
 216	int ret;
 217
 218	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
 219		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
 220					 inner_headers);
 221		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
 222					 inner_headers);
 223		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 224					ft_field_support.inner_ip_version);
 225	} else {
 226		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
 227					 outer_headers);
 228		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
 229					 outer_headers);
 230		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 231					ft_field_support.outer_ip_version);
 232	}
 233
 234	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
 235	case IB_FLOW_SPEC_ETH:
 236		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
 237			return -EOPNOTSUPP;
 238
 239		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 240					     dmac_47_16),
 241				ib_spec->eth.mask.dst_mac);
 242		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 243					     dmac_47_16),
 244				ib_spec->eth.val.dst_mac);
 245
 246		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 247					     smac_47_16),
 248				ib_spec->eth.mask.src_mac);
 249		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 250					     smac_47_16),
 251				ib_spec->eth.val.src_mac);
 252
 253		if (ib_spec->eth.mask.vlan_tag) {
 254			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 255				 cvlan_tag, 1);
 256			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 257				 cvlan_tag, 1);
 258
 259			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 260				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
 261			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 262				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
 263
 264			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 265				 first_cfi,
 266				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
 267			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 268				 first_cfi,
 269				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
 270
 271			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 272				 first_prio,
 273				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
 274			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 275				 first_prio,
 276				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
 277		}
 278		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 279			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
 280		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 281			 ethertype, ntohs(ib_spec->eth.val.ether_type));
 282		break;
 283	case IB_FLOW_SPEC_IPV4:
 284		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
 285			return -EOPNOTSUPP;
 286
 287		if (match_ipv) {
 288			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 289				 ip_version, 0xf);
 290			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 291				 ip_version, MLX5_FS_IPV4_VERSION);
 292		} else {
 293			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 294				 ethertype, 0xffff);
 295			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 296				 ethertype, ETH_P_IP);
 297		}
 298
 299		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 300				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
 301		       &ib_spec->ipv4.mask.src_ip,
 302		       sizeof(ib_spec->ipv4.mask.src_ip));
 303		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 304				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
 305		       &ib_spec->ipv4.val.src_ip,
 306		       sizeof(ib_spec->ipv4.val.src_ip));
 307		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 308				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
 309		       &ib_spec->ipv4.mask.dst_ip,
 310		       sizeof(ib_spec->ipv4.mask.dst_ip));
 311		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 312				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
 313		       &ib_spec->ipv4.val.dst_ip,
 314		       sizeof(ib_spec->ipv4.val.dst_ip));
 315
 316		set_tos(headers_c, headers_v,
 317			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
 318
 319		if (set_proto(headers_c, headers_v,
 320			      ib_spec->ipv4.mask.proto,
 321			      ib_spec->ipv4.val.proto))
 322			return -EINVAL;
 323		break;
 324	case IB_FLOW_SPEC_IPV6:
 325		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
 326			return -EOPNOTSUPP;
 327
 328		if (match_ipv) {
 329			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 330				 ip_version, 0xf);
 331			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 332				 ip_version, MLX5_FS_IPV6_VERSION);
 333		} else {
 334			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
 335				 ethertype, 0xffff);
 336			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
 337				 ethertype, ETH_P_IPV6);
 338		}
 339
 340		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 341				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
 342		       &ib_spec->ipv6.mask.src_ip,
 343		       sizeof(ib_spec->ipv6.mask.src_ip));
 344		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 345				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
 346		       &ib_spec->ipv6.val.src_ip,
 347		       sizeof(ib_spec->ipv6.val.src_ip));
 348		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
 349				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
 350		       &ib_spec->ipv6.mask.dst_ip,
 351		       sizeof(ib_spec->ipv6.mask.dst_ip));
 352		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
 353				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
 354		       &ib_spec->ipv6.val.dst_ip,
 355		       sizeof(ib_spec->ipv6.val.dst_ip));
 356
 357		set_tos(headers_c, headers_v,
 358			ib_spec->ipv6.mask.traffic_class,
 359			ib_spec->ipv6.val.traffic_class);
 360
 361		if (set_proto(headers_c, headers_v,
 362			      ib_spec->ipv6.mask.next_hdr,
 363			      ib_spec->ipv6.val.next_hdr))
 364			return -EINVAL;
 365
 366		set_flow_label(misc_params_c, misc_params_v,
 367			       ntohl(ib_spec->ipv6.mask.flow_label),
 368			       ntohl(ib_spec->ipv6.val.flow_label),
 369			       ib_spec->type & IB_FLOW_SPEC_INNER);
 370		break;
 371	case IB_FLOW_SPEC_ESP:
 372		if (ib_spec->esp.mask.seq)
 373			return -EOPNOTSUPP;
 374
 375		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
 376			 ntohl(ib_spec->esp.mask.spi));
 377		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
 378			 ntohl(ib_spec->esp.val.spi));
 379		break;
 380	case IB_FLOW_SPEC_TCP:
 381		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
 382					 LAST_TCP_UDP_FIELD))
 383			return -EOPNOTSUPP;
 384
 385		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
 386			return -EINVAL;
 387
 388		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
 389			 ntohs(ib_spec->tcp_udp.mask.src_port));
 390		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
 391			 ntohs(ib_spec->tcp_udp.val.src_port));
 392
 393		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
 394			 ntohs(ib_spec->tcp_udp.mask.dst_port));
 395		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
 396			 ntohs(ib_spec->tcp_udp.val.dst_port));
 397		break;
 398	case IB_FLOW_SPEC_UDP:
 399		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
 400					 LAST_TCP_UDP_FIELD))
 401			return -EOPNOTSUPP;
 402
 403		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
 404			return -EINVAL;
 405
 406		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
 407			 ntohs(ib_spec->tcp_udp.mask.src_port));
 408		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
 409			 ntohs(ib_spec->tcp_udp.val.src_port));
 410
 411		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
 412			 ntohs(ib_spec->tcp_udp.mask.dst_port));
 413		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
 414			 ntohs(ib_spec->tcp_udp.val.dst_port));
 415		break;
 416	case IB_FLOW_SPEC_GRE:
 417		if (ib_spec->gre.mask.c_ks_res0_ver)
 418			return -EOPNOTSUPP;
 419
 420		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
 421			return -EINVAL;
 422
 423		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
 424			 0xff);
 425		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
 426			 IPPROTO_GRE);
 427
 428		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
 429			 ntohs(ib_spec->gre.mask.protocol));
 430		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
 431			 ntohs(ib_spec->gre.val.protocol));
 432
 433		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
 434				    gre_key.nvgre.hi),
 435		       &ib_spec->gre.mask.key,
 436		       sizeof(ib_spec->gre.mask.key));
 437		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
 438				    gre_key.nvgre.hi),
 439		       &ib_spec->gre.val.key,
 440		       sizeof(ib_spec->gre.val.key));
 441		break;
 442	case IB_FLOW_SPEC_MPLS:
 443		switch (prev_type) {
 444		case IB_FLOW_SPEC_UDP:
 445			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 446						   ft_field_support.outer_first_mpls_over_udp),
 447						   &ib_spec->mpls.mask.tag))
 448				return -EOPNOTSUPP;
 449
 450			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 451					    outer_first_mpls_over_udp),
 452			       &ib_spec->mpls.val.tag,
 453			       sizeof(ib_spec->mpls.val.tag));
 454			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 455					    outer_first_mpls_over_udp),
 456			       &ib_spec->mpls.mask.tag,
 457			       sizeof(ib_spec->mpls.mask.tag));
 458			break;
 459		case IB_FLOW_SPEC_GRE:
 460			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 461						   ft_field_support.outer_first_mpls_over_gre),
 462						   &ib_spec->mpls.mask.tag))
 463				return -EOPNOTSUPP;
 464
 465			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 466					    outer_first_mpls_over_gre),
 467			       &ib_spec->mpls.val.tag,
 468			       sizeof(ib_spec->mpls.val.tag));
 469			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 470					    outer_first_mpls_over_gre),
 471			       &ib_spec->mpls.mask.tag,
 472			       sizeof(ib_spec->mpls.mask.tag));
 473			break;
 474		default:
 475			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
 476				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 477							   ft_field_support.inner_first_mpls),
 478							   &ib_spec->mpls.mask.tag))
 479					return -EOPNOTSUPP;
 480
 481				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 482						    inner_first_mpls),
 483				       &ib_spec->mpls.val.tag,
 484				       sizeof(ib_spec->mpls.val.tag));
 485				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 486						    inner_first_mpls),
 487				       &ib_spec->mpls.mask.tag,
 488				       sizeof(ib_spec->mpls.mask.tag));
 489			} else {
 490				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 491							   ft_field_support.outer_first_mpls),
 492							   &ib_spec->mpls.mask.tag))
 493					return -EOPNOTSUPP;
 494
 495				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
 496						    outer_first_mpls),
 497				       &ib_spec->mpls.val.tag,
 498				       sizeof(ib_spec->mpls.val.tag));
 499				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
 500						    outer_first_mpls),
 501				       &ib_spec->mpls.mask.tag,
 502				       sizeof(ib_spec->mpls.mask.tag));
 503			}
 504		}
 505		break;
 506	case IB_FLOW_SPEC_VXLAN_TUNNEL:
 507		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
 508					 LAST_TUNNEL_FIELD))
 509			return -EOPNOTSUPP;
 510
 511		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
 512			 ntohl(ib_spec->tunnel.mask.tunnel_id));
 513		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
 514			 ntohl(ib_spec->tunnel.val.tunnel_id));
 515		break;
 516	case IB_FLOW_SPEC_ACTION_TAG:
 517		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
 518					 LAST_FLOW_TAG_FIELD))
 519			return -EOPNOTSUPP;
 520		if (ib_spec->flow_tag.tag_id >= BIT(24))
 521			return -EINVAL;
 522
 523		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
 524		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
 525		break;
 526	case IB_FLOW_SPEC_ACTION_DROP:
 527		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
 528					 LAST_DROP_FIELD))
 529			return -EOPNOTSUPP;
 530		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
 531		break;
 532	case IB_FLOW_SPEC_ACTION_HANDLE:
 533		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
 534			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
 535		if (ret)
 536			return ret;
 537		break;
 538	case IB_FLOW_SPEC_ACTION_COUNT:
 539		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
 540					 LAST_COUNTERS_FIELD))
 541			return -EOPNOTSUPP;
 542
 543		/* for now support only one counters spec per flow */
 544		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
 545			return -EINVAL;
 546
 547		action->counters = ib_spec->flow_count.counters;
 548		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
 549		break;
 550	default:
 551		return -EINVAL;
 552	}
 553
 554	return 0;
 555}
 556
 557/* If a flow could catch both multicast and unicast packets,
 558 * it won't fall into the multicast flow steering table and this rule
 559 * could steal other multicast packets.
 560 */
 561static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
 562{
 563	union ib_flow_spec *flow_spec;
 564
 565	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
 566	    ib_attr->num_of_specs < 1)
 567		return false;
 568
 569	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
 570	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
 571		struct ib_flow_spec_ipv4 *ipv4_spec;
 572
 573		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
 574		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
 575			return true;
 576
 577		return false;
 578	}
 579
 580	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
 581		struct ib_flow_spec_eth *eth_spec;
 582
 583		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
 584		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
 585		       is_multicast_ether_addr(eth_spec->val.dst_mac);
 586	}
 587
 588	return false;
 589}
 590
 591enum valid_spec {
 592	VALID_SPEC_INVALID,
 593	VALID_SPEC_VALID,
 594	VALID_SPEC_NA,
 595};
 596
 597static enum valid_spec
 598is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
 599		     const struct mlx5_flow_spec *spec,
 600		     const struct mlx5_flow_act *flow_act,
 601		     bool egress)
 602{
 603	const u32 *match_c = spec->match_criteria;
 604	bool is_crypto =
 605		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
 606				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
 607	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
 608	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
 609
 610	/*
 611	 * Currently only crypto is supported in egress, when regular egress
 612	 * rules would be supported, always return VALID_SPEC_NA.
 613	 */
 614	if (!is_crypto)
 615		return VALID_SPEC_NA;
 616
 617	return is_crypto && is_ipsec &&
 618		(!egress || (!is_drop &&
 619			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
 620		VALID_SPEC_VALID : VALID_SPEC_INVALID;
 621}
 622
 623static bool is_valid_spec(struct mlx5_core_dev *mdev,
 624			  const struct mlx5_flow_spec *spec,
 625			  const struct mlx5_flow_act *flow_act,
 626			  bool egress)
 627{
 628	/* We curretly only support ipsec egress flow */
 629	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
 630}
 631
 632static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
 633			       const struct ib_flow_attr *flow_attr,
 634			       bool check_inner)
 635{
 636	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
 637	int match_ipv = check_inner ?
 638			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 639					ft_field_support.inner_ip_version) :
 640			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
 641					ft_field_support.outer_ip_version);
 642	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
 643	bool ipv4_spec_valid, ipv6_spec_valid;
 644	unsigned int ip_spec_type = 0;
 645	bool has_ethertype = false;
 646	unsigned int spec_index;
 647	bool mask_valid = true;
 648	u16 eth_type = 0;
 649	bool type_valid;
 650
 651	/* Validate that ethertype is correct */
 652	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
 653		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
 654		    ib_spec->eth.mask.ether_type) {
 655			mask_valid = (ib_spec->eth.mask.ether_type ==
 656				      htons(0xffff));
 657			has_ethertype = true;
 658			eth_type = ntohs(ib_spec->eth.val.ether_type);
 659		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
 660			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
 661			ip_spec_type = ib_spec->type;
 662		}
 663		ib_spec = (void *)ib_spec + ib_spec->size;
 664	}
 665
 666	type_valid = (!has_ethertype) || (!ip_spec_type);
 667	if (!type_valid && mask_valid) {
 668		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
 669			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
 670		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
 671			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
 672
 673		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
 674			     (((eth_type == ETH_P_MPLS_UC) ||
 675			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
 676	}
 677
 678	return type_valid;
 679}
 680
 681static bool is_valid_attr(struct mlx5_core_dev *mdev,
 682			  const struct ib_flow_attr *flow_attr)
 683{
 684	return is_valid_ethertype(mdev, flow_attr, false) &&
 685	       is_valid_ethertype(mdev, flow_attr, true);
 686}
 687
 688static void put_flow_table(struct mlx5_ib_dev *dev,
 689			   struct mlx5_ib_flow_prio *prio, bool ft_added)
 690{
 691	prio->refcount -= !!ft_added;
 692	if (!prio->refcount) {
 693		mlx5_destroy_flow_table(prio->flow_table);
 694		prio->flow_table = NULL;
 695	}
 696}
 697
 698static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
 699{
 700	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
 701							  struct mlx5_ib_flow_handler,
 702							  ibflow);
 703	struct mlx5_ib_flow_handler *iter, *tmp;
 704	struct mlx5_ib_dev *dev = handler->dev;
 705
 706	mutex_lock(&dev->flow_db->lock);
 707
 708	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
 709		mlx5_del_flow_rules(iter->rule);
 710		put_flow_table(dev, iter->prio, true);
 711		list_del(&iter->list);
 712		kfree(iter);
 713	}
 714
 715	mlx5_del_flow_rules(handler->rule);
 716	put_flow_table(dev, handler->prio, true);
 717	mlx5_ib_counters_clear_description(handler->ibcounters);
 718	mutex_unlock(&dev->flow_db->lock);
 719	if (handler->flow_matcher)
 720		atomic_dec(&handler->flow_matcher->usecnt);
 721	kfree(handler);
 722
 723	return 0;
 724}
 725
 726static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
 727{
 728	priority *= 2;
 729	if (!dont_trap)
 730		priority++;
 731	return priority;
 732}
 733
 734enum flow_table_type {
 735	MLX5_IB_FT_RX,
 736	MLX5_IB_FT_TX
 737};
 738
 739#define MLX5_FS_MAX_TYPES	 6
 740#define MLX5_FS_MAX_ENTRIES	 BIT(16)
 741
 742static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
 
 
 
 
 
 
 
 
 743					   struct mlx5_ib_flow_prio *prio,
 744					   int priority,
 745					   int num_entries, int num_groups,
 746					   u32 flags)
 747{
 748	struct mlx5_flow_table_attr ft_attr = {};
 749	struct mlx5_flow_table *ft;
 750
 751	ft_attr.prio = priority;
 752	ft_attr.max_fte = num_entries;
 753	ft_attr.flags = flags;
 754	ft_attr.autogroup.max_num_groups = num_groups;
 755	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
 756	if (IS_ERR(ft))
 757		return ERR_CAST(ft);
 758
 759	prio->flow_table = ft;
 760	prio->refcount = 0;
 761	return prio;
 762}
 763
 764static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
 765						struct ib_flow_attr *flow_attr,
 766						enum flow_table_type ft_type)
 767{
 768	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
 769	struct mlx5_flow_namespace *ns = NULL;
 
 770	struct mlx5_ib_flow_prio *prio;
 771	struct mlx5_flow_table *ft;
 772	int max_table_size;
 773	int num_entries;
 774	int num_groups;
 775	bool esw_encap;
 776	u32 flags = 0;
 777	int priority;
 778
 779	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 780						       log_max_ft_size));
 781	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
 782		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
 783	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
 784		enum mlx5_flow_namespace_type fn_type;
 785
 786		if (flow_is_multicast_only(flow_attr) &&
 787		    !dont_trap)
 788			priority = MLX5_IB_FLOW_MCAST_PRIO;
 789		else
 790			priority = ib_prio_to_core_prio(flow_attr->priority,
 791							dont_trap);
 792		if (ft_type == MLX5_IB_FT_RX) {
 793			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
 794			prio = &dev->flow_db->prios[priority];
 795			if (!dev->is_rep && !esw_encap &&
 796			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
 797				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
 798			if (!dev->is_rep && !esw_encap &&
 799			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 800					reformat_l3_tunnel_to_l2))
 801				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
 802		} else {
 803			max_table_size =
 804				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
 805							      log_max_ft_size));
 806			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
 807			prio = &dev->flow_db->egress_prios[priority];
 808			if (!dev->is_rep && !esw_encap &&
 809			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
 810				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
 811		}
 812		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
 813		num_entries = MLX5_FS_MAX_ENTRIES;
 814		num_groups = MLX5_FS_MAX_TYPES;
 815	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
 816		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
 
 817		ns = mlx5_get_flow_namespace(dev->mdev,
 818					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
 819		build_leftovers_ft_param(&priority,
 820					 &num_entries,
 821					 &num_groups);
 822		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
 823	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
 
 824		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
 825					allow_sniffer_and_nic_rx_shared_tir))
 826			return ERR_PTR(-EOPNOTSUPP);
 827
 828		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
 829					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
 830					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
 
 831
 832		prio = &dev->flow_db->sniffer[ft_type];
 833		priority = 0;
 834		num_entries = 1;
 835		num_groups = 1;
 
 
 
 836	}
 837
 838	if (!ns)
 839		return ERR_PTR(-EOPNOTSUPP);
 840
 841	max_table_size = min_t(int, num_entries, max_table_size);
 842
 843	ft = prio->flow_table;
 844	if (!ft)
 845		return _get_prio(ns, prio, priority, max_table_size, num_groups,
 846				 flags);
 847
 848	return prio;
 849}
 850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 851static void set_underlay_qp(struct mlx5_ib_dev *dev,
 852			    struct mlx5_flow_spec *spec,
 853			    u32 underlay_qpn)
 854{
 855	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
 856					   spec->match_criteria,
 857					   misc_parameters);
 858	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
 859					   misc_parameters);
 860
 861	if (underlay_qpn &&
 862	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
 863				      ft_field_support.bth_dst_qp)) {
 864		MLX5_SET(fte_match_set_misc,
 865			 misc_params_v, bth_dst_qp, underlay_qpn);
 866		MLX5_SET(fte_match_set_misc,
 867			 misc_params_c, bth_dst_qp, 0xffffff);
 868	}
 869}
 870
 871static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
 872					 struct mlx5_flow_spec *spec,
 873					 struct mlx5_eswitch_rep *rep)
 874{
 875	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
 876	void *misc;
 877
 878	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
 879		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
 880				    misc_parameters_2);
 881
 882		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
 883			 mlx5_eswitch_get_vport_metadata_for_match(esw,
 884								   rep->vport));
 885		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
 886				    misc_parameters_2);
 887
 888		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
 889			 mlx5_eswitch_get_vport_metadata_mask());
 890	} else {
 891		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
 892				    misc_parameters);
 893
 894		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
 895
 896		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
 897				    misc_parameters);
 898
 899		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
 900	}
 901}
 902
 903static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
 904						      struct mlx5_ib_flow_prio *ft_prio,
 905						      const struct ib_flow_attr *flow_attr,
 906						      struct mlx5_flow_destination *dst,
 907						      u32 underlay_qpn,
 908						      struct mlx5_ib_create_flow *ucmd)
 909{
 910	struct mlx5_flow_table	*ft = ft_prio->flow_table;
 911	struct mlx5_ib_flow_handler *handler;
 912	struct mlx5_flow_act flow_act = {};
 913	struct mlx5_flow_spec *spec;
 914	struct mlx5_flow_destination dest_arr[2] = {};
 915	struct mlx5_flow_destination *rule_dst = dest_arr;
 916	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
 917	unsigned int spec_index;
 918	u32 prev_type = 0;
 919	int err = 0;
 920	int dest_num = 0;
 921	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
 922
 923	if (!is_valid_attr(dev->mdev, flow_attr))
 924		return ERR_PTR(-EINVAL);
 925
 926	if (dev->is_rep && is_egress)
 927		return ERR_PTR(-EINVAL);
 928
 929	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
 930	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
 931	if (!handler || !spec) {
 932		err = -ENOMEM;
 933		goto free;
 934	}
 935
 936	INIT_LIST_HEAD(&handler->list);
 937
 938	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
 939		err = parse_flow_attr(dev->mdev, spec,
 940				      ib_flow, flow_attr, &flow_act,
 941				      prev_type);
 942		if (err < 0)
 943			goto free;
 944
 945		prev_type = ((union ib_flow_spec *)ib_flow)->type;
 946		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
 947	}
 948
 949	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
 950		memcpy(&dest_arr[0], dst, sizeof(*dst));
 951		dest_num++;
 952	}
 953
 954	if (!flow_is_multicast_only(flow_attr))
 955		set_underlay_qp(dev, spec, underlay_qpn);
 956
 957	if (dev->is_rep) {
 958		struct mlx5_eswitch_rep *rep;
 959
 960		rep = dev->port[flow_attr->port - 1].rep;
 961		if (!rep) {
 962			err = -EINVAL;
 963			goto free;
 964		}
 965
 966		mlx5_ib_set_rule_source_port(dev, spec, rep);
 967	}
 968
 969	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
 970
 971	if (is_egress &&
 972	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
 973		err = -EINVAL;
 974		goto free;
 975	}
 976
 977	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
 978		struct mlx5_ib_mcounters *mcounters;
 979
 980		err = mlx5_ib_flow_counters_set_data(flow_act.counters, ucmd);
 981		if (err)
 982			goto free;
 983
 984		mcounters = to_mcounters(flow_act.counters);
 985		handler->ibcounters = flow_act.counters;
 986		dest_arr[dest_num].type =
 987			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
 988		dest_arr[dest_num].counter_id =
 989			mlx5_fc_id(mcounters->hw_cntrs_hndl);
 990		dest_num++;
 991	}
 992
 993	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
 994		if (!dest_num)
 995			rule_dst = NULL;
 996	} else {
 997		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)
 998			flow_act.action |=
 999				MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1000		if (is_egress)
1001			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1002		else if (dest_num)
1003			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1004	}
1005
1006	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
1007	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1008	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
1009		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
1010			     spec->flow_context.flow_tag, flow_attr->type);
1011		err = -EINVAL;
1012		goto free;
1013	}
1014	handler->rule = mlx5_add_flow_rules(ft, spec,
1015					    &flow_act,
1016					    rule_dst, dest_num);
1017
1018	if (IS_ERR(handler->rule)) {
1019		err = PTR_ERR(handler->rule);
1020		goto free;
1021	}
1022
1023	ft_prio->refcount++;
1024	handler->prio = ft_prio;
1025	handler->dev = dev;
1026
1027	ft_prio->flow_table = ft;
1028free:
1029	if (err && handler) {
1030		mlx5_ib_counters_clear_description(handler->ibcounters);
1031		kfree(handler);
1032	}
1033	kvfree(spec);
1034	return err ? ERR_PTR(err) : handler;
1035}
1036
1037static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1038						     struct mlx5_ib_flow_prio *ft_prio,
1039						     const struct ib_flow_attr *flow_attr,
1040						     struct mlx5_flow_destination *dst)
1041{
1042	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
1043}
1044
1045enum {
1046	LEFTOVERS_MC,
1047	LEFTOVERS_UC,
1048};
1049
1050static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1051							  struct mlx5_ib_flow_prio *ft_prio,
1052							  struct ib_flow_attr *flow_attr,
1053							  struct mlx5_flow_destination *dst)
1054{
1055	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1056	struct mlx5_ib_flow_handler *handler = NULL;
1057
1058	static struct {
1059		struct ib_flow_attr	flow_attr;
1060		struct ib_flow_spec_eth eth_flow;
1061	} leftovers_specs[] = {
1062		[LEFTOVERS_MC] = {
1063			.flow_attr = {
1064				.num_of_specs = 1,
1065				.size = sizeof(leftovers_specs[0])
1066			},
1067			.eth_flow = {
1068				.type = IB_FLOW_SPEC_ETH,
1069				.size = sizeof(struct ib_flow_spec_eth),
1070				.mask = {.dst_mac = {0x1} },
1071				.val =  {.dst_mac = {0x1} }
1072			}
1073		},
1074		[LEFTOVERS_UC] = {
1075			.flow_attr = {
1076				.num_of_specs = 1,
1077				.size = sizeof(leftovers_specs[0])
1078			},
1079			.eth_flow = {
1080				.type = IB_FLOW_SPEC_ETH,
1081				.size = sizeof(struct ib_flow_spec_eth),
1082				.mask = {.dst_mac = {0x1} },
1083				.val = {.dst_mac = {} }
1084			}
1085		}
1086	};
1087
1088	handler = create_flow_rule(dev, ft_prio,
1089				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
1090				   dst);
1091	if (!IS_ERR(handler) &&
1092	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
1093		handler_ucast = create_flow_rule(dev, ft_prio,
1094						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
1095						 dst);
1096		if (IS_ERR(handler_ucast)) {
1097			mlx5_del_flow_rules(handler->rule);
1098			ft_prio->refcount--;
1099			kfree(handler);
1100			handler = handler_ucast;
1101		} else {
1102			list_add(&handler_ucast->list, &handler->list);
1103		}
1104	}
1105
1106	return handler;
1107}
1108
1109static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
1110							struct mlx5_ib_flow_prio *ft_rx,
1111							struct mlx5_ib_flow_prio *ft_tx,
1112							struct mlx5_flow_destination *dst)
1113{
1114	struct mlx5_ib_flow_handler *handler_rx;
1115	struct mlx5_ib_flow_handler *handler_tx;
1116	int err;
1117	static const struct ib_flow_attr flow_attr  = {
1118		.num_of_specs = 0,
 
1119		.size = sizeof(flow_attr)
1120	};
1121
1122	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
1123	if (IS_ERR(handler_rx)) {
1124		err = PTR_ERR(handler_rx);
1125		goto err;
1126	}
1127
1128	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
1129	if (IS_ERR(handler_tx)) {
1130		err = PTR_ERR(handler_tx);
1131		goto err_tx;
1132	}
1133
1134	list_add(&handler_tx->list, &handler_rx->list);
1135
1136	return handler_rx;
1137
1138err_tx:
1139	mlx5_del_flow_rules(handler_rx->rule);
1140	ft_rx->refcount--;
1141	kfree(handler_rx);
1142err:
1143	return ERR_PTR(err);
1144}
1145
1146
1147static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
1148					   struct ib_flow_attr *flow_attr,
1149					   int domain,
1150					   struct ib_udata *udata)
1151{
1152	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1153	struct mlx5_ib_qp *mqp = to_mqp(qp);
1154	struct mlx5_ib_flow_handler *handler = NULL;
1155	struct mlx5_flow_destination *dst = NULL;
1156	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
1157	struct mlx5_ib_flow_prio *ft_prio;
1158	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
1159	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
1160	size_t min_ucmd_sz, required_ucmd_sz;
1161	int err;
1162	int underlay_qpn;
1163
1164	if (udata && udata->inlen) {
1165		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
1166				sizeof(ucmd_hdr.reserved);
1167		if (udata->inlen < min_ucmd_sz)
1168			return ERR_PTR(-EOPNOTSUPP);
1169
1170		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
1171		if (err)
1172			return ERR_PTR(err);
1173
1174		/* currently supports only one counters data */
1175		if (ucmd_hdr.ncounters_data > 1)
1176			return ERR_PTR(-EINVAL);
1177
1178		required_ucmd_sz = min_ucmd_sz +
1179			sizeof(struct mlx5_ib_flow_counters_data) *
1180			ucmd_hdr.ncounters_data;
1181		if (udata->inlen > required_ucmd_sz &&
1182		    !ib_is_udata_cleared(udata, required_ucmd_sz,
1183					 udata->inlen - required_ucmd_sz))
1184			return ERR_PTR(-EOPNOTSUPP);
1185
1186		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
1187		if (!ucmd)
1188			return ERR_PTR(-ENOMEM);
1189
1190		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
1191		if (err)
1192			goto free_ucmd;
1193	}
1194
1195	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
1196		err = -ENOMEM;
1197		goto free_ucmd;
1198	}
1199
1200	if (domain != IB_FLOW_DOMAIN_USER ||
1201	    flow_attr->port > dev->num_ports ||
1202	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
1203				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
1204		err = -EINVAL;
1205		goto free_ucmd;
1206	}
1207
1208	if (is_egress &&
1209	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1210	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
1211		err = -EINVAL;
1212		goto free_ucmd;
1213	}
1214
1215	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
1216	if (!dst) {
1217		err = -ENOMEM;
1218		goto free_ucmd;
1219	}
1220
1221	mutex_lock(&dev->flow_db->lock);
1222
1223	ft_prio = get_flow_table(dev, flow_attr,
1224				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
1225	if (IS_ERR(ft_prio)) {
1226		err = PTR_ERR(ft_prio);
1227		goto unlock;
1228	}
1229	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
1230		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
1231		if (IS_ERR(ft_prio_tx)) {
1232			err = PTR_ERR(ft_prio_tx);
1233			ft_prio_tx = NULL;
1234			goto destroy_ft;
1235		}
1236	}
1237
1238	if (is_egress) {
1239		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1240	} else {
1241		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1242		if (mqp->is_rss)
1243			dst->tir_num = mqp->rss_qp.tirn;
1244		else
1245			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
1246	}
1247
1248	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
 
1249		underlay_qpn = (mqp->flags & IB_QP_CREATE_SOURCE_QPN) ?
1250				       mqp->underlay_qpn :
1251				       0;
1252		handler = _create_flow_rule(dev, ft_prio, flow_attr, dst,
1253					    underlay_qpn, ucmd);
1254	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1255		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1256		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
1257						dst);
1258	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
 
1259		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
1260	} else {
 
1261		err = -EINVAL;
1262		goto destroy_ft;
1263	}
1264
1265	if (IS_ERR(handler)) {
1266		err = PTR_ERR(handler);
1267		handler = NULL;
1268		goto destroy_ft;
1269	}
1270
1271	mutex_unlock(&dev->flow_db->lock);
1272	kfree(dst);
1273	kfree(ucmd);
1274
1275	return &handler->ibflow;
1276
1277destroy_ft:
1278	put_flow_table(dev, ft_prio, false);
1279	if (ft_prio_tx)
1280		put_flow_table(dev, ft_prio_tx, false);
1281unlock:
1282	mutex_unlock(&dev->flow_db->lock);
1283	kfree(dst);
1284free_ucmd:
1285	kfree(ucmd);
1286	return ERR_PTR(err);
1287}
1288
1289static struct mlx5_ib_flow_prio *
1290_get_flow_table(struct mlx5_ib_dev *dev,
1291		struct mlx5_ib_flow_matcher *fs_matcher,
1292		bool mcast)
1293{
1294	struct mlx5_flow_namespace *ns = NULL;
1295	struct mlx5_ib_flow_prio *prio = NULL;
1296	int max_table_size = 0;
1297	bool esw_encap;
1298	u32 flags = 0;
1299	int priority;
1300
1301	if (mcast)
1302		priority = MLX5_IB_FLOW_MCAST_PRIO;
1303	else
1304		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
1305
1306	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
1307		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1308	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
1309		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
1310					log_max_ft_size));
 
1311		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
1312			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
1313		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
1314					      reformat_l3_tunnel_to_l2) &&
1315		    !esw_encap)
1316			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1317	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
 
1318		max_table_size = BIT(
1319			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
1320		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
 
1321			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1322	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
 
1323		max_table_size = BIT(
1324			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
1325		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
1326			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
1327		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
 
1328		    esw_encap)
1329			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
1330		priority = FDB_BYPASS_PATH;
1331	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
1332		max_table_size =
1333			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
1334						       log_max_ft_size));
1335		priority = fs_matcher->priority;
1336	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
1337		max_table_size =
1338			BIT(MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
1339						       log_max_ft_size));
1340		priority = fs_matcher->priority;
 
 
 
1341	}
1342
1343	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
1344
1345	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
1346	if (!ns)
1347		return ERR_PTR(-EOPNOTSUPP);
1348
1349	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
 
1350		prio = &dev->flow_db->prios[priority];
1351	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
 
1352		prio = &dev->flow_db->egress_prios[priority];
1353	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
1354		prio = &dev->flow_db->fdb;
1355	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
 
 
1356		prio = &dev->flow_db->rdma_rx[priority];
1357	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX)
 
1358		prio = &dev->flow_db->rdma_tx[priority];
 
 
 
1359
1360	if (!prio)
1361		return ERR_PTR(-EINVAL);
1362
1363	if (prio->flow_table)
1364		return prio;
1365
1366	return _get_prio(ns, prio, priority, max_table_size,
1367			 MLX5_FS_MAX_TYPES, flags);
1368}
1369
1370static struct mlx5_ib_flow_handler *
1371_create_raw_flow_rule(struct mlx5_ib_dev *dev,
1372		      struct mlx5_ib_flow_prio *ft_prio,
1373		      struct mlx5_flow_destination *dst,
1374		      struct mlx5_ib_flow_matcher  *fs_matcher,
1375		      struct mlx5_flow_context *flow_context,
1376		      struct mlx5_flow_act *flow_act,
1377		      void *cmd_in, int inlen,
1378		      int dst_num)
1379{
1380	struct mlx5_ib_flow_handler *handler;
1381	struct mlx5_flow_spec *spec;
1382	struct mlx5_flow_table *ft = ft_prio->flow_table;
1383	int err = 0;
1384
1385	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1386	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1387	if (!handler || !spec) {
1388		err = -ENOMEM;
1389		goto free;
1390	}
1391
1392	INIT_LIST_HEAD(&handler->list);
1393
1394	memcpy(spec->match_value, cmd_in, inlen);
1395	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
1396	       fs_matcher->mask_len);
1397	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
1398	spec->flow_context = *flow_context;
1399
1400	handler->rule = mlx5_add_flow_rules(ft, spec,
1401					    flow_act, dst, dst_num);
1402
1403	if (IS_ERR(handler->rule)) {
1404		err = PTR_ERR(handler->rule);
1405		goto free;
1406	}
1407
1408	ft_prio->refcount++;
1409	handler->prio = ft_prio;
1410	handler->dev = dev;
1411	ft_prio->flow_table = ft;
1412
1413free:
1414	if (err)
1415		kfree(handler);
1416	kvfree(spec);
1417	return err ? ERR_PTR(err) : handler;
1418}
1419
1420static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
1421				void *match_v)
1422{
1423	void *match_c;
1424	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
1425	void *dmac, *dmac_mask;
1426	void *ipv4, *ipv4_mask;
1427
1428	if (!(fs_matcher->match_criteria_enable &
1429	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
1430		return false;
1431
1432	match_c = fs_matcher->matcher_mask.match_params;
1433	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
1434					   outer_headers);
1435	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
1436					   outer_headers);
1437
1438	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
1439			    dmac_47_16);
1440	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
1441				 dmac_47_16);
1442
1443	if (is_multicast_ether_addr(dmac) &&
1444	    is_multicast_ether_addr(dmac_mask))
1445		return true;
1446
1447	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
1448			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1449
1450	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
1451				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
1452
1453	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
1454	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
1455		return true;
1456
1457	return false;
1458}
1459
1460static struct mlx5_ib_flow_handler *raw_fs_rule_add(
1461	struct mlx5_ib_dev *dev, struct mlx5_ib_flow_matcher *fs_matcher,
1462	struct mlx5_flow_context *flow_context, struct mlx5_flow_act *flow_act,
1463	u32 counter_id, void *cmd_in, int inlen, int dest_id, int dest_type)
1464{
1465	struct mlx5_flow_destination *dst;
1466	struct mlx5_ib_flow_prio *ft_prio;
1467	struct mlx5_ib_flow_handler *handler;
1468	int dst_num = 0;
1469	bool mcast;
1470	int err;
1471
1472	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
1473		return ERR_PTR(-EOPNOTSUPP);
1474
1475	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
1476		return ERR_PTR(-ENOMEM);
1477
1478	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
1479	if (!dst)
1480		return ERR_PTR(-ENOMEM);
1481
1482	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
1483	mutex_lock(&dev->flow_db->lock);
1484
1485	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
 
1486	if (IS_ERR(ft_prio)) {
1487		err = PTR_ERR(ft_prio);
1488		goto unlock;
1489	}
1490
1491	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
 
1492		dst[dst_num].type = dest_type;
1493		dst[dst_num++].tir_num = dest_id;
1494		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1495	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
 
1496		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
1497		dst[dst_num++].ft_num = dest_id;
1498		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1499	} else  if (dest_type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
 
1500		dst[dst_num++].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1501		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
 
 
 
1502	}
1503
1504
1505	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1506		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1507		dst[dst_num].counter_id = counter_id;
1508		dst_num++;
1509	}
1510
1511	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
1512					flow_context, flow_act,
1513					cmd_in, inlen, dst_num);
1514
1515	if (IS_ERR(handler)) {
1516		err = PTR_ERR(handler);
1517		goto destroy_ft;
1518	}
1519
1520	mutex_unlock(&dev->flow_db->lock);
1521	atomic_inc(&fs_matcher->usecnt);
1522	handler->flow_matcher = fs_matcher;
1523
1524	kfree(dst);
1525
1526	return handler;
1527
1528destroy_ft:
1529	put_flow_table(dev, ft_prio, false);
1530unlock:
1531	mutex_unlock(&dev->flow_db->lock);
1532	kfree(dst);
1533
1534	return ERR_PTR(err);
1535}
1536
1537static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
1538{
1539	u32 flags = 0;
1540
1541	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
1542		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
1543
1544	return flags;
1545}
1546
1547#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED                             \
1548	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
1549static struct ib_flow_action *
1550mlx5_ib_create_flow_action_esp(struct ib_device *device,
1551			       const struct ib_flow_action_attrs_esp *attr,
1552			       struct uverbs_attr_bundle *attrs)
1553{
1554	struct mlx5_ib_dev *mdev = to_mdev(device);
1555	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
1556	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
1557	struct mlx5_ib_flow_action *action;
1558	u64 action_flags;
1559	u64 flags;
1560	int err = 0;
1561
1562	err = uverbs_get_flags64(
1563		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
1564		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
1565	if (err)
1566		return ERR_PTR(err);
1567
1568	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
1569
1570	/* We current only support a subset of the standard features. Only a
1571	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
1572	 * (with overlap). Full offload mode isn't supported.
1573	 */
1574	if (!attr->keymat || attr->replay || attr->encap ||
1575	    attr->spi || attr->seq || attr->tfc_pad ||
1576	    attr->hard_limit_pkts ||
1577	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
1578			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
1579		return ERR_PTR(-EOPNOTSUPP);
1580
1581	if (attr->keymat->protocol !=
1582	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
1583		return ERR_PTR(-EOPNOTSUPP);
1584
1585	aes_gcm = &attr->keymat->keymat.aes_gcm;
1586
1587	if (aes_gcm->icv_len != 16 ||
1588	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
1589		return ERR_PTR(-EOPNOTSUPP);
1590
1591	action = kmalloc(sizeof(*action), GFP_KERNEL);
1592	if (!action)
1593		return ERR_PTR(-ENOMEM);
1594
1595	action->esp_aes_gcm.ib_flags = attr->flags;
1596	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
1597	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
1598	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
1599	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
1600	       sizeof(accel_attrs.keymat.aes_gcm.salt));
1601	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
1602	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
1603	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
1604	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
1605	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
1606
1607	accel_attrs.esn = attr->esn;
1608	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
1609		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
1610	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
1611		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
1612
1613	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
1614		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
1615
1616	action->esp_aes_gcm.ctx =
1617		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
1618	if (IS_ERR(action->esp_aes_gcm.ctx)) {
1619		err = PTR_ERR(action->esp_aes_gcm.ctx);
1620		goto err_parse;
1621	}
1622
1623	action->esp_aes_gcm.ib_flags = attr->flags;
1624
1625	return &action->ib_action;
1626
1627err_parse:
1628	kfree(action);
1629	return ERR_PTR(err);
1630}
1631
1632static int
1633mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
1634			       const struct ib_flow_action_attrs_esp *attr,
1635			       struct uverbs_attr_bundle *attrs)
1636{
1637	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
1638	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
1639	int err = 0;
1640
1641	if (attr->keymat || attr->replay || attr->encap ||
1642	    attr->spi || attr->seq || attr->tfc_pad ||
1643	    attr->hard_limit_pkts ||
1644	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
1645			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
1646			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
1647		return -EOPNOTSUPP;
1648
1649	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
1650	 * be modified.
1651	 */
1652	if (!(maction->esp_aes_gcm.ib_flags &
1653	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
1654	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
1655			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
1656		return -EINVAL;
1657
1658	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
1659	       sizeof(accel_attrs));
1660
1661	accel_attrs.esn = attr->esn;
1662	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
1663		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
1664	else
1665		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
1666
1667	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
1668					 &accel_attrs);
1669	if (err)
1670		return err;
1671
1672	maction->esp_aes_gcm.ib_flags &=
1673		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
1674	maction->esp_aes_gcm.ib_flags |=
1675		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
1676
1677	return 0;
1678}
1679
1680static void destroy_flow_action_raw(struct mlx5_ib_flow_action *maction)
1681{
1682	switch (maction->flow_action_raw.sub_type) {
1683	case MLX5_IB_FLOW_ACTION_MODIFY_HEADER:
1684		mlx5_modify_header_dealloc(maction->flow_action_raw.dev->mdev,
1685					   maction->flow_action_raw.modify_hdr);
1686		break;
1687	case MLX5_IB_FLOW_ACTION_PACKET_REFORMAT:
1688		mlx5_packet_reformat_dealloc(maction->flow_action_raw.dev->mdev,
1689					     maction->flow_action_raw.pkt_reformat);
1690		break;
1691	case MLX5_IB_FLOW_ACTION_DECAP:
1692		break;
1693	default:
1694		break;
1695	}
1696}
1697
1698static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
1699{
1700	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
1701
1702	switch (action->type) {
1703	case IB_FLOW_ACTION_ESP:
1704		/*
1705		 * We only support aes_gcm by now, so we implicitly know this is
1706		 * the underline crypto.
1707		 */
1708		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
1709		break;
1710	case IB_FLOW_ACTION_UNSPECIFIED:
1711		destroy_flow_action_raw(maction);
1712		break;
1713	default:
1714		WARN_ON(true);
1715		break;
1716	}
1717
1718	kfree(maction);
1719	return 0;
1720}
1721
1722static int
1723mlx5_ib_ft_type_to_namespace(enum mlx5_ib_uapi_flow_table_type table_type,
1724			     enum mlx5_flow_namespace_type *namespace)
1725{
1726	switch (table_type) {
1727	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX:
1728		*namespace = MLX5_FLOW_NAMESPACE_BYPASS;
1729		break;
1730	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX:
1731		*namespace = MLX5_FLOW_NAMESPACE_EGRESS;
1732		break;
1733	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_FDB:
1734		*namespace = MLX5_FLOW_NAMESPACE_FDB;
1735		break;
1736	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_RDMA_RX:
1737		*namespace = MLX5_FLOW_NAMESPACE_RDMA_RX;
1738		break;
1739	case MLX5_IB_UAPI_FLOW_TABLE_TYPE_RDMA_TX:
1740		*namespace = MLX5_FLOW_NAMESPACE_RDMA_TX;
1741		break;
1742	default:
1743		return -EINVAL;
1744	}
1745
1746	return 0;
1747}
1748
1749static const struct uverbs_attr_spec mlx5_ib_flow_type[] = {
1750	[MLX5_IB_FLOW_TYPE_NORMAL] = {
1751		.type = UVERBS_ATTR_TYPE_PTR_IN,
1752		.u.ptr = {
1753			.len = sizeof(u16), /* data is priority */
1754			.min_len = sizeof(u16),
1755		}
1756	},
1757	[MLX5_IB_FLOW_TYPE_SNIFFER] = {
1758		.type = UVERBS_ATTR_TYPE_PTR_IN,
1759		UVERBS_ATTR_NO_DATA(),
1760	},
1761	[MLX5_IB_FLOW_TYPE_ALL_DEFAULT] = {
1762		.type = UVERBS_ATTR_TYPE_PTR_IN,
1763		UVERBS_ATTR_NO_DATA(),
1764	},
1765	[MLX5_IB_FLOW_TYPE_MC_DEFAULT] = {
1766		.type = UVERBS_ATTR_TYPE_PTR_IN,
1767		UVERBS_ATTR_NO_DATA(),
1768	},
1769};
1770
1771static bool is_flow_dest(void *obj, int *dest_id, int *dest_type)
1772{
1773	struct devx_obj *devx_obj = obj;
1774	u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox, opcode);
1775
1776	switch (opcode) {
1777	case MLX5_CMD_OP_DESTROY_TIR:
1778		*dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1779		*dest_id = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox,
1780				    obj_id);
1781		return true;
1782
1783	case MLX5_CMD_OP_DESTROY_FLOW_TABLE:
1784		*dest_type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1785		*dest_id = MLX5_GET(destroy_flow_table_in, devx_obj->dinbox,
1786				    table_id);
1787		return true;
1788	default:
1789		return false;
1790	}
1791}
1792
1793static int get_dests(struct uverbs_attr_bundle *attrs,
1794		     struct mlx5_ib_flow_matcher *fs_matcher, int *dest_id,
1795		     int *dest_type, struct ib_qp **qp, u32 *flags)
1796{
1797	bool dest_devx, dest_qp;
1798	void *devx_obj;
1799	int err;
1800
1801	dest_devx = uverbs_attr_is_valid(attrs,
1802					 MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
1803	dest_qp = uverbs_attr_is_valid(attrs,
1804				       MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
1805
1806	*flags = 0;
1807	err = uverbs_get_flags32(flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_FLAGS,
1808				 MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS |
1809					 MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP);
1810	if (err)
1811		return err;
1812
1813	/* Both flags are not allowed */
1814	if (*flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS &&
1815	    *flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)
1816		return -EINVAL;
1817
1818	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
1819		if (dest_devx && (dest_qp || *flags))
1820			return -EINVAL;
1821		else if (dest_qp && *flags)
1822			return -EINVAL;
1823	}
1824
1825	/* Allow only DEVX object, drop as dest for FDB */
1826	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB && !(dest_devx ||
1827	     (*flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)))
1828		return -EINVAL;
1829
1830	/* Allow only DEVX object or QP as dest when inserting to RDMA_RX */
1831	if ((fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) &&
1832	    ((!dest_devx && !dest_qp) || (dest_devx && dest_qp)))
1833		return -EINVAL;
1834
1835	*qp = NULL;
1836	if (dest_devx) {
1837		devx_obj =
1838			uverbs_attr_get_obj(attrs,
1839					    MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX);
1840
1841		/* Verify that the given DEVX object is a flow
1842		 * steering destination.
1843		 */
1844		if (!is_flow_dest(devx_obj, dest_id, dest_type))
1845			return -EINVAL;
1846		/* Allow only flow table as dest when inserting to FDB or RDMA_RX */
1847		if ((fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB ||
1848		     fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) &&
1849		    *dest_type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1850			return -EINVAL;
1851	} else if (dest_qp) {
1852		struct mlx5_ib_qp *mqp;
1853
1854		*qp = uverbs_attr_get_obj(attrs,
1855					  MLX5_IB_ATTR_CREATE_FLOW_DEST_QP);
1856		if (IS_ERR(*qp))
1857			return PTR_ERR(*qp);
1858
1859		if ((*qp)->qp_type != IB_QPT_RAW_PACKET)
1860			return -EINVAL;
1861
1862		mqp = to_mqp(*qp);
1863		if (mqp->is_rss)
1864			*dest_id = mqp->rss_qp.tirn;
1865		else
1866			*dest_id = mqp->raw_packet_qp.rq.tirn;
1867		*dest_type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1868	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS ||
1869		   fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
 
1870		*dest_type = MLX5_FLOW_DESTINATION_TYPE_PORT;
1871	}
1872
1873	if (*dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1874	    (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS ||
1875	     fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX))
1876		return -EINVAL;
1877
1878	return 0;
1879}
1880
1881static bool is_flow_counter(void *obj, u32 offset, u32 *counter_id)
1882{
1883	struct devx_obj *devx_obj = obj;
1884	u16 opcode = MLX5_GET(general_obj_in_cmd_hdr, devx_obj->dinbox, opcode);
1885
1886	if (opcode == MLX5_CMD_OP_DEALLOC_FLOW_COUNTER) {
1887
1888		if (offset && offset >= devx_obj->flow_counter_bulk_size)
1889			return false;
1890
1891		*counter_id = MLX5_GET(dealloc_flow_counter_in,
1892				       devx_obj->dinbox,
1893				       flow_counter_id);
1894		*counter_id += offset;
1895		return true;
1896	}
1897
1898	return false;
1899}
1900
1901#define MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS 2
1902static int UVERBS_HANDLER(MLX5_IB_METHOD_CREATE_FLOW)(
1903	struct uverbs_attr_bundle *attrs)
1904{
1905	struct mlx5_flow_context flow_context = {.flow_tag =
1906		MLX5_FS_DEFAULT_FLOW_TAG};
1907	u32 *offset_attr, offset = 0, counter_id = 0;
1908	int dest_id, dest_type = -1, inlen, len, ret, i;
1909	struct mlx5_ib_flow_handler *flow_handler;
1910	struct mlx5_ib_flow_matcher *fs_matcher;
1911	struct ib_uobject **arr_flow_actions;
1912	struct ib_uflow_resources *uflow_res;
1913	struct mlx5_flow_act flow_act = {};
1914	struct ib_qp *qp = NULL;
1915	void *devx_obj, *cmd_in;
1916	struct ib_uobject *uobj;
1917	struct mlx5_ib_dev *dev;
1918	u32 flags;
1919
1920	if (!capable(CAP_NET_RAW))
1921		return -EPERM;
1922
1923	fs_matcher = uverbs_attr_get_obj(attrs,
1924					 MLX5_IB_ATTR_CREATE_FLOW_MATCHER);
1925	uobj =  uverbs_attr_get_uobject(attrs, MLX5_IB_ATTR_CREATE_FLOW_HANDLE);
1926	dev = mlx5_udata_to_mdev(&attrs->driver_udata);
1927
1928	if (get_dests(attrs, fs_matcher, &dest_id, &dest_type, &qp, &flags))
1929		return -EINVAL;
1930
1931	if (flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DEFAULT_MISS)
1932		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
1933
1934	if (flags & MLX5_IB_ATTR_CREATE_FLOW_FLAGS_DROP)
1935		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1936
1937	len = uverbs_attr_get_uobjs_arr(attrs,
1938		MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX, &arr_flow_actions);
1939	if (len) {
1940		devx_obj = arr_flow_actions[0]->object;
1941
1942		if (uverbs_attr_is_valid(attrs,
1943					 MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET)) {
1944
1945			int num_offsets = uverbs_attr_ptr_get_array_size(
1946				attrs,
1947				MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET,
1948				sizeof(u32));
1949
1950			if (num_offsets != 1)
1951				return -EINVAL;
1952
1953			offset_attr = uverbs_attr_get_alloced_ptr(
1954				attrs,
1955				MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET);
1956			offset = *offset_attr;
1957		}
1958
1959		if (!is_flow_counter(devx_obj, offset, &counter_id))
1960			return -EINVAL;
1961
1962		flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1963	}
1964
1965	cmd_in = uverbs_attr_get_alloced_ptr(
1966		attrs, MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
1967	inlen = uverbs_attr_get_len(attrs,
1968				    MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE);
1969
1970	uflow_res = flow_resources_alloc(MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS);
1971	if (!uflow_res)
1972		return -ENOMEM;
1973
1974	len = uverbs_attr_get_uobjs_arr(attrs,
1975		MLX5_IB_ATTR_CREATE_FLOW_ARR_FLOW_ACTIONS, &arr_flow_actions);
1976	for (i = 0; i < len; i++) {
1977		struct mlx5_ib_flow_action *maction =
1978			to_mflow_act(arr_flow_actions[i]->object);
1979
1980		ret = parse_flow_flow_action(maction, false, &flow_act);
1981		if (ret)
1982			goto err_out;
1983		flow_resources_add(uflow_res, IB_FLOW_SPEC_ACTION_HANDLE,
1984				   arr_flow_actions[i]->object);
1985	}
1986
1987	ret = uverbs_copy_from(&flow_context.flow_tag, attrs,
1988			       MLX5_IB_ATTR_CREATE_FLOW_TAG);
1989	if (!ret) {
1990		if (flow_context.flow_tag >= BIT(24)) {
1991			ret = -EINVAL;
1992			goto err_out;
1993		}
1994		flow_context.flags |= FLOW_CONTEXT_HAS_TAG;
1995	}
1996
1997	flow_handler =
1998		raw_fs_rule_add(dev, fs_matcher, &flow_context, &flow_act,
1999				counter_id, cmd_in, inlen, dest_id, dest_type);
2000	if (IS_ERR(flow_handler)) {
2001		ret = PTR_ERR(flow_handler);
2002		goto err_out;
2003	}
2004
2005	ib_set_flow(uobj, &flow_handler->ibflow, qp, &dev->ib_dev, uflow_res);
2006
2007	return 0;
2008err_out:
2009	ib_uverbs_flow_resources_free(uflow_res);
2010	return ret;
2011}
2012
2013static int flow_matcher_cleanup(struct ib_uobject *uobject,
2014				enum rdma_remove_reason why,
2015				struct uverbs_attr_bundle *attrs)
2016{
2017	struct mlx5_ib_flow_matcher *obj = uobject->object;
2018	int ret;
2019
2020	ret = ib_destroy_usecnt(&obj->usecnt, why, uobject);
2021	if (ret)
2022		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2023
2024	kfree(obj);
2025	return 0;
2026}
2027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2028static int mlx5_ib_matcher_ns(struct uverbs_attr_bundle *attrs,
2029			      struct mlx5_ib_flow_matcher *obj)
2030{
2031	enum mlx5_ib_uapi_flow_table_type ft_type =
2032		MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX;
2033	u32 flags;
2034	int err;
2035
2036	/* New users should use MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE and older
2037	 * users should switch to it. We leave this to not break userspace
2038	 */
2039	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE) &&
2040	    uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS))
2041		return -EINVAL;
2042
2043	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE)) {
2044		err = uverbs_get_const(&ft_type, attrs,
2045				       MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE);
2046		if (err)
2047			return err;
2048
2049		err = mlx5_ib_ft_type_to_namespace(ft_type, &obj->ns_type);
2050		if (err)
2051			return err;
2052
2053		return 0;
2054	}
2055
2056	if (uverbs_attr_is_valid(attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS)) {
2057		err = uverbs_get_flags32(&flags, attrs,
2058					 MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS,
2059					 IB_FLOW_ATTR_FLAGS_EGRESS);
2060		if (err)
2061			return err;
2062
2063		if (flags) {
2064			mlx5_ib_ft_type_to_namespace(
2065				MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX,
2066				&obj->ns_type);
2067			return 0;
2068		}
2069	}
2070
2071	obj->ns_type = MLX5_FLOW_NAMESPACE_BYPASS;
2072
2073	return 0;
2074}
2075
2076static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_MATCHER_CREATE)(
2077	struct uverbs_attr_bundle *attrs)
2078{
2079	struct ib_uobject *uobj = uverbs_attr_get_uobject(
2080		attrs, MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE);
2081	struct mlx5_ib_dev *dev = mlx5_udata_to_mdev(&attrs->driver_udata);
2082	struct mlx5_ib_flow_matcher *obj;
2083	int err;
2084
2085	obj = kzalloc(sizeof(struct mlx5_ib_flow_matcher), GFP_KERNEL);
2086	if (!obj)
2087		return -ENOMEM;
2088
2089	obj->mask_len = uverbs_attr_get_len(
2090		attrs, MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
2091	err = uverbs_copy_from(&obj->matcher_mask,
2092			       attrs,
2093			       MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK);
2094	if (err)
2095		goto end;
2096
2097	obj->flow_type = uverbs_attr_get_enum_id(
2098		attrs, MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
2099
2100	if (obj->flow_type == MLX5_IB_FLOW_TYPE_NORMAL) {
2101		err = uverbs_copy_from(&obj->priority,
2102				       attrs,
2103				       MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE);
2104		if (err)
2105			goto end;
2106	}
2107
2108	err = uverbs_copy_from(&obj->match_criteria_enable,
2109			       attrs,
2110			       MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA);
2111	if (err)
2112		goto end;
2113
2114	err = mlx5_ib_matcher_ns(attrs, obj);
2115	if (err)
2116		goto end;
2117
 
 
 
 
 
 
2118	uobj->object = obj;
2119	obj->mdev = dev->mdev;
2120	atomic_set(&obj->usecnt, 0);
2121	return 0;
2122
2123end:
2124	kfree(obj);
2125	return err;
2126}
2127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2128static struct ib_flow_action *
2129mlx5_ib_create_modify_header(struct mlx5_ib_dev *dev,
2130			     enum mlx5_ib_uapi_flow_table_type ft_type,
2131			     u8 num_actions, void *in)
2132{
2133	enum mlx5_flow_namespace_type namespace;
2134	struct mlx5_ib_flow_action *maction;
2135	int ret;
2136
2137	ret = mlx5_ib_ft_type_to_namespace(ft_type, &namespace);
2138	if (ret)
2139		return ERR_PTR(-EINVAL);
2140
2141	maction = kzalloc(sizeof(*maction), GFP_KERNEL);
2142	if (!maction)
2143		return ERR_PTR(-ENOMEM);
2144
2145	maction->flow_action_raw.modify_hdr =
2146		mlx5_modify_header_alloc(dev->mdev, namespace, num_actions, in);
2147
2148	if (IS_ERR(maction->flow_action_raw.modify_hdr)) {
2149		ret = PTR_ERR(maction->flow_action_raw.modify_hdr);
2150		kfree(maction);
2151		return ERR_PTR(ret);
2152	}
2153	maction->flow_action_raw.sub_type =
2154		MLX5_IB_FLOW_ACTION_MODIFY_HEADER;
2155	maction->flow_action_raw.dev = dev;
2156
2157	return &maction->ib_action;
2158}
2159
2160static bool mlx5_ib_modify_header_supported(struct mlx5_ib_dev *dev)
2161{
2162	return MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2163					 max_modify_header_actions) ||
2164	       MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
2165					 max_modify_header_actions) ||
2166	       MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
2167					 max_modify_header_actions);
2168}
2169
2170static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER)(
2171	struct uverbs_attr_bundle *attrs)
2172{
2173	struct ib_uobject *uobj = uverbs_attr_get_uobject(
2174		attrs, MLX5_IB_ATTR_CREATE_MODIFY_HEADER_HANDLE);
2175	struct mlx5_ib_dev *mdev = mlx5_udata_to_mdev(&attrs->driver_udata);
2176	enum mlx5_ib_uapi_flow_table_type ft_type;
2177	struct ib_flow_action *action;
2178	int num_actions;
2179	void *in;
2180	int ret;
2181
2182	if (!mlx5_ib_modify_header_supported(mdev))
2183		return -EOPNOTSUPP;
2184
2185	in = uverbs_attr_get_alloced_ptr(attrs,
2186		MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM);
2187
2188	num_actions = uverbs_attr_ptr_get_array_size(
2189		attrs, MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM,
2190		MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto));
2191	if (num_actions < 0)
2192		return num_actions;
2193
2194	ret = uverbs_get_const(&ft_type, attrs,
2195			       MLX5_IB_ATTR_CREATE_MODIFY_HEADER_FT_TYPE);
2196	if (ret)
2197		return ret;
2198	action = mlx5_ib_create_modify_header(mdev, ft_type, num_actions, in);
2199	if (IS_ERR(action))
2200		return PTR_ERR(action);
2201
2202	uverbs_flow_action_fill_action(action, uobj, &mdev->ib_dev,
2203				       IB_FLOW_ACTION_UNSPECIFIED);
2204
2205	return 0;
2206}
2207
2208static bool mlx5_ib_flow_action_packet_reformat_valid(struct mlx5_ib_dev *ibdev,
2209						      u8 packet_reformat_type,
2210						      u8 ft_type)
2211{
2212	switch (packet_reformat_type) {
2213	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
2214		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX)
2215			return MLX5_CAP_FLOWTABLE(ibdev->mdev,
2216						  encap_general_header);
2217		break;
2218	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
2219		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_TX)
2220			return MLX5_CAP_FLOWTABLE_NIC_TX(ibdev->mdev,
2221				reformat_l2_to_l3_tunnel);
2222		break;
2223	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
2224		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX)
2225			return MLX5_CAP_FLOWTABLE_NIC_RX(ibdev->mdev,
2226				reformat_l3_tunnel_to_l2);
2227		break;
2228	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2:
2229		if (ft_type == MLX5_IB_UAPI_FLOW_TABLE_TYPE_NIC_RX)
2230			return MLX5_CAP_FLOWTABLE_NIC_RX(ibdev->mdev, decap);
2231		break;
2232	default:
2233		break;
2234	}
2235
2236	return false;
2237}
2238
2239static int mlx5_ib_dv_to_prm_packet_reforamt_type(u8 dv_prt, u8 *prm_prt)
2240{
2241	switch (dv_prt) {
2242	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
2243		*prm_prt = MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL;
2244		break;
2245	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L3_TUNNEL_TO_L2:
2246		*prm_prt = MLX5_REFORMAT_TYPE_L3_TUNNEL_TO_L2;
2247		break;
2248	case MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
2249		*prm_prt = MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL;
2250		break;
2251	default:
2252		return -EINVAL;
2253	}
2254
2255	return 0;
2256}
2257
2258static int mlx5_ib_flow_action_create_packet_reformat_ctx(
2259	struct mlx5_ib_dev *dev,
2260	struct mlx5_ib_flow_action *maction,
2261	u8 ft_type, u8 dv_prt,
2262	void *in, size_t len)
2263{
 
2264	enum mlx5_flow_namespace_type namespace;
2265	u8 prm_prt;
2266	int ret;
2267
2268	ret = mlx5_ib_ft_type_to_namespace(ft_type, &namespace);
2269	if (ret)
2270		return ret;
2271
2272	ret = mlx5_ib_dv_to_prm_packet_reforamt_type(dv_prt, &prm_prt);
2273	if (ret)
2274		return ret;
2275
 
 
 
 
2276	maction->flow_action_raw.pkt_reformat =
2277		mlx5_packet_reformat_alloc(dev->mdev, prm_prt, len,
2278					   in, namespace);
2279	if (IS_ERR(maction->flow_action_raw.pkt_reformat)) {
2280		ret = PTR_ERR(maction->flow_action_raw.pkt_reformat);
2281		return ret;
2282	}
2283
2284	maction->flow_action_raw.sub_type =
2285		MLX5_IB_FLOW_ACTION_PACKET_REFORMAT;
2286	maction->flow_action_raw.dev = dev;
2287
2288	return 0;
2289}
2290
2291static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT)(
2292	struct uverbs_attr_bundle *attrs)
2293{
2294	struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs,
2295		MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_HANDLE);
2296	struct mlx5_ib_dev *mdev = mlx5_udata_to_mdev(&attrs->driver_udata);
2297	enum mlx5_ib_uapi_flow_action_packet_reformat_type dv_prt;
2298	enum mlx5_ib_uapi_flow_table_type ft_type;
2299	struct mlx5_ib_flow_action *maction;
2300	int ret;
2301
2302	ret = uverbs_get_const(&ft_type, attrs,
2303			       MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_FT_TYPE);
2304	if (ret)
2305		return ret;
2306
2307	ret = uverbs_get_const(&dv_prt, attrs,
2308			       MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_TYPE);
2309	if (ret)
2310		return ret;
2311
2312	if (!mlx5_ib_flow_action_packet_reformat_valid(mdev, dv_prt, ft_type))
2313		return -EOPNOTSUPP;
2314
2315	maction = kzalloc(sizeof(*maction), GFP_KERNEL);
2316	if (!maction)
2317		return -ENOMEM;
2318
2319	if (dv_prt ==
2320	    MLX5_IB_UAPI_FLOW_ACTION_PACKET_REFORMAT_TYPE_L2_TUNNEL_TO_L2) {
2321		maction->flow_action_raw.sub_type =
2322			MLX5_IB_FLOW_ACTION_DECAP;
2323		maction->flow_action_raw.dev = mdev;
2324	} else {
2325		void *in;
2326		int len;
2327
2328		in = uverbs_attr_get_alloced_ptr(attrs,
2329			MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF);
2330		if (IS_ERR(in)) {
2331			ret = PTR_ERR(in);
2332			goto free_maction;
2333		}
2334
2335		len = uverbs_attr_get_len(attrs,
2336			MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF);
2337
2338		ret = mlx5_ib_flow_action_create_packet_reformat_ctx(mdev,
2339			maction, ft_type, dv_prt, in, len);
2340		if (ret)
2341			goto free_maction;
2342	}
2343
2344	uverbs_flow_action_fill_action(&maction->ib_action, uobj, &mdev->ib_dev,
2345				       IB_FLOW_ACTION_UNSPECIFIED);
2346	return 0;
2347
2348free_maction:
2349	kfree(maction);
2350	return ret;
2351}
2352
2353DECLARE_UVERBS_NAMED_METHOD(
2354	MLX5_IB_METHOD_CREATE_FLOW,
2355	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
2356			UVERBS_OBJECT_FLOW,
2357			UVERBS_ACCESS_NEW,
2358			UA_MANDATORY),
2359	UVERBS_ATTR_PTR_IN(
2360		MLX5_IB_ATTR_CREATE_FLOW_MATCH_VALUE,
2361		UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
2362		UA_MANDATORY,
2363		UA_ALLOC_AND_COPY),
2364	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_MATCHER,
2365			MLX5_IB_OBJECT_FLOW_MATCHER,
2366			UVERBS_ACCESS_READ,
2367			UA_MANDATORY),
2368	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_QP,
2369			UVERBS_OBJECT_QP,
2370			UVERBS_ACCESS_READ),
2371	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_DEST_DEVX,
2372			MLX5_IB_OBJECT_DEVX_OBJ,
2373			UVERBS_ACCESS_READ),
2374	UVERBS_ATTR_IDRS_ARR(MLX5_IB_ATTR_CREATE_FLOW_ARR_FLOW_ACTIONS,
2375			     UVERBS_OBJECT_FLOW_ACTION,
2376			     UVERBS_ACCESS_READ, 1,
2377			     MLX5_IB_CREATE_FLOW_MAX_FLOW_ACTIONS,
2378			     UA_OPTIONAL),
2379	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_TAG,
2380			   UVERBS_ATTR_TYPE(u32),
2381			   UA_OPTIONAL),
2382	UVERBS_ATTR_IDRS_ARR(MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX,
2383			     MLX5_IB_OBJECT_DEVX_OBJ,
2384			     UVERBS_ACCESS_READ, 1, 1,
2385			     UA_OPTIONAL),
2386	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ARR_COUNTERS_DEVX_OFFSET,
2387			   UVERBS_ATTR_MIN_SIZE(sizeof(u32)),
2388			   UA_OPTIONAL,
2389			   UA_ALLOC_AND_COPY),
2390	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_FLAGS,
2391			     enum mlx5_ib_create_flow_flags,
2392			     UA_OPTIONAL));
2393
2394DECLARE_UVERBS_NAMED_METHOD_DESTROY(
2395	MLX5_IB_METHOD_DESTROY_FLOW,
2396	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_FLOW_HANDLE,
2397			UVERBS_OBJECT_FLOW,
2398			UVERBS_ACCESS_DESTROY,
2399			UA_MANDATORY));
2400
2401ADD_UVERBS_METHODS(mlx5_ib_fs,
2402		   UVERBS_OBJECT_FLOW,
2403		   &UVERBS_METHOD(MLX5_IB_METHOD_CREATE_FLOW),
2404		   &UVERBS_METHOD(MLX5_IB_METHOD_DESTROY_FLOW));
2405
2406DECLARE_UVERBS_NAMED_METHOD(
2407	MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER,
2408	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_HANDLE,
2409			UVERBS_OBJECT_FLOW_ACTION,
2410			UVERBS_ACCESS_NEW,
2411			UA_MANDATORY),
2412	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM,
2413			   UVERBS_ATTR_MIN_SIZE(MLX5_UN_SZ_BYTES(
2414				   set_add_copy_action_in_auto)),
2415			   UA_MANDATORY,
2416			   UA_ALLOC_AND_COPY),
2417	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_MODIFY_HEADER_FT_TYPE,
2418			     enum mlx5_ib_uapi_flow_table_type,
2419			     UA_MANDATORY));
2420
2421DECLARE_UVERBS_NAMED_METHOD(
2422	MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT,
2423	UVERBS_ATTR_IDR(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_HANDLE,
2424			UVERBS_OBJECT_FLOW_ACTION,
2425			UVERBS_ACCESS_NEW,
2426			UA_MANDATORY),
2427	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_DATA_BUF,
2428			   UVERBS_ATTR_MIN_SIZE(1),
2429			   UA_ALLOC_AND_COPY,
2430			   UA_OPTIONAL),
2431	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_TYPE,
2432			     enum mlx5_ib_uapi_flow_action_packet_reformat_type,
2433			     UA_MANDATORY),
2434	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_CREATE_PACKET_REFORMAT_FT_TYPE,
2435			     enum mlx5_ib_uapi_flow_table_type,
2436			     UA_MANDATORY));
2437
2438ADD_UVERBS_METHODS(
2439	mlx5_ib_flow_actions,
2440	UVERBS_OBJECT_FLOW_ACTION,
2441	&UVERBS_METHOD(MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER),
2442	&UVERBS_METHOD(MLX5_IB_METHOD_FLOW_ACTION_CREATE_PACKET_REFORMAT));
2443
2444DECLARE_UVERBS_NAMED_METHOD(
2445	MLX5_IB_METHOD_FLOW_MATCHER_CREATE,
2446	UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_CREATE_HANDLE,
2447			MLX5_IB_OBJECT_FLOW_MATCHER,
2448			UVERBS_ACCESS_NEW,
2449			UA_MANDATORY),
2450	UVERBS_ATTR_PTR_IN(
2451		MLX5_IB_ATTR_FLOW_MATCHER_MATCH_MASK,
2452		UVERBS_ATTR_SIZE(1, sizeof(struct mlx5_ib_match_params)),
2453		UA_MANDATORY),
2454	UVERBS_ATTR_ENUM_IN(MLX5_IB_ATTR_FLOW_MATCHER_FLOW_TYPE,
2455			    mlx5_ib_flow_type,
2456			    UA_MANDATORY),
2457	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_FLOW_MATCHER_MATCH_CRITERIA,
2458			   UVERBS_ATTR_TYPE(u8),
2459			   UA_MANDATORY),
2460	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_FLOW_MATCHER_FLOW_FLAGS,
2461			     enum ib_flow_flags,
2462			     UA_OPTIONAL),
2463	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_FLOW_MATCHER_FT_TYPE,
2464			     enum mlx5_ib_uapi_flow_table_type,
2465			     UA_OPTIONAL));
2466
2467DECLARE_UVERBS_NAMED_METHOD_DESTROY(
2468	MLX5_IB_METHOD_FLOW_MATCHER_DESTROY,
2469	UVERBS_ATTR_IDR(MLX5_IB_ATTR_FLOW_MATCHER_DESTROY_HANDLE,
2470			MLX5_IB_OBJECT_FLOW_MATCHER,
2471			UVERBS_ACCESS_DESTROY,
2472			UA_MANDATORY));
2473
2474DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_FLOW_MATCHER,
2475			    UVERBS_TYPE_ALLOC_IDR(flow_matcher_cleanup),
2476			    &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_CREATE),
2477			    &UVERBS_METHOD(MLX5_IB_METHOD_FLOW_MATCHER_DESTROY));
2478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2479const struct uapi_definition mlx5_ib_flow_defs[] = {
2480	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(
2481		MLX5_IB_OBJECT_FLOW_MATCHER),
2482	UAPI_DEF_CHAIN_OBJ_TREE(
2483		UVERBS_OBJECT_FLOW,
2484		&mlx5_ib_fs),
2485	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
2486				&mlx5_ib_flow_actions),
 
 
 
2487	{},
2488};
2489
2490static const struct ib_device_ops flow_ops = {
2491	.create_flow = mlx5_ib_create_flow,
2492	.destroy_flow = mlx5_ib_destroy_flow,
2493	.destroy_flow_action = mlx5_ib_destroy_flow_action,
2494};
2495
2496static const struct ib_device_ops flow_ipsec_ops = {
2497	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
2498	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
2499};
2500
2501int mlx5_ib_fs_init(struct mlx5_ib_dev *dev)
2502{
2503	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
2504
2505	if (!dev->flow_db)
2506		return -ENOMEM;
2507
2508	mutex_init(&dev->flow_db->lock);
2509
2510	ib_set_device_ops(&dev->ib_dev, &flow_ops);
2511	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
2512	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
2513		ib_set_device_ops(&dev->ib_dev, &flow_ipsec_ops);
2514
2515	return 0;
2516}