eswitch_offloads.c 35.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/etherdevice.h>
#include <linux/mlx5/driver.h>
#include <linux/mlx5/mlx5_ifc.h>
#include <linux/mlx5/vport.h>
#include <linux/mlx5/fs.h>
#include "mlx5_core.h"
#include "eswitch.h"

41 42 43 44 45
enum {
	FDB_FAST_PATH = 0,
	FDB_SLOW_PATH
};

M
Mark Bloch 已提交
46
struct mlx5_flow_handle *
47 48
mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
				struct mlx5_flow_spec *spec,
49
				struct mlx5_esw_flow_attr *attr)
50
{
51
	struct mlx5_flow_destination dest[MLX5_MAX_FLOW_FWD_VPORTS + 1] = {};
52
	struct mlx5_flow_act flow_act = {0};
53
	struct mlx5_flow_table *ft = NULL;
54
	struct mlx5_fc *counter = NULL;
M
Mark Bloch 已提交
55
	struct mlx5_flow_handle *rule;
56
	int j, i = 0;
57 58 59 60 61
	void *misc;

	if (esw->mode != SRIOV_OFFLOADS)
		return ERR_PTR(-EOPNOTSUPP);

62 63 64 65 66
	if (attr->mirror_count)
		ft = esw->fdb_table.offloads.fwd_fdb;
	else
		ft = esw->fdb_table.offloads.fast_fdb;

67 68
	flow_act.action = attr->action;
	/* if per flow vlan pop/push is emulated, don't set that into the firmware */
69
	if (!mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
70 71 72
		flow_act.action &= ~(MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
				     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
	else if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
73 74 75
		flow_act.vlan[0].ethtype = ntohs(attr->vlan_proto[0]);
		flow_act.vlan[0].vid = attr->vlan_vid[0];
		flow_act.vlan[0].prio = attr->vlan_prio[0];
76 77 78 79 80
		if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
			flow_act.vlan[1].ethtype = ntohs(attr->vlan_proto[1]);
			flow_act.vlan[1].vid = attr->vlan_vid[1];
			flow_act.vlan[1].prio = attr->vlan_prio[1];
		}
81
	}
82

83
	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
84 85 86
		for (j = attr->mirror_count; j < attr->out_count; j++) {
			dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
			dest[i].vport.num = attr->out_rep[j]->vport;
87 88 89
			dest[i].vport.vhca_id =
				MLX5_CAP_GEN(attr->out_mdev[j], vhca_id);
			dest[i].vport.vhca_id_valid = !!MLX5_CAP_ESW(esw->dev, merged_eswitch);
90
			i++;
91
		}
92
	}
93
	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
94
		counter = mlx5_fc_create(esw->dev, true);
95 96 97 98
		if (IS_ERR(counter)) {
			rule = ERR_CAST(counter);
			goto err_counter_alloc;
		}
99 100 101
		dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
		dest[i].counter = counter;
		i++;
102 103 104
	}

	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
105
	MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);
106

107 108 109 110 111
	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
		MLX5_SET(fte_match_set_misc, misc,
			 source_eswitch_owner_vhca_id,
			 MLX5_CAP_GEN(attr->in_mdev, vhca_id));

112 113
	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
114 115 116
	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
		MLX5_SET_TO_ONES(fte_match_set_misc, misc,
				 source_eswitch_owner_vhca_id);
117

118 119 120 121 122 123
	if (attr->match_level == MLX5_MATCH_NONE)
		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
	else
		spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS |
					      MLX5_MATCH_MISC_PARAMETERS;

124 125
	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
		spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
126

127
	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
128 129
		flow_act.modify_id = attr->mod_hdr_id;

130
	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
131
		flow_act.encap_id = attr->encap_id;
132

133
	rule = mlx5_add_flow_rules(ft, spec, &flow_act, dest, i);
134
	if (IS_ERR(rule))
135
		goto err_add_rule;
136 137
	else
		esw->offloads.num_flows++;
138 139

	return rule;
140 141 142 143 144

err_add_rule:
	mlx5_fc_destroy(esw->dev, counter);
err_counter_alloc:
	return rule;
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
struct mlx5_flow_handle *
mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch *esw,
			  struct mlx5_flow_spec *spec,
			  struct mlx5_esw_flow_attr *attr)
{
	struct mlx5_flow_destination dest[MLX5_MAX_FLOW_FWD_VPORTS + 1] = {};
	struct mlx5_flow_act flow_act = {0};
	struct mlx5_flow_handle *rule;
	void *misc;
	int i;

	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
	for (i = 0; i < attr->mirror_count; i++) {
		dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
		dest[i].vport.num = attr->out_rep[i]->vport;
		dest[i].vport.vhca_id =
			MLX5_CAP_GEN(attr->out_mdev[i], vhca_id);
		dest[i].vport.vhca_id_valid = !!MLX5_CAP_ESW(esw->dev, merged_eswitch);
	}
	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
	dest[i].ft = esw->fdb_table.offloads.fwd_fdb,
	i++;

	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
	MLX5_SET(fte_match_set_misc, misc, source_port, attr->in_rep->vport);

	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
		MLX5_SET(fte_match_set_misc, misc,
			 source_eswitch_owner_vhca_id,
			 MLX5_CAP_GEN(attr->in_mdev, vhca_id));

	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
		MLX5_SET_TO_ONES(fte_match_set_misc, misc,
				 source_eswitch_owner_vhca_id);

