DPDK logo

Elixir Cross Referencer

   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
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2017 Huawei Technologies Co., Ltd
 */

#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include <rte_byteorder.h>
#include <rte_common.h>
#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_malloc.h>
#include <rte_flow.h>
#include <rte_flow_driver.h>
#include "base/hinic_compat.h"
#include "base/hinic_pmd_hwdev.h"
#include "base/hinic_pmd_hwif.h"
#include "base/hinic_pmd_wq.h"
#include "base/hinic_pmd_cmdq.h"
#include "base/hinic_pmd_niccfg.h"
#include "hinic_pmd_ethdev.h"

#define HINIC_MAX_RX_QUEUE_NUM		64

#ifndef UINT8_MAX
#define UINT8_MAX          (u8)(~((u8)0))	/* 0xFF               */
#define UINT16_MAX         (u16)(~((u16)0))	/* 0xFFFF             */
#define UINT32_MAX         (u32)(~((u32)0))	/* 0xFFFFFFFF         */
#define UINT64_MAX         (u64)(~((u64)0))	/* 0xFFFFFFFFFFFFFFFF */
#define ASCII_MAX          (0x7F)
#endif

/* IPSURX MACRO */
#define PA_ETH_TYPE_ROCE		0
#define PA_ETH_TYPE_IPV4		1
#define PA_ETH_TYPE_IPV6		2
#define PA_ETH_TYPE_OTHER		3

#define PA_IP_PROTOCOL_TYPE_TCP		1
#define PA_IP_PROTOCOL_TYPE_UDP		2
#define PA_IP_PROTOCOL_TYPE_ICMP	3
#define PA_IP_PROTOCOL_TYPE_IPV4_IGMP	4
#define PA_IP_PROTOCOL_TYPE_SCTP	5
#define PA_IP_PROTOCOL_TYPE_VRRP	112

#define IP_HEADER_PROTOCOL_TYPE_TCP     6
#define IP_HEADER_PROTOCOL_TYPE_UDP     17
#define IP_HEADER_PROTOCOL_TYPE_ICMP    1
#define IP_HEADER_PROTOCOL_TYPE_ICMPV6  58

#define FDIR_TCAM_NORMAL_PACKET         0
#define FDIR_TCAM_TUNNEL_PACKET         1

#define HINIC_MIN_N_TUPLE_PRIO		1
#define HINIC_MAX_N_TUPLE_PRIO		7

/* TCAM type mask in hardware */
#define TCAM_PKT_BGP_SPORT	1
#define TCAM_PKT_VRRP		2
#define TCAM_PKT_BGP_DPORT	3
#define TCAM_PKT_LACP		4

#define TCAM_DIP_IPV4_TYPE	0
#define TCAM_DIP_IPV6_TYPE	1

#define BGP_DPORT_ID		179
#define IPPROTO_VRRP		112

/* Packet type defined in hardware to perform filter */
#define PKT_IGMP_IPV4_TYPE     64
#define PKT_ICMP_IPV4_TYPE     65
#define PKT_ICMP_IPV6_TYPE     66
#define PKT_ICMP_IPV6RS_TYPE   67
#define PKT_ICMP_IPV6RA_TYPE   68
#define PKT_ICMP_IPV6NS_TYPE   69
#define PKT_ICMP_IPV6NA_TYPE   70
#define PKT_ICMP_IPV6RE_TYPE   71
#define PKT_DHCP_IPV4_TYPE     72
#define PKT_DHCP_IPV6_TYPE     73
#define PKT_LACP_TYPE          74
#define PKT_ARP_REQ_TYPE       79
#define PKT_ARP_REP_TYPE       80
#define PKT_ARP_TYPE           81
#define PKT_BGPD_DPORT_TYPE    83
#define PKT_BGPD_SPORT_TYPE    84
#define PKT_VRRP_TYPE          85

#define HINIC_DEV_PRIVATE_TO_FILTER_INFO(nic_dev) \
	(&((struct hinic_nic_dev *)nic_dev)->filter)

#define HINIC_DEV_PRIVATE_TO_TCAM_INFO(nic_dev) \
	(&((struct hinic_nic_dev *)nic_dev)->tcam)


enum hinic_atr_flow_type {
	HINIC_ATR_FLOW_TYPE_IPV4_DIP    = 0x1,
	HINIC_ATR_FLOW_TYPE_IPV4_SIP    = 0x2,
	HINIC_ATR_FLOW_TYPE_DPORT       = 0x3,
	HINIC_ATR_FLOW_TYPE_SPORT       = 0x4,
};

/* Structure to store fdir's info. */
struct hinic_fdir_info {
	uint8_t fdir_flag;
	uint8_t qid;
	uint32_t fdir_key;
};

/**
 * Endless loop will never happen with below assumption
 * 1. there is at least one no-void item(END)
 * 2. cur is before END.
 */
static inline const struct rte_flow_item *
next_no_void_pattern(const struct rte_flow_item pattern[],
		const struct rte_flow_item *cur)
{
	const struct rte_flow_item *next =
		cur ? cur + 1 : &pattern[0];
	while (1) {
		if (next->type != RTE_FLOW_ITEM_TYPE_VOID)
			return next;
		next++;
	}
}

static inline const struct rte_flow_action *
next_no_void_action(const struct rte_flow_action actions[],
		const struct rte_flow_action *cur)
{
	const struct rte_flow_action *next =
		cur ? cur + 1 : &actions[0];
	while (1) {
		if (next->type != RTE_FLOW_ACTION_TYPE_VOID)
			return next;
		next++;
	}
}

static int hinic_check_ethertype_attr_ele(const struct rte_flow_attr *attr,
					struct rte_flow_error *error)
{
	/* Must be input direction */
	if (!attr->ingress) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
			attr, "Only support ingress.");
		return -rte_errno;
	}

	if (attr->egress) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
				attr, "Not support egress.");
		return -rte_errno;
	}

	if (attr->priority) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
				attr, "Not support priority.");
		return -rte_errno;
	}

	if (attr->group) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
				attr, "Not support group.");
		return -rte_errno;
	}

	return 0;
}

static int hinic_check_filter_arg(const struct rte_flow_attr *attr,
				const struct rte_flow_item *pattern,
				const struct rte_flow_action *actions,
				struct rte_flow_error *error)
{
	if (!pattern) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
				NULL, "NULL pattern.");
		return -rte_errno;
	}

	if (!actions) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ACTION_NUM,
				NULL, "NULL action.");
		return -rte_errno;
	}

	if (!attr) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR,
				   NULL, "NULL attribute.");
		return -rte_errno;
	}

	return 0;
}

static int hinic_check_ethertype_first_item(const struct rte_flow_item *item,
					struct rte_flow_error *error)
{
	/* The first non-void item should be MAC */
	if (item->type != RTE_FLOW_ITEM_TYPE_ETH) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by ethertype filter");
		return -rte_errno;
	}

	/* Not supported last point for range */
	if (item->last) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
			item, "Not supported last point for range");
		return -rte_errno;
	}

	/* Get the MAC info. */
	if (!item->spec || !item->mask) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ethertype filter");
		return -rte_errno;
	}
	return 0;
}

static int
hinic_parse_ethertype_aciton(const struct rte_flow_action *actions,
			const struct rte_flow_action *act,
			const struct rte_flow_action_queue *act_q,
			struct rte_eth_ethertype_filter *filter,
			struct rte_flow_error *error)
{
	/* Parse action */
	act = next_no_void_action(actions, NULL);
	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE &&
		act->type != RTE_FLOW_ACTION_TYPE_DROP) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ACTION,
				act, "Not supported action.");
		return -rte_errno;
	}

	if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
		act_q = (const struct rte_flow_action_queue *)act->conf;
		filter->queue = act_q->index;
	} else {
		filter->flags |= RTE_ETHTYPE_FLAGS_DROP;
	}

	/* Check if the next non-void item is END */
	act = next_no_void_action(actions, act);
	if (act->type != RTE_FLOW_ACTION_TYPE_END) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ACTION,
				act, "Not supported action.");
		return -rte_errno;
	}

	return 0;
}

/**
 * Parse the rule to see if it is a ethertype rule.
 * And get the ethertype filter info BTW.
 * pattern:
 * The first not void item can be ETH.
 * The next not void item must be END.
 * action:
 * The first not void action should be QUEUE.
 * The next not void action should be END.
 * pattern example:
 * ITEM		Spec			Mask
 * ETH		type	0x0807		0xFFFF
 * END
 * other members in mask and spec should set to 0x00.
 * item->last should be NULL.
 */
static int cons_parse_ethertype_filter(const struct rte_flow_attr *attr,
			const struct rte_flow_item *pattern,
			const struct rte_flow_action *actions,
			struct rte_eth_ethertype_filter *filter,
			struct rte_flow_error *error)
{
	const struct rte_flow_item *item;
	const struct rte_flow_action *act = NULL;
	const struct rte_flow_item_eth *eth_spec;
	const struct rte_flow_item_eth *eth_mask;
	const struct rte_flow_action_queue *act_q = NULL;

	if (hinic_check_filter_arg(attr, pattern, actions, error))
		return -rte_errno;

	item = next_no_void_pattern(pattern, NULL);
	if (hinic_check_ethertype_first_item(item, error))
		return -rte_errno;

	eth_spec = (const struct rte_flow_item_eth *)item->spec;
	eth_mask = (const struct rte_flow_item_eth *)item->mask;

	/*
	 * Mask bits of source MAC address must be full of 0.
	 * Mask bits of destination MAC address must be full
	 * of 1 or full of 0.
	 */
	if (!rte_is_zero_ether_addr(&eth_mask->src) ||
	    (!rte_is_zero_ether_addr(&eth_mask->dst) &&
	     !rte_is_broadcast_ether_addr(&eth_mask->dst))) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Invalid ether address mask");
		return -rte_errno;
	}

	if ((eth_mask->type & UINT16_MAX) != UINT16_MAX) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Invalid ethertype mask");
		return -rte_errno;
	}

	/*
	 * If mask bits of destination MAC address
	 * are full of 1, set RTE_ETHTYPE_FLAGS_MAC.
	 */
	if (rte_is_broadcast_ether_addr(&eth_mask->dst)) {
		filter->mac_addr = eth_spec->dst;
		filter->flags |= RTE_ETHTYPE_FLAGS_MAC;
	} else {
		filter->flags &= ~RTE_ETHTYPE_FLAGS_MAC;
	}
	filter->ether_type = rte_be_to_cpu_16(eth_spec->type);

	/* Check if the next non-void item is END. */
	item = next_no_void_pattern(pattern, item);
	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by ethertype filter.");
		return -rte_errno;
	}

	if (hinic_parse_ethertype_aciton(actions, act, act_q, filter, error))
		return -rte_errno;

	if (hinic_check_ethertype_attr_ele(attr, error))
		return -rte_errno;

	return 0;
}

static int hinic_parse_ethertype_filter(struct rte_eth_dev *dev,
			const struct rte_flow_attr *attr,
			const struct rte_flow_item pattern[],
			const struct rte_flow_action actions[],
			struct rte_eth_ethertype_filter *filter,
			struct rte_flow_error *error)
{
	if (cons_parse_ethertype_filter(attr, pattern, actions, filter, error))
		return -rte_errno;

	/* NIC doesn't support MAC address. */
	if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
		memset(filter, 0, sizeof(struct rte_eth_ethertype_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			NULL, "Not supported by ethertype filter");
		return -rte_errno;
	}

	if (filter->queue >= dev->data->nb_rx_queues) {
		memset(filter, 0, sizeof(struct rte_eth_ethertype_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			NULL, "Queue index much too big");
		return -rte_errno;
	}

	if (filter->ether_type == RTE_ETHER_TYPE_IPV4 ||
		filter->ether_type == RTE_ETHER_TYPE_IPV6) {
		memset(filter, 0, sizeof(struct rte_eth_ethertype_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			NULL, "IPv4/IPv6 not supported by ethertype filter");
		return -rte_errno;
	}

	if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
		memset(filter, 0, sizeof(struct rte_eth_ethertype_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			NULL, "Drop option is unsupported");
		return -rte_errno;
	}

	/* Hinic only support LACP/ARP for ether type */
	if (filter->ether_type != RTE_ETHER_TYPE_SLOW &&
		filter->ether_type != RTE_ETHER_TYPE_ARP) {
		memset(filter, 0, sizeof(struct rte_eth_ethertype_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM, NULL,
			"only lacp/arp type supported by ethertype filter");
		return -rte_errno;
	}

	return 0;
}

static int hinic_check_ntuple_attr_ele(const struct rte_flow_attr *attr,
				struct rte_eth_ntuple_filter *filter,
				struct rte_flow_error *error)
{
	/* Must be input direction */
	if (!attr->ingress) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
				   attr, "Only support ingress.");
		return -rte_errno;
	}

	if (attr->egress) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
				   attr, "Not support egress.");
		return -rte_errno;
	}

	if (attr->priority > 0xFFFF) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
				   attr, "Error priority.");
		return -rte_errno;
	}

	if (attr->priority < HINIC_MIN_N_TUPLE_PRIO ||
		    attr->priority > HINIC_MAX_N_TUPLE_PRIO)
		filter->priority = 1;
	else
		filter->priority = (uint16_t)attr->priority;

	return 0;
}

static int
hinic_check_ntuple_act_ele(__rte_unused const struct rte_flow_item *item,
			const struct rte_flow_action actions[],
			struct rte_eth_ntuple_filter *filter,
			struct rte_flow_error *error)
{
	const struct rte_flow_action *act;
	/*
	 * n-tuple only supports forwarding,
	 * check if the first not void action is QUEUE.
	 */
	act = next_no_void_action(actions, NULL);
	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ACTION,
			act, "Flow action type is not QUEUE.");
		return -rte_errno;
	}
	filter->queue =
		((const struct rte_flow_action_queue *)act->conf)->index;

	/* Check if the next not void item is END */
	act = next_no_void_action(actions, act);
	if (act->type != RTE_FLOW_ACTION_TYPE_END) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ACTION,
			act, "Next not void item is not END.");
		return -rte_errno;
	}

	return 0;
}

