File: cpfl_ethdev.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (3163 lines) | stat: -rw-r--r-- 85,532 bytes parent folder | download
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2023 Intel Corporation
 */

#include <rte_atomic.h>
#include <rte_eal.h>
#include <rte_ether.h>
#include <rte_malloc.h>
#include <rte_memzone.h>
#include <rte_dev.h>
#include <errno.h>
#include <rte_alarm.h>
#include <rte_hash_crc.h>

#include "cpfl_ethdev.h"
#include <ethdev_private.h>
#include <sys/utsname.h>
#include "cpfl_rxtx.h"
#include "cpfl_flow.h"
#include "cpfl_rules.h"
#include "../common/tx.h"

#define CPFL_REPRESENTOR	"representor"
#define CPFL_TX_SINGLE_Q	"tx_single"
#define CPFL_RX_SINGLE_Q	"rx_single"
#define CPFL_VPORT		"vport"

#ifdef RTE_HAS_JANSSON
#define CPFL_FLOW_PARSER	"flow_parser"
#endif

#define VCPF_FID	0
#define CPFL_FID	6

rte_spinlock_t cpfl_adapter_lock;
/* A list for all adapters, one adapter matches one PCI device */
struct cpfl_adapter_list cpfl_adapter_list;
bool cpfl_adapter_list_init;

static const char * const cpfl_valid_args_first[] = {
	CPFL_REPRESENTOR,
	CPFL_TX_SINGLE_Q,
	CPFL_RX_SINGLE_Q,
	CPFL_VPORT,
#ifdef RTE_HAS_JANSSON
	CPFL_FLOW_PARSER,
#endif
	NULL
};

static const char * const cpfl_valid_args_again[] = {
	CPFL_REPRESENTOR,
	NULL
};

uint32_t cpfl_supported_speeds[] = {
	RTE_ETH_SPEED_NUM_NONE,
	RTE_ETH_SPEED_NUM_10M,
	RTE_ETH_SPEED_NUM_100M,
	RTE_ETH_SPEED_NUM_1G,
	RTE_ETH_SPEED_NUM_2_5G,
	RTE_ETH_SPEED_NUM_5G,
	RTE_ETH_SPEED_NUM_10G,
	RTE_ETH_SPEED_NUM_20G,
	RTE_ETH_SPEED_NUM_25G,
	RTE_ETH_SPEED_NUM_40G,
	RTE_ETH_SPEED_NUM_50G,
	RTE_ETH_SPEED_NUM_56G,
	RTE_ETH_SPEED_NUM_100G,
	RTE_ETH_SPEED_NUM_200G
};

static const uint64_t cpfl_map_hena_rss[] = {
	[IDPF_HASH_NONF_UNICAST_IPV4_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV4_UDP,
	[IDPF_HASH_NONF_MULTICAST_IPV4_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV4_UDP,
	[IDPF_HASH_NONF_IPV4_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV4_UDP,
	[IDPF_HASH_NONF_IPV4_TCP_SYN_NO_ACK] =
			RTE_ETH_RSS_NONFRAG_IPV4_TCP,
	[IDPF_HASH_NONF_IPV4_TCP] =
			RTE_ETH_RSS_NONFRAG_IPV4_TCP,
	[IDPF_HASH_NONF_IPV4_SCTP] =
			RTE_ETH_RSS_NONFRAG_IPV4_SCTP,
	[IDPF_HASH_NONF_IPV4_OTHER] =
			RTE_ETH_RSS_NONFRAG_IPV4_OTHER,
	[IDPF_HASH_FRAG_IPV4] = RTE_ETH_RSS_FRAG_IPV4,

	/* IPv6 */
	[IDPF_HASH_NONF_UNICAST_IPV6_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV6_UDP,
	[IDPF_HASH_NONF_MULTICAST_IPV6_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV6_UDP,
	[IDPF_HASH_NONF_IPV6_UDP] =
			RTE_ETH_RSS_NONFRAG_IPV6_UDP,
	[IDPF_HASH_NONF_IPV6_TCP_SYN_NO_ACK] =
			RTE_ETH_RSS_NONFRAG_IPV6_TCP,
	[IDPF_HASH_NONF_IPV6_TCP] =
			RTE_ETH_RSS_NONFRAG_IPV6_TCP,
	[IDPF_HASH_NONF_IPV6_SCTP] =
			RTE_ETH_RSS_NONFRAG_IPV6_SCTP,
	[IDPF_HASH_NONF_IPV6_OTHER] =
			RTE_ETH_RSS_NONFRAG_IPV6_OTHER,
	[IDPF_HASH_FRAG_IPV6] = RTE_ETH_RSS_FRAG_IPV6,

	/* L2 Payload */
	[IDPF_HASH_L2_PAYLOAD] = RTE_ETH_RSS_L2_PAYLOAD
};

static const uint64_t cpfl_ipv4_rss = RTE_ETH_RSS_NONFRAG_IPV4_UDP |
			  RTE_ETH_RSS_NONFRAG_IPV4_TCP |
			  RTE_ETH_RSS_NONFRAG_IPV4_SCTP |
			  RTE_ETH_RSS_NONFRAG_IPV4_OTHER |
			  RTE_ETH_RSS_FRAG_IPV4;

static const uint64_t cpfl_ipv6_rss = RTE_ETH_RSS_NONFRAG_IPV6_UDP |
			  RTE_ETH_RSS_NONFRAG_IPV6_TCP |
			  RTE_ETH_RSS_NONFRAG_IPV6_SCTP |
			  RTE_ETH_RSS_NONFRAG_IPV6_OTHER |
			  RTE_ETH_RSS_FRAG_IPV6;

struct rte_cpfl_xstats_name_off {
	char name[RTE_ETH_XSTATS_NAME_SIZE];
	unsigned int offset;
};

static const struct rte_cpfl_xstats_name_off rte_cpfl_stats_strings[] = {
	{"rx_bytes", offsetof(struct virtchnl2_vport_stats, rx_bytes)},
	{"rx_unicast_packets", offsetof(struct virtchnl2_vport_stats, rx_unicast)},
	{"rx_multicast_packets", offsetof(struct virtchnl2_vport_stats, rx_multicast)},
	{"rx_broadcast_packets", offsetof(struct virtchnl2_vport_stats, rx_broadcast)},
	{"rx_dropped_packets", offsetof(struct virtchnl2_vport_stats, rx_discards)},
	{"rx_errors", offsetof(struct virtchnl2_vport_stats, rx_errors)},
	{"rx_unknown_protocol_packets", offsetof(struct virtchnl2_vport_stats,
						 rx_unknown_protocol)},
	{"tx_bytes", offsetof(struct virtchnl2_vport_stats, tx_bytes)},
	{"tx_unicast_packets", offsetof(struct virtchnl2_vport_stats, tx_unicast)},
	{"tx_multicast_packets", offsetof(struct virtchnl2_vport_stats, tx_multicast)},
	{"tx_broadcast_packets", offsetof(struct virtchnl2_vport_stats, tx_broadcast)},
	{"tx_dropped_packets", offsetof(struct virtchnl2_vport_stats, tx_discards)},
	{"tx_error_packets", offsetof(struct virtchnl2_vport_stats, tx_errors)}};

#define CPFL_NB_XSTATS			RTE_DIM(rte_cpfl_stats_strings)

static int
cpfl_dev_link_update(struct rte_eth_dev *dev,
		     __rte_unused int wait_to_complete)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct rte_eth_link new_link;
	unsigned int i;

	memset(&new_link, 0, sizeof(new_link));

	for (i = 0; i < RTE_DIM(cpfl_supported_speeds); i++) {
		if (vport->link_speed == cpfl_supported_speeds[i]) {
			new_link.link_speed = vport->link_speed;
			break;
		}
	}

	if (i == RTE_DIM(cpfl_supported_speeds)) {
		if (vport->link_up)
			new_link.link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
		else
			new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
	}

	new_link.link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
	new_link.link_status = vport->link_up ? RTE_ETH_LINK_UP :
		RTE_ETH_LINK_DOWN;
	new_link.link_autoneg = (dev->data->dev_conf.link_speeds & RTE_ETH_LINK_SPEED_FIXED) ?
				 RTE_ETH_LINK_FIXED : RTE_ETH_LINK_AUTONEG;

	return rte_eth_linkstatus_set(dev, &new_link);
}

static int
cpfl_hairpin_cap_get(struct rte_eth_dev *dev,
		     struct rte_eth_hairpin_cap *cap)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;

	if (cpfl_vport->p2p_q_chunks_info == NULL)
		return -ENOTSUP;

	cap->max_nb_queues = CPFL_MAX_P2P_NB_QUEUES;
	cap->max_rx_2_tx = CPFL_MAX_HAIRPINQ_RX_2_TX;
	cap->max_tx_2_rx = CPFL_MAX_HAIRPINQ_TX_2_RX;
	cap->max_nb_desc = CPFL_MAX_HAIRPINQ_NB_DESC;

	return 0;
}

static int
cpfl_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;

	dev_info->max_rx_queues = base->caps.max_rx_q;
	dev_info->max_tx_queues = base->caps.max_tx_q;
	dev_info->min_rx_bufsize = CPFL_MIN_BUF_SIZE;
	dev_info->max_rx_pktlen = vport->max_mtu + CPFL_ETH_OVERHEAD;

	dev_info->max_mtu = vport->max_mtu;
	dev_info->min_mtu = RTE_ETHER_MIN_MTU;

	dev_info->hash_key_size = vport->rss_key_size;
	dev_info->reta_size = vport->rss_lut_size;

	dev_info->flow_type_rss_offloads = CPFL_RSS_OFFLOAD_ALL;

	dev_info->rx_offload_capa =
		RTE_ETH_RX_OFFLOAD_IPV4_CKSUM           |
		RTE_ETH_RX_OFFLOAD_UDP_CKSUM            |
		RTE_ETH_RX_OFFLOAD_TCP_CKSUM            |
		RTE_ETH_RX_OFFLOAD_OUTER_IPV4_CKSUM     |
		RTE_ETH_RX_OFFLOAD_TIMESTAMP		|
		RTE_ETH_RX_OFFLOAD_SCATTER;

	dev_info->tx_offload_capa =
		RTE_ETH_TX_OFFLOAD_IPV4_CKSUM		|
		RTE_ETH_TX_OFFLOAD_UDP_CKSUM		|
		RTE_ETH_TX_OFFLOAD_TCP_CKSUM		|
		RTE_ETH_TX_OFFLOAD_SCTP_CKSUM		|
		RTE_ETH_TX_OFFLOAD_TCP_TSO		|
		RTE_ETH_TX_OFFLOAD_MULTI_SEGS		|
		RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;

	dev_info->default_txconf = (struct rte_eth_txconf) {
		.tx_free_thresh = CPFL_DEFAULT_TX_FREE_THRESH,
		.tx_rs_thresh = CPFL_DEFAULT_TX_RS_THRESH,
	};

	dev_info->default_rxconf = (struct rte_eth_rxconf) {
		.rx_free_thresh = CPFL_DEFAULT_RX_FREE_THRESH,
	};

	dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
		.nb_max = CPFL_MAX_RING_DESC,
		.nb_min = CPFL_MIN_RING_DESC,
		.nb_align = CPFL_ALIGN_RING_DESC,
	};

	dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
		.nb_max = CPFL_MAX_RING_DESC,
		.nb_min = CPFL_MIN_RING_DESC,
		.nb_align = CPFL_ALIGN_RING_DESC,
	};

	return 0;
}

static int
cpfl_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;

	/* mtu setting is forbidden if port is start */
	if (dev->data->dev_started) {
		PMD_DRV_LOG(ERR, "port must be stopped before configuration");
		return -EBUSY;
	}

	if (mtu > vport->max_mtu) {
		PMD_DRV_LOG(ERR, "MTU should be less than %d", vport->max_mtu);
		return -EINVAL;
	}

	vport->max_pkt_len = mtu + CPFL_ETH_OVERHEAD;

	return 0;
}

static const uint32_t *
cpfl_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused,
			      size_t *no_of_elements)
{
	static const uint32_t ptypes[] = {
		RTE_PTYPE_L2_ETHER,
		RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
		RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
		RTE_PTYPE_L4_FRAG,
		RTE_PTYPE_L4_UDP,
		RTE_PTYPE_L4_TCP,
		RTE_PTYPE_L4_SCTP,
		RTE_PTYPE_L4_ICMP,
	};

	*no_of_elements = RTE_DIM(ptypes);
	return ptypes;
}

static uint64_t
cpfl_get_mbuf_alloc_failed_stats(struct rte_eth_dev *dev)
{
	uint64_t mbuf_alloc_failed = 0;
	struct cpfl_rx_queue *cpfl_rxq;
	int i = 0;

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		mbuf_alloc_failed +=
			rte_atomic_load_explicit(&cpfl_rxq->base.rx_stats.mbuf_alloc_failed,
						 rte_memory_order_relaxed);
	}

	return mbuf_alloc_failed;
}

static int
cpfl_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats,
		struct eth_queue_stats *qstats __rte_unused)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct virtchnl2_vport_stats *pstats = NULL;
	int ret;

	ret = idpf_vc_stats_query(vport, &pstats);
	if (ret == 0) {
		uint8_t crc_stats_len = (dev->data->dev_conf.rxmode.offloads &
					 RTE_ETH_RX_OFFLOAD_KEEP_CRC) ? 0 :
					 RTE_ETHER_CRC_LEN;

		idpf_vport_stats_update(&vport->eth_stats_offset, pstats);
		stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
				  pstats->rx_broadcast;
		stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
						pstats->tx_unicast;
		stats->imissed = pstats->rx_discards;
		stats->ierrors = pstats->rx_errors;
		stats->oerrors = pstats->tx_errors + pstats->tx_discards;
		stats->ibytes = pstats->rx_bytes;
		stats->ibytes -= stats->ipackets * crc_stats_len;
		stats->obytes = pstats->tx_bytes;

		dev->data->rx_mbuf_alloc_failed = cpfl_get_mbuf_alloc_failed_stats(dev);
		stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
	} else {
		PMD_DRV_LOG(ERR, "Get statistics failed");
	}
	return ret;
}

