summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/freescale/enetc/ntmp.c
blob: 0d83f0c9dc6eec0605945003696a7c3bbf19c2ce (plain)
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
741
742
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/*
 * NETC NTMP (NETC Table Management Protocol) 2.0 Library
 * Copyright 2025-2026 NXP
 */

#include <linux/dma-mapping.h>
#include <linux/fsl/netc_global.h>
#include <linux/iopoll.h>
#include <linux/vmalloc.h>

#include "ntmp_private.h"

#define NETC_CBDR_TIMEOUT		1000 /* us */
#define NETC_CBDR_DELAY_US		10
#define NETC_CBDR_MR_EN			BIT(31)

#define NTMP_BASE_ADDR_ALIGN		128
#define NTMP_DATA_ADDR_ALIGN		32

/* Define NTMP Table ID */
#define NTMP_MAFT_ID			1
#define NTMP_RSST_ID			3
#define NTMP_IPFT_ID			13
#define NTMP_FDBT_ID			15
#define NTMP_VFT_ID			18
#define NTMP_ETT_ID			33
#define NTMP_ECT_ID			39
#define NTMP_BPT_ID			41

/* Generic Update Actions for most tables */
#define NTMP_GEN_UA_CFGEU		BIT(0)
#define NTMP_GEN_UA_STSEU		BIT(1)

/* Specific Update Actions for some tables */
#define FDBT_UA_ACTEU			BIT(1)
#define ECT_UA_STSEU			BIT(0)
#define BPT_UA_BPSEU			BIT(1)

/* Query Action: 0: Full query. 1: Query entry ID, the fields after entry
 * ID are not returned.
 */
#define NTMP_QA_ENTRY_ID		1

#define NTMP_ENTRY_ID_SIZE		4
#define RSST_ENTRY_NUM			64
#define RSST_STSE_DATA_SIZE(n)		((n) * 8)
#define RSST_CFGE_DATA_SIZE(n)		(n)

u32 ntmp_lookup_free_eid(unsigned long *bitmap, u32 size)
{
	u32 entry_id;

	entry_id = find_first_zero_bit(bitmap, size);
	if (entry_id == size)
		return NTMP_NULL_ENTRY_ID;

	/* Set the bit once we found it */
	__set_bit(entry_id, bitmap);

	return entry_id;
}
EXPORT_SYMBOL_GPL(ntmp_lookup_free_eid);

void ntmp_clear_eid_bitmap(unsigned long *bitmap, u32 entry_id)
{
	if (entry_id == NTMP_NULL_ENTRY_ID)
		return;

	__clear_bit(entry_id, bitmap);
}
EXPORT_SYMBOL_GPL(ntmp_clear_eid_bitmap);

int ntmp_init_cbdr(struct netc_cbdr *cbdr, struct device *dev,
		   const struct netc_cbdr_regs *regs)
{
	int cbd_num = NETC_CBDR_BD_NUM;
	size_t size;

	size = cbd_num * sizeof(union netc_cbd) + NTMP_BASE_ADDR_ALIGN;
	cbdr->addr_base = dma_alloc_coherent(dev, size, &cbdr->dma_base,
					     GFP_KERNEL);
	if (!cbdr->addr_base)
		return -ENOMEM;

	cbdr->swcbd = vcalloc(cbd_num, sizeof(struct netc_swcbd));
	if (!cbdr->swcbd) {
		dma_free_coherent(dev, size, cbdr->addr_base, cbdr->dma_base);
		return -ENOMEM;
	}

	cbdr->dma_size = size;
	cbdr->bd_num = cbd_num;
	cbdr->regs = *regs;
	cbdr->dev = dev;

	/* The base address of the Control BD Ring must be 128 bytes aligned */
	cbdr->dma_base_align =  ALIGN(cbdr->dma_base,  NTMP_BASE_ADDR_ALIGN);
	cbdr->addr_base_align = PTR_ALIGN(cbdr->addr_base,
					  NTMP_BASE_ADDR_ALIGN);

	mutex_init(&cbdr->ring_lock);

	cbdr->next_to_use = netc_read(cbdr->regs.pir);
	cbdr->next_to_clean = netc_read(cbdr->regs.cir) & NETC_CBDRCIR_INDEX;

	/* Step 1: Configure the base address of the Control BD Ring */
	netc_write(cbdr->regs.bar0, lower_32_bits(cbdr->dma_base_align));
	netc_write(cbdr->regs.bar1, upper_32_bits(cbdr->dma_base_align));

	/* Step 2: Configure the number of BDs of the Control BD Ring */
	netc_write(cbdr->regs.lenr, cbdr->bd_num);

	/* Step 3: Enable the Control BD Ring */
	netc_write(cbdr->regs.mr, NETC_CBDR_MR_EN);

	return 0;
}
EXPORT_SYMBOL_GPL(ntmp_init_cbdr);