static int hinic_ntuple_item_check_ether(const struct rte_flow_item **ipv4_item,
					const struct rte_flow_item pattern[],
					struct rte_flow_error *error)
{
	const struct rte_flow_item *item;

	/* The first not void item can be MAC or IPv4 */
	item = next_no_void_pattern(pattern, NULL);

	if (item->type != RTE_FLOW_ITEM_TYPE_ETH &&
		item->type != RTE_FLOW_ITEM_TYPE_IPV4) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by ntuple filter");
		return -rte_errno;
	}

	/* Skip Ethernet */
	if (item->type == RTE_FLOW_ITEM_TYPE_ETH) {
		/* Not supported last point for range */
		if (item->last) {
			rte_flow_error_set(error,
				EINVAL,
				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				item, "Not supported last point for range");
			return -rte_errno;
		}
		/* if the first item is MAC, the content should be NULL */
		if (item->spec || item->mask) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ntuple filter");
			return -rte_errno;
		}
		/* check if the next not void item is IPv4 */
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_IPV4) {
			rte_flow_error_set(error,
				EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ntuple filter");
			return -rte_errno;
		}
	}

	*ipv4_item = item;
	return 0;
}

static int
hinic_ntuple_item_check_ipv4(const struct rte_flow_item **in_out_item,
			const struct rte_flow_item pattern[],
			struct rte_eth_ntuple_filter *filter,
			struct rte_flow_error *error)
{
	const struct rte_flow_item_ipv4 *ipv4_spec;
	const struct rte_flow_item_ipv4 *ipv4_mask;
	const struct rte_flow_item *item = *in_out_item;

	/* Get the IPv4 info */
	if (!item->spec || !item->mask) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Invalid ntuple mask");
		return -rte_errno;
	}
	/* Not supported last point for range */
	if (item->last) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
			item, "Not supported last point for range");
		return -rte_errno;
	}

	ipv4_mask = (const struct rte_flow_item_ipv4 *)item->mask;
	/*
	 * Only support src & dst addresses, protocol,
	 * others should be masked.
	 */
	if (ipv4_mask->hdr.version_ihl ||
		ipv4_mask->hdr.type_of_service ||
		ipv4_mask->hdr.total_length ||
		ipv4_mask->hdr.packet_id ||
		ipv4_mask->hdr.fragment_offset ||
		ipv4_mask->hdr.time_to_live ||
		ipv4_mask->hdr.hdr_checksum ||
		!ipv4_mask->hdr.next_proto_id) {
		rte_flow_error_set(error,
			EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by ntuple filter");
		return -rte_errno;
	}

	filter->dst_ip_mask = ipv4_mask->hdr.dst_addr;
	filter->src_ip_mask = ipv4_mask->hdr.src_addr;
	filter->proto_mask = ipv4_mask->hdr.next_proto_id;

	ipv4_spec = (const struct rte_flow_item_ipv4 *)item->spec;
	filter->dst_ip = ipv4_spec->hdr.dst_addr;
	filter->src_ip = ipv4_spec->hdr.src_addr;
	filter->proto  = ipv4_spec->hdr.next_proto_id;

	/* Get next no void item */
	*in_out_item = next_no_void_pattern(pattern, item);
	return 0;
}

static int hinic_ntuple_item_check_l4(const struct rte_flow_item **in_out_item,
				const struct rte_flow_item pattern[],
				struct rte_eth_ntuple_filter *filter,
				struct rte_flow_error *error)
{
	const struct rte_flow_item_tcp *tcp_spec;
	const struct rte_flow_item_tcp *tcp_mask;
	const struct rte_flow_item_icmp *icmp_mask;
	const struct rte_flow_item *item = *in_out_item;
	u32 ntuple_filter_size = sizeof(struct rte_eth_ntuple_filter);

	if (item->type == RTE_FLOW_ITEM_TYPE_END)
		return 0;

	/* Get TCP or UDP info */
	if (item->type != RTE_FLOW_ITEM_TYPE_END &&
		(!item->spec || !item->mask)) {
		memset(filter, 0, ntuple_filter_size);
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Invalid ntuple mask");
		return -rte_errno;
	}

	/* Not supported last point for range */
	if (item->last) {
		memset(filter, 0, ntuple_filter_size);
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
			item, "Not supported last point for range");
		return -rte_errno;
	}

	if (item->type == RTE_FLOW_ITEM_TYPE_TCP) {
		tcp_mask = (const struct rte_flow_item_tcp *)item->mask;

		/*
		 * Only support src & dst ports, tcp flags,
		 * others should be masked.
		 */
		if (tcp_mask->hdr.sent_seq ||
			tcp_mask->hdr.recv_ack ||
			tcp_mask->hdr.data_off ||
			tcp_mask->hdr.rx_win ||
			tcp_mask->hdr.cksum ||
			tcp_mask->hdr.tcp_urp) {
			memset(filter, 0, ntuple_filter_size);
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ntuple filter");
			return -rte_errno;
		}

		filter->dst_port_mask  = tcp_mask->hdr.dst_port;
		filter->src_port_mask  = tcp_mask->hdr.src_port;
		if (tcp_mask->hdr.tcp_flags == 0xFF) {
			filter->flags |= RTE_NTUPLE_FLAGS_TCP_FLAG;
		} else if (!tcp_mask->hdr.tcp_flags) {
			filter->flags &= ~RTE_NTUPLE_FLAGS_TCP_FLAG;
		} else {
			memset(filter, 0, ntuple_filter_size);
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ntuple filter");
			return -rte_errno;
		}

		tcp_spec = (const struct rte_flow_item_tcp *)item->spec;
		filter->dst_port  = tcp_spec->hdr.dst_port;
		filter->src_port  = tcp_spec->hdr.src_port;
		filter->tcp_flags = tcp_spec->hdr.tcp_flags;
	} else if (item->type == RTE_FLOW_ITEM_TYPE_ICMP) {
		icmp_mask = (const struct rte_flow_item_icmp *)item->mask;

		/* ICMP all should be masked. */
		if (icmp_mask->hdr.icmp_cksum ||
			icmp_mask->hdr.icmp_ident ||
			icmp_mask->hdr.icmp_seq_nb ||
			icmp_mask->hdr.icmp_type ||
			icmp_mask->hdr.icmp_code) {
			memset(filter, 0, ntuple_filter_size);
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by ntuple filter");
			return -rte_errno;
		}
	}

	/* Get next no void item */
	*in_out_item = next_no_void_pattern(pattern, item);
	return 0;
}

static int hinic_ntuple_item_check_end(const struct rte_flow_item *item,
					struct rte_eth_ntuple_filter *filter,
					struct rte_flow_error *error)
{
	/* Check if the next not void item is END */
	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by ntuple filter");
		return -rte_errno;
	}
	return 0;
}

static int hinic_check_ntuple_item_ele(const struct rte_flow_item *item,
					const struct rte_flow_item pattern[],
					struct rte_eth_ntuple_filter *filter,
					struct rte_flow_error *error)
{
	if (hinic_ntuple_item_check_ether(&item, pattern, error) ||
		hinic_ntuple_item_check_ipv4(&item, pattern, filter, error) ||
		hinic_ntuple_item_check_l4(&item, pattern, filter, error) ||
		hinic_ntuple_item_check_end(item, filter, error))
		return -rte_errno;

	return 0;
}

/**
 * Parse the rule to see if it is a n-tuple rule.
 * And get the n-tuple filter info BTW.
 * pattern:
 * The first not void item can be ETH or IPV4.
 * The second not void item must be IPV4 if the first one is ETH.
 * The third not void item must be UDP or TCP.
 * The next not void item must be END.
 * action:
 * The first not void action should be QUEUE.
 * The next not void action should be END.
 * pattern example:
 * ITEM		Spec			Mask
 * ETH		NULL			NULL
 * IPV4		src_addr 192.168.1.20	0xFFFFFFFF
 *		dst_addr 192.167.3.50	0xFFFFFFFF
 *		next_proto_id	17	0xFF
 * UDP/TCP/	src_port	80	0xFFFF
 * SCTP		dst_port	80	0xFFFF
 * END
 * other members in mask and spec should set to 0x00.
 * item->last should be NULL.
 * Please aware there's an asumption for all the parsers.
 * rte_flow_item is using big endian, rte_flow_attr and
 * rte_flow_action are using CPU order.
 * Because the pattern is used to describe the packets,
 * normally the packets should use network order.
 */
static int cons_parse_ntuple_filter(const struct rte_flow_attr *attr,
			const struct rte_flow_item pattern[],
			const struct rte_flow_action actions[],
			struct rte_eth_ntuple_filter *filter,
			struct rte_flow_error *error)
{
	const struct rte_flow_item *item = NULL;

	if (hinic_check_filter_arg(attr, pattern, actions, error))
		return -rte_errno;

	if (hinic_check_ntuple_item_ele(item, pattern, filter, error))
		return -rte_errno;

	if (hinic_check_ntuple_act_ele(item, actions, filter, error))
		return -rte_errno;

	if (hinic_check_ntuple_attr_ele(attr, filter, error))
		return -rte_errno;

	return 0;
}

static int hinic_parse_ntuple_filter(struct rte_eth_dev *dev,
			const struct rte_flow_attr *attr,
			const struct rte_flow_item pattern[],
			const struct rte_flow_action actions[],
			struct rte_eth_ntuple_filter *filter,
			struct rte_flow_error *error)
{
	int ret;

	ret = cons_parse_ntuple_filter(attr, pattern, actions, filter, error);
	if (ret)
		return ret;

	/* Hinic doesn't support tcp flags */
	if (filter->flags & RTE_NTUPLE_FLAGS_TCP_FLAG) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ITEM,
				   NULL, "Not supported by ntuple filter");
		return -rte_errno;
	}

	/* Hinic doesn't support many priorities */
	if (filter->priority < HINIC_MIN_N_TUPLE_PRIO ||
	    filter->priority > HINIC_MAX_N_TUPLE_PRIO) {
		memset(filter, 0, sizeof(struct rte_eth_ntuple_filter));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			NULL, "Priority not supported by ntuple filter");
		return -rte_errno;
	}

	if (filter->queue >= dev->data->nb_rx_queues)
		return -rte_errno;

	/* Fixed value for hinic */
	filter->flags = RTE_5TUPLE_FLAGS;
	return 0;
}

static int hinic_normal_item_check_ether(const struct rte_flow_item **ip_item,
					const struct rte_flow_item pattern[],
					struct rte_flow_error *error)
{
	const struct rte_flow_item *item;

	/* The first not void item can be MAC or IPv4  or TCP or UDP */
	item = next_no_void_pattern(pattern, NULL);

	if (item->type != RTE_FLOW_ITEM_TYPE_ETH &&
		item->type != RTE_FLOW_ITEM_TYPE_IPV4 &&
		item->type != RTE_FLOW_ITEM_TYPE_TCP &&
		item->type != RTE_FLOW_ITEM_TYPE_UDP) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM, item,
			"Not supported by fdir filter,support mac,ipv4,tcp,udp");
		return -rte_errno;
	}

	/* Not supported last point for range */
	if (item->last) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_UNSPECIFIED, item,
			"Not supported last point for range");
		return -rte_errno;
	}

	/* Skip Ethernet */
	if (item->type == RTE_FLOW_ITEM_TYPE_ETH) {
		/* All should be masked. */
		if (item->spec || item->mask) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter,support mac");
			return -rte_errno;
		}
		/* Check if the next not void item is IPv4 */
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_IPV4 &&
			item->type != RTE_FLOW_ITEM_TYPE_IPV6) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter,support mac,ipv4");
			return -rte_errno;
		}
	}

	*ip_item = item;
	return 0;
}

static int hinic_normal_item_check_ip(const struct rte_flow_item **in_out_item,
				const struct rte_flow_item pattern[],
				struct hinic_fdir_rule *rule,
				struct rte_flow_error *error)
{
	const struct rte_flow_item_ipv4 *ipv4_spec;
	const struct rte_flow_item_ipv4 *ipv4_mask;
	const struct rte_flow_item_ipv6 *ipv6_spec;
	const struct rte_flow_item_ipv6 *ipv6_mask;
	const struct rte_flow_item *item = *in_out_item;
	int i;