	if (attr->match_level == MLX5_MATCH_NONE)
		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
	else
		spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS |
					      MLX5_MATCH_MISC_PARAMETERS;

	rule = mlx5_add_flow_rules(esw->fdb_table.offloads.fast_fdb, spec, &flow_act, dest, i);

	if (!IS_ERR(rule))
		esw->offloads.num_flows++;

	return rule;
}

198 199 200 201 202 203 204
void
mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
				struct mlx5_flow_handle *rule,
				struct mlx5_esw_flow_attr *attr)
{
	struct mlx5_fc *counter = NULL;

205 206 207 208
	counter = mlx5_flow_rule_counter(rule);
	mlx5_del_flow_rules(rule);
	mlx5_fc_destroy(esw->dev, counter);
	esw->offloads.num_flows--;
209 210
}

211 212 213 214 215 216 217 218
static int esw_set_global_vlan_pop(struct mlx5_eswitch *esw, u8 val)
{
	struct mlx5_eswitch_rep *rep;
	int vf_vport, err = 0;

	esw_debug(esw->dev, "%s applying global %s policy\n", __func__, val ? "pop" : "none");
	for (vf_vport = 1; vf_vport < esw->enabled_vports; vf_vport++) {
		rep = &esw->offloads.vport_reps[vf_vport];
219
		if (!rep->rep_if[REP_ETH].valid)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
			continue;

		err = __mlx5_eswitch_set_vport_vlan(esw, rep->vport, 0, 0, val);
		if (err)
			goto out;
	}

out:
	return err;
}

static struct mlx5_eswitch_rep *
esw_vlan_action_get_vport(struct mlx5_esw_flow_attr *attr, bool push, bool pop)
{
	struct mlx5_eswitch_rep *in_rep, *out_rep, *vport = NULL;

	in_rep  = attr->in_rep;
237
	out_rep = attr->out_rep[0];
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

	if (push)
		vport = in_rep;
	else if (pop)
		vport = out_rep;
	else
		vport = in_rep;

	return vport;
}

static int esw_add_vlan_action_check(struct mlx5_esw_flow_attr *attr,
				     bool push, bool pop, bool fwd)
{
	struct mlx5_eswitch_rep *in_rep, *out_rep;

	if ((push || pop) && !fwd)
		goto out_notsupp;

	in_rep  = attr->in_rep;
258
	out_rep = attr->out_rep[0];
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

	if (push && in_rep->vport == FDB_UPLINK_VPORT)
		goto out_notsupp;

	if (pop && out_rep->vport == FDB_UPLINK_VPORT)
		goto out_notsupp;

	/* vport has vlan push configured, can't offload VF --> wire rules w.o it */
	if (!push && !pop && fwd)
		if (in_rep->vlan && out_rep->vport == FDB_UPLINK_VPORT)
			goto out_notsupp;

	/* protects against (1) setting rules with different vlans to push and
	 * (2) setting rules w.o vlans (attr->vlan = 0) && w. vlans to push (!= 0)
	 */
274
	if (push && in_rep->vlan_refcount && (in_rep->vlan != attr->vlan_vid[0]))
275 276 277 278 279
		goto out_notsupp;

	return 0;

out_notsupp:
280
	return -EOPNOTSUPP;
281 282 283 284 285 286 287 288 289 290
}

int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
				 struct mlx5_esw_flow_attr *attr)
{
	struct offloads_fdb *offloads = &esw->fdb_table.offloads;
	struct mlx5_eswitch_rep *vport = NULL;
	bool push, pop, fwd;
	int err = 0;

291
	/* nop if we're on the vlan push/pop non emulation mode */
292
	if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
293 294
		return 0;

295 296 297 298 299 300 301 302 303 304 305 306 307 308
	push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
	pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
	fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);

	err = esw_add_vlan_action_check(attr, push, pop, fwd);
	if (err)
		return err;

	attr->vlan_handled = false;

	vport = esw_vlan_action_get_vport(attr, push, pop);

	if (!push && !pop && fwd) {
		/* tracks VF --> wire rules without vlan push action */
309
		if (attr->out_rep[0]->vport == FDB_UPLINK_VPORT) {
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
			vport->vlan_refcount++;
			attr->vlan_handled = true;
		}

		return 0;
	}

	if (!push && !pop)
		return 0;

	if (!(offloads->vlan_push_pop_refcount)) {
		/* it's the 1st vlan rule, apply global vlan pop policy */
		err = esw_set_global_vlan_pop(esw, SET_VLAN_STRIP);
		if (err)
			goto out;
	}
	offloads->vlan_push_pop_refcount++;

	if (push) {
		if (vport->vlan_refcount)
			goto skip_set_push;

332
		err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport, attr->vlan_vid[0], 0,
333 334 335
						    SET_VLAN_INSERT | SET_VLAN_STRIP);
		if (err)
			goto out;
