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
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018-2021 HiSilicon Limited.
 */

#include <rte_bus_pci.h>
#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_geneve.h>
#include <rte_vxlan.h>
#include <ethdev_driver.h>
#include <rte_io.h>
#include <rte_net.h>
#include <rte_malloc.h>
#if defined(RTE_ARCH_ARM64)
#include <rte_cpuflags.h>
#include <rte_vect.h>
#endif

#include "hns3_common.h"
#include "hns3_rxtx.h"
#include "hns3_regs.h"
#include "hns3_logs.h"
#include "hns3_mp.h"

#define HNS3_CFG_DESC_NUM(num)	((num) / 8 - 1)
#define HNS3_RX_RING_PREFETCTH_MASK	3

static void
hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
{
	uint16_t i;

	/* Note: Fake rx queue will not enter here */
	if (rxq->sw_ring == NULL)
		return;

	if (rxq->rx_rearm_nb == 0) {
		for (i = 0; i < rxq->nb_rx_desc; i++) {
			if (rxq->sw_ring[i].mbuf != NULL) {
				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
				rxq->sw_ring[i].mbuf = NULL;
			}
		}
	} else {
		for (i = rxq->next_to_use;
		     i != rxq->rx_rearm_start;
		     i = (i + 1) % rxq->nb_rx_desc) {
			if (rxq->sw_ring[i].mbuf != NULL) {
				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
				rxq->sw_ring[i].mbuf = NULL;
			}
		}
	}

	for (i = 0; i < rxq->bulk_mbuf_num; i++)
		rte_pktmbuf_free_seg(rxq->bulk_mbuf[i]);
	rxq->bulk_mbuf_num = 0;

	if (rxq->pkt_first_seg) {
		rte_pktmbuf_free(rxq->pkt_first_seg);
		rxq->pkt_first_seg = NULL;
	}
}

static void
hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
{
	uint16_t i;

	/* Note: Fake tx queue will not enter here */
	if (txq->sw_ring) {
		for (i = 0; i < txq->nb_tx_desc; i++) {
			if (txq->sw_ring[i].mbuf) {
				rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
				txq->sw_ring[i].mbuf = NULL;
			}
		}
	}
}

static void
hns3_rx_queue_release(void *queue)
{
	struct hns3_rx_queue *rxq = queue;
	if (rxq) {
		hns3_rx_queue_release_mbufs(rxq);
		if (rxq->mz)
			rte_memzone_free(rxq->mz);
		if (rxq->sw_ring)
			rte_free(rxq->sw_ring);
		rte_free(rxq);
	}
}

static void
hns3_tx_queue_release(void *queue)
{
	struct hns3_tx_queue *txq = queue;
	if (txq) {
		hns3_tx_queue_release_mbufs(txq);
		if (txq->mz)
			rte_memzone_free(txq->mz);
		if (txq->sw_ring)
			rte_free(txq->sw_ring);
		if (txq->free)
			rte_free(txq->free);
		rte_free(txq);
	}
}

static void
hns3_rx_queue_release_lock(void *queue)
{
	struct hns3_rx_queue *rxq = queue;
	struct hns3_adapter *hns;

	if (rxq == NULL)
		return;

	hns = rxq->hns;
	rte_spinlock_lock(&hns->hw.lock);
	hns3_rx_queue_release(queue);
	rte_spinlock_unlock(&hns->hw.lock);
}

void
hns3_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t queue_id)
{
	hns3_rx_queue_release_lock(dev->data->rx_queues[queue_id]);
}

static void
hns3_tx_queue_release_lock(void *queue)
{
	struct hns3_tx_queue *txq = queue;
	struct hns3_adapter *hns;

	if (txq == NULL)
		return;

	hns = txq->hns;
	rte_spinlock_lock(&hns->hw.lock);
	hns3_tx_queue_release(queue);
	rte_spinlock_unlock(&hns->hw.lock);
}

void
hns3_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t queue_id)
{
	hns3_tx_queue_release_lock(dev->data->tx_queues[queue_id]);
}

static void
hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
{
	struct hns3_rx_queue *rxq = queue;
	struct hns3_adapter *hns;
	struct hns3_hw *hw;
	uint16_t idx;

	if (rxq == NULL)
		return;

	hns = rxq->hns;
	hw = &hns->hw;
	idx = rxq->queue_id;
	if (hw->fkq_data.rx_queues[idx]) {
		hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
		hw->fkq_data.rx_queues[idx] = NULL;
	}

	/* free fake rx queue arrays */
	if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
		hw->fkq_data.nb_fake_rx_queues = 0;
		rte_free(hw->fkq_data.rx_queues);
		hw->fkq_data.rx_queues = NULL;
	}
}

static void
hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
{
	struct hns3_tx_queue *txq = queue;
	struct hns3_adapter *hns;
	struct hns3_hw *hw;
	uint16_t idx;

	if (txq == NULL)
		return;

	hns = txq->hns;
	hw = &hns->hw;
	idx = txq->queue_id;
	if (hw->fkq_data.tx_queues[idx]) {
		hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
		hw->fkq_data.tx_queues[idx] = NULL;
	}

	/* free fake tx queue arrays */
	if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
		hw->fkq_data.nb_fake_tx_queues = 0;
		rte_free(hw->fkq_data.tx_queues);
		hw->fkq_data.tx_queues = NULL;
	}
}

static void
hns3_free_rx_queues(struct rte_eth_dev *dev)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_fake_queue_data *fkq_data;
	struct hns3_hw *hw = &hns->hw;
	uint16_t nb_rx_q;
	uint16_t i;

	nb_rx_q = hw->data->nb_rx_queues;
	for (i = 0; i < nb_rx_q; i++) {
		if (dev->data->rx_queues[i]) {
			hns3_rx_queue_release(dev->data->rx_queues[i]);
			dev->data->rx_queues[i] = NULL;
		}
	}

	/* Free fake Rx queues */
	fkq_data = &hw->fkq_data;
	for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
		if (fkq_data->rx_queues[i])
			hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
	}
}

static void
hns3_free_tx_queues(struct rte_eth_dev *dev)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_fake_queue_data *fkq_data;
	struct hns3_hw *hw = &hns->hw;
	uint16_t nb_tx_q;
	uint16_t i;

	nb_tx_q = hw->data->nb_tx_queues;
	for (i = 0; i < nb_tx_q; i++) {
		if (dev->data->tx_queues[i]) {
			hns3_tx_queue_release(dev->data->tx_queues[i]);
			dev->data->tx_queues[i] = NULL;
		}
	}

	/* Free fake Tx queues */
	fkq_data = &hw->fkq_data;
	for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
		if (fkq_data->tx_queues[i])
			hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
	}
}

void
hns3_free_all_queues(struct rte_eth_dev *dev)
{
	hns3_free_rx_queues(dev);
	hns3_free_tx_queues(dev);
}

static int
hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
{
	struct rte_mbuf *mbuf;
	uint64_t dma_addr;
	uint16_t i;

	for (i = 0; i < rxq->nb_rx_desc; i++) {
		mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
		if (unlikely(mbuf == NULL)) {
			hns3_err(hw, "Failed to allocate RXD[%u] for rx queue!",
				 i);
			hns3_rx_queue_release_mbufs(rxq);
			return -ENOMEM;
		}

		rte_mbuf_refcnt_set(mbuf, 1);
		mbuf->next = NULL;
		mbuf->data_off = RTE_PKTMBUF_HEADROOM;
		mbuf->nb_segs = 1;
		mbuf->port = rxq->port_id;

		rxq->sw_ring[i].mbuf = mbuf;
		dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
		rxq->rx_ring[i].addr = dma_addr;
		rxq->rx_ring[i].rx.bd_base_info = 0;
	}

	return 0;
}

static int
hns3_buf_size2type(uint32_t buf_size)
{
	int bd_size_type;

	switch (buf_size) {
	case 512:
		bd_size_type = HNS3_BD_SIZE_512_TYPE;
		break;
	case 1024:
		bd_size_type = HNS3_BD_SIZE_1024_TYPE;
		break;
	case 4096:
		bd_size_type = HNS3_BD_SIZE_4096_TYPE;
		break;
	default:
		bd_size_type = HNS3_BD_SIZE_2048_TYPE;
	}

	return bd_size_type;
}

static void
hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
{
	uint32_t rx_buf_len = rxq->rx_buf_len;
	uint64_t dma_addr = rxq->rx_ring_phys_addr;

	hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
	hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
		       (uint32_t)(dma_addr >> 32));

	hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
		       hns3_buf_size2type(rx_buf_len));
	hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
		       HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
}

static void
hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
{
	uint64_t dma_addr = txq->tx_ring_phys_addr;

	hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
	hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
		       (uint32_t)(dma_addr >> 32));

	hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
		       HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
}

void
hns3_update_all_queues_pvid_proc_en(struct hns3_hw *hw)
{
	uint16_t nb_rx_q = hw->data->nb_rx_queues;
	uint16_t nb_tx_q = hw->data->nb_tx_queues;
	struct hns3_rx_queue *rxq;
	struct hns3_tx_queue *txq;
	bool pvid_en;
	int i;

	pvid_en = hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE;
	for (i = 0; i < hw->cfg_max_queues; i++) {
		if (i < nb_rx_q) {
			rxq = hw->data->rx_queues[i];
			if (rxq != NULL)
				rxq->pvid_sw_discard_en = pvid_en;
		}
		if (i < nb_tx_q) {
			txq = hw->data->tx_queues[i];
			if (txq != NULL)
				txq->pvid_sw_shift_en = pvid_en;
		}
	}
}

static void
hns3_stop_unused_queue(void *tqp_base, enum hns3_ring_type queue_type)
{
	uint32_t reg_offset;
	uint32_t reg;

	reg_offset = queue_type == HNS3_RING_TYPE_TX ?
				   HNS3_RING_TX_EN_REG : HNS3_RING_RX_EN_REG;
	reg = hns3_read_reg(tqp_base, reg_offset);
	reg &= ~BIT(HNS3_RING_EN_B);
	hns3_write_reg(tqp_base, reg_offset, reg);
}

void
hns3_enable_all_queues(struct hns3_hw *hw, bool en)
{
	uint16_t nb_rx_q = hw->data->nb_rx_queues;
	uint16_t nb_tx_q = hw->data->nb_tx_queues;
	struct hns3_rx_queue *rxq;
	struct hns3_tx_queue *txq;
	uint32_t rcb_reg;
	void *tqp_base;
	int i;

	for (i = 0; i < hw->cfg_max_queues; i++) {
		if (hns3_dev_get_support(hw, INDEP_TXRX)) {
			rxq = i < nb_rx_q ? hw->data->rx_queues[i] : NULL;
			txq = i < nb_tx_q ? hw->data->tx_queues[i] : NULL;

			tqp_base = (void *)((char *)hw->io_base +
					hns3_get_tqp_reg_offset(i));
			/*
			 * If queue struct is not initialized, it means the
			 * related HW ring has not been initialized yet.
			 * So, these queues should be disabled before enable
			 * the tqps to avoid a HW exception since the queues
			 * are enabled by default.
			 */
			if (rxq == NULL)
				hns3_stop_unused_queue(tqp_base,
							HNS3_RING_TYPE_RX);
			if (txq == NULL)
				hns3_stop_unused_queue(tqp_base,
							HNS3_RING_TYPE_TX);
		} else {
			rxq = i < nb_rx_q ? hw->data->rx_queues[i] :
			      hw->fkq_data.rx_queues[i - nb_rx_q];

			tqp_base = rxq->io_base;
		}
		/*
		 * This is the master switch that used to control the enabling
		 * of a pair of Tx and Rx queues. Both the Rx and Tx point to
		 * the same register
		 */
		rcb_reg = hns3_read_reg(tqp_base, HNS3_RING_EN_REG);
		if (en)
			rcb_reg |= BIT(HNS3_RING_EN_B);
		else
			rcb_reg &= ~BIT(HNS3_RING_EN_B);
		hns3_write_reg(tqp_base, HNS3_RING_EN_REG, rcb_reg);
	}
}

static void
hns3_enable_txq(struct hns3_tx_queue *txq, bool en)
{
	struct hns3_hw *hw = &txq->hns->hw;
	uint32_t reg;

	if (hns3_dev_get_support(hw, INDEP_TXRX)) {
		reg = hns3_read_dev(txq, HNS3_RING_TX_EN_REG);
		if (en)
			reg |= BIT(HNS3_RING_EN_B);
		else
			reg &= ~BIT(HNS3_RING_EN_B);
		hns3_write_dev(txq, HNS3_RING_TX_EN_REG, reg);
	}
	txq->enabled = en;
}

static void
hns3_enable_rxq(struct hns3_rx_queue *rxq, bool en)
{
	struct hns3_hw *hw = &rxq->hns->hw;
	uint32_t reg;

	if (hns3_dev_get_support(hw, INDEP_TXRX)) {
		reg = hns3_read_dev(rxq, HNS3_RING_RX_EN_REG);
		if (en)
			reg |= BIT(HNS3_RING_EN_B);
		else
			reg &= ~BIT(HNS3_RING_EN_B);
		hns3_write_dev(rxq, HNS3_RING_RX_EN_REG, reg);
	}
	rxq->enabled = en;
}

int
hns3_start_all_txqs(struct rte_eth_dev *dev)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	struct hns3_tx_queue *txq;
	uint16_t i, j;

	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		txq = hw->data->tx_queues[i];
		if (!txq) {
			hns3_err(hw, "Tx queue %u not available or setup.", i);
			goto start_txqs_fail;
		}
		/*
		 * Tx queue is enabled by default. Therefore, the Tx queues
		 * needs to be disabled when deferred_start is set. There is
		 * another master switch used to control the enabling of a pair
		 * of Tx and Rx queues. And the master switch is disabled by
		 * default.
		 */
		if (txq->tx_deferred_start)
			hns3_enable_txq(txq, false);
		else
			hns3_enable_txq(txq, true);
	}
	return 0;

start_txqs_fail:
	for (j = 0; j < i; j++) {
		txq = hw->data->tx_queues[j];
		hns3_enable_txq(txq, false);
	}
	return -EINVAL;
}

int
hns3_start_all_rxqs(struct rte_eth_dev *dev)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	struct hns3_rx_queue *rxq;
	uint16_t i, j;

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		rxq = hw->data->rx_queues[i];
		if (!rxq) {
			hns3_err(hw, "Rx queue %u not available or setup.", i);
			goto start_rxqs_fail;
		}
		/*
		 * Rx queue is enabled by default. Therefore, the Rx queues
		 * needs to be disabled when deferred_start is set. There is
		 * another master switch used to control the enabling of a pair
		 * of Tx and Rx queues. And the master switch is disabled by
		 * default.
		 */
		if (rxq->rx_deferred_start)
			hns3_enable_rxq(rxq, false);
		else
			hns3_enable_rxq(rxq, true);
	}
	return 0;

start_rxqs_fail:
	for (j = 0; j < i; j++) {
		rxq = hw->data->rx_queues[j];
		hns3_enable_rxq(rxq, false);
	}
	return -EINVAL;
}