	/* Get the IPv4 info */
	if (item->type == RTE_FLOW_ITEM_TYPE_IPV4) {
		/* Not supported last point for range */
		if (item->last) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				item, "Not supported last point for range");
			return -rte_errno;
		}

		if (!item->mask) {
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Invalid fdir filter mask");
			return -rte_errno;
		}

		ipv4_mask = (const struct rte_flow_item_ipv4 *)item->mask;
		/*
		 * Only support src & dst addresses,
		 * others should be masked.
		 */
		if (ipv4_mask->hdr.version_ihl ||
			ipv4_mask->hdr.type_of_service ||
			ipv4_mask->hdr.total_length ||
			ipv4_mask->hdr.packet_id ||
			ipv4_mask->hdr.fragment_offset ||
			ipv4_mask->hdr.time_to_live ||
			ipv4_mask->hdr.next_proto_id ||
			ipv4_mask->hdr.hdr_checksum) {
			rte_flow_error_set(error,
				EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter, support src,dst ip");
			return -rte_errno;
		}

		rule->mask.dst_ipv4_mask = ipv4_mask->hdr.dst_addr;
		rule->mask.src_ipv4_mask = ipv4_mask->hdr.src_addr;
		rule->mode = HINIC_FDIR_MODE_NORMAL;

		if (item->spec) {
			ipv4_spec =
				(const struct rte_flow_item_ipv4 *)item->spec;
			rule->hinic_fdir.dst_ip = ipv4_spec->hdr.dst_addr;
			rule->hinic_fdir.src_ip = ipv4_spec->hdr.src_addr;
		}

		/*
		 * Check if the next not void item is
		 * TCP or UDP or END.
		 */
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_TCP &&
		    item->type != RTE_FLOW_ITEM_TYPE_UDP &&
		    item->type != RTE_FLOW_ITEM_TYPE_ICMP &&
		    item->type != RTE_FLOW_ITEM_TYPE_ANY &&
		    item->type != RTE_FLOW_ITEM_TYPE_END) {
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter, support tcp, udp, end");
			return -rte_errno;
		}
	} else if (item->type == RTE_FLOW_ITEM_TYPE_IPV6) {
		/* Not supported last point for range */
		if (item->last) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				item, "Not supported last point for range");
			return -rte_errno;
		}

		if (!item->mask) {
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Invalid fdir filter mask");
			return -rte_errno;
		}

		ipv6_mask = (const struct rte_flow_item_ipv6 *)item->mask;

		/* Only support dst addresses,  others should be masked */
		if (ipv6_mask->hdr.vtc_flow ||
		    ipv6_mask->hdr.payload_len ||
		    ipv6_mask->hdr.proto ||
		    ipv6_mask->hdr.hop_limits) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter, support dst ipv6");
			return -rte_errno;
		}

		/* check ipv6 src addr mask, ipv6 src addr is 16 bytes */
		for (i = 0; i < 16; i++) {
			if (ipv6_mask->hdr.src_addr[i] == UINT8_MAX) {
				rte_flow_error_set(error, EINVAL,
					RTE_FLOW_ERROR_TYPE_ITEM, item,
					"Not supported by fdir filter, do not support src ipv6");
				return -rte_errno;
			}
		}

		if (!item->spec) {
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter, ipv6 spec is NULL");
			return -rte_errno;
		}

		for (i = 0; i < 16; i++) {
			if (ipv6_mask->hdr.dst_addr[i] == UINT8_MAX)
				rule->mask.dst_ipv6_mask |= 1 << i;
		}

		ipv6_spec = (const struct rte_flow_item_ipv6 *)item->spec;
		rte_memcpy(rule->hinic_fdir.dst_ipv6,
			   ipv6_spec->hdr.dst_addr, 16);

		/*
		 * Check if the next not void item is TCP or UDP or ICMP.
		 */
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_TCP &&
		    item->type != RTE_FLOW_ITEM_TYPE_UDP &&
		    item->type != RTE_FLOW_ITEM_TYPE_ICMP &&
		    item->type != RTE_FLOW_ITEM_TYPE_ICMP6){
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM, item,
				"Not supported by fdir filter, support tcp, udp, icmp");
			return -rte_errno;
		}
	}

	*in_out_item = item;
	return 0;
}

static int hinic_normal_item_check_l4(const struct rte_flow_item **in_out_item,
			__rte_unused const struct rte_flow_item pattern[],
			__rte_unused struct hinic_fdir_rule *rule,
			struct rte_flow_error *error)
{
	const struct rte_flow_item *item = *in_out_item;

	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by normal fdir filter, not support l4");
		return -rte_errno;
	}

	return 0;
}


static int hinic_normal_item_check_end(const struct rte_flow_item *item,
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	/* Check if the next not void item is END */
	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by fdir filter, support end");
		return -rte_errno;
	}

	return 0;
}

static int hinic_check_normal_item_ele(const struct rte_flow_item *item,
					const struct rte_flow_item pattern[],
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	if (hinic_normal_item_check_ether(&item, pattern, error) ||
	    hinic_normal_item_check_ip(&item, pattern, rule, error) ||
	    hinic_normal_item_check_l4(&item, pattern, rule, error) ||
	    hinic_normal_item_check_end(item, rule, error))
		return -rte_errno;

	return 0;
}

static int
hinic_tcam_normal_item_check_l4(const struct rte_flow_item **in_out_item,
				const struct rte_flow_item pattern[],
				struct hinic_fdir_rule *rule,
				struct rte_flow_error *error)
{
	const struct rte_flow_item *item = *in_out_item;
	const struct rte_flow_item_tcp *tcp_spec;
	const struct rte_flow_item_tcp *tcp_mask;
	const struct rte_flow_item_udp *udp_spec;
	const struct rte_flow_item_udp *udp_mask;

	if (item->type == RTE_FLOW_ITEM_TYPE_ICMP) {
		rule->mode = HINIC_FDIR_MODE_TCAM;
		rule->mask.proto_mask = UINT16_MAX;
		rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_ICMP;
	} else if (item->type == RTE_FLOW_ITEM_TYPE_ICMP6) {
		rule->mode = HINIC_FDIR_MODE_TCAM;
		rule->mask.proto_mask = UINT16_MAX;
		rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_ICMPV6;
	} else if (item->type == RTE_FLOW_ITEM_TYPE_ANY) {
		rule->mode = HINIC_FDIR_MODE_TCAM;
	} else if (item->type == RTE_FLOW_ITEM_TYPE_TCP) {
		if (!item->mask) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support src, dst ports");
			return -rte_errno;
		}

		tcp_mask = (const struct rte_flow_item_tcp *)item->mask;

		/*
		 * Only support src & dst ports, tcp flags,
		 * others should be masked.
		 */
		if (tcp_mask->hdr.sent_seq ||
			tcp_mask->hdr.recv_ack ||
			tcp_mask->hdr.data_off ||
			tcp_mask->hdr.rx_win ||
			tcp_mask->hdr.cksum ||
			tcp_mask->hdr.tcp_urp) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir normal tcam filter");
			return -rte_errno;
		}

		rule->mode = HINIC_FDIR_MODE_TCAM;
		rule->mask.proto_mask = UINT16_MAX;
		rule->mask.dst_port_mask = tcp_mask->hdr.dst_port;
		rule->mask.src_port_mask = tcp_mask->hdr.src_port;

		rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_TCP;
		if (item->spec) {
			tcp_spec = (const struct rte_flow_item_tcp *)item->spec;
			rule->hinic_fdir.dst_port = tcp_spec->hdr.dst_port;
			rule->hinic_fdir.src_port = tcp_spec->hdr.src_port;
		}
	} else if (item->type == RTE_FLOW_ITEM_TYPE_UDP) {
		/*
		 * Only care about src & dst ports,
		 * others should be masked.
		 */
		if (!item->mask) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support src, dst ports");
			return -rte_errno;
		}

		udp_mask = (const struct rte_flow_item_udp *)item->mask;
		if (udp_mask->hdr.dgram_len ||
			udp_mask->hdr.dgram_cksum) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support udp");
			return -rte_errno;
		}

		rule->mode = HINIC_FDIR_MODE_TCAM;
		rule->mask.proto_mask = UINT16_MAX;
		rule->mask.src_port_mask = udp_mask->hdr.src_port;
		rule->mask.dst_port_mask = udp_mask->hdr.dst_port;

		rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_UDP;
		if (item->spec) {
			udp_spec = (const struct rte_flow_item_udp *)item->spec;
			rule->hinic_fdir.src_port = udp_spec->hdr.src_port;
			rule->hinic_fdir.dst_port = udp_spec->hdr.dst_port;
		}
	} else {
		(void)memset(rule,  0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter tcam normal, l4 only support icmp, tcp");
		return -rte_errno;
	}

	item = next_no_void_pattern(pattern, item);
	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ITEM,
			item, "Not supported by fdir filter tcam normal, support end");
		return -rte_errno;
	}

	/* get next no void item */
	*in_out_item = item;

	return 0;
}

static int hinic_check_tcam_normal_item_ele(const struct rte_flow_item *item,
					const struct rte_flow_item pattern[],
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	if (hinic_normal_item_check_ether(&item, pattern, error) ||
		hinic_normal_item_check_ip(&item, pattern, rule, error) ||
		hinic_tcam_normal_item_check_l4(&item, pattern, rule, error) ||
		hinic_normal_item_check_end(item, rule, error))
		return -rte_errno;

	return 0;
}

static int hinic_tunnel_item_check_l4(const struct rte_flow_item **in_out_item,
					const struct rte_flow_item pattern[],
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	const struct rte_flow_item *item = *in_out_item;

	if (item->type == RTE_FLOW_ITEM_TYPE_UDP) {
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support vxlan");
			return -rte_errno;
		}

		*in_out_item = item;
	} else {
		(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter tcam tunnel, outer l4 only support udp");
		return -rte_errno;
	}

	return 0;
}

static int
hinic_tunnel_item_check_vxlan(const struct rte_flow_item **in_out_item,
				const struct rte_flow_item pattern[],
				struct hinic_fdir_rule *rule,
				struct rte_flow_error *error)
{
	const struct rte_flow_item *item = *in_out_item;


	if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN) {
		item = next_no_void_pattern(pattern, item);
		if (item->type != RTE_FLOW_ITEM_TYPE_TCP &&
		    item->type != RTE_FLOW_ITEM_TYPE_UDP &&
		    item->type != RTE_FLOW_ITEM_TYPE_ANY) {
			(void)memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support tcp/udp");
			return -rte_errno;
		}

		*in_out_item = item;
	}

	return 0;
}

static int
hinic_tunnel_inner_item_check_l4(const struct rte_flow_item **in_out_item,
				const struct rte_flow_item pattern[],
				struct hinic_fdir_rule *rule,
				struct rte_flow_error *error)
{
	const struct rte_flow_item_tcp *tcp_spec;
	const struct rte_flow_item_tcp *tcp_mask;
	const struct rte_flow_item_udp *udp_spec;
	const struct rte_flow_item_udp *udp_mask;
	const struct rte_flow_item *item = *in_out_item;

	if (item->type != RTE_FLOW_ITEM_TYPE_END) {
		/* Not supported last point for range */
		if (item->last) {
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
				item, "Not supported last point for range");
			return -rte_errno;
		}

		/* get the TCP/UDP info */
		if (item->type == RTE_FLOW_ITEM_TYPE_TCP) {
			/*
			 * Only care about src & dst ports,
			 * others should be masked.
			 */
			if (!item->mask) {
				memset(rule, 0, sizeof(struct hinic_fdir_rule));
				rte_flow_error_set(error, EINVAL,
					RTE_FLOW_ERROR_TYPE_ITEM,
					item, "Not supported by fdir filter, support src, dst ports");
				return -rte_errno;
			}

			tcp_mask = (const struct rte_flow_item_tcp *)item->mask;
			if (tcp_mask->hdr.sent_seq ||
				tcp_mask->hdr.recv_ack ||
				tcp_mask->hdr.data_off ||
				tcp_mask->hdr.tcp_flags ||
				tcp_mask->hdr.rx_win ||
				tcp_mask->hdr.cksum ||
				tcp_mask->hdr.tcp_urp) {
				(void)memset(rule, 0,
					sizeof(struct hinic_fdir_rule));
				rte_flow_error_set(error, EINVAL,
					RTE_FLOW_ERROR_TYPE_ITEM,
					item, "Not supported by fdir filter, support tcp");
				return -rte_errno;
			}

			rule->mode = HINIC_FDIR_MODE_TCAM;
			rule->mask.tunnel_flag = UINT16_MAX;
			rule->mask.tunnel_inner_src_port_mask =
							tcp_mask->hdr.src_port;
			rule->mask.tunnel_inner_dst_port_mask =
							tcp_mask->hdr.dst_port;
			rule->mask.proto_mask = UINT16_MAX;

			rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_TCP;
			if (item->spec) {
				tcp_spec =
				(const struct rte_flow_item_tcp *)item->spec;
				rule->hinic_fdir.tunnel_inner_src_port =
							tcp_spec->hdr.src_port;
				rule->hinic_fdir.tunnel_inner_dst_port =
							tcp_spec->hdr.dst_port;
			}
		} else if (item->type == RTE_FLOW_ITEM_TYPE_UDP) {
			/*
			 * Only care about src & dst ports,
			 * others should be masked.
			 */
			if (!item->mask) {
				memset(rule, 0, sizeof(struct hinic_fdir_rule));
				rte_flow_error_set(error, EINVAL,
					RTE_FLOW_ERROR_TYPE_ITEM,
					item, "Not supported by fdir filter, support src, dst ports");
				return -rte_errno;
			}

			udp_mask = (const struct rte_flow_item_udp *)item->mask;
			if (udp_mask->hdr.dgram_len ||
			    udp_mask->hdr.dgram_cksum) {
				memset(rule, 0, sizeof(struct hinic_fdir_rule));
				rte_flow_error_set(error, EINVAL,
					RTE_FLOW_ERROR_TYPE_ITEM,
					item, "Not supported by fdir filter, support udp");
				return -rte_errno;
			}

			rule->mode = HINIC_FDIR_MODE_TCAM;
			rule->mask.tunnel_flag = UINT16_MAX;
			rule->mask.tunnel_inner_src_port_mask =
							udp_mask->hdr.src_port;
			rule->mask.tunnel_inner_dst_port_mask =
							udp_mask->hdr.dst_port;
			rule->mask.proto_mask = UINT16_MAX;

			rule->hinic_fdir.proto = IP_HEADER_PROTOCOL_TYPE_UDP;
			if (item->spec) {
				udp_spec =
				(const struct rte_flow_item_udp *)item->spec;
				rule->hinic_fdir.tunnel_inner_src_port =
							udp_spec->hdr.src_port;
				rule->hinic_fdir.tunnel_inner_dst_port =
							udp_spec->hdr.dst_port;
			}
		} else if (item->type == RTE_FLOW_ITEM_TYPE_ANY) {
			rule->mode = HINIC_FDIR_MODE_TCAM;
			rule->mask.tunnel_flag = UINT16_MAX;
		} else {
			memset(rule, 0, sizeof(struct hinic_fdir_rule));
			rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ITEM,
				item, "Not supported by fdir filter, support tcp/udp");
			return -rte_errno;
		}

		/* get next no void item */
		*in_out_item = next_no_void_pattern(pattern, item);
	}

	return 0;
}