static void ntmp_free_data_mem(struct device *dev, struct netc_swcbd *swcbd)
{
	if (unlikely(!swcbd->buf))
		return;

	dma_free_coherent(dev, swcbd->size + NTMP_DATA_ADDR_ALIGN,
			  swcbd->buf, swcbd->dma);
}

void ntmp_free_cbdr(struct netc_cbdr *cbdr)
{
	/* Disable the Control BD Ring */
	netc_write(cbdr->regs.mr, 0);

	for (int i = 0; i < cbdr->bd_num; i++)
		ntmp_free_data_mem(cbdr->dev, &cbdr->swcbd[i]);

	vfree(cbdr->swcbd);
	dma_free_coherent(cbdr->dev, cbdr->dma_size, cbdr->addr_base,
			  cbdr->dma_base);
	memset(cbdr, 0, sizeof(*cbdr));
}
EXPORT_SYMBOL_GPL(ntmp_free_cbdr);

static int ntmp_get_free_cbd_num(struct netc_cbdr *cbdr)
{
	return (cbdr->next_to_clean - cbdr->next_to_use - 1 +
		cbdr->bd_num) % cbdr->bd_num;
}

static union netc_cbd *ntmp_get_cbd(struct netc_cbdr *cbdr, int index)
{
	return &((union netc_cbd *)(cbdr->addr_base_align))[index];
}

static void ntmp_clean_cbdr(struct netc_cbdr *cbdr)
{
	int i = cbdr->next_to_clean;

	while ((netc_read(cbdr->regs.cir) & NETC_CBDRCIR_INDEX) != i) {
		union netc_cbd *cbd = ntmp_get_cbd(cbdr, i);
		struct netc_swcbd *swcbd = &cbdr->swcbd[i];

		ntmp_free_data_mem(cbdr->dev, swcbd);
		memset(swcbd, 0, sizeof(*swcbd));
		memset(cbd, 0, sizeof(*cbd));
		i = (i + 1) % cbdr->bd_num;
	}

	dma_wmb();
	cbdr->next_to_clean = i;
}

static void ntmp_select_and_lock_cbdr(struct ntmp_user *user,
				      struct netc_cbdr **cbdr)
{
	for (int i = 0; i < user->cbdr_num; i++) {
		*cbdr = &user->ring[i];
		if (mutex_trylock(&(*cbdr)->ring_lock))
			return;
	}

	/* If all command BD rings are locked, we need to select one of
	 * them and wait for it.
	 */
	*cbdr = &user->ring[raw_smp_processor_id() % user->cbdr_num];
	mutex_lock(&(*cbdr)->ring_lock);
}

static void ntmp_unlock_cbdr(struct netc_cbdr *cbdr)
{
	mutex_unlock(&cbdr->ring_lock);
}

static int netc_xmit_ntmp_cmd(struct netc_cbdr *cbdr, union netc_cbd *cbd,
			      struct netc_swcbd *swcbd)
{
	union netc_cbd *cur_cbd;
	int i, err, used_bds;
	u16 status;
	u32 val;

	used_bds = cbdr->bd_num - ntmp_get_free_cbd_num(cbdr);
	if (unlikely(used_bds >= NETC_CBDR_CLEAN_WORK)) {
		ntmp_clean_cbdr(cbdr);
		if (unlikely(!ntmp_get_free_cbd_num(cbdr))) {
			ntmp_free_data_mem(cbdr->dev, swcbd);
			return -EBUSY;
		}
	}

	i = cbdr->next_to_use;
	cur_cbd = ntmp_get_cbd(cbdr, i);
	*cur_cbd = *cbd;
	cbdr->swcbd[i] = *swcbd;
	dma_wmb();

	/* Update producer index of both software and hardware */
	i = (i + 1) % cbdr->bd_num;
	cbdr->next_to_use = i;
	netc_write(cbdr->regs.pir, i);

	err = read_poll_timeout(netc_read, val,
				(val & NETC_CBDRCIR_INDEX) == i,
				NETC_CBDR_DELAY_US, NETC_CBDR_TIMEOUT,
				true, cbdr->regs.cir);
	if (unlikely(err))
		return err;

	if (unlikely(val & NETC_CBDRCIR_SBE)) {
		dev_err(cbdr->dev, "Command BD system bus error\n");
		return -EIO;
	}