void
hns3_restore_tqp_enable_state(struct hns3_hw *hw)
{
	struct hns3_rx_queue *rxq;
	struct hns3_tx_queue *txq;
	uint16_t i;

	for (i = 0; i < hw->data->nb_rx_queues; i++) {
		rxq = hw->data->rx_queues[i];
		if (rxq != NULL)
			hns3_enable_rxq(rxq, rxq->enabled);
	}

	for (i = 0; i < hw->data->nb_tx_queues; i++) {
		txq = hw->data->tx_queues[i];
		if (txq != NULL)
			hns3_enable_txq(txq, txq->enabled);
	}
}

void
hns3_stop_all_txqs(struct rte_eth_dev *dev)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	struct hns3_tx_queue *txq;
	uint16_t i;

	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		txq = hw->data->tx_queues[i];
		if (!txq)
			continue;
		hns3_enable_txq(txq, false);
	}
}

static int
hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
{
	struct hns3_cfg_com_tqp_queue_cmd *req;
	struct hns3_cmd_desc desc;
	int ret;

	req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
	req->tqp_id = rte_cpu_to_le_16(queue_id);
	req->stream_id = 0;
	hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret)
		hns3_err(hw, "TQP enable fail, ret = %d", ret);

	return ret;
}

static int
hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
{
	struct hns3_reset_tqp_queue_cmd *req;
	struct hns3_cmd_desc desc;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);

	req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
	req->tqp_id = rte_cpu_to_le_16(queue_id);
	hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret)
		hns3_err(hw, "send tqp reset cmd error, queue_id = %u, "
			     "ret = %d", queue_id, ret);

	return ret;
}

static int
hns3_get_tqp_reset_status(struct hns3_hw *hw, uint16_t queue_id,
			  uint8_t *reset_status)
{
	struct hns3_reset_tqp_queue_cmd *req;
	struct hns3_cmd_desc desc;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);

	req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
	req->tqp_id = rte_cpu_to_le_16(queue_id);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret) {
		hns3_err(hw, "get tqp reset status error, queue_id = %u, "
			     "ret = %d.", queue_id, ret);
		return ret;
	}
	*reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
	return ret;
}

static int
hns3pf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
{
#define HNS3_TQP_RESET_TRY_MS	200
	uint16_t wait_time = 0;
	uint8_t reset_status;
	int ret;

	/*
	 * In current version VF is not supported when PF is driven by DPDK
	 * driver, all task queue pairs are mapped to PF function, so PF's queue
	 * id is equals to the global queue id in PF range.
	 */
	ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
	if (ret) {
		hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
		return ret;
	}

	do {
		/* Wait for tqp hw reset */
		rte_delay_ms(HNS3_POLL_RESPONE_MS);
		wait_time += HNS3_POLL_RESPONE_MS;
		ret = hns3_get_tqp_reset_status(hw, queue_id, &reset_status);
		if (ret)
			goto tqp_reset_fail;

		if (reset_status)
			break;
	} while (wait_time < HNS3_TQP_RESET_TRY_MS);

	if (!reset_status) {
		ret = -ETIMEDOUT;
		hns3_err(hw, "reset tqp timeout, queue_id = %u, ret = %d",
			     queue_id, ret);
		goto tqp_reset_fail;
	}

	ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
	if (ret)
		hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);

	return ret;

tqp_reset_fail:
	hns3_send_reset_tqp_cmd(hw, queue_id, false);
	return ret;
}

static int
hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
{
	uint8_t msg_data[2];
	int ret;

	memcpy(msg_data, &queue_id, sizeof(uint16_t));

	ret = hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
				 sizeof(msg_data), true, NULL, 0);
	if (ret)
		hns3_err(hw, "fail to reset tqp, queue_id = %u, ret = %d.",
			 queue_id, ret);
	return ret;
}

static int
hns3_reset_rcb_cmd(struct hns3_hw *hw, uint8_t *reset_status)
{
	struct hns3_reset_cmd *req;
	struct hns3_cmd_desc desc;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
	req = (struct hns3_reset_cmd *)desc.data;
	hns3_set_bit(req->fun_reset_rcb, HNS3_CFG_RESET_RCB_B, 1);

	/*
	 * The start qid should be the global qid of the first tqp of the
	 * function which should be reset in this port. Since our PF not
	 * support take over of VFs, so we only need to reset function 0,
	 * and its start qid is always 0.
	 */
	req->fun_reset_rcb_vqid_start = rte_cpu_to_le_16(0);
	req->fun_reset_rcb_vqid_num = rte_cpu_to_le_16(hw->cfg_max_queues);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret) {
		hns3_err(hw, "fail to send rcb reset cmd, ret = %d.", ret);
		return ret;
	}

	*reset_status = req->fun_reset_rcb_return_status;
	return 0;
}

static int
hns3pf_reset_all_tqps(struct hns3_hw *hw)
{
#define HNS3_RESET_RCB_NOT_SUPPORT	0U
#define HNS3_RESET_ALL_TQP_SUCCESS	1U
	uint8_t reset_status;
	int ret;
	int i;

	ret = hns3_reset_rcb_cmd(hw, &reset_status);
	if (ret)
		return ret;

	/*
	 * If the firmware version is low, it may not support the rcb reset
	 * which means reset all the tqps at a time. In this case, we should
	 * reset tqps one by one.
	 */
	if (reset_status == HNS3_RESET_RCB_NOT_SUPPORT) {
		for (i = 0; i < hw->cfg_max_queues; i++) {
			ret = hns3pf_reset_tqp(hw, i);
			if (ret) {
				hns3_err(hw,
				  "fail to reset tqp, queue_id = %d, ret = %d.",
				  i, ret);
				return ret;
			}
		}
	} else if (reset_status != HNS3_RESET_ALL_TQP_SUCCESS) {
		hns3_err(hw, "fail to reset all tqps, reset_status = %u.",
				reset_status);
		return -EIO;
	}

	return 0;
}

static int
hns3vf_reset_all_tqps(struct hns3_hw *hw)
{
#define HNS3VF_RESET_ALL_TQP_DONE	1U
	uint8_t reset_status;
	uint8_t msg_data[2];
	int ret;
	int i;

	memset(msg_data, 0, sizeof(uint16_t));
	ret = hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
				sizeof(msg_data), true, &reset_status,
				sizeof(reset_status));
	if (ret) {
		hns3_err(hw, "fail to send rcb reset mbx, ret = %d.", ret);
		return ret;
	}

	if (reset_status == HNS3VF_RESET_ALL_TQP_DONE)
		return 0;

	/*
	 * If the firmware version or kernel PF version is low, it may not
	 * support the rcb reset which means reset all the tqps at a time.
	 * In this case, we should reset tqps one by one.
	 */
	for (i = 1; i < hw->cfg_max_queues; i++) {
		ret = hns3vf_reset_tqp(hw, i);
		if (ret)
			return ret;
	}

	return 0;
}

int
hns3_reset_all_tqps(struct hns3_adapter *hns)
{
	struct hns3_hw *hw = &hns->hw;
	int ret, i;

	/* Disable all queues before reset all queues */
	for (i = 0; i < hw->cfg_max_queues; i++) {
		ret = hns3_tqp_enable(hw, i, false);
		if (ret) {
			hns3_err(hw,
			    "fail to disable tqps before tqps reset, ret = %d.",
			    ret);
			return ret;
		}
	}

	if (hns->is_vf)
		return hns3vf_reset_all_tqps(hw);
	else
		return hns3pf_reset_all_tqps(hw);
}

static int
hns3_send_reset_queue_cmd(struct hns3_hw *hw, uint16_t queue_id,
			  enum hns3_ring_type queue_type, bool enable)
{
	struct hns3_reset_tqp_queue_cmd *req;
	struct hns3_cmd_desc desc;
	int queue_direction;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, false);

	req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
	req->tqp_id = rte_cpu_to_le_16(queue_id);
	queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
	req->queue_direction = rte_cpu_to_le_16(queue_direction);
	hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret)
		hns3_err(hw, "send queue reset cmd error, queue_id = %u, "
			 "queue_type = %s, ret = %d.", queue_id,
			 queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
	return ret;
}

static int
hns3_get_queue_reset_status(struct hns3_hw *hw, uint16_t queue_id,
			    enum hns3_ring_type queue_type,
			    uint8_t *reset_status)
{
	struct hns3_reset_tqp_queue_cmd *req;
	struct hns3_cmd_desc desc;
	int queue_direction;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, true);

	req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
	req->tqp_id = rte_cpu_to_le_16(queue_id);
	queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
	req->queue_direction = rte_cpu_to_le_16(queue_direction);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret) {
		hns3_err(hw, "get queue reset status error, queue_id = %u "
			 "queue_type = %s, ret = %d.", queue_id,
			 queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
		return ret;
	}

	*reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
	return  ret;
}

static int
hns3_reset_queue(struct hns3_hw *hw, uint16_t queue_id,
		 enum hns3_ring_type queue_type)
{
#define HNS3_QUEUE_RESET_TRY_MS	200
	struct hns3_tx_queue *txq;
	struct hns3_rx_queue *rxq;
	uint32_t reset_wait_times;
	uint32_t max_wait_times;
	uint8_t reset_status;
	int ret;

	if (queue_type == HNS3_RING_TYPE_TX) {
		txq = hw->data->tx_queues[queue_id];
		hns3_enable_txq(txq, false);
	} else {
		rxq = hw->data->rx_queues[queue_id];
		hns3_enable_rxq(rxq, false);
	}

	ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, true);
	if (ret) {
		hns3_err(hw, "send reset queue cmd fail, ret = %d.", ret);
		return ret;
	}

	reset_wait_times = 0;
	max_wait_times = HNS3_QUEUE_RESET_TRY_MS / HNS3_POLL_RESPONE_MS;
	while (reset_wait_times < max_wait_times) {
		/* Wait for queue hw reset */
		rte_delay_ms(HNS3_POLL_RESPONE_MS);
		ret = hns3_get_queue_reset_status(hw, queue_id,
						queue_type, &reset_status);
		if (ret)
			goto queue_reset_fail;

		if (reset_status)
			break;
		reset_wait_times++;
	}

	if (!reset_status) {
		hns3_err(hw, "reset queue timeout, queue_id = %u, "
			     "queue_type = %s", queue_id,
			     queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx");
		ret = -ETIMEDOUT;
		goto queue_reset_fail;
	}

	ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
	if (ret)
		hns3_err(hw, "deassert queue reset fail, ret = %d.", ret);

	return ret;

queue_reset_fail:
	hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
	return ret;
}

uint32_t
hns3_get_tqp_intr_reg_offset(uint16_t tqp_intr_id)
{
	uint32_t reg_offset;

	/* Need an extend offset to config queues > 64 */
	if (tqp_intr_id < HNS3_MIN_EXT_TQP_INTR_ID)
		reg_offset = HNS3_TQP_INTR_REG_BASE +
			     tqp_intr_id * HNS3_TQP_INTR_LOW_ORDER_OFFSET;
	else
		reg_offset = HNS3_TQP_INTR_EXT_REG_BASE +
			     tqp_intr_id / HNS3_MIN_EXT_TQP_INTR_ID *
			     HNS3_TQP_INTR_HIGH_ORDER_OFFSET +
			     tqp_intr_id % HNS3_MIN_EXT_TQP_INTR_ID *
			     HNS3_TQP_INTR_LOW_ORDER_OFFSET;

	return reg_offset;
}

void
hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
		       uint8_t gl_idx, uint16_t gl_value)
{
	uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
			     HNS3_TQP_INTR_GL1_REG,
			     HNS3_TQP_INTR_GL2_REG};
	uint32_t addr, value;

	if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
		return;

	addr = offset[gl_idx] + hns3_get_tqp_intr_reg_offset(queue_id);
	if (hw->intr.gl_unit == HNS3_INTR_COALESCE_GL_UINT_1US)
		value = gl_value | HNS3_TQP_INTR_GL_UNIT_1US;
	else
		value = HNS3_GL_USEC_TO_REG(gl_value);

	hns3_write_dev(hw, addr, value);
}

void
hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
{
	uint32_t addr, value;

	if (rl_value > HNS3_TQP_INTR_RL_MAX)
		return;

	addr = HNS3_TQP_INTR_RL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
	value = HNS3_RL_USEC_TO_REG(rl_value);
	if (value > 0)
		value |= HNS3_TQP_INTR_RL_ENABLE_MASK;

	hns3_write_dev(hw, addr, value);
}

void
hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id, uint16_t ql_value)
{
	uint32_t addr;

	/*
	 * int_ql_max == 0 means the hardware does not support QL,
	 * QL regs config is not permitted if QL is not supported,
	 * here just return.
	 */
	if (hw->intr.int_ql_max == HNS3_INTR_QL_NONE)
		return;

	addr = HNS3_TQP_INTR_TX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
	hns3_write_dev(hw, addr, ql_value);

	addr = HNS3_TQP_INTR_RX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
	hns3_write_dev(hw, addr, ql_value);
}

static void
hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
{
	uint32_t addr, value;

	addr = HNS3_TQP_INTR_CTRL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
	value = en ? 1 : 0;

	hns3_write_dev(hw, addr, value);
}

/*
 * Enable all rx queue interrupt when in interrupt rx mode.
 * This api was called before enable queue rx&tx (in normal start or reset
 * recover scenes), used to fix hardware rx queue interrupt enable was clear
 * when FLR.
 */
void
hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
{
	struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
	uint16_t nb_rx_q = hw->data->nb_rx_queues;
	int i;

	if (dev->data->dev_conf.intr_conf.rxq == 0)
		return;

	for (i = 0; i < nb_rx_q; i++)
		hns3_queue_intr_enable(hw, i, en);
}

int
hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
{
	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
	struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);

	if (dev->data->dev_conf.intr_conf.rxq == 0)
		return -ENOTSUP;

	hns3_queue_intr_enable(hw, queue_id, true);

	return rte_intr_ack(intr_handle);
}

int
hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);

	if (dev->data->dev_conf.intr_conf.rxq == 0)
		return -ENOTSUP;

	hns3_queue_intr_enable(hw, queue_id, false);

	return 0;
}

static int
hns3_init_rxq(struct hns3_adapter *hns, uint16_t idx)
{
	struct hns3_hw *hw = &hns->hw;
	struct hns3_rx_queue *rxq;
	int ret;

	PMD_INIT_FUNC_TRACE();

	rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
	ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
	if (ret) {
		hns3_err(hw, "fail to alloc mbuf for Rx queue %u, ret = %d.",
			 idx, ret);
		return ret;
	}

	rxq->next_to_use = 0;
	rxq->rx_rearm_start = 0;
	rxq->rx_free_hold = 0;
	rxq->rx_rearm_nb = 0;
	rxq->pkt_first_seg = NULL;
	rxq->pkt_last_seg = NULL;
	hns3_init_rx_queue_hw(rxq);
	hns3_rxq_vec_setup(rxq);

	return 0;
}

static void
hns3_init_fake_rxq(struct hns3_adapter *hns, uint16_t idx)
{
	struct hns3_hw *hw = &hns->hw;
	struct hns3_rx_queue *rxq;

	rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
	rxq->next_to_use = 0;
	rxq->rx_free_hold = 0;
	rxq->rx_rearm_start = 0;
	rxq->rx_rearm_nb = 0;
	hns3_init_rx_queue_hw(rxq);
}

