summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/mellanox/mlx5/core/steering/hws/mlx5hws_action.c
blob: b27bb41065326d3118cd592935a44e478b753ad6 (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
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2024 NVIDIA Corporation & Affiliates */

#include "mlx5hws_internal.h"

#define MLX5HWS_ACTION_METER_INIT_COLOR_OFFSET 1

/* Header removal size limited to 128B (64 words) */
#define MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE 128

/* This is the longest supported action sequence for FDB table:
 * DECAP, POP_VLAN, MODIFY, CTR, ASO, PUSH_VLAN, MODIFY, ENCAP, Term.
 */
static const u32 action_order_arr[MLX5HWS_TABLE_TYPE_MAX][MLX5HWS_ACTION_TYP_MAX] = {
	[MLX5HWS_TABLE_TYPE_FDB] = {
		BIT(MLX5HWS_ACTION_TYP_REMOVE_HEADER) |
		BIT(MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2) |
		BIT(MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2),
		BIT(MLX5HWS_ACTION_TYP_POP_VLAN),
		BIT(MLX5HWS_ACTION_TYP_POP_VLAN),
		BIT(MLX5HWS_ACTION_TYP_MODIFY_HDR),
		BIT(MLX5HWS_ACTION_TYP_PUSH_VLAN),
		BIT(MLX5HWS_ACTION_TYP_PUSH_VLAN),
		BIT(MLX5HWS_ACTION_TYP_INSERT_HEADER) |
		BIT(MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2) |
		BIT(MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3),
		BIT(MLX5HWS_ACTION_TYP_CTR),
		BIT(MLX5HWS_ACTION_TYP_TAG),
		BIT(MLX5HWS_ACTION_TYP_ASO_METER),
		BIT(MLX5HWS_ACTION_TYP_MODIFY_HDR),
		BIT(MLX5HWS_ACTION_TYP_TBL) |
		BIT(MLX5HWS_ACTION_TYP_VPORT) |
		BIT(MLX5HWS_ACTION_TYP_DROP) |
		BIT(MLX5HWS_ACTION_TYP_SAMPLER) |
		BIT(MLX5HWS_ACTION_TYP_RANGE) |
		BIT(MLX5HWS_ACTION_TYP_DEST_ARRAY),
		BIT(MLX5HWS_ACTION_TYP_LAST),
	},
};

static const char * const mlx5hws_action_type_str[] = {
	[MLX5HWS_ACTION_TYP_LAST] = "LAST",
	[MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2] = "TNL_L2_TO_L2",
	[MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2] = "L2_TO_TNL_L2",
	[MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2] = "TNL_L3_TO_L2",
	[MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3] = "L2_TO_TNL_L3",
	[MLX5HWS_ACTION_TYP_DROP] = "DROP",
	[MLX5HWS_ACTION_TYP_TBL] = "TBL",
	[MLX5HWS_ACTION_TYP_CTR] = "CTR",
	[MLX5HWS_ACTION_TYP_TAG] = "TAG",
	[MLX5HWS_ACTION_TYP_MODIFY_HDR] = "MODIFY_HDR",
	[MLX5HWS_ACTION_TYP_VPORT] = "VPORT",
	[MLX5HWS_ACTION_TYP_MISS] = "DEFAULT_MISS",
	[MLX5HWS_ACTION_TYP_POP_VLAN] = "POP_VLAN",
	[MLX5HWS_ACTION_TYP_PUSH_VLAN] = "PUSH_VLAN",
	[MLX5HWS_ACTION_TYP_ASO_METER] = "ASO_METER",
	[MLX5HWS_ACTION_TYP_DEST_ARRAY] = "DEST_ARRAY",
	[MLX5HWS_ACTION_TYP_INSERT_HEADER] = "INSERT_HEADER",
	[MLX5HWS_ACTION_TYP_REMOVE_HEADER] = "REMOVE_HEADER",
	[MLX5HWS_ACTION_TYP_SAMPLER] = "SAMPLER",
	[MLX5HWS_ACTION_TYP_RANGE] = "RANGE",
};

static_assert(ARRAY_SIZE(mlx5hws_action_type_str) == MLX5HWS_ACTION_TYP_MAX,
	      "Missing mlx5hws_action_type_str");

const char *mlx5hws_action_type_to_str(enum mlx5hws_action_type action_type)
{
	return mlx5hws_action_type_str[action_type];
}

enum mlx5hws_action_type mlx5hws_action_get_type(struct mlx5hws_action *action)
{
	return action->type;
}

static int hws_action_get_shared_stc_nic(struct mlx5hws_context *ctx,
					 enum mlx5hws_context_shared_stc_type stc_type,
					 u8 tbl_type)
{
	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
	struct mlx5hws_action_shared_stc *shared_stc;
	int ret;

	mutex_lock(&ctx->ctrl_lock);
	if (ctx->common_res[tbl_type].shared_stc[stc_type]) {
		ctx->common_res[tbl_type].shared_stc[stc_type]->refcount++;
		mutex_unlock(&ctx->ctrl_lock);
		return 0;
	}

	shared_stc = kzalloc(sizeof(*shared_stc), GFP_KERNEL);
	if (!shared_stc) {
		ret = -ENOMEM;
		goto unlock_and_out;
	}
	switch (stc_type) {
	case MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3:
		stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_REMOVE;
		stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
		stc_attr.remove_header.decap = 0;
		stc_attr.remove_header.start_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
		stc_attr.remove_header.end_anchor = MLX5_HEADER_ANCHOR_IPV6_IPV4;
		break;
	case MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP:
		stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
		stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		stc_attr.remove_words.start_anchor = MLX5_HEADER_ANCHOR_FIRST_VLAN_START;
		stc_attr.remove_words.num_of_words = MLX5HWS_ACTION_HDR_LEN_L2_VLAN;
		break;
	default:
		mlx5hws_err(ctx, "No such stc_type: %d\n", stc_type);
		pr_warn("HWS: Invalid stc_type: %d\n", stc_type);
		ret = -EINVAL;
		goto unlock_and_out;
	}

	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &shared_stc->stc_chunk);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate shared decap l2 STC\n");
		goto free_shared_stc;
	}

	ctx->common_res[tbl_type].shared_stc[stc_type] = shared_stc;
	ctx->common_res[tbl_type].shared_stc[stc_type]->refcount = 1;

	mutex_unlock(&ctx->ctrl_lock);

	return 0;

free_shared_stc:
	kfree(shared_stc);
unlock_and_out:
	mutex_unlock(&ctx->ctrl_lock);
	return ret;
}

static int hws_action_get_shared_stc(struct mlx5hws_action *action,
				     enum mlx5hws_context_shared_stc_type stc_type)
{
	struct mlx5hws_context *ctx = action->ctx;
	int ret;

	if (stc_type >= MLX5HWS_CONTEXT_SHARED_STC_MAX) {
		pr_warn("HWS: Invalid shared stc_type: %d\n", stc_type);
		return -EINVAL;
	}

	if (unlikely(!(action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB))) {
		pr_warn("HWS: Invalid action->flags: %d\n", action->flags);
		return -EINVAL;
	}

	ret = hws_action_get_shared_stc_nic(ctx, stc_type, MLX5HWS_TABLE_TYPE_FDB);
	if (ret) {
		mlx5hws_err(ctx,
			    "Failed to allocate memory for FDB shared STCs (type: %d)\n",
			    stc_type);
		return ret;
	}

	return 0;
}

static void hws_action_put_shared_stc(struct mlx5hws_action *action,
				      enum mlx5hws_context_shared_stc_type stc_type)
{
	enum mlx5hws_table_type tbl_type = MLX5HWS_TABLE_TYPE_FDB;
	struct mlx5hws_action_shared_stc *shared_stc;
	struct mlx5hws_context *ctx = action->ctx;

	if (stc_type >= MLX5HWS_CONTEXT_SHARED_STC_MAX) {
		pr_warn("HWS: Invalid shared stc_type: %d\n", stc_type);
		return;
	}

	mutex_lock(&ctx->ctrl_lock);
	if (--ctx->common_res[tbl_type].shared_stc[stc_type]->refcount) {
		mutex_unlock(&ctx->ctrl_lock);
		return;
	}

	shared_stc = ctx->common_res[tbl_type].shared_stc[stc_type];

	mlx5hws_action_free_single_stc(ctx, tbl_type, &shared_stc->stc_chunk);
	kfree(shared_stc);
	ctx->common_res[tbl_type].shared_stc[stc_type] = NULL;
	mutex_unlock(&ctx->ctrl_lock);
}

static void hws_action_print_combo(struct mlx5hws_context *ctx,
				   enum mlx5hws_action_type *user_actions)
{
	mlx5hws_err(ctx, "Invalid action_type sequence");
	while (*user_actions != MLX5HWS_ACTION_TYP_LAST) {
		mlx5hws_err(ctx, " %s", mlx5hws_action_type_to_str(*user_actions));
		user_actions++;
	}
	mlx5hws_err(ctx, "\n");
}

bool mlx5hws_action_check_combo(struct mlx5hws_context *ctx,
				enum mlx5hws_action_type *user_actions,
				enum mlx5hws_table_type table_type)
{
	const u32 *order_arr = action_order_arr[table_type];
	u8 order_idx = 0;
	u8 user_idx = 0;
	bool valid_combo;

	if (table_type >= MLX5HWS_TABLE_TYPE_MAX) {
		mlx5hws_err(ctx, "Invalid table_type %d", table_type);
		return false;
	}

	while (order_arr[order_idx] != BIT(MLX5HWS_ACTION_TYP_LAST)) {
		/* User action order validated move to next user action */
		if (BIT(user_actions[user_idx]) & order_arr[order_idx])
			user_idx++;

		/* Iterate to the next supported action in the order */
		order_idx++;
	}

	/* Combination is valid if all user action were processed */
	valid_combo = user_actions[user_idx] == MLX5HWS_ACTION_TYP_LAST;
	if (!valid_combo)
		hws_action_print_combo(ctx, user_actions);

	return valid_combo;
}