	dma_rmb();
	/* Get the writeback command BD, because the caller may need
	 * to check some other fields of the response header.
	 */
	*cbd = *cur_cbd;

	/* Check the writeback error status */
	status = le16_to_cpu(cbd->resp_hdr.error_rr) & NTMP_RESP_ERROR;
	if (unlikely(status)) {
		dev_err(cbdr->dev, "Command BD error: 0x%04x\n", status);
		return -EIO;
	}

	return 0;
}

static int ntmp_alloc_data_mem(struct device *dev, struct netc_swcbd *swcbd,
			       void **buf_align)
{
	void *buf;

	buf = dma_alloc_coherent(dev, swcbd->size + NTMP_DATA_ADDR_ALIGN,
				 &swcbd->dma, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	swcbd->buf = buf;
	*buf_align = PTR_ALIGN(buf, NTMP_DATA_ADDR_ALIGN);

	return 0;
}

static void ntmp_fill_request_hdr(union netc_cbd *cbd, dma_addr_t dma,
				  int len, int table_id, int cmd,
				  int access_method)
{
	dma_addr_t dma_align;

	memset(cbd, 0, sizeof(*cbd));
	dma_align = ALIGN(dma, NTMP_DATA_ADDR_ALIGN);
	cbd->req_hdr.addr = cpu_to_le64(dma_align);
	cbd->req_hdr.len = cpu_to_le32(len);
	cbd->req_hdr.cmd = cmd;
	cbd->req_hdr.access_method = FIELD_PREP(NTMP_ACCESS_METHOD,
						access_method);
	cbd->req_hdr.table_id = table_id;
	cbd->req_hdr.ver_cci_rr = FIELD_PREP(NTMP_HDR_VERSION,
					     NTMP_HDR_VER2);
	/* For NTMP version 2.0 or later version */
	cbd->req_hdr.npf = cpu_to_le32(NTMP_NPF);
}

static void ntmp_fill_crd(struct ntmp_cmn_req_data *crd, u8 tblv,
			  u8 qa, u16 ua)
{
	crd->update_act = cpu_to_le16(ua);
	crd->tblv_qact = NTMP_TBLV_QACT(tblv, qa);
}

static void ntmp_fill_crd_eid(struct ntmp_req_by_eid *rbe, u8 tblv,
			      u8 qa, u16 ua, u32 entry_id)
{
	ntmp_fill_crd(&rbe->crd, tblv, qa, ua);
	rbe->entry_id = cpu_to_le32(entry_id);
}

static const char *ntmp_table_name(int tbl_id)
{
	switch (tbl_id) {
	case NTMP_MAFT_ID:
		return "MAC Address Filter Table";
	case NTMP_RSST_ID:
		return "RSS Table";
	case NTMP_IPFT_ID:
		return "Ingress Port Filter Table";
	case NTMP_FDBT_ID:
		return "FDB Table";
	case NTMP_VFT_ID:
		return "VLAN Filter Table";
	case NTMP_ETT_ID:
		return "Egress Treatment Table";
	case NTMP_ECT_ID:
		return "Egress Count Table";
	case NTMP_BPT_ID:
		return "Buffer Pool Table";
	default:
		return "Unknown Table";
	}
}

static int ntmp_delete_entry_by_id(struct ntmp_user *user, int tbl_id,
				   u8 tbl_ver, u32 entry_id, u32 req_len,
				   u32 resp_len)
{
	struct netc_swcbd swcbd = {
		.size = max(req_len, resp_len),
	};
	struct ntmp_req_by_eid *req;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err;

	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	ntmp_fill_crd_eid(req, tbl_ver, 0, 0, entry_id);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(req_len, resp_len),
			      tbl_id, NTMP_CMD_DELETE, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to delete entry 0x%x of %s, err: %pe",
			entry_id, ntmp_table_name(tbl_id), ERR_PTR(err));
	ntmp_unlock_cbdr(cbdr);

	return err;
}