static void
cpfl_reset_mbuf_alloc_failed_stats(struct rte_eth_dev *dev)
{
	struct cpfl_rx_queue *cpfl_rxq;
	int i;

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		rte_atomic_store_explicit(&cpfl_rxq->base.rx_stats.mbuf_alloc_failed, 0,
					  rte_memory_order_relaxed);
	}
}

static int
cpfl_dev_stats_reset(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct virtchnl2_vport_stats *pstats = NULL;
	int ret;

	ret = idpf_vc_stats_query(vport, &pstats);
	if (ret != 0)
		return ret;

	/* set stats offset base on current values */
	vport->eth_stats_offset = *pstats;

	cpfl_reset_mbuf_alloc_failed_stats(dev);

	return 0;
}

static int cpfl_dev_xstats_reset(struct rte_eth_dev *dev)
{
	cpfl_dev_stats_reset(dev);
	return 0;
}

static int cpfl_dev_xstats_get(struct rte_eth_dev *dev,
			       struct rte_eth_xstat *xstats, unsigned int n)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct virtchnl2_vport_stats *pstats = NULL;
	unsigned int i;
	int ret;

	if (n < CPFL_NB_XSTATS)
		return CPFL_NB_XSTATS;

	if (!xstats)
		return CPFL_NB_XSTATS;

	ret = idpf_vc_stats_query(vport, &pstats);
	if (ret) {
		PMD_DRV_LOG(ERR, "Get statistics failed");
		return 0;
	}

	idpf_vport_stats_update(&vport->eth_stats_offset, pstats);

	/* loop over xstats array and values from pstats */
	for (i = 0; i < CPFL_NB_XSTATS; i++) {
		xstats[i].id = i;
		xstats[i].value = *(uint64_t *)(((char *)pstats) +
			rte_cpfl_stats_strings[i].offset);
	}
	return CPFL_NB_XSTATS;
}

static int cpfl_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
				     struct rte_eth_xstat_name *xstats_names,
				     __rte_unused unsigned int limit)
{
	unsigned int i;

	if (xstats_names) {
		for (i = 0; i < CPFL_NB_XSTATS; i++) {
			snprintf(xstats_names[i].name,
				 sizeof(xstats_names[i].name),
				 "%s", rte_cpfl_stats_strings[i].name);
		}
	}
	return CPFL_NB_XSTATS;
}

static int cpfl_config_rss_hf(struct idpf_vport *vport, uint64_t rss_hf)
{
	uint64_t hena = 0;
	uint16_t i;

	/**
	 * RTE_ETH_RSS_IPV4 and RTE_ETH_RSS_IPV6 can be considered as 2
	 * generalizations of all other IPv4 and IPv6 RSS types.
	 */
	if (rss_hf & RTE_ETH_RSS_IPV4)
		rss_hf |= cpfl_ipv4_rss;

	if (rss_hf & RTE_ETH_RSS_IPV6)
		rss_hf |= cpfl_ipv6_rss;

	for (i = 0; i < RTE_DIM(cpfl_map_hena_rss); i++) {
		if (cpfl_map_hena_rss[i] & rss_hf)
			hena |= BIT_ULL(i);
	}

	/**
	 * At present, cp doesn't process the virtual channel msg of rss_hf configuration,
	 * tips are given below.
	 */
	if (hena != vport->rss_hf)
		PMD_DRV_LOG(WARNING, "Updating RSS Hash Function is not supported at present.");

	return 0;
}

static int
cpfl_init_rss(struct idpf_vport *vport)
{
	struct rte_eth_rss_conf *rss_conf;
	struct rte_eth_dev_data *dev_data;
	uint16_t i, nb_q;
	int ret = 0;

	dev_data = vport->dev_data;
	rss_conf = &dev_data->dev_conf.rx_adv_conf.rss_conf;
	nb_q = dev_data->nb_rx_queues;

	if (rss_conf->rss_key == NULL) {
		for (i = 0; i < vport->rss_key_size; i++)
			vport->rss_key[i] = (uint8_t)rte_rand();
	} else if (rss_conf->rss_key_len != vport->rss_key_size) {
		PMD_INIT_LOG(ERR, "Invalid RSS key length in RSS configuration, should be %d",
			     vport->rss_key_size);
		return -EINVAL;
	} else {
		memcpy(vport->rss_key, rss_conf->rss_key,
			   vport->rss_key_size);
	}

	for (i = 0; i < vport->rss_lut_size; i++)
		vport->rss_lut[i] = i % nb_q;

	vport->rss_hf = IDPF_DEFAULT_RSS_HASH_EXPANDED;

	ret = idpf_vport_rss_config(vport);
	if (ret != 0)
		PMD_INIT_LOG(ERR, "Failed to configure RSS");

	return ret;
}

static int
cpfl_rss_reta_update(struct rte_eth_dev *dev,
		     struct rte_eth_rss_reta_entry64 *reta_conf,
		     uint16_t reta_size)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;
	uint16_t idx, shift;
	int ret = 0;
	uint16_t i;

	if (base->caps.rss_caps == 0 || dev->data->nb_rx_queues == 0) {
		PMD_DRV_LOG(DEBUG, "RSS is not supported");
		return -ENOTSUP;
	}

	if (reta_size != vport->rss_lut_size) {
		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
				 "(%d) doesn't match the number of hardware can "
				 "support (%d)",
			    reta_size, vport->rss_lut_size);
		return -EINVAL;
	}

	for (i = 0; i < reta_size; i++) {
		idx = i / RTE_ETH_RETA_GROUP_SIZE;
		shift = i % RTE_ETH_RETA_GROUP_SIZE;
		if (reta_conf[idx].mask & (1ULL << shift))
			vport->rss_lut[i] = reta_conf[idx].reta[shift];
	}

	/* send virtchnl ops to configure RSS */
	ret = idpf_vc_rss_lut_set(vport);
	if (ret)
		PMD_INIT_LOG(ERR, "Failed to configure RSS lut");

	return ret;
}

static int
cpfl_rss_reta_query(struct rte_eth_dev *dev,
		    struct rte_eth_rss_reta_entry64 *reta_conf,
		    uint16_t reta_size)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;
	uint16_t idx, shift;
	int ret = 0;
	uint16_t i;

	if (base->caps.rss_caps == 0 || dev->data->nb_rx_queues == 0) {
		PMD_DRV_LOG(DEBUG, "RSS is not supported");
		return -ENOTSUP;
	}

	if (reta_size != vport->rss_lut_size) {
		PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
			"(%d) doesn't match the number of hardware can "
			"support (%d)", reta_size, vport->rss_lut_size);
		return -EINVAL;
	}

	ret = idpf_vc_rss_lut_get(vport);
	if (ret) {
		PMD_DRV_LOG(ERR, "Failed to get RSS LUT");
		return ret;
	}

	for (i = 0; i < reta_size; i++) {
		idx = i / RTE_ETH_RETA_GROUP_SIZE;
		shift = i % RTE_ETH_RETA_GROUP_SIZE;
		if (reta_conf[idx].mask & (1ULL << shift))
			reta_conf[idx].reta[shift] = vport->rss_lut[i];
	}

	return 0;
}

static int
cpfl_rss_hash_update(struct rte_eth_dev *dev,
		     struct rte_eth_rss_conf *rss_conf)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;
	int ret = 0;

	if (base->caps.rss_caps == 0 || dev->data->nb_rx_queues == 0) {
		PMD_DRV_LOG(DEBUG, "RSS is not supported");
		return -ENOTSUP;
	}

	if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
		PMD_DRV_LOG(DEBUG, "No key to be configured");
		goto skip_rss_key;
	} else if (rss_conf->rss_key_len != vport->rss_key_size) {
		PMD_DRV_LOG(ERR, "The size of hash key configured "
				 "(%d) doesn't match the size of hardware can "
				 "support (%d)",
			    rss_conf->rss_key_len,
			    vport->rss_key_size);
		return -EINVAL;
	}

	memcpy(vport->rss_key, rss_conf->rss_key,
		   vport->rss_key_size);
	ret = idpf_vc_rss_key_set(vport);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to configure RSS key");
		return ret;
	}

skip_rss_key:
	ret = cpfl_config_rss_hf(vport, rss_conf->rss_hf);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to configure RSS hash");
		return ret;
	}

	return 0;
}

static uint64_t
cpfl_map_general_rss_hf(uint64_t config_rss_hf, uint64_t last_general_rss_hf)
{
	uint64_t valid_rss_hf = 0;
	uint16_t i;

	for (i = 0; i < RTE_DIM(cpfl_map_hena_rss); i++) {
		uint64_t bit = BIT_ULL(i);

		if (bit & config_rss_hf)
			valid_rss_hf |= cpfl_map_hena_rss[i];
	}

	if (valid_rss_hf & cpfl_ipv4_rss)
		valid_rss_hf |= last_general_rss_hf & RTE_ETH_RSS_IPV4;

	if (valid_rss_hf & cpfl_ipv6_rss)
		valid_rss_hf |= last_general_rss_hf & RTE_ETH_RSS_IPV6;

	return valid_rss_hf;
}

static int
cpfl_rss_hash_conf_get(struct rte_eth_dev *dev,
		       struct rte_eth_rss_conf *rss_conf)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;
	int ret = 0;

	if (base->caps.rss_caps == 0 || dev->data->nb_rx_queues == 0) {
		PMD_DRV_LOG(DEBUG, "RSS is not supported");
		return -ENOTSUP;
	}

	ret = idpf_vc_rss_hash_get(vport);
	if (ret) {
		PMD_DRV_LOG(ERR, "Failed to get RSS hf");
		return ret;
	}

	rss_conf->rss_hf = cpfl_map_general_rss_hf(vport->rss_hf, vport->last_general_rss_hf);

	if (!rss_conf->rss_key)
		return 0;

	ret = idpf_vc_rss_key_get(vport);
	if (ret) {
		PMD_DRV_LOG(ERR, "Failed to get RSS key");
		return ret;
	}

	if (rss_conf->rss_key_len > vport->rss_key_size)
		rss_conf->rss_key_len = vport->rss_key_size;

	memcpy(rss_conf->rss_key, vport->rss_key, rss_conf->rss_key_len);

	return 0;
}

static int
cpfl_dev_configure(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct rte_eth_conf *conf = &dev->data->dev_conf;
	struct idpf_adapter *base = vport->adapter;
	int ret;

	if (conf->link_speeds & RTE_ETH_LINK_SPEED_FIXED) {
		PMD_INIT_LOG(ERR, "Setting link speed is not supported");
		return -ENOTSUP;
	}

	if (conf->txmode.mq_mode != RTE_ETH_MQ_TX_NONE) {
		PMD_INIT_LOG(ERR, "Multi-queue TX mode %d is not supported",
			     conf->txmode.mq_mode);
		return -ENOTSUP;
	}

	if (conf->lpbk_mode != 0) {
		PMD_INIT_LOG(ERR, "Loopback operation mode %d is not supported",
			     conf->lpbk_mode);
		return -ENOTSUP;
	}

	if (conf->dcb_capability_en != 0) {
		PMD_INIT_LOG(ERR, "Priority Flow Control(PFC) if not supported");
		return -ENOTSUP;
	}

	if (conf->intr_conf.lsc != 0) {
		PMD_INIT_LOG(ERR, "LSC interrupt is not supported");
		return -ENOTSUP;
	}

	if (conf->intr_conf.rxq != 0) {
		PMD_INIT_LOG(ERR, "RXQ interrupt is not supported");
		return -ENOTSUP;
	}

	if (conf->intr_conf.rmv != 0) {
		PMD_INIT_LOG(ERR, "RMV interrupt is not supported");
		return -ENOTSUP;
	}

	if (conf->rxmode.mq_mode != RTE_ETH_MQ_RX_RSS &&
	    conf->rxmode.mq_mode != RTE_ETH_MQ_RX_NONE) {
		PMD_INIT_LOG(ERR, "RX mode %d is not supported.",
			     conf->rxmode.mq_mode);
		return -EINVAL;
	}

	if (base->caps.rss_caps != 0 && dev->data->nb_rx_queues != 0) {
		ret = cpfl_init_rss(vport);
		if (ret != 0) {
			PMD_INIT_LOG(ERR, "Failed to init rss");
			return ret;
		}
	} else if (conf->rxmode.mq_mode == RTE_ETH_MQ_RX_RSS) {
		PMD_INIT_LOG(ERR, "RSS is not supported.");
		return -ENOTSUP;
	}

	vport->max_pkt_len =
		(dev->data->mtu == 0) ? CPFL_DEFAULT_MTU : dev->data->mtu +
		CPFL_ETH_OVERHEAD;

	return 0;
}

static int
cpfl_config_rx_queues_irqs(struct rte_eth_dev *dev)
{
	uint32_t qids[CPFL_MAX_P2P_NB_QUEUES + IDPF_DEFAULT_RXQ_NUM] = {0};
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	uint16_t nb_rx_queues = dev->data->nb_rx_queues;
	struct cpfl_rx_queue *cpfl_rxq;
	int i;

	for (i = 0; i < nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		if (cpfl_rxq->hairpin_info.hairpin_q)
			qids[i] = cpfl_hw_qid_get(cpfl_vport->p2p_q_chunks_info->rx_start_qid,
						  (i - cpfl_vport->nb_data_rxq));
		else
			qids[i] = cpfl_hw_qid_get(vport->chunks_info.rx_start_qid, i);
	}
	return idpf_vport_irq_map_config_by_qids(vport, qids, nb_rx_queues);
}

/* Update hairpin_info for dev's tx hairpin queue */
static int
cpfl_txq_hairpin_info_update(struct rte_eth_dev *dev, uint16_t rx_port)
{
	struct cpfl_vport *cpfl_tx_vport = dev->data->dev_private;
	struct rte_eth_dev *peer_dev = &rte_eth_devices[rx_port];
	struct cpfl_vport *cpfl_rx_vport = peer_dev->data->dev_private;
	struct cpfl_txq_hairpin_info *hairpin_info;
	struct cpfl_tx_queue *cpfl_txq;
	int i;

	for (i = cpfl_tx_vport->nb_data_txq; i < dev->data->nb_tx_queues; i++) {
		cpfl_txq = dev->data->tx_queues[i];
		hairpin_info = &cpfl_txq->hairpin_info;
		if (hairpin_info->peer_rxp != rx_port) {
			PMD_DRV_LOG(ERR, "port %d is not the peer port", rx_port);
			return -EINVAL;
		}
		hairpin_info->peer_rxq_id =
			cpfl_hw_qid_get(cpfl_rx_vport->p2p_q_chunks_info->rx_start_qid,
					hairpin_info->peer_rxq_id - cpfl_rx_vport->nb_data_rxq);
	}

	return 0;
}