static void
hns3_init_txq(struct hns3_tx_queue *txq)
{
	struct hns3_desc *desc;
	int i;

	/* Clear tx bd */
	desc = txq->tx_ring;
	for (i = 0; i < txq->nb_tx_desc; i++) {
		desc->tx.tp_fe_sc_vld_ra_ri = 0;
		desc++;
	}

	txq->next_to_use = 0;
	txq->next_to_clean = 0;
	txq->tx_bd_ready = txq->nb_tx_desc - 1;
	hns3_init_tx_queue_hw(txq);
}

static void
hns3_init_tx_ring_tc(struct hns3_adapter *hns)
{
	struct hns3_hw *hw = &hns->hw;
	struct hns3_tx_queue *txq;
	int i, num;

	for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
		struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
		int j;

		if (!tc_queue->enable)
			continue;

		for (j = 0; j < tc_queue->tqp_count; j++) {
			num = tc_queue->tqp_offset + j;
			txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
			if (txq == NULL)
				continue;

			hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
		}
	}
}

static int
hns3_init_rx_queues(struct hns3_adapter *hns)
{
	struct hns3_hw *hw = &hns->hw;
	struct hns3_rx_queue *rxq;
	uint16_t i, j;
	int ret;

	/* Initialize RSS for queues */
	ret = hns3_config_rss(hns);
	if (ret) {
		hns3_err(hw, "failed to configure rss, ret = %d.", ret);
		return ret;
	}

	for (i = 0; i < hw->data->nb_rx_queues; i++) {
		rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
		if (!rxq) {
			hns3_err(hw, "Rx queue %u not available or setup.", i);
			goto out;
		}

		if (rxq->rx_deferred_start)
			continue;

		ret = hns3_init_rxq(hns, i);
		if (ret) {
			hns3_err(hw, "failed to init Rx queue %u, ret = %d.", i,
				 ret);
			goto out;
		}
	}

	for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++)
		hns3_init_fake_rxq(hns, i);

	return 0;

out:
	for (j = 0; j < i; j++) {
		rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
		hns3_rx_queue_release_mbufs(rxq);
	}

	return ret;
}

static int
hns3_init_tx_queues(struct hns3_adapter *hns)
{
	struct hns3_hw *hw = &hns->hw;
	struct hns3_tx_queue *txq;
	uint16_t i;

	for (i = 0; i < hw->data->nb_tx_queues; i++) {
		txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
		if (!txq) {
			hns3_err(hw, "Tx queue %u not available or setup.", i);
			return -EINVAL;
		}

		if (txq->tx_deferred_start)
			continue;
		hns3_init_txq(txq);
	}

	for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
		txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
		hns3_init_txq(txq);
	}
	hns3_init_tx_ring_tc(hns);

	return 0;
}

/*
 * Init all queues.
 * Note: just init and setup queues, and don't enable tqps.
 */
int
hns3_init_queues(struct hns3_adapter *hns, bool reset_queue)
{
	struct hns3_hw *hw = &hns->hw;
	int ret;

	if (reset_queue) {
		ret = hns3_reset_all_tqps(hns);
		if (ret) {
			hns3_err(hw, "failed to reset all queues, ret = %d.",
				 ret);
			return ret;
		}
	}

	ret = hns3_init_rx_queues(hns);
	if (ret) {
		hns3_err(hw, "failed to init rx queues, ret = %d.", ret);
		return ret;
	}

	ret = hns3_init_tx_queues(hns);
	if (ret) {
		hns3_dev_release_mbufs(hns);
		hns3_err(hw, "failed to init tx queues, ret = %d.", ret);
	}

	return ret;
}

void
hns3_start_tqps(struct hns3_hw *hw)
{
	struct hns3_tx_queue *txq;
	struct hns3_rx_queue *rxq;
	uint16_t i;

	hns3_enable_all_queues(hw, true);

	for (i = 0; i < hw->data->nb_tx_queues; i++) {
		txq = hw->data->tx_queues[i];
		if (txq->enabled)
			hw->data->tx_queue_state[i] =
				RTE_ETH_QUEUE_STATE_STARTED;
	}

	for (i = 0; i < hw->data->nb_rx_queues; i++) {
		rxq = hw->data->rx_queues[i];
		if (rxq->enabled)
			hw->data->rx_queue_state[i] =
				RTE_ETH_QUEUE_STATE_STARTED;
	}
}

void
hns3_stop_tqps(struct hns3_hw *hw)
{
	uint16_t i;

	hns3_enable_all_queues(hw, false);

	for (i = 0; i < hw->data->nb_tx_queues; i++)
		hw->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;

	for (i = 0; i < hw->data->nb_rx_queues; i++)
		hw->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
}

/*
 * Iterate over all Rx Queue, and call the callback() function for each Rx
 * queue.
 *
 * @param[in] dev
 *   The target eth dev.
 * @param[in] callback
 *   The function to call for each queue.
 *   if callback function return nonzero will stop iterate and return it's value
 * @param[in] arg
 *   The arguments to provide the callback function with.
 *
 * @return
 *   0 on success, otherwise with errno set.
 */
int
hns3_rxq_iterate(struct rte_eth_dev *dev,
		 int (*callback)(struct hns3_rx_queue *, void *), void *arg)
{
	uint32_t i;
	int ret;

	if (dev->data->rx_queues == NULL)
		return -EINVAL;

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		ret = callback(dev->data->rx_queues[i], arg);
		if (ret != 0)
			return ret;
	}

	return 0;
}

static void*
hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
			    struct hns3_queue_info *q_info)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	const struct rte_memzone *rx_mz;
	struct hns3_rx_queue *rxq;
	unsigned int rx_desc;

	rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
				 RTE_CACHE_LINE_SIZE, q_info->socket_id);
	if (rxq == NULL) {
		hns3_err(hw, "Failed to allocate memory for No.%u rx ring!",
			 q_info->idx);
		return NULL;
	}

	/* Allocate rx ring hardware descriptors. */
	rxq->queue_id = q_info->idx;
	rxq->nb_rx_desc = q_info->nb_desc;

	/*
	 * Allocate a litter more memory because rx vector functions
	 * don't check boundaries each time.
	 */
	rx_desc = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
			sizeof(struct hns3_desc);
	rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
					 rx_desc, HNS3_RING_BASE_ALIGN,
					 q_info->socket_id);
	if (rx_mz == NULL) {
		hns3_err(hw, "Failed to reserve DMA memory for No.%u rx ring!",
			 q_info->idx);
		hns3_rx_queue_release(rxq);
		return NULL;
	}
	rxq->mz = rx_mz;
	rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
	rxq->rx_ring_phys_addr = rx_mz->iova;

	hns3_dbg(hw, "No.%u rx descriptors iova 0x%" PRIx64, q_info->idx,
		 rxq->rx_ring_phys_addr);

	return rxq;
}

static int
hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
			 uint16_t nb_desc, unsigned int socket_id)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_hw *hw = &hns->hw;
	struct hns3_queue_info q_info;
	struct hns3_rx_queue *rxq;
	uint16_t nb_rx_q;

	if (hw->fkq_data.rx_queues[idx]) {
		hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
		hw->fkq_data.rx_queues[idx] = NULL;
	}

	q_info.idx = idx;
	q_info.socket_id = socket_id;
	q_info.nb_desc = nb_desc;
	q_info.type = "hns3 fake RX queue";
	q_info.ring_name = "rx_fake_ring";
	rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
	if (rxq == NULL) {
		hns3_err(hw, "Failed to setup No.%u fake rx ring.", idx);
		return -ENOMEM;
	}

	/* Don't need alloc sw_ring, because upper applications don't use it */
	rxq->sw_ring = NULL;

	rxq->hns = hns;
	rxq->rx_deferred_start = false;
	rxq->port_id = dev->data->port_id;
	rxq->configured = true;
	nb_rx_q = dev->data->nb_rx_queues;
	rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
				(nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
	rxq->rx_buf_len = HNS3_MIN_BD_BUF_SIZE;

	rte_spinlock_lock(&hw->lock);
	hw->fkq_data.rx_queues[idx] = rxq;
	rte_spinlock_unlock(&hw->lock);

	return 0;
}

static void*
hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
			    struct hns3_queue_info *q_info)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	const struct rte_memzone *tx_mz;
	struct hns3_tx_queue *txq;
	struct hns3_desc *desc;
	unsigned int tx_desc;
	int i;

	txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
				 RTE_CACHE_LINE_SIZE, q_info->socket_id);
	if (txq == NULL) {
		hns3_err(hw, "Failed to allocate memory for No.%u tx ring!",
			 q_info->idx);
		return NULL;
	}

	/* Allocate tx ring hardware descriptors. */
	txq->queue_id = q_info->idx;
	txq->nb_tx_desc = q_info->nb_desc;
	tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
	tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
					 tx_desc, HNS3_RING_BASE_ALIGN,
					 q_info->socket_id);
	if (tx_mz == NULL) {
		hns3_err(hw, "Failed to reserve DMA memory for No.%u tx ring!",
			 q_info->idx);
		hns3_tx_queue_release(txq);
		return NULL;
	}
	txq->mz = tx_mz;
	txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
	txq->tx_ring_phys_addr = tx_mz->iova;

	hns3_dbg(hw, "No.%u tx descriptors iova 0x%" PRIx64, q_info->idx,
		 txq->tx_ring_phys_addr);

	/* Clear tx bd */
	desc = txq->tx_ring;
	for (i = 0; i < txq->nb_tx_desc; i++) {
		desc->tx.tp_fe_sc_vld_ra_ri = 0;
		desc++;
	}

	return txq;
}

static int
hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
			 uint16_t nb_desc, unsigned int socket_id)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_hw *hw = &hns->hw;
	struct hns3_queue_info q_info;
	struct hns3_tx_queue *txq;
	uint16_t nb_tx_q;

	if (hw->fkq_data.tx_queues[idx] != NULL) {
		hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
		hw->fkq_data.tx_queues[idx] = NULL;
	}

	q_info.idx = idx;
	q_info.socket_id = socket_id;
	q_info.nb_desc = nb_desc;
	q_info.type = "hns3 fake TX queue";
	q_info.ring_name = "tx_fake_ring";
	txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
	if (txq == NULL) {
		hns3_err(hw, "Failed to setup No.%u fake tx ring.", idx);
		return -ENOMEM;
	}

	/* Don't need alloc sw_ring, because upper applications don't use it */
	txq->sw_ring = NULL;
	txq->free = NULL;

	txq->hns = hns;
	txq->tx_deferred_start = false;
	txq->port_id = dev->data->port_id;
	txq->configured = true;
	nb_tx_q = dev->data->nb_tx_queues;
	txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
				(nb_tx_q + idx) * HNS3_TQP_REG_SIZE);

	rte_spinlock_lock(&hw->lock);
	hw->fkq_data.tx_queues[idx] = txq;
	rte_spinlock_unlock(&hw->lock);

	return 0;
}

static int
hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
{
	uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
	void **rxq;
	uint16_t i;

	if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
		/* first time configuration */
		uint32_t size;
		size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
		hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
						     RTE_CACHE_LINE_SIZE);
		if (hw->fkq_data.rx_queues == NULL) {
			hw->fkq_data.nb_fake_rx_queues = 0;
			return -ENOMEM;
		}
	} else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
		/* re-configure */
		rxq = hw->fkq_data.rx_queues;
		for (i = nb_queues; i < old_nb_queues; i++)
			hns3_rx_queue_release_lock(rxq[i]);

		rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
				  RTE_CACHE_LINE_SIZE);
		if (rxq == NULL)
			return -ENOMEM;
		if (nb_queues > old_nb_queues) {
			uint16_t new_qs = nb_queues - old_nb_queues;
			memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
		}

		hw->fkq_data.rx_queues = rxq;
	} else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
		rxq = hw->fkq_data.rx_queues;
		for (i = nb_queues; i < old_nb_queues; i++)
			hns3_rx_queue_release_lock(rxq[i]);

		rte_free(hw->fkq_data.rx_queues);
		hw->fkq_data.rx_queues = NULL;
	}

	hw->fkq_data.nb_fake_rx_queues = nb_queues;

	return 0;
}

static int
hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
{
	uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
	void **txq;
	uint16_t i;

	if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
		/* first time configuration */
		uint32_t size;
		size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
		hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
						     RTE_CACHE_LINE_SIZE);
		if (hw->fkq_data.tx_queues == NULL) {
			hw->fkq_data.nb_fake_tx_queues = 0;
			return -ENOMEM;
		}
	} else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
		/* re-configure */
		txq = hw->fkq_data.tx_queues;
		for (i = nb_queues; i < old_nb_queues; i++)
			hns3_tx_queue_release_lock(txq[i]);
		txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
				  RTE_CACHE_LINE_SIZE);
		if (txq == NULL)
			return -ENOMEM;
		if (nb_queues > old_nb_queues) {
			uint16_t new_qs = nb_queues - old_nb_queues;
			memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
		}

		hw->fkq_data.tx_queues = txq;
	} else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
		txq = hw->fkq_data.tx_queues;
		for (i = nb_queues; i < old_nb_queues; i++)
			hns3_tx_queue_release_lock(txq[i]);

		rte_free(hw->fkq_data.tx_queues);
		hw->fkq_data.tx_queues = NULL;
	}
	hw->fkq_data.nb_fake_tx_queues = nb_queues;

	return 0;
}

int
hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
			      uint16_t nb_tx_q)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	uint16_t rx_need_add_nb_q;
	uint16_t tx_need_add_nb_q;
	uint16_t port_id;
	uint16_t q;
	int ret;

	if (hns3_dev_get_support(hw, INDEP_TXRX))
		return 0;

	/* Setup new number of fake RX/TX queues and reconfigure device. */
	rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
	tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
	ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
	if (ret) {
		hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
		return ret;
	}

	ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
	if (ret) {
		hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
		goto cfg_fake_tx_q_fail;
	}

	/* Allocate and set up fake RX queue per Ethernet port. */
	port_id = hw->data->port_id;
	for (q = 0; q < rx_need_add_nb_q; q++) {
		ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
					       rte_eth_dev_socket_id(port_id));
		if (ret)
			goto setup_fake_rx_q_fail;
	}

	/* Allocate and set up fake TX queue per Ethernet port. */
	for (q = 0; q < tx_need_add_nb_q; q++) {
		ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
					       rte_eth_dev_socket_id(port_id));
		if (ret)
			goto setup_fake_tx_q_fail;
	}

	return 0;

setup_fake_tx_q_fail:
setup_fake_rx_q_fail:
	(void)hns3_fake_tx_queue_config(hw, 0);
cfg_fake_tx_q_fail:
	(void)hns3_fake_rx_queue_config(hw, 0);

	return ret;
}