static bool
hws_action_fixup_stc_attr(struct mlx5hws_context *ctx,
			  struct mlx5hws_cmd_stc_modify_attr *stc_attr,
			  struct mlx5hws_cmd_stc_modify_attr *fixup_stc_attr,
			  enum mlx5hws_table_type table_type,
			  bool is_mirror)
{
	bool use_fixup = false;
	u32 fw_tbl_type;
	u32 base_id;

	fw_tbl_type = mlx5hws_table_get_res_fw_ft_type(table_type, is_mirror);

	switch (stc_attr->action_type) {
	case MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_STE_TABLE:
		if (is_mirror && stc_attr->ste_table.ignore_tx) {
			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
			fixup_stc_attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
			use_fixup = true;
			break;
		}
		if (!is_mirror)
			base_id = mlx5hws_pool_chunk_get_base_id(stc_attr->ste_table.ste_pool,
								 &stc_attr->ste_table.ste);
		else
			base_id =
				mlx5hws_pool_chunk_get_base_mirror_id(stc_attr->ste_table.ste_pool,
								      &stc_attr->ste_table.ste);

		*fixup_stc_attr = *stc_attr;
		fixup_stc_attr->ste_table.ste_obj_id = base_id;
		use_fixup = true;
		break;

	case MLX5_IFC_STC_ACTION_TYPE_TAG:
		if (fw_tbl_type == FS_FT_FDB_TX) {
			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_NOP;
			fixup_stc_attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
			use_fixup = true;
		}
		break;

	case MLX5_IFC_STC_ACTION_TYPE_ALLOW:
		if (fw_tbl_type == FS_FT_FDB_TX || fw_tbl_type == FS_FT_FDB_RX) {
			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT;
			fixup_stc_attr->action_offset = stc_attr->action_offset;
			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
			fixup_stc_attr->vport.esw_owner_vhca_id = ctx->caps->vhca_id;
			fixup_stc_attr->vport.vport_num = ctx->caps->eswitch_manager_vport_number;
			fixup_stc_attr->vport.eswitch_owner_vhca_id_valid =
				ctx->caps->merged_eswitch;
			use_fixup = true;
		}
		break;

	case MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT:
		if (stc_attr->vport.vport_num != MLX5_VPORT_UPLINK)
			break;

		if (fw_tbl_type == FS_FT_FDB_TX || fw_tbl_type == FS_FT_FDB_RX) {
			/* The FW doesn't allow to go to wire in the TX/RX by JUMP_TO_VPORT */
			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_UPLINK;
			fixup_stc_attr->action_offset = stc_attr->action_offset;
			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
			fixup_stc_attr->vport.vport_num = 0;
			fixup_stc_attr->vport.esw_owner_vhca_id = stc_attr->vport.esw_owner_vhca_id;
			fixup_stc_attr->vport.eswitch_owner_vhca_id_valid =
				stc_attr->vport.eswitch_owner_vhca_id_valid;
		}
		use_fixup = true;
		break;

	default:
		break;
	}

	return use_fixup;
}

int mlx5hws_action_alloc_single_stc(struct mlx5hws_context *ctx,
				    struct mlx5hws_cmd_stc_modify_attr *stc_attr,
				    u32 table_type,
				    struct mlx5hws_pool_chunk *stc)
__must_hold(&ctx->ctrl_lock)
{
	struct mlx5hws_cmd_stc_modify_attr cleanup_stc_attr = {0};
	struct mlx5hws_pool *stc_pool = ctx->stc_pool[table_type];
	struct mlx5hws_cmd_stc_modify_attr fixup_stc_attr = {0};
	bool use_fixup;
	u32 obj_0_id;
	int ret;

	ret = mlx5hws_pool_chunk_alloc(stc_pool, stc);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate single action STC\n");
		return ret;
	}

	stc_attr->stc_offset = stc->offset;

	/* Dynamic reparse not supported, overwrite and use default */
	if (!mlx5hws_context_cap_dynamic_reparse(ctx))
		stc_attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;

	obj_0_id = mlx5hws_pool_chunk_get_base_id(stc_pool, stc);

	/* According to table/action limitation change the stc_attr */
	use_fixup = hws_action_fixup_stc_attr(ctx, stc_attr, &fixup_stc_attr, table_type, false);
	ret = mlx5hws_cmd_stc_modify(ctx->mdev, obj_0_id,
				     use_fixup ? &fixup_stc_attr : stc_attr);
	if (ret) {
		mlx5hws_err(ctx, "Failed to modify STC action_type %d tbl_type %d\n",
			    stc_attr->action_type, table_type);
		goto free_chunk;
	}

	/* Modify the FDB peer */
	if (table_type == MLX5HWS_TABLE_TYPE_FDB) {
		u32 obj_1_id;

		obj_1_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, stc);

		use_fixup = hws_action_fixup_stc_attr(ctx, stc_attr,
						      &fixup_stc_attr,
						      table_type, true);
		ret = mlx5hws_cmd_stc_modify(ctx->mdev, obj_1_id,
					     use_fixup ? &fixup_stc_attr : stc_attr);
		if (ret) {
			mlx5hws_err(ctx,
				    "Failed to modify peer STC action_type %d tbl_type %d\n",
				    stc_attr->action_type, table_type);
			goto clean_obj_0;
		}
	}

	return 0;

clean_obj_0:
	cleanup_stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
	cleanup_stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
	cleanup_stc_attr.stc_offset = stc->offset;
	mlx5hws_cmd_stc_modify(ctx->mdev, obj_0_id, &cleanup_stc_attr);
free_chunk:
	mlx5hws_pool_chunk_free(stc_pool, stc);
	return ret;
}

void mlx5hws_action_free_single_stc(struct mlx5hws_context *ctx,
				    u32 table_type,
				    struct mlx5hws_pool_chunk *stc)
__must_hold(&ctx->ctrl_lock)
{
	struct mlx5hws_pool *stc_pool = ctx->stc_pool[table_type];
	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
	u32 obj_id;

	/* Modify the STC not to point to an object */
	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
	stc_attr.stc_offset = stc->offset;
	obj_id = mlx5hws_pool_chunk_get_base_id(stc_pool, stc);
	mlx5hws_cmd_stc_modify(ctx->mdev, obj_id, &stc_attr);

	if (table_type == MLX5HWS_TABLE_TYPE_FDB) {
		obj_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, stc);
		mlx5hws_cmd_stc_modify(ctx->mdev, obj_id, &stc_attr);
	}

	mlx5hws_pool_chunk_free(stc_pool, stc);
}

static u32 hws_action_get_mh_stc_type(struct mlx5hws_context *ctx,
				      __be64 pattern)
{
	u8 action_type = MLX5_GET(set_action_in, &pattern, action_type);

	switch (action_type) {
	case MLX5_MODIFICATION_TYPE_SET:
		return MLX5_IFC_STC_ACTION_TYPE_SET;
	case MLX5_MODIFICATION_TYPE_ADD:
		return MLX5_IFC_STC_ACTION_TYPE_ADD;
	case MLX5_MODIFICATION_TYPE_COPY:
		return MLX5_IFC_STC_ACTION_TYPE_COPY;
	case MLX5_MODIFICATION_TYPE_ADD_FIELD:
		return MLX5_IFC_STC_ACTION_TYPE_ADD_FIELD;
	default:
		mlx5hws_err(ctx, "Unsupported action type: 0x%x\n", action_type);
		return MLX5_IFC_STC_ACTION_TYPE_NOP;
	}
}

static void hws_action_fill_stc_attr(struct mlx5hws_action *action,
				     u32 obj_id,
				     struct mlx5hws_cmd_stc_modify_attr *attr)
{
	attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;

	switch (action->type) {
	case MLX5HWS_ACTION_TYP_TAG:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_TAG;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		break;
	case MLX5HWS_ACTION_TYP_DROP:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
		break;
	case MLX5HWS_ACTION_TYP_MISS:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ALLOW;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
		break;
	case MLX5HWS_ACTION_TYP_CTR:
		attr->id = obj_id;
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_COUNTER;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW0;
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
	case MLX5HWS_ACTION_TYP_MODIFY_HDR:
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
		if (action->modify_header.require_reparse)
			attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;

		if (action->modify_header.num_of_actions == 1) {
			attr->modify_action.data = action->modify_header.single_action;
			attr->action_type = hws_action_get_mh_stc_type(action->ctx,
								       attr->modify_action.data);

			if (attr->action_type == MLX5_IFC_STC_ACTION_TYPE_ADD ||
			    attr->action_type == MLX5_IFC_STC_ACTION_TYPE_SET)
				MLX5_SET(set_action_in, &attr->modify_action.data, data, 0);
		} else {
			attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ACC_MODIFY_LIST;
			attr->modify_header.arg_id = action->modify_header.arg_id;
			attr->modify_header.pattern_id = action->modify_header.pat_id;
		}
		break;
	case MLX5HWS_ACTION_TYP_TBL:
	case MLX5HWS_ACTION_TYP_DEST_ARRAY:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_FT;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
		attr->dest_table_id = obj_id;
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_REMOVE;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		attr->remove_header.decap = 1;
		attr->remove_header.start_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
		attr->remove_header.end_anchor = MLX5_HEADER_ANCHOR_INNER_MAC;
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
	case MLX5HWS_ACTION_TYP_INSERT_HEADER:
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		if (!action->reformat.require_reparse)
			attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;

		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
		attr->insert_header.encap = action->reformat.encap;
		attr->insert_header.insert_anchor = action->reformat.anchor;
		attr->insert_header.arg_id = action->reformat.arg_id;
		attr->insert_header.header_size = action->reformat.header_size;
		attr->insert_header.insert_offset = action->reformat.offset;
		break;
	case MLX5HWS_ACTION_TYP_ASO_METER:
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ASO;
		attr->aso.aso_type = ASO_OPC_MOD_POLICER;
		attr->aso.devx_obj_id = obj_id;
		attr->aso.return_reg_id = action->aso.return_reg_id;
		break;
	case MLX5HWS_ACTION_TYP_VPORT:
		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT;
		attr->vport.vport_num = action->vport.vport_num;
		attr->vport.esw_owner_vhca_id =	action->vport.esw_owner_vhca_id;
		attr->vport.eswitch_owner_vhca_id_valid = action->vport.esw_owner_vhca_id_valid;
		break;
	case MLX5HWS_ACTION_TYP_POP_VLAN:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		attr->remove_words.start_anchor = MLX5_HEADER_ANCHOR_FIRST_VLAN_START;
		attr->remove_words.num_of_words = MLX5HWS_ACTION_HDR_LEN_L2_VLAN / 2;
		break;
	case MLX5HWS_ACTION_TYP_PUSH_VLAN:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		attr->insert_header.encap = 0;
		attr->insert_header.is_inline = 1;
		attr->insert_header.insert_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
		attr->insert_header.insert_offset = MLX5HWS_ACTION_HDR_LEN_L2_MACS;
		attr->insert_header.header_size = MLX5HWS_ACTION_HDR_LEN_L2_VLAN;
		break;
	case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
		attr->remove_header.decap = 0; /* the mode we support decap is 0 */
		attr->remove_words.start_anchor = action->remove_header.anchor;
		/* the size is in already in words */
		attr->remove_words.num_of_words = action->remove_header.size;
		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
		break;
	default:
		mlx5hws_err(action->ctx, "Invalid action type %d\n", action->type);
	}
}