/* Bind Rx hairpin queue's memory zone to peer Tx hairpin queue's memory zone */
static void
cpfl_rxq_hairpin_mz_bind(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_rx_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_rx_vport->base;
	struct idpf_adapter *adapter = vport->adapter;
	struct idpf_hw *hw = &adapter->hw;
	struct cpfl_rx_queue *cpfl_rxq;
	struct cpfl_tx_queue *cpfl_txq;
	struct rte_eth_dev *peer_dev;
	const struct rte_memzone *mz;
	uint16_t peer_tx_port;
	uint16_t peer_tx_qid;
	int i;

	for (i = cpfl_rx_vport->nb_data_rxq; i < dev->data->nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		peer_tx_port = cpfl_rxq->hairpin_info.peer_txp;
		peer_tx_qid = cpfl_rxq->hairpin_info.peer_txq_id;
		peer_dev = &rte_eth_devices[peer_tx_port];
		cpfl_txq = peer_dev->data->tx_queues[peer_tx_qid];

		/* bind rx queue */
		mz = cpfl_txq->base.mz;
		cpfl_rxq->base.rx_ring_phys_addr = mz->iova;
		cpfl_rxq->base.rx_ring = mz->addr;
		cpfl_rxq->base.mz = mz;

		/* bind rx buffer queue */
		mz = cpfl_txq->base.complq->mz;
		cpfl_rxq->base.bufq1->rx_ring_phys_addr = mz->iova;
		cpfl_rxq->base.bufq1->rx_ring = mz->addr;
		cpfl_rxq->base.bufq1->mz = mz;
		cpfl_rxq->base.bufq1->qrx_tail = hw->hw_addr +
			cpfl_hw_qtail_get(cpfl_rx_vport->p2p_q_chunks_info->rx_buf_qtail_start,
					0, cpfl_rx_vport->p2p_q_chunks_info->rx_buf_qtail_spacing);
	}
}

static int
cpfl_rss_lut_config(struct cpfl_vport *cpfl_vport, uint16_t nb_q)
{
	struct idpf_vport *vport = &cpfl_vport->base;
	uint16_t lut_size = vport->rss_lut_size;
	uint16_t i;
	int ret;

	for (i = 0; i < lut_size; i++)
		vport->rss_lut[i] = i % nb_q;

	ret = idpf_vc_rss_lut_set(vport);
	if (ret)
		PMD_INIT_LOG(ERR, "Failed to configure RSS lut");

	return ret;
}

static int
cpfl_start_queues(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct cpfl_rx_queue *cpfl_rxq;
	struct cpfl_tx_queue *cpfl_txq;
	int update_flag = 0;
	int err = 0;
	int i;

	/* For normal data queues, configure, init and enale Txq.
	 * For non-manual bind hairpin queues, configure Txq.
	 */
	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		cpfl_txq = dev->data->tx_queues[i];
		if (cpfl_txq == NULL || cpfl_txq->base.tx_deferred_start)
			continue;
		if (!cpfl_txq->hairpin_info.hairpin_q) {
			err = cpfl_tx_queue_start(dev, i);
			if (err != 0) {
				PMD_DRV_LOG(ERR, "Fail to start Tx queue %u", i);
				return err;
			}
		} else if (!cpfl_vport->p2p_manual_bind) {
			if (update_flag == 0) {
				err = cpfl_txq_hairpin_info_update(dev,
								   cpfl_txq->hairpin_info.peer_rxp);
				if (err != 0) {
					PMD_DRV_LOG(ERR, "Fail to update Tx hairpin queue info");
					return err;
				}
				update_flag = 1;
			}
			err = cpfl_hairpin_txq_config(vport, cpfl_txq);
			if (err != 0) {
				PMD_DRV_LOG(ERR, "Fail to configure hairpin Tx queue %u", i);
				return err;
			}
		}
	}

	/* For non-manual bind hairpin queues, configure Tx completion queue first.*/
	if (!cpfl_vport->p2p_manual_bind && cpfl_vport->p2p_tx_complq != NULL) {
		err = cpfl_hairpin_tx_complq_config(cpfl_vport);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Fail to config Tx completion queue");
			return err;
		}
	}

	/* For non-manual bind hairpin queues, configure Rx buffer queue.*/
	if (!cpfl_vport->p2p_manual_bind && cpfl_vport->p2p_rx_bufq != NULL) {
		cpfl_rxq_hairpin_mz_bind(dev);
		err = cpfl_hairpin_rx_bufq_config(cpfl_vport);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Fail to config Rx buffer queue");
			return err;
		}
	}

	/* For normal data queues, configure, init and enale Rxq.
	 * For non-manual bind hairpin queues, configure Rxq, and then init Rxq.
	 */
	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		if (cpfl_rxq == NULL || cpfl_rxq->base.rx_deferred_start)
			continue;
		if (!cpfl_rxq->hairpin_info.hairpin_q) {
			err = cpfl_rx_queue_start(dev, i);
			if (err != 0) {
				PMD_DRV_LOG(ERR, "Fail to start Rx queue %u", i);
				return err;
			}
		} else if (!cpfl_vport->p2p_manual_bind) {
			err = cpfl_hairpin_rxq_config(vport, cpfl_rxq);
			if (err != 0) {
				PMD_DRV_LOG(ERR, "Fail to configure hairpin Rx queue %u", i);
				return err;
			}
			err = cpfl_rx_queue_init(dev, i);
			if (err != 0) {
				PMD_DRV_LOG(ERR, "Fail to init hairpin Rx queue %u", i);
				return err;
			}
		}
	}

	/* For non-manual bind hairpin queues, enable Tx queue and Rx queue,
	 * then enable Tx completion queue and Rx buffer queue.
	 */
	for (i = cpfl_vport->nb_data_txq; i < dev->data->nb_tx_queues; i++) {
		cpfl_txq = dev->data->tx_queues[i];
		if (cpfl_txq->hairpin_info.hairpin_q && !cpfl_vport->p2p_manual_bind) {
			err = cpfl_switch_hairpin_rxtx_queue(cpfl_vport,
							     i - cpfl_vport->nb_data_txq,
							     false, true);
			if (err)
				PMD_DRV_LOG(ERR, "Failed to switch hairpin TX queue %u on",
					    i);
		}
	}

	for (i = cpfl_vport->nb_data_rxq; i < dev->data->nb_rx_queues; i++) {
		cpfl_rxq = dev->data->rx_queues[i];
		if (cpfl_rxq->hairpin_info.hairpin_q && !cpfl_vport->p2p_manual_bind) {
			err = cpfl_switch_hairpin_rxtx_queue(cpfl_vport,
							     i - cpfl_vport->nb_data_rxq,
							     true, true);
			if (err)
				PMD_DRV_LOG(ERR, "Failed to switch hairpin RX queue %u on",
					    i);
			else
				cpfl_rxq->base.q_started = true;
		}
	}

	if (!cpfl_vport->p2p_manual_bind &&
	    cpfl_vport->p2p_tx_complq != NULL &&
	    cpfl_vport->p2p_rx_bufq != NULL) {
		err = cpfl_switch_hairpin_complq(cpfl_vport, true);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Failed to switch hairpin Tx complq");
			return err;
		}
		err = cpfl_switch_hairpin_bufq(cpfl_vport, true);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Failed to switch hairpin Rx bufq");
			return err;
		}
	}

	/* re-configure RSS lut if there's hairpin queue */
	if (cpfl_vport->nb_p2p_rxq > 0)
		err = cpfl_rss_lut_config(cpfl_vport, cpfl_vport->nb_data_rxq);

	return err;
}

static int
cpfl_dev_start(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct idpf_adapter *base = vport->adapter;
	struct cpfl_adapter_ext *adapter = CPFL_ADAPTER_TO_EXT(base);
	uint16_t num_allocated_vectors = base->caps.num_allocated_vectors;
	uint16_t req_vecs_num;
	int ret;

	req_vecs_num = CPFL_DFLT_Q_VEC_NUM;
	if (req_vecs_num + adapter->used_vecs_num > num_allocated_vectors) {
		PMD_DRV_LOG(ERR, "The accumulated request vectors' number should be less than %d",
			    num_allocated_vectors);
		ret = -EINVAL;
		goto err_vec;
	}

	ret = idpf_vc_vectors_alloc(vport, req_vecs_num);
	if (ret != 0) {
		PMD_DRV_LOG(ERR, "Failed to allocate interrupt vectors");
		goto err_vec;
	}
	adapter->used_vecs_num += req_vecs_num;

	ret = cpfl_config_rx_queues_irqs(dev);
	if (ret != 0) {
		PMD_DRV_LOG(ERR, "Failed to configure irqs");
		goto err_irq;
	}

	ret = cpfl_start_queues(dev);
	if (ret != 0) {
		PMD_DRV_LOG(ERR, "Failed to start queues");
		goto err_startq;
	}

	cpfl_set_rx_function(dev);
	cpfl_set_tx_function(dev);

	ret = idpf_vc_vport_ena_dis(vport, true);
	if (ret != 0) {
		PMD_DRV_LOG(ERR, "Failed to enable vport");
		goto err_vport;
	}

	if (cpfl_dev_stats_reset(dev))
		PMD_DRV_LOG(ERR, "Failed to reset stats");

	return 0;

err_vport:
	cpfl_stop_queues(dev);
err_startq:
	idpf_vport_irq_unmap_config(vport, dev->data->nb_rx_queues);
err_irq:
	idpf_vc_vectors_dealloc(vport);
err_vec:
	return ret;
}

static int
cpfl_dev_stop(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;

	if (dev->data->dev_started == 0)
		return 0;

	idpf_vc_vport_ena_dis(vport, false);

	cpfl_stop_queues(dev);

	idpf_vport_irq_unmap_config(vport, dev->data->nb_rx_queues);

	idpf_vc_vectors_dealloc(vport);

	return 0;
}

static void
cpfl_flow_free(struct cpfl_vport *vport)
{
	struct rte_flow *p_flow;

	while ((p_flow = TAILQ_FIRST(&vport->itf.flow_list))) {
		TAILQ_REMOVE(&vport->itf.flow_list, p_flow, next);
		if (p_flow->engine->free)
			p_flow->engine->free(p_flow);
		rte_free(p_flow);
	}
}

static int
cpfl_p2p_queue_grps_del(struct idpf_vport *vport)
{
	struct virtchnl2_queue_group_id qg_ids;
	int ret = 0;

	memset(&qg_ids, 0, sizeof(qg_ids));
	qg_ids.queue_group_id = CPFL_P2P_QUEUE_GRP_ID;
	qg_ids.queue_group_type = VIRTCHNL2_QUEUE_GROUP_P2P;
	ret = idpf_vc_queue_grps_del(vport, CPFL_P2P_NB_QUEUE_GRPS, &qg_ids);
	if (ret)
		PMD_DRV_LOG(ERR, "Failed to delete p2p queue groups");
	return ret;
}

static int
cpfl_dev_close(struct rte_eth_dev *dev)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct cpfl_adapter_ext *adapter = CPFL_ADAPTER_TO_EXT(vport->adapter);

	cpfl_dev_stop(dev);
	if (cpfl_vport->p2p_mp) {
		rte_mempool_free(cpfl_vport->p2p_mp);
		cpfl_vport->p2p_mp = NULL;
	}

	if (!adapter->base.is_rx_singleq && !adapter->base.is_tx_singleq)
		cpfl_p2p_queue_grps_del(vport);

	cpfl_flow_free(cpfl_vport);
	idpf_vport_deinit(vport);
	rte_free(cpfl_vport->p2p_q_chunks_info);

	adapter->cur_vports &= ~RTE_BIT32(vport->devarg_id);
	adapter->cur_vport_nb--;
	dev->data->dev_private = NULL;
	adapter->vports[vport->sw_idx] = NULL;
	idpf_free_dma_mem(NULL, &cpfl_vport->itf.flow_dma);
	rte_free(cpfl_vport);

	return 0;
}

static int
cpfl_dev_flow_ops_get(struct rte_eth_dev *dev,
		      const struct rte_flow_ops **ops)
{
	struct cpfl_itf *itf;

	if (!dev)
		return -EINVAL;

	itf = CPFL_DEV_TO_ITF(dev);

	/* only vport support rte_flow */
	if (itf->type != CPFL_ITF_TYPE_VPORT)
		return -ENOTSUP;
#ifdef RTE_HAS_JANSSON
	*ops = &cpfl_flow_ops;
#else
	*ops = NULL;
	PMD_DRV_LOG(NOTICE, "not support rte_flow, please install json-c library.");
#endif
	return 0;
}

static int
cpfl_hairpin_get_peer_ports(struct rte_eth_dev *dev, uint16_t *peer_ports,
			    size_t len, uint32_t tx)
{
	struct cpfl_vport *cpfl_vport =
		(struct cpfl_vport *)dev->data->dev_private;
	struct ci_tx_queue *txq;
	struct idpf_rx_queue *rxq;
	struct cpfl_tx_queue *cpfl_txq;
	struct cpfl_rx_queue *cpfl_rxq;
	uint16_t i;
	uint16_t j = 0;

	if (len <= 0)
		return -EINVAL;

	if (cpfl_vport->p2p_q_chunks_info == NULL)
		return -ENOTSUP;

	if (tx > 0) {
		for (i = cpfl_vport->nb_data_txq, j = 0; i < dev->data->nb_tx_queues; i++, j++) {
			txq = dev->data->tx_queues[i];
			if (txq == NULL || j >= len)
				return -EINVAL;
			cpfl_txq = (struct cpfl_tx_queue *)txq;
			peer_ports[j] = cpfl_txq->hairpin_info.peer_rxp;
		}
	} else if (tx == 0) {
		for (i = cpfl_vport->nb_data_rxq, j = 0; i < dev->data->nb_rx_queues; i++, j++) {
			rxq = dev->data->rx_queues[i];
			if (rxq == NULL || j >= len)
				return -EINVAL;
			cpfl_rxq = (struct cpfl_rx_queue *)rxq;
			peer_ports[j] = cpfl_rxq->hairpin_info.peer_txp;
		}
	}

	return j;
}