336
		vport->vlan = attr->vlan_vid[0];
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
skip_set_push:
		vport->vlan_refcount++;
	}
out:
	if (!err)
		attr->vlan_handled = true;
	return err;
}

int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
				 struct mlx5_esw_flow_attr *attr)
{
	struct offloads_fdb *offloads = &esw->fdb_table.offloads;
	struct mlx5_eswitch_rep *vport = NULL;
	bool push, pop, fwd;
	int err = 0;

354
	/* nop if we're on the vlan push/pop non emulation mode */
355
	if (mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
356 357
		return 0;

358 359 360 361 362 363 364 365 366 367 368
	if (!attr->vlan_handled)
		return 0;

	push = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH);
	pop  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP);
	fwd  = !!(attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST);

	vport = esw_vlan_action_get_vport(attr, push, pop);

	if (!push && !pop && fwd) {
		/* tracks VF --> wire rules without vlan push action */
369
		if (attr->out_rep[0]->vport == FDB_UPLINK_VPORT)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
			vport->vlan_refcount--;

		return 0;
	}

	if (push) {
		vport->vlan_refcount--;
		if (vport->vlan_refcount)
			goto skip_unset_push;

		vport->vlan = 0;
		err = __mlx5_eswitch_set_vport_vlan(esw, vport->vport,
						    0, 0, SET_VLAN_STRIP);
		if (err)
			goto out;
	}

skip_unset_push:
	offloads->vlan_push_pop_refcount--;
	if (offloads->vlan_push_pop_refcount)
		return 0;

	/* no more vlan rules, stop global vlan pop policy */
	err = esw_set_global_vlan_pop(esw, 0);

out:
	return err;
}

399
struct mlx5_flow_handle *
400 401
mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *esw, int vport, u32 sqn)
{
402
	struct mlx5_flow_act flow_act = {0};
403
	struct mlx5_flow_destination dest = {};
M
Mark Bloch 已提交
404
	struct mlx5_flow_handle *flow_rule;
405
	struct mlx5_flow_spec *spec;
406 407
	void *misc;

408
	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
409
	if (!spec) {
410 411 412 413
		flow_rule = ERR_PTR(-ENOMEM);
		goto out;
	}

414
	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
415 416 417
	MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
	MLX5_SET(fte_match_set_misc, misc, source_port, 0x0); /* source vport is 0 */

418
	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
419 420 421
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);

422
	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
423
	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
424
	dest.vport.num = vport;
425
	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
426

427
	flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
428
					&flow_act, &dest, 1);
429 430 431
	if (IS_ERR(flow_rule))
		esw_warn(esw->dev, "FDB: Failed to add send to vport rule err %ld\n", PTR_ERR(flow_rule));
out:
432
	kvfree(spec);
433 434
	return flow_rule;
}
435
EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
436

437 438 439 440 441
void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
{
	mlx5_del_flow_rules(rule);
}

442 443
static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
{
444
	struct mlx5_flow_act flow_act = {0};
445
	struct mlx5_flow_destination dest = {};
M
Mark Bloch 已提交
446
	struct mlx5_flow_handle *flow_rule = NULL;
447
	struct mlx5_flow_spec *spec;
448 449
	void *headers_c;
	void *headers_v;
450
	int err = 0;
451 452
	u8 *dmac_c;
	u8 *dmac_v;
453

454
	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
455
	if (!spec) {
456 457 458 459
		err = -ENOMEM;
		goto out;
	}

460 461 462 463 464 465 466
	spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
	headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
				 outer_headers);
	dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
			      outer_headers.dmac_47_16);
	dmac_c[0] = 0x01;

467
	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
468
	dest.vport.num = 0;
469
	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
470

471
	flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
472
					&flow_act, &dest, 1);
473 474
	if (IS_ERR(flow_rule)) {
		err = PTR_ERR(flow_rule);
475
		esw_warn(esw->dev,  "FDB: Failed to add unicast miss flow rule err %d\n", err);
476 477 478
		goto out;
	}

479 480 481 482 483 484 485
	esw->fdb_table.offloads.miss_rule_uni = flow_rule;

	headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
				 outer_headers);
	dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
			      outer_headers.dmac_47_16);
	dmac_v[0] = 0x01;
486
	flow_rule = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb, spec,
487 488 489 490 491 492 493 494 495 496
					&flow_act, &dest, 1);
	if (IS_ERR(flow_rule)) {
		err = PTR_ERR(flow_rule);
		esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
		mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
		goto out;
	}

	esw->fdb_table.offloads.miss_rule_multi = flow_rule;

497
out:
498
	kvfree(spec);
499 500 501
	return err;
}

502
#define ESW_OFFLOADS_NUM_GROUPS  4
503