static int hinic_check_tcam_tunnel_item_ele(const struct rte_flow_item *item,
					const struct rte_flow_item pattern[],
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	if (hinic_normal_item_check_ether(&item, pattern, error) ||
		hinic_normal_item_check_ip(&item, pattern, rule, error) ||
		hinic_tunnel_item_check_l4(&item, pattern, rule, error) ||
		hinic_tunnel_item_check_vxlan(&item, pattern, rule, error) ||
		hinic_tunnel_inner_item_check_l4(&item, pattern, rule, error) ||
		hinic_normal_item_check_end(item, rule, error))
		return -rte_errno;

	return 0;
}

static int hinic_check_normal_attr_ele(const struct rte_flow_attr *attr,
					struct hinic_fdir_rule *rule,
					struct rte_flow_error *error)
{
	/* Must be input direction */
	if (!attr->ingress) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
				   attr, "Only support ingress.");
		return -rte_errno;
	}

	/* Not supported */
	if (attr->egress) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
				   attr, "Not support egress.");
		return -rte_errno;
	}

	/* Not supported */
	if (attr->priority) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL,
			RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
			attr, "Not support priority.");
		return -rte_errno;
	}

	return 0;
}

static int hinic_check_normal_act_ele(const struct rte_flow_item *item,
				const struct rte_flow_action actions[],
				struct hinic_fdir_rule *rule,
				struct rte_flow_error *error)
{
	const struct rte_flow_action *act;

	/* Check if the first not void action is QUEUE */
	act = next_no_void_action(actions, NULL);
	if (act->type != RTE_FLOW_ACTION_TYPE_QUEUE) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
			item, "Not supported action.");
		return -rte_errno;
	}

	rule->queue = ((const struct rte_flow_action_queue *)act->conf)->index;

	/* Check if the next not void item is END */
	act = next_no_void_action(actions, act);
	if (act->type != RTE_FLOW_ACTION_TYPE_END) {
		memset(rule, 0, sizeof(struct hinic_fdir_rule));
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
			act, "Not supported action.");
		return -rte_errno;
	}

	return 0;
}

/**
 * Parse the rule to see if it is a IP or MAC VLAN flow director rule.
 * And get the flow director filter info BTW.
 * UDP/TCP/SCTP PATTERN:
 * The first not void item can be ETH or IPV4 or IPV6
 * The second not void item must be IPV4 or IPV6 if the first one is ETH.
 * The next not void item could be UDP or TCP(optional)
 * The next not void item must be END.
 * ACTION:
 * The first not void action should be QUEUE.
 * The second not void optional action should be MARK,
 * mark_id is a uint32_t number.
 * The next not void action should be END.
 * UDP/TCP pattern example:
 * ITEM          Spec	                                    Mask
 * ETH            NULL                                    NULL
 * IPV4           src_addr  1.2.3.6                 0xFFFFFFFF
 *                   dst_addr  1.2.3.5                 0xFFFFFFFF
 * UDP/TCP    src_port  80                         0xFFFF
 *                   dst_port  80                         0xFFFF
 * END
 * Other members in mask and spec should set to 0x00.
 * Item->last should be NULL.
 */
static int
hinic_parse_fdir_filter_normal(const struct rte_flow_attr *attr,
			       const struct rte_flow_item pattern[],
			       const struct rte_flow_action actions[],
			       struct hinic_fdir_rule *rule,
			       struct rte_flow_error *error)
{
	const struct rte_flow_item *item = NULL;

	if (hinic_check_filter_arg(attr, pattern, actions, error))
		return -rte_errno;

	if (hinic_check_normal_item_ele(item, pattern, rule, error))
		return -rte_errno;

	if (hinic_check_normal_attr_ele(attr, rule, error))
		return -rte_errno;

	if (hinic_check_normal_act_ele(item, actions, rule, error))
		return -rte_errno;

	return 0;
}

/**
 * Parse the rule to see if it is a IP or MAC VLAN flow director rule.
 * And get the flow director filter info BTW.
 * UDP/TCP/SCTP PATTERN:
 * The first not void item can be ETH or IPV4 or IPV6
 * The second not void item must be IPV4 or IPV6 if the first one is ETH.
 * The next not void item can be ANY/TCP/UDP
 * ACTION:
 * The first not void action should be QUEUE.
 * The second not void optional action should be MARK,
 * mark_id is a uint32_t number.
 * The next not void action should be END.
 * UDP/TCP pattern example:
 * ITEM                 Spec	                       Mask
 * ETH            NULL                                 NULL
 * IPV4           src_addr  1.2.3.6                 0xFFFFFFFF
 *                dst_addr  1.2.3.5                 0xFFFFFFFF
 * UDP/TCP        src_port  80                      0xFFFF
 *                dst_port  80                      0xFFFF
 * END
 * Other members in mask and spec should set to 0x00.
 * Item->last should be NULL.
 */
static int
hinic_parse_fdir_filter_tcam_normal(const struct rte_flow_attr *attr,
			       const struct rte_flow_item pattern[],
			       const struct rte_flow_action actions[],
			       struct hinic_fdir_rule *rule,
			       struct rte_flow_error *error)
{
	const struct rte_flow_item *item = NULL;

	if (hinic_check_filter_arg(attr, pattern, actions, error))
		return -rte_errno;

	if (hinic_check_tcam_normal_item_ele(item, pattern, rule, error))
		return -rte_errno;

	if (hinic_check_normal_attr_ele(attr, rule, error))
		return -rte_errno;

	if (hinic_check_normal_act_ele(item, actions, rule, error))
		return -rte_errno;

	return 0;
}

/**
 * Parse the rule to see if it is a IP or MAC VLAN flow director rule.
 * And get the flow director filter info BTW.
 * UDP/TCP/SCTP PATTERN:
 * The first not void item can be ETH or IPV4 or IPV6
 * The second not void item must be IPV4 or IPV6 if the first one is ETH.
 * The next not void item must be UDP
 * The next not void item must be VXLAN(optional)
 * The first not void item can be ETH or IPV4 or IPV6
 * The next not void item could be ANY or UDP or TCP(optional)
 * The next not void item must be END.
 * ACTION:
 * The first not void action should be QUEUE.
 * The second not void optional action should be MARK,
 * mark_id is a uint32_t number.
 * The next not void action should be END.
 * UDP/TCP pattern example:
 * ITEM             Spec	                    Mask
 * ETH            NULL                              NULL
 * IPV4        src_addr  1.2.3.6                 0xFFFFFFFF
 *             dst_addr  1.2.3.5                 0xFFFFFFFF
 * UDP            NULL                              NULL
 * VXLAN          NULL                              NULL
 * UDP/TCP     src_port  80                      0xFFFF
 *             dst_port  80                      0xFFFF
 * END
 * Other members in mask and spec should set to 0x00.
 * Item->last should be NULL.
 */
static int
hinic_parse_fdir_filter_tacm_tunnel(const struct rte_flow_attr *attr,
			       const struct rte_flow_item pattern[],
			       const struct rte_flow_action actions[],
			       struct hinic_fdir_rule *rule,
			       struct rte_flow_error *error)
{
	const struct rte_flow_item *item = NULL;

	if (hinic_check_filter_arg(attr, pattern, actions, error))
		return -rte_errno;

	if (hinic_check_tcam_tunnel_item_ele(item, pattern, rule, error))
		return -rte_errno;

	if (hinic_check_normal_attr_ele(attr, rule, error))
		return -rte_errno;

	if (hinic_check_normal_act_ele(item, actions, rule, error))
		return -rte_errno;

	return 0;
}

static int hinic_parse_fdir_filter(struct rte_eth_dev *dev,
			const struct rte_flow_attr *attr,
			const struct rte_flow_item pattern[],
			const struct rte_flow_action actions[],
			struct hinic_fdir_rule *rule,
			struct rte_flow_error *error)
{
	int ret;

	ret = hinic_parse_fdir_filter_normal(attr, pattern, actions,
						rule, error);
	if (!ret)
		goto step_next;

	ret = hinic_parse_fdir_filter_tcam_normal(attr, pattern, actions,
						rule, error);
	if (!ret)
		goto step_next;

	ret = hinic_parse_fdir_filter_tacm_tunnel(attr, pattern, actions,
						rule, error);
	if (ret)
		return ret;

step_next:
	if (rule->queue >= dev->data->nb_rx_queues)
		return -ENOTSUP;

	return ret;
}

/**
 * Check if the flow rule is supported by nic.
 * It only checkes the format. Don't guarantee the rule can be programmed into
 * the HW. Because there can be no enough room for the rule.
 */
static int hinic_flow_validate(struct rte_eth_dev *dev,
				const struct rte_flow_attr *attr,
				const struct rte_flow_item pattern[],
				const struct rte_flow_action actions[],
				struct rte_flow_error *error)
{
	struct rte_eth_ethertype_filter ethertype_filter;
	struct rte_eth_ntuple_filter ntuple_filter;
	struct hinic_fdir_rule fdir_rule;
	int ret;

	memset(&ntuple_filter, 0, sizeof(struct rte_eth_ntuple_filter));
	ret = hinic_parse_ntuple_filter(dev, attr, pattern,
				actions, &ntuple_filter, error);
	if (!ret)
		return 0;

	memset(&ethertype_filter, 0, sizeof(struct rte_eth_ethertype_filter));
	ret = hinic_parse_ethertype_filter(dev, attr, pattern,
				actions, &ethertype_filter, error);

	if (!ret)
		return 0;

	memset(&fdir_rule, 0, sizeof(struct hinic_fdir_rule));
	ret = hinic_parse_fdir_filter(dev, attr, pattern,
				actions, &fdir_rule, error);

	return ret;
}

static inline int ntuple_ip_filter(struct rte_eth_ntuple_filter *filter,
		 struct hinic_5tuple_filter_info *hinic_filter_info)
{
	switch (filter->dst_ip_mask) {
	case UINT32_MAX:
		hinic_filter_info->dst_ip_mask = 0;
		hinic_filter_info->dst_ip = filter->dst_ip;
		break;
	case 0:
		hinic_filter_info->dst_ip_mask = 1;
		hinic_filter_info->dst_ip = 0;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid dst_ip mask.");
		return -EINVAL;
	}

	switch (filter->src_ip_mask) {
	case UINT32_MAX:
		hinic_filter_info->src_ip_mask = 0;
		hinic_filter_info->src_ip = filter->src_ip;
		break;
	case 0:
		hinic_filter_info->src_ip_mask = 1;
		hinic_filter_info->src_ip = 0;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid src_ip mask.");
		return -EINVAL;
	}
	return 0;
}

static inline int ntuple_port_filter(struct rte_eth_ntuple_filter *filter,
		   struct hinic_5tuple_filter_info *hinic_filter_info)
{
	switch (filter->dst_port_mask) {
	case UINT16_MAX:
		hinic_filter_info->dst_port_mask = 0;
		hinic_filter_info->dst_port = filter->dst_port;
		break;
	case 0:
		hinic_filter_info->dst_port_mask = 1;
		hinic_filter_info->dst_port = 0;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid dst_port mask.");
		return -EINVAL;
	}

	switch (filter->src_port_mask) {
	case UINT16_MAX:
		hinic_filter_info->src_port_mask = 0;
		hinic_filter_info->src_port = filter->src_port;
		break;
	case 0:
		hinic_filter_info->src_port_mask = 1;
		hinic_filter_info->src_port = 0;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid src_port mask.");
		return -EINVAL;
	}

	return 0;
}

static inline int ntuple_proto_filter(struct rte_eth_ntuple_filter *filter,
		    struct hinic_5tuple_filter_info *hinic_filter_info)
{
	switch (filter->proto_mask) {
	case UINT8_MAX:
		hinic_filter_info->proto_mask = 0;
		hinic_filter_info->proto = filter->proto;
		break;
	case 0:
		hinic_filter_info->proto_mask = 1;
		hinic_filter_info->proto = 0;
		break;
	default:
		PMD_DRV_LOG(ERR, "Invalid protocol mask.");
		return -EINVAL;
	}

	return 0;
}

static inline int ntuple_filter_to_5tuple(struct rte_eth_ntuple_filter *filter,
			struct hinic_5tuple_filter_info *filter_info)
{
	if (filter->queue >= HINIC_MAX_RX_QUEUE_NUM ||
		filter->priority > HINIC_MAX_N_TUPLE_PRIO ||
		filter->priority < HINIC_MIN_N_TUPLE_PRIO)
		return -EINVAL;

	if (ntuple_ip_filter(filter, filter_info) ||
		ntuple_port_filter(filter, filter_info) ||
		ntuple_proto_filter(filter, filter_info))
		return -EINVAL;

	filter_info->priority = (uint8_t)filter->priority;
	return 0;
}

static inline struct hinic_5tuple_filter *
hinic_5tuple_filter_lookup(struct hinic_5tuple_filter_list *filter_list,
			   struct hinic_5tuple_filter_info *key)
{
	struct hinic_5tuple_filter *it;

	TAILQ_FOREACH(it, filter_list, entries) {
		if (memcmp(key, &it->filter_info,
			sizeof(struct hinic_5tuple_filter_info)) == 0) {
			return it;
		}
	}

	return NULL;
}

static int hinic_set_lacp_tcam(struct hinic_nic_dev *nic_dev)
{
	struct tag_pa_rule lacp_rule;
	struct tag_pa_action lacp_action;

	memset(&lacp_rule, 0, sizeof(lacp_rule));
	memset(&lacp_action, 0, sizeof(lacp_action));
	/* LACP TCAM rule */
	lacp_rule.eth_type = PA_ETH_TYPE_OTHER;
	lacp_rule.l2_header.eth_type.val16 = 0x8809;
	lacp_rule.l2_header.eth_type.mask16 = 0xffff;

	/* LACP TCAM action */
	lacp_action.err_type = 0x3f; /* err from ipsu, not convert */
	lacp_action.fwd_action = 0x7; /* 0x3:drop; 0x7: not convert */
	lacp_action.pkt_type = PKT_LACP_TYPE;
	lacp_action.pri = 0x0;
	lacp_action.push_len = 0xf; /* push_len:0xf, not convert */

	return hinic_set_fdir_tcam(nic_dev->hwdev, TCAM_PKT_LACP,
					&lacp_rule, &lacp_action);
}

