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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2024 NVIDIA Corporation & Affiliates */

#include "internal.h"

static void hws_rule_skip(struct mlx5hws_matcher *matcher,
			  struct mlx5hws_match_template *mt,
			  u32 flow_source,
			  bool *skip_rx, bool *skip_tx)
{
	/* By default FDB rules are added to both RX and TX */
	*skip_rx = false;
	*skip_tx = false;

	if (flow_source == MLX5_FLOW_CONTEXT_FLOW_SOURCE_LOCAL_VPORT) {
		*skip_rx = true;
	} else if (flow_source == MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK) {
		*skip_tx = true;
	} else {
		/* If no flow source was set for current rule,
		 * check for flow source in matcher attributes.
		 */
		if (matcher->attr.optimize_flow_src) {
			*skip_tx =
				matcher->attr.optimize_flow_src == MLX5HWS_MATCHER_FLOW_SRC_WIRE;
			*skip_rx =
				matcher->attr.optimize_flow_src == MLX5HWS_MATCHER_FLOW_SRC_VPORT;
			return;
		}
	}
}

static void
hws_rule_update_copy_tag(struct mlx5hws_rule *rule,
			 struct mlx5hws_wqe_gta_data_seg_ste *wqe_data,
			 bool is_jumbo)
{
	struct mlx5hws_rule_match_tag *tag;

	if (!mlx5hws_matcher_is_resizable(rule->matcher)) {
		tag = &rule->tag;
	} else {
		struct mlx5hws_wqe_gta_data_seg_ste *data_seg =
			(struct mlx5hws_wqe_gta_data_seg_ste *)(void *)rule->resize_info->data_seg;
		tag = (struct mlx5hws_rule_match_tag *)(void *)data_seg->action;
	}

	if (is_jumbo)
		memcpy(wqe_data->jumbo, tag->jumbo, MLX5HWS_JUMBO_TAG_SZ);
	else
		memcpy(wqe_data->tag, tag->match, MLX5HWS_MATCH_TAG_SZ);
}

static void hws_rule_init_dep_wqe(struct mlx5hws_send_ring_dep_wqe *dep_wqe,
				  struct mlx5hws_rule *rule,
				  struct mlx5hws_match_template *mt,
				  struct mlx5hws_rule_attr *attr)
{
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_table *tbl = matcher->tbl;
	bool skip_rx, skip_tx;

	dep_wqe->rule = rule;
	dep_wqe->user_data = attr->user_data;
	dep_wqe->direct_index = mlx5hws_matcher_is_insert_by_idx(matcher) ?
				attr->rule_idx : 0;

	if (tbl->type == MLX5HWS_TABLE_TYPE_FDB) {
		hws_rule_skip(matcher, mt, attr->flow_source, &skip_rx, &skip_tx);

		if (!skip_rx) {
			dep_wqe->rtc_0 = matcher->match_ste.rtc_0_id;
			dep_wqe->retry_rtc_0 = matcher->col_matcher ?
					       matcher->col_matcher->match_ste.rtc_0_id : 0;
		} else {
			dep_wqe->rtc_0 = 0;
			dep_wqe->retry_rtc_0 = 0;
		}

		if (!skip_tx) {
			dep_wqe->rtc_1 = matcher->match_ste.rtc_1_id;
			dep_wqe->retry_rtc_1 = matcher->col_matcher ?
					       matcher->col_matcher->match_ste.rtc_1_id : 0;
		} else {
			dep_wqe->rtc_1 = 0;
			dep_wqe->retry_rtc_1 = 0;
		}
	} else {
		pr_warn("HWS: invalid tbl->type: %d\n", tbl->type);
	}
}

static void hws_rule_move_get_rtc(struct mlx5hws_rule *rule,
				  struct mlx5hws_send_ste_attr *ste_attr)
{
	struct mlx5hws_matcher *dst_matcher = rule->matcher->resize_dst;

	if (rule->resize_info->rtc_0) {
		ste_attr->rtc_0 = dst_matcher->match_ste.rtc_0_id;
		ste_attr->retry_rtc_0 = dst_matcher->col_matcher ?
					dst_matcher->col_matcher->match_ste.rtc_0_id : 0;
	}
	if (rule->resize_info->rtc_1) {
		ste_attr->rtc_1 = dst_matcher->match_ste.rtc_1_id;
		ste_attr->retry_rtc_1 = dst_matcher->col_matcher ?
					dst_matcher->col_matcher->match_ste.rtc_1_id : 0;
	}
}