static int
cpfl_hairpin_bind(struct rte_eth_dev *dev, uint16_t rx_port)
{
	struct cpfl_vport *cpfl_tx_vport = dev->data->dev_private;
	struct idpf_vport *tx_vport = &cpfl_tx_vport->base;
	struct cpfl_vport *cpfl_rx_vport;
	struct cpfl_tx_queue *cpfl_txq;
	struct cpfl_rx_queue *cpfl_rxq;
	struct rte_eth_dev *peer_dev;
	struct idpf_vport *rx_vport;
	int err = 0;
	int i;

	err = cpfl_txq_hairpin_info_update(dev, rx_port);
	if (err != 0) {
		PMD_DRV_LOG(ERR, "Fail to update Tx hairpin queue info.");
		return err;
	}

	/* configure hairpin queues */
	for (i = cpfl_tx_vport->nb_data_txq; i < dev->data->nb_tx_queues; i++) {
		cpfl_txq = dev->data->tx_queues[i];
		err = cpfl_hairpin_txq_config(tx_vport, cpfl_txq);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Fail to configure hairpin Tx queue %u", i);
			return err;
		}
	}

	err = cpfl_hairpin_tx_complq_config(cpfl_tx_vport);
	if (err != 0) {
		PMD_DRV_LOG(ERR, "Fail to config Tx completion queue");
		return err;
	}

	peer_dev = &rte_eth_devices[rx_port];
	cpfl_rx_vport = (struct cpfl_vport *)peer_dev->data->dev_private;
	rx_vport = &cpfl_rx_vport->base;
	cpfl_rxq_hairpin_mz_bind(peer_dev);

	err = cpfl_hairpin_rx_bufq_config(cpfl_rx_vport);
	if (err != 0) {
		PMD_DRV_LOG(ERR, "Fail to config Rx buffer queue");
		return err;
	}

	for (i = cpfl_rx_vport->nb_data_rxq; i < peer_dev->data->nb_rx_queues; i++) {
		cpfl_rxq = peer_dev->data->rx_queues[i];
		err = cpfl_hairpin_rxq_config(rx_vport, cpfl_rxq);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Fail to configure hairpin Rx queue %u", i);
			return err;
		}
		err = cpfl_rx_queue_init(peer_dev, i);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Fail to init hairpin Rx queue %u", i);
			return err;
		}
	}

	/* enable hairpin queues */
	for (i = cpfl_tx_vport->nb_data_txq; i < dev->data->nb_tx_queues; i++) {
		cpfl_txq = dev->data->tx_queues[i];
		err = cpfl_switch_hairpin_rxtx_queue(cpfl_tx_vport,
						     i - cpfl_tx_vport->nb_data_txq,
						     false, true);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Failed to switch hairpin TX queue %u on",
				    i);
			return err;
		}
	}

	err = cpfl_switch_hairpin_complq(cpfl_tx_vport, true);
	if (err != 0) {
		PMD_DRV_LOG(ERR, "Failed to switch hairpin Tx complq");
		return err;
	}

	for (i = cpfl_rx_vport->nb_data_rxq; i < peer_dev->data->nb_rx_queues; i++) {
		cpfl_rxq = peer_dev->data->rx_queues[i];
		err = cpfl_switch_hairpin_rxtx_queue(cpfl_rx_vport,
						     i - cpfl_rx_vport->nb_data_rxq,
						     true, true);
		if (err != 0) {
			PMD_DRV_LOG(ERR, "Failed to switch hairpin RX queue %u on",
				    i);
		}
		cpfl_rxq->base.q_started = true;
	}

	err = cpfl_switch_hairpin_bufq(cpfl_rx_vport, true);
	if (err != 0) {
		PMD_DRV_LOG(ERR, "Failed to switch hairpin Rx buffer queue");
		return err;
	}

	return 0;
}

static int
cpfl_hairpin_unbind(struct rte_eth_dev *dev, uint16_t rx_port)
{
	struct cpfl_vport *cpfl_tx_vport = dev->data->dev_private;
	struct rte_eth_dev *peer_dev = &rte_eth_devices[rx_port];
	struct cpfl_vport *cpfl_rx_vport = peer_dev->data->dev_private;
	struct cpfl_rx_queue *cpfl_rxq;
	int i;

	/* disable hairpin queues */
	for (i = cpfl_tx_vport->nb_data_txq; i < dev->data->nb_tx_queues; i++) {
		cpfl_switch_hairpin_rxtx_queue(cpfl_tx_vport,
					       i - cpfl_tx_vport->nb_data_txq,
					       false, false);
	}

	cpfl_switch_hairpin_complq(cpfl_tx_vport, false);

	for (i = cpfl_rx_vport->nb_data_rxq; i < peer_dev->data->nb_rx_queues; i++) {
		cpfl_rxq = peer_dev->data->rx_queues[i];
		cpfl_switch_hairpin_rxtx_queue(cpfl_rx_vport,
					       i - cpfl_rx_vport->nb_data_rxq,
					       true, false);
		cpfl_rxq->base.q_started = false;
	}

	cpfl_switch_hairpin_bufq(cpfl_rx_vport, false);

	return 0;
}

static const struct eth_dev_ops cpfl_eth_dev_ops = {
	.dev_configure			= cpfl_dev_configure,
	.dev_close			= cpfl_dev_close,
	.rx_queue_setup			= cpfl_rx_queue_setup,
	.tx_queue_setup			= cpfl_tx_queue_setup,
	.dev_infos_get			= cpfl_dev_info_get,
	.dev_start			= cpfl_dev_start,
	.dev_stop			= cpfl_dev_stop,
	.link_update			= cpfl_dev_link_update,
	.rx_queue_start			= cpfl_rx_queue_start,
	.tx_queue_start			= cpfl_tx_queue_start,
	.rx_queue_stop			= cpfl_rx_queue_stop,
	.tx_queue_stop			= cpfl_tx_queue_stop,
	.rx_queue_release		= cpfl_dev_rx_queue_release,
	.tx_queue_release		= cpfl_dev_tx_queue_release,
	.mtu_set			= cpfl_dev_mtu_set,
	.dev_supported_ptypes_get	= cpfl_dev_supported_ptypes_get,
	.stats_get			= cpfl_dev_stats_get,
	.stats_reset			= cpfl_dev_stats_reset,
	.reta_update			= cpfl_rss_reta_update,
	.reta_query			= cpfl_rss_reta_query,
	.rss_hash_update		= cpfl_rss_hash_update,
	.rss_hash_conf_get		= cpfl_rss_hash_conf_get,
	.xstats_get			= cpfl_dev_xstats_get,
	.xstats_get_names		= cpfl_dev_xstats_get_names,
	.xstats_reset			= cpfl_dev_xstats_reset,
	.flow_ops_get			= cpfl_dev_flow_ops_get,
	.hairpin_cap_get		= cpfl_hairpin_cap_get,
	.rx_hairpin_queue_setup		= cpfl_rx_hairpin_queue_setup,
	.tx_hairpin_queue_setup		= cpfl_tx_hairpin_queue_setup,
	.hairpin_get_peer_ports         = cpfl_hairpin_get_peer_ports,
	.hairpin_bind                   = cpfl_hairpin_bind,
	.hairpin_unbind                 = cpfl_hairpin_unbind,
};

static int
insert_value(struct cpfl_devargs *devargs, uint16_t id)
{
	uint16_t i;

	/* ignore duplicate */
	for (i = 0; i < devargs->req_vport_nb; i++) {
		if (devargs->req_vports[i] == id)
			return 0;
	}

	devargs->req_vports[devargs->req_vport_nb] = id;
	devargs->req_vport_nb++;

	return 0;
}

static const char *
parse_range(const char *value, struct cpfl_devargs *devargs)
{
	uint16_t lo, hi, i;
	int n = 0;
	int result;
	const char *pos = value;

	result = sscanf(value, "%hu%n-%hu%n", &lo, &n, &hi, &n);
	if (result == 1) {
		if (insert_value(devargs, lo) != 0)
			return NULL;
	} else if (result == 2) {
		if (lo > hi)
			return NULL;
		for (i = lo; i <= hi; i++) {
			if (insert_value(devargs, i) != 0)
				return NULL;
		}
	} else {
		return NULL;
	}

	return pos + n;
}

static int
parse_vport(const char *key, const char *value, void *args)
{
	struct cpfl_devargs *devargs = args;
	const char *pos = value;

	devargs->req_vport_nb = 0;

	if (*pos == '[')
		pos++;

	while (1) {
		pos = parse_range(pos, devargs);
		if (pos == NULL) {
			PMD_INIT_LOG(ERR, "invalid value:\"%s\" for key:\"%s\", ",
				     value, key);
			return -EINVAL;
		}
		if (*pos != ',')
			break;
		pos++;
	}

	if (*value == '[' && *pos != ']') {
		PMD_INIT_LOG(ERR, "invalid value:\"%s\" for key:\"%s\", ",
			     value, key);
		return -EINVAL;
	}

	return 0;
}

static int
parse_bool(const char *key, const char *value, void *args)
{
	int *i = args;
	char *end;
	int num;

	errno = 0;

	num = strtoul(value, &end, 10);

	if (errno == ERANGE || (num != 0 && num != 1)) {
		PMD_INIT_LOG(ERR, "invalid value:\"%s\" for key:\"%s\", value must be 0 or 1",
			value, key);
		return -EINVAL;
	}

	*i = num;
	return 0;
}

static int
enlist(uint16_t *list, uint16_t *len_list, const uint16_t max_list, uint16_t val)
{
	uint16_t i;

	for (i = 0; i < *len_list; i++) {
		if (list[i] == val)
			return 0;
	}
	if (*len_list >= max_list)
		return -1;
	list[(*len_list)++] = val;
	return 0;
}

static const char *
process_range(const char *str, uint16_t *list, uint16_t *len_list,
	const uint16_t max_list)
{
	uint16_t lo, hi, val;
	int result, n = 0;
	const char *pos = str;

	result = sscanf(str, "%hu%n-%hu%n", &lo, &n, &hi, &n);
	if (result == 1) {
		if (enlist(list, len_list, max_list, lo) != 0)
			return NULL;
	} else if (result == 2) {
		if (lo > hi)
			return NULL;
		for (val = lo; val <= hi; val++) {
			if (enlist(list, len_list, max_list, val) != 0)
				return NULL;
		}
	} else {
		return NULL;
	}
	return pos + n;
}

static const char *
process_list(const char *str, uint16_t *list, uint16_t *len_list, const uint16_t max_list)
{
	const char *pos = str;

	if (*pos == '[')
		pos++;
	while (1) {
		pos = process_range(pos, list, len_list, max_list);
		if (pos == NULL)
			return NULL;
		if (*pos != ',') /* end of list */
			break;
		pos++;
	}
	if (*str == '[' && *pos != ']')
		return NULL;
	if (*pos == ']')
		pos++;
	return pos;
}

static int
parse_repr(const char *key __rte_unused, const char *value, void *args)
{
	struct cpfl_devargs *devargs = args;
	struct rte_eth_devargs *eth_da;
	const char *str = value;

	if (devargs->repr_args_num == CPFL_REPR_ARG_NUM_MAX)
		return -EINVAL;

	eth_da = &devargs->repr_args[devargs->repr_args_num];

	if (str[0] == 'c') {
		str += 1;
		str = process_list(str, eth_da->mh_controllers,
				&eth_da->nb_mh_controllers,
				RTE_DIM(eth_da->mh_controllers));
		if (str == NULL)
			goto done;
	}
	if (str[0] == 'p' && str[1] == 'f') {
		eth_da->type = RTE_ETH_REPRESENTOR_PF;
		str += 2;
		str = process_list(str, eth_da->ports,
				&eth_da->nb_ports, RTE_DIM(eth_da->ports));
		if (str == NULL || str[0] == '\0')
			goto done;
	} else if (eth_da->nb_mh_controllers > 0) {
		/* 'c' must followed by 'pf'. */
		str = NULL;
		goto done;
	}
	if (str[0] == 'v' && str[1] == 'f') {
		eth_da->type = RTE_ETH_REPRESENTOR_VF;
		str += 2;
	} else if (str[0] == 's' && str[1] == 'f') {
		eth_da->type = RTE_ETH_REPRESENTOR_SF;
		str += 2;
	} else {
		/* 'pf' must followed by 'vf' or 'sf'. */
		if (eth_da->type == RTE_ETH_REPRESENTOR_PF) {
			str = NULL;
			goto done;
		}
		eth_da->type = RTE_ETH_REPRESENTOR_VF;
	}
	str = process_list(str, eth_da->representor_ports,
		&eth_da->nb_representor_ports,
		RTE_DIM(eth_da->representor_ports));
done:
	if (str == NULL) {
		PMD_DRV_LOG(ERR, "wrong representor format: %s", value);
		return -1;
	}

	devargs->repr_args_num++;

	return 0;
}

#ifdef RTE_HAS_JANSSON
static int
parse_file(const char *key, const char *value, void *args)
{
	char *name = args;

	if (strlen(value) > CPFL_FLOW_FILE_LEN - 1) {
		PMD_DRV_LOG(ERR, "file path(%s) is too long.", value);
		return -1;
	}

	PMD_DRV_LOG(DEBUG, "value:\"%s\" for key:\"%s\"", value, key);
	strlcpy(name, value, CPFL_FLOW_FILE_LEN);

	return 0;
}
#endif