static int hinic_set_bgp_dport_tcam(struct hinic_nic_dev *nic_dev)
{
	struct tag_pa_rule bgp_rule;
	struct tag_pa_action bgp_action;

	memset(&bgp_rule, 0, sizeof(bgp_rule));
	memset(&bgp_action, 0, sizeof(bgp_action));
	/* BGP TCAM rule */
	bgp_rule.eth_type = PA_ETH_TYPE_IPV4; /* Eth type is IPV4 */
	bgp_rule.ip_header.protocol.val8 = IP_HEADER_PROTOCOL_TYPE_TCP;
	bgp_rule.ip_header.protocol.mask8 = UINT8_MAX;
	bgp_rule.ip_protocol_type = PA_IP_PROTOCOL_TYPE_TCP;
	bgp_rule.eth_ip_tcp.dport.val16 = BGP_DPORT_ID; /* Dport is 179 */
	bgp_rule.eth_ip_tcp.dport.mask16 = UINT16_MAX;

	/* BGP TCAM action */
	bgp_action.err_type = 0x3f; /* err from ipsu, not convert */
	bgp_action.fwd_action = 0x7; /* 0x3:drop; 0x7: not convert */
	bgp_action.pkt_type = PKT_BGPD_DPORT_TYPE; /* bgp_dport: 83 */
	bgp_action.pri = 0xf; /* pri of BGP is 0xf, result from ipsu parse
			       * results, not need to convert
			       */
	bgp_action.push_len = 0xf; /* push_len:0xf, not convert */

	return hinic_set_fdir_tcam(nic_dev->hwdev,
			TCAM_PKT_BGP_DPORT, &bgp_rule, &bgp_action);
}

static int hinic_set_bgp_sport_tcam(struct hinic_nic_dev *nic_dev)
{
	struct tag_pa_rule bgp_rule;
	struct tag_pa_action bgp_action;

	memset(&bgp_rule, 0, sizeof(bgp_rule));
	memset(&bgp_action, 0, sizeof(bgp_action));
	/* BGP TCAM rule */
	bgp_rule.eth_type = PA_ETH_TYPE_IPV4;
	bgp_rule.ip_header.protocol.val8 = IP_HEADER_PROTOCOL_TYPE_TCP;
	bgp_rule.ip_header.protocol.mask8 = UINT8_MAX;
	bgp_rule.ip_protocol_type = PA_IP_PROTOCOL_TYPE_TCP;
	bgp_rule.eth_ip_tcp.sport.val16 = BGP_DPORT_ID;
	bgp_rule.eth_ip_tcp.sport.mask16 = UINT16_MAX;

	/* BGP TCAM action */
	bgp_action.err_type = 0x3f; /* err from ipsu, not convert */
	bgp_action.fwd_action = 0x7; /* 0x3:drop; 0x7: not convert */
	bgp_action.pkt_type = PKT_BGPD_SPORT_TYPE; /* bgp:sport: 84 */
	bgp_action.pri = 0xf; /* pri of BGP is 0xf, result from ipsu parse
			       * results, not need to convert
			       */
	bgp_action.push_len = 0xf; /* push_len:0xf, not convert */

	return hinic_set_fdir_tcam(nic_dev->hwdev, TCAM_PKT_BGP_SPORT,
					&bgp_rule, &bgp_action);
}

static int hinic_set_vrrp_tcam(struct hinic_nic_dev *nic_dev)
{
	struct tag_pa_rule vrrp_rule;
	struct tag_pa_action vrrp_action;

	memset(&vrrp_rule, 0, sizeof(vrrp_rule));
	memset(&vrrp_action, 0, sizeof(vrrp_action));
	/* VRRP TCAM rule */
	vrrp_rule.eth_type = PA_ETH_TYPE_IPV4;
	vrrp_rule.ip_protocol_type = PA_IP_PROTOCOL_TYPE_TCP;
	vrrp_rule.ip_header.protocol.mask8 = 0xff;
	vrrp_rule.ip_header.protocol.val8 = PA_IP_PROTOCOL_TYPE_VRRP;

	/* VRRP TCAM action */
	vrrp_action.err_type = 0x3f;
	vrrp_action.fwd_action = 0x7;
	vrrp_action.pkt_type = PKT_VRRP_TYPE; /* VRRP: 85 */
	vrrp_action.pri = 0xf;
	vrrp_action.push_len = 0xf;

	return hinic_set_fdir_tcam(nic_dev->hwdev, TCAM_PKT_VRRP,
					&vrrp_rule, &vrrp_action);
}

/**
 *  Clear all fdir configuration.
 *
 * @param nic_dev
 *   The hardware interface of a Ethernet device.
 *
 * @return
 *   0 on success,
 *   negative error value otherwise.
 */
void hinic_free_fdir_filter(struct hinic_nic_dev *nic_dev)
{
	(void)hinic_set_fdir_filter(nic_dev->hwdev, 0, 0, 0, false);

	(void)hinic_set_fdir_tcam_rule_filter(nic_dev->hwdev, false);

	(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_BGP_DPORT);

	(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_BGP_SPORT);

	(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_VRRP);

	(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_LACP);

	(void)hinic_flush_tcam_rule(nic_dev->hwdev);
}

static int hinic_filter_info_init(struct hinic_5tuple_filter *filter,
		       struct hinic_filter_info *filter_info)
{
	switch (filter->filter_info.proto) {
	case IPPROTO_TCP:
		/* Filter type is bgp type if dst_port or src_port is 179 */
		if (filter->filter_info.dst_port == RTE_BE16(BGP_DPORT_ID) &&
			!(filter->filter_info.dst_port_mask)) {
			filter_info->pkt_type = PKT_BGPD_DPORT_TYPE;
		} else if (filter->filter_info.src_port ==
			RTE_BE16(BGP_DPORT_ID) &&
			!(filter->filter_info.src_port_mask)) {
			filter_info->pkt_type = PKT_BGPD_SPORT_TYPE;
		} else {
			PMD_DRV_LOG(INFO, "TCP PROTOCOL:5tuple filters"
			" just support BGP now, proto:0x%x, "
			"dst_port:0x%x, dst_port_mask:0x%x."
			"src_port:0x%x, src_port_mask:0x%x.",
			filter->filter_info.proto,
			filter->filter_info.dst_port,
			filter->filter_info.dst_port_mask,
			filter->filter_info.src_port,
			filter->filter_info.src_port_mask);
			return -EINVAL;
		}
		break;

	case IPPROTO_VRRP:
		filter_info->pkt_type = PKT_VRRP_TYPE;
		break;

	case IPPROTO_ICMP:
		filter_info->pkt_type = PKT_ICMP_IPV4_TYPE;
		break;

	case IPPROTO_ICMPV6:
		filter_info->pkt_type = PKT_ICMP_IPV6_TYPE;
		break;

	default:
		PMD_DRV_LOG(ERR, "5tuple filters just support BGP/VRRP/ICMP now, "
		"proto: 0x%x, dst_port: 0x%x, dst_port_mask: 0x%x."
		"src_port: 0x%x, src_port_mask: 0x%x.",
		filter->filter_info.proto, filter->filter_info.dst_port,
		filter->filter_info.dst_port_mask,
		filter->filter_info.src_port,
		filter->filter_info.src_port_mask);
		return -EINVAL;
	}

	return 0;
}

static int hinic_lookup_new_filter(struct hinic_5tuple_filter *filter,
			struct hinic_filter_info *filter_info, int *index)
{
	int type_id;

	type_id = HINIC_PKT_TYPE_FIND_ID(filter_info->pkt_type);

	if (type_id > HINIC_MAX_Q_FILTERS - 1) {
		PMD_DRV_LOG(ERR, "Pkt filters only support 64 filter type.");
		return -EINVAL;
	}

	if (!(filter_info->type_mask & (1 << type_id))) {
		filter_info->type_mask |= 1 << type_id;
		filter->index = type_id;
		filter_info->pkt_filters[type_id].enable = true;
		filter_info->pkt_filters[type_id].pkt_proto =
						filter->filter_info.proto;
		TAILQ_INSERT_TAIL(&filter_info->fivetuple_list,
				  filter, entries);
	} else {
		PMD_DRV_LOG(ERR, "Filter type: %d exists.", type_id);
		return -EIO;
	}

	*index = type_id;
	return 0;
}

/*
 * Add a 5tuple filter
 *
 * @param dev:
 *  Pointer to struct rte_eth_dev.
 * @param filter:
 *  Pointer to the filter that will be added.
 * @return
 *    - On success, zero.
 *    - On failure, a negative value.
 */
static int hinic_add_5tuple_filter(struct rte_eth_dev *dev,
				struct hinic_5tuple_filter *filter)
{
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
	int i, ret_fw;
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	if (hinic_filter_info_init(filter, filter_info) ||
		hinic_lookup_new_filter(filter, filter_info, &i))
		return -EFAULT;

	ret_fw = hinic_set_fdir_filter(nic_dev->hwdev, filter_info->pkt_type,
					filter_info->qid,
					filter_info->pkt_filters[i].enable,
					true);
	if (ret_fw) {
		PMD_DRV_LOG(ERR, "Set fdir filter failed, type: 0x%x, qid: 0x%x, enable: 0x%x",
			filter_info->pkt_type, filter->queue,
			filter_info->pkt_filters[i].enable);
		return -EFAULT;
	}

	PMD_DRV_LOG(INFO, "Add 5tuple succeed, type: 0x%x, qid: 0x%x, enable: 0x%x",
			filter_info->pkt_type, filter_info->qid,
			filter_info->pkt_filters[filter->index].enable);

	switch (filter->filter_info.proto) {
	case IPPROTO_TCP:
		if (filter->filter_info.dst_port == RTE_BE16(BGP_DPORT_ID)) {
			ret_fw = hinic_set_bgp_dport_tcam(nic_dev);
			if (ret_fw) {
				PMD_DRV_LOG(ERR, "Set dport bgp failed, "
					"type: 0x%x, qid: 0x%x, enable: 0x%x",
					filter_info->pkt_type, filter->queue,
					filter_info->pkt_filters[i].enable);
				return -EFAULT;
			}

			PMD_DRV_LOG(INFO, "Set dport bgp succeed, qid: 0x%x, enable: 0x%x",
				filter->queue,
				filter_info->pkt_filters[i].enable);
		} else if (filter->filter_info.src_port ==
			RTE_BE16(BGP_DPORT_ID)) {
			ret_fw = hinic_set_bgp_sport_tcam(nic_dev);
			if (ret_fw) {
				PMD_DRV_LOG(ERR, "Set sport bgp failed, "
					"type: 0x%x, qid: 0x%x, enable: 0x%x",
					filter_info->pkt_type, filter->queue,
					filter_info->pkt_filters[i].enable);
				return -EFAULT;
			}

			PMD_DRV_LOG(INFO, "Set sport bgp succeed, qid: 0x%x, enable: 0x%x",
					filter->queue,
					filter_info->pkt_filters[i].enable);
		}

		break;

	case IPPROTO_VRRP:
		ret_fw = hinic_set_vrrp_tcam(nic_dev);
		if (ret_fw) {
			PMD_DRV_LOG(ERR, "Set VRRP failed, "
				"type: 0x%x, qid: 0x%x, enable: 0x%x",
				filter_info->pkt_type, filter->queue,
				filter_info->pkt_filters[i].enable);
			return -EFAULT;
		}
		PMD_DRV_LOG(INFO, "Set VRRP succeed, qid: 0x%x, enable: 0x%x",
				filter->queue,
				filter_info->pkt_filters[i].enable);
		break;

	default:
		break;
	}

	return 0;
}

/*
 * Remove a 5tuple filter
 *
 * @param dev
 *  Pointer to struct rte_eth_dev.
 * @param filter
 *  The pointer of the filter will be removed.
 */
static void hinic_remove_5tuple_filter(struct rte_eth_dev *dev,
			   struct hinic_5tuple_filter *filter)
{
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	switch (filter->filter_info.proto) {
	case IPPROTO_VRRP:
		(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_VRRP);
		break;

	case IPPROTO_TCP:
		if (filter->filter_info.dst_port == RTE_BE16(BGP_DPORT_ID))
			(void)hinic_clear_fdir_tcam(nic_dev->hwdev,
							TCAM_PKT_BGP_DPORT);
		else if (filter->filter_info.src_port == RTE_BE16(BGP_DPORT_ID))
			(void)hinic_clear_fdir_tcam(nic_dev->hwdev,
							TCAM_PKT_BGP_SPORT);
		break;

	default:
		break;
	}

	hinic_filter_info_init(filter, filter_info);

	filter_info->pkt_filters[filter->index].enable = false;
	filter_info->pkt_filters[filter->index].pkt_proto = 0;

	PMD_DRV_LOG(INFO, "Del 5tuple succeed, type: 0x%x, qid: 0x%x, enable: 0x%x",
		filter_info->pkt_type,
		filter_info->pkt_filters[filter->index].qid,
		filter_info->pkt_filters[filter->index].enable);
	(void)hinic_set_fdir_filter(nic_dev->hwdev, filter_info->pkt_type,
				filter_info->pkt_filters[filter->index].qid,
				filter_info->pkt_filters[filter->index].enable,
				true);

	filter_info->pkt_type = 0;
	filter_info->qid = 0;
	filter_info->pkt_filters[filter->index].qid = 0;
	filter_info->type_mask &= ~(1 <<  (filter->index));
	TAILQ_REMOVE(&filter_info->fivetuple_list, filter, entries);

	rte_free(filter);
}

/*
 * Add or delete a ntuple filter
 *
 * @param dev
 *  Pointer to struct rte_eth_dev.
 * @param ntuple_filter
 *  Pointer to struct rte_eth_ntuple_filter
 * @param add
 *  If true, add filter; if false, remove filter
 * @return
 *    - On success, zero.
 *    - On failure, a negative value.
 */