504
static int esw_create_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
505 506 507 508
{
	struct mlx5_core_dev *dev = esw->dev;
	struct mlx5_flow_namespace *root_ns;
	struct mlx5_flow_table *fdb = NULL;
509
	int esw_size, err = 0;
510
	u32 flags = 0;
511 512
	u32 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
				MLX5_CAP_GEN(dev, max_flow_counter_15_0);
513 514 515 516

	root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
	if (!root_ns) {
		esw_warn(dev, "Failed to get FDB flow namespace\n");
517
		err = -EOPNOTSUPP;
518
		goto out_namespace;
519 520
	}

521 522
	esw_debug(dev, "Create offloads FDB table, min (max esw size(2^%d), max counters(%d)*groups(%d))\n",
		  MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size),
523
		  max_flow_counter, ESW_OFFLOADS_NUM_GROUPS);
524

525
	esw_size = min_t(int, max_flow_counter * ESW_OFFLOADS_NUM_GROUPS,
526
			 1 << MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
527

528 529 530
	if (mlx5_esw_has_fwd_fdb(dev))
		esw_size >>= 1;

531
	if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
532 533
		flags |= MLX5_FLOW_TABLE_TUNNEL_EN;

534
	fdb = mlx5_create_auto_grouped_flow_table(root_ns, FDB_FAST_PATH,
535
						  esw_size,
536
						  ESW_OFFLOADS_NUM_GROUPS, 0,
537
						  flags);
538 539
	if (IS_ERR(fdb)) {
		err = PTR_ERR(fdb);
540
		esw_warn(dev, "Failed to create Fast path FDB Table err %d\n", err);
541
		goto out_namespace;
542
	}
543
	esw->fdb_table.offloads.fast_fdb = fdb;
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	if (!mlx5_esw_has_fwd_fdb(dev))
		goto out_namespace;

	fdb = mlx5_create_auto_grouped_flow_table(root_ns, FDB_FAST_PATH,
						  esw_size,
						  ESW_OFFLOADS_NUM_GROUPS, 1,
						  flags);
	if (IS_ERR(fdb)) {
		err = PTR_ERR(fdb);
		esw_warn(dev, "Failed to create fwd table err %d\n", err);
		goto out_ft;
	}
	esw->fdb_table.offloads.fwd_fdb = fdb;

	return err;

out_ft:
	mlx5_destroy_flow_table(esw->fdb_table.offloads.fast_fdb);
out_namespace:
564 565 566 567 568
	return err;
}

static void esw_destroy_offloads_fast_fdb_table(struct mlx5_eswitch *esw)
{
569 570
	if (mlx5_esw_has_fwd_fdb(esw->dev))
		mlx5_destroy_flow_table(esw->fdb_table.offloads.fwd_fdb);
571
	mlx5_destroy_flow_table(esw->fdb_table.offloads.fast_fdb);
572 573 574
}

#define MAX_PF_SQ 256
575
#define MAX_SQ_NVPORTS 32
576 577 578 579 580 581 582 583 584 585 586 587

static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw, int nvports)
{
	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
	struct mlx5_flow_table_attr ft_attr = {};
	struct mlx5_core_dev *dev = esw->dev;
	struct mlx5_flow_namespace *root_ns;
	struct mlx5_flow_table *fdb = NULL;
	int table_size, ix, err = 0;
	struct mlx5_flow_group *g;
	void *match_criteria;
	u32 *flow_group_in;
588
	u8 *dmac;
589 590

	esw_debug(esw->dev, "Create offloads FDB Tables\n");
591
	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
592 593 594 595 596 597 598 599 600 601 602 603 604 605
	if (!flow_group_in)
		return -ENOMEM;

	root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
	if (!root_ns) {
		esw_warn(dev, "Failed to get FDB flow namespace\n");
		err = -EOPNOTSUPP;
		goto ns_err;
	}

	err = esw_create_offloads_fast_fdb_table(esw);
	if (err)
		goto fast_fdb_err;

606
	table_size = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ + 2;
607 608 609 610 611

	ft_attr.max_fte = table_size;
	ft_attr.prio = FDB_SLOW_PATH;

	fdb = mlx5_create_flow_table(root_ns, &ft_attr);
612 613 614 615 616
	if (IS_ERR(fdb)) {
		err = PTR_ERR(fdb);
		esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
		goto slow_fdb_err;
	}
617
	esw->fdb_table.offloads.slow_fdb = fdb;
618

619 620 621 622 623 624 625 626 627 628
	/* create send-to-vport group */
	memset(flow_group_in, 0, inlen);
	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
		 MLX5_MATCH_MISC_PARAMETERS);

	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);

	MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
	MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);

629
	ix = nvports * MAX_SQ_NVPORTS + MAX_PF_SQ;
630 631 632 633 634 635 636 637 638 639 640 641 642
	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix - 1);

	g = mlx5_create_flow_group(fdb, flow_group_in);
	if (IS_ERR(g)) {
		err = PTR_ERR(g);
		esw_warn(dev, "Failed to create send-to-vport flow group err(%d)\n", err);
		goto send_vport_err;
	}
	esw->fdb_table.offloads.send_to_vport_grp = g;

	/* create miss group */
	memset(flow_group_in, 0, inlen);