static int
cpfl_parse_devargs(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapter,
		   bool first, struct cpfl_devargs *cpfl_args)
{
	struct rte_devargs *devargs = pci_dev->device.devargs;
	struct rte_kvargs *kvlist;
	int ret;

	if (devargs == NULL)
		return 0;

	kvlist = rte_kvargs_parse(devargs->args,
			first ? cpfl_valid_args_first : cpfl_valid_args_again);
	if (kvlist == NULL) {
		PMD_INIT_LOG(ERR, "invalid kvargs key");
		return -EINVAL;
	}

	if (rte_kvargs_count(kvlist, CPFL_VPORT) > 1) {
		PMD_INIT_LOG(ERR, "devarg vport is duplicated.");
		ret = -EINVAL;
		goto fail;
	}

	ret = rte_kvargs_process(kvlist, CPFL_REPRESENTOR, &parse_repr, cpfl_args);

	if (ret != 0)
		goto fail;

	if (!first)
		goto finish;

	ret = rte_kvargs_process(kvlist, CPFL_VPORT, &parse_vport,
				 cpfl_args);
	if (ret != 0)
		goto fail;

	ret = rte_kvargs_process(kvlist, CPFL_TX_SINGLE_Q, &parse_bool,
				 &adapter->base.is_tx_singleq);
	if (ret != 0)
		goto fail;

	ret = rte_kvargs_process(kvlist, CPFL_RX_SINGLE_Q, &parse_bool,
				 &adapter->base.is_rx_singleq);
	if (ret != 0)
		goto fail;
#ifdef RTE_HAS_JANSSON
	if (rte_kvargs_get(kvlist, CPFL_FLOW_PARSER)) {
		ret = rte_kvargs_process(kvlist, CPFL_FLOW_PARSER,
					 &parse_file, cpfl_args->flow_parser);
		if (ret) {
			PMD_DRV_LOG(ERR, "Failed to parser flow_parser, ret: %d", ret);
			goto fail;
		}
	} else {
		cpfl_args->flow_parser[0] = '\0';
	}
#endif
finish:
fail:
	rte_kvargs_free(kvlist);
	return ret;
}

static struct cpfl_vport *
cpfl_find_vport(struct cpfl_adapter_ext *adapter, uint32_t vport_id)
{
	struct cpfl_vport *vport = NULL;
	int i;

	for (i = 0; i < adapter->cur_vport_nb; i++) {
		vport = adapter->vports[i];
		if (vport == NULL)
			continue;
		if (vport->base.vport_id != vport_id)
			continue;
		else
			return vport;
	}

	return NULL;
}

static void
cpfl_handle_vchnl_event_msg(struct cpfl_adapter_ext *adapter, uint8_t *msg, uint16_t msglen)
{
	struct virtchnl2_event *vc_event = (struct virtchnl2_event *)msg;
	struct cpfl_vport *vport;
	struct rte_eth_dev_data *data;
	struct rte_eth_dev *dev;

	if (msglen < sizeof(struct virtchnl2_event)) {
		PMD_DRV_LOG(ERR, "Error event");
		return;
	}

	/* ignore if it is ctrl vport */
	if (adapter->base.hw.device_id == CPFL_DEV_ID_MEV &&
			adapter->ctrl_vport.base.vport_id == vc_event->vport_id)
		return;

	vport = cpfl_find_vport(adapter, vc_event->vport_id);
	if (!vport) {
		PMD_DRV_LOG(ERR, "Can't find vport.");
		return;
	}

	data = vport->itf.data;
	dev = &rte_eth_devices[data->port_id];

	switch (vc_event->event) {
	case VIRTCHNL2_EVENT_LINK_CHANGE:
		PMD_DRV_LOG(DEBUG, "VIRTCHNL2_EVENT_LINK_CHANGE");
		vport->base.link_up = !!(vc_event->link_status);
		vport->base.link_speed = vc_event->link_speed;
		cpfl_dev_link_update(dev, 0);
		break;
	default:
		PMD_DRV_LOG(ERR, " unknown event received %u", vc_event->event);
		break;
	}
}

int
cpfl_vport_info_create(struct cpfl_adapter_ext *adapter,
		       struct cpfl_vport_id *vport_identity,
		       struct cpchnl2_event_vport_created *vport_created)
{
	struct cpfl_vport_info *info = NULL;
	int ret;

	rte_spinlock_lock(&adapter->vport_map_lock);
	ret = rte_hash_lookup_data(adapter->vport_map_hash, vport_identity, (void **)&info);
	if (ret >= 0) {
		PMD_DRV_LOG(WARNING, "vport already exist, overwrite info anyway");
		/* overwrite info */
		if (info)
			info->vport = *vport_created;
		goto fini;
	}

	info = rte_zmalloc(NULL, sizeof(*info), 0);
	if (info == NULL) {
		PMD_DRV_LOG(ERR, "Failed to alloc memory for vport map info");
		ret = -ENOMEM;
		goto err;
	}

	info->vport = *vport_created;

	ret = rte_hash_add_key_data(adapter->vport_map_hash, vport_identity, info);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to add vport map into hash");
		rte_free(info);
		goto err;
	}

fini:
	rte_spinlock_unlock(&adapter->vport_map_lock);
	return 0;
err:
	rte_spinlock_unlock(&adapter->vport_map_lock);
	return ret;
}

static int
cpfl_vport_info_destroy(struct cpfl_adapter_ext *adapter, struct cpfl_vport_id *vport_identity)
{
	struct cpfl_vport_info *info;
	int ret;

	rte_spinlock_lock(&adapter->vport_map_lock);
	ret = rte_hash_lookup_data(adapter->vport_map_hash, vport_identity, (void **)&info);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "vport id doesn't exist");
		goto err;
	}

	rte_hash_del_key(adapter->vport_map_hash, vport_identity);
	rte_spinlock_unlock(&adapter->vport_map_lock);
	rte_free(info);

	return 0;

err:
	rte_spinlock_unlock(&adapter->vport_map_lock);
	return ret;
}

static void
cpfl_handle_cpchnl_event_msg(struct cpfl_adapter_ext *adapter, uint8_t *msg, uint16_t msglen)
{
	struct cpchnl2_event_info *cpchnl2_event = (struct cpchnl2_event_info *)msg;
	struct cpchnl2_event_vport_created *vport_created;
	struct cpfl_vport_id vport_identity = { 0 };

	if (msglen < sizeof(struct cpchnl2_event_info)) {
		PMD_DRV_LOG(ERR, "Error event");
		return;
	}

	switch (cpchnl2_event->header.type) {
	case CPCHNL2_EVENT_VPORT_CREATED:
		vport_identity.vport_id = cpchnl2_event->data.vport_created.vport.vport_id;
		vport_created = &cpchnl2_event->data.vport_created;
		vport_identity.func_type = vport_created->info.func_type;
		vport_identity.pf_id = vport_created->info.pf_id;
		vport_identity.vf_id = vport_created->info.vf_id;
		if (cpfl_vport_info_create(adapter, &vport_identity, vport_created))
			PMD_DRV_LOG(WARNING, "Failed to handle CPCHNL2_EVENT_VPORT_CREATED");
		break;
	case CPCHNL2_EVENT_VPORT_DESTROYED:
		vport_identity.vport_id = cpchnl2_event->data.vport_destroyed.vport.vport_id;
		vport_identity.func_type = cpchnl2_event->data.vport_destroyed.func.func_type;
		vport_identity.pf_id = cpchnl2_event->data.vport_destroyed.func.pf_id;
		vport_identity.vf_id = cpchnl2_event->data.vport_destroyed.func.vf_id;
		if (cpfl_vport_info_destroy(adapter, &vport_identity))
			PMD_DRV_LOG(WARNING, "Failed to handle CPCHNL2_EVENT_VPORT_DESTROY");
		break;
	default:
		PMD_DRV_LOG(ERR, " unknown event received %u", cpchnl2_event->header.type);
		break;
	}
}

static void
cpfl_handle_virtchnl_msg(struct cpfl_adapter_ext *adapter)
{
	struct idpf_adapter *base = &adapter->base;
	struct idpf_dma_mem *dma_mem = NULL;
	struct idpf_hw *hw = &base->hw;
	struct idpf_ctlq_msg ctlq_msg;
	enum idpf_mbx_opc mbx_op;
	uint16_t pending = 1;
	uint32_t vc_op;
	int ret;

	while (pending) {
		ret = idpf_vc_ctlq_recv(hw->arq, &pending, &ctlq_msg);
		if (ret) {
			PMD_DRV_LOG(INFO, "Failed to read msg from virtual channel, ret: %d", ret);
			return;
		}

		memcpy(base->mbx_resp, ctlq_msg.ctx.indirect.payload->va,
			   IDPF_DFLT_MBX_BUF_SIZE);

		mbx_op = rte_le_to_cpu_16(ctlq_msg.opcode);
		vc_op = rte_le_to_cpu_32(ctlq_msg.cookie.mbx.chnl_opcode);
		base->cmd_retval = rte_le_to_cpu_32(ctlq_msg.cookie.mbx.chnl_retval);

		switch (mbx_op) {
		case idpf_mbq_opc_send_msg_to_peer_pf:
		case idpf_mbq_opc_send_msg_to_peer_drv:
			if (vc_op == VIRTCHNL2_OP_EVENT) {
				cpfl_handle_vchnl_event_msg(adapter, adapter->base.mbx_resp,
							    ctlq_msg.data_len);
			} else if (vc_op == CPCHNL2_OP_EVENT) {
				cpfl_handle_cpchnl_event_msg(adapter, adapter->base.mbx_resp,
							     ctlq_msg.data_len);
			} else {
				if (vc_op == base->pend_cmd)
					notify_cmd(base, base->cmd_retval);
				else
					PMD_DRV_LOG(ERR, "command mismatch, expect %u, get %u",
						    base->pend_cmd, vc_op);

				PMD_DRV_LOG(DEBUG, " Virtual channel response is received,"
					    "opcode = %d", vc_op);
			}
			goto post_buf;
		default:
			PMD_DRV_LOG(DEBUG, "Request %u is not supported yet", mbx_op);
		}
	}

post_buf:
	if (ctlq_msg.data_len)
		dma_mem = ctlq_msg.ctx.indirect.payload;
	else
		pending = 0;

	ret = idpf_vc_ctlq_post_rx_buffs(hw, hw->arq, &pending, &dma_mem);
	if (ret && dma_mem)
		idpf_free_dma_mem(hw, dma_mem);
}

static void
cpfl_dev_alarm_handler(void *param)
{
	struct cpfl_adapter_ext *adapter = param;

	cpfl_handle_virtchnl_msg(adapter);

	rte_eal_alarm_set(CPFL_ALARM_INTERVAL, cpfl_dev_alarm_handler, adapter);
}

static
int vcpf_save_vport_info_response(struct cpfl_vport *cpfl_vport,
		struct cpchnl2_get_vport_info_response *response)
{
	struct cpchnl2_vport_info *info;
	struct vcpf_vport_info *vport_info;
	struct cpchnl2_queue_group_info *qgp;
	struct cpchnl2_queue_chunk *q_chnk;
	u16 num_queue_groups;
	u16 num_chunks;
	u32 q_type;

	info = &response->info;
	vport_info = &cpfl_vport->vport_info;
	vport_info->vport_index = info->vport_index;
	vport_info->vsi_id = info->vsi_id;

	num_queue_groups = response->queue_groups.num_queue_groups;
	for (u16 i = 0; i < num_queue_groups; i++) {
		qgp = &response->queue_groups.groups[i];
		num_chunks = qgp->chunks.num_chunks;
		/* rx q and tx q are stored in first 2 chunks */
		for (u16 j = 0; j < (num_chunks - 2); j++) {
			q_chnk = &qgp->chunks.chunks[j];
			q_type = q_chnk->type;
			if (q_type == VIRTCHNL2_QUEUE_TYPE_TX) {
				vport_info->abs_start_txq_id = q_chnk->start_queue_id;
				vport_info->num_tx_q = q_chnk->num_queues;
			} else if (q_type == VIRTCHNL2_QUEUE_TYPE_RX) {
				vport_info->abs_start_rxq_id = q_chnk->start_queue_id;
				vport_info->num_rx_q = q_chnk->num_queues;
			}
		}
	}
	return 0;
}

static int
cpfl_stop_cfgqs(struct cpfl_adapter_ext *adapter)
{
	int i, ret;

	for (i = 0; i < adapter->num_tx_cfgq; i++) {
		if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
			ret = idpf_vc_ena_dis_one_queue_vcpf(&adapter->base,
					adapter->cfgq_info[0].id,
					VIRTCHNL2_QUEUE_TYPE_CONFIG_TX, false);
		else
			ret = idpf_vc_queue_switch(&adapter->ctrl_vport.base, i, false, false,
								VIRTCHNL2_QUEUE_TYPE_CONFIG_TX);

		if (ret) {
			PMD_DRV_LOG(ERR, "Fail to disable Tx config queue.");
			return ret;
		}
	}

	for (i = 0; i < adapter->num_rx_cfgq; i++) {
		if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
			ret = idpf_vc_ena_dis_one_queue_vcpf(&adapter->base,
					adapter->cfgq_info[1].id,
					VIRTCHNL2_QUEUE_TYPE_CONFIG_RX, false);
		else
			ret = idpf_vc_queue_switch(&adapter->ctrl_vport.base, i, true, false,
							VIRTCHNL2_QUEUE_TYPE_CONFIG_RX);

		if (ret) {
			PMD_DRV_LOG(ERR, "Fail to disable Rx config queue.");
			return ret;
		}
	}

	return 0;

}

static int
cpfl_start_cfgqs(struct cpfl_adapter_ext *adapter)
{
	int i, ret;

	ret = cpfl_config_ctlq_tx(adapter);
	if (ret) {
		PMD_DRV_LOG(ERR, "Fail to configure Tx config queue.");
		return ret;
	}

	ret = cpfl_config_ctlq_rx(adapter);
	if (ret) {
		PMD_DRV_LOG(ERR, "Fail to configure Rx config queue.");
		return ret;
	}

	for (i = 0; i < adapter->num_tx_cfgq; i++) {
		if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
			ret = idpf_vc_ena_dis_one_queue_vcpf(&adapter->base,
					adapter->cfgq_info[0].id,
					VIRTCHNL2_QUEUE_TYPE_CONFIG_TX, true);
		else
			ret = idpf_vc_queue_switch(&adapter->ctrl_vport.base, i, false, true,
								VIRTCHNL2_QUEUE_TYPE_CONFIG_TX);
		if (ret) {
			PMD_DRV_LOG(ERR, "Fail to enable Tx config queue.");
			return ret;
		}
	}

	for (i = 0; i < adapter->num_rx_cfgq; i++) {
		if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
			ret = idpf_vc_ena_dis_one_queue_vcpf(&adapter->base,
					adapter->cfgq_info[1].id,
					VIRTCHNL2_QUEUE_TYPE_CONFIG_RX, true);
		else
			ret = idpf_vc_queue_switch(&adapter->ctrl_vport.base, i, true, true,
								VIRTCHNL2_QUEUE_TYPE_CONFIG_RX);
		if (ret) {
			PMD_DRV_LOG(ERR, "Fail to enable Rx config queue.");
			return ret;
		}
	}

	return 0;
}