static int ntmp_query_entry_by_id(struct netc_cbdr *cbdr, int tbl_id,
				  struct ntmp_req_by_eid *req,
				  struct netc_swcbd *swcbd,
				  bool compare_eid)
{
	u32 len = NTMP_LEN(sizeof(*req), swcbd->size);
	struct ntmp_cmn_resp_query *resp;
	int cmd = NTMP_CMD_QUERY;
	union netc_cbd cbd;
	u32 entry_id;
	int err;

	entry_id = le32_to_cpu(req->entry_id);
	if (le16_to_cpu(req->crd.update_act))
		cmd = NTMP_CMD_QU;

	/* Request header */
	ntmp_fill_request_hdr(&cbd, swcbd->dma, len, tbl_id, cmd,
			      NTMP_AM_ENTRY_ID);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, swcbd);
	if (err) {
		dev_err(cbdr->dev,
			"Failed to query entry 0x%x of %s, err: %pe\n",
			entry_id, ntmp_table_name(tbl_id), ERR_PTR(err));
		return err;
	}

	/* For a few tables, the first field of their response data is not
	 * entry_id, so directly return success.
	 */
	if (!compare_eid)
		return 0;

	resp = (struct ntmp_cmn_resp_query *)req;
	if (unlikely(le32_to_cpu(resp->entry_id) != entry_id)) {
		dev_err(cbdr->dev,
			"%s: query EID 0x%x doesn't match response EID 0x%x\n",
			ntmp_table_name(tbl_id), entry_id, le32_to_cpu(resp->entry_id));
		return -EIO;
	}

	return 0;
}

int ntmp_maft_add_entry(struct ntmp_user *user, u32 entry_id,
			struct maft_entry_data *maft)
{
	struct netc_swcbd swcbd = {
		.size = sizeof(struct maft_req_add),
	};
	struct maft_req_add *req;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err;

	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Set mac address filter table request data buffer */
	ntmp_fill_crd_eid(&req->rbe, user->tbl.maft_ver, 0, 0, entry_id);
	req->keye = maft->keye;
	req->cfge = maft->cfge;

	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0),
			      NTMP_MAFT_ID, NTMP_CMD_ADD, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev, "Failed to add MAFT entry 0x%x, err: %pe\n",
			entry_id, ERR_PTR(err));
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_maft_add_entry);

int ntmp_maft_query_entry(struct ntmp_user *user, u32 entry_id,
			  struct maft_entry_data *maft)
{
	struct netc_swcbd swcbd = {
		.size = sizeof(struct maft_resp_query),
	};
	struct maft_resp_query *resp;
	struct ntmp_req_by_eid *req;
	struct netc_cbdr *cbdr;
	int err;

	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	ntmp_fill_crd_eid(req, user->tbl.maft_ver, 0, 0, entry_id);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = ntmp_query_entry_by_id(cbdr, NTMP_MAFT_ID, req, &swcbd, true);
	if (err)
		goto unlock_cbdr;

	resp = (struct maft_resp_query *)req;
	maft->keye = resp->keye;
	maft->cfge = resp->cfge;

unlock_cbdr:
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_maft_query_entry);

int ntmp_maft_delete_entry(struct ntmp_user *user, u32 entry_id)
{
	return ntmp_delete_entry_by_id(user, NTMP_MAFT_ID, user->tbl.maft_ver,
				       entry_id, NTMP_EID_REQ_LEN, 0);
}
EXPORT_SYMBOL_GPL(ntmp_maft_delete_entry);

int ntmp_rsst_update_entry(struct ntmp_user *user, const u32 *table,
			   int count)
{
	struct rsst_req_update *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err, i;

	if (count != RSST_ENTRY_NUM)
		/* HW only takes in a full 64 entry table */
		return -EINVAL;

	swcbd.size = struct_size(req, groups, count);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Set the request data buffer */
	ntmp_fill_crd_eid(&req->rbe, user->tbl.rsst_ver, 0,
			  NTMP_GEN_UA_CFGEU | NTMP_GEN_UA_STSEU, 0);
	for (i = 0; i < count; i++)
		req->groups[i] = (u8)(table[i]);

	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0),
			      NTMP_RSST_ID, NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev, "Failed to update RSST entry, err: %pe\n",
			ERR_PTR(err));
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_rsst_update_entry);

int ntmp_rsst_query_entry(struct ntmp_user *user, u32 *table, int count)
{
	struct ntmp_req_by_eid *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err, i;
	u8 *group;

	if (count != RSST_ENTRY_NUM)
		/* HW only takes in a full 64 entry table */
		return -EINVAL;

	swcbd.size = NTMP_ENTRY_ID_SIZE + RSST_STSE_DATA_SIZE(count) +
		     RSST_CFGE_DATA_SIZE(count);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Set the request data buffer */
	ntmp_fill_crd_eid(req, user->tbl.rsst_ver, 0, 0, 0);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(sizeof(*req), swcbd.size),
			      NTMP_RSST_ID, NTMP_CMD_QUERY, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err) {
		dev_err(user->dev, "Failed to query RSST entry, err: %pe\n",
			ERR_PTR(err));
		goto unlock_cbdr;
	}

	group = (u8 *)req;
	group += NTMP_ENTRY_ID_SIZE + RSST_STSE_DATA_SIZE(count);
	for (i = 0; i < count; i++)
		table[i] = group[i];