643 644 645 646 647 648 649
	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
		 MLX5_MATCH_OUTER_HEADERS);
	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
				      match_criteria);
	dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
			    outer_headers.dmac_47_16);
	dmac[0] = 0x01;
650 651

	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, ix);
652
	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, ix + 2);
653 654 655 656 657 658 659 660 661

	g = mlx5_create_flow_group(fdb, flow_group_in);
	if (IS_ERR(g)) {
		err = PTR_ERR(g);
		esw_warn(dev, "Failed to create miss flow group err(%d)\n", err);
		goto miss_err;
	}
	esw->fdb_table.offloads.miss_grp = g;

662 663 664 665
	err = esw_add_fdb_miss_rule(esw);
	if (err)
		goto miss_rule_err;

666
	kvfree(flow_group_in);
667 668
	return 0;

669 670
miss_rule_err:
	mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
671 672 673
miss_err:
	mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
send_vport_err:
674
	mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
675
slow_fdb_err:
676
	esw_destroy_offloads_fast_fdb_table(esw);
677
fast_fdb_err:
678 679 680 681 682
ns_err:
	kvfree(flow_group_in);
	return err;
}

683
static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
684
{
685
	if (!esw->fdb_table.offloads.fast_fdb)
686 687
		return;

688
	esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
689 690
	mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
	mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
691 692 693
	mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
	mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);

694
	mlx5_destroy_flow_table(esw->fdb_table.offloads.slow_fdb);
695
	esw_destroy_offloads_fast_fdb_table(esw);
696
}
697 698 699

static int esw_create_offloads_table(struct mlx5_eswitch *esw)
{
700
	struct mlx5_flow_table_attr ft_attr = {};
701
	struct mlx5_core_dev *dev = esw->dev;
702 703
	struct mlx5_flow_table *ft_offloads;
	struct mlx5_flow_namespace *ns;
704 705 706 707 708
	int err = 0;

	ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
	if (!ns) {
		esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
709
		return -EOPNOTSUPP;
710 711
	}

712 713 714
	ft_attr.max_fte = dev->priv.sriov.num_vfs + 2;

	ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
	if (IS_ERR(ft_offloads)) {
		err = PTR_ERR(ft_offloads);
		esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
		return err;
	}

	esw->offloads.ft_offloads = ft_offloads;
	return 0;
}

static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
{
	struct mlx5_esw_offload *offloads = &esw->offloads;

	mlx5_destroy_flow_table(offloads->ft_offloads);
}
731 732 733 734 735 736 737 738 739 740 741

static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
{
	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
	struct mlx5_flow_group *g;
	struct mlx5_priv *priv = &esw->dev->priv;
	u32 *flow_group_in;
	void *match_criteria, *misc;
	int err = 0;
	int nvports = priv->sriov.num_vfs + 2;

742
	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	if (!flow_group_in)
		return -ENOMEM;

	/* create vport rx group */
	memset(flow_group_in, 0, inlen);
	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
		 MLX5_MATCH_MISC_PARAMETERS);

	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
	misc = MLX5_ADDR_OF(fte_match_param, match_criteria, misc_parameters);
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);

	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);

	g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);

	if (IS_ERR(g)) {
		err = PTR_ERR(g);
		mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
		goto out;
	}

	esw->offloads.vport_rx_group = g;
out:
768
	kvfree(flow_group_in);
769 770 771 772 773 774 775 776
	return err;
}

static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
{
	mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
}

M
Mark Bloch 已提交
777
struct mlx5_flow_handle *
778 779
mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn)
{
780
	struct mlx5_flow_act flow_act = {0};
781
	struct mlx5_flow_destination dest = {};
M
Mark Bloch 已提交
782
	struct mlx5_flow_handle *flow_rule;
783
	struct mlx5_flow_spec *spec;
784 785
	void *misc;

786
	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
787
	if (!spec) {
788 789 790 791
		flow_rule = ERR_PTR(-ENOMEM);
		goto out;
	}

792
	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
793 794
	MLX5_SET(fte_match_set_misc, misc, source_port, vport);

795
	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
796 797
	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);

798
	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
799 800 801
	dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
	dest.tir_num = tirn;

802
	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
M
Mark Bloch 已提交
803
	flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
804
					&flow_act, &dest, 1);
805 806 807 808 809 810
	if (IS_ERR(flow_rule)) {
		esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
		goto out;
	}

out:
811
	kvfree(spec);
812 813
	return flow_rule;
}
O
Or Gerlitz 已提交
814

815 816
static int esw_offloads_start(struct mlx5_eswitch *esw)
{
817
	int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
818 819 820 821 822 823 824 825

	if (esw->mode != SRIOV_LEGACY) {
		esw_warn(esw->dev, "Can't set offloads mode, SRIOV legacy not enabled\n");
		return -EINVAL;
	}

	mlx5_eswitch_disable_sriov(esw);
	err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
826 827 828 829
	if (err) {
		esw_warn(esw->dev, "Failed setting eswitch to offloads, err %d\n", err);
		err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
		if (err1)
830
			esw_warn(esw->dev, "Failed setting eswitch back to legacy, err %d\n", err1);
831
	}
832 833 834 835 836 837 838 839
	if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
		if (mlx5_eswitch_inline_mode_get(esw,
						 num_vfs,
						 &esw->offloads.inline_mode)) {
			esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
			esw_warn(esw->dev, "Inline mode is different between vports\n");
		}
	}