static int hinic_add_del_ntuple_filter(struct rte_eth_dev *dev,
				struct rte_eth_ntuple_filter *ntuple_filter,
				bool add)
{
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
	struct hinic_5tuple_filter_info filter_5tuple;
	struct hinic_5tuple_filter *filter;
	int ret;

	if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
		PMD_DRV_LOG(ERR, "Only 5tuple is supported.");
		return -EINVAL;
	}

	memset(&filter_5tuple, 0, sizeof(struct hinic_5tuple_filter_info));
	ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
	if (ret < 0)
		return ret;

	filter = hinic_5tuple_filter_lookup(&filter_info->fivetuple_list,
					 &filter_5tuple);
	if (filter != NULL && add) {
		PMD_DRV_LOG(ERR, "Filter exists.");
		return -EEXIST;
	}
	if (filter == NULL && !add) {
		PMD_DRV_LOG(ERR, "Filter doesn't exist.");
		return -ENOENT;
	}

	if (add) {
		filter = rte_zmalloc("hinic_5tuple_filter",
				sizeof(struct hinic_5tuple_filter), 0);
		if (filter == NULL)
			return -ENOMEM;
		rte_memcpy(&filter->filter_info, &filter_5tuple,
				sizeof(struct hinic_5tuple_filter_info));
		filter->queue = ntuple_filter->queue;

		filter_info->qid = ntuple_filter->queue;

		ret = hinic_add_5tuple_filter(dev, filter);
		if (ret)
			rte_free(filter);

		return ret;
	}

	hinic_remove_5tuple_filter(dev, filter);

	return 0;
}

static inline int
hinic_check_ethertype_filter(struct rte_eth_ethertype_filter *filter)
{
	if (filter->queue >= HINIC_MAX_RX_QUEUE_NUM)
		return -EINVAL;

	if (filter->ether_type == RTE_ETHER_TYPE_IPV4 ||
		filter->ether_type == RTE_ETHER_TYPE_IPV6) {
		PMD_DRV_LOG(ERR, "Unsupported ether_type(0x%04x) in"
			" ethertype filter", filter->ether_type);
		return -EINVAL;
	}

	if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
		PMD_DRV_LOG(ERR, "Mac compare is not supported");
		return -EINVAL;
	}
	if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
		PMD_DRV_LOG(ERR, "Drop option is not supported");
		return -EINVAL;
	}

	return 0;
}

static inline int
hinic_ethertype_filter_lookup(struct hinic_filter_info *filter_info,
			      struct hinic_pkt_filter *ethertype_filter)
{
	switch (ethertype_filter->pkt_proto) {
	case RTE_ETHER_TYPE_SLOW:
		filter_info->pkt_type = PKT_LACP_TYPE;
		break;

	case RTE_ETHER_TYPE_ARP:
		filter_info->pkt_type = PKT_ARP_TYPE;
		break;

	default:
		PMD_DRV_LOG(ERR, "Just support LACP/ARP for ethertype filters");
		return -EIO;
	}

	return HINIC_PKT_TYPE_FIND_ID(filter_info->pkt_type);
}

static inline int
hinic_ethertype_filter_insert(struct hinic_filter_info *filter_info,
			      struct hinic_pkt_filter *ethertype_filter)
{
	int id;

	/* Find LACP or VRRP type id */
	id = hinic_ethertype_filter_lookup(filter_info, ethertype_filter);
	if (id < 0)
		return -EINVAL;

	if (!(filter_info->type_mask & (1 << id))) {
		filter_info->type_mask |= 1 << id;
		filter_info->pkt_filters[id].pkt_proto =
			ethertype_filter->pkt_proto;
		filter_info->pkt_filters[id].enable = ethertype_filter->enable;
		filter_info->qid = ethertype_filter->qid;
		return id;
	}

	PMD_DRV_LOG(ERR, "Filter type: %d exists", id);
	return -EINVAL;
}

static inline void
hinic_ethertype_filter_remove(struct hinic_filter_info *filter_info,
			      uint8_t idx)
{
	if (idx >= HINIC_MAX_Q_FILTERS)
		return;

	filter_info->pkt_type = 0;
	filter_info->type_mask &= ~(1 << idx);
	filter_info->pkt_filters[idx].pkt_proto = (uint16_t)0;
	filter_info->pkt_filters[idx].enable = FALSE;
	filter_info->pkt_filters[idx].qid = 0;
}

static inline int
hinic_add_del_ethertype_filter(struct rte_eth_dev *dev,
			       struct rte_eth_ethertype_filter *filter,
			       bool add)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
	struct hinic_pkt_filter ethertype_filter;
	int i;
	int ret_fw;

	if (hinic_check_ethertype_filter(filter))
		return -EINVAL;

	if (add) {
		ethertype_filter.pkt_proto = filter->ether_type;
		ethertype_filter.enable = TRUE;
		ethertype_filter.qid = (u8)filter->queue;
		i = hinic_ethertype_filter_insert(filter_info,
						    &ethertype_filter);
		if (i < 0)
			return -ENOSPC;

		ret_fw = hinic_set_fdir_filter(nic_dev->hwdev,
				filter_info->pkt_type, filter_info->qid,
				filter_info->pkt_filters[i].enable, true);
		if (ret_fw) {
			PMD_DRV_LOG(ERR, "add ethertype failed, type: 0x%x, qid: 0x%x, enable: 0x%x",
				filter_info->pkt_type, filter->queue,
				filter_info->pkt_filters[i].enable);

			hinic_ethertype_filter_remove(filter_info, i);
			return -ENOENT;
		}
		PMD_DRV_LOG(INFO, "Add ethertype succeed, type: 0x%x, qid: 0x%x, enable: 0x%x",
				filter_info->pkt_type, filter->queue,
				filter_info->pkt_filters[i].enable);

		switch (ethertype_filter.pkt_proto) {
		case RTE_ETHER_TYPE_SLOW:
			ret_fw = hinic_set_lacp_tcam(nic_dev);
			if (ret_fw) {
				PMD_DRV_LOG(ERR, "Add lacp tcam failed");
				hinic_ethertype_filter_remove(filter_info, i);
				return -ENOENT;
			}

			PMD_DRV_LOG(INFO, "Add lacp tcam succeed");
			break;
		default:
			break;
		}
	} else {
		ethertype_filter.pkt_proto = filter->ether_type;
		i = hinic_ethertype_filter_lookup(filter_info,
						&ethertype_filter);

		if ((filter_info->type_mask & (1 << i))) {
			filter_info->pkt_filters[i].enable = FALSE;
			(void)hinic_set_fdir_filter(nic_dev->hwdev,
					filter_info->pkt_type,
					filter_info->pkt_filters[i].qid,
					filter_info->pkt_filters[i].enable,
					true);

			PMD_DRV_LOG(INFO, "Del ethertype succeed, type: 0x%x, qid: 0x%x, enable: 0x%x",
					filter_info->pkt_type,
					filter_info->pkt_filters[i].qid,
					filter_info->pkt_filters[i].enable);

			switch (ethertype_filter.pkt_proto) {
			case RTE_ETHER_TYPE_SLOW:
				(void)hinic_clear_fdir_tcam(nic_dev->hwdev,
								TCAM_PKT_LACP);
				PMD_DRV_LOG(INFO, "Del lacp tcam succeed");
				break;
			default:
				break;
			}

			hinic_ethertype_filter_remove(filter_info, i);

		} else {
			PMD_DRV_LOG(ERR, "Ethertype doesn't exist, type: 0x%x, qid: 0x%x, enable: 0x%x",
					filter_info->pkt_type, filter->queue,
					filter_info->pkt_filters[i].enable);
			return -ENOENT;
		}
	}

	return 0;
}

static int hinic_fdir_info_init(struct hinic_fdir_rule *rule,
				struct hinic_fdir_info *fdir_info)
{
	switch (rule->mask.src_ipv4_mask) {
	case UINT32_MAX:
		fdir_info->fdir_flag = HINIC_ATR_FLOW_TYPE_IPV4_SIP;
		fdir_info->qid = rule->queue;
		fdir_info->fdir_key = rule->hinic_fdir.src_ip;
		return 0;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "Invalid src_ip mask.");
		return -EINVAL;
	}

	switch (rule->mask.dst_ipv4_mask) {
	case UINT32_MAX:
		fdir_info->fdir_flag = HINIC_ATR_FLOW_TYPE_IPV4_DIP;
		fdir_info->qid = rule->queue;
		fdir_info->fdir_key = rule->hinic_fdir.dst_ip;
		return 0;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "Invalid dst_ip mask.");
		return -EINVAL;
	}

	if (fdir_info->fdir_flag == 0) {
		PMD_DRV_LOG(ERR, "All support mask is NULL.");
		return -EINVAL;
	}

	return 0;
}

static inline int hinic_add_del_fdir_filter(struct rte_eth_dev *dev,
					struct hinic_fdir_rule *rule, bool add)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	struct hinic_fdir_info fdir_info;
	int ret;

	memset(&fdir_info, 0, sizeof(struct hinic_fdir_info));

	ret = hinic_fdir_info_init(rule, &fdir_info);
	if (ret) {
		PMD_DRV_LOG(ERR, "Init hinic fdir info failed!");
		return ret;
	}

	if (add) {
		ret = hinic_set_normal_filter(nic_dev->hwdev, fdir_info.qid,
						true, fdir_info.fdir_key,
						true, fdir_info.fdir_flag);
		if (ret) {
			PMD_DRV_LOG(ERR, "Add fdir filter failed, flag: 0x%x, qid: 0x%x, key: 0x%x",
					fdir_info.fdir_flag, fdir_info.qid,
					fdir_info.fdir_key);
			return -ENOENT;
		}
		PMD_DRV_LOG(INFO, "Add fdir filter succeed, flag: 0x%x, qid: 0x%x, key: 0x%x",
				fdir_info.fdir_flag, fdir_info.qid,
				fdir_info.fdir_key);
	} else {
		ret = hinic_set_normal_filter(nic_dev->hwdev, fdir_info.qid,
						false, fdir_info.fdir_key, true,
						fdir_info.fdir_flag);
		if (ret) {
			PMD_DRV_LOG(ERR, "Del fdir filter failed, flag: 0x%x, qid: 0x%x, key: 0x%x",
				fdir_info.fdir_flag, fdir_info.qid,
				fdir_info.fdir_key);
			return -ENOENT;
		}
		PMD_DRV_LOG(INFO, "Del fdir filter succeed, flag: 0x%x, qid: 0x%x, key: 0x%x",
				fdir_info.fdir_flag, fdir_info.qid,
				fdir_info.fdir_key);
	}

	return 0;
}

static void tcam_translate_key_y(u8 *key_y, u8 *src_input, u8 *mask, u8 len)
{
	u8 idx;

	for (idx = 0; idx < len; idx++)
		key_y[idx] = src_input[idx] & mask[idx];
}

static void tcam_translate_key_x(u8 *key_x, u8 *key_y, u8 *mask, u8 len)
{
	u8 idx;

	for (idx = 0; idx < len; idx++)
		key_x[idx] = key_y[idx] ^ mask[idx];
}

static void tcam_key_calculate(struct tag_tcam_key *tcam_key,
				struct tag_tcam_cfg_rule *fdir_tcam_rule)
{
	tcam_translate_key_y(fdir_tcam_rule->key.y,
		(u8 *)(&tcam_key->key_info),
		(u8 *)(&tcam_key->key_mask),
		TCAM_FLOW_KEY_SIZE);
	tcam_translate_key_x(fdir_tcam_rule->key.x,
		fdir_tcam_rule->key.y,
		(u8 *)(&tcam_key->key_mask),
		TCAM_FLOW_KEY_SIZE);
}

static int hinic_fdir_tcam_ipv4_init(struct rte_eth_dev *dev,
				     struct hinic_fdir_rule *rule,
				     struct tag_tcam_key *tcam_key)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	switch (rule->mask.dst_ipv4_mask) {
	case UINT32_MAX:
		tcam_key->key_info.ext_dip_h =
			(rule->hinic_fdir.dst_ip >> 16) & 0xffffU;
		tcam_key->key_info.ext_dip_l =
			rule->hinic_fdir.dst_ip & 0xffffU;
		tcam_key->key_mask.ext_dip_h =
			(rule->mask.dst_ipv4_mask >> 16) & 0xffffU;
		tcam_key->key_mask.ext_dip_l =
			rule->mask.dst_ipv4_mask & 0xffffU;
		break;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "invalid src_ip mask.");
		return -EINVAL;
	}

	if (rule->mask.dst_port_mask > 0) {
		tcam_key->key_info.dst_port = rule->hinic_fdir.dst_port;
		tcam_key->key_mask.dst_port = rule->mask.dst_port_mask;
	}

	if (rule->mask.src_port_mask > 0) {
		tcam_key->key_info.src_port = rule->hinic_fdir.src_port;
		tcam_key->key_mask.src_port = rule->mask.src_port_mask;
	}

	switch (rule->mask.tunnel_flag) {
	case UINT16_MAX:
		tcam_key->key_info.tunnel_flag = FDIR_TCAM_TUNNEL_PACKET;
		tcam_key->key_mask.tunnel_flag = UINT8_MAX;
		break;

	case 0:
		tcam_key->key_info.tunnel_flag = FDIR_TCAM_NORMAL_PACKET;
		tcam_key->key_mask.tunnel_flag = 0;
		break;

	default:
		PMD_DRV_LOG(ERR, "invalid tunnel flag mask.");
		return -EINVAL;
	}

	if (rule->mask.tunnel_inner_dst_port_mask > 0) {
		tcam_key->key_info.dst_port =
					rule->hinic_fdir.tunnel_inner_dst_port;
		tcam_key->key_mask.dst_port =
					rule->mask.tunnel_inner_dst_port_mask;
	}

	if (rule->mask.tunnel_inner_src_port_mask > 0) {
		tcam_key->key_info.src_port =
					rule->hinic_fdir.tunnel_inner_src_port;
		tcam_key->key_mask.src_port =
					rule->mask.tunnel_inner_src_port_mask;
	}

	switch (rule->mask.proto_mask) {
	case UINT16_MAX:
		tcam_key->key_info.protocol = rule->hinic_fdir.proto;
		tcam_key->key_mask.protocol = UINT8_MAX;
		break;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "invalid tunnel flag mask.");
		return -EINVAL;
	}

	tcam_key->key_mask.function_id = UINT16_MAX;
	tcam_key->key_info.function_id =
		hinic_global_func_id(nic_dev->hwdev) & 0x7fff;

	return 0;
}