unlock_cbdr:
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_rsst_query_entry);

/**
 * ntmp_ipft_add_entry - add an entry into the ingress port filter table
 * @user: target ntmp_user struct
 * @entry: the entry data, entry->cfge (configuration element data) and
 * entry->keye (key element data) are used as input. Since the entry ID
 * is assigned by the hardware, so entry->entry_id is a returned value
 * for the driver to use, the driver can update/delete/query the entry
 * based on the entry_id.
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ipft_add_entry(struct ntmp_user *user,
			struct ipft_entry_data *entry)
{
	struct ipft_resp_query *resp;
	struct ipft_req_ua *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*resp);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Note that NTMP_GEN_UA_STSEU is used to reset the statistics of
	 * the entry. The STSE_DATA is not present in the request data for
	 * 'Add' operation.
	 */
	ntmp_fill_crd(&req->crd, user->tbl.ipft_ver, NTMP_QA_ENTRY_ID,
		      NTMP_GEN_UA_CFGEU | NTMP_GEN_UA_STSEU);
	req->ak.keye = entry->keye;
	req->cfge = entry->cfge;

	len = NTMP_LEN(sizeof(*req), swcbd.size);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_IPFT_ID,
			      NTMP_CMD_AQ, NTMP_AM_TERNARY_KEY);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err) {
		dev_err(user->dev, "Failed to add %s entry, err: %pe\n",
			ntmp_table_name(NTMP_IPFT_ID), ERR_PTR(err));

		goto unlock_cbdr;
	}

	resp = (struct ipft_resp_query *)req;
	entry->entry_id = le32_to_cpu(resp->entry_id);

unlock_cbdr:
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_ipft_add_entry);

/**
 * ntmp_ipft_delete_entry - delete a specified ingress port filter table entry
 * @user: target ntmp_user struct
 * @entry_id: the specified ID of the ingress port filter table entry
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ipft_delete_entry(struct ntmp_user *user, u32 entry_id)
{
	u32 req_len = sizeof(struct ipft_req_qd);

	return ntmp_delete_entry_by_id(user, NTMP_IPFT_ID,
				       user->tbl.ipft_ver,
				       entry_id, req_len,
				       NTMP_STATUS_RESP_LEN);
}
EXPORT_SYMBOL_GPL(ntmp_ipft_delete_entry);

/**
 * ntmp_fdbt_add_entry - add an entry into the FDB table
 * @user: target ntmp_user struct
 * @entry_id: returned value, the entry ID of the new added entry
 * @keye: key element data
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_add_entry(struct ntmp_user *user, u32 *entry_id,
			const struct fdbt_keye_data *keye,
			const struct fdbt_cfge_data *cfge)
{
	struct fdbt_resp_query *resp;
	struct fdbt_req_ua *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, NTMP_QA_ENTRY_ID,
		      NTMP_GEN_UA_CFGEU);
	req->ak.exact.keye = *keye;
	req->cfge = *cfge;

	len = NTMP_LEN(swcbd.size, sizeof(*resp));
	/* The entry ID is allotted by hardware, so we need to perform
	 * a query action after the add action to get the entry ID from
	 * hardware.
	 */
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_AQ, NTMP_AM_EXACT_KEY);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err) {
		dev_err(user->dev, "Failed to add %s entry, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), ERR_PTR(err));
		goto unlock_cbdr;
	}

	if (entry_id) {
		resp = (struct fdbt_resp_query *)req;
		*entry_id = le32_to_cpu(resp->entry_id);
	}

unlock_cbdr:
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_add_entry);