static void hws_rule_gen_comp(struct mlx5hws_send_engine *queue,
			      struct mlx5hws_rule *rule,
			      bool err,
			      void *user_data,
			      enum mlx5hws_rule_status rule_status_on_succ)
{
	enum mlx5hws_flow_op_status comp_status;

	if (!err) {
		comp_status = MLX5HWS_FLOW_OP_SUCCESS;
		rule->status = rule_status_on_succ;
	} else {
		comp_status = MLX5HWS_FLOW_OP_ERROR;
		rule->status = MLX5HWS_RULE_STATUS_FAILED;
	}

	mlx5hws_send_engine_inc_rule(queue);
	mlx5hws_send_engine_gen_comp(queue, user_data, comp_status);
}

static void
hws_rule_save_resize_info(struct mlx5hws_rule *rule,
			  struct mlx5hws_send_ste_attr *ste_attr)
{
	if (!mlx5hws_matcher_is_resizable(rule->matcher))
		return;

	/* resize_info might already exist (if we're in update flow) */
	if (likely(!rule->resize_info)) {
		rule->resize_info = kzalloc(sizeof(*rule->resize_info), GFP_KERNEL);
		if (unlikely(!rule->resize_info)) {
			pr_warn("HWS: resize info isn't allocated for rule\n");
			return;
		}
	}

	memcpy(rule->resize_info->ctrl_seg, ste_attr->wqe_ctrl,
	       sizeof(rule->resize_info->ctrl_seg));
	memcpy(rule->resize_info->data_seg, ste_attr->wqe_data,
	       sizeof(rule->resize_info->data_seg));
}

void mlx5hws_rule_clear_resize_info(struct mlx5hws_rule *rule)
{
	if (mlx5hws_matcher_is_resizable(rule->matcher) &&
	    rule->resize_info) {
		kfree(rule->resize_info);
		rule->resize_info = NULL;
	}
}

static void
hws_rule_save_delete_info(struct mlx5hws_rule *rule,
			  struct mlx5hws_send_ste_attr *ste_attr)
{
	struct mlx5hws_match_template *mt = rule->matcher->mt;
	bool is_jumbo = mlx5hws_matcher_mt_is_jumbo(mt);

	if (mlx5hws_matcher_is_resizable(rule->matcher))
		return;

	if (is_jumbo)
		memcpy(&rule->tag.jumbo, ste_attr->wqe_data->jumbo, MLX5HWS_JUMBO_TAG_SZ);
	else
		memcpy(&rule->tag.match, ste_attr->wqe_data->tag, MLX5HWS_MATCH_TAG_SZ);
}

static void
hws_rule_clear_delete_info(struct mlx5hws_rule *rule)
{
	/* nothing to do here */
}

static void
hws_rule_load_delete_info(struct mlx5hws_rule *rule,
			  struct mlx5hws_send_ste_attr *ste_attr)
{
	if (unlikely(!mlx5hws_matcher_is_resizable(rule->matcher))) {
		ste_attr->wqe_tag = &rule->tag;
	} else {
		struct mlx5hws_wqe_gta_data_seg_ste *data_seg =
			(struct mlx5hws_wqe_gta_data_seg_ste *)(void *)rule->resize_info->data_seg;
		struct mlx5hws_rule_match_tag *tag =
			(struct mlx5hws_rule_match_tag *)(void *)data_seg->action;
		ste_attr->wqe_tag = tag;
	}
}

static int hws_rule_alloc_action_ste(struct mlx5hws_rule *rule)
{
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_matcher_action_ste *action_ste;
	struct mlx5hws_pool_chunk ste = {0};
	int ret;

	action_ste = &matcher->action_ste;
	ste.order = ilog2(roundup_pow_of_two(action_ste->max_stes));
	ret = mlx5hws_pool_chunk_alloc(action_ste->pool, &ste);
	if (unlikely(ret)) {
		mlx5hws_err(matcher->tbl->ctx,
			    "Failed to allocate STE for rule actions");
		return ret;
	}

	rule->action_ste.pool = matcher->action_ste.pool;
	rule->action_ste.num_stes = matcher->action_ste.max_stes;
	rule->action_ste.index = ste.offset;

	return 0;
}