840 841 842
	return err;
}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
{
	kfree(esw->offloads.vport_reps);
}

int esw_offloads_init_reps(struct mlx5_eswitch *esw)
{
	int total_vfs = MLX5_TOTAL_VPORTS(esw->dev);
	struct mlx5_core_dev *dev = esw->dev;
	struct mlx5_esw_offload *offloads;
	struct mlx5_eswitch_rep *rep;
	u8 hw_id[ETH_ALEN];
	int vport;

	esw->offloads.vport_reps = kcalloc(total_vfs,
					   sizeof(struct mlx5_eswitch_rep),
					   GFP_KERNEL);
	if (!esw->offloads.vport_reps)
		return -ENOMEM;

	offloads = &esw->offloads;
	mlx5_query_nic_vport_mac_address(dev, 0, hw_id);

	for (vport = 0; vport < total_vfs; vport++) {
		rep = &offloads->vport_reps[vport];

		rep->vport = vport;
		ether_addr_copy(rep->hw_id, hw_id);
	}

	offloads->vport_reps[0].vport = FDB_UPLINK_VPORT;

	return 0;
}

878 879
static void esw_offloads_unload_reps_type(struct mlx5_eswitch *esw, int nvports,
					  u8 rep_type)
880 881 882 883 884 885
{
	struct mlx5_eswitch_rep *rep;
	int vport;

	for (vport = nvports - 1; vport >= 0; vport--) {
		rep = &esw->offloads.vport_reps[vport];
886
		if (!rep->rep_if[rep_type].valid)
887 888
			continue;

889
		rep->rep_if[rep_type].unload(rep);
890 891 892
	}
}

893 894 895 896 897 898 899 900 901 902
static void esw_offloads_unload_reps(struct mlx5_eswitch *esw, int nvports)
{
	u8 rep_type = NUM_REP_TYPES;

	while (rep_type-- > 0)
		esw_offloads_unload_reps_type(esw, nvports, rep_type);
}

static int esw_offloads_load_reps_type(struct mlx5_eswitch *esw, int nvports,
				       u8 rep_type)
903
{
904 905
	struct mlx5_eswitch_rep *rep;
	int vport;
906 907
	int err;

908 909
	for (vport = 0; vport < nvports; vport++) {
		rep = &esw->offloads.vport_reps[vport];
910
		if (!rep->rep_if[rep_type].valid)
911 912
			continue;

913
		err = rep->rep_if[rep_type].load(esw->dev, rep);
914 915 916 917 918 919 920
		if (err)
			goto err_reps;
	}

	return 0;

err_reps:
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	esw_offloads_unload_reps_type(esw, vport, rep_type);
	return err;
}

static int esw_offloads_load_reps(struct mlx5_eswitch *esw, int nvports)
{
	u8 rep_type = 0;
	int err;

	for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
		err = esw_offloads_load_reps_type(esw, nvports, rep_type);
		if (err)
			goto err_reps;
	}

	return err;

err_reps:
	while (rep_type-- > 0)
		esw_offloads_unload_reps_type(esw, nvports, rep_type);
941 942 943 944 945 946 947
	return err;
}

int esw_offloads_init(struct mlx5_eswitch *esw, int nvports)
{
	int err;

948
	err = esw_create_offloads_fdb_tables(esw, nvports);
949
	if (err)
950
		return err;
951 952 953 954 955 956 957 958 959

	err = esw_create_offloads_table(esw);
	if (err)
		goto create_ft_err;

	err = esw_create_vport_rx_group(esw);
	if (err)
		goto create_fg_err;

960 961 962
	err = esw_offloads_load_reps(esw, nvports);
	if (err)
		goto err_reps;
963

964 965
	return 0;

966 967 968
err_reps:
	esw_destroy_vport_rx_group(esw);

969 970 971 972
create_fg_err:
	esw_destroy_offloads_table(esw);

create_ft_err:
973
	esw_destroy_offloads_fdb_tables(esw);
974

975 976 977 978 979
	return err;
}

static int esw_offloads_stop(struct mlx5_eswitch *esw)
{
980
	int err, err1, num_vfs = esw->dev->priv.sriov.num_vfs;
981 982 983

	mlx5_eswitch_disable_sriov(esw);
	err = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_LEGACY);
984 985 986 987 988 989
	if (err) {
		esw_warn(esw->dev, "Failed setting eswitch to legacy, err %d\n", err);
		err1 = mlx5_eswitch_enable_sriov(esw, num_vfs, SRIOV_OFFLOADS);
		if (err1)
			esw_warn(esw->dev, "Failed setting eswitch back to offloads, err %d\n", err);
	}
990

991
	/* enable back PF RoCE */
992
	mlx5_reload_interface(esw->dev, MLX5_INTERFACE_PROTOCOL_IB);
993

994 995 996 997 998
	return err;
}