/**
 * ntmp_fdbt_update_entry - update the configuration element data of the
 * specified FDB entry
 * @user: target ntmp_user struct
 * @entry_id: the specified entry ID of the FDB table
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_update_entry(struct ntmp_user *user, u32 entry_id,
			   const struct fdbt_cfge_data *cfge)
{
	struct fdbt_req_ua *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, 0, NTMP_GEN_UA_CFGEU);
	req->ak.eid.entry_id = cpu_to_le32(entry_id);
	req->cfge = *cfge;

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev, "Failed to update %s entry, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_update_entry);

/**
 * ntmp_fdbt_delete_entry - delete the specified FDB entry
 * @user: target ntmp_user struct
 * @entry_id: the specified ID of the FDB entry
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_delete_entry(struct ntmp_user *user, u32 entry_id)
{
	u32 req_len = sizeof(struct fdbt_req_qd);

	return ntmp_delete_entry_by_id(user, NTMP_FDBT_ID,
				       user->tbl.fdbt_ver,
				       entry_id, req_len,
				       NTMP_STATUS_RESP_LEN);
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_delete_entry);

/**
 * ntmp_fdbt_search_port_entry - Search the FDB entry on the specified
 * port based on RESUME_ENTRY_ID
 * @user: target ntmp_user struct
 * @port: the specified switch port ID
 * @resume_entry_id: it is both an input and an output. As an input, it
 * represents the FDB entry ID to be searched. If it is a NULL entry ID,
 * it indicates that the first FDB entry for that port is being searched.
 * As an output, it represents the next FDB entry ID to be searched.
 * @entry: returned value, the response data of the searched FDB entry
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_search_port_entry(struct ntmp_user *user, int port,
				u32 *resume_entry_id,
				struct fdbt_entry_data *entry)
{
	struct fdbt_resp_query *resp;
	struct fdbt_req_qd *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, 0, 0);
	req->ak.search.resume_eid = cpu_to_le32(*resume_entry_id);
	req->ak.search.cfge.port_bitmap = cpu_to_le32(BIT(port));
	/* Match CFGE_DATA[PORT_BITMAP] field */
	req->ak.search.cfge_mc = FDBT_CFGE_MC_PORT_BITMAP;

	/* Request header */
	len = NTMP_LEN(swcbd.size, sizeof(*resp));
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_QUERY, NTMP_AM_SEARCH);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err) {
		dev_err(user->dev,
			"Failed to search %s entry on port %d, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), port, ERR_PTR(err));
		goto unlock_cbdr;
	}

	if (!cbd.resp_hdr.num_matched) {
		entry->entry_id = NTMP_NULL_ENTRY_ID;
		*resume_entry_id = NTMP_NULL_ENTRY_ID;
		goto unlock_cbdr;
	}

	resp = (struct fdbt_resp_query *)req;
	*resume_entry_id = le32_to_cpu(resp->status);
	entry->entry_id = le32_to_cpu(resp->entry_id);
	entry->keye = resp->keye;
	entry->cfge = resp->cfge;
	entry->acte = resp->acte;

unlock_cbdr:
	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_search_port_entry);

/**
 * ntmp_fdbt_update_activity_element - update the activity element of all
 * the dynamic entries in the FDB table.
 * @user: target ntmp_user struct
 *
 * A single activity update management could be used to process all the
 * dynamic entries in the FDB table. When hardware process an activity
 * update management command for an entry in the FDB table and the entry
 * does not have its activity flag set, the activity counter is incremented.
 * However, if the activity flag is set, then both the activity flag and
 * activity counter are reset. Software can issue the activity update
 * management commands at predefined times and the value of the activity
 * counter can then be used to estimate the period of how long an FDB
 * entry has been inactive.
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_update_activity_element(struct ntmp_user *user)
{
	struct fdbt_req_ua *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, 0, FDBT_UA_ACTEU);
	req->ak.search.resume_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID);
	req->ak.search.cfge.cfg = cpu_to_le32(FDBT_DYNAMIC);
	req->ak.search.cfge_mc = FDBT_CFGE_MC_DYNAMIC;

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	/* For activity update, the access method must be search */
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_UPDATE, NTMP_AM_SEARCH);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to update activity of %s, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_update_activity_element);

/**
 * ntmp_fdbt_delete_ageing_entries - delete all the ageing dynamic entries
 * in the FDB table
 * @user: target ntmp_user struct
 * @act_cnt: the target value of the activity counter
 *
 * The matching rule is that the activity flag is not set and the activity
 * counter is greater than or equal to act_cnt
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_delete_ageing_entries(struct ntmp_user *user, u8 act_cnt)
{
	struct fdbt_req_qd *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	if (act_cnt > FDBT_ACT_CNT)
		return -EINVAL;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, 0, 0);
	req->ak.search.resume_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID);
	req->ak.search.cfge.cfg = cpu_to_le32(FDBT_DYNAMIC);
	req->ak.search.acte = act_cnt;
	/* Exact match with ACTE_DATA[ACT_FLAG] AND
	 * match >= ACTE_DATA[ACT_CNT]
	 */
	req->ak.search.acte_mc = FDBT_ACTE_MC;
	req->ak.search.cfge_mc = FDBT_CFGE_MC_DYNAMIC;

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_DELETE, NTMP_AM_SEARCH);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to delete ageing entries of %s, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_delete_ageing_entries);