void mlx5hws_rule_free_action_ste(struct mlx5hws_rule_action_ste_info *action_ste)
{
	struct mlx5hws_pool_chunk ste = {0};

	if (!action_ste->num_stes)
		return;

	ste.order = ilog2(roundup_pow_of_two(action_ste->num_stes));
	ste.offset = action_ste->index;

	/* This release is safe only when the rule match STE was deleted
	 * (when the rule is being deleted) or replaced with the new STE that
	 * isn't pointing to old action STEs (when the rule is being updated).
	 */
	mlx5hws_pool_chunk_free(action_ste->pool, &ste);
}

static void hws_rule_create_init(struct mlx5hws_rule *rule,
				 struct mlx5hws_send_ste_attr *ste_attr,
				 struct mlx5hws_actions_apply_data *apply,
				 bool is_update)
{
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_table *tbl = matcher->tbl;
	struct mlx5hws_context *ctx = tbl->ctx;

	/* Init rule before reuse */
	if (!is_update) {
		/* In update we use these rtc's */
		rule->rtc_0 = 0;
		rule->rtc_1 = 0;

		rule->action_ste.pool = NULL;
		rule->action_ste.num_stes = 0;
		rule->action_ste.index = -1;

		rule->status = MLX5HWS_RULE_STATUS_CREATING;
	} else {
		rule->status = MLX5HWS_RULE_STATUS_UPDATING;
	}

	/* Initialize the old action STE info - shallow-copy action_ste.
	 * In create flow this will set old_action_ste fields to initial values.
	 * In update flow this will save the existing action STE info,
	 * so that we will later use it to free old STEs.
	 */
	rule->old_action_ste = rule->action_ste;

	rule->pending_wqes = 0;

	/* Init default send STE attributes */
	ste_attr->gta_opcode = MLX5HWS_WQE_GTA_OP_ACTIVATE;
	ste_attr->send_attr.opmod = MLX5HWS_WQE_GTA_OPMOD_STE;
	ste_attr->send_attr.opcode = MLX5HWS_WQE_OPCODE_TBL_ACCESS;
	ste_attr->send_attr.len = MLX5HWS_WQE_SZ_GTA_CTRL + MLX5HWS_WQE_SZ_GTA_DATA;

	/* Init default action apply */
	apply->tbl_type = tbl->type;
	apply->common_res = &ctx->common_res;
	apply->jump_to_action_stc = matcher->action_ste.stc.offset;
	apply->require_dep = 0;
}

static void hws_rule_move_init(struct mlx5hws_rule *rule,
			       struct mlx5hws_rule_attr *attr)
{
	/* Save the old RTC IDs to be later used in match STE delete */
	rule->resize_info->rtc_0 = rule->rtc_0;
	rule->resize_info->rtc_1 = rule->rtc_1;
	rule->resize_info->rule_idx = attr->rule_idx;

	rule->rtc_0 = 0;
	rule->rtc_1 = 0;

	rule->pending_wqes = 0;
	rule->status = MLX5HWS_RULE_STATUS_CREATING;
	rule->resize_info->state = MLX5HWS_RULE_RESIZE_STATE_WRITING;
}

bool mlx5hws_rule_move_in_progress(struct mlx5hws_rule *rule)
{
	return mlx5hws_matcher_is_in_resize(rule->matcher) &&
	       rule->resize_info &&
	       rule->resize_info->state != MLX5HWS_RULE_RESIZE_STATE_IDLE;
}

static int hws_rule_create_hws(struct mlx5hws_rule *rule,
			       struct mlx5hws_rule_attr *attr,
			       u8 mt_idx,
			       u32 *match_param,
			       u8 at_idx,
			       struct mlx5hws_rule_action rule_actions[])
{
	struct mlx5hws_action_template *at = &rule->matcher->at[at_idx];
	struct mlx5hws_match_template *mt = &rule->matcher->mt[mt_idx];
	bool is_jumbo = mlx5hws_matcher_mt_is_jumbo(mt);
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_context *ctx = matcher->tbl->ctx;
	struct mlx5hws_send_ste_attr ste_attr = {0};
	struct mlx5hws_send_ring_dep_wqe *dep_wqe;
	struct mlx5hws_actions_wqe_setter *setter;
	struct mlx5hws_actions_apply_data apply;
	struct mlx5hws_send_engine *queue;
	u8 total_stes, action_stes;
	bool is_update;
	int i, ret;