static void
cpfl_remove_cfgqs(struct cpfl_adapter_ext *adapter)
{
	struct idpf_hw *hw = (struct idpf_hw *)(&adapter->base.hw);
	struct cpfl_ctlq_create_info *create_cfgq_info;
	int i;

	create_cfgq_info = adapter->cfgq_info;

	for (i = 0; i < adapter->num_cfgq; i++) {
		if (adapter->ctlqp[i]) {
			cpfl_vport_ctlq_remove(hw, adapter->ctlqp[i]);
			adapter->ctlqp[i] = NULL;
		}
		if (create_cfgq_info[i].ring_mem.va)
			idpf_free_dma_mem(&adapter->base.hw, &create_cfgq_info[i].ring_mem);
		if (create_cfgq_info[i].buf_mem.va)
			idpf_free_dma_mem(&adapter->base.hw, &create_cfgq_info[i].buf_mem);
	}
	if (adapter->ctlqp) {
		rte_free(adapter->ctlqp);
		adapter->ctlqp = NULL;
	}
}

static int
cpfl_add_cfgqs(struct cpfl_adapter_ext *adapter)
{
	struct idpf_ctlq_info *cfg_cq;
	int ret = 0;
	int i = 0;

	adapter->ctlqp = rte_zmalloc("ctlqp", adapter->num_cfgq *
				sizeof(struct idpf_ctlq_info *),
				RTE_CACHE_LINE_SIZE);

	if (!adapter->ctlqp) {
		PMD_DRV_LOG(ERR, "Failed to allocate memory for control queues");
		return -ENOMEM;
	}

	for (i = 0; i < adapter->num_cfgq; i++) {
		cfg_cq = NULL;
		ret = cpfl_vport_ctlq_add((struct idpf_hw *)(&adapter->base.hw),
					  &adapter->cfgq_info[i],
					  &cfg_cq);
		if (ret || !cfg_cq) {
			PMD_DRV_LOG(ERR, "ctlq add failed for queue id: %d",
				    adapter->cfgq_info[i].id);
			cpfl_remove_cfgqs(adapter);
			return ret;
		}
		PMD_DRV_LOG(INFO, "added cfgq to hw. queue id: %d",
			    adapter->cfgq_info[i].id);
		adapter->ctlqp[i] = cfg_cq;
	}

	return ret;
}

static
int vcpf_save_chunk_in_cfgq(struct cpfl_adapter_ext *adapter)
{
	struct virtchnl2_add_queues *add_q =
		(struct virtchnl2_add_queues *)adapter->addq_recv_info;
	struct vcpf_cfg_queue *cfgq;
	struct virtchnl2_queue_reg_chunk *q_chnk;
	u16 rx, tx, num_chunks, num_q, struct_size;
	u32 q_id, q_type;

	rx = 0; tx = 0;

	cfgq = rte_zmalloc("cfgq", adapter->num_cfgq * sizeof(struct vcpf_cfg_queue),
				RTE_CACHE_LINE_SIZE);
	if (!cfgq) {
		PMD_DRV_LOG(ERR, "Failed to allocate memory for cfgq");
		return -ENOMEM;
	}

	struct_size = idpf_struct_size(add_q, chunks.chunks, (add_q->chunks.num_chunks - 1));
	adapter->cfgq_in.cfgq_add = rte_zmalloc("config_queues", struct_size, 0);
	if (!adapter->cfgq_in.cfgq_add) {
		PMD_DRV_LOG(ERR, "Failed to allocate memory for add_q");
		return -ENOMEM;
	}
	rte_memcpy(adapter->cfgq_in.cfgq_add, add_q, struct_size);

	num_chunks = add_q->chunks.num_chunks;
	for (u16 i = 0; i < num_chunks; i++) {
		num_q = add_q->chunks.chunks[i].num_queues;
		q_chnk = &add_q->chunks.chunks[i];
		for (u16 j = 0; j < num_q; j++) {
			if (rx > adapter->num_cfgq || tx > adapter->num_cfgq)
				break;
			q_id = q_chnk->start_queue_id + j;
			q_type = q_chnk->type;
			if (q_type == VIRTCHNL2_QUEUE_TYPE_MBX_TX) {
				cfgq[0].qid = q_id;
				cfgq[0].qtail_reg_start = q_chnk->qtail_reg_start;
				cfgq[0].qtail_reg_spacing = q_chnk->qtail_reg_spacing;
				q_chnk->type = VIRTCHNL2_QUEUE_TYPE_CONFIG_TX;
				tx++;
			} else if (q_type == VIRTCHNL2_QUEUE_TYPE_MBX_RX) {
				cfgq[1].qid = q_id;
				cfgq[1].qtail_reg_start = q_chnk->qtail_reg_start;
				cfgq[1].qtail_reg_spacing = q_chnk->qtail_reg_spacing;
				q_chnk->type = VIRTCHNL2_QUEUE_TYPE_CONFIG_RX;
				rx++;
			}
		}
	}

	adapter->cfgq_in.cfgq = cfgq;
	adapter->cfgq_in.num_cfgq = adapter->num_cfgq;

	return 0;
}

#define CPFL_CFGQ_RING_LEN		512
#define CPFL_CFGQ_DESCRIPTOR_SIZE	32
#define CPFL_CFGQ_BUFFER_SIZE		256
#define CPFL_CFGQ_RING_SIZE		512

static int
cpfl_cfgq_setup(struct cpfl_adapter_ext *adapter)
{
	struct cpfl_ctlq_create_info *create_cfgq_info;
	struct cpfl_vport *vport;
	struct vcpf_cfgq_info *cfgq_info = &adapter->cfgq_in;
	int i, err;
	uint32_t ring_size = CPFL_CFGQ_RING_SIZE * sizeof(struct idpf_ctlq_desc);
	uint32_t buf_size = CPFL_CFGQ_RING_SIZE * CPFL_CFGQ_BUFFER_SIZE;
	uint64_t tx_qtail_start;
	uint64_t rx_qtail_start;
	uint32_t tx_qtail_spacing;
	uint32_t rx_qtail_spacing;

	vport = &adapter->ctrl_vport;

	tx_qtail_start = vport->base.chunks_info.tx_qtail_start;
	tx_qtail_spacing = vport->base.chunks_info.tx_qtail_spacing;
	rx_qtail_start = vport->base.chunks_info.rx_qtail_start;
	rx_qtail_spacing = vport->base.chunks_info.rx_qtail_spacing;

	adapter->cfgq_info = rte_zmalloc("cfgq_info", adapter->num_cfgq *
				sizeof(struct cpfl_ctlq_create_info),
				RTE_CACHE_LINE_SIZE);

	if (!adapter->cfgq_info) {
		PMD_DRV_LOG(ERR, "Failed to allocate memory for cfgq_info");
		return -ENOMEM;
	}

	create_cfgq_info = adapter->cfgq_info;

	for (i = 0; i < adapter->num_cfgq; i++) {
		if (i % 2 == 0) {
		/* Setup Tx config queue */
			if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
				create_cfgq_info[i].id = cfgq_info->cfgq[i].qid;
			else
				create_cfgq_info[i].id = vport->base.chunks_info.tx_start_qid +
								i / 2;

			create_cfgq_info[i].type = IDPF_CTLQ_TYPE_CONFIG_TX;
			create_cfgq_info[i].len = CPFL_CFGQ_RING_SIZE;
			create_cfgq_info[i].buf_size = CPFL_CFGQ_BUFFER_SIZE;
			memset(&create_cfgq_info[i].reg, 0, sizeof(struct idpf_ctlq_reg));
			if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
				create_cfgq_info[i].reg.tail = cfgq_info->cfgq[i].qtail_reg_start;
			else
				create_cfgq_info[i].reg.tail = tx_qtail_start +
								i / 2 * tx_qtail_spacing;

		} else {
		/* Setup Rx config queue */
			if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
				create_cfgq_info[i].id = cfgq_info->cfgq[i].qid;
			else
				create_cfgq_info[i].id = vport->base.chunks_info.rx_start_qid +
								i / 2;

			create_cfgq_info[i].type = IDPF_CTLQ_TYPE_CONFIG_RX;
			create_cfgq_info[i].len = CPFL_CFGQ_RING_SIZE;
			create_cfgq_info[i].buf_size = CPFL_CFGQ_BUFFER_SIZE;
			memset(&create_cfgq_info[i].reg, 0, sizeof(struct idpf_ctlq_reg));
			if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF)
				create_cfgq_info[i].reg.tail = cfgq_info->cfgq[i].qtail_reg_start;
			else
				create_cfgq_info[i].reg.tail = rx_qtail_start +
								i / 2 * rx_qtail_spacing;


			if (!idpf_alloc_dma_mem(&adapter->base.hw, &create_cfgq_info[i].buf_mem,
						buf_size)) {
				err = -ENOMEM;
				goto free_mem;
			}
		}
		if (!idpf_alloc_dma_mem(&adapter->base.hw, &create_cfgq_info[i].ring_mem,
				ring_size)) {
			err = -ENOMEM;
			goto free_mem;
		}
	}

	return 0;
free_mem:
	for (i = 0; i < adapter->num_cfgq; i++) {
		if (create_cfgq_info[i].ring_mem.va)
			idpf_free_dma_mem(&adapter->base.hw, &create_cfgq_info[i].ring_mem);
		if (create_cfgq_info[i].buf_mem.va)
			idpf_free_dma_mem(&adapter->base.hw, &create_cfgq_info[i].buf_mem);
	}
	if (adapter->cfgq_info) {
		rte_free(adapter->cfgq_info);
		adapter->cfgq_info = NULL;
	}
	return err;
}

static int
cpfl_init_ctrl_vport(struct cpfl_adapter_ext *adapter)
{
	struct cpfl_vport *vport = &adapter->ctrl_vport;
	struct virtchnl2_create_vport *vport_info =
		(struct virtchnl2_create_vport *)adapter->ctrl_vport_recv_info;
	int i;

	vport->itf.adapter = adapter;
	vport->base.adapter = &adapter->base;
	vport->base.vport_id = vport_info->vport_id;

	for (i = 0; i < vport_info->chunks.num_chunks; i++) {
		if (vport_info->chunks.chunks[i].type == VIRTCHNL2_QUEUE_TYPE_TX) {
			vport->base.chunks_info.tx_start_qid =
				vport_info->chunks.chunks[i].start_queue_id;
			vport->base.chunks_info.tx_qtail_start =
			vport_info->chunks.chunks[i].qtail_reg_start;
			vport->base.chunks_info.tx_qtail_spacing =
			vport_info->chunks.chunks[i].qtail_reg_spacing;
		} else if (vport_info->chunks.chunks[i].type == VIRTCHNL2_QUEUE_TYPE_RX) {
			vport->base.chunks_info.rx_start_qid =
				vport_info->chunks.chunks[i].start_queue_id;
			vport->base.chunks_info.rx_qtail_start =
			vport_info->chunks.chunks[i].qtail_reg_start;
			vport->base.chunks_info.rx_qtail_spacing =
			vport_info->chunks.chunks[i].qtail_reg_spacing;
		} else {
			PMD_INIT_LOG(ERR, "Unsupported chunk type");
			return -EINVAL;
		}
	}

	return 0;
}

static void
cpfl_ctrl_path_close(struct cpfl_adapter_ext *adapter)
{
	cpfl_stop_cfgqs(adapter);
	cpfl_remove_cfgqs(adapter);
	if (adapter->base.hw.device_id == CPFL_DEV_ID_MEV)
		idpf_vc_vport_destroy(&adapter->ctrl_vport.base);
	else
		vcpf_del_queues(adapter);
}

static int
cpfl_ctrl_path_open(struct cpfl_adapter_ext *adapter)
{
	int ret;

	if (adapter->base.hw.device_id == CPFL_DEV_ID_MEV) {
		ret = cpfl_vc_create_ctrl_vport(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to create control vport");
			return ret;
		}

		ret = cpfl_init_ctrl_vport(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to init control vport");
			goto err_init_ctrl_vport;
		}
	} else {
		ret = vcpf_add_queues(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to add queues");
			return ret;
		}

		ret = vcpf_save_chunk_in_cfgq(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to save config queue chunk");
			return ret;
		}
	}

	ret = cpfl_cfgq_setup(adapter);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to setup control queues");
		if (adapter->base.hw.device_id == CPFL_DEV_ID_MEV)
			goto err_cfgq_setup;
		else
			goto err_del_cfg;
	}

	ret = cpfl_add_cfgqs(adapter);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to add control queues");
		goto err_add_cfgq;
	}

	ret = cpfl_start_cfgqs(adapter);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to start control queues");
		goto err_start_cfgqs;
	}

	return 0;

err_start_cfgqs:
	cpfl_stop_cfgqs(adapter);
err_add_cfgq:
	cpfl_remove_cfgqs(adapter);
err_cfgq_setup:
err_init_ctrl_vport:
	if (adapter->base.hw.device_id == CPFL_DEV_ID_MEV)
		idpf_vc_vport_destroy(&adapter->ctrl_vport.base);
err_del_cfg:
	vcpf_del_queues(adapter);

	return ret;

}

static struct virtchnl2_get_capabilities req_caps = {
	.csum_caps =
	VIRTCHNL2_CAP_TX_CSUM_L3_IPV4          |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_TCP      |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_UDP      |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_SCTP     |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_TCP      |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_UDP      |
	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_SCTP     |
	VIRTCHNL2_CAP_TX_CSUM_GENERIC          |
	VIRTCHNL2_CAP_RX_CSUM_L3_IPV4          |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP      |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP      |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_SCTP     |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP      |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP      |
	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_SCTP     |
	VIRTCHNL2_CAP_RX_CSUM_GENERIC,

	.rss_caps =
	VIRTCHNL2_CAP_RSS_IPV4_TCP             |
	VIRTCHNL2_CAP_RSS_IPV4_UDP             |
	VIRTCHNL2_CAP_RSS_IPV4_SCTP            |
	VIRTCHNL2_CAP_RSS_IPV4_OTHER           |
	VIRTCHNL2_CAP_RSS_IPV6_TCP             |
	VIRTCHNL2_CAP_RSS_IPV6_UDP             |
	VIRTCHNL2_CAP_RSS_IPV6_SCTP            |
	VIRTCHNL2_CAP_RSS_IPV6_OTHER           |
	VIRTCHNL2_CAP_RSS_IPV4_AH              |
	VIRTCHNL2_CAP_RSS_IPV4_ESP             |
	VIRTCHNL2_CAP_RSS_IPV4_AH_ESP          |
	VIRTCHNL2_CAP_RSS_IPV6_AH              |
	VIRTCHNL2_CAP_RSS_IPV6_ESP             |
	VIRTCHNL2_CAP_RSS_IPV6_AH_ESP,