static int hinic_fdir_tcam_ipv6_init(struct rte_eth_dev *dev,
				     struct hinic_fdir_rule *rule,
				     struct tag_tcam_key *tcam_key)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	switch (rule->mask.dst_ipv6_mask) {
	case UINT16_MAX:
		tcam_key->key_info_ipv6.ipv6_key0 =
			((rule->hinic_fdir.dst_ipv6[0] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[1];
		tcam_key->key_info_ipv6.ipv6_key1 =
			((rule->hinic_fdir.dst_ipv6[2] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[3];
		tcam_key->key_info_ipv6.ipv6_key2 =
			((rule->hinic_fdir.dst_ipv6[4] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[5];
		tcam_key->key_info_ipv6.ipv6_key3 =
			((rule->hinic_fdir.dst_ipv6[6] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[7];
		tcam_key->key_info_ipv6.ipv6_key4 =
			((rule->hinic_fdir.dst_ipv6[8] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[9];
		tcam_key->key_info_ipv6.ipv6_key5 =
			((rule->hinic_fdir.dst_ipv6[10] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[11];
		tcam_key->key_info_ipv6.ipv6_key6 =
			((rule->hinic_fdir.dst_ipv6[12] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[13];
		tcam_key->key_info_ipv6.ipv6_key7 =
			((rule->hinic_fdir.dst_ipv6[14] << 8) & 0xff00) |
			rule->hinic_fdir.dst_ipv6[15];
		tcam_key->key_mask_ipv6.ipv6_key0 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key1 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key2 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key3 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key4 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key5 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key6 = UINT16_MAX;
		tcam_key->key_mask_ipv6.ipv6_key7 = UINT16_MAX;
		break;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "invalid dst_ipv6 mask");
		return -EINVAL;
	}

	if (rule->mask.dst_port_mask > 0) {
		tcam_key->key_info_ipv6.dst_port = rule->hinic_fdir.dst_port;
		tcam_key->key_mask_ipv6.dst_port = rule->mask.dst_port_mask;
	}

	switch (rule->mask.proto_mask) {
	case UINT16_MAX:
		tcam_key->key_info_ipv6.protocol =
			(rule->hinic_fdir.proto) & 0x7F;
		tcam_key->key_mask_ipv6.protocol = 0x7F;
		break;

	case 0:
		break;

	default:
		PMD_DRV_LOG(ERR, "invalid tunnel flag mask");
		return -EINVAL;
	}

	tcam_key->key_info_ipv6.ipv6_flag = 1;
	tcam_key->key_mask_ipv6.ipv6_flag = 1;

	tcam_key->key_mask_ipv6.function_id = UINT8_MAX;
	tcam_key->key_info_ipv6.function_id =
			(u8)hinic_global_func_id(nic_dev->hwdev);

	return 0;
}

static int hinic_fdir_tcam_info_init(struct rte_eth_dev *dev,
				     struct hinic_fdir_rule *rule,
				     struct tag_tcam_key *tcam_key,
				     struct tag_tcam_cfg_rule *fdir_tcam_rule)
{
	int ret = -1;

	if (rule->mask.dst_ipv4_mask == UINT32_MAX)
		ret = hinic_fdir_tcam_ipv4_init(dev, rule, tcam_key);
	else if (rule->mask.dst_ipv6_mask == UINT16_MAX)
		ret = hinic_fdir_tcam_ipv6_init(dev, rule, tcam_key);

	if (ret < 0)
		return ret;

	fdir_tcam_rule->data.qid = rule->queue;

	tcam_key_calculate(tcam_key, fdir_tcam_rule);

	return 0;
}

static inline struct hinic_tcam_filter *
hinic_tcam_filter_lookup(struct hinic_tcam_filter_list *filter_list,
			struct tag_tcam_key *key)
{
	struct hinic_tcam_filter *it;

	TAILQ_FOREACH(it, filter_list, entries) {
		if (memcmp(key, &it->tcam_key,
			sizeof(struct tag_tcam_key)) == 0) {
			return it;
		}
	}

	return NULL;
}

static int hinic_lookup_new_tcam_filter(struct rte_eth_dev *dev,
					struct hinic_tcam_info *tcam_info,
					struct hinic_tcam_filter *tcam_filter,
					u16 *tcam_index)
{
	int index;
	int max_index;
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	if (hinic_func_type(nic_dev->hwdev) == TYPE_VF)
		max_index = HINIC_VF_MAX_TCAM_FILTERS;
	else
		max_index = HINIC_PF_MAX_TCAM_FILTERS;

	for (index = 0; index < max_index; index++) {
		if (tcam_info->tcam_index_array[index] == 0)
			break;
	}

	if (index == max_index) {
		PMD_DRV_LOG(ERR, "function 0x%x tcam filters only support %d filter rules",
			hinic_global_func_id(nic_dev->hwdev), max_index);
		return -EINVAL;
	}

	tcam_filter->index = index;
	*tcam_index = index;

	return 0;
}

static int hinic_add_tcam_filter(struct rte_eth_dev *dev,
				struct hinic_tcam_filter *tcam_filter,
				struct tag_tcam_cfg_rule *fdir_tcam_rule)
{
	struct hinic_tcam_info *tcam_info =
		HINIC_DEV_PRIVATE_TO_TCAM_INFO(dev->data->dev_private);
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	u16 index = 0;
	u16 tcam_block_index = 0;
	int rc;

	if (hinic_lookup_new_tcam_filter(dev, tcam_info, tcam_filter, &index))
		return -EINVAL;

	if (tcam_info->tcam_rule_nums == 0) {
		if (hinic_func_type(nic_dev->hwdev) == TYPE_VF) {
			rc = hinic_alloc_tcam_block(nic_dev->hwdev,
				HINIC_TCAM_BLOCK_TYPE_VF, &tcam_block_index);
			if (rc != 0) {
				PMD_DRV_LOG(ERR, "VF fdir filter tcam alloc block failed!");
				return -EFAULT;
			}
		} else {
			rc = hinic_alloc_tcam_block(nic_dev->hwdev,
				HINIC_TCAM_BLOCK_TYPE_PF, &tcam_block_index);
			if (rc != 0) {
				PMD_DRV_LOG(ERR, "PF fdir filter tcam alloc block failed!");
				return -EFAULT;
			}
		}

		tcam_info->tcam_block_index = tcam_block_index;
	} else {
		tcam_block_index = tcam_info->tcam_block_index;
	}

	if (hinic_func_type(nic_dev->hwdev) == TYPE_VF) {
		fdir_tcam_rule->index =
			HINIC_PKT_VF_TCAM_INDEX_START(tcam_block_index) + index;
	} else {
		fdir_tcam_rule->index =
			tcam_block_index * HINIC_PF_MAX_TCAM_FILTERS + index;
	}

	rc = hinic_add_tcam_rule(nic_dev->hwdev, fdir_tcam_rule);
	if (rc != 0) {
		PMD_DRV_LOG(ERR, "Fdir_tcam_rule add failed!");
		return -EFAULT;
	}

	PMD_DRV_LOG(INFO, "Add fdir_tcam_rule function_id: 0x%x,"
		"tcam_block_id: %d, index: %d, queue: %d, tcam_rule_nums: %d succeed",
		hinic_global_func_id(nic_dev->hwdev), tcam_block_index,
		fdir_tcam_rule->index, fdir_tcam_rule->data.qid,
		tcam_info->tcam_rule_nums + 1);

	if (tcam_info->tcam_rule_nums == 0) {
		rc = hinic_set_fdir_filter(nic_dev->hwdev, 0, 0, 0, true);
		if (rc < 0) {
			(void)hinic_del_tcam_rule(nic_dev->hwdev,
						fdir_tcam_rule->index);
			return rc;
		}

		rc = hinic_set_fdir_tcam_rule_filter(nic_dev->hwdev, true);
		if (rc && rc != HINIC_MGMT_CMD_UNSUPPORTED) {
			(void)hinic_set_fdir_filter(nic_dev->hwdev, 0, 0, 0,
						false);
			(void)hinic_del_tcam_rule(nic_dev->hwdev,
						fdir_tcam_rule->index);
			return rc;
		}
	}

	TAILQ_INSERT_TAIL(&tcam_info->tcam_list, tcam_filter, entries);

	tcam_info->tcam_index_array[index] = 1;
	tcam_info->tcam_rule_nums++;

	return 0;
}

static int hinic_del_tcam_filter(struct rte_eth_dev *dev,
				struct hinic_tcam_filter *tcam_filter)
{
	struct hinic_tcam_info *tcam_info =
		HINIC_DEV_PRIVATE_TO_TCAM_INFO(dev->data->dev_private);
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	u32 index = 0;
	u16 tcam_block_index = tcam_info->tcam_block_index;
	int rc;
	u8 block_type = 0;

	if (hinic_func_type(nic_dev->hwdev) == TYPE_VF) {
		index = HINIC_PKT_VF_TCAM_INDEX_START(tcam_block_index) +
			tcam_filter->index;
		block_type = HINIC_TCAM_BLOCK_TYPE_VF;
	} else {
		index = tcam_block_index * HINIC_PF_MAX_TCAM_FILTERS +
			tcam_filter->index;
		block_type = HINIC_TCAM_BLOCK_TYPE_PF;
	}

	rc = hinic_del_tcam_rule(nic_dev->hwdev, index);
	if (rc != 0) {
		PMD_DRV_LOG(ERR, "fdir_tcam_rule del failed!");
		return -EFAULT;
	}

	PMD_DRV_LOG(INFO, "Del fdir_tcam_rule function_id: 0x%x, "
		"tcam_block_id: %d, index: %d, tcam_rule_nums: %d succeed",
		hinic_global_func_id(nic_dev->hwdev), tcam_block_index, index,
		tcam_info->tcam_rule_nums - 1);

	TAILQ_REMOVE(&tcam_info->tcam_list, tcam_filter, entries);

	tcam_info->tcam_index_array[tcam_filter->index] = 0;

	rte_free(tcam_filter);

	tcam_info->tcam_rule_nums--;

	if (tcam_info->tcam_rule_nums == 0) {
		(void)hinic_free_tcam_block(nic_dev->hwdev, block_type,
					&tcam_block_index);
	}

	return 0;
}

static int hinic_add_del_tcam_fdir_filter(struct rte_eth_dev *dev,
					struct hinic_fdir_rule *rule, bool add)
{
	struct hinic_tcam_info *tcam_info =
		HINIC_DEV_PRIVATE_TO_TCAM_INFO(dev->data->dev_private);
	struct hinic_tcam_filter *tcam_filter;
	struct tag_tcam_cfg_rule fdir_tcam_rule;
	struct tag_tcam_key tcam_key;
	int ret;

	memset(&fdir_tcam_rule, 0, sizeof(struct tag_tcam_cfg_rule));
	memset((void *)&tcam_key, 0, sizeof(struct tag_tcam_key));

	ret = hinic_fdir_tcam_info_init(dev, rule, &tcam_key, &fdir_tcam_rule);
	if (ret) {
		PMD_DRV_LOG(ERR, "Init hinic fdir info failed!");
		return ret;
	}

	tcam_filter = hinic_tcam_filter_lookup(&tcam_info->tcam_list,
						&tcam_key);
	if (tcam_filter != NULL && add) {
		PMD_DRV_LOG(ERR, "Filter exists.");
		return -EEXIST;
	}
	if (tcam_filter == NULL && !add) {
		PMD_DRV_LOG(ERR, "Filter doesn't exist.");
		return -ENOENT;
	}

	if (add) {
		tcam_filter = rte_zmalloc("hinic_5tuple_filter",
				sizeof(struct hinic_tcam_filter), 0);
		if (tcam_filter == NULL)
			return -ENOMEM;
		(void)rte_memcpy(&tcam_filter->tcam_key,
				 &tcam_key, sizeof(struct tag_tcam_key));
		tcam_filter->queue = fdir_tcam_rule.data.qid;

		ret = hinic_add_tcam_filter(dev, tcam_filter, &fdir_tcam_rule);
		if (ret < 0) {
			rte_free(tcam_filter);
			return ret;
		}

		rule->tcam_index = fdir_tcam_rule.index;

	} else {
		PMD_DRV_LOG(INFO, "begin to hinic_del_tcam_filter");
		ret = hinic_del_tcam_filter(dev, tcam_filter);
		if (ret < 0)
			return ret;
	}

	return 0;
}

/**
 * Create or destroy a flow rule.
 * Theorically one rule can match more than one filters.
 * We will let it use the filter which it hitt first.
 * So, the sequence matters.
 */
static struct rte_flow *hinic_flow_create(struct rte_eth_dev *dev,
					const struct rte_flow_attr *attr,
					const struct rte_flow_item pattern[],
					const struct rte_flow_action actions[],
					struct rte_flow_error *error)
{
	int ret;
	struct rte_eth_ntuple_filter ntuple_filter;
	struct rte_eth_ethertype_filter ethertype_filter;
	struct hinic_fdir_rule fdir_rule;
	struct rte_flow *flow = NULL;
	struct hinic_ethertype_filter_ele *ethertype_filter_ptr;
	struct hinic_ntuple_filter_ele *ntuple_filter_ptr;
	struct hinic_fdir_rule_ele *fdir_rule_ptr;
	struct hinic_flow_mem *hinic_flow_mem_ptr;
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	flow = rte_zmalloc("hinic_rte_flow", sizeof(struct rte_flow), 0);
	if (!flow) {
		PMD_DRV_LOG(ERR, "Failed to allocate flow memory");
		return NULL;
	}

	hinic_flow_mem_ptr = rte_zmalloc("hinic_flow_mem",
			sizeof(struct hinic_flow_mem), 0);
	if (!hinic_flow_mem_ptr) {
		PMD_DRV_LOG(ERR, "Failed to allocate hinic_flow_mem_ptr");
		rte_free(flow);
		return NULL;
	}

	hinic_flow_mem_ptr->flow = flow;
	TAILQ_INSERT_TAIL(&nic_dev->hinic_flow_list, hinic_flow_mem_ptr,
				entries);

	/* Add ntuple filter */
	memset(&ntuple_filter, 0, sizeof(struct rte_eth_ntuple_filter));
	ret = hinic_parse_ntuple_filter(dev, attr, pattern,
			actions, &ntuple_filter, error);
	if (!ret) {
		ret = hinic_add_del_ntuple_filter(dev, &ntuple_filter, TRUE);
		if (!ret) {
			ntuple_filter_ptr = rte_zmalloc("hinic_ntuple_filter",
				sizeof(struct hinic_ntuple_filter_ele), 0);
			if (ntuple_filter_ptr == NULL) {
				PMD_DRV_LOG(ERR, "Failed to allocate ntuple_filter_ptr");
				goto out;
			}
			rte_memcpy(&ntuple_filter_ptr->filter_info,
				   &ntuple_filter,
				   sizeof(struct rte_eth_ntuple_filter));
			TAILQ_INSERT_TAIL(&nic_dev->filter_ntuple_list,
			ntuple_filter_ptr, entries);
			flow->rule = ntuple_filter_ptr;
			flow->filter_type = RTE_ETH_FILTER_NTUPLE;

			PMD_DRV_LOG(INFO, "Create flow ntuple succeed, func_id: 0x%x",
			hinic_global_func_id(nic_dev->hwdev));
			return flow;
		}
		goto out;
	}

	/* Add ethertype filter */
	memset(&ethertype_filter, 0, sizeof(struct rte_eth_ethertype_filter));
	ret = hinic_parse_ethertype_filter(dev, attr, pattern, actions,
					&ethertype_filter, error);
	if (!ret) {
		ret = hinic_add_del_ethertype_filter(dev, &ethertype_filter,
						     TRUE);
		if (!ret) {
			ethertype_filter_ptr =
				rte_zmalloc("hinic_ethertype_filter",
				sizeof(struct hinic_ethertype_filter_ele), 0);
			if (ethertype_filter_ptr == NULL) {
				PMD_DRV_LOG(ERR, "Failed to allocate ethertype_filter_ptr");
				goto out;
			}
			rte_memcpy(&ethertype_filter_ptr->filter_info,
				&ethertype_filter,
				sizeof(struct rte_eth_ethertype_filter));
			TAILQ_INSERT_TAIL(&nic_dev->filter_ethertype_list,
				ethertype_filter_ptr, entries);
			flow->rule = ethertype_filter_ptr;
			flow->filter_type = RTE_ETH_FILTER_ETHERTYPE;

			PMD_DRV_LOG(INFO, "Create flow ethertype succeed, func_id: 0x%x",
					hinic_global_func_id(nic_dev->hwdev));
			return flow;
		}
		goto out;
	}

	/* Add fdir filter */
	memset(&fdir_rule, 0, sizeof(struct hinic_fdir_rule));
	ret = hinic_parse_fdir_filter(dev, attr, pattern,
				      actions, &fdir_rule, error);
	if (!ret) {
		if (fdir_rule.mode == HINIC_FDIR_MODE_NORMAL) {
			ret = hinic_add_del_fdir_filter(dev,
					&fdir_rule, TRUE);
		} else if (fdir_rule.mode == HINIC_FDIR_MODE_TCAM) {
			ret = hinic_add_del_tcam_fdir_filter(dev,
					&fdir_rule, TRUE);
		}  else {
			PMD_DRV_LOG(INFO, "flow fdir rule create failed, rule mode wrong");
			goto out;
		}
		if (!ret) {
			fdir_rule_ptr = rte_zmalloc("hinic_fdir_rule",
				sizeof(struct hinic_fdir_rule_ele), 0);
			if (fdir_rule_ptr == NULL) {
				PMD_DRV_LOG(ERR, "Failed to allocate fdir_rule_ptr");
				goto out;
			}
			rte_memcpy(&fdir_rule_ptr->filter_info, &fdir_rule,
				sizeof(struct hinic_fdir_rule));
			TAILQ_INSERT_TAIL(&nic_dev->filter_fdir_rule_list,
				fdir_rule_ptr, entries);
			flow->rule = fdir_rule_ptr;
			flow->filter_type = RTE_ETH_FILTER_FDIR;

			PMD_DRV_LOG(INFO, "Create flow fdir rule succeed, func_id : 0x%x",
					hinic_global_func_id(nic_dev->hwdev));
			return flow;
		}
		goto out;
	}

out:
	TAILQ_REMOVE(&nic_dev->hinic_flow_list, hinic_flow_mem_ptr, entries);
	rte_flow_error_set(error, -ret,
			   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
			   "Failed to create flow.");
	rte_free(hinic_flow_mem_ptr);
	rte_free(flow);
	return NULL;
}

/* Destroy a flow rule on hinic. */
static int hinic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
				struct rte_flow_error *error)
{
	int ret;
	struct rte_flow *pmd_flow = flow;
	enum rte_filter_type filter_type = pmd_flow->filter_type;
	struct rte_eth_ntuple_filter ntuple_filter;
	struct rte_eth_ethertype_filter ethertype_filter;
	struct hinic_fdir_rule fdir_rule;
	struct hinic_ntuple_filter_ele *ntuple_filter_ptr;
	struct hinic_ethertype_filter_ele *ethertype_filter_ptr;
	struct hinic_fdir_rule_ele *fdir_rule_ptr;
	struct hinic_flow_mem *hinic_flow_mem_ptr;
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	switch (filter_type) {
	case RTE_ETH_FILTER_NTUPLE:
		ntuple_filter_ptr = (struct hinic_ntuple_filter_ele *)
					pmd_flow->rule;
		rte_memcpy(&ntuple_filter, &ntuple_filter_ptr->filter_info,
			sizeof(struct rte_eth_ntuple_filter));
		ret = hinic_add_del_ntuple_filter(dev, &ntuple_filter, FALSE);
		if (!ret) {
			TAILQ_REMOVE(&nic_dev->filter_ntuple_list,
				ntuple_filter_ptr, entries);
			rte_free(ntuple_filter_ptr);
		}
		break;
	case RTE_ETH_FILTER_ETHERTYPE:
		ethertype_filter_ptr = (struct hinic_ethertype_filter_ele *)
					pmd_flow->rule;
		rte_memcpy(&ethertype_filter,
			&ethertype_filter_ptr->filter_info,
			sizeof(struct rte_eth_ethertype_filter));
		ret = hinic_add_del_ethertype_filter(dev,
				&ethertype_filter, FALSE);
		if (!ret) {
			TAILQ_REMOVE(&nic_dev->filter_ethertype_list,
				ethertype_filter_ptr, entries);
			rte_free(ethertype_filter_ptr);
		}
		break;
	case RTE_ETH_FILTER_FDIR:
		fdir_rule_ptr = (struct hinic_fdir_rule_ele *)pmd_flow->rule;
		rte_memcpy(&fdir_rule,
			&fdir_rule_ptr->filter_info,
			sizeof(struct hinic_fdir_rule));
		if (fdir_rule.mode == HINIC_FDIR_MODE_NORMAL) {
			ret = hinic_add_del_fdir_filter(dev, &fdir_rule, FALSE);
		} else if (fdir_rule.mode == HINIC_FDIR_MODE_TCAM) {
			ret = hinic_add_del_tcam_fdir_filter(dev, &fdir_rule,
								FALSE);
		} else {
			PMD_DRV_LOG(ERR, "FDIR Filter type is wrong!");
			ret = -EINVAL;
		}
		if (!ret) {
			TAILQ_REMOVE(&nic_dev->filter_fdir_rule_list,
				fdir_rule_ptr, entries);
			rte_free(fdir_rule_ptr);
		}
		break;
	default:
		PMD_DRV_LOG(WARNING, "Filter type (%d) is not supported",
			filter_type);
		ret = -EINVAL;
		break;
	}

	if (ret) {
		rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_HANDLE,
				NULL, "Failed to destroy flow");
		return ret;
	}

	TAILQ_FOREACH(hinic_flow_mem_ptr, &nic_dev->hinic_flow_list, entries) {
		if (hinic_flow_mem_ptr->flow == pmd_flow) {
			TAILQ_REMOVE(&nic_dev->hinic_flow_list,
				hinic_flow_mem_ptr, entries);
			rte_free(hinic_flow_mem_ptr);
			break;
		}
	}
	rte_free(flow);

	PMD_DRV_LOG(INFO, "Destroy flow succeed, func_id: 0x%x",
			hinic_global_func_id(nic_dev->hwdev));

	return ret;
}

/* Remove all the n-tuple filters */
static void hinic_clear_all_ntuple_filter(struct rte_eth_dev *dev)
{
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
	struct hinic_5tuple_filter *p_5tuple;

	while ((p_5tuple = TAILQ_FIRST(&filter_info->fivetuple_list)))
		hinic_remove_5tuple_filter(dev, p_5tuple);
}

/* Remove all the ether type filters */
static void hinic_clear_all_ethertype_filter(struct rte_eth_dev *dev)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	struct hinic_filter_info *filter_info =
		HINIC_DEV_PRIVATE_TO_FILTER_INFO(nic_dev);
	int ret = 0;

	if (filter_info->type_mask &
		(1 << HINIC_PKT_TYPE_FIND_ID(PKT_LACP_TYPE))) {
		hinic_ethertype_filter_remove(filter_info,
			HINIC_PKT_TYPE_FIND_ID(PKT_LACP_TYPE));
		ret = hinic_set_fdir_filter(nic_dev->hwdev, PKT_LACP_TYPE,
					filter_info->qid, false, true);

		(void)hinic_clear_fdir_tcam(nic_dev->hwdev, TCAM_PKT_LACP);
	}

	if (filter_info->type_mask &
		(1 << HINIC_PKT_TYPE_FIND_ID(PKT_ARP_TYPE))) {
		hinic_ethertype_filter_remove(filter_info,
			HINIC_PKT_TYPE_FIND_ID(PKT_ARP_TYPE));
		ret = hinic_set_fdir_filter(nic_dev->hwdev, PKT_ARP_TYPE,
			filter_info->qid, false, true);
	}

	if (ret)
		PMD_DRV_LOG(ERR, "Clear ethertype failed, filter type: 0x%x",
				filter_info->pkt_type);
}

/* Remove all the ether type filters */
static void hinic_clear_all_fdir_filter(struct rte_eth_dev *dev)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
	struct hinic_tcam_info *tcam_info =
		HINIC_DEV_PRIVATE_TO_TCAM_INFO(dev->data->dev_private);
	struct hinic_tcam_filter *tcam_filter_ptr;

	while ((tcam_filter_ptr = TAILQ_FIRST(&tcam_info->tcam_list)))
		(void)hinic_del_tcam_filter(dev, tcam_filter_ptr);

	(void)hinic_set_fdir_filter(nic_dev->hwdev, 0, 0, 0, false);

	(void)hinic_set_fdir_tcam_rule_filter(nic_dev->hwdev, false);

	(void)hinic_flush_tcam_rule(nic_dev->hwdev);
}

static void hinic_filterlist_flush(struct rte_eth_dev *dev)
{
	struct hinic_ntuple_filter_ele *ntuple_filter_ptr;
	struct hinic_ethertype_filter_ele *ethertype_filter_ptr;
	struct hinic_fdir_rule_ele *fdir_rule_ptr;
	struct hinic_flow_mem *hinic_flow_mem_ptr;
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	while ((ntuple_filter_ptr =
			TAILQ_FIRST(&nic_dev->filter_ntuple_list))) {
		TAILQ_REMOVE(&nic_dev->filter_ntuple_list, ntuple_filter_ptr,
				 entries);
		rte_free(ntuple_filter_ptr);
	}

	while ((ethertype_filter_ptr =
			TAILQ_FIRST(&nic_dev->filter_ethertype_list))) {
		TAILQ_REMOVE(&nic_dev->filter_ethertype_list,
				ethertype_filter_ptr,
				entries);
		rte_free(ethertype_filter_ptr);
	}

	while ((fdir_rule_ptr =
			TAILQ_FIRST(&nic_dev->filter_fdir_rule_list))) {
		TAILQ_REMOVE(&nic_dev->filter_fdir_rule_list, fdir_rule_ptr,
				 entries);
		rte_free(fdir_rule_ptr);
	}

	while ((hinic_flow_mem_ptr =
			TAILQ_FIRST(&nic_dev->hinic_flow_list))) {
		TAILQ_REMOVE(&nic_dev->hinic_flow_list, hinic_flow_mem_ptr,
				 entries);
		rte_free(hinic_flow_mem_ptr->flow);
		rte_free(hinic_flow_mem_ptr);
	}
}

/* Destroy all flow rules associated with a port on hinic. */
static int hinic_flow_flush(struct rte_eth_dev *dev,
				__rte_unused struct rte_flow_error *error)
{
	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);

	hinic_clear_all_ntuple_filter(dev);
	hinic_clear_all_ethertype_filter(dev);
	hinic_clear_all_fdir_filter(dev);
	hinic_filterlist_flush(dev);

	PMD_DRV_LOG(INFO, "Flush flow succeed, func_id: 0x%x",
			hinic_global_func_id(nic_dev->hwdev));
	return 0;
}

void hinic_destroy_fdir_filter(struct rte_eth_dev *dev)
{
	hinic_clear_all_ntuple_filter(dev);
	hinic_clear_all_ethertype_filter(dev);
	hinic_clear_all_fdir_filter(dev);
	hinic_filterlist_flush(dev);
}

const struct rte_flow_ops hinic_flow_ops = {
	.validate = hinic_flow_validate,
	.create = hinic_flow_create,
	.destroy = hinic_flow_destroy,
	.flush = hinic_flow_flush,
};