	is_update = !match_param;

	setter = &at->setters[at->num_of_action_stes];
	total_stes = at->num_of_action_stes + (is_jumbo && !at->only_term);
	action_stes = total_stes - 1;

	queue = &ctx->send_queue[attr->queue_id];
	if (unlikely(mlx5hws_send_engine_err(queue)))
		return -EIO;

	hws_rule_create_init(rule, &ste_attr, &apply, is_update);

	/* Allocate dependent match WQE since rule might have dependent writes.
	 * The queued dependent WQE can be later aborted or kept as a dependency.
	 * dep_wqe buffers (ctrl, data) are also reused for all STE writes.
	 */
	dep_wqe = mlx5hws_send_add_new_dep_wqe(queue);
	hws_rule_init_dep_wqe(dep_wqe, rule, mt, attr);

	ste_attr.wqe_ctrl = &dep_wqe->wqe_ctrl;
	ste_attr.wqe_data = &dep_wqe->wqe_data;
	apply.wqe_ctrl = &dep_wqe->wqe_ctrl;
	apply.wqe_data = (__force __be32 *)&dep_wqe->wqe_data;
	apply.rule_action = rule_actions;
	apply.queue = queue;

	if (action_stes) {
		/* Allocate action STEs for rules that need more than match STE */
		ret = hws_rule_alloc_action_ste(rule);
		if (ret) {
			mlx5hws_err(ctx, "Failed to allocate action memory %d", ret);
			mlx5hws_send_abort_new_dep_wqe(queue);
			return ret;
		}
		/* Skip RX/TX based on the dep_wqe init */
		ste_attr.rtc_0 = dep_wqe->rtc_0 ? matcher->action_ste.rtc_0_id : 0;
		ste_attr.rtc_1 = dep_wqe->rtc_1 ? matcher->action_ste.rtc_1_id : 0;
		/* Action STEs are written to a specific index last to first */
		ste_attr.direct_index = rule->action_ste.index + action_stes;
		apply.next_direct_idx = ste_attr.direct_index;
	} else {
		apply.next_direct_idx = 0;
	}

	for (i = total_stes; i-- > 0;) {
		mlx5hws_action_apply_setter(&apply, setter--, !i && is_jumbo);

		if (i == 0) {
			/* Handle last match STE.
			 * For hash split / linear lookup RTCs, packets reaching any STE
			 * will always match and perform the specified actions, which
			 * makes the tag irrelevant.
			 */
			if (likely(!mlx5hws_matcher_is_insert_by_idx(matcher) && !is_update))
				mlx5hws_definer_create_tag(match_param, mt->fc, mt->fc_sz,
							   (u8 *)dep_wqe->wqe_data.action);
			else if (is_update)
				hws_rule_update_copy_tag(rule, &dep_wqe->wqe_data, is_jumbo);

			/* Rule has dependent WQEs, match dep_wqe is queued */
			if (action_stes || apply.require_dep)
				break;

			/* Rule has no dependencies, abort dep_wqe and send WQE now */
			mlx5hws_send_abort_new_dep_wqe(queue);
			ste_attr.wqe_tag_is_jumbo = is_jumbo;
			ste_attr.send_attr.notify_hw = !attr->burst;
			ste_attr.send_attr.user_data = dep_wqe->user_data;
			ste_attr.send_attr.rule = dep_wqe->rule;
			ste_attr.rtc_0 = dep_wqe->rtc_0;
			ste_attr.rtc_1 = dep_wqe->rtc_1;
			ste_attr.used_id_rtc_0 = &rule->rtc_0;
			ste_attr.used_id_rtc_1 = &rule->rtc_1;
			ste_attr.retry_rtc_0 = dep_wqe->retry_rtc_0;
			ste_attr.retry_rtc_1 = dep_wqe->retry_rtc_1;
			ste_attr.direct_index = dep_wqe->direct_index;
		} else {
			apply.next_direct_idx = --ste_attr.direct_index;
		}

		mlx5hws_send_ste(queue, &ste_attr);
	}

	/* Backup TAG on the rule for deletion and resize info for
	 * moving rules to a new matcher, only after insertion.
	 */
	if (!is_update)
		hws_rule_save_delete_info(rule, &ste_attr);