static int
hws_action_create_stcs(struct mlx5hws_action *action, u32 obj_id)
{
	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
	struct mlx5hws_context *ctx = action->ctx;
	int ret;

	hws_action_fill_stc_attr(action, obj_id, &stc_attr);

	/* Block unsupported parallel obj modify over the same base */
	mutex_lock(&ctx->ctrl_lock);

	/* Allocate STC for FDB */
	if (action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB) {
		ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr,
						      MLX5HWS_TABLE_TYPE_FDB,
						      &action->stc[MLX5HWS_TABLE_TYPE_FDB]);
		if (ret)
			goto out_err;
	}

	mutex_unlock(&ctx->ctrl_lock);

	return 0;

out_err:
	mutex_unlock(&ctx->ctrl_lock);
	return ret;
}

static void
hws_action_destroy_stcs(struct mlx5hws_action *action)
{
	struct mlx5hws_context *ctx = action->ctx;

	/* Block unsupported parallel obj modify over the same base */
	mutex_lock(&ctx->ctrl_lock);

	if (action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB)
		mlx5hws_action_free_single_stc(ctx, MLX5HWS_TABLE_TYPE_FDB,
					       &action->stc[MLX5HWS_TABLE_TYPE_FDB]);

	mutex_unlock(&ctx->ctrl_lock);
}

static bool hws_action_is_flag_hws_fdb(u32 flags)
{
	return flags & MLX5HWS_ACTION_FLAG_HWS_FDB;
}

static bool
hws_action_validate_hws_action(struct mlx5hws_context *ctx, u32 flags)
{
	if (!(ctx->flags & MLX5HWS_CONTEXT_FLAG_HWS_SUPPORT)) {
		mlx5hws_err(ctx, "Cannot create HWS action since HWS is not supported\n");
		return false;
	}

	if ((flags & MLX5HWS_ACTION_FLAG_HWS_FDB) && !ctx->caps->eswitch_manager) {
		mlx5hws_err(ctx, "Cannot create HWS action for FDB for non-eswitch-manager\n");
		return false;
	}

	return true;
}

static struct mlx5hws_action *
hws_action_create_generic_bulk(struct mlx5hws_context *ctx,
			       u32 flags,
			       enum mlx5hws_action_type action_type,
			       u8 bulk_sz)
{
	struct mlx5hws_action *action;
	int i;

	if (!hws_action_is_flag_hws_fdb(flags)) {
		mlx5hws_err(ctx,
			    "Action (type: %d) flags must specify only HWS FDB\n", action_type);
		return NULL;
	}

	if (!hws_action_validate_hws_action(ctx, flags))
		return NULL;

	action = kcalloc(bulk_sz, sizeof(*action), GFP_KERNEL);
	if (!action)
		return NULL;

	for (i = 0; i < bulk_sz; i++) {
		action[i].ctx = ctx;
		action[i].flags = flags;
		action[i].type = action_type;
	}

	return action;
}

static struct mlx5hws_action *
hws_action_create_generic(struct mlx5hws_context *ctx,
			  u32 flags,
			  enum mlx5hws_action_type action_type)
{
	return hws_action_create_generic_bulk(ctx, flags, action_type, 1);
}

struct mlx5hws_action *
mlx5hws_action_create_dest_table_num(struct mlx5hws_context *ctx,
				     u32 table_id,
				     u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_TBL);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, table_id);
	if (ret)
		goto free_action;

	action->dest_obj.obj_id = table_id;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_dest_table(struct mlx5hws_context *ctx,
				 struct mlx5hws_table *tbl,
				 u32 flags)
{
	return mlx5hws_action_create_dest_table_num(ctx, tbl->ft_id, flags);
}

struct mlx5hws_action *
mlx5hws_action_create_dest_drop(struct mlx5hws_context *ctx, u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_DROP);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, 0);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_default_miss(struct mlx5hws_context *ctx, u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_MISS);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, 0);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_tag(struct mlx5hws_context *ctx, u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_TAG);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, 0);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

static struct mlx5hws_action *
hws_action_create_aso(struct mlx5hws_context *ctx,
		      enum mlx5hws_action_type action_type,
		      u32 obj_id,
		      u8 return_reg_id,
		      u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, action_type);
	if (!action)
		return NULL;

	action->aso.obj_id = obj_id;
	action->aso.return_reg_id = return_reg_id;

	ret = hws_action_create_stcs(action, obj_id);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_aso_meter(struct mlx5hws_context *ctx,
				u32 obj_id,
				u8 return_reg_id,
				u32 flags)
{
	return hws_action_create_aso(ctx, MLX5HWS_ACTION_TYP_ASO_METER,
				     obj_id, return_reg_id, flags);
}

struct mlx5hws_action *
mlx5hws_action_create_counter(struct mlx5hws_context *ctx,
			      u32 obj_id,
			      u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_CTR);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, obj_id);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_dest_vport(struct mlx5hws_context *ctx,
				 u16 vport_num,
				 bool vhca_id_valid,
				 u16 vhca_id,
				 u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	if (!(flags & MLX5HWS_ACTION_FLAG_HWS_FDB)) {
		mlx5hws_err(ctx, "Vport action is supported for FDB only\n");
		return NULL;
	}

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_VPORT);
	if (!action)
		return NULL;

	if (!ctx->caps->merged_eswitch && vhca_id_valid && vhca_id != ctx->caps->vhca_id) {
		mlx5hws_err(ctx, "Non merged eswitch cannot send to other vhca\n");
		goto free_action;
	}

	action->vport.vport_num = vport_num;
	action->vport.esw_owner_vhca_id_valid = vhca_id_valid;

	if (vhca_id_valid)
		action->vport.esw_owner_vhca_id = vhca_id;

	ret = hws_action_create_stcs(action, 0);
	if (ret) {
		mlx5hws_err(ctx, "Failed creating stc for vport %d\n", vport_num);
		goto free_action;
	}

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_push_vlan(struct mlx5hws_context *ctx, u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_PUSH_VLAN);
	if (!action)
		return NULL;

	ret = hws_action_create_stcs(action, 0);
	if (ret) {
		mlx5hws_err(ctx, "Failed creating stc for push vlan\n");
		goto free_action;
	}

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_pop_vlan(struct mlx5hws_context *ctx, u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_POP_VLAN);
	if (!action)
		return NULL;

	ret = hws_action_get_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
	if (ret) {
		mlx5hws_err(ctx, "Failed to create remove stc for reformat\n");
		goto free_action;
	}

	ret = hws_action_create_stcs(action, 0);
	if (ret) {
		mlx5hws_err(ctx, "Failed creating stc for pop vlan\n");
		goto free_shared;
	}

	return action;

free_shared:
	hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
free_action:
	kfree(action);
	return NULL;
}

static int
hws_action_handle_insert_with_ptr(struct mlx5hws_action *action,
				  u8 num_of_hdrs,
				  struct mlx5hws_action_reformat_header *hdrs,
				  u32 log_bulk_sz)
{
	size_t max_sz = 0;
	u32 arg_id;
	int ret, i;

	for (i = 0; i < num_of_hdrs; i++) {
		if (hdrs[i].sz % W_SIZE != 0) {
			mlx5hws_err(action->ctx,
				    "Header data size should be in WORD granularity\n");
			return -EINVAL;
		}
		max_sz = max(hdrs[i].sz, max_sz);
	}

	/* Allocate single shared arg object for all headers */
	ret = mlx5hws_arg_create(action->ctx,
				 hdrs->data,
				 max_sz,
				 log_bulk_sz,
				 action->flags & MLX5HWS_ACTION_FLAG_SHARED,
				 &arg_id);
	if (ret)
		return ret;

	for (i = 0; i < num_of_hdrs; i++) {
		action[i].reformat.arg_id = arg_id;
		action[i].reformat.header_size = hdrs[i].sz;
		action[i].reformat.num_of_hdrs = num_of_hdrs;
		action[i].reformat.max_hdr_sz = max_sz;
		action[i].reformat.require_reparse = true;

		if (action[i].type == MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2 ||
		    action[i].type == MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3) {
			action[i].reformat.anchor = MLX5_HEADER_ANCHOR_PACKET_START;
			action[i].reformat.offset = 0;
			action[i].reformat.encap = 1;
		}

		ret = hws_action_create_stcs(&action[i], 0);
		if (ret) {
			mlx5hws_err(action->ctx, "Failed to create stc for reformat\n");
			goto free_stc;
		}
	}

	return 0;

free_stc:
	while (i--)
		hws_action_destroy_stcs(&action[i]);

	mlx5hws_arg_destroy(action->ctx, arg_id);
	return ret;
}

static int
hws_action_handle_l2_to_tunnel_l3(struct mlx5hws_action *action,
				  u8 num_of_hdrs,
				  struct mlx5hws_action_reformat_header *hdrs,
				  u32 log_bulk_sz)
{
	int ret;

	/* The action is remove-l2-header + insert-l3-header */
	ret = hws_action_get_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
	if (ret) {
		mlx5hws_err(action->ctx, "Failed to create remove stc for reformat\n");
		return ret;
	}

	/* Reuse the insert with pointer for the L2L3 header */
	ret = hws_action_handle_insert_with_ptr(action,
						num_of_hdrs,
						hdrs,
						log_bulk_sz);
	if (ret)
		goto put_shared_stc;

	return 0;

put_shared_stc:
	hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
	return ret;
}

static void hws_action_prepare_decap_l3_actions(size_t data_sz,
						u8 *mh_data,
						int *num_of_actions)
{
	int actions;
	u32 i;

	/* Remove L2L3 outer headers */
	MLX5_SET(stc_ste_param_remove, mh_data, action_type,
		 MLX5_MODIFICATION_TYPE_REMOVE);
	MLX5_SET(stc_ste_param_remove, mh_data, decap, 0x1);
	MLX5_SET(stc_ste_param_remove, mh_data, remove_start_anchor,
		 MLX5_HEADER_ANCHOR_PACKET_START);
	MLX5_SET(stc_ste_param_remove, mh_data, remove_end_anchor,
		 MLX5_HEADER_ANCHOR_INNER_IPV6_IPV4);
	mh_data += MLX5HWS_ACTION_DOUBLE_SIZE; /* Assume every action is 2 dw */
	actions = 1;

	/* Add the new header using inline action 4Byte at a time, the header
	 * is added in reversed order to the beginning of the packet to avoid
	 * incorrect parsing by the HW. Since header is 14B or 18B an extra
	 * two bytes are padded and later removed.
	 */
	for (i = 0; i < data_sz / MLX5HWS_ACTION_INLINE_DATA_SIZE + 1; i++) {
		MLX5_SET(stc_ste_param_insert, mh_data, action_type,
			 MLX5_MODIFICATION_TYPE_INSERT);
		MLX5_SET(stc_ste_param_insert, mh_data, inline_data, 0x1);
		MLX5_SET(stc_ste_param_insert, mh_data, insert_anchor,
			 MLX5_HEADER_ANCHOR_PACKET_START);
		MLX5_SET(stc_ste_param_insert, mh_data, insert_size, 2);
		mh_data += MLX5HWS_ACTION_DOUBLE_SIZE;
		actions++;
	}

	/* Remove first 2 extra bytes */
	MLX5_SET(stc_ste_param_remove_words, mh_data, action_type,
		 MLX5_MODIFICATION_TYPE_REMOVE_WORDS);
	MLX5_SET(stc_ste_param_remove_words, mh_data, remove_start_anchor,
		 MLX5_HEADER_ANCHOR_PACKET_START);
	/* The hardware expects here size in words (2 bytes) */
	MLX5_SET(stc_ste_param_remove_words, mh_data, remove_size, 1);
	actions++;

	*num_of_actions = actions;
}