void
hns3_dev_release_mbufs(struct hns3_adapter *hns)
{
	struct rte_eth_dev_data *dev_data = hns->hw.data;
	struct hns3_rx_queue *rxq;
	struct hns3_tx_queue *txq;
	int i;

	if (dev_data->rx_queues)
		for (i = 0; i < dev_data->nb_rx_queues; i++) {
			rxq = dev_data->rx_queues[i];
			if (rxq == NULL)
				continue;
			hns3_rx_queue_release_mbufs(rxq);
		}

	if (dev_data->tx_queues)
		for (i = 0; i < dev_data->nb_tx_queues; i++) {
			txq = dev_data->tx_queues[i];
			if (txq == NULL)
				continue;
			hns3_tx_queue_release_mbufs(txq);
		}
}

static int
hns3_rx_buf_len_calc(struct rte_mempool *mp, uint16_t *rx_buf_len)
{
	uint16_t vld_buf_size;
	uint16_t num_hw_specs;
	uint16_t i;

	/*
	 * hns3 network engine only support to set 4 typical specification, and
	 * different buffer size will affect the max packet_len and the max
	 * number of segmentation when hw gro is turned on in receive side. The
	 * relationship between them is as follows:
	 *      rx_buf_size     |  max_gro_pkt_len  |  max_gro_nb_seg
	 * ---------------------|-------------------|----------------
	 * HNS3_4K_BD_BUF_SIZE  |        60KB       |       15
	 * HNS3_2K_BD_BUF_SIZE  |        62KB       |       31
	 * HNS3_1K_BD_BUF_SIZE  |        63KB       |       63
	 * HNS3_512_BD_BUF_SIZE |      31.5KB       |       63
	 */
	static const uint16_t hw_rx_buf_size[] = {
		HNS3_4K_BD_BUF_SIZE,
		HNS3_2K_BD_BUF_SIZE,
		HNS3_1K_BD_BUF_SIZE,
		HNS3_512_BD_BUF_SIZE
	};

	vld_buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
			RTE_PKTMBUF_HEADROOM);
	if (vld_buf_size < HNS3_MIN_BD_BUF_SIZE)
		return -EINVAL;

	num_hw_specs = RTE_DIM(hw_rx_buf_size);
	for (i = 0; i < num_hw_specs; i++) {
		if (vld_buf_size >= hw_rx_buf_size[i]) {
			*rx_buf_len = hw_rx_buf_size[i];
			break;
		}
	}
	return 0;
}

static int
hns3_rxq_conf_runtime_check(struct hns3_hw *hw, uint16_t buf_size,
				uint16_t nb_desc)
{
	struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
	eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
	uint32_t frame_size = dev->data->mtu + HNS3_ETH_OVERHEAD;
	uint16_t min_vec_bds;

	/*
	 * HNS3 hardware network engine set scattered as default. If the driver
	 * is not work in scattered mode and the pkts greater than buf_size
	 * but smaller than frame size will be distributed to multiple BDs.
	 * Driver cannot handle this situation.
	 */
	if (!hw->data->scattered_rx && frame_size > buf_size) {
		hns3_err(hw, "frame size is not allowed to be set greater "
			     "than rx_buf_len if scattered is off.");
		return -EINVAL;
	}

	if (pkt_burst == hns3_recv_pkts_vec) {
		min_vec_bds = HNS3_DEFAULT_RXQ_REARM_THRESH +
			      HNS3_DEFAULT_RX_BURST;
		if (nb_desc < min_vec_bds ||
		    nb_desc % HNS3_DEFAULT_RXQ_REARM_THRESH) {
			hns3_err(hw, "if Rx burst mode is vector, "
				 "number of descriptor is required to be "
				 "bigger than min vector bds:%u, and could be "
				 "divided by rxq rearm thresh:%u.",
				 min_vec_bds, HNS3_DEFAULT_RXQ_REARM_THRESH);
			return -EINVAL;
		}
	}
	return 0;
}

static int
hns3_rx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_rxconf *conf,
			 struct rte_mempool *mp, uint16_t nb_desc,
			 uint16_t *buf_size)
{
	int ret;

	if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
	    nb_desc % HNS3_ALIGN_RING_DESC) {
		hns3_err(hw, "Number (%u) of rx descriptors is invalid",
			 nb_desc);
		return -EINVAL;
	}

	if (conf->rx_drop_en == 0)
		hns3_warn(hw, "if no descriptors available, packets are always "
			  "dropped and rx_drop_en (1) is fixed on");

	if (hns3_rx_buf_len_calc(mp, buf_size)) {
		hns3_err(hw, "rxq mbufs' data room size (%u) is not enough! "
				"minimal data room size (%u).",
				rte_pktmbuf_data_room_size(mp),
				HNS3_MIN_BD_BUF_SIZE + RTE_PKTMBUF_HEADROOM);
		return -EINVAL;
	}

	if (hw->data->dev_started) {
		ret = hns3_rxq_conf_runtime_check(hw, *buf_size, nb_desc);
		if (ret) {
			hns3_err(hw, "Rx queue runtime setup fail.");
			return ret;
		}
	}

	return 0;
}

uint32_t
hns3_get_tqp_reg_offset(uint16_t queue_id)
{
	uint32_t reg_offset;

	/* Need an extend offset to config queue > 1024 */
	if (queue_id < HNS3_MIN_EXTEND_QUEUE_ID)
		reg_offset = HNS3_TQP_REG_OFFSET + queue_id * HNS3_TQP_REG_SIZE;
	else
		reg_offset = HNS3_TQP_REG_OFFSET + HNS3_TQP_EXT_REG_OFFSET +
			     (queue_id - HNS3_MIN_EXTEND_QUEUE_ID) *
			     HNS3_TQP_REG_SIZE;

	return reg_offset;
}

int
hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
		    unsigned int socket_id, const struct rte_eth_rxconf *conf,
		    struct rte_mempool *mp)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_hw *hw = &hns->hw;
	struct hns3_queue_info q_info;
	struct hns3_rx_queue *rxq;
	uint16_t rx_buf_size;
	int rx_entry_len;
	int ret;

	ret = hns3_rx_queue_conf_check(hw, conf, mp, nb_desc, &rx_buf_size);
	if (ret)
		return ret;

	if (dev->data->rx_queues[idx]) {
		hns3_rx_queue_release(dev->data->rx_queues[idx]);
		dev->data->rx_queues[idx] = NULL;
	}

	q_info.idx = idx;
	q_info.socket_id = socket_id;
	q_info.nb_desc = nb_desc;
	q_info.type = "hns3 RX queue";
	q_info.ring_name = "rx_ring";

	rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
	if (rxq == NULL) {
		hns3_err(hw,
			 "Failed to alloc mem and reserve DMA mem for rx ring!");
		return -ENOMEM;
	}

	rxq->hns = hns;
	rxq->ptype_tbl = &hns->ptype_tbl;
	rxq->mb_pool = mp;
	rxq->rx_free_thresh = (conf->rx_free_thresh > 0) ?
		conf->rx_free_thresh : HNS3_DEFAULT_RX_FREE_THRESH;

	rxq->rx_deferred_start = conf->rx_deferred_start;
	if (rxq->rx_deferred_start && !hns3_dev_get_support(hw, INDEP_TXRX)) {
		hns3_warn(hw, "deferred start is not supported.");
		rxq->rx_deferred_start = false;
	}

	rx_entry_len = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
			sizeof(struct hns3_entry);
	rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
					  RTE_CACHE_LINE_SIZE, socket_id);
	if (rxq->sw_ring == NULL) {
		hns3_err(hw, "Failed to allocate memory for rx sw ring!");
		hns3_rx_queue_release(rxq);
		return -ENOMEM;
	}

	rxq->next_to_use = 0;
	rxq->rx_free_hold = 0;
	rxq->rx_rearm_start = 0;
	rxq->rx_rearm_nb = 0;
	rxq->pkt_first_seg = NULL;
	rxq->pkt_last_seg = NULL;
	rxq->port_id = dev->data->port_id;
	/*
	 * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
	 * the pvid_sw_discard_en in the queue struct should not be changed,
	 * because PVID-related operations do not need to be processed by PMD.
	 * For hns3 VF device, whether it needs to process PVID depends
	 * on the configuration of PF kernel mode netdevice driver. And the
	 * related PF configuration is delivered through the mailbox and finally
	 * reflectd in port_base_vlan_cfg.
	 */
	if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
		rxq->pvid_sw_discard_en = hw->port_base_vlan_cfg.state ==
						HNS3_PORT_BASE_VLAN_ENABLE;
	else
		rxq->pvid_sw_discard_en = false;
	rxq->ptype_en = hns3_dev_get_support(hw, RXD_ADV_LAYOUT) ? true : false;
	rxq->configured = true;
	rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
				idx * HNS3_TQP_REG_SIZE);
	rxq->io_base = (void *)((char *)hw->io_base +
					hns3_get_tqp_reg_offset(idx));
	rxq->io_head_reg = (volatile void *)((char *)rxq->io_base +
			   HNS3_RING_RX_HEAD_REG);
	rxq->rx_buf_len = rx_buf_size;
	memset(&rxq->basic_stats, 0, sizeof(struct hns3_rx_basic_stats));
	memset(&rxq->err_stats, 0, sizeof(struct hns3_rx_bd_errors_stats));
	memset(&rxq->dfx_stats, 0, sizeof(struct hns3_rx_dfx_stats));

	/* CRC len set here is used for amending packet length */
	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
		rxq->crc_len = RTE_ETHER_CRC_LEN;
	else
		rxq->crc_len = 0;

	rxq->bulk_mbuf_num = 0;

	rte_spinlock_lock(&hw->lock);
	dev->data->rx_queues[idx] = rxq;
	rte_spinlock_unlock(&hw->lock);

	return 0;
}

void
hns3_rx_scattered_reset(struct rte_eth_dev *dev)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_hw *hw = &hns->hw;

	hw->rx_buf_len = 0;
	dev->data->scattered_rx = false;
}

void
hns3_rx_scattered_calc(struct rte_eth_dev *dev)
{
	struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_hw *hw = &hns->hw;
	struct hns3_rx_queue *rxq;
	uint32_t queue_id;

	if (dev->data->rx_queues == NULL)
		return;

	for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
		rxq = dev->data->rx_queues[queue_id];
		if (hw->rx_buf_len == 0)
			hw->rx_buf_len = rxq->rx_buf_len;
		else
			hw->rx_buf_len = RTE_MIN(hw->rx_buf_len,
						 rxq->rx_buf_len);
	}

	if (dev_conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_SCATTER ||
	    dev->data->mtu + HNS3_ETH_OVERHEAD > hw->rx_buf_len)
		dev->data->scattered_rx = true;
}

const uint32_t *
hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
{
	static const uint32_t ptypes[] = {
		RTE_PTYPE_L2_ETHER,
		RTE_PTYPE_L2_ETHER_LLDP,
		RTE_PTYPE_L2_ETHER_ARP,
		RTE_PTYPE_L3_IPV4,
		RTE_PTYPE_L3_IPV4_EXT,
		RTE_PTYPE_L3_IPV6,
		RTE_PTYPE_L3_IPV6_EXT,
		RTE_PTYPE_L4_IGMP,
		RTE_PTYPE_L4_ICMP,
		RTE_PTYPE_L4_SCTP,
		RTE_PTYPE_L4_TCP,
		RTE_PTYPE_L4_UDP,
		RTE_PTYPE_TUNNEL_GRE,
		RTE_PTYPE_INNER_L2_ETHER,
		RTE_PTYPE_INNER_L3_IPV4,
		RTE_PTYPE_INNER_L3_IPV6,
		RTE_PTYPE_INNER_L3_IPV4_EXT,
		RTE_PTYPE_INNER_L3_IPV6_EXT,
		RTE_PTYPE_INNER_L4_UDP,
		RTE_PTYPE_INNER_L4_TCP,
		RTE_PTYPE_INNER_L4_SCTP,
		RTE_PTYPE_INNER_L4_ICMP,
		RTE_PTYPE_TUNNEL_VXLAN,
		RTE_PTYPE_TUNNEL_NVGRE,
		RTE_PTYPE_UNKNOWN
	};
	static const uint32_t adv_layout_ptypes[] = {
		RTE_PTYPE_L2_ETHER,
		RTE_PTYPE_L2_ETHER_TIMESYNC,
		RTE_PTYPE_L2_ETHER_LLDP,
		RTE_PTYPE_L2_ETHER_ARP,
		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
		RTE_PTYPE_L4_FRAG,
		RTE_PTYPE_L4_NONFRAG,
		RTE_PTYPE_L4_UDP,
		RTE_PTYPE_L4_TCP,
		RTE_PTYPE_L4_SCTP,
		RTE_PTYPE_L4_IGMP,
		RTE_PTYPE_L4_ICMP,
		RTE_PTYPE_TUNNEL_GRE,
		RTE_PTYPE_TUNNEL_GRENAT,
		RTE_PTYPE_INNER_L2_ETHER,
		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
		RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
		RTE_PTYPE_INNER_L4_FRAG,
		RTE_PTYPE_INNER_L4_ICMP,
		RTE_PTYPE_INNER_L4_NONFRAG,
		RTE_PTYPE_INNER_L4_UDP,
		RTE_PTYPE_INNER_L4_TCP,
		RTE_PTYPE_INNER_L4_SCTP,
		RTE_PTYPE_INNER_L4_ICMP,
		RTE_PTYPE_UNKNOWN
	};
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);

	if (dev->rx_pkt_burst == hns3_recv_pkts_simple ||
	    dev->rx_pkt_burst == hns3_recv_scattered_pkts ||
	    dev->rx_pkt_burst == hns3_recv_pkts_vec ||
	    dev->rx_pkt_burst == hns3_recv_pkts_vec_sve) {
		if (hns3_dev_get_support(hw, RXD_ADV_LAYOUT))
			return adv_layout_ptypes;
		else
			return ptypes;
	}

	return NULL;
}