	hws_rule_save_resize_info(rule, &ste_attr);
	mlx5hws_send_engine_inc_rule(queue);

	if (!attr->burst)
		mlx5hws_send_all_dep_wqe(queue);

	return 0;
}

static void hws_rule_destroy_failed_hws(struct mlx5hws_rule *rule,
					struct mlx5hws_rule_attr *attr)
{
	struct mlx5hws_context *ctx = rule->matcher->tbl->ctx;
	struct mlx5hws_send_engine *queue;

	queue = &ctx->send_queue[attr->queue_id];

	hws_rule_gen_comp(queue, rule, false,
			  attr->user_data, MLX5HWS_RULE_STATUS_DELETED);

	/* Rule failed now we can safely release action STEs */
	mlx5hws_rule_free_action_ste(&rule->action_ste);

	/* Perhaps the rule failed updating - release old action STEs as well */
	mlx5hws_rule_free_action_ste(&rule->old_action_ste);

	/* Clear complex tag */
	hws_rule_clear_delete_info(rule);

	/* Clear info that was saved for resizing */
	mlx5hws_rule_clear_resize_info(rule);

	/* If a rule that was indicated as burst (need to trigger HW) has failed
	 * insertion we won't ring the HW as nothing is being written to the WQ.
	 * In such case update the last WQE and ring the HW with that work
	 */
	if (attr->burst)
		return;

	mlx5hws_send_all_dep_wqe(queue);
	mlx5hws_send_engine_flush_queue(queue);
}

static int hws_rule_destroy_hws(struct mlx5hws_rule *rule,
				struct mlx5hws_rule_attr *attr)
{
	bool is_jumbo = mlx5hws_matcher_mt_is_jumbo(rule->matcher->mt);
	struct mlx5hws_context *ctx = rule->matcher->tbl->ctx;
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_wqe_gta_ctrl_seg wqe_ctrl = {0};
	struct mlx5hws_send_ste_attr ste_attr = {0};
	struct mlx5hws_send_engine *queue;

	queue = &ctx->send_queue[attr->queue_id];

	if (unlikely(mlx5hws_send_engine_err(queue))) {
		hws_rule_destroy_failed_hws(rule, attr);
		return 0;
	}

	/* Rule is not completed yet */
	if (rule->status == MLX5HWS_RULE_STATUS_CREATING ||
	    rule->status == MLX5HWS_RULE_STATUS_UPDATING)
		return -EBUSY;

	/* Rule failed and doesn't require cleanup */
	if (rule->status == MLX5HWS_RULE_STATUS_FAILED) {
		hws_rule_destroy_failed_hws(rule, attr);
		return 0;
	}

	if (rule->skip_delete) {
		/* Rule shouldn't be deleted in HW.
		 * Generate completion as if write succeeded, and we can
		 * safely release action STEs and clear resize info.
		 */
		hws_rule_gen_comp(queue, rule, false,
				  attr->user_data, MLX5HWS_RULE_STATUS_DELETED);

		mlx5hws_rule_free_action_ste(&rule->action_ste);
		mlx5hws_rule_clear_resize_info(rule);
		return 0;
	}

	mlx5hws_send_engine_inc_rule(queue);

	/* Send dependent WQE */
	if (!attr->burst)
		mlx5hws_send_all_dep_wqe(queue);

	rule->status = MLX5HWS_RULE_STATUS_DELETING;

	ste_attr.send_attr.opmod = MLX5HWS_WQE_GTA_OPMOD_STE;
	ste_attr.send_attr.opcode = MLX5HWS_WQE_OPCODE_TBL_ACCESS;
	ste_attr.send_attr.len = MLX5HWS_WQE_SZ_GTA_CTRL + MLX5HWS_WQE_SZ_GTA_DATA;

	ste_attr.send_attr.rule = rule;
	ste_attr.send_attr.notify_hw = !attr->burst;
	ste_attr.send_attr.user_data = attr->user_data;

	ste_attr.rtc_0 = rule->rtc_0;
	ste_attr.rtc_1 = rule->rtc_1;
	ste_attr.used_id_rtc_0 = &rule->rtc_0;
	ste_attr.used_id_rtc_1 = &rule->rtc_1;
	ste_attr.wqe_ctrl = &wqe_ctrl;
	ste_attr.wqe_tag_is_jumbo = is_jumbo;
	ste_attr.gta_opcode = MLX5HWS_WQE_GTA_OP_DEACTIVATE;
	if (unlikely(mlx5hws_matcher_is_insert_by_idx(matcher)))
		ste_attr.direct_index = attr->rule_idx;