static int
hws_action_handle_tunnel_l3_to_l2(struct mlx5hws_action *action,
				  u8 num_of_hdrs,
				  struct mlx5hws_action_reformat_header *hdrs,
				  u32 log_bulk_sz)
{
	u8 mh_data[MLX5HWS_ACTION_REFORMAT_DATA_SIZE] = {0};
	struct mlx5hws_context *ctx = action->ctx;
	u32 arg_id, pat_id;
	int num_of_actions;
	int mh_data_size;
	int ret, i;

	for (i = 0; i < num_of_hdrs; i++) {
		if (hdrs[i].sz != MLX5HWS_ACTION_HDR_LEN_L2 &&
		    hdrs[i].sz != MLX5HWS_ACTION_HDR_LEN_L2_W_VLAN) {
			mlx5hws_err(ctx, "Data size is not supported for decap-l3\n");
			return -EINVAL;
		}
	}

	/* Create a full modify header action list in case shared */
	hws_action_prepare_decap_l3_actions(hdrs->sz, mh_data, &num_of_actions);
	if (action->flags & MLX5HWS_ACTION_FLAG_SHARED)
		mlx5hws_action_prepare_decap_l3_data(hdrs->data, mh_data, num_of_actions);

	/* All DecapL3 cases require the same max arg size */
	ret = mlx5hws_arg_create_modify_header_arg(ctx,
						   (__be64 *)mh_data,
						   num_of_actions,
						   log_bulk_sz,
						   action->flags & MLX5HWS_ACTION_FLAG_SHARED,
						   &arg_id);
	if (ret)
		return ret;

	for (i = 0; i < num_of_hdrs; i++) {
		memset(mh_data, 0, MLX5HWS_ACTION_REFORMAT_DATA_SIZE);
		hws_action_prepare_decap_l3_actions(hdrs[i].sz, mh_data, &num_of_actions);
		mh_data_size = num_of_actions * MLX5HWS_MODIFY_ACTION_SIZE;

		ret = mlx5hws_pat_get_pattern(ctx, (__be64 *)mh_data, mh_data_size, &pat_id);
		if (ret) {
			mlx5hws_err(ctx, "Failed to allocate pattern for DecapL3\n");
			goto free_stc_and_pat;
		}

		action[i].modify_header.max_num_of_actions = num_of_actions;
		action[i].modify_header.num_of_actions = num_of_actions;
		action[i].modify_header.num_of_patterns = num_of_hdrs;
		action[i].modify_header.arg_id = arg_id;
		action[i].modify_header.pat_id = pat_id;
		action[i].modify_header.require_reparse =
			mlx5hws_pat_require_reparse((__be64 *)mh_data, num_of_actions);

		ret = hws_action_create_stcs(&action[i], 0);
		if (ret) {
			mlx5hws_pat_put_pattern(ctx, pat_id);
			goto free_stc_and_pat;
		}
	}

	return 0;

free_stc_and_pat:
	while (i--) {
		hws_action_destroy_stcs(&action[i]);
		mlx5hws_pat_put_pattern(ctx, action[i].modify_header.pat_id);
	}

	mlx5hws_arg_destroy(action->ctx, arg_id);
	return ret;
}

static int
hws_action_create_reformat_hws(struct mlx5hws_action *action,
			       u8 num_of_hdrs,
			       struct mlx5hws_action_reformat_header *hdrs,
			       u32 bulk_size)
{
	int ret;

	switch (action->type) {
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
		ret = hws_action_create_stcs(action, 0);
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
		ret = hws_action_handle_insert_with_ptr(action, num_of_hdrs, hdrs, bulk_size);
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
		ret = hws_action_handle_l2_to_tunnel_l3(action, num_of_hdrs, hdrs, bulk_size);
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
		ret = hws_action_handle_tunnel_l3_to_l2(action, num_of_hdrs, hdrs, bulk_size);
		break;
	default:
		mlx5hws_err(action->ctx, "Invalid HWS reformat action type\n");
		return -EINVAL;
	}

	return ret;
}

struct mlx5hws_action *
mlx5hws_action_create_reformat(struct mlx5hws_context *ctx,
			       enum mlx5hws_action_type reformat_type,
			       u8 num_of_hdrs,
			       struct mlx5hws_action_reformat_header *hdrs,
			       u32 log_bulk_size,
			       u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	if (!num_of_hdrs) {
		mlx5hws_err(ctx, "Reformat num_of_hdrs cannot be zero\n");
		return NULL;
	}

	action = hws_action_create_generic_bulk(ctx, flags, reformat_type, num_of_hdrs);
	if (!action)
		return NULL;

	if ((flags & MLX5HWS_ACTION_FLAG_SHARED) && (log_bulk_size || num_of_hdrs > 1)) {
		mlx5hws_err(ctx, "Reformat flags don't fit HWS (flags: 0x%x)\n", flags);
		goto free_action;
	}

	ret = hws_action_create_reformat_hws(action, num_of_hdrs, hdrs, log_bulk_size);
	if (ret) {
		mlx5hws_err(ctx, "Failed to create HWS reformat action\n");
		goto free_action;
	}

	return action;

free_action:
	kfree(action);
	return NULL;
}

static int
hws_action_create_modify_header_hws(struct mlx5hws_action *action,
				    u8 num_of_patterns,
				    struct mlx5hws_action_mh_pattern *pattern,
				    u32 log_bulk_size)
{
	struct mlx5hws_context *ctx = action->ctx;
	u16 num_actions, max_mh_actions = 0;
	int i, ret, size_in_bytes;
	u32 pat_id, arg_id = 0;
	__be64 *new_pattern;
	size_t pat_max_sz;

	pat_max_sz = MLX5HWS_ARG_CHUNK_SIZE_MAX * MLX5HWS_ARG_DATA_SIZE;
	size_in_bytes = pat_max_sz * sizeof(__be64);
	new_pattern = kcalloc(num_of_patterns, size_in_bytes, GFP_KERNEL);
	if (!new_pattern)
		return -ENOMEM;

	/* Calculate maximum number of mh actions for shared arg allocation */
	for (i = 0; i < num_of_patterns; i++) {
		size_t new_num_actions;
		size_t cur_num_actions;
		u32 nope_location;

		cur_num_actions = pattern[i].sz / MLX5HWS_MODIFY_ACTION_SIZE;

		mlx5hws_pat_calc_nope(pattern[i].data, cur_num_actions,
				      pat_max_sz / MLX5HWS_MODIFY_ACTION_SIZE,
				      &new_num_actions, &nope_location,
				      &new_pattern[i * pat_max_sz]);

		action[i].modify_header.nope_locations = nope_location;
		action[i].modify_header.num_of_actions = new_num_actions;

		max_mh_actions = max(max_mh_actions, new_num_actions);
	}

	if (mlx5hws_arg_get_arg_log_size(max_mh_actions) >= MLX5HWS_ARG_CHUNK_SIZE_MAX) {
		mlx5hws_err(ctx, "Num of actions (%d) bigger than allowed\n",
			    max_mh_actions);
		ret = -EINVAL;
		goto free_new_pat;
	}

	/* Allocate single shared arg for all patterns based on the max size */
	if (max_mh_actions > 1) {
		ret = mlx5hws_arg_create_modify_header_arg(ctx,
							   pattern->data,
							   max_mh_actions,
							   log_bulk_size,
							   action->flags &
							   MLX5HWS_ACTION_FLAG_SHARED,
							   &arg_id);
		if (ret)
			goto free_new_pat;
	}

	for (i = 0; i < num_of_patterns; i++) {
		if (!mlx5hws_pat_verify_actions(ctx, pattern[i].data, pattern[i].sz)) {
			mlx5hws_err(ctx, "Fail to verify pattern modify actions\n");
			ret = -EINVAL;
			goto free_stc_and_pat;
		}
		num_actions = pattern[i].sz / MLX5HWS_MODIFY_ACTION_SIZE;
		action[i].modify_header.num_of_patterns = num_of_patterns;
		action[i].modify_header.max_num_of_actions = max_mh_actions;

		action[i].modify_header.require_reparse =
			mlx5hws_pat_require_reparse(pattern[i].data, num_actions);

		if (num_actions == 1) {
			pat_id = 0;
			/* Optimize single modify action to be used inline */
			action[i].modify_header.single_action = pattern[i].data[0];
			action[i].modify_header.single_action_type =
				MLX5_GET(set_action_in, pattern[i].data, action_type);
		} else {
			/* Multiple modify actions require a pattern */
			if (unlikely(action[i].modify_header.nope_locations)) {
				size_t pattern_sz;

				pattern_sz = action[i].modify_header.num_of_actions *
					     MLX5HWS_MODIFY_ACTION_SIZE;
				ret =
				mlx5hws_pat_get_pattern(ctx,
							&new_pattern[i * pat_max_sz],
							pattern_sz, &pat_id);
			} else {
				ret = mlx5hws_pat_get_pattern(ctx,
							      pattern[i].data,
							      pattern[i].sz,
							      &pat_id);
			}
			if (ret) {
				mlx5hws_err(ctx,
					    "Failed to allocate pattern for modify header\n");
				goto free_stc_and_pat;
			}

			action[i].modify_header.arg_id = arg_id;
			action[i].modify_header.pat_id = pat_id;
		}
		/* Allocate STC for each action representing a header */
		ret = hws_action_create_stcs(&action[i], 0);
		if (ret) {
			if (pat_id)
				mlx5hws_pat_put_pattern(ctx, pat_id);
			goto free_stc_and_pat;
		}
	}

	kfree(new_pattern);
	return 0;

free_stc_and_pat:
	while (i--) {
		hws_action_destroy_stcs(&action[i]);
		if (action[i].modify_header.pat_id)
			mlx5hws_pat_put_pattern(ctx, action[i].modify_header.pat_id);
	}

	if (arg_id)
		mlx5hws_arg_destroy(ctx, arg_id);
free_new_pat:
	kfree(new_pattern);
	return ret;
}