static void
hns3_init_non_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
{
	tbl->l3table[0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
	tbl->l3table[1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
	tbl->l3table[2] = RTE_PTYPE_L2_ETHER_ARP;
	tbl->l3table[4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT;
	tbl->l3table[5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT;
	tbl->l3table[6] = RTE_PTYPE_L2_ETHER_LLDP;

	tbl->l4table[0] = RTE_PTYPE_L4_UDP;
	tbl->l4table[1] = RTE_PTYPE_L4_TCP;
	tbl->l4table[2] = RTE_PTYPE_TUNNEL_GRE;
	tbl->l4table[3] = RTE_PTYPE_L4_SCTP;
	tbl->l4table[4] = RTE_PTYPE_L4_IGMP;
	tbl->l4table[5] = RTE_PTYPE_L4_ICMP;
}

static void
hns3_init_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
{
	tbl->inner_l3table[0] = RTE_PTYPE_INNER_L2_ETHER |
				RTE_PTYPE_INNER_L3_IPV4;
	tbl->inner_l3table[1] = RTE_PTYPE_INNER_L2_ETHER |
				RTE_PTYPE_INNER_L3_IPV6;
	/* There is not a ptype for inner ARP/RARP */
	tbl->inner_l3table[2] = RTE_PTYPE_UNKNOWN;
	tbl->inner_l3table[3] = RTE_PTYPE_UNKNOWN;
	tbl->inner_l3table[4] = RTE_PTYPE_INNER_L2_ETHER |
				RTE_PTYPE_INNER_L3_IPV4_EXT;
	tbl->inner_l3table[5] = RTE_PTYPE_INNER_L2_ETHER |
				RTE_PTYPE_INNER_L3_IPV6_EXT;

	tbl->inner_l4table[0] = RTE_PTYPE_INNER_L4_UDP;
	tbl->inner_l4table[1] = RTE_PTYPE_INNER_L4_TCP;
	/* There is not a ptype for inner GRE */
	tbl->inner_l4table[2] = RTE_PTYPE_UNKNOWN;
	tbl->inner_l4table[3] = RTE_PTYPE_INNER_L4_SCTP;
	/* There is not a ptype for inner IGMP */
	tbl->inner_l4table[4] = RTE_PTYPE_UNKNOWN;
	tbl->inner_l4table[5] = RTE_PTYPE_INNER_L4_ICMP;

	tbl->ol3table[0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
	tbl->ol3table[1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
	tbl->ol3table[2] = RTE_PTYPE_UNKNOWN;
	tbl->ol3table[3] = RTE_PTYPE_UNKNOWN;
	tbl->ol3table[4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT;
	tbl->ol3table[5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT;

	tbl->ol4table[0] = RTE_PTYPE_UNKNOWN;
	tbl->ol4table[1] = RTE_PTYPE_L4_UDP | RTE_PTYPE_TUNNEL_VXLAN;
	tbl->ol4table[2] = RTE_PTYPE_TUNNEL_NVGRE;
}

static void
hns3_init_adv_layout_ptype(struct hns3_ptype_table *tbl)
{
	uint32_t *ptype = tbl->ptype;

	/* Non-tunnel L2 */
	ptype[1] = RTE_PTYPE_L2_ETHER_ARP;
	ptype[3] = RTE_PTYPE_L2_ETHER_LLDP;
	ptype[8] = RTE_PTYPE_L2_ETHER_TIMESYNC;

	/* Non-tunnel IPv4 */
	ptype[17] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_FRAG;
	ptype[18] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_NONFRAG;
	ptype[19] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_UDP;
	ptype[20] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_TCP;
	ptype[21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRE;
	ptype[22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_SCTP;
	ptype[23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_IGMP;
	ptype[24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_ICMP;
	/* The next ptype is PTP over IPv4 + UDP */
	ptype[25] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_L4_UDP;

	/* IPv4 --> GRE/Teredo/VXLAN */
	ptype[29] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT;
	/* IPv4 --> GRE/Teredo/VXLAN --> MAC */
	ptype[30] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;

	/* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
	ptype[31] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_FRAG;
	ptype[32] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_NONFRAG;
	ptype[33] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_UDP;
	ptype[34] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_TCP;
	ptype[35] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_SCTP;
	/* The next ptype's inner L4 is IGMP */
	ptype[36] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
	ptype[37] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_ICMP;

	/* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
	ptype[39] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_FRAG;
	ptype[40] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_NONFRAG;
	ptype[41] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_UDP;
	ptype[42] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_TCP;
	ptype[43] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_SCTP;
	/* The next ptype's inner L4 is IGMP */
	ptype[44] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
	ptype[45] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
		    RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		    RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		    RTE_PTYPE_INNER_L4_ICMP;

	/* Non-tunnel IPv6 */
	ptype[111] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_FRAG;
	ptype[112] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_NONFRAG;
	ptype[113] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_UDP;
	ptype[114] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_TCP;
	ptype[115] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRE;
	ptype[116] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_SCTP;
	ptype[117] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_IGMP;
	ptype[118] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_ICMP;
	/* Special for PTP over IPv6 + UDP */
	ptype[119] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_L4_UDP;

	/* IPv6 --> GRE/Teredo/VXLAN */
	ptype[123] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT;
	/* IPv6 --> GRE/Teredo/VXLAN --> MAC */
	ptype[124] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;

	/* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
	ptype[125] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_FRAG;
	ptype[126] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_NONFRAG;
	ptype[127] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_UDP;
	ptype[128] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_TCP;
	ptype[129] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_SCTP;
	/* The next ptype's inner L4 is IGMP */
	ptype[130] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
	ptype[131] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_ICMP;

	/* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
	ptype[133] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_FRAG;
	ptype[134] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_NONFRAG;
	ptype[135] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_UDP;
	ptype[136] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_TCP;
	ptype[137] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_SCTP;
	/* The next ptype's inner L4 is IGMP */
	ptype[138] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
	ptype[139] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
		     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
		     RTE_PTYPE_INNER_L4_ICMP;
}

void
hns3_init_rx_ptype_tble(struct rte_eth_dev *dev)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	struct hns3_ptype_table *tbl = &hns->ptype_tbl;

	memset(tbl, 0, sizeof(*tbl));

	hns3_init_non_tunnel_ptype_tbl(tbl);
	hns3_init_tunnel_ptype_tbl(tbl);
	hns3_init_adv_layout_ptype(tbl);
}

static inline void
hns3_rxd_to_vlan_tci(struct hns3_rx_queue *rxq, struct rte_mbuf *mb,
		     uint32_t l234_info, const struct hns3_desc *rxd)
{
#define HNS3_STRP_STATUS_NUM		0x4

#define HNS3_NO_STRP_VLAN_VLD		0x0
#define HNS3_INNER_STRP_VLAN_VLD	0x1
#define HNS3_OUTER_STRP_VLAN_VLD	0x2
	uint32_t strip_status;
	uint32_t report_mode;

	/*
	 * Since HW limitation, the vlan tag will always be inserted into RX
	 * descriptor when strip the tag from packet, driver needs to determine
	 * reporting which tag to mbuf according to the PVID configuration
	 * and vlan striped status.
	 */
	static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
		{
			HNS3_NO_STRP_VLAN_VLD,
			HNS3_OUTER_STRP_VLAN_VLD,
			HNS3_INNER_STRP_VLAN_VLD,
			HNS3_OUTER_STRP_VLAN_VLD
		},
		{
			HNS3_NO_STRP_VLAN_VLD,
			HNS3_NO_STRP_VLAN_VLD,
			HNS3_NO_STRP_VLAN_VLD,
			HNS3_INNER_STRP_VLAN_VLD
		}
	};
	strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
				      HNS3_RXD_STRP_TAGP_S);
	report_mode = report_type[rxq->pvid_sw_discard_en][strip_status];
	switch (report_mode) {
	case HNS3_NO_STRP_VLAN_VLD:
		mb->vlan_tci = 0;
		return;
	case HNS3_INNER_STRP_VLAN_VLD:
		mb->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
		mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
		return;
	case HNS3_OUTER_STRP_VLAN_VLD:
		mb->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
		mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
		return;
	default:
		mb->vlan_tci = 0;
		return;
	}
}

static inline void
recalculate_data_len(struct rte_mbuf *first_seg, struct rte_mbuf *last_seg,
		    struct rte_mbuf *rxm, struct hns3_rx_queue *rxq,
		    uint16_t data_len)
{
	uint8_t crc_len = rxq->crc_len;

	if (data_len <= crc_len) {
		rte_pktmbuf_free_seg(rxm);
		first_seg->nb_segs--;
		last_seg->data_len = (uint16_t)(last_seg->data_len -
			(crc_len - data_len));
		last_seg->next = NULL;
	} else
		rxm->data_len = (uint16_t)(data_len - crc_len);
}

static inline struct rte_mbuf *
hns3_rx_alloc_buffer(struct hns3_rx_queue *rxq)
{
	int ret;

	if (likely(rxq->bulk_mbuf_num > 0))
		return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];

	ret = rte_mempool_get_bulk(rxq->mb_pool, (void **)rxq->bulk_mbuf,
				   HNS3_BULK_ALLOC_MBUF_NUM);
	if (likely(ret == 0)) {
		rxq->bulk_mbuf_num = HNS3_BULK_ALLOC_MBUF_NUM;
		return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
	} else
		return rte_mbuf_raw_alloc(rxq->mb_pool);
}

static inline void
hns3_rx_ptp_timestamp_handle(struct hns3_rx_queue *rxq, struct rte_mbuf *mbuf,
		  volatile struct hns3_desc *rxd)
{
	struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(rxq->hns);
	uint64_t timestamp = rte_le_to_cpu_64(rxd->timestamp);

	mbuf->ol_flags |= RTE_MBUF_F_RX_IEEE1588_PTP | RTE_MBUF_F_RX_IEEE1588_TMST;
	if (hns3_timestamp_rx_dynflag > 0) {
		*RTE_MBUF_DYNFIELD(mbuf, hns3_timestamp_dynfield_offset,
			rte_mbuf_timestamp_t *) = timestamp;
		mbuf->ol_flags |= hns3_timestamp_rx_dynflag;
	}

	pf->rx_timestamp = timestamp;
}

uint16_t
hns3_recv_pkts_simple(void *rx_queue,
		      struct rte_mbuf **rx_pkts,
		      uint16_t nb_pkts)
{
	volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
	volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
	struct hns3_rx_queue *rxq;      /* RX queue */
	struct hns3_entry *sw_ring;
	struct hns3_entry *rxe;
	struct hns3_desc rxd;
	struct rte_mbuf *nmb;           /* pointer of the new mbuf */
	struct rte_mbuf *rxm;
	uint32_t bd_base_info;
	uint32_t l234_info;
	uint32_t ol_info;
	uint64_t dma_addr;
	uint16_t nb_rx_bd;
	uint16_t nb_rx;
	uint16_t rx_id;
	int ret;

	nb_rx = 0;
	nb_rx_bd = 0;
	rxq = rx_queue;
	rx_ring = rxq->rx_ring;
	sw_ring = rxq->sw_ring;
	rx_id = rxq->next_to_use;

	while (nb_rx < nb_pkts) {
		rxdp = &rx_ring[rx_id];
		bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
		if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
			break;

		rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
			   (1u << HNS3_RXD_VLD_B)];

		nmb = hns3_rx_alloc_buffer(rxq);
		if (unlikely(nmb == NULL)) {
			uint16_t port_id;

			port_id = rxq->port_id;
			rte_eth_devices[port_id].data->rx_mbuf_alloc_failed++;
			break;
		}

		nb_rx_bd++;
		rxe = &sw_ring[rx_id];
		rx_id++;
		if (unlikely(rx_id == rxq->nb_rx_desc))
			rx_id = 0;

		rte_prefetch0(sw_ring[rx_id].mbuf);
		if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
			rte_prefetch0(&rx_ring[rx_id]);
			rte_prefetch0(&sw_ring[rx_id]);
		}

		rxm = rxe->mbuf;
		rxm->ol_flags = 0;
		rxe->mbuf = nmb;

		if (unlikely(bd_base_info & BIT(HNS3_RXD_TS_VLD_B)))
			hns3_rx_ptp_timestamp_handle(rxq, rxm, rxdp);

		dma_addr = rte_mbuf_data_iova_default(nmb);
		rxdp->addr = rte_cpu_to_le_64(dma_addr);
		rxdp->rx.bd_base_info = 0;

		rxm->data_off = RTE_PKTMBUF_HEADROOM;
		rxm->pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len)) -
				rxq->crc_len;
		rxm->data_len = rxm->pkt_len;
		rxm->port = rxq->port_id;
		rxm->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
		rxm->ol_flags |= RTE_MBUF_F_RX_RSS_HASH;
		if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
			rxm->hash.fdir.hi =
				rte_le_to_cpu_16(rxd.rx.fd_id);
			rxm->ol_flags |= RTE_MBUF_F_RX_FDIR | RTE_MBUF_F_RX_FDIR_ID;
		}
		rxm->nb_segs = 1;
		rxm->next = NULL;

		/* Load remained descriptor data and extract necessary fields */
		l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
		ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
		ret = hns3_handle_bdinfo(rxq, rxm, bd_base_info, l234_info);
		if (unlikely(ret))
			goto pkt_err;

		rxm->packet_type = hns3_rx_calc_ptype(rxq, l234_info, ol_info);

		if (rxm->packet_type == RTE_PTYPE_L2_ETHER_TIMESYNC)
			rxm->ol_flags |= RTE_MBUF_F_RX_IEEE1588_PTP;

		hns3_rxd_to_vlan_tci(rxq, rxm, l234_info, &rxd);

		/* Increment bytes counter  */
		rxq->basic_stats.bytes += rxm->pkt_len;

		rx_pkts[nb_rx++] = rxm;
		continue;
pkt_err:
		rte_pktmbuf_free(rxm);
	}

	rxq->next_to_use = rx_id;
	rxq->rx_free_hold += nb_rx_bd;
	if (rxq->rx_free_hold > rxq->rx_free_thresh) {
		hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
		rxq->rx_free_hold = 0;
	}

	return nb_rx;
}

uint16_t
hns3_recv_scattered_pkts(void *rx_queue,
			 struct rte_mbuf **rx_pkts,
			 uint16_t nb_pkts)
{
	volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
	volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
	struct hns3_rx_queue *rxq;      /* RX queue */
	struct hns3_entry *sw_ring;
	struct hns3_entry *rxe;
	struct rte_mbuf *first_seg;
	struct rte_mbuf *last_seg;
	struct hns3_desc rxd;
	struct rte_mbuf *nmb;           /* pointer of the new mbuf */
	struct rte_mbuf *rxm;
	struct rte_eth_dev *dev;
	uint32_t bd_base_info;
	uint32_t l234_info;
	uint32_t gro_size;
	uint32_t ol_info;
	uint64_t dma_addr;
	uint16_t nb_rx_bd;
	uint16_t nb_rx;
	uint16_t rx_id;
	int ret;

	nb_rx = 0;
	nb_rx_bd = 0;
	rxq = rx_queue;

	rx_id = rxq->next_to_use;
	rx_ring = rxq->rx_ring;
	sw_ring = rxq->sw_ring;
	first_seg = rxq->pkt_first_seg;
	last_seg = rxq->pkt_last_seg;

	while (nb_rx < nb_pkts) {
		rxdp = &rx_ring[rx_id];
		bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
		if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
			break;

		/*
		 * The interactive process between software and hardware of
		 * receiving a new packet in hns3 network engine:
		 * 1. Hardware network engine firstly writes the packet content
		 *    to the memory pointed by the 'addr' field of the Rx Buffer
		 *    Descriptor, secondly fills the result of parsing the
		 *    packet include the valid field into the Rx Buffer
		 *    Descriptor in one write operation.
		 * 2. Driver reads the Rx BD's valid field in the loop to check
		 *    whether it's valid, if valid then assign a new address to
		 *    the addr field, clear the valid field, get the other
		 *    information of the packet by parsing Rx BD's other fields,
		 *    finally write back the number of Rx BDs processed by the
		 *    driver to the HNS3_RING_RX_HEAD_REG register to inform
		 *    hardware.
		 * In the above process, the ordering is very important. We must
		 * make sure that CPU read Rx BD's other fields only after the
		 * Rx BD is valid.
		 *
		 * There are two type of re-ordering: compiler re-ordering and
		 * CPU re-ordering under the ARMv8 architecture.
		 * 1. we use volatile to deal with compiler re-ordering, so you
		 *    can see that rx_ring/rxdp defined with volatile.
		 * 2. we commonly use memory barrier to deal with CPU
		 *    re-ordering, but the cost is high.
		 *
		 * In order to solve the high cost of using memory barrier, we
		 * use the data dependency order under the ARMv8 architecture,
		 * for example:
		 *      instr01: load A
		 *      instr02: load B <- A
		 * the instr02 will always execute after instr01.
		 *
		 * To construct the data dependency ordering, we use the
		 * following assignment:
		 *      rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
		 *                 (1u<<HNS3_RXD_VLD_B)]
		 * Using gcc compiler under the ARMv8 architecture, the related
		 * assembly code example as follows:
		 * note: (1u << HNS3_RXD_VLD_B) equal 0x10
		 *      instr01: ldr w26, [x22, #28]  --read bd_base_info
		 *      instr02: and w0, w26, #0x10   --calc bd_base_info & 0x10
		 *      instr03: sub w0, w0, #0x10    --calc (bd_base_info &
		 *                                            0x10) - 0x10
		 *      instr04: add x0, x22, x0, lsl #5 --calc copy source addr
		 *      instr05: ldp x2, x3, [x0]
		 *      instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
		 *      instr07: ldp x4, x5, [x0, #16]
		 *      instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
		 * the instr05~08 depend on x0's value, x0 depent on w26's
		 * value, the w26 is the bd_base_info, this form the data
		 * dependency ordering.
		 * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
		 *       (1u<<HNS3_RXD_VLD_B) will always zero, so the
		 *       assignment is correct.
		 *
		 * So we use the data dependency ordering instead of memory
		 * barrier to improve receive performance.
		 */
		rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
			   (1u << HNS3_RXD_VLD_B)];

		nmb = hns3_rx_alloc_buffer(rxq);
		if (unlikely(nmb == NULL)) {
			dev = &rte_eth_devices[rxq->port_id];
			dev->data->rx_mbuf_alloc_failed++;
			break;
		}

		nb_rx_bd++;
		rxe = &sw_ring[rx_id];
		rx_id++;
		if (unlikely(rx_id == rxq->nb_rx_desc))
			rx_id = 0;

		rte_prefetch0(sw_ring[rx_id].mbuf);
		if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
			rte_prefetch0(&rx_ring[rx_id]);
			rte_prefetch0(&sw_ring[rx_id]);
		}

		rxm = rxe->mbuf;
		rxe->mbuf = nmb;

		dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
		rxdp->rx.bd_base_info = 0;
		rxdp->addr = dma_addr;

		if (first_seg == NULL) {
			first_seg = rxm;
			first_seg->nb_segs = 1;
		} else {
			first_seg->nb_segs++;
			last_seg->next = rxm;
		}

		rxm->data_off = RTE_PKTMBUF_HEADROOM;
		rxm->data_len = rte_le_to_cpu_16(rxd.rx.size);

		if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
			last_seg = rxm;
			rxm->next = NULL;
			continue;
		}

		if (unlikely(bd_base_info & BIT(HNS3_RXD_TS_VLD_B)))
			hns3_rx_ptp_timestamp_handle(rxq, first_seg, rxdp);

		/*
		 * The last buffer of the received packet. packet len from
		 * buffer description may contains CRC len, packet len should
		 * subtract it, same as data len.
		 */
		first_seg->pkt_len = rte_le_to_cpu_16(rxd.rx.pkt_len);

		/*
		 * This is the last buffer of the received packet. If the CRC
		 * is not stripped by the hardware:
		 *  - Subtract the CRC length from the total packet length.
		 *  - If the last buffer only contains the whole CRC or a part
		 *  of it, free the mbuf associated to the last buffer. If part
		 *  of the CRC is also contained in the previous mbuf, subtract
		 *  the length of that CRC part from the data length of the
		 *  previous mbuf.
		 */
		rxm->next = NULL;
		if (unlikely(rxq->crc_len > 0)) {
			first_seg->pkt_len -= rxq->crc_len;
			recalculate_data_len(first_seg, last_seg, rxm, rxq,
				rxm->data_len);
		}

		first_seg->port = rxq->port_id;
		first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
		first_seg->ol_flags = RTE_MBUF_F_RX_RSS_HASH;
		if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
			first_seg->hash.fdir.hi =
				rte_le_to_cpu_16(rxd.rx.fd_id);
			first_seg->ol_flags |= RTE_MBUF_F_RX_FDIR | RTE_MBUF_F_RX_FDIR_ID;
		}

		gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
					  HNS3_RXD_GRO_SIZE_S);
		if (gro_size != 0) {
			first_seg->ol_flags |= RTE_MBUF_F_RX_LRO;
			first_seg->tso_segsz = gro_size;
		}

		l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
		ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
		ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
					 l234_info);
		if (unlikely(ret))
			goto pkt_err;

		first_seg->packet_type = hns3_rx_calc_ptype(rxq,
						l234_info, ol_info);

		if (first_seg->packet_type == RTE_PTYPE_L2_ETHER_TIMESYNC)
			rxm->ol_flags |= RTE_MBUF_F_RX_IEEE1588_PTP;

		hns3_rxd_to_vlan_tci(rxq, first_seg, l234_info, &rxd);

		/* Increment bytes counter */
		rxq->basic_stats.bytes += first_seg->pkt_len;

		rx_pkts[nb_rx++] = first_seg;
		first_seg = NULL;
		continue;