	.other_caps = VIRTCHNL2_CAP_WB_ON_ITR
};

static int
cpfl_vport_map_init(struct cpfl_adapter_ext *adapter)
{
	char hname[32];

	snprintf(hname, 32, "%s-vport", adapter->name);

	rte_spinlock_init(&adapter->vport_map_lock);

#define CPFL_VPORT_MAP_HASH_ENTRY_NUM 2048

	struct rte_hash_parameters params = {
		.name = adapter->name,
		.entries = CPFL_VPORT_MAP_HASH_ENTRY_NUM,
		.key_len = sizeof(struct cpfl_vport_id),
		.hash_func = rte_hash_crc,
		.socket_id = SOCKET_ID_ANY,
	};

	adapter->vport_map_hash = rte_hash_create(&params);

	if (adapter->vport_map_hash == NULL) {
		PMD_INIT_LOG(ERR, "Failed to create vport map hash");
		return -EINVAL;
	}

	return 0;
}

static void
cpfl_vport_map_uninit(struct cpfl_adapter_ext *adapter)
{
	const void *key = NULL;
	struct cpfl_vport_map_info *info;
	uint32_t iter = 0;

	while (rte_hash_iterate(adapter->vport_map_hash, &key, (void **)&info, &iter) >= 0)
		rte_free(info);

	rte_hash_free(adapter->vport_map_hash);
}

static int
cpfl_repr_allowlist_init(struct cpfl_adapter_ext *adapter)
{
	char hname[32];

	snprintf(hname, 32, "%s-repr_al", adapter->name);

	rte_spinlock_init(&adapter->repr_lock);

#define CPFL_REPR_HASH_ENTRY_NUM 2048

	struct rte_hash_parameters params = {
		.name = hname,
		.entries = CPFL_REPR_HASH_ENTRY_NUM,
		.key_len = sizeof(struct cpfl_repr_id),
		.hash_func = rte_hash_crc,
		.socket_id = SOCKET_ID_ANY,
	};

	adapter->repr_allowlist_hash = rte_hash_create(&params);

	if (adapter->repr_allowlist_hash == NULL) {
		PMD_INIT_LOG(ERR, "Failed to create repr allowlist hash");
		return -EINVAL;
	}

	return 0;
}

static void
cpfl_repr_allowlist_uninit(struct cpfl_adapter_ext *adapter)
{
	rte_hash_free(adapter->repr_allowlist_hash);
}

static uint8_t
get_running_host_id(void)
{
	struct utsname unamedata;
	uint8_t host_id = CPFL_INVALID_HOST_ID;

	if (uname(&unamedata) != 0)
		PMD_INIT_LOG(ERR, "Cannot fetch node_name for host");
	else if (strstr(unamedata.nodename, "ipu-imc"))
		PMD_INIT_LOG(ERR, "CPFL PMD cannot be running on IMC.");
	else if (strstr(unamedata.nodename, "ipu-acc"))
		host_id = CPFL_HOST_ID_ACC;
	else
		host_id = CPFL_HOST_ID_HOST;

	return host_id;
}

static uint8_t
set_config_queue_details(struct cpfl_adapter_ext *adapter, struct rte_pci_addr *pci_addr)
{
	if (pci_addr->function == CPFL_FID) {
		adapter->num_cfgq = CPFL_CFGQ_NUM;
		adapter->num_rx_cfgq = CPFL_RX_CFGQ_NUM;
		adapter->num_tx_cfgq = CPFL_TX_CFGQ_NUM;
	} else if (pci_addr->function == VCPF_FID) {
		adapter->num_cfgq = VCPF_CFGQ_NUM;
		adapter->num_rx_cfgq = VCPF_RX_CFGQ_NUM;
		adapter->num_tx_cfgq = VCPF_TX_CFGQ_NUM;
	}

	return 0;
}

static int
cpfl_adapter_ext_init(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapter,
		      struct cpfl_devargs *devargs)
{
	struct idpf_adapter *base = &adapter->base;
	struct idpf_hw *hw = &base->hw;
	struct rte_pci_addr *pci_addr = &pci_dev->addr;
	int ret = 0;

#ifndef RTE_HAS_JANSSON
	RTE_SET_USED(devargs);
#endif

	hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
	hw->hw_addr_len = pci_dev->mem_resource[0].len;
	hw->back = base;
	hw->vendor_id = pci_dev->id.vendor_id;
	hw->device_id = pci_dev->id.device_id;
	hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
	adapter->host_id = get_running_host_id();

	strncpy(adapter->name, pci_dev->device.name, PCI_PRI_STR_SIZE);

	rte_memcpy(&base->caps, &req_caps, sizeof(struct virtchnl2_get_capabilities));

	ret = idpf_adapter_init(base);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to init adapter");
		goto err_adapter_init;
	}

	ret = cpfl_vport_map_init(adapter);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to init vport map");
		goto err_vport_map_init;
	}

	ret = cpfl_repr_allowlist_init(adapter);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to init representor allowlist");
		goto err_repr_allowlist_init;
	}

	rte_eal_alarm_set(CPFL_ALARM_INTERVAL, cpfl_dev_alarm_handler, adapter);

	adapter->max_vport_nb = adapter->base.caps.max_vports > CPFL_MAX_VPORT_NUM ?
				CPFL_MAX_VPORT_NUM : adapter->base.caps.max_vports;

	adapter->vports = rte_zmalloc("vports",
				      adapter->max_vport_nb *
				      sizeof(*adapter->vports),
				      0);
	if (adapter->vports == NULL) {
		PMD_INIT_LOG(ERR, "Failed to allocate vports memory");
		ret = -ENOMEM;
		goto err_vports_alloc;
	}

	/* set the number of config queues to be requested */
	ret = set_config_queue_details(adapter, pci_addr);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to set the config queue details");
		return -1;
	}

	if (pci_addr->function == VCPF_FID || pci_addr->function == CPFL_FID) {
		ret = cpfl_ctrl_path_open(adapter);
		if (ret) {
			PMD_INIT_LOG(ERR, "Failed to setup control path");
			if (pci_addr->function == CPFL_FID)
				goto err_create_ctrl_vport;
			else
				return ret;
		}

	}

#ifdef RTE_HAS_JANSSON
	ret = cpfl_flow_init(adapter, devargs);
	if (ret) {
		PMD_INIT_LOG(ERR, "Failed to init flow module");
		goto err_flow_init;
	}
#endif
	adapter->cur_vports = 0;
	adapter->cur_vport_nb = 0;

	adapter->used_vecs_num = 0;

	return ret;

#ifdef RTE_HAS_JANSSON
err_flow_init:
	cpfl_ctrl_path_close(adapter);
#endif
err_create_ctrl_vport:
	rte_free(adapter->vports);
err_vports_alloc:
	rte_eal_alarm_cancel(cpfl_dev_alarm_handler, adapter);
	cpfl_repr_allowlist_uninit(adapter);
err_repr_allowlist_init:
	cpfl_vport_map_uninit(adapter);
err_vport_map_init:
	idpf_adapter_deinit(base);
err_adapter_init:
	return ret;
}

static uint16_t
cpfl_vport_idx_alloc(struct cpfl_adapter_ext *adapter)
{
	uint16_t vport_idx;
	uint16_t i;

	for (i = 0; i < adapter->max_vport_nb; i++) {
		if (adapter->vports[i] == NULL)
			break;
	}

	if (i == adapter->max_vport_nb)
		vport_idx = CPFL_INVALID_VPORT_IDX;
	else
		vport_idx = i;

	return vport_idx;
}

static int
cpfl_p2p_q_grps_add(struct idpf_vport *vport,
		    struct virtchnl2_add_queue_groups *p2p_queue_grps_info,
		    uint8_t *p2p_q_vc_out_info)
{
	int ret;

	p2p_queue_grps_info->vport_id = vport->vport_id;
	p2p_queue_grps_info->num_queue_groups = CPFL_P2P_NB_QUEUE_GRPS;
	p2p_queue_grps_info->groups[0].num_rx_q = CPFL_MAX_P2P_NB_QUEUES;
	p2p_queue_grps_info->groups[0].num_rx_bufq = CPFL_P2P_NB_RX_BUFQ;
	p2p_queue_grps_info->groups[0].num_tx_q = CPFL_MAX_P2P_NB_QUEUES;
	p2p_queue_grps_info->groups[0].num_tx_complq = CPFL_P2P_NB_TX_COMPLQ;
	p2p_queue_grps_info->groups[0].qg_id.queue_group_id = CPFL_P2P_QUEUE_GRP_ID;
	p2p_queue_grps_info->groups[0].qg_id.queue_group_type = VIRTCHNL2_QUEUE_GROUP_P2P;
	p2p_queue_grps_info->groups[0].rx_q_grp_info.rss_lut_size = 0;
	p2p_queue_grps_info->groups[0].tx_q_grp_info.tx_tc = 0;
	p2p_queue_grps_info->groups[0].tx_q_grp_info.priority = 0;
	p2p_queue_grps_info->groups[0].tx_q_grp_info.is_sp = 0;
	p2p_queue_grps_info->groups[0].tx_q_grp_info.pir_weight = 0;

	ret = idpf_vc_queue_grps_add(vport, p2p_queue_grps_info, p2p_q_vc_out_info);
	if (ret != 0) {
		PMD_DRV_LOG(ERR, "Failed to add p2p queue groups.");
		return ret;
	}

	return ret;
}

static int
cpfl_p2p_queue_info_init(struct cpfl_vport *cpfl_vport,
			 struct virtchnl2_add_queue_groups *p2p_q_vc_out_info)
{
	struct p2p_queue_chunks_info *p2p_q_chunks_info = cpfl_vport->p2p_q_chunks_info;
	struct virtchnl2_queue_reg_chunks *vc_chunks_out;
	int i, type;

	if (p2p_q_vc_out_info->groups[0].qg_id.queue_group_type !=
	    VIRTCHNL2_QUEUE_GROUP_P2P) {
		PMD_DRV_LOG(ERR, "Add queue group response mismatch.");
		return -EINVAL;
	}

	vc_chunks_out = &p2p_q_vc_out_info->groups[0].chunks;

	for (i = 0; i < vc_chunks_out->num_chunks; i++) {
		type = vc_chunks_out->chunks[i].type;
		switch (type) {
		case VIRTCHNL2_QUEUE_TYPE_TX:
			p2p_q_chunks_info->tx_start_qid =
				vc_chunks_out->chunks[i].start_queue_id;
			p2p_q_chunks_info->tx_qtail_start =
				vc_chunks_out->chunks[i].qtail_reg_start;
			p2p_q_chunks_info->tx_qtail_spacing =
				vc_chunks_out->chunks[i].qtail_reg_spacing;
			break;
		case VIRTCHNL2_QUEUE_TYPE_RX:
			p2p_q_chunks_info->rx_start_qid =
				vc_chunks_out->chunks[i].start_queue_id;
			p2p_q_chunks_info->rx_qtail_start =
				vc_chunks_out->chunks[i].qtail_reg_start;
			p2p_q_chunks_info->rx_qtail_spacing =
				vc_chunks_out->chunks[i].qtail_reg_spacing;
			break;
		case VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION:
			p2p_q_chunks_info->tx_compl_start_qid =
				vc_chunks_out->chunks[i].start_queue_id;
			p2p_q_chunks_info->tx_compl_qtail_start =
				vc_chunks_out->chunks[i].qtail_reg_start;
			p2p_q_chunks_info->tx_compl_qtail_spacing =
				vc_chunks_out->chunks[i].qtail_reg_spacing;
			break;
		case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
			p2p_q_chunks_info->rx_buf_start_qid =
				vc_chunks_out->chunks[i].start_queue_id;
			p2p_q_chunks_info->rx_buf_qtail_start =
				vc_chunks_out->chunks[i].qtail_reg_start;
			p2p_q_chunks_info->rx_buf_qtail_spacing =
				vc_chunks_out->chunks[i].qtail_reg_spacing;
			break;
		default:
			PMD_DRV_LOG(ERR, "Unsupported queue type");
			break;
		}
	}

	return 0;
}

int
cpfl_alloc_dma_mem_batch(struct idpf_dma_mem *orig_dma, struct idpf_dma_mem *dma, uint32_t size,
			 int batch_size)
{
	int i;

	if (!idpf_alloc_dma_mem(NULL, orig_dma, (uint64_t)size * (1 + batch_size))) {
		PMD_INIT_LOG(ERR, "Could not alloc dma memory");
		return -ENOMEM;
	}

	for (i = 0; i < batch_size; i++) {
		dma[i].va = (void *)((char *)orig_dma->va + size * (i + 1));
		dma[i].pa = orig_dma->pa + size * (i + 1);
		dma[i].size = size;
		dma[i].zone = NULL;
	}
	return 0;
}