struct mlx5hws_action *
mlx5hws_action_create_modify_header(struct mlx5hws_context *ctx,
				    u8 num_of_patterns,
				    struct mlx5hws_action_mh_pattern *patterns,
				    u32 log_bulk_size,
				    u32 flags)
{
	struct mlx5hws_action *action;
	int ret;

	if (!num_of_patterns) {
		mlx5hws_err(ctx, "Invalid number of patterns\n");
		return NULL;
	}
	action = hws_action_create_generic_bulk(ctx, flags,
						MLX5HWS_ACTION_TYP_MODIFY_HDR,
						num_of_patterns);
	if (!action)
		return NULL;

	if ((flags & MLX5HWS_ACTION_FLAG_SHARED) && (log_bulk_size || num_of_patterns > 1)) {
		mlx5hws_err(ctx, "Action cannot be shared with requested pattern or size\n");
		goto free_action;
	}

	ret = hws_action_create_modify_header_hws(action,
						  num_of_patterns,
						  patterns,
						  log_bulk_size);
	if (ret)
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_dest_array(struct mlx5hws_context *ctx,
				 size_t num_dest,
				 struct mlx5hws_action_dest_attr *dests,
				 bool ignore_flow_level,
				 u32 flow_source,
				 u32 flags)
{
	struct mlx5hws_cmd_set_fte_dest *dest_list = NULL;
	struct mlx5hws_cmd_ft_create_attr ft_attr = {0};
	struct mlx5hws_cmd_set_fte_attr fte_attr = {0};
	struct mlx5hws_cmd_forward_tbl *fw_island;
	struct mlx5hws_action *action;
	u32 i /*, packet_reformat_id*/;
	int ret;

	if (num_dest <= 1) {
		mlx5hws_err(ctx, "Action must have multiple dests\n");
		return NULL;
	}

	if (flags == (MLX5HWS_ACTION_FLAG_HWS_FDB | MLX5HWS_ACTION_FLAG_SHARED)) {
		ft_attr.type = FS_FT_FDB;
		ft_attr.level = ctx->caps->fdb_ft.max_level - 1;
	} else {
		mlx5hws_err(ctx, "Action flags not supported\n");
		return NULL;
	}

	dest_list = kcalloc(num_dest, sizeof(*dest_list), GFP_KERNEL);
	if (!dest_list)
		return NULL;

	for (i = 0; i < num_dest; i++) {
		enum mlx5hws_action_type action_type = dests[i].dest->type;
		struct mlx5hws_action *reformat_action = dests[i].reformat;

		switch (action_type) {
		case MLX5HWS_ACTION_TYP_TBL:
			dest_list[i].destination_type =
				MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
			dest_list[i].destination_id = dests[i].dest->dest_obj.obj_id;
			fte_attr.action_flags |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
			fte_attr.ignore_flow_level = ignore_flow_level;
			/* ToDo: In SW steering we have a handling of 'go to WIRE'
			 * destination here by upper layer setting 'is_wire_ft' flag
			 * if the destination is wire.
			 * This is because uplink should be last dest in the list.
			 */
			break;
		case MLX5HWS_ACTION_TYP_VPORT:
			dest_list[i].destination_type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
			dest_list[i].destination_id = dests[i].dest->vport.vport_num;
			fte_attr.action_flags |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
			if (ctx->caps->merged_eswitch) {
				dest_list[i].ext_flags |=
					MLX5HWS_CMD_EXT_DEST_ESW_OWNER_VHCA_ID;
				dest_list[i].esw_owner_vhca_id =
					dests[i].dest->vport.esw_owner_vhca_id;
			}
			break;
		default:
			mlx5hws_err(ctx, "Unsupported action in dest_array\n");
			goto free_dest_list;
		}

		if (reformat_action) {
			mlx5hws_err(ctx, "dest_array with reformat action - unsupported\n");
			goto free_dest_list;
		}
	}

	fte_attr.dests_num = num_dest;
	fte_attr.dests = dest_list;

	fw_island = mlx5hws_cmd_forward_tbl_create(ctx->mdev, &ft_attr, &fte_attr);
	if (!fw_island)
		goto free_dest_list;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_DEST_ARRAY);
	if (!action)
		goto destroy_fw_island;

	ret = hws_action_create_stcs(action, fw_island->ft_id);
	if (ret)
		goto free_action;

	action->dest_array.fw_island = fw_island;
	action->dest_array.num_dest = num_dest;
	action->dest_array.dest_list = dest_list;

	return action;

free_action:
	kfree(action);
destroy_fw_island:
	mlx5hws_cmd_forward_tbl_destroy(ctx->mdev, fw_island);
free_dest_list:
	for (i = 0; i < num_dest; i++) {
		if (dest_list[i].ext_reformat_id)
			mlx5hws_cmd_packet_reformat_destroy(ctx->mdev,
							    dest_list[i].ext_reformat_id);
	}
	kfree(dest_list);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_insert_header(struct mlx5hws_context *ctx,
				    u8 num_of_hdrs,
				    struct mlx5hws_action_insert_header *hdrs,
				    u32 log_bulk_size,
				    u32 flags)
{
	struct mlx5hws_action_reformat_header *reformat_hdrs;
	struct mlx5hws_action *action;
	int ret;
	int i;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_INSERT_HEADER);
	if (!action)
		return NULL;

	reformat_hdrs = kcalloc(num_of_hdrs, sizeof(*reformat_hdrs), GFP_KERNEL);
	if (!reformat_hdrs)
		goto free_action;

	for (i = 0; i < num_of_hdrs; i++) {
		if (hdrs[i].offset % W_SIZE != 0) {
			mlx5hws_err(ctx, "Header offset should be in WORD granularity\n");
			goto free_reformat_hdrs;
		}

		action[i].reformat.anchor = hdrs[i].anchor;
		action[i].reformat.encap = hdrs[i].encap;
		action[i].reformat.offset = hdrs[i].offset;

		reformat_hdrs[i].sz = hdrs[i].hdr.sz;
		reformat_hdrs[i].data = hdrs[i].hdr.data;
	}

	ret = hws_action_handle_insert_with_ptr(action, num_of_hdrs,
						reformat_hdrs, log_bulk_size);
	if (ret) {
		mlx5hws_err(ctx, "Failed to create HWS reformat action\n");
		goto free_reformat_hdrs;
	}

	kfree(reformat_hdrs);

	return action;

free_reformat_hdrs:
	kfree(reformat_hdrs);
free_action:
	kfree(action);
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_remove_header(struct mlx5hws_context *ctx,
				    struct mlx5hws_action_remove_header_attr *attr,
				    u32 flags)
{
	struct mlx5hws_action *action;

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_REMOVE_HEADER);
	if (!action)
		return NULL;

	/* support only remove anchor with size */
	if (attr->size % W_SIZE != 0) {
		mlx5hws_err(ctx,
			    "Invalid size, HW supports header remove in WORD granularity\n");
		goto free_action;
	}

	if (attr->size > MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE) {
		mlx5hws_err(ctx, "Header removal size limited to %u bytes\n",
			    MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE);
		goto free_action;
	}

	action->remove_header.anchor = attr->anchor;
	action->remove_header.size = attr->size / W_SIZE;

	if (hws_action_create_stcs(action, 0))
		goto free_action;

	return action;

free_action:
	kfree(action);
	return NULL;
}

static struct mlx5hws_definer *
hws_action_create_dest_match_range_definer(struct mlx5hws_context *ctx)
{
	struct mlx5hws_definer *definer;
	__be32 *tag;
	int ret;

	definer = kzalloc(sizeof(*definer), GFP_KERNEL);
	if (!definer)
		return NULL;

	definer->dw_selector[0] = MLX5_IFC_DEFINER_FORMAT_OFFSET_OUTER_ETH_PKT_LEN / 4;
	/* Set DW0 tag mask */
	tag = (__force __be32 *)definer->mask.jumbo;
	tag[MLX5HWS_RULE_JUMBO_MATCH_TAG_OFFSET_DW0] = htonl(0xffffUL << 16);

	mutex_lock(&ctx->ctrl_lock);

	ret = mlx5hws_definer_get_obj(ctx, definer);
	if (ret < 0) {
		mutex_unlock(&ctx->ctrl_lock);
		kfree(definer);
		return NULL;
	}

	mutex_unlock(&ctx->ctrl_lock);
	definer->obj_id = ret;

	return definer;
}

static struct mlx5hws_matcher_action_ste *
hws_action_create_dest_match_range_table(struct mlx5hws_context *ctx,
					 struct mlx5hws_definer *definer,
					 u32 miss_ft_id)
{
	struct mlx5hws_cmd_rtc_create_attr rtc_attr = {0};
	struct mlx5hws_action_default_stc *default_stc;
	struct mlx5hws_matcher_action_ste *table_ste;
	struct mlx5hws_pool_attr pool_attr = {0};
	struct mlx5hws_pool *ste_pool, *stc_pool;
	struct mlx5hws_pool_chunk *ste;
	u32 *rtc_0_id, *rtc_1_id;
	u32 obj_id;
	int ret;

	/* Check if STE range is supported */
	if (!IS_BIT_SET(ctx->caps->supp_ste_format_gen_wqe, MLX5_IFC_RTC_STE_FORMAT_RANGE)) {
		mlx5hws_err(ctx, "Range STE format not supported\n");
		return NULL;
	}

	table_ste = kzalloc(sizeof(*table_ste), GFP_KERNEL);
	if (!table_ste)
		return NULL;

	mutex_lock(&ctx->ctrl_lock);

	pool_attr.table_type = MLX5HWS_TABLE_TYPE_FDB;
	pool_attr.pool_type = MLX5HWS_POOL_TYPE_STE;
	pool_attr.flags = MLX5HWS_POOL_FLAGS_FOR_STE_ACTION_POOL;
	pool_attr.alloc_log_sz = 1;
	table_ste->pool = mlx5hws_pool_create(ctx, &pool_attr);
	if (!table_ste->pool) {
		mlx5hws_err(ctx, "Failed to allocate memory ste pool\n");
		goto free_ste;
	}

	/* Allocate RTC */
	rtc_0_id = &table_ste->rtc_0_id;
	rtc_1_id = &table_ste->rtc_1_id;
	ste_pool = table_ste->pool;
	ste = &table_ste->ste;
	ste->order = 1;

	rtc_attr.log_size = 0;
	rtc_attr.log_depth = 0;
	rtc_attr.miss_ft_id = miss_ft_id;
	rtc_attr.num_hash_definer = 1;
	rtc_attr.update_index_mode = MLX5_IFC_RTC_STE_UPDATE_MODE_BY_HASH;
	rtc_attr.access_index_mode = MLX5_IFC_RTC_STE_ACCESS_MODE_BY_HASH;
	rtc_attr.match_definer_0 = ctx->caps->trivial_match_definer;
	rtc_attr.fw_gen_wqe = true;
	rtc_attr.is_scnd_range = true;

	obj_id = mlx5hws_pool_chunk_get_base_id(ste_pool, ste);

	rtc_attr.pd = ctx->pd_num;
	rtc_attr.ste_base = obj_id;
	rtc_attr.ste_offset = ste->offset;
	rtc_attr.reparse_mode = mlx5hws_context_get_reparse_mode(ctx);
	rtc_attr.table_type = mlx5hws_table_get_res_fw_ft_type(MLX5HWS_TABLE_TYPE_FDB, false);

	/* STC is a single resource (obj_id), use any STC for the ID */
	stc_pool = ctx->stc_pool[MLX5HWS_TABLE_TYPE_FDB];
	default_stc = ctx->common_res[MLX5HWS_TABLE_TYPE_FDB].default_stc;
	obj_id = mlx5hws_pool_chunk_get_base_id(stc_pool, &default_stc->default_hit);
	rtc_attr.stc_base = obj_id;

	ret = mlx5hws_cmd_rtc_create(ctx->mdev, &rtc_attr, rtc_0_id);
	if (ret) {
		mlx5hws_err(ctx, "Failed to create RTC");
		goto pool_destroy;
	}

	/* Create mirror RTC */
	obj_id = mlx5hws_pool_chunk_get_base_mirror_id(ste_pool, ste);
	rtc_attr.ste_base = obj_id;
	rtc_attr.table_type = mlx5hws_table_get_res_fw_ft_type(MLX5HWS_TABLE_TYPE_FDB, true);

	obj_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, &default_stc->default_hit);
	rtc_attr.stc_base = obj_id;

	ret = mlx5hws_cmd_rtc_create(ctx->mdev, &rtc_attr, rtc_1_id);
	if (ret) {
		mlx5hws_err(ctx, "Failed to create mirror RTC");
		goto destroy_rtc_0;
	}

	mutex_unlock(&ctx->ctrl_lock);

	return table_ste;