pkt_err:
		rte_pktmbuf_free(first_seg);
		first_seg = NULL;
	}

	rxq->next_to_use = rx_id;
	rxq->pkt_first_seg = first_seg;
	rxq->pkt_last_seg = last_seg;

	rxq->rx_free_hold += nb_rx_bd;
	if (rxq->rx_free_hold > rxq->rx_free_thresh) {
		hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
		rxq->rx_free_hold = 0;
	}

	return nb_rx;
}

void __rte_weak
hns3_rxq_vec_setup(__rte_unused struct hns3_rx_queue *rxq)
{
}

int __rte_weak
hns3_rx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
{
	return -ENOTSUP;
}

uint16_t __rte_weak
hns3_recv_pkts_vec(__rte_unused void *tx_queue,
		   __rte_unused struct rte_mbuf **rx_pkts,
		   __rte_unused uint16_t nb_pkts)
{
	return 0;
}

uint16_t __rte_weak
hns3_recv_pkts_vec_sve(__rte_unused void *tx_queue,
		       __rte_unused struct rte_mbuf **rx_pkts,
		       __rte_unused uint16_t nb_pkts)
{
	return 0;
}

int
hns3_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
		       struct rte_eth_burst_mode *mode)
{
	static const struct {
		eth_rx_burst_t pkt_burst;
		const char *info;
	} burst_infos[] = {
		{ hns3_recv_pkts_simple,	"Scalar Simple" },
		{ hns3_recv_scattered_pkts,	"Scalar Scattered" },
		{ hns3_recv_pkts_vec,		"Vector Neon"   },
		{ hns3_recv_pkts_vec_sve,	"Vector Sve"    },
	};

	eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
	int ret = -EINVAL;
	unsigned int i;

	for (i = 0; i < RTE_DIM(burst_infos); i++) {
		if (pkt_burst == burst_infos[i].pkt_burst) {
			snprintf(mode->info, sizeof(mode->info), "%s",
				 burst_infos[i].info);
			ret = 0;
			break;
		}
	}

	return ret;
}

static bool
hns3_get_default_vec_support(void)
{
#if defined(RTE_ARCH_ARM64)
	if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)
		return false;
	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
		return true;
#endif
	return false;
}

static bool
hns3_get_sve_support(void)
{
#if defined(RTE_HAS_SVE_ACLE)
	if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_256)
		return false;
	if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SVE))
		return true;
#endif
	return false;
}

static eth_rx_burst_t
hns3_get_rx_function(struct rte_eth_dev *dev)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	uint64_t offloads = dev->data->dev_conf.rxmode.offloads;
	bool vec_allowed, sve_allowed, simple_allowed;
	bool vec_support;

	vec_support = hns3_rx_check_vec_support(dev) == 0;
	vec_allowed = vec_support && hns3_get_default_vec_support();
	sve_allowed = vec_support && hns3_get_sve_support();
	simple_allowed = !dev->data->scattered_rx &&
			 (offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) == 0;

	if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_VEC && vec_allowed)
		return hns3_recv_pkts_vec;
	if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SVE && sve_allowed)
		return hns3_recv_pkts_vec_sve;
	if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SIMPLE && simple_allowed)
		return hns3_recv_pkts_simple;
	if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_COMMON)
		return hns3_recv_scattered_pkts;

	if (vec_allowed)
		return hns3_recv_pkts_vec;
	if (simple_allowed)
		return hns3_recv_pkts_simple;

	return hns3_recv_scattered_pkts;
}

static int
hns3_tx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_txconf *conf,
			 uint16_t nb_desc, uint16_t *tx_rs_thresh,
			 uint16_t *tx_free_thresh, uint16_t idx)
{
#define HNS3_TX_RS_FREE_THRESH_GAP	8
	uint16_t rs_thresh, free_thresh, fast_free_thresh;

	if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
	    nb_desc % HNS3_ALIGN_RING_DESC) {
		hns3_err(hw, "number (%u) of tx descriptors is invalid",
			 nb_desc);
		return -EINVAL;
	}

	rs_thresh = (conf->tx_rs_thresh > 0) ?
			conf->tx_rs_thresh : HNS3_DEFAULT_TX_RS_THRESH;
	free_thresh = (conf->tx_free_thresh > 0) ?
			conf->tx_free_thresh : HNS3_DEFAULT_TX_FREE_THRESH;
	if (rs_thresh + free_thresh > nb_desc || nb_desc % rs_thresh ||
	    rs_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP ||
	    free_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP) {
		hns3_err(hw, "tx_rs_thresh (%u) tx_free_thresh (%u) nb_desc "
			 "(%u) of tx descriptors for port=%u queue=%u check "
			 "fail!",
			 rs_thresh, free_thresh, nb_desc, hw->data->port_id,
			 idx);
		return -EINVAL;
	}

	if (conf->tx_free_thresh == 0) {
		/* Fast free Tx memory buffer to improve cache hit rate */
		fast_free_thresh = nb_desc - rs_thresh;
		if (fast_free_thresh >=
		    HNS3_TX_FAST_FREE_AHEAD + HNS3_DEFAULT_TX_FREE_THRESH)
			free_thresh = fast_free_thresh -
					HNS3_TX_FAST_FREE_AHEAD;
	}

	*tx_rs_thresh = rs_thresh;
	*tx_free_thresh = free_thresh;
	return 0;
}

static void *
hns3_tx_push_get_queue_tail_reg(struct rte_eth_dev *dev, uint16_t queue_id)
{
#define HNS3_TX_PUSH_TQP_REGION_SIZE		0x10000
#define HNS3_TX_PUSH_QUICK_DOORBELL_OFFSET	64
#define HNS3_TX_PUSH_PCI_BAR_INDEX		4

	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
	uint8_t bar_id = HNS3_TX_PUSH_PCI_BAR_INDEX;

	/*
	 * If device support Tx push then its PCIe bar45 must exist, and DPDK
	 * framework will mmap the bar45 default in PCI probe stage.
	 *
	 * In the bar45, the first half is for RoCE (RDMA over Converged
	 * Ethernet), and the second half is for NIC, every TQP occupy 64KB.
	 *
	 * The quick doorbell located at 64B offset in the TQP region.
	 */
	return (char *)pci_dev->mem_resource[bar_id].addr +
			(pci_dev->mem_resource[bar_id].len >> 1) +
			HNS3_TX_PUSH_TQP_REGION_SIZE * queue_id +
			HNS3_TX_PUSH_QUICK_DOORBELL_OFFSET;
}

void
hns3_tx_push_init(struct rte_eth_dev *dev)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	volatile uint32_t *reg;
	uint32_t val;

	if (!hns3_dev_get_support(hw, TX_PUSH))
		return;

	reg = (volatile uint32_t *)hns3_tx_push_get_queue_tail_reg(dev, 0);
	/*
	 * Because the size of bar45 is about 8GB size, it may take a long time
	 * to do the page fault in Tx process when work with vfio-pci, so use
	 * one read operation to make kernel setup page table mapping for bar45
	 * in the init stage.
	 * Note: the bar45 is readable but the result is all 1.
	 */
	val = *reg;
	RTE_SET_USED(val);
}

static void
hns3_tx_push_queue_init(struct rte_eth_dev *dev,
			uint16_t queue_id,
			struct hns3_tx_queue *txq)
{
	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
	if (!hns3_dev_get_support(hw, TX_PUSH)) {
		txq->tx_push_enable = false;
		return;
	}

	txq->io_tail_reg = (volatile void *)hns3_tx_push_get_queue_tail_reg(dev,
						queue_id);
	txq->tx_push_enable = true;
}

int
hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
		    unsigned int socket_id, const struct rte_eth_txconf *conf)
{
	struct hns3_adapter *hns = dev->data->dev_private;
	uint16_t tx_rs_thresh, tx_free_thresh;
	struct hns3_hw *hw = &hns->hw;
	struct hns3_queue_info q_info;
	struct hns3_tx_queue *txq;
	int tx_entry_len;
	int ret;

	ret = hns3_tx_queue_conf_check(hw, conf, nb_desc,
				       &tx_rs_thresh, &tx_free_thresh, idx);
	if (ret)
		return ret;

	if (dev->data->tx_queues[idx] != NULL) {
		hns3_tx_queue_release(dev->data->tx_queues[idx]);
		dev->data->tx_queues[idx] = NULL;
	}

	q_info.idx = idx;
	q_info.socket_id = socket_id;
	q_info.nb_desc = nb_desc;
	q_info.type = "hns3 TX queue";
	q_info.ring_name = "tx_ring";
	txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
	if (txq == NULL) {
		hns3_err(hw,
			 "Failed to alloc mem and reserve DMA mem for tx ring!");
		return -ENOMEM;
	}

	txq->tx_deferred_start = conf->tx_deferred_start;
	if (txq->tx_deferred_start && !hns3_dev_get_support(hw, INDEP_TXRX)) {
		hns3_warn(hw, "deferred start is not supported.");
		txq->tx_deferred_start = false;
	}

	tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
	txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
					  RTE_CACHE_LINE_SIZE, socket_id);
	if (txq->sw_ring == NULL) {
		hns3_err(hw, "Failed to allocate memory for tx sw ring!");
		hns3_tx_queue_release(txq);
		return -ENOMEM;
	}

	txq->hns = hns;
	txq->next_to_use = 0;
	txq->next_to_clean = 0;
	txq->tx_bd_ready = txq->nb_tx_desc - 1;
	txq->tx_free_thresh = tx_free_thresh;
	txq->tx_rs_thresh = tx_rs_thresh;
	txq->free = rte_zmalloc_socket("hns3 TX mbuf free array",
				sizeof(struct rte_mbuf *) * txq->tx_rs_thresh,
				RTE_CACHE_LINE_SIZE, socket_id);
	if (!txq->free) {
		hns3_err(hw, "failed to allocate tx mbuf free array!");
		hns3_tx_queue_release(txq);
		return -ENOMEM;
	}

	txq->port_id = dev->data->port_id;
	/*
	 * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
	 * the pvid_sw_shift_en in the queue struct should not be changed,
	 * because PVID-related operations do not need to be processed by PMD.
	 * For hns3 VF device, whether it needs to process PVID depends
	 * on the configuration of PF kernel mode netdev driver. And the
	 * related PF configuration is delivered through the mailbox and finally
	 * reflectd in port_base_vlan_cfg.
	 */
	if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
		txq->pvid_sw_shift_en = hw->port_base_vlan_cfg.state ==
					HNS3_PORT_BASE_VLAN_ENABLE;
	else
		txq->pvid_sw_shift_en = false;
	txq->max_non_tso_bd_num = hw->max_non_tso_bd_num;
	txq->configured = true;
	txq->io_base = (void *)((char *)hw->io_base +
						hns3_get_tqp_reg_offset(idx));
	txq->io_tail_reg = (volatile void *)((char *)txq->io_base +
					     HNS3_RING_TX_TAIL_REG);
	txq->min_tx_pkt_len = hw->min_tx_pkt_len;
	txq->tso_mode = hw->tso_mode;
	txq->udp_cksum_mode = hw->udp_cksum_mode;
	txq->mbuf_fast_free_en = !!(dev->data->dev_conf.txmode.offloads &
				    RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE);
	memset(&txq->basic_stats, 0, sizeof(struct hns3_tx_basic_stats));
	memset(&txq->dfx_stats, 0, sizeof(struct hns3_tx_dfx_stats));

	/*
	 * Call hns3_tx_push_queue_init after assigned io_tail_reg field because
	 * it may overwrite the io_tail_reg field.
	 */
	hns3_tx_push_queue_init(dev, idx, txq);

	rte_spinlock_lock(&hw->lock);
	dev->data->tx_queues[idx] = txq;
	rte_spinlock_unlock(&hw->lock);

	return 0;
}