	hws_rule_load_delete_info(rule, &ste_attr);
	mlx5hws_send_ste(queue, &ste_attr);
	hws_rule_clear_delete_info(rule);

	return 0;
}

static int hws_rule_enqueue_precheck(struct mlx5hws_rule *rule,
				     struct mlx5hws_rule_attr *attr)
{
	struct mlx5hws_context *ctx = rule->matcher->tbl->ctx;

	if (unlikely(!attr->user_data))
		return -EINVAL;

	/* Check if there is room in queue */
	if (unlikely(mlx5hws_send_engine_full(&ctx->send_queue[attr->queue_id])))
		return -EBUSY;

	return 0;
}

static int hws_rule_enqueue_precheck_move(struct mlx5hws_rule *rule,
					  struct mlx5hws_rule_attr *attr)
{
	if (unlikely(rule->status != MLX5HWS_RULE_STATUS_CREATED))
		return -EINVAL;

	return hws_rule_enqueue_precheck(rule, attr);
}

static int hws_rule_enqueue_precheck_create(struct mlx5hws_rule *rule,
					    struct mlx5hws_rule_attr *attr)
{
	if (unlikely(mlx5hws_matcher_is_in_resize(rule->matcher)))
		/* Matcher in resize - new rules are not allowed */
		return -EAGAIN;

	return hws_rule_enqueue_precheck(rule, attr);
}

static int hws_rule_enqueue_precheck_update(struct mlx5hws_rule *rule,
					    struct mlx5hws_rule_attr *attr)
{
	struct mlx5hws_matcher *matcher = rule->matcher;

	if (unlikely(!mlx5hws_matcher_is_resizable(rule->matcher) &&
		     !matcher->attr.optimize_using_rule_idx &&
		     !mlx5hws_matcher_is_insert_by_idx(matcher))) {
		return -EOPNOTSUPP;
	}

	if (unlikely(rule->status != MLX5HWS_RULE_STATUS_CREATED))
		return -EBUSY;

	return hws_rule_enqueue_precheck_create(rule, attr);
}

int mlx5hws_rule_move_hws_remove(struct mlx5hws_rule *rule,
				 void *queue_ptr,
				 void *user_data)
{
	bool is_jumbo = mlx5hws_matcher_mt_is_jumbo(rule->matcher->mt);
	struct mlx5hws_wqe_gta_ctrl_seg empty_wqe_ctrl = {0};
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_send_engine *queue = queue_ptr;
	struct mlx5hws_send_ste_attr ste_attr = {0};

	mlx5hws_send_all_dep_wqe(queue);

	rule->resize_info->state = MLX5HWS_RULE_RESIZE_STATE_DELETING;

	ste_attr.send_attr.fence = 0;
	ste_attr.send_attr.opmod = MLX5HWS_WQE_GTA_OPMOD_STE;
	ste_attr.send_attr.opcode = MLX5HWS_WQE_OPCODE_TBL_ACCESS;
	ste_attr.send_attr.len = MLX5HWS_WQE_SZ_GTA_CTRL + MLX5HWS_WQE_SZ_GTA_DATA;
	ste_attr.send_attr.rule = rule;
	ste_attr.send_attr.notify_hw = 1;
	ste_attr.send_attr.user_data = user_data;
	ste_attr.rtc_0 = rule->resize_info->rtc_0;
	ste_attr.rtc_1 = rule->resize_info->rtc_1;
	ste_attr.used_id_rtc_0 = &rule->resize_info->rtc_0;
	ste_attr.used_id_rtc_1 = &rule->resize_info->rtc_1;
	ste_attr.wqe_ctrl = &empty_wqe_ctrl;
	ste_attr.wqe_tag_is_jumbo = is_jumbo;
	ste_attr.gta_opcode = MLX5HWS_WQE_GTA_OP_DEACTIVATE;

	if (unlikely(mlx5hws_matcher_is_insert_by_idx(matcher)))
		ste_attr.direct_index = rule->resize_info->rule_idx;

	hws_rule_load_delete_info(rule, &ste_attr);
	mlx5hws_send_ste(queue, &ste_attr);

	return 0;
}