destroy_rtc_0:
	mlx5hws_cmd_rtc_destroy(ctx->mdev, *rtc_0_id);
pool_destroy:
	mlx5hws_pool_destroy(table_ste->pool);
free_ste:
	mutex_unlock(&ctx->ctrl_lock);
	kfree(table_ste);
	return NULL;
}

static void
hws_action_destroy_dest_match_range_table(struct mlx5hws_context *ctx,
					  struct mlx5hws_matcher_action_ste *table_ste)
{
	mutex_lock(&ctx->ctrl_lock);

	mlx5hws_cmd_rtc_destroy(ctx->mdev, table_ste->rtc_1_id);
	mlx5hws_cmd_rtc_destroy(ctx->mdev, table_ste->rtc_0_id);
	mlx5hws_pool_destroy(table_ste->pool);
	kfree(table_ste);

	mutex_unlock(&ctx->ctrl_lock);
}

static int
hws_action_create_dest_match_range_fill_table(struct mlx5hws_context *ctx,
					      struct mlx5hws_matcher_action_ste *table_ste,
					      struct mlx5hws_action *hit_ft_action,
					      struct mlx5hws_definer *range_definer,
					      u32 min, u32 max)
{
	struct mlx5hws_wqe_gta_data_seg_ste match_wqe_data = {0};
	struct mlx5hws_wqe_gta_data_seg_ste range_wqe_data = {0};
	struct mlx5hws_wqe_gta_ctrl_seg wqe_ctrl = {0};
	u32 no_use, used_rtc_0_id, used_rtc_1_id, ret;
	struct mlx5hws_context_common_res *common_res;
	struct mlx5hws_send_ste_attr ste_attr = {0};
	struct mlx5hws_send_engine *queue;
	__be32 *wqe_data_arr;

	mutex_lock(&ctx->ctrl_lock);

	/* Get the control queue */
	queue = &ctx->send_queue[ctx->queues - 1];
	if (unlikely(mlx5hws_send_engine_err(queue))) {
		ret = -EIO;
		goto error;
	}

	/* 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;
	ste_attr.send_attr.user_data = &no_use;
	ste_attr.send_attr.rule = NULL;
	ste_attr.send_attr.fence = 1;
	ste_attr.send_attr.notify_hw = true;
	ste_attr.rtc_0 = table_ste->rtc_0_id;
	ste_attr.rtc_1 = table_ste->rtc_1_id;
	ste_attr.used_id_rtc_0 = &used_rtc_0_id;
	ste_attr.used_id_rtc_1 = &used_rtc_1_id;

	common_res = &ctx->common_res[MLX5HWS_TABLE_TYPE_FDB];

	/* init an empty match STE which will always hit */
	ste_attr.wqe_ctrl = &wqe_ctrl;
	ste_attr.wqe_data = &match_wqe_data;
	ste_attr.send_attr.match_definer_id = ctx->caps->trivial_match_definer;

	/* Fill WQE control data */
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] =
		htonl(common_res->default_stc->nop_ctr.offset);
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
		htonl(common_res->default_stc->nop_dw5.offset);
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] =
		htonl(common_res->default_stc->nop_dw6.offset);
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] =
		htonl(common_res->default_stc->nop_dw7.offset);
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] |=
		htonl(MLX5HWS_ACTION_STC_IDX_LAST_COMBO2 << 29);
	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] =
		htonl(hit_ft_action->stc[MLX5HWS_TABLE_TYPE_FDB].offset);

	wqe_data_arr = (__force __be32 *)&range_wqe_data;

	ste_attr.range_wqe_data = &range_wqe_data;
	ste_attr.send_attr.len += MLX5HWS_WQE_SZ_GTA_DATA;
	ste_attr.send_attr.range_definer_id = mlx5hws_definer_get_id(range_definer);

	/* Fill range matching fields,
	 * min/max_value_2 corresponds to match_dw_0 in its definer,
	 * min_value_2 sets in DW0 in the STE and max_value_2 sets in DW1 in the STE.
	 */
	wqe_data_arr[MLX5HWS_MATCHER_OFFSET_TAG_DW0] = htonl(min << 16);
	wqe_data_arr[MLX5HWS_MATCHER_OFFSET_TAG_DW1] = htonl(max << 16);

	/* Send WQEs to FW */
	mlx5hws_send_stes_fw(ctx, queue, &ste_attr);

	/* Poll for completion */
	ret = mlx5hws_send_queue_action(ctx, ctx->queues - 1,
					MLX5HWS_SEND_QUEUE_ACTION_DRAIN_SYNC);
	if (ret) {
		mlx5hws_err(ctx, "Failed to drain control queue");
		goto error;
	}

	mutex_unlock(&ctx->ctrl_lock);

	return 0;

error:
	mutex_unlock(&ctx->ctrl_lock);
	return ret;
}

struct mlx5hws_action *
mlx5hws_action_create_dest_match_range(struct mlx5hws_context *ctx,
				       u32 field,
				       struct mlx5_flow_table *hit_ft,
				       struct mlx5_flow_table *miss_ft,
				       u32 min, u32 max, u32 flags)
{
	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
	struct mlx5hws_matcher_action_ste *table_ste;
	struct mlx5hws_action *hit_ft_action;
	struct mlx5hws_definer *definer;
	struct mlx5hws_action *action;
	u32 miss_ft_id = miss_ft->id;
	u32 hit_ft_id = hit_ft->id;
	int ret;

	if (field != MLX5_FLOW_DEST_RANGE_FIELD_PKT_LEN ||
	    min > 0xffff || max > 0xffff) {
		mlx5hws_err(ctx, "Invalid match range parameters\n");
		return NULL;
	}

	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_RANGE);
	if (!action)
		return NULL;

	definer = hws_action_create_dest_match_range_definer(ctx);
	if (!definer)
		goto free_action;

	table_ste = hws_action_create_dest_match_range_table(ctx, definer, miss_ft_id);
	if (!table_ste)
		goto destroy_definer;

	hit_ft_action = mlx5hws_action_create_dest_table_num(ctx, hit_ft_id, flags);
	if (!hit_ft_action)
		goto destroy_table_ste;

	ret = hws_action_create_dest_match_range_fill_table(ctx, table_ste,
							    hit_ft_action,
							    definer, min, max);
	if (ret)
		goto destroy_hit_ft_action;

	action->range.table_ste = table_ste;
	action->range.definer = definer;
	action->range.hit_ft_action = hit_ft_action;

	/* Allocate STC for jumps to STE */
	mutex_lock(&ctx->ctrl_lock);
	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_STE_TABLE;
	stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
	stc_attr.ste_table.ste = table_ste->ste;
	stc_attr.ste_table.ste_pool = table_ste->pool;
	stc_attr.ste_table.match_definer_id = ctx->caps->trivial_match_definer;

	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, MLX5HWS_TABLE_TYPE_FDB,
					      &action->stc[MLX5HWS_TABLE_TYPE_FDB]);
	if (ret)
		goto error_unlock;

	mutex_unlock(&ctx->ctrl_lock);

	return action;

error_unlock:
	mutex_unlock(&ctx->ctrl_lock);
destroy_hit_ft_action:
	mlx5hws_action_destroy(hit_ft_action);
destroy_table_ste:
	hws_action_destroy_dest_match_range_table(ctx, table_ste);
destroy_definer:
	mlx5hws_definer_free(ctx, definer);
free_action:
	kfree(action);
	mlx5hws_err(ctx, "Failed to create action dest match range");
	return NULL;
}

struct mlx5hws_action *
mlx5hws_action_create_last(struct mlx5hws_context *ctx, u32 flags)
{
	return hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_LAST);
}

struct mlx5hws_action *
mlx5hws_action_create_flow_sampler(struct mlx5hws_context *ctx,
				   u32 sampler_id, u32 flags)
{
	mlx5hws_err(ctx, "Flow sampler action - unsupported\n");
	return NULL;
}