static int
hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
{
	uint16_t tx_next_clean = txq->next_to_clean;
	uint16_t tx_next_use = txq->next_to_use;
	struct hns3_entry *tx_entry = &txq->sw_ring[tx_next_clean];
	struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
	int i;

	if (tx_next_use >= tx_next_clean &&
	    tx_next_use < tx_next_clean + txq->tx_rs_thresh)
		return -1;

	/*
	 * All mbufs can be released only when the VLD bits of all
	 * descriptors in a batch are cleared.
	 */
	for (i = 0; i < txq->tx_rs_thresh; i++) {
		if (desc[i].tx.tp_fe_sc_vld_ra_ri &
			rte_le_to_cpu_16(BIT(HNS3_TXD_VLD_B)))
			return -1;
	}

	for (i = 0; i < txq->tx_rs_thresh; i++) {
		rte_pktmbuf_free_seg(tx_entry[i].mbuf);
		tx_entry[i].mbuf = NULL;
	}

	/* Update numbers of available descriptor due to buffer freed */
	txq->tx_bd_ready += txq->tx_rs_thresh;
	txq->next_to_clean += txq->tx_rs_thresh;
	if (txq->next_to_clean >= txq->nb_tx_desc)
		txq->next_to_clean = 0;

	return 0;
}

static inline int
hns3_tx_free_required_buffer(struct hns3_tx_queue *txq, uint16_t required_bds)
{
	while (required_bds > txq->tx_bd_ready) {
		if (hns3_tx_free_useless_buffer(txq) != 0)
			return -1;
	}
	return 0;
}

int
hns3_config_gro(struct hns3_hw *hw, bool en)
{
	struct hns3_cfg_gro_status_cmd *req;
	struct hns3_cmd_desc desc;
	int ret;

	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
	req = (struct hns3_cfg_gro_status_cmd *)desc.data;

	req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);

	ret = hns3_cmd_send(hw, &desc, 1);
	if (ret)
		hns3_err(hw, "%s hardware GRO failed, ret = %d",
			 en ? "enable" : "disable", ret);

	return ret;
}

int
hns3_restore_gro_conf(struct hns3_hw *hw)
{
	uint64_t offloads;
	bool gro_en;
	int ret;

	offloads = hw->data->dev_conf.rxmode.offloads;
	gro_en = offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false;
	ret = hns3_config_gro(hw, gro_en);
	if (ret)
		hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
			 gro_en ? "enabled" : "disabled", ret);

	return ret;
}

static inline bool
hns3_pkt_is_tso(struct rte_mbuf *m)
{
	return (m->tso_segsz != 0 && m->ol_flags & RTE_MBUF_F_TX_TCP_SEG);
}

static void
hns3_set_tso(struct hns3_desc *desc, uint32_t paylen, struct rte_mbuf *rxm)
{
	if (!hns3_pkt_is_tso(rxm))
		return;

	if (paylen <= rxm->tso_segsz)
		return;

	desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(BIT(HNS3_TXD_TSO_B));
	desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
}

static inline void
hns3_fill_per_desc(struct hns3_desc *desc, struct rte_mbuf *rxm)
{
	desc->addr = rte_mbuf_data_iova(rxm);
	desc->tx.send_size = rte_cpu_to_le_16(rte_pktmbuf_data_len(rxm));
	desc->tx.tp_fe_sc_vld_ra_ri |= rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B));
}

static void
hns3_fill_first_desc(struct hns3_tx_queue *txq, struct hns3_desc *desc,
		     struct rte_mbuf *rxm)
{
	uint64_t ol_flags = rxm->ol_flags;
	uint32_t hdr_len;
	uint32_t paylen;

	hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
	hdr_len += (ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) ?
			   rxm->outer_l2_len + rxm->outer_l3_len : 0;
	paylen = rxm->pkt_len - hdr_len;
	desc->tx.paylen_fd_dop_ol4cs |= rte_cpu_to_le_32(paylen);
	hns3_set_tso(desc, paylen, rxm);

	/*
	 * Currently, hardware doesn't support more than two layers VLAN offload
	 * in Tx direction based on hns3 network engine. So when the number of
	 * VLANs in the packets represented by rxm plus the number of VLAN
	 * offload by hardware such as PVID etc, exceeds two, the packets will
	 * be discarded or the original VLAN of the packets will be overwitted
	 * by hardware. When the PF PVID is enabled by calling the API function
	 * named rte_eth_dev_set_vlan_pvid or the VF PVID is enabled by the hns3
	 * PF kernel ether driver, the outer VLAN tag will always be the PVID.
	 * To avoid the VLAN of Tx descriptor is overwritten by PVID, it should
	 * be added to the position close to the IP header when PVID is enabled.
	 */
	if (!txq->pvid_sw_shift_en &&
	    ol_flags & (RTE_MBUF_F_TX_VLAN | RTE_MBUF_F_TX_QINQ)) {
		desc->tx.ol_type_vlan_len_msec |=
				rte_cpu_to_le_32(BIT(HNS3_TXD_OVLAN_B));
		if (ol_flags & RTE_MBUF_F_TX_QINQ)
			desc->tx.outer_vlan_tag =
					rte_cpu_to_le_16(rxm->vlan_tci_outer);
		else
			desc->tx.outer_vlan_tag =
					rte_cpu_to_le_16(rxm->vlan_tci);
	}

	if (ol_flags & RTE_MBUF_F_TX_QINQ ||
	    ((ol_flags & RTE_MBUF_F_TX_VLAN) && txq->pvid_sw_shift_en)) {
		desc->tx.type_cs_vlan_tso_len |=
					rte_cpu_to_le_32(BIT(HNS3_TXD_VLAN_B));
		desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
	}

	if (ol_flags & RTE_MBUF_F_TX_IEEE1588_TMST)
		desc->tx.tp_fe_sc_vld_ra_ri |=
				rte_cpu_to_le_16(BIT(HNS3_TXD_TSYN_B));
}

static inline int
hns3_tx_alloc_mbufs(struct rte_mempool *mb_pool, uint16_t nb_new_buf,
			struct rte_mbuf **alloc_mbuf)
{
#define MAX_NON_TSO_BD_PER_PKT 18
	struct rte_mbuf *pkt_segs[MAX_NON_TSO_BD_PER_PKT];
	uint16_t i;

	/* Allocate enough mbufs */
	if (rte_mempool_get_bulk(mb_pool, (void **)pkt_segs, nb_new_buf))
		return -ENOMEM;

	for (i = 0; i < nb_new_buf - 1; i++)
		pkt_segs[i]->next = pkt_segs[i + 1];

	pkt_segs[nb_new_buf - 1]->next = NULL;
	pkt_segs[0]->nb_segs = nb_new_buf;
	*alloc_mbuf = pkt_segs[0];

	return 0;
}

static inline void
hns3_pktmbuf_copy_hdr(struct rte_mbuf *new_pkt, struct rte_mbuf *old_pkt)
{
	new_pkt->ol_flags = old_pkt->ol_flags;
	new_pkt->pkt_len = rte_pktmbuf_pkt_len(old_pkt);
	new_pkt->outer_l2_len = old_pkt->outer_l2_len;
	new_pkt->outer_l3_len = old_pkt->outer_l3_len;
	new_pkt->l2_len = old_pkt->l2_len;
	new_pkt->l3_len = old_pkt->l3_len;
	new_pkt->l4_len = old_pkt->l4_len;
	new_pkt->vlan_tci_outer = old_pkt->vlan_tci_outer;
	new_pkt->vlan_tci = old_pkt->vlan_tci;
}

static int
hns3_reassemble_tx_pkts(struct rte_mbuf *tx_pkt, struct rte_mbuf **new_pkt,
				  uint8_t max_non_tso_bd_num)
{
	struct rte_mempool *mb_pool;
	struct rte_mbuf *new_mbuf;
	struct rte_mbuf *temp_new;
	struct rte_mbuf *temp;
	uint16_t last_buf_len;
	uint16_t nb_new_buf;
	uint16_t buf_size;
	uint16_t buf_len;
	uint16_t len_s;
	uint16_t len_d;
	uint16_t len;
	int ret;
	char *s;
	char *d;

	mb_pool = tx_pkt->pool;
	buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
	nb_new_buf = (rte_pktmbuf_pkt_len(tx_pkt) - 1) / buf_size + 1;
	if (nb_new_buf > max_non_tso_bd_num)
		return -EINVAL;

	last_buf_len = rte_pktmbuf_pkt_len(tx_pkt) % buf_size;
	if (last_buf_len == 0)
		last_buf_len = buf_size;

	/* Allocate enough mbufs */
	ret = hns3_tx_alloc_mbufs(mb_pool, nb_new_buf, &new_mbuf);
	if (ret)
		return ret;

	/* Copy the original packet content to the new mbufs */
	temp = tx_pkt;
	s = rte_pktmbuf_mtod(temp, char *);
	len_s = rte_pktmbuf_data_len(temp);
	temp_new = new_mbuf;
	while (temp != NULL && temp_new != NULL) {
		d = rte_pktmbuf_mtod(temp_new, char *);
		buf_len = temp_new->next == NULL ? last_buf_len : buf_size;
		len_d = buf_len;

		while (len_d) {
			len = RTE_MIN(len_s, len_d);
			memcpy(d, s, len);
			s = s + len;
			d = d + len;
			len_d = len_d - len;
			len_s = len_s - len;

			if (len_s == 0) {
				temp = temp->next;
				if (temp == NULL)
					break;
				s = rte_pktmbuf_mtod(temp, char *);
				len_s = rte_pktmbuf_data_len(temp);
			}
		}

		temp_new->data_len = buf_len;
		temp_new = temp_new->next;
	}
	hns3_pktmbuf_copy_hdr(new_mbuf, tx_pkt);

	/* free original mbufs */
	rte_pktmbuf_free(tx_pkt);

	*new_pkt = new_mbuf;

	return 0;
}

static void
hns3_parse_outer_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec)
{
	uint32_t tmp = *ol_type_vlan_len_msec;
	uint64_t ol_flags = m->ol_flags;

	/* (outer) IP header type */
	if (ol_flags & RTE_MBUF_F_TX_OUTER_IPV4) {
		if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM)
			tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
					HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
		else
			tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
				HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_NO_CSUM);
	} else if (ol_flags & RTE_MBUF_F_TX_OUTER_IPV6) {
		tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
					HNS3_OL3T_IPV6);
	}
	/* OL3 header size, defined in 4 bytes */
	tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
				m->outer_l3_len >> HNS3_L3_LEN_UNIT);
	*ol_type_vlan_len_msec = tmp;
}

static int
hns3_parse_inner_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec,
			uint32_t *type_cs_vlan_tso_len)
{
#define HNS3_NVGRE_HLEN 8
	uint32_t tmp_outer = *ol_type_vlan_len_msec;
	uint32_t tmp_inner = *type_cs_vlan_tso_len;
	uint64_t ol_flags = m->ol_flags;
	uint16_t inner_l2_len;

	switch (ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) {
	case RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE:
	case RTE_MBUF_F_TX_TUNNEL_GENEVE:
	case RTE_MBUF_F_TX_TUNNEL_VXLAN:
		/* MAC in UDP tunnelling packet, include VxLAN and GENEVE */
		tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
				HNS3_TXD_TUNTYPE_S, HNS3_TUN_MAC_IN_UDP);
		/*
		 * The inner l2 length of mbuf is the sum of outer l4 length,
		 * tunneling header length and inner l2 length for a tunnel
		 * packect. But in hns3 tx descriptor, the tunneling header
		 * length is contained in the field of outer L4 length.
		 * Therefore, driver need to calculate the outer L4 length and
		 * inner L2 length.
		 */
		tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
						HNS3_TXD_L4LEN_S,
						(uint8_t)RTE_ETHER_VXLAN_HLEN >>
						HNS3_L4_LEN_UNIT);

		inner_l2_len = m->l2_len - RTE_ETHER_VXLAN_HLEN;
		break;
	case RTE_MBUF_F_TX_TUNNEL_GRE:
		tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
					HNS3_TXD_TUNTYPE_S, HNS3_TUN_NVGRE);
		/*
		 * For NVGRE tunnel packect, the outer L4 is empty. So only
		 * fill the NVGRE header length to the outer L4 field.
		 */
		tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
				HNS3_TXD_L4LEN_S,
				(uint8_t)HNS3_NVGRE_HLEN >> HNS3_L4_LEN_UNIT);

		inner_l2_len = m->l2_len - HNS3_NVGRE_HLEN;
		break;
	default:
		/* For non UDP / GRE tunneling, drop the tunnel packet */
		return -EINVAL;
	}

	tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
					inner_l2_len >> HNS3_L2_LEN_UNIT);
	/* OL2 header size, defined in 2 bytes */
	tmp_outer |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
					m->outer_l2_len >> HNS3_L2_LEN_UNIT);

	*type_cs_vlan_tso_len = tmp_inner;
	*ol_type_vlan_len_msec = tmp_outer;

	return 0;
}