static int
cpfl_dev_vport_init(struct rte_eth_dev *dev, void *init_params)
{
	struct cpfl_vport *cpfl_vport = dev->data->dev_private;
	struct idpf_vport *vport = &cpfl_vport->base;
	struct cpfl_vport_param *param = init_params;
	struct cpfl_adapter_ext *adapter = param->adapter;
	/* for sending create vport virtchnl msg prepare */
	struct virtchnl2_create_vport create_vport_info;
	struct virtchnl2_add_queue_groups p2p_queue_grps_info;
	struct cpchnl2_get_vport_info_response response;
	uint8_t p2p_q_vc_out_info[IDPF_DFLT_MBX_BUF_SIZE] = {0};
	struct cpfl_vport_id vi;
	struct cpchnl2_vport_id v_id;
	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
	int ret = 0;

	dev->dev_ops = &cpfl_eth_dev_ops;
	vport->adapter = &adapter->base;
	vport->sw_idx = param->idx;
	vport->devarg_id = param->devarg_id;

	memset(&create_vport_info, 0, sizeof(create_vport_info));
	ret = idpf_vport_info_init(vport, &create_vport_info);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to init vport req_info.");
		goto err;
	}

	ret = idpf_vport_init(vport, &create_vport_info, dev->data);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to init vports.");
		goto err;
	}

	cpfl_vport->itf.type = CPFL_ITF_TYPE_VPORT;
	cpfl_vport->itf.adapter = adapter;
	cpfl_vport->itf.data = dev->data;
	TAILQ_INIT(&cpfl_vport->itf.flow_list);
	adapter->vports[param->idx] = cpfl_vport;
	adapter->cur_vports |= RTE_BIT32(param->devarg_id);
	adapter->cur_vport_nb++;

	dev->data->mac_addrs = rte_zmalloc(NULL, RTE_ETHER_ADDR_LEN, 0);
	if (dev->data->mac_addrs == NULL) {
		PMD_INIT_LOG(ERR, "Cannot allocate mac_addr memory.");
		ret = -ENOMEM;
		goto err_mac_addrs;
	}

	rte_ether_addr_copy((struct rte_ether_addr *)vport->default_mac_addr,
			    &dev->data->mac_addrs[0]);

	memset(cpfl_vport->itf.dma, 0, sizeof(cpfl_vport->itf.dma));
	memset(cpfl_vport->itf.msg, 0, sizeof(cpfl_vport->itf.msg));
	ret = cpfl_alloc_dma_mem_batch(&cpfl_vport->itf.flow_dma,
				       cpfl_vport->itf.dma,
				       sizeof(union cpfl_rule_cfg_pkt_record),
				       CPFL_FLOW_BATCH_SIZE);
	if (ret < 0)
		goto err_mac_addrs;

	if (!adapter->base.is_rx_singleq && !adapter->base.is_tx_singleq) {
		memset(&p2p_queue_grps_info, 0, sizeof(p2p_queue_grps_info));
		ret = cpfl_p2p_q_grps_add(vport, &p2p_queue_grps_info, p2p_q_vc_out_info);
		if (ret != 0) {
			PMD_INIT_LOG(WARNING, "Failed to add p2p queue group.");
			return 0;
		}
		cpfl_vport->p2p_q_chunks_info = rte_zmalloc(NULL,
						    sizeof(struct p2p_queue_chunks_info), 0);
		if (cpfl_vport->p2p_q_chunks_info == NULL) {
			PMD_INIT_LOG(WARNING, "Failed to allocate p2p queue info.");
			cpfl_p2p_queue_grps_del(vport);
			return 0;
		}
		ret = cpfl_p2p_queue_info_init(cpfl_vport,
				       (struct virtchnl2_add_queue_groups *)p2p_q_vc_out_info);
		if (ret != 0) {
			PMD_INIT_LOG(WARNING, "Failed to init p2p queue info.");
			rte_free(cpfl_vport->p2p_q_chunks_info);
			cpfl_p2p_queue_grps_del(vport);
		}
	}
	/* get the vport info */
	if (adapter->base.hw.device_id == IXD_DEV_ID_VCPF) {
		pci_dev = RTE_DEV_TO_PCI(dev->device);
		vi.func_type = VCPF_CPCHNL2_FTYPE_LAN_VF;
		vi.pf_id = CPFL_HOST0_CPF_ID;
		vi.vf_id = pci_dev->addr.function;

		v_id.vport_id = cpfl_vport->base.vport_info.info.vport_id;
		v_id.vport_type = cpfl_vport->base.vport_info.info.vport_type;

		ret = cpfl_cc_vport_info_get(adapter, &v_id, &vi, &response);
		if (ret != 0) {
			PMD_INIT_LOG(ERR, "Failed to send vport info cpchnl message.");
			return -1;
		}

		ret = vcpf_save_vport_info_response(cpfl_vport, &response);
		if (ret != 0) {
			PMD_INIT_LOG(ERR, "Failed to save cpchnl response.");
			return -1;
		}
	}

	return 0;

err_mac_addrs:
	adapter->vports[param->idx] = NULL;  /* reset */
	idpf_vport_deinit(vport);
	adapter->cur_vports &= ~RTE_BIT32(param->devarg_id);
	adapter->cur_vport_nb--;
err:
	return ret;
}

static const struct rte_pci_id pci_id_cpfl_map[] = {
	{ RTE_PCI_DEVICE(IDPF_INTEL_VENDOR_ID, CPFL_DEV_ID_MEV) },
	{ RTE_PCI_DEVICE(IDPF_INTEL_VENDOR_ID, CPFL_DEV_ID_MMG) },
	{ .vendor_id = 0, /* sentinel */ },
};

static const struct rte_pci_id pci_id_vcpf_map[] = {
	{ RTE_PCI_DEVICE(IDPF_INTEL_VENDOR_ID, IXD_DEV_ID_VCPF) },
	{ .vendor_id = 0, /* sentinel */ },
};

static struct cpfl_adapter_ext *
cpfl_find_adapter_ext(struct rte_pci_device *pci_dev)
{
	struct cpfl_adapter_ext *adapter;
	int found = 0;

	if (pci_dev == NULL)
		return NULL;

	rte_spinlock_lock(&cpfl_adapter_lock);
	TAILQ_FOREACH(adapter, &cpfl_adapter_list, next) {
		if (strncmp(adapter->name, pci_dev->device.name, PCI_PRI_STR_SIZE) == 0) {
			found = 1;
			break;
		}
	}
	rte_spinlock_unlock(&cpfl_adapter_lock);

	if (found == 0)
		return NULL;

	return adapter;
}

static void
cpfl_adapter_ext_deinit(struct cpfl_adapter_ext *adapter)
{
#ifdef RTE_HAS_JANSSON
	cpfl_flow_uninit(adapter);
#endif
	cpfl_ctrl_path_close(adapter);
	rte_eal_alarm_cancel(cpfl_dev_alarm_handler, adapter);
	cpfl_vport_map_uninit(adapter);
	idpf_adapter_deinit(&adapter->base);

	rte_free(adapter->vports);
	adapter->vports = NULL;
}

static int
cpfl_vport_devargs_process(struct cpfl_adapter_ext *adapter, struct cpfl_devargs *devargs)
{
	int i;

	/* refine vport number, at least 1 vport */
	if (devargs->req_vport_nb == 0) {
		devargs->req_vport_nb = 1;
		devargs->req_vports[0] = 0;
	}

	/* check parsed devargs */
	if (adapter->cur_vport_nb + devargs->req_vport_nb >
	    adapter->max_vport_nb) {
		PMD_INIT_LOG(ERR, "Total vport number can't be > %d",
			     adapter->max_vport_nb);
		return -EINVAL;
	}

	for (i = 0; i < devargs->req_vport_nb; i++) {
		if (devargs->req_vports[i] > adapter->max_vport_nb - 1) {
			PMD_INIT_LOG(ERR, "Invalid vport id %d, it should be 0 ~ %d",
				     devargs->req_vports[i], adapter->max_vport_nb - 1);
			return -EINVAL;
		}

		if (adapter->cur_vports & RTE_BIT32(devargs->req_vports[i])) {
			PMD_INIT_LOG(ERR, "Vport %d has been requested",
				     devargs->req_vports[i]);
			return -EINVAL;
		}
	}

	return 0;
}

static int
cpfl_vport_create(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapter,
		  struct cpfl_devargs *devargs)
{
	struct cpfl_vport_param vport_param;
	char name[RTE_ETH_NAME_MAX_LEN];
	int ret, i;

	for (i = 0; i < devargs->req_vport_nb; i++) {
		vport_param.adapter = adapter;
		vport_param.devarg_id = devargs->req_vports[i];
		vport_param.idx = cpfl_vport_idx_alloc(adapter);
		if (vport_param.idx == CPFL_INVALID_VPORT_IDX) {
			PMD_INIT_LOG(ERR, "No space for vport %u", vport_param.devarg_id);
			break;
		}
		snprintf(name, sizeof(name), "net_%s_vport_%d",
			 pci_dev->device.name,
			 devargs->req_vports[i]);
		ret = rte_eth_dev_create(&pci_dev->device, name,
					    sizeof(struct cpfl_vport),
					    NULL, NULL, cpfl_dev_vport_init,
					    &vport_param);
		if (ret != 0)
			PMD_DRV_LOG(ERR, "Failed to create vport %d",
				    vport_param.devarg_id);
	}

	return 0;
}

static int
cpfl_pci_probe_first(struct rte_pci_device *pci_dev)
{
	struct cpfl_adapter_ext *adapter;
	struct cpfl_devargs devargs;
	int retval;
	uint16_t port_id;

	adapter = rte_zmalloc("cpfl_adapter_ext",
			      sizeof(struct cpfl_adapter_ext), 0);
	if (adapter == NULL) {
		PMD_INIT_LOG(ERR, "Failed to allocate adapter.");
		return -ENOMEM;
	}

	memset(&devargs, 0, sizeof(devargs));

	retval = cpfl_parse_devargs(pci_dev, adapter, true, &devargs);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to parse private devargs");
		return retval;
	}

	retval = cpfl_adapter_ext_init(pci_dev, adapter, &devargs);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to init adapter.");
		return retval;
	}

	rte_spinlock_lock(&cpfl_adapter_lock);
	TAILQ_INSERT_TAIL(&cpfl_adapter_list, adapter, next);
	rte_spinlock_unlock(&cpfl_adapter_lock);

	retval = cpfl_vport_devargs_process(adapter, &devargs);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to process vport devargs");
		goto err;
	}

	retval = cpfl_vport_create(pci_dev, adapter, &devargs);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to create vports.");
		goto err;
	}

	retval = cpfl_repr_devargs_process(adapter, &devargs);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to process repr devargs");
		goto close_ethdev;
	}

	retval = cpfl_repr_create(pci_dev, adapter);
	if (retval != 0) {
		PMD_INIT_LOG(ERR, "Failed to create representors ");
		goto close_ethdev;
	}


	return 0;

close_ethdev:
	/* Ethdev created can be found RTE_ETH_FOREACH_DEV_OF through rte_device */
	RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device) {
		rte_eth_dev_close(port_id);
	}
err:
	rte_spinlock_lock(&cpfl_adapter_lock);
	TAILQ_REMOVE(&cpfl_adapter_list, adapter, next);
	rte_spinlock_unlock(&cpfl_adapter_lock);
	cpfl_adapter_ext_deinit(adapter);
	rte_free(adapter);
	return retval;
}

static int
cpfl_pci_probe_again(struct rte_pci_device *pci_dev, struct cpfl_adapter_ext *adapter)
{
	struct cpfl_devargs devargs;
	int ret;

	memset(&devargs, 0, sizeof(devargs));
	ret = cpfl_parse_devargs(pci_dev, adapter, false, &devargs);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to parse private devargs");
		return ret;
	}

	ret = cpfl_repr_devargs_process(adapter, &devargs);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to process reprenstor devargs");
		return ret;
	}

	ret = cpfl_repr_create(pci_dev, adapter);
	if (ret != 0) {
		PMD_INIT_LOG(ERR, "Failed to create representors ");
		return ret;
	}

	return 0;
}

static int
cpfl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
	       struct rte_pci_device *pci_dev)
{
	struct cpfl_adapter_ext *adapter;

	if (!cpfl_adapter_list_init) {
		rte_spinlock_init(&cpfl_adapter_lock);
		TAILQ_INIT(&cpfl_adapter_list);
		cpfl_adapter_list_init = true;
	}

	adapter = cpfl_find_adapter_ext(pci_dev);

	if (adapter == NULL)
		return cpfl_pci_probe_first(pci_dev);
	else
		return cpfl_pci_probe_again(pci_dev, adapter);
}

static int
cpfl_pci_remove(struct rte_pci_device *pci_dev)
{
	struct cpfl_adapter_ext *adapter = cpfl_find_adapter_ext(pci_dev);
	uint16_t port_id;

	/* Ethdev created can be found RTE_ETH_FOREACH_DEV_OF through rte_device */
	RTE_ETH_FOREACH_DEV_OF(port_id, &pci_dev->device) {
			rte_eth_dev_close(port_id);
	}

	rte_spinlock_lock(&cpfl_adapter_lock);
	TAILQ_REMOVE(&cpfl_adapter_list, adapter, next);
	rte_spinlock_unlock(&cpfl_adapter_lock);
	cpfl_adapter_ext_deinit(adapter);
	rte_free(adapter);

	return 0;
}

static struct rte_pci_driver rte_cpfl_pmd = {
	.id_table	= pci_id_cpfl_map,
	.drv_flags	= RTE_PCI_DRV_NEED_MAPPING |
			  RTE_PCI_DRV_PROBE_AGAIN,
	.probe		= cpfl_pci_probe,
	.remove		= cpfl_pci_remove,
};

static struct rte_pci_driver rte_vcpf_pmd = {
	.id_table	= pci_id_vcpf_map,
	.drv_flags	= RTE_PCI_DRV_NEED_MAPPING |
			  RTE_PCI_DRV_PROBE_AGAIN,
	.probe		= cpfl_pci_probe,
	.remove		= cpfl_pci_remove,
};

/**
 * Driver initialization routine.
 * Invoked once at EAL init time.
 * Register itself as the [Poll Mode] Driver of PCI devices.
 */
RTE_PMD_REGISTER_PCI(net_cpfl, rte_cpfl_pmd);
RTE_PMD_REGISTER_PCI_TABLE(net_cpfl, pci_id_cpfl_map);
RTE_PMD_REGISTER_KMOD_DEP(net_cpfl, "* igb_uio | vfio-pci");
RTE_PMD_REGISTER_PCI(net_vcpf, rte_vcpf_pmd);
RTE_PMD_REGISTER_PCI_TABLE(net_vcpf, pci_id_vcpf_map);
RTE_PMD_REGISTER_KMOD_DEP(net_vcpf, "vfio-pci");
RTE_PMD_REGISTER_PARAM_STRING(net_cpfl,
	CPFL_TX_SINGLE_Q "=<0|1> "
	CPFL_RX_SINGLE_Q "=<0|1> "
	CPFL_VPORT "=[vport0_begin[-vport0_end][,vport1_begin[-vport1_end]][,..]]");

RTE_LOG_REGISTER_SUFFIX(cpfl_logtype_init, init, NOTICE);
RTE_LOG_REGISTER_SUFFIX(cpfl_logtype_driver, driver, NOTICE);