int mlx5hws_rule_move_hws_add(struct mlx5hws_rule *rule,
			      struct mlx5hws_rule_attr *attr)
{
	bool is_jumbo = mlx5hws_matcher_mt_is_jumbo(rule->matcher->mt);
	struct mlx5hws_context *ctx = rule->matcher->tbl->ctx;
	struct mlx5hws_matcher *matcher = rule->matcher;
	struct mlx5hws_send_ste_attr ste_attr = {0};
	struct mlx5hws_send_engine *queue;
	int ret;

	ret = hws_rule_enqueue_precheck_move(rule, attr);
	if (unlikely(ret))
		return ret;

	queue = &ctx->send_queue[attr->queue_id];

	ret = mlx5hws_send_engine_err(queue);
	if (ret)
		return ret;

	hws_rule_move_init(rule, attr);
	hws_rule_move_get_rtc(rule, &ste_attr);

	ste_attr.send_attr.opmod = MLX5HWS_WQE_GTA_OPMOD_STE;
	ste_attr.send_attr.opcode = MLX5HWS_WQE_OPCODE_TBL_ACCESS;
	ste_attr.send_attr.len = MLX5HWS_WQE_SZ_GTA_CTRL + MLX5HWS_WQE_SZ_GTA_DATA;
	ste_attr.gta_opcode = MLX5HWS_WQE_GTA_OP_ACTIVATE;
	ste_attr.wqe_tag_is_jumbo = is_jumbo;

	ste_attr.send_attr.rule = rule;
	ste_attr.send_attr.fence = 0;
	ste_attr.send_attr.notify_hw = !attr->burst;
	ste_attr.send_attr.user_data = attr->user_data;

	ste_attr.used_id_rtc_0 = &rule->rtc_0;
	ste_attr.used_id_rtc_1 = &rule->rtc_1;
	ste_attr.wqe_ctrl = (struct mlx5hws_wqe_gta_ctrl_seg *)rule->resize_info->ctrl_seg;
	ste_attr.wqe_data = (struct mlx5hws_wqe_gta_data_seg_ste *)rule->resize_info->data_seg;
	ste_attr.direct_index = mlx5hws_matcher_is_insert_by_idx(matcher) ?
				attr->rule_idx : 0;

	mlx5hws_send_ste(queue, &ste_attr);
	mlx5hws_send_engine_inc_rule(queue);

	if (!attr->burst)
		mlx5hws_send_all_dep_wqe(queue);

	return 0;
}

int mlx5hws_rule_create(struct mlx5hws_matcher *matcher,
			u8 mt_idx,
			u32 *match_param,
			u8 at_idx,
			struct mlx5hws_rule_action rule_actions[],
			struct mlx5hws_rule_attr *attr,
			struct mlx5hws_rule *rule_handle)
{
	int ret;

	rule_handle->matcher = matcher;

	ret = hws_rule_enqueue_precheck_create(rule_handle, attr);
	if (unlikely(ret))
		return ret;

	if (unlikely(!(matcher->num_of_mt >= mt_idx) ||
		     !(matcher->num_of_at >= at_idx) ||
		     !match_param)) {
		pr_warn("HWS: Invalid rule creation parameters (MTs, ATs or match params)\n");
		return -EINVAL;
	}

	ret = hws_rule_create_hws(rule_handle,
				  attr,
				  mt_idx,
				  match_param,
				  at_idx,
				  rule_actions);

	return ret;
}

int mlx5hws_rule_destroy(struct mlx5hws_rule *rule,
			 struct mlx5hws_rule_attr *attr)
{
	int ret;

	ret = hws_rule_enqueue_precheck(rule, attr);
	if (unlikely(ret))
		return ret;

	ret = hws_rule_destroy_hws(rule, attr);

	return ret;
}

int mlx5hws_rule_action_update(struct mlx5hws_rule *rule,
			       u8 at_idx,
			       struct mlx5hws_rule_action rule_actions[],
			       struct mlx5hws_rule_attr *attr)
{
	int ret;

	ret = hws_rule_enqueue_precheck_update(rule, attr);
	if (unlikely(ret))
		return ret;

	ret = hws_rule_create_hws(rule,
				  attr,
				  0,
				  NULL,
				  at_idx,
				  rule_actions);

	return ret;
}
我懷念的 是爭吵以後還是想要愛你的衝動