static int
hns3_parse_tunneling_params(struct hns3_tx_queue *txq, struct rte_mbuf *m,
			    uint16_t tx_desc_id)
{
	struct hns3_desc *tx_ring = txq->tx_ring;
	struct hns3_desc *desc = &tx_ring[tx_desc_id];
	uint64_t ol_flags = m->ol_flags;
	uint32_t tmp_outer = 0;
	uint32_t tmp_inner = 0;
	uint32_t tmp_ol4cs;
	int ret;

	/*
	 * The tunnel header is contained in the inner L2 header field of the
	 * mbuf, but for hns3 descriptor, it is contained in the outer L4. So,
	 * there is a need that switching between them. To avoid multiple
	 * calculations, the length of the L2 header include the outer and
	 * inner, will be filled during the parsing of tunnel packects.
	 */
	if (!(ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)) {
		/*
		 * For non tunnel type the tunnel type id is 0, so no need to
		 * assign a value to it. Only the inner(normal) L2 header length
		 * is assigned.
		 */
		tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M,
			       HNS3_TXD_L2LEN_S, m->l2_len >> HNS3_L2_LEN_UNIT);
	} else {
		/*
		 * If outer csum is not offload, the outer length may be filled
		 * with 0. And the length of the outer header is added to the
		 * inner l2_len. It would lead a cksum error. So driver has to
		 * calculate the header length.
		 */
		if (unlikely(!(ol_flags &
			(RTE_MBUF_F_TX_OUTER_IP_CKSUM | RTE_MBUF_F_TX_OUTER_UDP_CKSUM)) &&
					m->outer_l2_len == 0)) {
			struct rte_net_hdr_lens hdr_len;
			(void)rte_net_get_ptype(m, &hdr_len,
					RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
			m->outer_l3_len = hdr_len.l3_len;
			m->outer_l2_len = hdr_len.l2_len;
			m->l2_len = m->l2_len - hdr_len.l2_len - hdr_len.l3_len;
		}
		hns3_parse_outer_params(m, &tmp_outer);
		ret = hns3_parse_inner_params(m, &tmp_outer, &tmp_inner);
		if (ret)
			return -EINVAL;
	}

	desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp_outer);
	desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp_inner);
	tmp_ol4cs = ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM ?
			BIT(HNS3_TXD_OL4CS_B) : 0;
	desc->tx.paylen_fd_dop_ol4cs = rte_cpu_to_le_32(tmp_ol4cs);

	return 0;
}

static void
hns3_parse_l3_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
{
	uint64_t ol_flags = m->ol_flags;
	uint32_t l3_type;
	uint32_t tmp;

	tmp = *type_cs_vlan_tso_len;
	if (ol_flags & RTE_MBUF_F_TX_IPV4)
		l3_type = HNS3_L3T_IPV4;
	else if (ol_flags & RTE_MBUF_F_TX_IPV6)
		l3_type = HNS3_L3T_IPV6;
	else
		l3_type = HNS3_L3T_NONE;

	/* inner(/normal) L3 header size, defined in 4 bytes */
	tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
					m->l3_len >> HNS3_L3_LEN_UNIT);

	tmp |= hns3_gen_field_val(HNS3_TXD_L3T_M, HNS3_TXD_L3T_S, l3_type);

	/* Enable L3 checksum offloads */
	if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
		tmp |= BIT(HNS3_TXD_L3CS_B);
	*type_cs_vlan_tso_len = tmp;
}

static void
hns3_parse_l4_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
{
	uint64_t ol_flags = m->ol_flags;
	uint32_t tmp;
	/* Enable L4 checksum offloads */
	switch (ol_flags & (RTE_MBUF_F_TX_L4_MASK | RTE_MBUF_F_TX_TCP_SEG)) {
	case RTE_MBUF_F_TX_TCP_CKSUM | RTE_MBUF_F_TX_TCP_SEG:
	case RTE_MBUF_F_TX_TCP_CKSUM:
	case RTE_MBUF_F_TX_TCP_SEG:
		tmp = *type_cs_vlan_tso_len;
		tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
					HNS3_L4T_TCP);
		break;
	case RTE_MBUF_F_TX_UDP_CKSUM:
		tmp = *type_cs_vlan_tso_len;
		tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
					HNS3_L4T_UDP);
		break;
	case RTE_MBUF_F_TX_SCTP_CKSUM:
		tmp = *type_cs_vlan_tso_len;
		tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
					HNS3_L4T_SCTP);
		break;
	default:
		return;
	}
	tmp |= BIT(HNS3_TXD_L4CS_B);
	tmp |= hns3_gen_field_val(HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
					m->l4_len >> HNS3_L4_LEN_UNIT);
	*type_cs_vlan_tso_len = tmp;
}

static void
hns3_txd_enable_checksum(struct hns3_tx_queue *txq, struct rte_mbuf *m,
			 uint16_t tx_desc_id)
{
	struct hns3_desc *tx_ring = txq->tx_ring;
	struct hns3_desc *desc = &tx_ring[tx_desc_id];
	uint32_t value = 0;

	hns3_parse_l3_cksum_params(m, &value);
	hns3_parse_l4_cksum_params(m, &value);

	desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
}

static bool
hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num,
				 uint32_t max_non_tso_bd_num)
{
	struct rte_mbuf *m_first = tx_pkts;
	struct rte_mbuf *m_last = tx_pkts;
	uint32_t tot_len = 0;
	uint32_t hdr_len;
	uint32_t i;

	/*
	 * Hardware requires that the sum of the data length of every 8
	 * consecutive buffers is greater than MSS in hns3 network engine.
	 * We simplify it by ensuring pkt_headlen + the first 8 consecutive
	 * frags greater than gso header len + mss, and the remaining 7
	 * consecutive frags greater than MSS except the last 7 frags.
	 */
	if (bd_num <= max_non_tso_bd_num)
		return false;

	for (i = 0; m_last && i < max_non_tso_bd_num - 1;
	     i++, m_last = m_last->next)
		tot_len += m_last->data_len;

	if (!m_last)
		return true;

	/* ensure the first 8 frags is greater than mss + header */
	hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
	hdr_len += (tx_pkts->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) ?
		   tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
	if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
		return true;

	/*
	 * ensure the sum of the data length of every 7 consecutive buffer
	 * is greater than mss except the last one.
	 */
	for (i = 0; m_last && i < bd_num - max_non_tso_bd_num; i++) {
		tot_len -= m_first->data_len;
		tot_len += m_last->data_len;

		if (tot_len < tx_pkts->tso_segsz)
			return true;

		m_first = m_first->next;
		m_last = m_last->next;
	}

	return false;
}

static bool
hns3_outer_ipv4_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
				uint32_t *l4_proto)
{
	struct rte_ipv4_hdr *ipv4_hdr;
	ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
					   m->outer_l2_len);
	if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM)
		ipv4_hdr->hdr_checksum = 0;
	if (ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM) {
		struct rte_udp_hdr *udp_hdr;
		/*
		 * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
		 * header for TSO packets
		 */
		if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
			return true;
		udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
				m->outer_l2_len + m->outer_l3_len);
		udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);

		return true;
	}
	*l4_proto = ipv4_hdr->next_proto_id;
	return false;
}

static bool
hns3_outer_ipv6_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
				uint32_t *l4_proto)
{
	struct rte_ipv6_hdr *ipv6_hdr;
	ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
					   m->outer_l2_len);
	if (ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM) {
		struct rte_udp_hdr *udp_hdr;
		/*
		 * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
		 * header for TSO packets
		 */
		if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
			return true;
		udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
				m->outer_l2_len + m->outer_l3_len);
		udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);

		return true;
	}
	*l4_proto = ipv6_hdr->proto;
	return false;
}

static void
hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
{
	uint64_t ol_flags = m->ol_flags;
	uint32_t paylen, hdr_len, l4_proto;
	struct rte_udp_hdr *udp_hdr;

	if (!(ol_flags & (RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_OUTER_IPV6)))
		return;

	if (ol_flags & RTE_MBUF_F_TX_OUTER_IPV4) {
		if (hns3_outer_ipv4_cksum_prepared(m, ol_flags, &l4_proto))
			return;
	} else {
		if (hns3_outer_ipv6_cksum_prepared(m, ol_flags, &l4_proto))
			return;
	}

	/* driver should ensure the outer udp cksum is 0 for TUNNEL TSO */
	if (l4_proto == IPPROTO_UDP && (ol_flags & RTE_MBUF_F_TX_TCP_SEG)) {
		hdr_len = m->l2_len + m->l3_len + m->l4_len;
		hdr_len += m->outer_l2_len + m->outer_l3_len;
		paylen = m->pkt_len - hdr_len;
		if (paylen <= m->tso_segsz)
			return;
		udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
						  m->outer_l2_len +
						  m->outer_l3_len);
		udp_hdr->dgram_cksum = 0;
	}
}

static int
hns3_check_tso_pkt_valid(struct rte_mbuf *m)
{
	uint32_t tmp_data_len_sum = 0;
	uint16_t nb_buf = m->nb_segs;
	uint32_t paylen, hdr_len;
	struct rte_mbuf *m_seg;
	int i;

	if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
		return -EINVAL;

	hdr_len = m->l2_len + m->l3_len + m->l4_len;
	hdr_len += (m->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) ?
			m->outer_l2_len + m->outer_l3_len : 0;
	if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
		return -EINVAL;

	paylen = m->pkt_len - hdr_len;
	if (paylen > HNS3_MAX_BD_PAYLEN)
		return -EINVAL;

	/*
	 * The TSO header (include outer and inner L2, L3 and L4 header)
	 * should be provided by three descriptors in maximum in hns3 network
	 * engine.
	 */
	m_seg = m;
	for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
	     i++, m_seg = m_seg->next) {
		tmp_data_len_sum += m_seg->data_len;
	}

	if (hdr_len > tmp_data_len_sum)
		return -EINVAL;

	return 0;
}

#ifdef RTE_LIBRTE_ETHDEV_DEBUG
static inline int
hns3_vld_vlan_chk(struct hns3_tx_queue *txq, struct rte_mbuf *m)
{
	struct rte_ether_hdr *eh;
	struct rte_vlan_hdr *vh;

	if (!txq->pvid_sw_shift_en)
		return 0;

	/*
	 * Due to hardware limitations, we only support two-layer VLAN hardware
	 * offload in Tx direction based on hns3 network engine, so when PVID is
	 * enabled, QinQ insert is no longer supported.
	 * And when PVID is enabled, in the following two cases:
	 *  i) packets with more than two VLAN tags.
	 *  ii) packets with one VLAN tag while the hardware VLAN insert is
	 *      enabled.
	 * The packets will be regarded as abnormal packets and discarded by
	 * hardware in Tx direction. For debugging purposes, a validation check
	 * for these types of packets is added to the '.tx_pkt_prepare' ops
	 * implementation function named hns3_prep_pkts to inform users that
	 * these packets will be discarded.
	 */
	if (m->ol_flags & RTE_MBUF_F_TX_QINQ)
		return -EINVAL;

	eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
	if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
		if (m->ol_flags & RTE_MBUF_F_TX_VLAN)
			return -EINVAL;

		/* Ensure the incoming packet is not a QinQ packet */
		vh = (struct rte_vlan_hdr *)(eh + 1);
		if (vh->eth_proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
			return -EINVAL;
	}

	return 0;
}
#endif

static uint16_t
hns3_udp_cksum_help(struct rte_mbuf *m)
{
	uint64_t ol_flags = m->ol_flags;
	uint16_t cksum = 0;
	uint32_t l4_len;

	if (ol_flags & RTE_MBUF_F_TX_IPV4) {
		struct rte_ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
				struct rte_ipv4_hdr *, m->l2_len);
		l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) - m->l3_len;
	} else {
		struct rte_ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
				struct rte_ipv6_hdr *, m->l2_len);
		l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
	}

	rte_raw_cksum_mbuf(m, m->l2_len + m->l3_len, l4_len, &cksum);

	cksum = ~cksum;
	/*
	 * RFC 768:If the computed checksum is zero for UDP, it is transmitted
	 * as all ones
	 */
	if (cksum == 0)
		cksum = 0xffff;

	return (uint16_t)cksum;
}

static bool
hns3_validate_tunnel_cksum(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
{
	uint64_t ol_flags = m->ol_flags;
	struct rte_udp_hdr *udp_hdr;
	uint16_t dst_port;

	if (tx_queue->udp_cksum_mode == HNS3_SPECIAL_PORT_HW_CKSUM_MODE ||
	    ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK ||
	    (ol_flags & RTE_MBUF_F_TX_L4_MASK) != RTE_MBUF_F_TX_UDP_CKSUM)
		return true;
	/*
	 * A UDP packet with the same dst_port as VXLAN\VXLAN_GPE\GENEVE will
	 * be recognized as a tunnel packet in HW. In this case, if UDP CKSUM
	 * offload is set and the tunnel mask has not been set, the CKSUM will
	 * be wrong since the header length is wrong and driver should complete
	 * the CKSUM to avoid CKSUM error.
	 */
	udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
						m->l2_len + m->l3_len);
	dst_port = rte_be_to_cpu_16(udp_hdr->dst_port);
	switch (dst_port) {
	case RTE_VXLAN_DEFAULT_PORT:
	case RTE_VXLAN_GPE_DEFAULT_PORT:
	case RTE_GENEVE_DEFAULT_PORT:
		udp_hdr->dgram_cksum = hns3_udp_cksum_help(m);
		m->ol_flags = ol_flags & ~RTE_MBUF_F_TX_L4_MASK;
		return false;
	default:
		return true;
	}
}

static int
hns3_prep_pkt_proc(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
{
	int ret;

#ifdef RTE_LIBRTE_ETHDEV_DEBUG
	ret = rte_validate_tx_offload(m);
	if (ret != 0) {
		rte_errno = -ret;
		return ret;
	}

	ret = hns3_vld_vlan_chk(tx_queue, m);
	if (ret != 0) {
		rte_errno = EINVAL;
		return ret;
	}
#endif
	if (hns3_pkt_is_tso(m)) {
		if (hns3_pkt_need_linearized(m, m->nb_segs,
					     tx_queue->max_non_tso_bd_num) ||
		    hns3_check_tso_pkt_valid(m)) {
			rte_errno = EINVAL;
			return -EINVAL;
		}

		if (tx_queue->tso_mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) {
			/*
			 * (tso mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) means
			 * hardware support recalculate the TCP pseudo header
			 * checksum of packets that need TSO, so network driver
			 * software not need to recalculate it.
			 */
			hns3_outer_header_cksum_prepare(m);
			return 0;
		}
	}

	ret = rte_net_intel_cksum_prepare(m);
	if (ret != 0) {
		rte_errno = -ret;
		return ret;
	}

	if (!hns3_validate_tunnel_cksum(tx_queue, m))
		return 0;

	hns3_outer_header_cksum_prepare(m);

	return 0;
}

uint16_t
hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
	       uint16_t nb_pkts)
{
	struct rte_mbuf *m;
	uint16_t i;

	for (i = 0; i < nb_pkts; i++) {
		m = tx_pkts[i];
		if (hns3_prep_pkt_proc(tx_queue, m))
			return i;
	}

	return i;
}

static int
hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
		 struct rte_mbuf *m)
{
	struct hns3_desc *tx_ring =