static void hws_action_destroy_hws(struct mlx5hws_action *action)
{
	u32 ext_reformat_id;
	bool shared_arg;
	u32 obj_id;
	u32 i;

	switch (action->type) {
	case MLX5HWS_ACTION_TYP_MISS:
	case MLX5HWS_ACTION_TYP_TAG:
	case MLX5HWS_ACTION_TYP_DROP:
	case MLX5HWS_ACTION_TYP_CTR:
	case MLX5HWS_ACTION_TYP_TBL:
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
	case MLX5HWS_ACTION_TYP_ASO_METER:
	case MLX5HWS_ACTION_TYP_PUSH_VLAN:
	case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
	case MLX5HWS_ACTION_TYP_VPORT:
		hws_action_destroy_stcs(action);
		break;
	case MLX5HWS_ACTION_TYP_POP_VLAN:
		hws_action_destroy_stcs(action);
		hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
		break;
	case MLX5HWS_ACTION_TYP_DEST_ARRAY:
		hws_action_destroy_stcs(action);
		mlx5hws_cmd_forward_tbl_destroy(action->ctx->mdev, action->dest_array.fw_island);
		for (i = 0; i < action->dest_array.num_dest; i++) {
			ext_reformat_id = action->dest_array.dest_list[i].ext_reformat_id;
			if (ext_reformat_id)
				mlx5hws_cmd_packet_reformat_destroy(action->ctx->mdev,
								    ext_reformat_id);
		}
		kfree(action->dest_array.dest_list);
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
	case MLX5HWS_ACTION_TYP_MODIFY_HDR:
		shared_arg = false;
		for (i = 0; i < action->modify_header.num_of_patterns; i++) {
			hws_action_destroy_stcs(&action[i]);
			if (action[i].modify_header.num_of_actions > 1) {
				mlx5hws_pat_put_pattern(action[i].ctx,
							action[i].modify_header.pat_id);
				/* Save shared arg object to be freed after */
				obj_id = action[i].modify_header.arg_id;
				shared_arg = true;
			}
		}
		if (shared_arg)
			mlx5hws_arg_destroy(action->ctx, obj_id);
		break;
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
		hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
		for (i = 0; i < action->reformat.num_of_hdrs; i++)
			hws_action_destroy_stcs(&action[i]);
		mlx5hws_arg_destroy(action->ctx, action->reformat.arg_id);
		break;
	case MLX5HWS_ACTION_TYP_INSERT_HEADER:
	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
		for (i = 0; i < action->reformat.num_of_hdrs; i++)
			hws_action_destroy_stcs(&action[i]);
		mlx5hws_arg_destroy(action->ctx, action->reformat.arg_id);
		break;
	case MLX5HWS_ACTION_TYP_RANGE:
		hws_action_destroy_stcs(action);
		hws_action_destroy_dest_match_range_table(action->ctx, action->range.table_ste);
		mlx5hws_definer_free(action->ctx, action->range.definer);
		mlx5hws_action_destroy(action->range.hit_ft_action);
		break;
	case MLX5HWS_ACTION_TYP_LAST:
		break;
	default:
		pr_warn("HWS: Invalid action type: %d\n", action->type);
	}
}

int mlx5hws_action_destroy(struct mlx5hws_action *action)
{
	hws_action_destroy_hws(action);

	kfree(action);
	return 0;
}

int mlx5hws_action_get_default_stc(struct mlx5hws_context *ctx, u8 tbl_type)
__must_hold(&ctx->ctrl_lock)
{
	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
	struct mlx5hws_action_default_stc *default_stc;
	int ret;

	if (ctx->common_res[tbl_type].default_stc) {
		ctx->common_res[tbl_type].default_stc->refcount++;
		return 0;
	}

	default_stc = kzalloc(sizeof(*default_stc), GFP_KERNEL);
	if (!default_stc)
		return -ENOMEM;

	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_NOP;
	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW0;
	stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &default_stc->nop_ctr);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate default counter STC\n");
		goto free_default_stc;
	}

	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &default_stc->nop_dw5);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate default NOP DW5 STC\n");
		goto free_nop_ctr;
	}

	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW6;
	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &default_stc->nop_dw6);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate default NOP DW6 STC\n");
		goto free_nop_dw5;
	}

	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW7;
	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &default_stc->nop_dw7);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate default NOP DW7 STC\n");
		goto free_nop_dw6;
	}

	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_ALLOW;

	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
					      &default_stc->default_hit);
	if (ret) {
		mlx5hws_err(ctx, "Failed to allocate default allow STC\n");
		goto free_nop_dw7;
	}

	ctx->common_res[tbl_type].default_stc = default_stc;
	ctx->common_res[tbl_type].default_stc->refcount++;

	return 0;

free_nop_dw7:
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw7);
free_nop_dw6:
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw6);
free_nop_dw5:
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw5);
free_nop_ctr:
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_ctr);
free_default_stc:
	kfree(default_stc);
	return ret;
}

void mlx5hws_action_put_default_stc(struct mlx5hws_context *ctx, u8 tbl_type)
__must_hold(&ctx->ctrl_lock)
{
	struct mlx5hws_action_default_stc *default_stc;

	default_stc = ctx->common_res[tbl_type].default_stc;

	default_stc = ctx->common_res[tbl_type].default_stc;
	if (--default_stc->refcount)
		return;

	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->default_hit);
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw7);
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw6);
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw5);
	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_ctr);
	kfree(default_stc);
	ctx->common_res[tbl_type].default_stc = NULL;
}

static void hws_action_modify_write(struct mlx5hws_send_engine *queue,
				    u32 arg_idx,
				    u8 *arg_data,
				    u16 num_of_actions,
				    u32 nope_locations)
{
	u8 *new_arg_data = NULL;
	int i, j;

	if (unlikely(nope_locations)) {
		new_arg_data = kcalloc(num_of_actions,
				       MLX5HWS_MODIFY_ACTION_SIZE, GFP_KERNEL);
		if (unlikely(!new_arg_data))
			return;

		for (i = 0, j = 0; i < num_of_actions; i++, j++) {
			memcpy(&new_arg_data[j], arg_data, MLX5HWS_MODIFY_ACTION_SIZE);
			if (BIT(i) & nope_locations)
				j++;
		}
	}

	mlx5hws_arg_write(queue, NULL, arg_idx,
			  new_arg_data ? new_arg_data : arg_data,
			  num_of_actions * MLX5HWS_MODIFY_ACTION_SIZE);

	kfree(new_arg_data);
}

void mlx5hws_action_prepare_decap_l3_data(u8 *src, u8 *dst, u16 num_of_actions)
{
	u8 *e_src;
	int i;

	/* num_of_actions = remove l3l2 + 4/5 inserts + remove extra 2 bytes
	 * copy from end of src to the start of dst.
	 * move to the end, 2 is the leftover from 14B or 18B
	 */
	if (num_of_actions == DECAP_L3_NUM_ACTIONS_W_NO_VLAN)
		e_src = src + MLX5HWS_ACTION_HDR_LEN_L2;
	else
		e_src = src + MLX5HWS_ACTION_HDR_LEN_L2_W_VLAN;

	/* Move dst over the first remove action + zero data */
	dst += MLX5HWS_ACTION_DOUBLE_SIZE;
	/* Move dst over the first insert ctrl action */
	dst += MLX5HWS_ACTION_DOUBLE_SIZE / 2;
	/* Actions:
	 * no vlan: r_h-insert_4b-insert_4b-insert_4b-insert_4b-remove_2b.
	 * with vlan: r_h-insert_4b-insert_4b-insert_4b-insert_4b-insert_4b-remove_2b.
	 * the loop is without the last insertion.
	 */
	for (i = 0; i < num_of_actions - 3; i++) {
		e_src -= MLX5HWS_ACTION_INLINE_DATA_SIZE;
		memcpy(dst, e_src, MLX5HWS_ACTION_INLINE_DATA_SIZE); /* data */
		dst += MLX5HWS_ACTION_DOUBLE_SIZE;
	}
	/* Copy the last 2 bytes after a gap of 2 bytes which will be removed */
	e_src -= MLX5HWS_ACTION_INLINE_DATA_SIZE / 2;
	dst += MLX5HWS_ACTION_INLINE_DATA_SIZE / 2;
	memcpy(dst, e_src, 2);
}

static int
hws_action_get_shared_stc_offset(struct mlx5hws_context_common_res *common_res,
				 enum mlx5hws_context_shared_stc_type stc_type)
{
	return common_res->shared_stc[stc_type]->stc_chunk.offset;
}

static struct mlx5hws_actions_wqe_setter *
hws_action_setter_find_first(struct mlx5hws_actions_wqe_setter *setter,
			     u8 req_flags)
{
	/* Use a new setter if requested flags are taken */
	while (setter->flags & req_flags)
		setter++;

	/* Use current setter in required flags are not used */
	return setter;
}

static void
hws_action_apply_stc(struct mlx5hws_actions_apply_data *apply,
		     enum mlx5hws_action_stc_idx stc_idx,
		     u8 action_idx)
{
	struct mlx5hws_action *action = apply->rule_action[action_idx].action;

	apply->wqe_ctrl->stc_ix[stc_idx] =
		htonl(action->stc[apply->tbl_type].offset);
}

static void
hws_action_setter_push_vlan(struct mlx5hws_actions_apply_data *apply,
			    struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;

	rule_action = &apply->rule_action[setter->idx_double];
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = rule_action->push_vlan.vlan_hdr;

	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW6, setter->idx_double);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
}

static void
hws_action_setter_modify_header(struct mlx5hws_actions_apply_data *apply,
				struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;
	struct mlx5hws_action *action;
	u32 arg_sz, arg_idx;
	u8 *single_action;
	__be32 stc_idx;

	rule_action = &apply->rule_action[setter->idx_double];
	action = rule_action->action;

	stc_idx = htonl(action->stc[apply->tbl_type].offset);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;

	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;

	if (action->modify_header.num_of_actions == 1) {
		if (action->modify_header.single_action_type ==
		    MLX5_MODIFICATION_TYPE_COPY ||
		    action->modify_header.single_action_type ==
		    MLX5_MODIFICATION_TYPE_ADD_FIELD) {
			apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = 0;
			return;
		}

		if (action->flags & MLX5HWS_ACTION_FLAG_SHARED)
			single_action = (u8 *)&action->modify_header.single_action;
		else
			single_action = rule_action->modify_header.data;

		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] =
			*(__be32 *)MLX5_ADDR_OF(set_action_in, single_action, data);
	} else {
		/* Argument offset multiple with number of args per these actions */
		arg_sz = mlx5hws_arg_get_arg_size(action->modify_header.max_num_of_actions);
		arg_idx = rule_action->modify_header.offset * arg_sz;

		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);

		if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
			apply->require_dep = 1;
			hws_action_modify_write(apply->queue,
						action->modify_header.arg_id + arg_idx,
						rule_action->modify_header.data,
						action->modify_header.num_of_actions,
						action->modify_header.nope_locations);
		}
	}
}

static void
hws_action_setter_insert_ptr(struct mlx5hws_actions_apply_data *apply,
			     struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;
	struct mlx5hws_action *action;
	u32 arg_idx, arg_sz;
	__be32 stc_idx;

	rule_action = &apply->rule_action[setter->idx_double];
	action = rule_action->action + rule_action->reformat.hdr_idx;

	/* Argument offset multiple on args required for header size */
	arg_sz = mlx5hws_arg_data_size_to_arg_size(action->reformat.max_hdr_sz);
	arg_idx = rule_action->reformat.offset * arg_sz;

	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);

	stc_idx = htonl(action->stc[apply->tbl_type].offset);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;

	if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
		apply->require_dep = 1;
		mlx5hws_arg_write(apply->queue, NULL,
				  action->reformat.arg_id + arg_idx,
				  rule_action->reformat.data,
				  action->reformat.header_size);
	}
}