/**
 * ntmp_fdbt_delete_port_dynamic_entries - delete all dynamic FDB entries
 * associated with the specified switch port
 * @user: target ntmp_user struct
 * @port: the specified switch port ID
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_fdbt_delete_port_dynamic_entries(struct ntmp_user *user, int port)
{
	struct fdbt_req_qd *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.fdbt_ver, 0, 0);
	req->ak.search.resume_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID);
	req->ak.search.cfge.port_bitmap = cpu_to_le32(BIT(port));
	req->ak.search.cfge.cfg = cpu_to_le32(FDBT_DYNAMIC);
	/* Match CFGE_DATA[DYNAMIC & PORT_BITMAP] field */
	req->ak.search.cfge_mc = FDBT_CFGE_MC_DYNAMIC_AND_PORT_BITMAP;

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_FDBT_ID,
			      NTMP_CMD_DELETE, NTMP_AM_SEARCH);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to delete dynamic %s entries on port %d, err: %pe\n",
			ntmp_table_name(NTMP_FDBT_ID), port, ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_fdbt_delete_port_dynamic_entries);

/**
 * ntmp_vft_set_entry - add an entry into the VLAN filter table or update
 * the configuration element data of the specified VLAN filter entry
 * @user: target ntmp_user struct
 * @vid: VLAN ID
 * @cmd: command type, NTMP_CMD_ADD or NTMP_CMD_UPDATE
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
static int ntmp_vft_set_entry(struct ntmp_user *user, u16 vid, int cmd,
			      const struct vft_cfge_data *cfge)
{
	struct netc_swcbd swcbd;
	struct vft_req_ua *req;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	if (cmd != NTMP_CMD_ADD && cmd != NTMP_CMD_UPDATE)
		return -EINVAL;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.vft_ver, 0, NTMP_GEN_UA_CFGEU);
	req->ak.exact.vid = cpu_to_le16(vid);
	req->cfge = *cfge;

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_VFT_ID,
			      cmd, NTMP_AM_EXACT_KEY);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	ntmp_unlock_cbdr(cbdr);

	return err;
}

/**
 * ntmp_vft_add_entry - add an entry into the VLAN filter table
 * @user: target ntmp_user struct
 * @vid: VLAN ID
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_vft_add_entry(struct ntmp_user *user, u16 vid,
		       const struct vft_cfge_data *cfge)
{
	int err;

	err = ntmp_vft_set_entry(user, vid, NTMP_CMD_ADD, cfge);
	if (err)
		dev_err(user->dev,
			"Failed to add %s entry, vid: %u, err: %pe\n",
			ntmp_table_name(NTMP_VFT_ID), vid, ERR_PTR(err));

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_vft_add_entry);

/**
 * ntmp_vft_update_entry - update the configuration element data of the
 * specified VLAN filter entry
 * @user: target ntmp_user struct
 * @vid: VLAN ID
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_vft_update_entry(struct ntmp_user *user, u16 vid,
			  const struct vft_cfge_data *cfge)
{
	int err;

	err = ntmp_vft_set_entry(user, vid, NTMP_CMD_UPDATE, cfge);
	if (err)
		dev_err(user->dev,
			"Failed to update %s entry, vid: %u, err: %pe\n",
			ntmp_table_name(NTMP_VFT_ID), vid, ERR_PTR(err));

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_vft_update_entry);

/**
 * ntmp_vft_delete_entry - delete the VLAN filter entry based on the
 * specified VLAN ID
 * @user: target ntmp_user struct
 * @vid: VLAN ID
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_vft_delete_entry(struct ntmp_user *user, u16 vid)
{
	struct netc_swcbd swcbd;
	struct vft_req_qd *req;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	u32 len;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd(&req->crd, user->tbl.vft_ver, 0, 0);
	req->ak.exact.vid = cpu_to_le16(vid);

	/* Request header */
	len = NTMP_LEN(swcbd.size, NTMP_STATUS_RESP_LEN);
	ntmp_fill_request_hdr(&cbd, swcbd.dma, len, NTMP_VFT_ID,
			      NTMP_CMD_DELETE, NTMP_AM_EXACT_KEY);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to delete %s entry, vid: %u, err: %pe\n",
			ntmp_table_name(NTMP_VFT_ID), vid, ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_vft_delete_entry);

/**
 * ntmp_ett_set_entry - add a new entry to the egress treatment table or
 * update the configuration element data of the specified entry
 * @user: target ntmp_user struct
 * @entry_id: entry ID
 * @cmd: command type, NTMP_CMD_ADD or NTMP_CMD_UPDATE
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
static int ntmp_ett_set_entry(struct ntmp_user *user, u32 entry_id,
			      int cmd, const struct ett_cfge_data *cfge)
{
	struct netc_swcbd swcbd;
	struct ett_req_ua *req;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err;

	if (cmd != NTMP_CMD_ADD && cmd != NTMP_CMD_UPDATE)
		return -EINVAL;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd_eid(&req->rbe, user->tbl.ett_ver, 0,
			  NTMP_GEN_UA_CFGEU, entry_id);
	req->cfge = *cfge;

	/* Request header */
	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0),
			      NTMP_ETT_ID, cmd, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	ntmp_unlock_cbdr(cbdr);

	return err;
}