void esw_offloads_cleanup(struct mlx5_eswitch *esw, int nvports)
{
999
	esw_offloads_unload_reps(esw, nvports);
1000 1001
	esw_destroy_vport_rx_group(esw);
	esw_destroy_offloads_table(esw);
1002
	esw_destroy_offloads_fdb_tables(esw);
1003 1004
}

1005
static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
{
	switch (mode) {
	case DEVLINK_ESWITCH_MODE_LEGACY:
		*mlx5_mode = SRIOV_LEGACY;
		break;
	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
		*mlx5_mode = SRIOV_OFFLOADS;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
{
	switch (mlx5_mode) {
	case SRIOV_LEGACY:
		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
		break;
	case SRIOV_OFFLOADS:
		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
{
	switch (mode) {
	case DEVLINK_ESWITCH_INLINE_MODE_NONE:
		*mlx5_mode = MLX5_INLINE_MODE_NONE;
		break;
	case DEVLINK_ESWITCH_INLINE_MODE_LINK:
		*mlx5_mode = MLX5_INLINE_MODE_L2;
		break;
	case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
		*mlx5_mode = MLX5_INLINE_MODE_IP;
		break;
	case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
		*mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
{
	switch (mlx5_mode) {
	case MLX5_INLINE_MODE_NONE:
		*mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
		break;
	case MLX5_INLINE_MODE_L2:
		*mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
		break;
	case MLX5_INLINE_MODE_IP:
		*mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
		break;
	case MLX5_INLINE_MODE_TCP_UDP:
		*mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

1081
static int mlx5_devlink_eswitch_check(struct devlink *devlink)
O
Or Gerlitz 已提交
1082
{
1083
	struct mlx5_core_dev *dev = devlink_priv(devlink);
1084

1085 1086
	if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
		return -EOPNOTSUPP;
1087

1088 1089
	if(!MLX5_ESWITCH_MANAGER(dev))
		return -EPERM;
1090

1091
	if (dev->priv.eswitch->mode == SRIOV_NONE)
1092 1093
		return -EOPNOTSUPP;

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	return 0;
}

int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
{
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	u16 cur_mlx5_mode, mlx5_mode = 0;
	int err;

	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;

	cur_mlx5_mode = dev->priv.eswitch->mode;

1109
	if (esw_mode_from_devlink(mode, &mlx5_mode))
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
		return -EINVAL;

	if (cur_mlx5_mode == mlx5_mode)
		return 0;

	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
		return esw_offloads_start(dev->priv.eswitch);
	else if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
		return esw_offloads_stop(dev->priv.eswitch);
	else
		return -EINVAL;
O
Or Gerlitz 已提交
1121 1122 1123 1124
}

int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
{
1125 1126
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	int err;
1127

1128 1129 1130
	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;
1131

1132
	return esw_mode_to_devlink(dev->priv.eswitch->mode, mode);
O
Or Gerlitz 已提交
1133
}
1134

1135 1136 1137 1138
int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode)
{
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	struct mlx5_eswitch *esw = dev->priv.eswitch;
1139
	int err, vport;
1140 1141
	u8 mlx5_mode;

1142 1143 1144
	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;
1145

1146 1147 1148 1149 1150 1151 1152
	switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
		if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE)
			return 0;
		/* fall through */
	case MLX5_CAP_INLINE_MODE_L2:
		esw_warn(dev, "Inline mode can't be set\n");
1153
		return -EOPNOTSUPP;
1154 1155 1156
	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
		break;
	}
1157

1158 1159 1160 1161 1162
	if (esw->offloads.num_flows > 0) {
		esw_warn(dev, "Can't set inline mode when flows are configured\n");
		return -EOPNOTSUPP;
	}

1163 1164 1165 1166
	err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
	if (err)
		goto out;

1167
	for (vport = 1; vport < esw->enabled_vports; vport++) {
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
		err = mlx5_modify_nic_vport_min_inline(dev, vport, mlx5_mode);
		if (err) {
			esw_warn(dev, "Failed to set min inline on vport %d\n",
				 vport);
			goto revert_inline_mode;
		}
	}

	esw->offloads.inline_mode = mlx5_mode;
	return 0;

revert_inline_mode:
	while (--vport > 0)
		mlx5_modify_nic_vport_min_inline(dev,
						 vport,
						 esw->offloads.inline_mode);
out:
	return err;
}

int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
{
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	struct mlx5_eswitch *esw = dev->priv.eswitch;
1192
	int err;
1193

1194 1195 1196
	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;
1197 1198 1199 1200 1201 1202

	return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
}

int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, int nvfs, u8 *mode)
{
1203
	u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
1204 1205 1206 1207 1208 1209 1210 1211 1212
	struct mlx5_core_dev *dev = esw->dev;
	int vport;

	if (!MLX5_CAP_GEN(dev, vport_group_manager))
		return -EOPNOTSUPP;

	if (esw->mode == SRIOV_NONE)
		return -EOPNOTSUPP;

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
		mlx5_mode = MLX5_INLINE_MODE_NONE;
		goto out;
	case MLX5_CAP_INLINE_MODE_L2:
		mlx5_mode = MLX5_INLINE_MODE_L2;
		goto out;
	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
		goto query_vports;
	}
1223

1224
query_vports:
1225 1226 1227 1228 1229 1230 1231
	for (vport = 1; vport <= nvfs; vport++) {
		mlx5_query_nic_vport_min_inline(dev, vport, &mlx5_mode);
		if (vport > 1 && prev_mlx5_mode != mlx5_mode)
			return -EINVAL;
		prev_mlx5_mode = mlx5_mode;
	}

1232
out:
1233 1234 1235 1236
	*mode = mlx5_mode;
	return 0;
}

1237 1238 1239 1240 1241 1242
int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink, u8 encap)
{
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	struct mlx5_eswitch *esw = dev->priv.eswitch;
	int err;

1243 1244 1245
	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
	    (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, encap) ||
	     !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap)))
		return -EOPNOTSUPP;

	if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC)
		return -EOPNOTSUPP;

	if (esw->mode == SRIOV_LEGACY) {
		esw->offloads.encap = encap;
		return 0;
	}

	if (esw->offloads.encap == encap)
		return 0;

	if (esw->offloads.num_flows > 0) {
		esw_warn(dev, "Can't set encapsulation when flows are configured\n");
		return -EOPNOTSUPP;
	}

	esw_destroy_offloads_fast_fdb_table(esw);

	esw->offloads.encap = encap;
	err = esw_create_offloads_fast_fdb_table(esw);
	if (err) {
		esw_warn(esw->dev, "Failed re-creating fast FDB table, err %d\n", err);
		esw->offloads.encap = !encap;
O
Or Gerlitz 已提交
1275
		(void)esw_create_offloads_fast_fdb_table(esw);
1276 1277 1278 1279 1280 1281 1282 1283
	}
	return err;
}