static void
hws_action_setter_tnl_l3_to_l2(struct mlx5hws_actions_apply_data *apply,
			       struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;
	struct mlx5hws_action *action;
	u32 arg_sz, arg_idx;
	__be32 stc_idx;

	rule_action = &apply->rule_action[setter->idx_double];
	action = rule_action->action + rule_action->reformat.hdr_idx;

	/* Argument offset multiple on args required for num of actions */
	arg_sz = mlx5hws_arg_get_arg_size(action->modify_header.max_num_of_actions);
	arg_idx = rule_action->reformat.offset * arg_sz;

	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);

	stc_idx = htonl(action->stc[apply->tbl_type].offset);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;

	if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
		apply->require_dep = 1;
		mlx5hws_arg_decapl3_write(apply->queue,
					  action->modify_header.arg_id + arg_idx,
					  rule_action->reformat.data,
					  action->modify_header.num_of_actions);
	}
}

static void
hws_action_setter_aso(struct mlx5hws_actions_apply_data *apply,
		      struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;
	u32 exe_aso_ctrl;
	u32 offset;

	rule_action = &apply->rule_action[setter->idx_double];

	switch (rule_action->action->type) {
	case MLX5HWS_ACTION_TYP_ASO_METER:
		/* exe_aso_ctrl format:
		 * [STC only and reserved bits 29b][init_color 2b][meter_id 1b]
		 */
		offset = rule_action->aso_meter.offset / MLX5_ASO_METER_NUM_PER_OBJ;
		exe_aso_ctrl = rule_action->aso_meter.offset % MLX5_ASO_METER_NUM_PER_OBJ;
		exe_aso_ctrl |= rule_action->aso_meter.init_color <<
				MLX5HWS_ACTION_METER_INIT_COLOR_OFFSET;
		break;
	default:
		mlx5hws_err(rule_action->action->ctx,
			    "Unsupported ASO action type: %d\n", rule_action->action->type);
		return;
	}

	/* aso_object_offset format: [24B] */
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = htonl(offset);
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(exe_aso_ctrl);

	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW6, setter->idx_double);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
}

static void
hws_action_setter_tag(struct mlx5hws_actions_apply_data *apply,
		      struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;

	rule_action = &apply->rule_action[setter->idx_single];
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = htonl(rule_action->tag.value);
	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW5, setter->idx_single);
}

static void
hws_action_setter_ctrl_ctr(struct mlx5hws_actions_apply_data *apply,
			   struct mlx5hws_actions_wqe_setter *setter)
{
	struct mlx5hws_rule_action *rule_action;

	rule_action = &apply->rule_action[setter->idx_ctr];
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW0] = htonl(rule_action->counter.offset);
	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_CTRL, setter->idx_ctr);
}

static void
hws_action_setter_single(struct mlx5hws_actions_apply_data *apply,
			 struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW5, setter->idx_single);
}

static void
hws_action_setter_single_double_pop(struct mlx5hws_actions_apply_data *apply,
				    struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
		htonl(hws_action_get_shared_stc_offset(apply->common_res,
						       MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP));
}

static void
hws_action_setter_hit(struct mlx5hws_actions_apply_data *apply,
		      struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_HIT, setter->idx_hit);
}

static void
hws_action_setter_default_hit(struct mlx5hws_actions_apply_data *apply,
			      struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] =
		htonl(apply->common_res->default_stc->default_hit.offset);
}

static void
hws_action_setter_hit_next_action(struct mlx5hws_actions_apply_data *apply,
				  struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = htonl(apply->next_direct_idx << 6);
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] = htonl(apply->jump_to_action_stc);
}

static void
hws_action_setter_common_decap(struct mlx5hws_actions_apply_data *apply,
			       struct mlx5hws_actions_wqe_setter *setter)
{
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
		htonl(hws_action_get_shared_stc_offset(apply->common_res,
						       MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3));
}

static void
hws_action_setter_range(struct mlx5hws_actions_apply_data *apply,
			struct mlx5hws_actions_wqe_setter *setter)
{
	/* Always jump to index zero */
	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_HIT, setter->idx_hit);
}

int mlx5hws_action_template_process(struct mlx5hws_action_template *at)
{
	struct mlx5hws_actions_wqe_setter *start_setter = at->setters + 1;
	enum mlx5hws_action_type *action_type = at->action_type_arr;
	struct mlx5hws_actions_wqe_setter *setter = at->setters;
	struct mlx5hws_actions_wqe_setter *pop_setter = NULL;
	struct mlx5hws_actions_wqe_setter *last_setter;
	int i;

	/* Note: Given action combination must be valid */

	/* Check if action were already processed */
	if (at->num_of_action_stes)
		return 0;

	for (i = 0; i < MLX5HWS_ACTION_MAX_STE; i++)
		setter[i].set_hit = &hws_action_setter_hit_next_action;

	/* The same action template setters can be used with jumbo or match
	 * STE, to support both cases we reserve the first setter for cases
	 * with jumbo STE to allow jump to the first action STE.
	 * This extra setter can be reduced in some cases on rule creation.
	 */
	setter = start_setter;
	last_setter = start_setter;

	for (i = 0; i < at->num_actions; i++) {
		switch (action_type[i]) {
		case MLX5HWS_ACTION_TYP_DROP:
		case MLX5HWS_ACTION_TYP_TBL:
		case MLX5HWS_ACTION_TYP_DEST_ARRAY:
		case MLX5HWS_ACTION_TYP_VPORT:
		case MLX5HWS_ACTION_TYP_MISS:
			/* Hit action */
			last_setter->flags |= ASF_HIT;
			last_setter->set_hit = &hws_action_setter_hit;
			last_setter->idx_hit = i;
			break;

		case MLX5HWS_ACTION_TYP_RANGE:
			last_setter->flags |= ASF_HIT;
			last_setter->set_hit = &hws_action_setter_range;
			last_setter->idx_hit = i;
			break;

		case MLX5HWS_ACTION_TYP_POP_VLAN:
			/* Single remove header to header */
			if (pop_setter) {
				/* We have 2 pops, use the shared */
				pop_setter->set_single = &hws_action_setter_single_double_pop;
				break;
			}
			setter = hws_action_setter_find_first(last_setter,
							      ASF_SINGLE1 | ASF_MODIFY |
							      ASF_INSERT);
			setter->flags |= ASF_SINGLE1 | ASF_REMOVE;
			setter->set_single = &hws_action_setter_single;
			setter->idx_single = i;
			pop_setter = setter;
			break;

		case MLX5HWS_ACTION_TYP_PUSH_VLAN:
			/* Double insert inline */
			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
			setter->flags |= ASF_DOUBLE | ASF_INSERT;
			setter->set_double = &hws_action_setter_push_vlan;
			setter->idx_double = i;
			break;

		case MLX5HWS_ACTION_TYP_MODIFY_HDR:
			/* Double modify header list */
			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
			setter->flags |= ASF_DOUBLE | ASF_MODIFY;
			setter->set_double = &hws_action_setter_modify_header;
			setter->idx_double = i;
			break;

		case MLX5HWS_ACTION_TYP_ASO_METER:
			/* Double ASO action */
			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE);
			setter->flags |= ASF_DOUBLE;
			setter->set_double = &hws_action_setter_aso;
			setter->idx_double = i;
			break;

		case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
		case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
			/* Single remove header to header */
			setter = hws_action_setter_find_first(last_setter,
							      ASF_SINGLE1 | ASF_MODIFY);
			setter->flags |= ASF_SINGLE1 | ASF_REMOVE;
			setter->set_single = &hws_action_setter_single;
			setter->idx_single = i;
			break;

		case MLX5HWS_ACTION_TYP_INSERT_HEADER:
		case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
			/* Double insert header with pointer */
			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
			setter->flags |= ASF_DOUBLE | ASF_INSERT;
			setter->set_double = &hws_action_setter_insert_ptr;
			setter->idx_double = i;
			break;

		case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
			/* Single remove + Double insert header with pointer */
			setter = hws_action_setter_find_first(last_setter,
							      ASF_SINGLE1 | ASF_DOUBLE);
			setter->flags |= ASF_SINGLE1 | ASF_DOUBLE;
			setter->set_double = &hws_action_setter_insert_ptr;
			setter->idx_double = i;
			setter->set_single = &hws_action_setter_common_decap;
			setter->idx_single = i;
			break;

		case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
			/* Double modify header list with remove and push inline */
			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
			setter->flags |= ASF_DOUBLE | ASF_MODIFY | ASF_INSERT;
			setter->set_double = &hws_action_setter_tnl_l3_to_l2;
			setter->idx_double = i;
			break;

		case MLX5HWS_ACTION_TYP_TAG:
			/* Single TAG action, search for any room from the start */
			setter = hws_action_setter_find_first(start_setter, ASF_SINGLE1);
			setter->flags |= ASF_SINGLE1;
			setter->set_single = &hws_action_setter_tag;
			setter->idx_single = i;
			break;

		case MLX5HWS_ACTION_TYP_CTR:
			/* Control counter action
			 * TODO: Current counter executed first. Support is needed
			 *	 for single ation counter action which is done last.
			 *	 Example: Decap + CTR
			 */
			setter = hws_action_setter_find_first(start_setter, ASF_CTR);
			setter->flags |= ASF_CTR;
			setter->set_ctr = &hws_action_setter_ctrl_ctr;
			setter->idx_ctr = i;
			break;
		default:
			pr_warn("HWS: Invalid action type in processingaction template: action_type[%d]=%d\n",
				i, action_type[i]);
			return -EOPNOTSUPP;
		}

		last_setter = max(setter, last_setter);
	}

	/* Set default hit on the last STE if no hit action provided */
	if (!(last_setter->flags & ASF_HIT))
		last_setter->set_hit = &hws_action_setter_default_hit;

	at->num_of_action_stes = last_setter - start_setter + 1;

	/* Check if action template doesn't require any action DWs */
	at->only_term = (at->num_of_action_stes == 1) &&
		!(last_setter->flags & ~(ASF_CTR | ASF_HIT));

	return 0;
}

struct mlx5hws_action_template *
mlx5hws_action_template_create(enum mlx5hws_action_type action_type[])
{
	struct mlx5hws_action_template *at;
	u8 num_actions = 0;
	int i;

	at = kzalloc(sizeof(*at), GFP_KERNEL);
	if (!at)
		return NULL;

	while (action_type[num_actions++] != MLX5HWS_ACTION_TYP_LAST)
		;

	at->num_actions = num_actions - 1;
	at->action_type_arr = kcalloc(num_actions, sizeof(*action_type), GFP_KERNEL);
	if (!at->action_type_arr)
		goto free_at;

	for (i = 0; i < num_actions; i++)
		at->action_type_arr[i] = action_type[i];

	return at;

free_at:
	kfree(at);
	return NULL;
}

int mlx5hws_action_template_destroy(struct mlx5hws_action_template *at)
{
	kfree(at->action_type_arr);
	kfree(at);
	return 0;
}