/**
 * ntmp_ett_add_entry - add a new entry to the egress treatment table
 * @user: target ntmp_user struct
 * @entry_id: entry ID
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ett_add_entry(struct ntmp_user *user, u32 entry_id,
		       const struct ett_cfge_data *cfge)
{
	int err;

	err = ntmp_ett_set_entry(user, entry_id, NTMP_CMD_ADD, cfge);
	if (err)
		dev_err(user->dev, "Failed to add %s entry 0x%x, err: %pe\n",
			ntmp_table_name(NTMP_ETT_ID), entry_id, ERR_PTR(err));

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_ett_add_entry);

/**
 * ntmp_ett_update_entry - update the configuration element data of the
 * specified entry
 * @user: target ntmp_user struct
 * @entry_id: entry ID
 * @cfge: configuration element data
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ett_update_entry(struct ntmp_user *user, u32 entry_id,
			  const struct ett_cfge_data *cfge)
{
	int err;

	err = ntmp_ett_set_entry(user, entry_id, NTMP_CMD_UPDATE, cfge);
	if (err)
		dev_err(user->dev,
			"Failed to update %s entry 0x%x, err: %pe\n",
			ntmp_table_name(NTMP_ETT_ID), entry_id, ERR_PTR(err));

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_ett_update_entry);

/**
 * ntmp_ett_delete_entry - delete the specified egress treatment table entry
 * @user: target ntmp_user struct
 * @entry_id: entry ID
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ett_delete_entry(struct ntmp_user *user, u32 entry_id)
{
	return ntmp_delete_entry_by_id(user, NTMP_ETT_ID, user->tbl.ett_ver,
				       entry_id, NTMP_EID_REQ_LEN, 0);
}
EXPORT_SYMBOL_GPL(ntmp_ett_delete_entry);

/**
 * ntmp_ect_update_entry - reset the statistics element data of the
 * specified egress counter table entry
 * @user: target ntmp_user struct
 * @entry_id: entry ID
 *
 * Return: 0 on success, otherwise a negative error code
 */
int ntmp_ect_update_entry(struct ntmp_user *user, u32 entry_id)
{
	struct ntmp_req_by_eid *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Request data */
	ntmp_fill_crd_eid(req, user->tbl.ect_ver, 0, ECT_UA_STSEU, entry_id);

	/* Request header */
	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0),
			      NTMP_ECT_ID, NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to update %s entry 0x%x, err: %pe\n",
			ntmp_table_name(NTMP_ECT_ID), entry_id, ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_ect_update_entry);

int ntmp_bpt_update_entry(struct ntmp_user *user, u32 entry_id,
			  const struct bpt_cfge_data *cfge)
{
	struct bpt_req_update *req;
	struct netc_swcbd swcbd;
	struct netc_cbdr *cbdr;
	union netc_cbd cbd;
	int err;

	swcbd.size = sizeof(*req);
	err = ntmp_alloc_data_mem(user->dev, &swcbd, (void **)&req);
	if (err)
		return err;

	/* Note that BPT_UA_BPSEU is used to update the BPSE_DATA of the entry,
	 * which is maintained by the hardware. The BPSE_DATA is not present in
	 * the request data for 'Update' operation.
	 */
	ntmp_fill_crd_eid(&req->rbe, user->tbl.bpt_ver, 0,
			  NTMP_GEN_UA_CFGEU | BPT_UA_BPSEU, entry_id);
	req->cfge = *cfge;
	ntmp_fill_request_hdr(&cbd, swcbd.dma, NTMP_LEN(swcbd.size, 0),
			      NTMP_BPT_ID, NTMP_CMD_UPDATE, NTMP_AM_ENTRY_ID);

	ntmp_select_and_lock_cbdr(user, &cbdr);
	err = netc_xmit_ntmp_cmd(cbdr, &cbd, &swcbd);
	if (err)
		dev_err(user->dev,
			"Failed to update %s entry 0x%x, err: %pe\n",
			ntmp_table_name(NTMP_BPT_ID), entry_id, ERR_PTR(err));

	ntmp_unlock_cbdr(cbdr);

	return err;
}
EXPORT_SYMBOL_GPL(ntmp_bpt_update_entry);

MODULE_DESCRIPTION("NXP NETC Library");
MODULE_LICENSE("Dual BSD/GPL");