int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink, u8 *encap)
{
	struct mlx5_core_dev *dev = devlink_priv(devlink);
	struct mlx5_eswitch *esw = dev->priv.eswitch;
1284
	int err;
1285

1286 1287 1288
	err = mlx5_devlink_eswitch_check(devlink);
	if (err)
		return err;
1289 1290 1291 1292 1293

	*encap = esw->offloads.encap;
	return 0;
}

1294
void mlx5_eswitch_register_vport_rep(struct mlx5_eswitch *esw,
1295
				     int vport_index,
1296 1297
				     struct mlx5_eswitch_rep_if *__rep_if,
				     u8 rep_type)
1298 1299
{
	struct mlx5_esw_offload *offloads = &esw->offloads;
1300
	struct mlx5_eswitch_rep_if *rep_if;
1301

1302
	rep_if = &offloads->vport_reps[vport_index].rep_if[rep_type];
1303

1304 1305
	rep_if->load   = __rep_if->load;
	rep_if->unload = __rep_if->unload;
1306
	rep_if->get_proto_dev = __rep_if->get_proto_dev;
1307
	rep_if->priv = __rep_if->priv;
1308

1309
	rep_if->valid = true;
1310
}
1311
EXPORT_SYMBOL(mlx5_eswitch_register_vport_rep);
1312 1313

void mlx5_eswitch_unregister_vport_rep(struct mlx5_eswitch *esw,
1314
				       int vport_index, u8 rep_type)
1315 1316
{
	struct mlx5_esw_offload *offloads = &esw->offloads;
1317 1318
	struct mlx5_eswitch_rep *rep;

1319
	rep = &offloads->vport_reps[vport_index];
1320

1321
	if (esw->mode == SRIOV_OFFLOADS && esw->vports[vport_index].enabled)
1322
		rep->rep_if[rep_type].unload(rep);
1323

1324
	rep->rep_if[rep_type].valid = false;
1325
}
1326
EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_rep);
1327

1328
void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
1329 1330 1331 1332 1333 1334
{
#define UPLINK_REP_INDEX 0
	struct mlx5_esw_offload *offloads = &esw->offloads;
	struct mlx5_eswitch_rep *rep;

	rep = &offloads->vport_reps[UPLINK_REP_INDEX];
1335
	return rep->rep_if[rep_type].priv;
1336
}
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354

void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
				 int vport,
				 u8 rep_type)
{
	struct mlx5_esw_offload *offloads = &esw->offloads;
	struct mlx5_eswitch_rep *rep;

	if (vport == FDB_UPLINK_VPORT)
		vport = UPLINK_REP_INDEX;

	rep = &offloads->vport_reps[vport];

	if (rep->rep_if[rep_type].valid &&
	    rep->rep_if[rep_type].get_proto_dev)
		return rep->rep_if[rep_type].get_proto_dev(rep);
	return NULL;
}
1355
EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
1356 1357 1358 1359 1360

void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
{
	return mlx5_eswitch_get_proto_dev(esw, UPLINK_REP_INDEX, rep_type);
}
1361 1362 1363 1364 1365 1366 1367 1368
EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);

struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
						int vport)
{
	return &esw->offloads.vport_reps[vport];
}
EXPORT_SYMBOL(mlx5_eswitch_vport_rep);