summaryrefslogtreecommitdiff
path: root/core/hdd/src/wlan_hdd_p2p.c
blob: 9cdfd01810b45b83df2e0d988ff232ae6c68efd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
/*
 * Copyright (c) 2012-2018 The Linux Foundation. All rights reserved.
 *
 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
 *
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This file was originally distributed by Qualcomm Atheros, Inc.
 * under proprietary terms before Copyright ownership was assigned
 * to the Linux Foundation.
 */

/**
 *
 * @file  wlan_hdd_p2p.c
 *
 * @brief WLAN Host Device Driver implementation for P2P commands interface
 *
 */

#include <wlan_hdd_includes.h>
#include <wlan_hdd_hostapd.h>
#include <net/cfg80211.h>
#include "sme_api.h"
#include "sme_qos_api.h"
#include "wlan_hdd_p2p.h"
#include "sap_api.h"
#include "wlan_hdd_main.h"
#include "qdf_trace.h"
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/etherdevice.h>
#include <net/ieee80211_radiotap.h>
#include "wlan_hdd_tdls.h"
#include "wlan_hdd_trace.h"
#include "qdf_types.h"
#include "qdf_trace.h"
#include "cds_sched.h"
#include "cds_concurrency.h"
#include "cds_utils.h"
#include "wlan_hdd_request_manager.h"

/* Ms to Time Unit Micro Sec */
#define MS_TO_TU_MUS(x)   ((x) * 1024)
#define MAX_MUS_VAL       (INT_MAX / 1024)

static uint8_t *hdd_get_action_string(uint16_t MsgType)
{
	switch (MsgType) {
		CASE_RETURN_STRING(SIR_MAC_ACTION_SPECTRUM_MGMT);
		CASE_RETURN_STRING(SIR_MAC_ACTION_QOS_MGMT);
		CASE_RETURN_STRING(SIR_MAC_ACTION_DLP);
		CASE_RETURN_STRING(SIR_MAC_ACTION_PUBLIC_USAGE);
		CASE_RETURN_STRING(SIR_MAC_ACTION_RRM);
		CASE_RETURN_STRING(SIR_MAC_ACTION_FAST_BSS_TRNST);
		CASE_RETURN_STRING(SIR_MAC_ACTION_HT);
		CASE_RETURN_STRING(SIR_MAC_ACTION_SA_QUERY);
		CASE_RETURN_STRING(SIR_MAC_ACTION_PROT_DUAL_PUB);
		CASE_RETURN_STRING(SIR_MAC_ACTION_WNM);
		CASE_RETURN_STRING(SIR_MAC_ACTION_UNPROT_WNM);
		CASE_RETURN_STRING(SIR_MAC_ACTION_TDLS);
		CASE_RETURN_STRING(SIR_MAC_ACITON_MESH);
		CASE_RETURN_STRING(SIR_MAC_ACTION_MHF);
		CASE_RETURN_STRING(SIR_MAC_SELF_PROTECTED);
		CASE_RETURN_STRING(SIR_MAC_ACTION_WME);
		CASE_RETURN_STRING(SIR_MAC_ACTION_VHT);
	default:
		return "UNKNOWN";
	}
}

#ifdef WLAN_FEATURE_P2P_DEBUG
#define MAX_P2P_ACTION_FRAME_TYPE 9
const char *p2p_action_frame_type[] = { "GO Negotiation Request",
					"GO Negotiation Response",
					"GO Negotiation Confirmation",
					"P2P Invitation Request",
					"P2P Invitation Response",
					"Device Discoverability Request",
					"Device Discoverability Response",
					"Provision Discovery Request",
					"Provision Discovery Response"};

/* We no need to protect this variable since
 * there is no chance of race to condition
 * and also not make any complicating the code
 * just for debugging log
 */
enum p2p_connection_status global_p2p_connection_status = P2P_NOT_ACTIVE;

#endif
#define MAX_TDLS_ACTION_FRAME_TYPE 11
const char *tdls_action_frame_type[] = { "TDLS Setup Request",
					 "TDLS Setup Response",
					 "TDLS Setup Confirm",
					 "TDLS Teardown",
					 "TDLS Peer Traffic Indication",
					 "TDLS Channel Switch Request",
					 "TDLS Channel Switch Response",
					 "TDLS Peer PSM Request",
					 "TDLS Peer PSM Response",
					 "TDLS Peer Traffic Response",
					 "TDLS Discovery Request"};

static bool wlan_hdd_is_type_p2p_action(const u8 *buf, uint32_t len)
{
	const u8 *ouiPtr;

	if (len < WLAN_HDD_PUBLIC_ACTION_FRAME_SUB_TYPE_OFFSET + 1)
		return false;

	if (buf[WLAN_HDD_PUBLIC_ACTION_FRAME_CATEGORY_OFFSET] !=
	    WLAN_HDD_PUBLIC_ACTION_FRAME)
		return false;

	if (buf[WLAN_HDD_PUBLIC_ACTION_FRAME_ACTION_OFFSET] !=
	    WLAN_HDD_VENDOR_SPECIFIC_ACTION)
		return false;

	ouiPtr = &buf[WLAN_HDD_PUBLIC_ACTION_FRAME_OUI_OFFSET];

	if (WPA_GET_BE24(ouiPtr) != WLAN_HDD_WFA_OUI)
		return false;

	if (buf[WLAN_HDD_PUBLIC_ACTION_FRAME_OUI_TYPE_OFFSET] !=
	    WLAN_HDD_WFA_P2P_OUI_TYPE)
		return false;

	return true;
}

static bool hdd_p2p_is_action_type_rsp(const u8 *buf, uint32_t len)
{
	enum action_frm_type actionFrmType;

	if (wlan_hdd_is_type_p2p_action(buf, len)) {
		actionFrmType =
			buf[WLAN_HDD_PUBLIC_ACTION_FRAME_SUB_TYPE_OFFSET];
		if (actionFrmType != WLAN_HDD_INVITATION_REQ
		    && actionFrmType != WLAN_HDD_GO_NEG_REQ
		    && actionFrmType != WLAN_HDD_DEV_DIS_REQ
		    && actionFrmType != WLAN_HDD_PROV_DIS_REQ)
			return true;
	}

	return false;
}

/**
 * hdd_is_p2p_go_cnf_frame() - function to if the frame type is go neg cnf
 * @buf: pointer to frame
 * @len: frame length
 *
 * This function is used to check if the given frame is GO negotiation
 * confirmation frame.
 *
 * Return: true if the frame is go negotiation confirmation otherwise false
 */
static bool hdd_is_p2p_go_cnf_frame(const u8 *buf, uint32_t len)
{
	if (wlan_hdd_is_type_p2p_action(buf, len) &&
			buf[WLAN_HDD_PUBLIC_ACTION_FRAME_SUB_TYPE_OFFSET] ==
			WLAN_HDD_GO_NEG_CNF)
		return true;
	else
		return false;
}

struct random_mac_priv {
	bool set_random_addr;
};

/**
 * hdd_random_mac_callback() - Callback invoked from wmi layer
 * @set_random_addr: Status of random mac filter set operation
 * @context: Context passed while registring callback
 *
 * This function is invoked from wmi layer to give the status of
 * random mac filter set operation by firmware.
 *
 * Return: None
 */
static void hdd_random_mac_callback(bool set_random_addr, void *context)
{
	struct hdd_request *request;
	struct random_mac_priv *priv;

	request = hdd_request_get(context);
	if (!request) {
		hdd_err("invalid request");
		return;
	}

	priv = hdd_request_priv(request);
	priv->set_random_addr = set_random_addr;

	hdd_request_complete(request);
	hdd_request_put(request);
}

/**
 * hdd_set_random_mac() - Invoke sme api to set random mac filter
 * @adapter: Pointer to adapter
 * @random_mac_addr: Mac addr filter to be set
 * @freq: Channel frequency
 *
 * Return: If set is successful return true else return false
 */
static bool hdd_set_random_mac(hdd_adapter_t *adapter,
			       uint8_t *random_mac_addr,
			       uint32_t freq)
{
	hdd_context_t *hdd_ctx;
	QDF_STATUS sme_status;
	unsigned long rc;
	void *cookie;
	bool status = false;
	struct hdd_request     *request;
	struct random_mac_priv *priv;
	static const struct hdd_request_params params = {
		.priv_size = sizeof(*priv),
		.timeout_ms = WLAN_WAIT_TIME_SET_RND,
	};

	ENTER();
	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
	if (wlan_hdd_validate_context(hdd_ctx)) {
		hdd_err("Invalid hdd ctx");
		return false;
	}

	request = hdd_request_alloc(&params);
	if (!request) {
		hdd_err("Request allocation failure");
		return false;
	}

	cookie = hdd_request_cookie(request);

	sme_status = sme_set_random_mac(hdd_ctx->hHal, hdd_random_mac_callback,
				     adapter->sessionId, random_mac_addr, freq,
				     cookie);

	if (sme_status != QDF_STATUS_SUCCESS) {
		hdd_err("Unable to set random mac");
	} else {
		rc = hdd_request_wait_for_response(request);
		if (rc) {
			hdd_err("SME timed out while setting random mac");
		} else {
			priv = hdd_request_priv(request);
			status = priv->set_random_addr;
		}
	}

	hdd_request_put(request);
	EXIT();

	return status;
}

/**
 * hdd_clear_random_mac() - Invoke sme api to clear random mac filter
 * @adapter: Pointer to adapter
 * @random_mac_addr: Mac addr filter to be cleared
 * @freq: Channel frequency
 *
 * Return: If clear is successful return true else return false
 */
static bool hdd_clear_random_mac(hdd_adapter_t *adapter,
				 uint8_t *random_mac_addr,
				 uint32_t freq)
{
	hdd_context_t *hdd_ctx;
	QDF_STATUS status;

	ENTER();
	hdd_ctx = WLAN_HDD_GET_CTX(adapter);
	if (wlan_hdd_validate_context(hdd_ctx)) {
		hdd_err("Invalid hdd ctx");
		return false;
	}

	status = sme_clear_random_mac(hdd_ctx->hHal, adapter->sessionId,
				      random_mac_addr, freq);

	if (status != QDF_STATUS_SUCCESS) {
		hdd_err("Unable to clear random mac");
		return false;
	}

	EXIT();
	return true;
}

bool hdd_check_random_mac(hdd_adapter_t *adapter, uint8_t *random_mac_addr)
{
	uint32_t i = 0;

	spin_lock(&adapter->random_mac_lock);
	for (i = 0; i < MAX_RANDOM_MAC_ADDRS; i++) {
		if ((adapter->random_mac[i].in_use) &&
		    (!qdf_mem_cmp(adapter->random_mac[i].addr, random_mac_addr,
			     QDF_MAC_ADDR_SIZE))) {
			spin_unlock(&adapter->random_mac_lock);
			return true;
		}
	}
	spin_unlock(&adapter->random_mac_lock);
	return false;
}

/**
 * find_action_frame_cookie() - Checks for action cookie in cookie list
 * @cookie_list: List of cookies
 * @cookie: Cookie to be searched
 *
 * Return: If search is successful return pointer to action_frame_cookie
 * object in which cookie item is encapsulated.
 */
static struct action_frame_cookie *find_action_frame_cookie(
						struct list_head *cookie_list,
						uint64_t cookie)
{
	struct action_frame_cookie *action_cookie = NULL;
	struct list_head *temp = NULL;

	list_for_each(temp, cookie_list) {
		action_cookie = list_entry(temp, struct action_frame_cookie,
					   cookie_node);
		if (action_cookie->cookie == cookie)
			return action_cookie;
	}

	return NULL;
}

/**
 * allocate_action_frame_cookie() - Allocate and add action cookie to
 * given list
 * @cookie_list: List of cookies
 * @cookie: Cookie to be added
 *
 * Return: If allocation and addition is successful return pointer to
 * action_frame_cookie object in which cookie item is encapsulated.
 */
static struct action_frame_cookie *allocate_action_frame_cookie(
						struct list_head *cookie_list,
						uint64_t cookie)
{
	struct action_frame_cookie *action_cookie = NULL;

	action_cookie = qdf_mem_malloc(sizeof(*action_cookie));
	if (!action_cookie)
		return NULL;

	action_cookie->cookie = cookie;
	list_add(&action_cookie->cookie_node, cookie_list);

	return action_cookie;
}

/**
 * delete_action_frame_cookie() - Delete the cookie from given list
 * @cookie_list: List of cookies
 * @cookie: Cookie to be deleted
 *
 * This function deletes the cookie item from given list and corresponding
 * object in which it is encapsulated.
 *
 * Return: None
 */
static void delete_action_frame_cookie(
				struct action_frame_cookie *action_cookie)
{
	list_del(&action_cookie->cookie_node);
	qdf_mem_free(action_cookie);
}

/**
 * append_action_frame_cookie() - Append action cookie to given list
 * @cookie_list: List of cookies
 * @cookie: Cookie to be append
 *
 * This is a wrapper function which invokes allocate_action_frame_cookie
 * if the cookie to be added is not duplicate
 *
 * Return: 0 - for successful case
 *         -EALREADY - if cookie is duplicate
 *         -ENOMEM - if allocation is failed
 */
static int32_t append_action_frame_cookie(struct list_head *cookie_list,
					  uint64_t cookie)
{
	struct action_frame_cookie *action_cookie = NULL;

	/*
	 * There should be no mac entry with empty cookie list,
	 * check and ignore if duplicate
	 */
	action_cookie = find_action_frame_cookie(cookie_list, cookie);
	if (action_cookie)
		/* random mac address is already programmed */
		return -EALREADY;

	/* insert new cookie in cookie list */
	action_cookie = allocate_action_frame_cookie(cookie_list, cookie);
	if (!action_cookie)
		return -ENOMEM;

	return 0;
}

/**
 * hdd_set_action_frame_random_mac() - Store action frame cookie
 * @adapter: Pointer to adapter
 * @random_mac_addr: Mac addr for cookie
 * @cookie: Cookie to be stored
 * @freq: Channel frequency
 *
 * This function is used to create cookie list and append the cookies
 * to same for corresponding random mac addr. If this cookie is the first
 * item in the list then random mac filter is set.
 *
 * Return: 0 - for success else negative value
 */
static int32_t hdd_set_action_frame_random_mac(hdd_adapter_t *adapter,
					       uint8_t *random_mac_addr,
					       uint64_t cookie, uint32_t freq)
{
	uint32_t i = 0;
	uint32_t first_unused = MAX_RANDOM_MAC_ADDRS;
	struct action_frame_cookie *action_cookie = NULL;
	int32_t append_ret = 0;

	if (!cookie) {
		hdd_err("Invalid cookie");
		return -EINVAL;
	}

	hdd_info("mac_addr: " MAC_ADDRESS_STR " && cookie = %llu && freq = %u",
			MAC_ADDR_ARRAY(random_mac_addr), cookie, freq);

	spin_lock(&adapter->random_mac_lock);
	/*
	 * Following loop checks whether random mac entry is already
	 * present, if present get the index of matched entry else
	 * get the first unused slot to store this new random mac
	 */
	for (i = 0; i < MAX_RANDOM_MAC_ADDRS; i++) {
		if (!adapter->random_mac[i].in_use) {
			if (first_unused == MAX_RANDOM_MAC_ADDRS)
				first_unused = i;
			continue;
		}

		if (!qdf_mem_cmp(adapter->random_mac[i].addr, random_mac_addr,
				 QDF_MAC_ADDR_SIZE))
			break;
	}

	if (i != MAX_RANDOM_MAC_ADDRS) {
		append_ret = append_action_frame_cookie(
					&adapter->random_mac[i].cookie_list,
					cookie);
		spin_unlock(&adapter->random_mac_lock);

		if (append_ret == -ENOMEM) {
			hdd_err("No Sufficient memory for cookie");
			return append_ret;
		}

		return 0;
	}

	if (first_unused == MAX_RANDOM_MAC_ADDRS) {
		spin_unlock(&adapter->random_mac_lock);
		hdd_err("Reached the limit of Max random addresses");
		return -EBUSY;
	}

	/* get the first unused buf and store new random mac */
	i = first_unused;

	INIT_LIST_HEAD(&adapter->random_mac[i].cookie_list);
	action_cookie = allocate_action_frame_cookie(
					&adapter->random_mac[i].cookie_list,
					cookie);
	if (!action_cookie) {
		spin_unlock(&adapter->random_mac_lock);
		hdd_err("No Sufficient memory for cookie");
		return -ENOMEM;
	}
	qdf_mem_copy(adapter->random_mac[i].addr, random_mac_addr,
		     QDF_MAC_ADDR_SIZE);
	adapter->random_mac[i].in_use = true;
	adapter->random_mac[i].freq = freq;
	spin_unlock(&adapter->random_mac_lock);
	/* Program random mac_addr */
	if (!hdd_set_random_mac(adapter, adapter->random_mac[i].addr, freq)) {
		spin_lock(&adapter->random_mac_lock);
		/* clear the cookie */
		delete_action_frame_cookie(action_cookie);
		adapter->random_mac[i].in_use = false;
		spin_unlock(&adapter->random_mac_lock);
		hdd_err("random mac filter set failed for: "MAC_ADDRESS_STR,
				MAC_ADDR_ARRAY(adapter->random_mac[i].addr));
		return -EFAULT;
	}

	return 0;
}

/**
 * hdd_reset_action_frame_random_mac() - Delete action frame cookie with
 * given random mac addr
 * @adapter: Pointer to adapter
 * @random_mac_addr: Mac addr for cookie
 * @cookie: Cookie to be deleted
 *
 * This function is used to delete the cookie from the cookie list
 * corresponding to given random mac addr.If cookie list is empty after
 * deleting, it will clear random mac filter.
 *
 * Return: 0 - for success else negative value
 */
static int32_t hdd_reset_action_frame_random_mac(hdd_adapter_t *adapter,
						 uint8_t *random_mac_addr,
						 uint64_t cookie)
{
	uint32_t i = 0;
	uint32_t freq = 0;
	struct action_frame_cookie *action_cookie = NULL;

	if (!cookie) {
		hdd_err("Invalid cookie");
		return -EINVAL;
	}

	hdd_info("mac_addr: " MAC_ADDRESS_STR " && cookie = %llu",
			MAC_ADDR_ARRAY(random_mac_addr), cookie);

	spin_lock(&adapter->random_mac_lock);
	for (i = 0; i < MAX_RANDOM_MAC_ADDRS; i++) {
		if ((adapter->random_mac[i].in_use) &&
		    (!qdf_mem_cmp(adapter->random_mac[i].addr,
			     random_mac_addr, QDF_MAC_ADDR_SIZE)))
			break;
	}

	if (i == MAX_RANDOM_MAC_ADDRS) {
		/*
		 * trying to delete cookie of random mac-addr
		 * for which entry is not present
		 */
		spin_unlock(&adapter->random_mac_lock);
		return -EINVAL;
	}

	action_cookie = find_action_frame_cookie(
					&adapter->random_mac[i].cookie_list,
					cookie);

	if (!action_cookie) {
		spin_unlock(&adapter->random_mac_lock);
		hdd_info("No cookie matches");
		return 0;
	}

	delete_action_frame_cookie(action_cookie);
	if (list_empty(&adapter->random_mac[i].cookie_list)) {
		adapter->random_mac[i].in_use = false;
		freq = adapter->random_mac[i].freq;
		spin_unlock(&adapter->random_mac_lock);
		hdd_clear_random_mac(adapter, random_mac_addr,
				     freq);
		hdd_info("Deleted random mac_addr: "MAC_ADDRESS_STR,
			 MAC_ADDR_ARRAY(random_mac_addr));
		return 0;
	}

	spin_unlock(&adapter->random_mac_lock);
	return 0;
}

/**
 * hdd_delete_action_frame_cookie() - Delete action frame cookie
 * @adapter: Pointer to adapter
 * @cookie: Cookie to be deleted
 *
 * This function parses the cookie list of each random mac addr until the
 * specified cookie is found and then deletes it. If cookie list is empty
 * after deleting, it will clear random mac filter.
 *
 * Return: 0 - for success else negative value
 */
static int32_t hdd_delete_action_frame_cookie(hdd_adapter_t *adapter,
					      uint64_t cookie)
{
	uint32_t i = 0;
	struct action_frame_cookie *action_cookie = NULL;
	uint32_t freq = 0;

	hdd_debug("Delete cookie = %llu", cookie);

	spin_lock(&adapter->random_mac_lock);
	for (i = 0; i < MAX_RANDOM_MAC_ADDRS; i++) {
		if (!adapter->random_mac[i].in_use)
			continue;

		action_cookie = find_action_frame_cookie(
					&adapter->random_mac[i].cookie_list,
					cookie);

		if (!action_cookie)
			continue;

		delete_action_frame_cookie(action_cookie);

		if (list_empty(&adapter->random_mac[i].cookie_list)) {
			adapter->random_mac[i].in_use = false;
			freq = adapter->random_mac[i].freq;
			spin_unlock(&adapter->random_mac_lock);
			hdd_clear_random_mac(adapter,
					     adapter->random_mac[i].addr,
					     freq);
			hdd_info("Deleted random addr "MAC_ADDRESS_STR,
				 MAC_ADDR_ARRAY(adapter->random_mac[i].addr));
			return 0;
		}
		spin_unlock(&adapter->random_mac_lock);
		return 0;
	}

	spin_unlock(&adapter->random_mac_lock);
	return 0;
}

/**
 * hdd_delete_all_action_frame_cookies() - Delete all action frame cookies
 * @adapter: Pointer to adapter
 *
 * This function deletes all the cookie lists of each random mac addr and
 * clears the corresponding random mac filters.
 *
 * Return: 0 - for success else negative value
 */
static void hdd_delete_all_action_frame_cookies(hdd_adapter_t *adapter)
{
	uint32_t i = 0;
	struct action_frame_cookie *action_cookie = NULL;
	struct list_head *n;
	struct list_head *temp;
	uint32_t freq = 0;

	spin_lock(&adapter->random_mac_lock);

	for (i = 0; i < MAX_RANDOM_MAC_ADDRS; i++) {
		if (!adapter->random_mac[i].in_use)
			continue;

		/* empty the list and clear random addr */
		list_for_each_safe(temp, n,
				   &adapter->random_mac[i].cookie_list) {
			action_cookie = list_entry(temp,
						   struct action_frame_cookie,
						   cookie_node);
			list_del(temp);
			qdf_mem_free(action_cookie);
		}

		adapter->random_mac[i].in_use = false;
		freq = adapter->random_mac[i].freq;
		spin_unlock(&adapter->random_mac_lock);
		hdd_clear_random_mac(adapter, adapter->random_mac[i].addr,
				     freq);
		hdd_info("Deleted random addr " MAC_ADDRESS_STR,
			 MAC_ADDR_ARRAY(adapter->random_mac[i].addr));
		spin_lock(&adapter->random_mac_lock);
	}

	spin_unlock(&adapter->random_mac_lock);
}
static
QDF_STATUS wlan_hdd_remain_on_channel_callback(tHalHandle hHal, void *pCtx,
			QDF_STATUS status, uint32_t scan_id)
{
	hdd_adapter_t *pAdapter = (hdd_adapter_t *) pCtx;
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
	enum rem_on_channel_request_type req_type;

	if (!hdd_ctx) {
		hdd_err("Invalid HDD context");
		return QDF_STATUS_E_FAILURE;
	}

	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	pRemainChanCtx = cfgState->remain_on_chan_ctx;

	if (pRemainChanCtx == NULL) {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_warn("No Rem on channel pending for which Rsp is received");
		return QDF_STATUS_SUCCESS;
	}

	hdd_debug("Received remain on channel rsp");
	if (qdf_mc_timer_stop(&pRemainChanCtx->hdd_remain_on_chan_timer)
			!= QDF_STATUS_SUCCESS)
		hdd_warn("Failed to stop hdd_remain_on_chan_timer");
	if (qdf_mc_timer_destroy(&pRemainChanCtx->hdd_remain_on_chan_timer)
			!= QDF_STATUS_SUCCESS)
		hdd_warn("Failed to destroy hdd_remain_on_chan_timer");
	cfgState->remain_on_chan_ctx = NULL;
	/*
	 * Resetting the roc in progress early ensures that the subsequent
	 * roc requests are immediately processed without being queued
	 */
	pAdapter->is_roc_inprogress = false;
	qdf_runtime_pm_allow_suspend(&hdd_ctx->runtime_context.roc);
	/*
	 * If the allow suspend is done later, the scheduled roc wil prevent
	 * the system from going into suspend and immediately this logic
	 * will allow the system to go to suspend breaking the exising logic.
	 * Basically, the system must not go into suspend while roc is in
	 * progress.
	 */
	hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);

	if (REMAIN_ON_CHANNEL_REQUEST == pRemainChanCtx->rem_on_chan_request) {
		if (cfgState->buf)
			hdd_debug("Yet to rcv an ack for one of the tx pkt");

		cfg80211_remain_on_channel_expired(
			pRemainChanCtx->dev->
			ieee80211_ptr,
			(u64)pRemainChanCtx->id,
			&pRemainChanCtx->chan,
			GFP_KERNEL);
		pAdapter->last_roc_ts =
			(uint64_t)qdf_mc_timer_get_system_time();
	}
	req_type = pRemainChanCtx->rem_on_chan_request;
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);

	if ((QDF_STA_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)
	    ) {
		uint8_t sessionId = pAdapter->sessionId;

		if (REMAIN_ON_CHANNEL_REQUEST == req_type) {
			sme_deregister_mgmt_frame(hHal, sessionId,
						  (SIR_MAC_MGMT_FRAME << 2) |
						  (SIR_MAC_MGMT_PROBE_REQ << 4),
						  NULL, 0);
		}
		hdd_delete_all_action_frame_cookies(pAdapter);
	} else if ((QDF_SAP_MODE == pAdapter->device_mode) ||
		   (QDF_P2P_GO_MODE == pAdapter->device_mode)
		   ) {
		wlansap_de_register_mgmt_frame(
			WLAN_HDD_GET_SAP_CTX_PTR(pAdapter),
			(SIR_MAC_MGMT_FRAME << 2) |
			(SIR_MAC_MGMT_PROBE_REQ << 4),
			 NULL, 0);

	}

	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	if (pRemainChanCtx) {
		if (pRemainChanCtx->action_pkt_buff.frame_ptr != NULL
		    && pRemainChanCtx->action_pkt_buff.frame_length != 0) {
			qdf_mem_free(pRemainChanCtx->action_pkt_buff.frame_ptr);
			pRemainChanCtx->action_pkt_buff.frame_ptr = NULL;
			pRemainChanCtx->action_pkt_buff.frame_length = 0;
		}
		qdf_idr_remove(&hdd_ctx->p2p_idr, pRemainChanCtx->id);
	}
	qdf_mem_free(pRemainChanCtx);
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
	complete(&pAdapter->cancel_rem_on_chan_var);
	if (QDF_STATUS_SUCCESS != status)
		complete(&pAdapter->rem_on_chan_ready_event);

	/* If we schedule work queue to start new RoC before completing
	 * cancel_rem_on_chan_var then the work queue may immediately get
	 * scheduled and update cfgState->remain_on_chan_ctx which is referred
	 * in mgmt_tx. Due to this update the the mgmt_tx may extend the roc
	 * which was already completed. This will lead to mgmt tx failure.
	 * Always schedule below work queue only after completing the
	 * cancel_rem_on_chan_var event.
	 */
	schedule_delayed_work(&hdd_ctx->roc_req_work, 0);

	return QDF_STATUS_SUCCESS;
}

void wlan_hdd_cancel_existing_remain_on_channel(hdd_adapter_t *pAdapter)
{
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
	unsigned long rc;
	uint32_t roc_scan_id;

	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	if (cfgState->remain_on_chan_ctx != NULL) {
		hdd_debug("Cancel Existing Remain on Channel");

		if (QDF_TIMER_STATE_RUNNING == qdf_mc_timer_get_current_state(
		    &cfgState->remain_on_chan_ctx->hdd_remain_on_chan_timer)) {
			if (qdf_mc_timer_stop(&cfgState->remain_on_chan_ctx->
				hdd_remain_on_chan_timer) != QDF_STATUS_SUCCESS)
				hdd_warn("Failed to stop hdd_remain_on_chan_timer");
		}

		pRemainChanCtx = cfgState->remain_on_chan_ctx;
		if (pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress ==
			true) {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			hdd_warn("ROC timer cancellation in progress wait for completion");
			rc = wait_for_completion_timeout(&pAdapter->
							 cancel_rem_on_chan_var,
							 msecs_to_jiffies
								 (WAIT_CANCEL_REM_CHAN));
			if (!rc)
				hdd_err("wait on cancel_rem_on_chan_var timed out");

			return;
		}
		pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress = true;
		roc_scan_id = pRemainChanCtx->scan_id;
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		/* Wait till remain on channel ready indication before
		 * issuing cancel remain on channel request, otherwise
		 * if remain on channel not received and if the driver issues
		 * cancel remain on channel then lim will be in unknown state.
		 */
		rc = wait_for_completion_timeout(&pAdapter->
						 rem_on_chan_ready_event,
						 msecs_to_jiffies
							 (WAIT_REM_CHAN_READY));
		if (!rc) {
			hdd_err("timeout waiting for remain on channel ready indication");
			cds_flush_logs(WLAN_LOG_TYPE_FATAL,
				WLAN_LOG_INDICATOR_HOST_DRIVER,
				WLAN_LOG_REASON_HDD_TIME_OUT,
				true, false);
		}

		INIT_COMPLETION(pAdapter->cancel_rem_on_chan_var);

		/* Issue abort remain on chan request to sme.
		 * The remain on channel callback will make sure the
		 * remain_on_chan expired event is sent.
		 */
		if ((QDF_STA_MODE == pAdapter->device_mode) ||
		    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
		    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)
		    ) {
			hdd_delete_all_action_frame_cookies(pAdapter);
			sme_cancel_remain_on_channel(WLAN_HDD_GET_HAL_CTX
							     (pAdapter),
				pAdapter->sessionId, roc_scan_id);
		} else if ((QDF_SAP_MODE == pAdapter->device_mode)
			   || (QDF_P2P_GO_MODE == pAdapter->device_mode)
			   ) {
			wlansap_cancel_remain_on_channel(
				WLAN_HDD_GET_SAP_CTX_PTR(pAdapter), roc_scan_id);
		}

		rc = wait_for_completion_timeout(&pAdapter->
						 cancel_rem_on_chan_var,
						 msecs_to_jiffies
							 (WAIT_CANCEL_REM_CHAN));

		if (!rc)
			hdd_err("timeout for cancel ROC ready indication");

		qdf_runtime_pm_allow_suspend(&hdd_ctx->runtime_context.roc);
		hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
	} else
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
}

int wlan_hdd_check_remain_on_channel(hdd_adapter_t *pAdapter)
{
	int status = 0;
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);

	if (QDF_P2P_GO_MODE != pAdapter->device_mode) {
		/* Cancel Existing Remain On Channel */
		/* If no action frame is pending */
		if (cfgState->remain_on_chan_ctx != NULL) {
			/* Check whether Action Frame is pending or not */
			if (cfgState->buf == NULL) {
				wlan_hdd_cancel_existing_remain_on_channel
					(pAdapter);
			} else {
				hdd_debug("Cannot Cancel Existing Remain on Channel");
				status = -EBUSY;
			}
		}
	}
	return status;
}

/**
 * wlan_hdd_cancel_pending_roc() - Cancel pending roc
 * @adapter: HDD adapter
 *
 * Cancels any pending remain on channel request
 *
 * Return: None
 */
static void wlan_hdd_cancel_pending_roc(hdd_adapter_t *adapter)
{
	hdd_remain_on_chan_ctx_t *roc_ctx;
	unsigned long rc;
	hdd_cfg80211_state_t *cfg_state = WLAN_HDD_GET_CFG_STATE_PTR(adapter);
	uint32_t roc_scan_id;

	hdd_debug("ROC completion is not received !!!");

	mutex_lock(&cfg_state->remain_on_chan_ctx_lock);
	roc_ctx = cfg_state->remain_on_chan_ctx;

	if (!roc_ctx) {
		mutex_unlock(&cfg_state->remain_on_chan_ctx_lock);
		hdd_debug("roc_ctx is NULL, No pending RoC");
		return;
	}

	if (roc_ctx->hdd_remain_on_chan_cancel_in_progress) {
		mutex_unlock(&cfg_state->remain_on_chan_ctx_lock);
		hdd_debug("roc cancel already in progress");
		/*
		 * Since a cancel roc is already issued and is
		 * in progress, we need not send another
		 * cancel roc again. Instead we can just wait
		 * for cancel roc completion
		 */
		goto wait;
	}
	roc_scan_id = roc_ctx->scan_id;
	mutex_unlock(&cfg_state->remain_on_chan_ctx_lock);

	if (adapter->device_mode == QDF_P2P_GO_MODE) {
		void *sap_ctx = WLAN_HDD_GET_SAP_CTX_PTR(adapter);

		wlansap_cancel_remain_on_channel(sap_ctx, roc_scan_id);
	} else if (adapter->device_mode == QDF_P2P_CLIENT_MODE ||
		   adapter->device_mode == QDF_P2P_DEVICE_MODE) {
		hdd_delete_all_action_frame_cookies(adapter);
		sme_cancel_remain_on_channel(WLAN_HDD_GET_HAL_CTX
				(adapter),
				adapter->sessionId, roc_scan_id);
	}

wait:
	rc = wait_for_completion_timeout(&adapter->cancel_rem_on_chan_var,
			msecs_to_jiffies
			(WAIT_CANCEL_REM_CHAN));
	if (!rc) {
		hdd_err("Timeout occurred while waiting for RoC Cancellation");
		mutex_lock(&cfg_state->remain_on_chan_ctx_lock);
		roc_ctx = cfg_state->remain_on_chan_ctx;
		if (roc_ctx != NULL) {
			cfg_state->remain_on_chan_ctx = NULL;
			if (qdf_mc_timer_stop(&roc_ctx->
				hdd_remain_on_chan_timer)
				!= QDF_STATUS_SUCCESS)
				hdd_err("Failed to stop hdd_remain_on_chan_timer");
			if (qdf_mc_timer_destroy(
				&roc_ctx->hdd_remain_on_chan_timer)
				!= QDF_STATUS_SUCCESS)
				hdd_err("Failed to destroy hdd_remain_on_chan_timer");
			if (roc_ctx->action_pkt_buff.frame_ptr != NULL
				&& roc_ctx->action_pkt_buff.frame_length != 0) {
				qdf_mem_free(
					roc_ctx->action_pkt_buff.frame_ptr);
				roc_ctx->action_pkt_buff.frame_ptr = NULL;
				roc_ctx->action_pkt_buff.frame_length = 0;
			}
			qdf_mem_free(roc_ctx);
			adapter->is_roc_inprogress = false;
		}
		mutex_unlock(&cfg_state->remain_on_chan_ctx_lock);
	}
}

/* Clean up RoC context at hdd_stop_adapter*/
void wlan_hdd_cleanup_remain_on_channel_ctx(hdd_adapter_t *pAdapter)
{
	uint8_t retry = 0;
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);

	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	while (pAdapter->is_roc_inprogress) {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_debug("ROC in progress for session %d!!!",
			pAdapter->sessionId);
		msleep(500);
		if (retry++ > 3) {
			wlan_hdd_cancel_pending_roc(pAdapter);
			/* hold the lock before break from the loop */
			mutex_lock(&cfgState->remain_on_chan_ctx_lock);
			break;
		}
		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	} /* end of while */
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);

}

static void wlan_hdd_remain_on_chan_timeout(void *data)
{
	hdd_adapter_t *pAdapter = (hdd_adapter_t *) data;
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	hdd_cfg80211_state_t *cfgState;
	hdd_context_t *hdd_ctx;
	uint32_t roc_scan_id;

	if ((NULL == pAdapter) ||
	    (WLAN_HDD_ADAPTER_MAGIC != pAdapter->magic)) {
		hdd_err("pAdapter is invalid %pK !!!", pAdapter);
		return;
	}

	hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
	cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	pRemainChanCtx = cfgState->remain_on_chan_ctx;

	if (NULL == pRemainChanCtx) {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_err("No Remain on channel is pending");
		return;
	}

	if (true == pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress) {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_err("Cancellation already in progress");
		return;
	}

	pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress = true;
	roc_scan_id = pRemainChanCtx->scan_id;
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
	hdd_debug("Cancel Remain on Channel on timeout");

	if ((QDF_STA_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)) {
		hdd_delete_all_action_frame_cookies(pAdapter);
		sme_cancel_remain_on_channel(WLAN_HDD_GET_HAL_CTX(pAdapter),
			pAdapter->sessionId, roc_scan_id);
	} else if ((QDF_SAP_MODE == pAdapter->device_mode) ||
			(QDF_P2P_GO_MODE == pAdapter->device_mode)) {
		void *sap_ctx = WLAN_HDD_GET_SAP_CTX_PTR(pAdapter);

		wlansap_cancel_remain_on_channel(sap_ctx, roc_scan_id);
	}

	hdd_tdls_notify_p2p_roc(hdd_ctx, P2P_ROC_END);
	qdf_runtime_pm_allow_suspend(&hdd_ctx->runtime_context.roc);

	hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
}

static int wlan_hdd_execute_remain_on_channel(hdd_adapter_t *pAdapter,
		      hdd_remain_on_chan_ctx_t *pRemainChanCtx)
{
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	QDF_STATUS qdf_status = QDF_STATUS_E_FAILURE;
	hdd_context_t *pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
	hdd_adapter_list_node_t *pAdapterNode = NULL, *pNext = NULL;
	hdd_adapter_t *pAdapter_temp;
	QDF_STATUS status;
	bool isGoPresent = false;
	unsigned int duration;


	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	if (pAdapter->is_roc_inprogress == true) {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_err("remain on channel request is in execution");
		return -EBUSY;
	}

	cfgState->remain_on_chan_ctx = pRemainChanCtx;
	cfgState->current_freq = pRemainChanCtx->chan.center_freq;
	pAdapter->is_roc_inprogress = true;
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);

	/* Initialize Remain on chan timer */
	qdf_status =
		qdf_mc_timer_init(&pRemainChanCtx->hdd_remain_on_chan_timer,
				  QDF_TIMER_TYPE_SW,
				  wlan_hdd_remain_on_chan_timeout, pAdapter);
	if (qdf_status != QDF_STATUS_SUCCESS) {
		hdd_err("Not able to initialize remain_on_chan timer");
		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
		cfgState->remain_on_chan_ctx = NULL;
		pAdapter->is_roc_inprogress = false;
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		qdf_mem_free(pRemainChanCtx);
		return -EINVAL;
	}

	status = hdd_get_front_adapter(pHddCtx, &pAdapterNode);
	while (NULL != pAdapterNode && QDF_STATUS_SUCCESS == status) {
		pAdapter_temp = pAdapterNode->pAdapter;
		if (pAdapter_temp->device_mode == QDF_P2P_GO_MODE)
			isGoPresent = true;
		status = hdd_get_next_adapter(pHddCtx, pAdapterNode, &pNext);
		pAdapterNode = pNext;
	}

	/* Extending duration for proactive extension logic for RoC */
	duration = pRemainChanCtx->duration;
	if (duration < HDD_P2P_MAX_ROC_DURATION) {
		if (isGoPresent == true)
			duration *= P2P_ROC_DURATION_MULTIPLIER_GO_PRESENT;
		else
			duration *= P2P_ROC_DURATION_MULTIPLIER_GO_ABSENT;
	}

	hdd_prevent_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
	qdf_runtime_pm_prevent_suspend(&pHddCtx->runtime_context.roc);
	INIT_COMPLETION(pAdapter->rem_on_chan_ready_event);

	/* call sme API to start remain on channel. */
	if ((QDF_STA_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)
	    ) {
		uint8_t sessionId = pAdapter->sessionId;
		/* call sme API to start remain on channel. */

		if (QDF_STATUS_SUCCESS != sme_remain_on_channel(
				WLAN_HDD_GET_HAL_CTX(pAdapter),
				sessionId,
				pRemainChanCtx->chan.hw_value, duration,
				wlan_hdd_remain_on_channel_callback,
				pAdapter,
				(pRemainChanCtx->rem_on_chan_request ==
				 REMAIN_ON_CHANNEL_REQUEST) ? true : false,
				 &pRemainChanCtx->scan_id)) {
			hdd_err("sme_remain_on_channel failed");
			mutex_lock(&cfgState->remain_on_chan_ctx_lock);
			pAdapter->is_roc_inprogress = false;
			pRemainChanCtx = cfgState->remain_on_chan_ctx;
			hdd_debug("Freeing ROC ctx cfgState->remain_on_chan_ctx=%pK",
				 cfgState->remain_on_chan_ctx);
			if (pRemainChanCtx) {
				if (qdf_mc_timer_destroy(
					&pRemainChanCtx->
						hdd_remain_on_chan_timer)
						!= QDF_STATUS_SUCCESS)
					hdd_err("Failed to destroy hdd_remain_on_chan_timer");
				qdf_mem_free(pRemainChanCtx);
				cfgState->remain_on_chan_ctx = NULL;
			}
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			qdf_runtime_pm_allow_suspend(&pHddCtx->runtime_context.
						     roc);
			hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
			return -EINVAL;
		}

		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
		pRemainChanCtx = cfgState->remain_on_chan_ctx;
		if ((pRemainChanCtx) && (REMAIN_ON_CHANNEL_REQUEST ==
		    pRemainChanCtx->rem_on_chan_request)) {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			if (QDF_STATUS_SUCCESS != sme_register_mgmt_frame(
			    WLAN_HDD_GET_HAL_CTX(pAdapter), sessionId,
			    (SIR_MAC_MGMT_FRAME << 2) |
			    (SIR_MAC_MGMT_PROBE_REQ << 4), NULL, 0))
				hdd_err("sme_register_mgmt_frame failed");
		} else {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		}
	} else if ((QDF_SAP_MODE == pAdapter->device_mode) ||
		   (QDF_P2P_GO_MODE == pAdapter->device_mode)) {
		/* call sme API to start remain on channel. */
		if (QDF_STATUS_SUCCESS != wlansap_remain_on_channel(
			    WLAN_HDD_GET_SAP_CTX_PTR(pAdapter),
			    pRemainChanCtx->chan.hw_value,
			    duration, wlan_hdd_remain_on_channel_callback,
			    pAdapter, &pRemainChanCtx->scan_id)) {
			hdd_err("wlansap_remain_on_channel failed");
			mutex_lock(&cfgState->remain_on_chan_ctx_lock);
			pAdapter->is_roc_inprogress = false;
			pRemainChanCtx = cfgState->remain_on_chan_ctx;
			hdd_debug("Freeing ROC ctx cfgState->remain_on_chan_ctx=%pK",
				 cfgState->remain_on_chan_ctx);
			if (pRemainChanCtx) {
				if (qdf_mc_timer_destroy(
					&pRemainChanCtx->
					hdd_remain_on_chan_timer)
					!= QDF_STATUS_SUCCESS)
					hdd_err("Failed to destroy hdd_remain_on_chan_timer");
				qdf_mem_free(pRemainChanCtx);
				cfgState->remain_on_chan_ctx = NULL;
			}
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			qdf_runtime_pm_allow_suspend(&pHddCtx->runtime_context.
						     roc);
			hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
			return -EINVAL;
		}

		if (QDF_STATUS_SUCCESS != wlansap_register_mgmt_frame(
			WLAN_HDD_GET_SAP_CTX_PTR(pAdapter),
			(SIR_MAC_MGMT_FRAME << 2) |
			(SIR_MAC_MGMT_PROBE_REQ << 4), NULL, 0)) {
			hdd_err("wlansap_register_mgmt_frame return fail");
			wlansap_cancel_remain_on_channel(
				WLAN_HDD_GET_SAP_CTX_PTR(pAdapter),
				pRemainChanCtx->scan_id);
			qdf_runtime_pm_allow_suspend(&pHddCtx->runtime_context.
						     roc);
			hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);
			return -EINVAL;
		}

	}
	hdd_tdls_notify_p2p_roc(pHddCtx, P2P_ROC_START);
	return 0;
}

/**
 * wlan_hdd_roc_request_enqueue() - enqueue remain on channel request
 * @adapter: Pointer to the adapter
 * @remain_chan_ctx: Pointer to the remain on channel context
 *
 * Return: 0 on success, error number otherwise
 */
static int wlan_hdd_roc_request_enqueue(hdd_adapter_t *adapter,
			hdd_remain_on_chan_ctx_t *remain_chan_ctx)
{
	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(adapter);
	hdd_roc_req_t *hdd_roc_req;
	QDF_STATUS status;

	/*
	 * "Driver is busy" OR "there is already RoC request inside the queue"
	 * so enqueue this RoC Request and execute sequentially later.
	 */

	hdd_roc_req = qdf_mem_malloc(sizeof(*hdd_roc_req));

	if (NULL == hdd_roc_req) {
		hdd_err("malloc failed for roc req context");
		return -ENOMEM;
	}

	hdd_roc_req->pAdapter = adapter;
	hdd_roc_req->pRemainChanCtx = remain_chan_ctx;

	/* Enqueue this RoC request */
	qdf_spin_lock(&hdd_ctx->hdd_roc_req_q_lock);
	status = qdf_list_insert_back(&hdd_ctx->hdd_roc_req_q,
					&hdd_roc_req->node);
	qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);

	if (QDF_STATUS_SUCCESS != status) {
		hdd_err("Not able to enqueue RoC Req context");
		qdf_mem_free(hdd_roc_req);
		return -EINVAL;
	}

	return 0;
}

/**
 * wlan_hdd_indicate_roc_drop() - Indicate roc drop to userspace
 * @adapter: HDD adapter
 * @ctx: Remain on channel context
 *
 * Send remain on channel ready and cancel event for the queued
 * roc that is being dropped. This will ensure that the userspace
 * will send more roc requests. If this drop is not indicated to
 * userspace, subsequent roc will not be sent to the driver since
 * the userspace times out waiting for the remain on channel ready
 * event.
 *
 * Return: None
 */
static void wlan_hdd_indicate_roc_drop(hdd_adapter_t *adapter,
				       hdd_remain_on_chan_ctx_t *ctx)
{
	hdd_debug("indicate roc drop to userspace");
	cfg80211_ready_on_channel(
			adapter->dev->ieee80211_ptr,
			(u64)ctx->id,
			&ctx->chan,
			ctx->duration, GFP_KERNEL);

	cfg80211_remain_on_channel_expired(
			ctx->dev->ieee80211_ptr,
			(u64)ctx->id,
			&ctx->chan,
			GFP_KERNEL);
}

/**
 * wlan_hdd_roc_request_dequeue() - dequeue remain on channel request
 * @work: Pointer to work queue struct
 *
 * Return: none
 */
void wlan_hdd_roc_request_dequeue(struct work_struct *work)
{
	QDF_STATUS status;
	int ret = 0;
	hdd_roc_req_t *hdd_roc_req;
	hdd_context_t *hdd_ctx =
			container_of(work, hdd_context_t, roc_req_work.work);

	hdd_debug("going to dequeue roc");

	if (0 != (wlan_hdd_validate_context(hdd_ctx)))
		return;

	/*
	 * The queued roc requests is dequeued and processed one at a time.
	 * Callback 'wlan_hdd_remain_on_channel_callback' ensures
	 * that any pending roc in the queue will be scheduled
	 * on the current roc completion by scheduling the work queue.
	 */
	qdf_spin_lock(&hdd_ctx->hdd_roc_req_q_lock);
	if (list_empty(&hdd_ctx->hdd_roc_req_q.anchor)) {
		qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);
		return;
	}
	status = qdf_list_remove_front(&hdd_ctx->hdd_roc_req_q,
			(qdf_list_node_t **) &hdd_roc_req);
	qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);
	if (QDF_STATUS_SUCCESS != status) {
		hdd_debug("unable to remove roc element from list");
		return;
	}
	ret = wlan_hdd_execute_remain_on_channel(
			hdd_roc_req->pAdapter,
			hdd_roc_req->pRemainChanCtx);
	if (ret == -EBUSY) {
		hdd_err("dropping RoC request");
		wlan_hdd_indicate_roc_drop(hdd_roc_req->pAdapter,
					   hdd_roc_req->pRemainChanCtx);
		qdf_mem_free(hdd_roc_req->pRemainChanCtx);
	}
	qdf_mem_free(hdd_roc_req);
}

/**
 * wlan_hdd_is_roc_in_progress_for_other_adapters() - Check if roc is in
 *	progress for another adapter
 * @hdd_ctx: HDD context
 * @cur_adapter: current adapter
 *
 * Roc requests are serialized per adapter. This means that simultaneous
 * roc requests on multiple adapters are not supported. This function checks
 * and returns if there is an roc being executed on another adapter.
 *
 * Return: true if roc is ongoing for another adapter, false otherwise.
 */
static bool
wlan_hdd_is_roc_in_progress_for_other_adapters(hdd_context_t *hdd_ctx,
						hdd_adapter_t *cur_adapter)
{
	hdd_adapter_list_node_t *adapter_node = NULL, *next = NULL;
	hdd_adapter_t *adapter;
	QDF_STATUS qdf_status;

	qdf_status = hdd_get_front_adapter(hdd_ctx, &adapter_node);

	while ((NULL != adapter_node) && (QDF_STATUS_SUCCESS == qdf_status)) {
		adapter = adapter_node->pAdapter;
		if (cur_adapter != adapter) {
			if (adapter->is_roc_inprogress)
				return true;
		}

		qdf_status = hdd_get_next_adapter(hdd_ctx, adapter_node, &next);
		adapter_node = next;
	}

	return false;
}

/**
 * wlan_hdd_is_roc_req_queued_by_other_adapters() - Check if an roc req is
 *	queued by another adapter
 * @hdd_ctx: HDD context
 * @cur_adapter: current adapter
 *
 * Roc requests are serialized per adapter. This means that simultaneous
 * roc requests on multiple adapters are not supported. This function checks
 * and returns if there is an roc request queued by another adapter.
 *
 * Return: true if roc is queued by another adapter, false otherwise.
 */
static bool
wlan_hdd_is_roc_req_queued_by_other_adapters(hdd_context_t *hdd_ctx,
					     hdd_adapter_t *cur_adapter)
{
	qdf_list_node_t *node = NULL, *next_node = NULL;
	hdd_roc_req_t *roc_req;

	qdf_spin_lock(&hdd_ctx->hdd_roc_req_q_lock);
	if (list_empty(&hdd_ctx->hdd_roc_req_q.anchor)) {
		qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);
		return false;
	}
	if (QDF_STATUS_SUCCESS != qdf_list_peek_front(&hdd_ctx->hdd_roc_req_q,
						      &next_node)) {
		qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);
		hdd_err("Unable to peek roc element from list");
		return false;
	}

	do {
		node = next_node;
		roc_req = qdf_container_of(node, hdd_roc_req_t, node);
		if (roc_req->pAdapter != cur_adapter) {
			qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);
			return true;
		}

	} while (QDF_STATUS_SUCCESS  == qdf_list_peek_next(
							&hdd_ctx->hdd_roc_req_q,
							node, &next_node));
	qdf_spin_unlock(&hdd_ctx->hdd_roc_req_q_lock);

	return false;
}

static int wlan_hdd_request_remain_on_channel(struct wiphy *wiphy,
					      struct net_device *dev,
					      struct ieee80211_channel *chan,
					      unsigned int duration,
					      u64 *cookie,
					      enum rem_on_channel_request_type
					      request_type)
{
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	hdd_context_t *pHddCtx;
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	bool isBusy = false;
	uint32_t size = 0;
	hdd_adapter_t *sta_adapter;
	int ret;
	int status = 0;
	int roc_id;

	hdd_debug("Device_mode %s(%d)",
		   hdd_device_mode_to_string(pAdapter->device_mode),
		   pAdapter->device_mode);
	hdd_debug("chan(hw_val)0x%x chan(centerfreq) %d, duration %d",
		 chan->hw_value, chan->center_freq, duration);

	pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
	ret = wlan_hdd_validate_context(pHddCtx);
	if (0 != ret)
		return ret;

	if ((wlan_hdd_is_roc_in_progress_for_other_adapters(pHddCtx, pAdapter))
	   || (wlan_hdd_is_roc_req_queued_by_other_adapters(pHddCtx, pAdapter))
		) {
		hdd_debug("ROC in progress or queued for another adapter");
		return -EAGAIN;
	}
	if (cds_is_connection_in_progress(NULL, NULL)) {
		hdd_debug("Connection is in progress");
		if (request_type == OFF_CHANNEL_ACTION_TX) {
			hdd_debug("Reject Offchannel action frame tx as conection in progress");
			return -EAGAIN;
		}
		isBusy = true;
	}
	pRemainChanCtx = qdf_mem_malloc(sizeof(hdd_remain_on_chan_ctx_t));
	if (NULL == pRemainChanCtx) {
		hdd_err("Not able to allocate memory for Channel context");
		return -ENOMEM;
	}

	if (QDF_STATUS_SUCCESS != qdf_idr_alloc(&pHddCtx->p2p_idr,
		pRemainChanCtx, &roc_id)) {
		hdd_err("alloc id fail");
		qdf_mem_free(pRemainChanCtx);
		return -EFAULT;
	}
	qdf_mem_copy(&pRemainChanCtx->chan, chan,
		     sizeof(struct ieee80211_channel));
	pRemainChanCtx->duration = duration;
	pRemainChanCtx->dev = dev;
	*cookie = (u64)roc_id;
	pRemainChanCtx->id = roc_id;
	pRemainChanCtx->rem_on_chan_request = request_type;
	pRemainChanCtx->action_pkt_buff.freq = 0;
	pRemainChanCtx->action_pkt_buff.frame_ptr = NULL;
	pRemainChanCtx->action_pkt_buff.frame_length = 0;
	pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress = false;
	if (REMAIN_ON_CHANNEL_REQUEST == request_type) {
		sta_adapter = hdd_get_adapter(pHddCtx, QDF_STA_MODE);
		if ((NULL != sta_adapter) &&
			hdd_conn_is_connected(
				WLAN_HDD_GET_STATION_CTX_PTR(sta_adapter))) {
			if (pAdapter->last_roc_ts != 0 &&
				(((uint64_t)qdf_mc_timer_get_system_time() -
					 pAdapter->last_roc_ts) <
				pHddCtx->config->p2p_listen_defer_interval)) {
			if (pRemainChanCtx->duration > HDD_P2P_MAX_ROC_DURATION)
				pRemainChanCtx->duration =
						HDD_P2P_MAX_ROC_DURATION;

			wlan_hdd_roc_request_enqueue(pAdapter, pRemainChanCtx);
			schedule_delayed_work(&pHddCtx->roc_req_work,
			msecs_to_jiffies(
				pHddCtx->config->p2p_listen_defer_interval));
			hdd_debug("Defer interval is %hu, pAdapter %pK",
				pHddCtx->config->p2p_listen_defer_interval,
				pAdapter);
			return 0;
			}
		}
	}

	qdf_spin_lock(&pHddCtx->hdd_roc_req_q_lock);
	size = qdf_list_size(&(pHddCtx->hdd_roc_req_q));
	qdf_spin_unlock(&pHddCtx->hdd_roc_req_q_lock);
	if ((isBusy == false) && (!size)) {
		status = wlan_hdd_execute_remain_on_channel(pAdapter,
							    pRemainChanCtx);
		if (status == -EBUSY) {
			if (wlan_hdd_roc_request_enqueue(pAdapter,
							 pRemainChanCtx)) {
				qdf_mem_free(pRemainChanCtx);
				return -EAGAIN;
			}
		}
		return 0;
	}
	if (wlan_hdd_roc_request_enqueue(pAdapter, pRemainChanCtx)) {
		qdf_mem_free(pRemainChanCtx);
		return -EAGAIN;
	}

	/*
	 * If a connection is not in progress (isBusy), before scheduling
	 * the work queue it is necessary to check if a roc in in progress
	 * or not because: if an roc is in progress, the dequeued roc
	 * that will be processed will be dropped. To ensure that this new
	 * roc request is not dropped, it is suggested to check if an roc
	 * is in progress or not. The existing roc completion will provide
	 * the trigger to dequeue the next roc request.
	 */
	if (isBusy == false && pAdapter->is_roc_inprogress == false) {
		hdd_debug("scheduling delayed work: no connection/roc active");
		schedule_delayed_work(&pHddCtx->roc_req_work, 0);
	}
	return 0;
}

static int __wlan_hdd_cfg80211_remain_on_channel(struct wiphy *wiphy,
						 struct wireless_dev *wdev,
						 struct ieee80211_channel *chan,
						 unsigned int duration,
						 u64 *cookie)
{
	struct net_device *dev = wdev->netdev;
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	hdd_context_t *hdd_ctx;
	int ret;

	ENTER();

	hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
	ret = wlan_hdd_validate_context(hdd_ctx);
	if (0 != ret)
		return ret;

	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam()) {
		hdd_err("Command not allowed in FTM mode");
		return -EINVAL;
	}

	if (wlan_hdd_validate_session_id(pAdapter->sessionId)) {
		hdd_err("invalid session id: %d", pAdapter->sessionId);
		return -EINVAL;
	}

	MTRACE(qdf_trace(QDF_MODULE_ID_HDD,
			 TRACE_CODE_HDD_REMAIN_ON_CHANNEL,
			 pAdapter->sessionId, REMAIN_ON_CHANNEL_REQUEST));

	ret = wlan_hdd_request_remain_on_channel(wiphy, dev, chan,
						  duration, cookie,
						  REMAIN_ON_CHANNEL_REQUEST);
	EXIT();
	return ret;
}

int wlan_hdd_cfg80211_remain_on_channel(struct wiphy *wiphy,
					struct wireless_dev *wdev,
					struct ieee80211_channel *chan,
					unsigned int duration, u64 *cookie)
{
	int ret;

	cds_ssr_protect(__func__);
	ret = __wlan_hdd_cfg80211_remain_on_channel(wiphy,
						    wdev,
						    chan,
						    duration, cookie);
	cds_ssr_unprotect(__func__);

	return ret;
}

void hdd_remain_chan_ready_handler(hdd_adapter_t *pAdapter,
	uint32_t scan_id)
{
	hdd_cfg80211_state_t *cfgState = NULL;
	hdd_remain_on_chan_ctx_t *pRemainChanCtx = NULL;
	QDF_STATUS status;

	if (NULL == pAdapter) {
		hdd_err("pAdapter is NULL");
		return;
	}
	cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_debug("Ready on chan ind %d", scan_id);

	pAdapter->start_roc_ts = (uint64_t)qdf_mc_timer_get_system_time();
	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	pRemainChanCtx = cfgState->remain_on_chan_ctx;
	if (pRemainChanCtx != NULL) {
		MTRACE(qdf_trace(QDF_MODULE_ID_HDD,
				 TRACE_CODE_HDD_REMAINCHANREADYHANDLER,
				 pAdapter->sessionId,
				 pRemainChanCtx->duration));
		/* start timer for actual duration */
		if (QDF_TIMER_STATE_RUNNING ==
			qdf_mc_timer_get_current_state(
				&pRemainChanCtx->hdd_remain_on_chan_timer)) {
			hdd_debug("Timer Started before ready event!!!");
			if (qdf_mc_timer_stop(&pRemainChanCtx->
						hdd_remain_on_chan_timer)
					!= QDF_STATUS_SUCCESS)
				hdd_err("Failed to stop hdd_remain_on_chan_timer");
		}
		status =
			qdf_mc_timer_start(&pRemainChanCtx->
					   hdd_remain_on_chan_timer,
					   (pRemainChanCtx->duration +
					    COMPLETE_EVENT_PROPOGATE_TIME));
		if (status != QDF_STATUS_SUCCESS)
			hdd_err("Remain on Channel timer start failed");

		if (REMAIN_ON_CHANNEL_REQUEST ==
		    pRemainChanCtx->rem_on_chan_request) {
			cfg80211_ready_on_channel(
				pAdapter->dev->
				ieee80211_ptr,
				(u64)pRemainChanCtx->id,
				&pRemainChanCtx->chan,
				pRemainChanCtx->
				duration, GFP_KERNEL);
		} else if (OFF_CHANNEL_ACTION_TX ==
			   pRemainChanCtx->rem_on_chan_request) {
			complete(&pAdapter->offchannel_tx_event);
		}
		/* Check for cached action frame */
		if (pRemainChanCtx->action_pkt_buff.frame_length != 0) {
			hdd_debug("Sent cached action frame to supplicant");
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
			cfg80211_rx_mgmt(pAdapter->dev->ieee80211_ptr,
				pRemainChanCtx->action_pkt_buff.freq, 0,
				pRemainChanCtx->action_pkt_buff.frame_ptr,
				pRemainChanCtx->action_pkt_buff.frame_length,
				NL80211_RXMGMT_FLAG_ANSWERED);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
			cfg80211_rx_mgmt(pAdapter->dev->ieee80211_ptr,
				pRemainChanCtx->action_pkt_buff.freq, 0,
				pRemainChanCtx->action_pkt_buff.frame_ptr,
				pRemainChanCtx->action_pkt_buff.frame_length,
				NL80211_RXMGMT_FLAG_ANSWERED, GFP_ATOMIC);
#else
			cfg80211_rx_mgmt(pAdapter->dev->ieee80211_ptr,
					 pRemainChanCtx->action_pkt_buff.freq,
					 0,
					 pRemainChanCtx->action_pkt_buff.
					 frame_ptr,
					 pRemainChanCtx->action_pkt_buff.
					 frame_length, GFP_ATOMIC);
#endif /* LINUX_VERSION_CODE */

			qdf_mem_free(pRemainChanCtx->action_pkt_buff.frame_ptr);
			pRemainChanCtx->action_pkt_buff.frame_length = 0;
			pRemainChanCtx->action_pkt_buff.freq = 0;
			pRemainChanCtx->action_pkt_buff.frame_ptr = NULL;
		}
		complete(&pAdapter->rem_on_chan_ready_event);
	} else {
		hdd_debug("No Pending Remain on channel Request");
	}
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
}

static int
__wlan_hdd_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
					     struct wireless_dev *wdev,
					     u64 cookie)
{
	struct net_device *dev = wdev->netdev;
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	hdd_remain_on_chan_ctx_t *cur_ctx;
	hdd_context_t *pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
	int status;
	int qdf_status;
	unsigned long rc;
	qdf_list_node_t *tmp, *q;
	hdd_roc_req_t *curr_roc_req;
	uint32_t roc_scan_id;

	ENTER();

	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam()) {
		hdd_err("Command not allowed in FTM mode");
		return -EINVAL;
	}

	if (wlan_hdd_validate_session_id(pAdapter->sessionId)) {
		hdd_err("invalid session id: %d", pAdapter->sessionId);
		return -EINVAL;
	}

	status = wlan_hdd_validate_context(pHddCtx);

	if (0 != status)
		return status;

	if (QDF_STATUS_SUCCESS != qdf_idr_find(&pHddCtx->p2p_idr,
		(int32_t)cookie, (void **)&cur_ctx)) {
		hdd_debug("failed to find cookie, 0x%llx", cookie);
		return -EFAULT;
	}

	qdf_spin_lock(&pHddCtx->hdd_roc_req_q_lock);
	list_for_each_safe(tmp, q, &pHddCtx->hdd_roc_req_q.anchor) {
		curr_roc_req = list_entry(tmp, hdd_roc_req_t, node);
		if (curr_roc_req->pRemainChanCtx == cur_ctx) {
			qdf_status = qdf_list_remove_node(&pHddCtx->hdd_roc_req_q,
						      (qdf_list_node_t *)
						      curr_roc_req);
			qdf_spin_unlock(&pHddCtx->hdd_roc_req_q_lock);
			if (qdf_status == QDF_STATUS_SUCCESS) {
				qdf_mem_free(curr_roc_req->pRemainChanCtx);
				qdf_mem_free(curr_roc_req);
			}
			return 0;
		}
	}
	qdf_spin_unlock(&pHddCtx->hdd_roc_req_q_lock);
	/* FIXME cancel currently running remain on chan.
	 * Need to check cookie and cancel accordingly
	 */
	mutex_lock(&cfgState->remain_on_chan_ctx_lock);
	pRemainChanCtx = cfgState->remain_on_chan_ctx;

	if (pRemainChanCtx) {
		hdd_debug("action_id = %x, roc id = %x, cookie = %08llx",
				cfgState->action_id, pRemainChanCtx->id,
				cookie);

		if (pRemainChanCtx->id == (int32_t)cookie) {
			/* request to cancel on-going roc */
			if (cfgState->buf) {
				/* Tx frame pending */
				if (cfgState->action_id != (int32_t)cookie) {
					hdd_debug("Cookie matched with RoC cookie but not with tx cookie, indicate expired event for roc");
					/* RoC was extended to accomodate the tx frame */
					if (REMAIN_ON_CHANNEL_REQUEST ==
							pRemainChanCtx->
							rem_on_chan_request) {
					cfg80211_remain_on_channel_expired(
							pRemainChanCtx->dev->
							ieee80211_ptr,
							(u64)pRemainChanCtx->id,
							&pRemainChanCtx->chan,
							GFP_KERNEL);
					}
					pRemainChanCtx->rem_on_chan_request =
						OFF_CHANNEL_ACTION_TX;
					pRemainChanCtx->id =
						cfgState->action_id;
					mutex_unlock(&cfgState->
						remain_on_chan_ctx_lock);
					return 0;
				}
			}
		} else if (cfgState->buf && cfgState->action_id ==
				(int32_t)cookie) {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			hdd_debug("Cookie not matched with RoC cookie but matched with tx cookie, cleanup action frame");
			/*free the buf and return 0*/
			hdd_cleanup_actionframe(pHddCtx, pAdapter);
			return 0;
		} else {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			hdd_debug("No matching cookie");
			return -EINVAL;
		}
	} else {
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		hdd_debug("RoC context is NULL, return success");
		return 0;
	}

	if (NULL != cfgState->remain_on_chan_ctx) {
		if (qdf_mc_timer_stop(&cfgState->remain_on_chan_ctx->
					hdd_remain_on_chan_timer)
				!= QDF_STATUS_SUCCESS)
			hdd_err("Failed to stop hdd_remain_on_chan_timer");
		if (true ==
		    pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress) {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			hdd_debug("ROC timer cancellation in progress, wait for completion");
			rc = wait_for_completion_timeout(&pAdapter->
							 cancel_rem_on_chan_var,
							 msecs_to_jiffies
								 (WAIT_CANCEL_REM_CHAN));
			if (!rc)
				hdd_err("wait on cancel_rem_on_chan_var timed out");

			return 0;
		}
		pRemainChanCtx->hdd_remain_on_chan_cancel_in_progress = true;
	}
	roc_scan_id = pRemainChanCtx->scan_id;
	mutex_unlock(&cfgState->remain_on_chan_ctx_lock);

	/* wait until remain on channel ready event received
	 * for already issued remain on channel request
	 */
	rc = wait_for_completion_timeout(&pAdapter->rem_on_chan_ready_event,
					 msecs_to_jiffies(WAIT_REM_CHAN_READY));
	if (!rc) {
		hdd_err("timeout waiting for remain on channel ready indication");

		if (cds_is_driver_recovering()) {
			hdd_err("Recovery in Progress. State: 0x%x Ignore!!!",
				 cds_get_driver_state());
			return -EAGAIN;
		}
		cds_flush_logs(WLAN_LOG_TYPE_FATAL,
			WLAN_LOG_INDICATOR_HOST_DRIVER,
			WLAN_LOG_REASON_HDD_TIME_OUT,
			true, false);
	}
	INIT_COMPLETION(pAdapter->cancel_rem_on_chan_var);
	/* Issue abort remain on chan request to sme.
	 * The remain on channel callback will make sure the remain_on_chan
	 * expired event is sent.
	 */
	if ((QDF_STA_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)
	    ) {
		uint8_t sessionId = pAdapter->sessionId;

		hdd_delete_all_action_frame_cookies(pAdapter);
		sme_cancel_remain_on_channel(WLAN_HDD_GET_HAL_CTX(pAdapter),
			sessionId, roc_scan_id);
	} else if ((QDF_SAP_MODE == pAdapter->device_mode) ||
		   (QDF_P2P_GO_MODE == pAdapter->device_mode)
		   ) {
		wlansap_cancel_remain_on_channel(
			WLAN_HDD_GET_SAP_CTX_PTR(pAdapter), roc_scan_id);

	} else {
		hdd_err("Invalid device_mode %s(%d)",
			hdd_device_mode_to_string(pAdapter->device_mode),
			pAdapter->device_mode);
		return -EIO;
	}
	rc = wait_for_completion_timeout(&pAdapter->cancel_rem_on_chan_var,
					 msecs_to_jiffies
						 (WAIT_CANCEL_REM_CHAN));
	if (!rc)
		hdd_err("wait on cancel_rem_on_chan_var timed out");

	hdd_allow_suspend(WIFI_POWER_EVENT_WAKELOCK_ROC);

	EXIT();
	return 0;
}

int wlan_hdd_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
					       struct wireless_dev *wdev,
					       u64 cookie)
{
	int ret;

	cds_ssr_protect(__func__);
	ret = __wlan_hdd_cfg80211_cancel_remain_on_channel(wiphy,
							   wdev,
							   cookie);
	cds_ssr_unprotect(__func__);

	return ret;
}

static int __wlan_hdd_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
			      struct ieee80211_channel *chan, bool offchan,
			      unsigned int wait,
			      const u8 *buf, size_t len, bool no_cck,
			      bool dont_wait_for_ack, u64 *cookie)
{
	struct net_device *dev = wdev->netdev;
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_remain_on_chan_ctx_t *pRemainChanCtx;
	hdd_context_t *pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
	uint16_t extendedWait = 0;
	uint8_t type;
	uint8_t subType;
	enum action_frm_type actionFrmType;
	bool noack = 0;
	int status;
	unsigned long rc;
	hdd_adapter_t *goAdapter;
	uint16_t current_freq;
	uint8_t home_ch = 0;
	bool enb_random_mac = false;
	uint32_t mgmt_hdr_len = sizeof(struct ieee80211_hdr_3addr);
	int32_t mgmt_id;
	QDF_STATUS qdf_status;

	ENTER();

	if (len < mgmt_hdr_len + 1) {
		hdd_err("Invalid Length");
		return -EINVAL;
	}

	type = WLAN_HDD_GET_TYPE_FRM_FC(buf[0]);
	subType = WLAN_HDD_GET_SUBTYPE_FRM_FC(buf[0]);

	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam()) {
		hdd_err("Command not allowed in FTM mode");
		return -EINVAL;
	}

	if (wlan_hdd_validate_session_id(pAdapter->sessionId)) {
		hdd_err("invalid session id: %d", pAdapter->sessionId);
		return -EINVAL;
	}

	MTRACE(qdf_trace(QDF_MODULE_ID_HDD,
			 TRACE_CODE_HDD_ACTION, pAdapter->sessionId,
			 pAdapter->device_mode));
	status = wlan_hdd_validate_context(pHddCtx);

	if (0 != status)
		return status;

	hdd_debug("Device_mode %s(%d) type: %d, wait: %d, offchan: %d",
		   hdd_device_mode_to_string(pAdapter->device_mode),
		   pAdapter->device_mode, type, wait, offchan);

	/*
	 * When frame to be transmitted is auth mgmt, then trigger
	 * sme_send_mgmt_tx to send auth frame
	 */
	if ((pAdapter->device_mode == QDF_STA_MODE) &&
	    (type == SIR_MAC_MGMT_FRAME &&
	    subType == SIR_MAC_MGMT_AUTH)) {
		qdf_status = sme_send_mgmt_tx(WLAN_HDD_GET_HAL_CTX(pAdapter),
					      pAdapter->sessionId, buf, len);

		if (QDF_IS_STATUS_SUCCESS(qdf_status))
			return 0;
		else
			return -EINVAL;
	}

	if (type == SIR_MAC_MGMT_FRAME && subType == SIR_MAC_MGMT_ACTION &&
	    len > IEEE80211_MIN_ACTION_SIZE)
		hdd_debug("category: %d, actionId: %d",
			  buf[WLAN_HDD_PUBLIC_ACTION_FRAME_BODY_OFFSET +
			  WLAN_HDD_PUBLIC_ACTION_FRAME_CATEGORY_OFFSET],
			  buf[WLAN_HDD_PUBLIC_ACTION_FRAME_BODY_OFFSET +
			  WLAN_HDD_PUBLIC_ACTION_FRAME_ACTION_OFFSET]);

#ifdef WLAN_FEATURE_P2P_DEBUG
	if ((type == SIR_MAC_MGMT_FRAME) &&
	    (subType == SIR_MAC_MGMT_ACTION) &&
	    wlan_hdd_is_type_p2p_action(&buf
				[WLAN_HDD_PUBLIC_ACTION_FRAME_BODY_OFFSET],
				len - mgmt_hdr_len)) {
		actionFrmType = buf[WLAN_HDD_PUBLIC_ACTION_FRAME_TYPE_OFFSET];
		if (actionFrmType >= MAX_P2P_ACTION_FRAME_TYPE) {
			hdd_debug("[P2P] unknown[%d] ---> OTA to " MAC_ADDRESS_STR,
				actionFrmType,
				MAC_ADDR_ARRAY(&buf
					       [WLAN_HDD_80211_FRM_DA_OFFSET]));
		} else {
			hdd_debug("[P2P] %s ---> OTA to "
			       MAC_ADDRESS_STR,
			       p2p_action_frame_type[actionFrmType],
			       MAC_ADDR_ARRAY(&buf
					      [WLAN_HDD_80211_FRM_DA_OFFSET]));
			if ((actionFrmType == WLAN_HDD_PROV_DIS_REQ)
			    && (global_p2p_connection_status == P2P_NOT_ACTIVE)) {
				global_p2p_connection_status = P2P_GO_NEG_PROCESS;
				hdd_debug("[P2P State]Inactive state to GO negotiation progress state");
			} else if ((actionFrmType == WLAN_HDD_GO_NEG_CNF) &&
				   (global_p2p_connection_status ==
				    P2P_GO_NEG_PROCESS)) {
				global_p2p_connection_status =
					P2P_GO_NEG_COMPLETED;
				hdd_debug("[P2P State]GO nego progress to GO nego completed state");
			}
		}
	}
#endif

	noack = dont_wait_for_ack;

	/* If the wait is coming as 0 with off channel set */
	/* then set the wait to 200 ms */
	if (offchan && !wait) {
		wait = ACTION_FRAME_DEFAULT_WAIT;
		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
		if (cfgState->remain_on_chan_ctx) {
			uint64_t current_time =
				(uint64_t)qdf_mc_timer_get_system_time();
			int remaining_roc_time =
				((int) cfgState->remain_on_chan_ctx->duration -
				(current_time - pAdapter->start_roc_ts));

			if (remaining_roc_time > ACTION_FRAME_DEFAULT_WAIT)
				wait = remaining_roc_time;
		}
		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
	}

	if ((QDF_STA_MODE == pAdapter->device_mode) &&
		(type == SIR_MAC_MGMT_FRAME &&
		subType == SIR_MAC_MGMT_PROBE_RSP)) {
			/* Drop Probe response received
			 * from supplicant in sta mode
			 */
			goto err_rem_channel;
	}

	/* Call sme API to send out a action frame. */
	/* OR can we send it directly through data path?? */
	/* After tx completion send tx status back. */
	if ((QDF_SAP_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_GO_MODE == pAdapter->device_mode)
	    ) {
		if (type == SIR_MAC_MGMT_FRAME) {
			if (subType == SIR_MAC_MGMT_PROBE_RSP) {
				/* Drop Probe response recieved from supplicant,
				 * as for GO and SAP PE itself
				 * sends probe response
				 */
				goto err_rem_channel;
			} else if ((subType == SIR_MAC_MGMT_DISASSOC) ||
				   (subType == SIR_MAC_MGMT_DEAUTH)) {
				/* During EAP failure or P2P Group Remove
				 * supplicant is sending del_station command
				 * to driver. From del_station function,
				 * Driver will send deauth frame top2p client.
				 * No need to send disassoc frame from here.
				 * so Drop the frame here and send tx indication
				 * back to supplicant.
				 */
				uint8_t dstMac[ETH_ALEN] = { 0 };

				memcpy(&dstMac,
				       &buf[WLAN_HDD_80211_FRM_DA_OFFSET],
				       ETH_ALEN);
				hdd_debug("Deauth/Disassoc received for STA:"
					 MAC_ADDRESS_STR,
					 MAC_ADDR_ARRAY(dstMac));
				goto err_rem_channel;
			}
		}
	}

	if (NULL != cfgState->buf) {
		if (!noack) {
			hdd_warn("Previous P2P Action frame packet pending");
			if (!hdd_is_p2p_go_cnf_frame(buf, len))
				hdd_cleanup_actionframe(pAdapter->pHddCtx,
						pAdapter);
			else {
				hdd_cleanup_actionframe_no_wait(
						pAdapter->pHddCtx, pAdapter);
			}
		} else {
			hdd_err("Pending Action frame packet return EBUSY");
			return -EBUSY;
		}
	}

	if (subType == SIR_MAC_MGMT_ACTION) {
		hdd_debug("Action frame tx request : %s",
			   hdd_get_action_string(buf
						 [WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET]));
	}

	if ((pAdapter->device_mode == QDF_SAP_MODE) &&
	    (test_bit(SOFTAP_BSS_STARTED, &pAdapter->event_flags))) {
		home_ch = pAdapter->sessionCtx.ap.operatingChannel;
	} else if ((pAdapter->device_mode == QDF_STA_MODE) &&
		   (pAdapter->sessionCtx.station.conn_info.connState ==
				eConnectionState_Associated)) {
		home_ch =
			pAdapter->sessionCtx.station.conn_info.operationChannel;
	} else {
		goAdapter = hdd_get_adapter(pAdapter->pHddCtx, QDF_P2P_GO_MODE);
		if (goAdapter &&
		    (test_bit(SOFTAP_BSS_STARTED, &goAdapter->event_flags)))
			home_ch = goAdapter->sessionCtx.ap.operatingChannel;
	}

	if (chan &&
	    (ieee80211_frequency_to_channel(chan->center_freq) ==
	     home_ch)) {
		/* if adapter is already on requested ch, no need for ROC */
		wait = 0;
		hdd_debug("Adapter already on requested ch. No ROC needed");
		goto send_frame;
	}

	if (offchan && wait && chan) {
		int status;
		enum rem_on_channel_request_type req_type = OFF_CHANNEL_ACTION_TX;
		/* In case of P2P Client mode if we are already */
		/* on the same channel then send the frame directly */

		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
		pRemainChanCtx = cfgState->remain_on_chan_ctx;
		if ((type == SIR_MAC_MGMT_FRAME) &&
		    (subType == SIR_MAC_MGMT_ACTION) &&
		    hdd_p2p_is_action_type_rsp(&buf
			[WLAN_HDD_PUBLIC_ACTION_FRAME_BODY_OFFSET],
			len - mgmt_hdr_len)
		    && cfgState->remain_on_chan_ctx
		    && cfgState->current_freq == chan->center_freq) {
			if (QDF_TIMER_STATE_RUNNING ==
			    qdf_mc_timer_get_current_state(&cfgState->
						   remain_on_chan_ctx->
						   hdd_remain_on_chan_timer)) {

				/* In the latest wpa_supplicant, the wait time
				 * for go negotiation response is set to 100ms,
				 * due to which, there could be a possibility
				 * that, if the go negotaition confirmation
				 * frame is not received within 100 msec, ROC
				 * would be timeout and resulting in connection
				 * failures as the device will not be on the
				 * listen channel anymore to receive the conf
				 * frame. Also wpa_supplicant has set the wait
				 * to 50msec for go negotiation confirmation,
				 * invitation response and prov discovery rsp
				 * frames. So increase the wait time for all
				 * these frames.
				 */
				actionFrmType = buf
				[WLAN_HDD_PUBLIC_ACTION_FRAME_TYPE_OFFSET];
				if (actionFrmType == WLAN_HDD_GO_NEG_RESP ||
				    actionFrmType == WLAN_HDD_PROV_DIS_RESP)
					wait = wait + ACTION_FRAME_RSP_WAIT;
				else if (actionFrmType ==
					 WLAN_HDD_GO_NEG_CNF ||
					 actionFrmType ==
					 WLAN_HDD_INVITATION_RESP)
					wait = wait + ACTION_FRAME_ACK_WAIT;

				hdd_debug("Extending the wait time %d for actionFrmType=%d",
					 wait, actionFrmType);

				if (qdf_mc_timer_stop(&cfgState->
						remain_on_chan_ctx->
						hdd_remain_on_chan_timer)
						!= QDF_STATUS_SUCCESS)
					hdd_warn("Failed to stop hdd_remain_on_chan_timer");
				status =
					qdf_mc_timer_start(&cfgState->
							   remain_on_chan_ctx->
							   hdd_remain_on_chan_timer,
							   wait);
				if (status != QDF_STATUS_SUCCESS)
					hdd_warn("Remain on Channel timer start failed");

				mutex_unlock(&cfgState->
					     remain_on_chan_ctx_lock);
				goto send_frame;
			} else {
				if (pRemainChanCtx->
				    hdd_remain_on_chan_cancel_in_progress ==
				    true) {
					mutex_unlock(&cfgState->
						     remain_on_chan_ctx_lock);
					hdd_debug("action frame tx: waiting for completion of ROC ");

					rc = wait_for_completion_timeout
						     (&pAdapter->cancel_rem_on_chan_var,
						     msecs_to_jiffies
							     (WAIT_CANCEL_REM_CHAN));
					if (!rc)
						hdd_warn("wait on cancel_rem_on_chan_var timed out");
				} else
					mutex_unlock(&cfgState->
						     remain_on_chan_ctx_lock);
			}
		} else
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);

		mutex_lock(&cfgState->remain_on_chan_ctx_lock);
		pRemainChanCtx = cfgState->remain_on_chan_ctx;

		/* At this point if remain_on_chan_ctx exists but timer not
		 * running means that roc workqueue requested a new RoC and it
		 * is in progress. So wait for Ready on channel indication
		 */
		if ((pRemainChanCtx) &&
			(QDF_TIMER_STATE_RUNNING !=
			 qdf_mc_timer_get_current_state(
				 &pRemainChanCtx->hdd_remain_on_chan_timer))) {
				 mutex_unlock(
				 &cfgState->remain_on_chan_ctx_lock);
			hdd_debug("remain_on_chan_ctx exists but RoC timer not running. wait for ready on channel");
			rc = wait_for_completion_timeout(&pAdapter->
					rem_on_chan_ready_event,
					msecs_to_jiffies
					(WAIT_REM_CHAN_READY));
			if (!rc)
				hdd_err("timeout waiting for remain on channel ready indication");

			mutex_lock(&cfgState->remain_on_chan_ctx_lock);
			pRemainChanCtx = cfgState->remain_on_chan_ctx;
		}

		if ((pRemainChanCtx != NULL) &&
			(cfgState->current_freq == chan->center_freq)) {
			mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
			hdd_debug("action frame: extending the wait time");
			extendedWait = (uint16_t) wait;
			goto send_frame;
		}

		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
		INIT_COMPLETION(pAdapter->offchannel_tx_event);

		status = wlan_hdd_request_remain_on_channel(wiphy, dev, chan,
							    wait, cookie,
							    req_type);
		if (0 != status) {
			if ((-EBUSY == status) &&
			    (cfgState->current_freq == chan->center_freq)) {
				goto send_frame;
			}
			goto err_rem_channel;
		}
		/* This will extend timer in LIM when sending Any action frame
		 * It will cover remain on channel timer till next action frame
		 * in rx direction.
		 */
		extendedWait = (uint16_t) wait;
		/* Wait for driver to be ready on the requested channel */
		rc = wait_for_completion_timeout(&pAdapter->offchannel_tx_event,
						 msecs_to_jiffies
							 (WAIT_CHANGE_CHANNEL_FOR_OFFCHANNEL_TX));
		if (!rc) {
			hdd_err("wait on offchannel_tx_event timed out");
			goto err_rem_channel;
		}
	} else if (offchan) {
		/* Check before sending action frame
		 * whether we already remain on channel
		 */
		if (NULL == cfgState->remain_on_chan_ctx)
			goto err_rem_channel;
	}
send_frame:

	if (!noack) {
		cfgState->buf = qdf_mem_malloc(len);    /* buf; */
		if (cfgState->buf == NULL)
			return -ENOMEM;

		cfgState->len = len;

		qdf_mem_copy(cfgState->buf, buf, len);

		mutex_lock(&cfgState->remain_on_chan_ctx_lock);

		if (cfgState->remain_on_chan_ctx) {
			cfgState->action_id =
				cfgState->remain_on_chan_ctx->id;
			*cookie = cfgState->action_id;
		} else {
			if (QDF_STATUS_SUCCESS != qdf_idr_alloc(
					&pHddCtx->p2p_idr,
					cfgState->buf, &mgmt_id)) {
				hdd_err("alloc id fail");
				goto err;
			}

			*cookie = (u64)mgmt_id;
			cfgState->action_id = mgmt_id;
		}

		mutex_unlock(&cfgState->remain_on_chan_ctx_lock);
	}

	/*
	 * Firmware needs channel information for action frames
	 * which are not sent on the current operating channel of VDEV
	 */
	if ((QDF_P2P_DEVICE_MODE == pAdapter->device_mode) ||
		(QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
		(QDF_P2P_GO_MODE == pAdapter->device_mode)) {
		if (chan && (chan->center_freq != 0))
			current_freq = chan->center_freq;
		else
			current_freq = cfgState->current_freq;
	} else {
		current_freq = 0;
	}

	INIT_COMPLETION(pAdapter->tx_action_cnf_event);

	if ((QDF_STA_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_CLIENT_MODE == pAdapter->device_mode) ||
	    (QDF_P2P_DEVICE_MODE == pAdapter->device_mode)) {
		uint8_t sessionId = pAdapter->sessionId;

		if ((type == SIR_MAC_MGMT_FRAME) &&
		    (subType == SIR_MAC_MGMT_ACTION) &&
		     wlan_hdd_is_type_p2p_action(&buf
				[WLAN_HDD_PUBLIC_ACTION_FRAME_BODY_OFFSET],
				len - mgmt_hdr_len)) {
			actionFrmType =
				buf[WLAN_HDD_PUBLIC_ACTION_FRAME_TYPE_OFFSET];
			hdd_debug("Tx Action Frame %u", actionFrmType);
			if (actionFrmType == WLAN_HDD_PROV_DIS_REQ) {
				cfgState->actionFrmState =
					HDD_PD_REQ_ACK_PENDING;
				hdd_debug("HDD_PD_REQ_ACK_PENDING");
			} else if (actionFrmType == WLAN_HDD_GO_NEG_REQ) {
				cfgState->actionFrmState =
					HDD_GO_NEG_REQ_ACK_PENDING;
				hdd_debug("HDD_GO_NEG_REQ_ACK_PENDING");
			}
		}

		if (qdf_mem_cmp((uint8_t *)(&buf[WLAN_HDD_80211_FRM_SA_OFFSET]),
		    &pAdapter->macAddressCurrent, QDF_MAC_ADDR_SIZE)) {
			hdd_info("%s: action frame sa is randomized with mac: "
				 MAC_ADDRESS_STR, __func__,
				 MAC_ADDR_ARRAY((uint8_t *)
				 (&buf[WLAN_HDD_80211_FRM_SA_OFFSET])));
			enb_random_mac = true;
		}

		if (enb_random_mac && !noack && chan && chan->center_freq)
			hdd_set_action_frame_random_mac(pAdapter,
				(uint8_t *)(&buf[WLAN_HDD_80211_FRM_SA_OFFSET]),
				*cookie, chan->center_freq);

		if (QDF_STATUS_SUCCESS !=
		    sme_send_action(WLAN_HDD_GET_HAL_CTX(pAdapter),
				    sessionId, buf, len, extendedWait, noack,
				    current_freq)) {
			hdd_err("sme_send_action returned fail");
			goto err;
		}
	} else if (QDF_SAP_MODE == pAdapter->device_mode ||
		   QDF_P2P_GO_MODE == pAdapter->device_mode) {
		if (QDF_STATUS_SUCCESS !=
		    wlansap_send_action(WLAN_HDD_GET_SAP_CTX_PTR(pAdapter),
					buf, len, 0, current_freq)) {
			hdd_err("wlansap_send_action returned fail");
			goto err;
		}
	}

	return 0;
err:
	if (!noack) {
		if (enb_random_mac && chan && chan->center_freq &&
			((pAdapter->device_mode == QDF_STA_MODE) ||
			(pAdapter->device_mode == QDF_P2P_CLIENT_MODE) ||
			(pAdapter->device_mode == QDF_P2P_DEVICE_MODE)))
			hdd_reset_action_frame_random_mac(pAdapter,
				(uint8_t *)(&buf[WLAN_HDD_80211_FRM_SA_OFFSET]),
				*cookie);

		hdd_send_action_cnf(pAdapter, false);
	}
	return 0;
err_rem_channel:
	if (QDF_STATUS_SUCCESS != qdf_idr_alloc(&pHddCtx->p2p_idr,
		cfgState, &mgmt_id))
		mgmt_id = 0;

	*cookie = (u64)mgmt_id;
	cfg80211_mgmt_tx_status(
		pAdapter->dev->ieee80211_ptr,
		*cookie, buf, len, false, GFP_KERNEL);
	qdf_idr_remove(&pHddCtx->p2p_idr, mgmt_id);

	EXIT();
	return 0;
}

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0))
int wlan_hdd_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
		     struct cfg80211_mgmt_tx_params *params, u64 *cookie)
#else
int wlan_hdd_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
		     struct ieee80211_channel *chan, bool offchan,
		     unsigned int wait,
		     const u8 *buf, size_t len, bool no_cck,
		     bool dont_wait_for_ack, u64 *cookie)
#endif /* LINUX_VERSION_CODE */
{
	int ret;

	cds_ssr_protect(__func__);

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0))
	ret = __wlan_hdd_mgmt_tx(wiphy, wdev, params->chan, params->offchan,
				 params->wait, params->buf, params->len,
				 params->no_cck, params->dont_wait_for_ack,
				 cookie);
#else
	ret = __wlan_hdd_mgmt_tx(wiphy, wdev, chan, offchan,
				 wait, buf, len, no_cck,
				 dont_wait_for_ack, cookie);
#endif /* LINUX_VERSION_CODE */
	cds_ssr_unprotect(__func__);

	return ret;
}

/**
 * hdd_wlan_delete_mgmt_tx_cookie() - Wrapper to delete action frame cookie
 * @wdev: Pointer to wireless device
 * @cookie: Cookie to be deleted
 *
 * This is a wrapper function which actually invokes the hdd api to delete
 * cookie based on the device mode of adapter.
 *
 * Return: 0 - for success else negative value
 */
static int hdd_wlan_delete_mgmt_tx_cookie(struct wireless_dev *wdev,
				   u64 cookie)
{
	struct net_device *dev = wdev->netdev;
	hdd_adapter_t *adapter = WLAN_HDD_GET_PRIV_PTR(dev);

	if ((adapter->device_mode == QDF_STA_MODE) ||
	    (adapter->device_mode == QDF_P2P_CLIENT_MODE) ||
	    (adapter->device_mode == QDF_P2P_DEVICE_MODE)) {
		hdd_delete_action_frame_cookie(adapter, cookie);
	}

	return 0;
}

static int __wlan_hdd_cfg80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
						   struct wireless_dev *wdev,
						   u64 cookie)
{
	hdd_wlan_delete_mgmt_tx_cookie(wdev, cookie);
	return wlan_hdd_cfg80211_cancel_remain_on_channel(wiphy, wdev, cookie);
}

int wlan_hdd_cfg80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
					  struct wireless_dev *wdev, u64 cookie)
{
	int ret;

	cds_ssr_protect(__func__);
	ret = __wlan_hdd_cfg80211_mgmt_tx_cancel_wait(wiphy, wdev, cookie);
	cds_ssr_unprotect(__func__);

	return ret;
}

void hdd_send_action_cnf(hdd_adapter_t *pAdapter, bool actionSendSuccess)
{
	hdd_cfg80211_state_t *cfgState = WLAN_HDD_GET_CFG_STATE_PTR(pAdapter);
	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);

	cfgState->actionFrmState = HDD_IDLE;

	if (NULL == cfgState->buf) {
		qdf_idr_remove(&hdd_ctx->p2p_idr, cfgState->action_id);
		return;
	}

	if (cfgState->is_go_neg_ack_received) {

		cfgState->is_go_neg_ack_received = 0;
		/* Sometimes its possible that host may receive the ack for GO
		 * negotiation req after sending go negotaition confirmation,
		 * in such case drop the ack received for the go negotiation
		 * request, so that supplicant waits for the confirmation ack
		 * from firmware.
		 */
		hdd_debug("Drop the pending ack received in cfgState->actionFrmState %d",
				cfgState->actionFrmState);
		qdf_idr_remove(&hdd_ctx->p2p_idr, cfgState->action_id);
		return;
	}

	hdd_debug("Send Action cnf, actionSendSuccess %d",
		actionSendSuccess);
	/*
	 * buf is the same pointer it passed us to send. Since we are sending
	 * it through control path, we use different buffers.
	 * In case of mac80211, they just push it to the skb and pass the same
	 * data while sending tx ack status.
	 */
	cfg80211_mgmt_tx_status(
		pAdapter->dev->ieee80211_ptr,
		(u64)cfgState->action_id,
		cfgState->buf, cfgState->len,
		actionSendSuccess, GFP_KERNEL);

	qdf_idr_remove(&hdd_ctx->p2p_idr, cfgState->action_id);
	qdf_mem_free(cfgState->buf);
	cfgState->buf = NULL;

	complete(&pAdapter->tx_action_cnf_event);
}

/**
 * hdd_send_action_cnf_cb - action confirmation callback
 * @session_id: SME session ID
 * @tx_completed: ack status
 *
 * This function invokes hdd_sendActionCnf to update ack status to
 * supplicant.
 */
void hdd_send_action_cnf_cb(uint32_t session_id, bool tx_completed)
{
	hdd_context_t *hdd_ctx;
	hdd_adapter_t *adapter;

	ENTER();

	/* Get the HDD context.*/
	hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD);
	if (0 != wlan_hdd_validate_context(hdd_ctx))
		return;

	adapter = hdd_get_adapter_by_sme_session_id(hdd_ctx, session_id);
	if (NULL == adapter) {
		hdd_err("adapter not found");
		return;
	}

	if (WLAN_HDD_ADAPTER_MAGIC != adapter->magic) {
		hdd_err("adapter has invalid magic");
		return;
	}

	hdd_send_action_cnf(adapter, tx_completed);
}

/**
 * hdd_set_p2p_noa
 *
 ***FUNCTION:
 * This function is called from hdd_hostapd_ioctl function when Driver
 * get P2P_SET_NOA comand from wpa_supplicant using private ioctl
 *
 ***LOGIC:
 * Fill NoA Struct According to P2P Power save Option and Pass it to SME layer
 *
 ***ASSUMPTIONS:
 *
 *
 ***NOTE:
 *
 * @param dev          Pointer to net device structure
 * @param command      Pointer to command
 *
 * @return Status
 */

int hdd_set_p2p_noa(struct net_device *dev, uint8_t *command)
{
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	tHalHandle hHal = WLAN_HDD_GET_HAL_CTX(pAdapter);
	tP2pPsConfig NoA;
	int count, duration, interval;
	char *param;
	int ret;

	param = strnchr(command, strlen(command), ' ');
	if (param == NULL) {
		hdd_err("strnchr failed to find delimeter");
		return -EINVAL;
	}
	param++;
	ret = sscanf(param, "%d %d %d", &count, &interval, &duration);
	if (ret != 3) {
		hdd_err("P2P_SET GO NoA: fail to read params, ret=%d",
			ret);
		return -EINVAL;
	}
	if (count < 0 || interval < 0 || duration < 0 ||
	    interval > MAX_MUS_VAL || duration > MAX_MUS_VAL) {
		hdd_err("Invalid NOA parameters");
		return -EINVAL;
	}
	hdd_debug("P2P_SET GO NoA: count=%d interval=%d duration=%d",
		count, interval, duration);
	duration = MS_TO_TU_MUS(duration);
	/* PS Selection
	 * Periodic NoA (2)
	 * Single NOA   (4)
	 */
	NoA.opp_ps = 0;
	NoA.ctWindow = 0;
	if (count == 1) {
		NoA.duration = 0;
		NoA.single_noa_duration = duration;
		NoA.psSelection = P2P_POWER_SAVE_TYPE_SINGLE_NOA;
	} else {
		NoA.duration = duration;
		NoA.single_noa_duration = 0;
		NoA.psSelection = P2P_POWER_SAVE_TYPE_PERIODIC_NOA;
	}
	NoA.interval = MS_TO_TU_MUS(interval);
	NoA.count = count;
	NoA.sessionid = pAdapter->sessionId;

	hdd_debug("P2P_PS_ATTR:oppPS %d ctWindow %d duration %d "
		  "interval %d count %d single noa duration %d "
		  "PsSelection %x", NoA.opp_ps,
		  NoA.ctWindow, NoA.duration, NoA.interval,
		  NoA.count, NoA.single_noa_duration, NoA.psSelection);

	sme_p2p_set_ps(hHal, &NoA);
	return 0;
}

/**
 * hdd_set_p2p_opps
 *
 ***FUNCTION:
 * This function is called from hdd_hostapd_ioctl function when Driver
 * get P2P_SET_PS comand from wpa_supplicant using private ioctl
 *
 ***LOGIC:
 * Fill NoA Struct According to P2P Power save Option and Pass it to SME layer
 *
 ***ASSUMPTIONS:
 *
 *
 ***NOTE:
 *
 * @param  dev         Pointer to net device structure
 * @param  command     Pointer to command
 *
 * @return Status
 */

int hdd_set_p2p_opps(struct net_device *dev, uint8_t *command)
{
	hdd_adapter_t *adapter = WLAN_HDD_GET_PRIV_PTR(dev);
	tHalHandle handle = WLAN_HDD_GET_HAL_CTX(adapter);
	tP2pPsConfig noa;
	char *param;
	int legacy_ps, opp_ps, ctwindow;
	int ret;

	param = strnchr(command, strlen(command), ' ');
	if (param == NULL) {
		hdd_err("strnchr failed to find delimiter");
		return -EINVAL;
	}
	param++;
	ret = sscanf(param, "%d %d %d", &legacy_ps, &opp_ps, &ctwindow);
	if (ret != 3) {
		hdd_err("P2P_SET GO PS: fail to read params, ret=%d", ret);
		return -EINVAL;
	}

	if ((opp_ps != -1) && (opp_ps != 0) && (opp_ps != 1)) {
		hdd_err("Invalid opp_ps value:%d", opp_ps);
		return -EINVAL;
	}

	/* P2P spec: 3.3.2 Power Management and discovery:
	 *     CTWindow should be at least 10 TU.
	 * P2P spec: Table 27 - CTWindow and OppPS Parameters field format:
	 *     CTWindow and OppPS Parameters together is 8 bits.
	 *     CTWindow uses 7 bits (0-6, Bit 7 is for OppPS)
	 * 0 indicates that there shall be no CTWindow
	 */
	if ((ctwindow != -1) && (ctwindow != 0) &&
	    (!((ctwindow >= 10) && (ctwindow <= 127)))) {
		hdd_err("Invalid CT window value:%d", ctwindow);
		return -EINVAL;
	}

	hdd_debug("P2P_SET GO PS: legacy_ps=%d opp_ps=%d ctwindow=%d",
		  legacy_ps, opp_ps, ctwindow);

	/* PS Selection
	 * Opportunistic Power Save (1)
	 */

	/* From wpa_cli user need to use separate command to set ctWindow and
	 * Opps when user want to set ctWindow during that time other parameters
	 * values are coming from wpa_supplicant as -1.
	 * Example : User want to set ctWindow with 30 then wpa_cli command :
	 * P2P_SET ctwindow 30
	 * Command Received at hdd_hostapd_ioctl is as below:
	 * P2P_SET_PS -1 -1 30 (legacy_ps = -1, opp_ps = -1, ctwindow = 30)
	 *
	 * e.g., 1: P2P_SET_PS 1 1 30
	 * Driver sets the Opps and CTwindow as 30 and send it to FW.
	 * e.g., 2: P2P_SET_PS 1 -1 15
	 * Driver caches the CTwindow value but not send the command to FW.
	 * e.g., 3: P2P_SET_PS 1 1 -1
	 * Driver sends the command to FW with Opps enabled and CT window as
	 * 15 (last cached CTWindow value).
	 * (or) : P2P_SET_PS 1 1 20
	 * Driver sends the command to FW with opps enabled and CT window
	 * as 20.
	 *
	 * legacy_ps param remains unused until required in the future.
	 */
	if (ctwindow != -1)
		adapter->ctw = ctwindow;

	/* Send command to FW when OppPS is either enabled(1)/disbaled(0) */
	if (opp_ps != -1) {
		adapter->ops = opp_ps;
		noa.opp_ps = adapter->ops;
		noa.ctWindow = adapter->ctw;
		noa.duration = 0;
		noa.single_noa_duration = 0;
		noa.interval = 0;
		noa.count = 0;
		noa.psSelection = P2P_POWER_SAVE_TYPE_OPPORTUNISTIC;
		noa.sessionid = adapter->sessionId;

		hdd_debug("P2P_PS_ATTR: oppPS %d ctWindow %d duration %d interval %d count %d single noa duration %d PsSelection %x",
			noa.opp_ps, noa.ctWindow,
			noa.duration, noa.interval, noa.count,
			noa.single_noa_duration,
			noa.psSelection);

		sme_p2p_set_ps(handle, &noa);
	}

	return 0;
}

int hdd_set_p2p_ps(struct net_device *dev, void *msgData)
{
	hdd_adapter_t *pAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	tHalHandle hHal = WLAN_HDD_GET_HAL_CTX(pAdapter);
	QDF_STATUS status = QDF_STATUS_SUCCESS;
	tP2pPsConfig NoA;
	struct p2p_app_set_ps *pappNoA = (struct p2p_app_set_ps *) msgData;

	NoA.opp_ps = pappNoA->opp_ps;
	NoA.ctWindow = pappNoA->ctWindow;
	NoA.duration = pappNoA->duration;
	NoA.interval = pappNoA->interval;
	NoA.count = pappNoA->count;
	NoA.single_noa_duration = pappNoA->single_noa_duration;
	NoA.psSelection = pappNoA->psSelection;
	NoA.sessionid = pAdapter->sessionId;

	sme_p2p_set_ps(hHal, &NoA);
	return status;
}

static uint8_t wlan_hdd_get_session_type(enum nl80211_iftype type)
{
	switch (type) {
	case NL80211_IFTYPE_AP:
		return QDF_SAP_MODE;
	case NL80211_IFTYPE_P2P_GO:
		return QDF_P2P_GO_MODE;
	case NL80211_IFTYPE_P2P_CLIENT:
		return QDF_P2P_CLIENT_MODE;
	case NL80211_IFTYPE_STATION:
		return QDF_STA_MODE;
	default:
		return QDF_STA_MODE;
	}
}

/**
 * wlan_hdd_allow_sap_add() - check to add new sap interface
 * @hdd_ctx: pointer to hdd context
 * @name: name of the new interface
 * @sap_dev: output pointer to hold existing interface
 *
 * Return: If able to add interface return true else false
 */
static bool
wlan_hdd_allow_sap_add(hdd_context_t *hdd_ctx,
		       const char *name,
		       struct wireless_dev **sap_dev)
{
	hdd_adapter_list_node_t *adapter_node = NULL, *next = NULL;
	QDF_STATUS status;
	hdd_adapter_t *adapter;

	*sap_dev = NULL;
	status = hdd_get_front_adapter(hdd_ctx, &adapter_node);
	while (adapter_node && QDF_IS_STATUS_SUCCESS(status)) {
		adapter = adapter_node->pAdapter;
		if (adapter && adapter->device_mode == QDF_SAP_MODE &&
		    test_bit(NET_DEVICE_REGISTERED, &adapter->event_flags) &&
		    adapter->dev &&
		    !strncmp(adapter->dev->name, name, IFNAMSIZ)) {
			beacon_data_t *beacon = adapter->sessionCtx.ap.beacon;

			hdd_debug("iface already registered");
			if (beacon) {
				adapter->sessionCtx.ap.beacon = NULL;
				qdf_mem_free(beacon);
			}
			if (adapter->dev->ieee80211_ptr) {
				*sap_dev = adapter->dev->ieee80211_ptr;
				return false;
			}

			hdd_err("ieee80211_ptr points to NULL");
			return false;
		}

		status = hdd_get_next_adapter(hdd_ctx, adapter_node, &next);
		adapter_node = next;
	}

	return true;
}

/**
 * __wlan_hdd_add_virtual_intf() - Add virtual interface
 * @wiphy: wiphy pointer
 * @name: User-visible name of the interface
 * @name_assign_type: the name of assign type of the netdev
 * @nl80211_iftype: (virtual) interface types
 * @flags: moniter configuraiton flags (not used)
 * @vif_params: virtual interface parameters (not used)
 *
 * Return: the pointer of wireless dev, otherwise ERR_PTR.
 */
static
struct wireless_dev *__wlan_hdd_add_virtual_intf(struct wiphy *wiphy,
						 const char *name,
						 unsigned char name_assign_type,
						 enum nl80211_iftype type,
						 u32 *flags,
						 struct vif_params *params)
{
	hdd_context_t *pHddCtx = (hdd_context_t *) wiphy_priv(wiphy);
	hdd_adapter_t *pAdapter = NULL;
	hdd_scaninfo_t *scan_info = NULL;
	int ret;
	uint8_t session_type;

	ENTER();

	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam()) {
		hdd_err("Command not allowed in FTM mode");
		return ERR_PTR(-EINVAL);
	}

	ret = wlan_hdd_validate_context(pHddCtx);
	if (0 != ret)
		return ERR_PTR(ret);

	MTRACE(qdf_trace(QDF_MODULE_ID_HDD,
			 TRACE_CODE_HDD_ADD_VIRTUAL_INTF, NO_SESSION, type));
	/*
	 * Allow addition multiple interfaces for QDF_P2P_GO_MODE,
	 * QDF_SAP_MODE, QDF_P2P_CLIENT_MODE and QDF_STA_MODE
	 * session type.
	 */
	session_type = wlan_hdd_get_session_type(type);
	if (hdd_get_adapter(pHddCtx, session_type) != NULL
	    && QDF_SAP_MODE != session_type
	    && QDF_P2P_GO_MODE != session_type
	    && QDF_P2P_CLIENT_MODE != session_type
	    && QDF_STA_MODE != session_type) {
		hdd_err("Interface type %d already exists. Two interfaces of same type are not supported currently.",
			type);
		return ERR_PTR(-EINVAL);
	}

	pAdapter = hdd_get_adapter(pHddCtx, QDF_STA_MODE);
	if ((pAdapter != NULL) &&
		!(wlan_hdd_validate_session_id(pAdapter->sessionId))) {
		scan_info = &pAdapter->scan_info;
		if (scan_info->mScanPending) {
			hdd_abort_mac_scan(pHddCtx, pAdapter->sessionId,
					   INVALID_SCAN_ID,
					   eCSR_SCAN_ABORT_DEFAULT);
			hdd_debug("Abort Scan while adding virtual interface");
		}
	}

	if (session_type == QDF_SAP_MODE) {
		struct wireless_dev *sap_dev;
		bool allow_add_sap = wlan_hdd_allow_sap_add(pHddCtx, name,
							    &sap_dev);
		if (!allow_add_sap) {
			if (sap_dev)
				return sap_dev;

			return ERR_PTR(-EINVAL);
		}
	}

	pAdapter = NULL;
	if (pHddCtx->config->isP2pDeviceAddrAdministrated &&
	    ((NL80211_IFTYPE_P2P_GO == type) ||
	     (NL80211_IFTYPE_P2P_CLIENT == type))) {
		/*
		 * Generate the P2P Interface Address. this address must be
		 * different from the P2P Device Address.
		 */
		struct qdf_mac_addr p2pDeviceAddress =
						pHddCtx->p2pDeviceAddress;
		p2pDeviceAddress.bytes[4] ^= 0x80;
		pAdapter = hdd_open_adapter(pHddCtx,
					    session_type,
					    name, p2pDeviceAddress.bytes,
					    name_assign_type,
					    true);
	} else {
		pAdapter = hdd_open_adapter(pHddCtx,
					    session_type,
					    name,
					    wlan_hdd_get_intf_addr(
								pHddCtx,
								session_type),
					    name_assign_type,
					    true);
	}

	if (NULL == pAdapter) {
		hdd_err("hdd_open_adapter failed");
		return ERR_PTR(-ENOSPC);
	}

	/*
	 * Add interface can be requested from the upper layer at any time
	 * check the statemachine for modules state and if they are closed
	 * open the modules.
	 */
	ret = hdd_wlan_start_modules(pHddCtx, pAdapter, false);
	if (ret) {
		hdd_err("Failed to start the wlan_modules");
		goto close_adapter;
	}

	/*
	 * Once the support for session creation/deletion from
	 * hdd_hostapd_open/hdd_host_stop is in place.
	 * The support for starting adapter from here can be removed.
	 */
	if (NL80211_IFTYPE_AP == type || (NL80211_IFTYPE_P2P_GO == type)) {
		ret = hdd_start_adapter(pAdapter);
		if (ret) {
			hdd_err("Failed to start %s", name);
			goto stop_modules;
		}
	}

	if (pHddCtx->rps)
		hdd_send_rps_ind(pAdapter);

	EXIT();
	return pAdapter->dev->ieee80211_ptr;

stop_modules:
	/*
	 * Find if any iface is up. If there is not iface which is up
	 * start the timer to close the modules
	 */
	if (hdd_check_for_opened_interfaces(pHddCtx)) {
		hdd_debug("Closing all modules from the add_virt_iface");
		qdf_sched_delayed_work(&pHddCtx->iface_idle_work,
				       pHddCtx->config->iface_change_wait_time);
		hdd_prevent_suspend_timeout(
			pHddCtx->config->iface_change_wait_time,
			WIFI_POWER_EVENT_WAKELOCK_IFACE_CHANGE_TIMER);
	} else
		hdd_debug("Other interfaces are still up dont close modules!");

close_adapter:
	hdd_close_adapter(pHddCtx, pAdapter, true);

	return ERR_PTR(-EINVAL);
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
struct wireless_dev *wlan_hdd_add_virtual_intf(struct wiphy *wiphy,
					       const char *name,
					       unsigned char name_assign_type,
					       enum nl80211_iftype type,
					       struct vif_params *params)
{
	struct wireless_dev *wdev;

	cds_ssr_protect(__func__);
	wdev = __wlan_hdd_add_virtual_intf(wiphy, name, name_assign_type,
					   type, &params->flags, params);
	cds_ssr_unprotect(__func__);

	return wdev;
}
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)) || defined(WITH_BACKPORTS)
/**
 * wlan_hdd_add_virtual_intf() - Add virtual interface wrapper
 * @wiphy: wiphy pointer
 * @name: User-visible name of the interface
 * @name_assign_type: the name of assign type of the netdev
 * @nl80211_iftype: (virtual) interface types
 * @flags: monitor mode configuration flags (not used)
 * @vif_params: virtual interface parameters (not used)
 *
 * Return: the pointer of wireless dev, otherwise ERR_PTR.
 */
struct wireless_dev *wlan_hdd_add_virtual_intf(struct wiphy *wiphy,
					       const char *name,
					       unsigned char name_assign_type,
					       enum nl80211_iftype type,
					       u32 *flags,
					       struct vif_params *params)
{
	struct wireless_dev *wdev;

	cds_ssr_protect(__func__);
	wdev = __wlan_hdd_add_virtual_intf(wiphy, name, name_assign_type,
					   type, flags, params);
	cds_ssr_unprotect(__func__);
	return wdev;

}
#else
/**
 * wlan_hdd_add_virtual_intf() - Add virtual interface wrapper
 * @wiphy: wiphy pointer
 * @name: User-visible name of the interface
 * @nl80211_iftype: (virtual) interface types
 * @flags: monitor mode configuration flags (not used)
 * @vif_params: virtual interface parameters (not used)
 *
 * Return: the pointer of wireless dev, otherwise ERR_PTR.
 */
struct wireless_dev *wlan_hdd_add_virtual_intf(struct wiphy *wiphy,
					       const char *name,
					       enum nl80211_iftype type,
					       u32 *flags,
					       struct vif_params *params)
{
	struct wireless_dev *wdev;
	unsigned char name_assign_type = 0;

	cds_ssr_protect(__func__);
	wdev = __wlan_hdd_add_virtual_intf(wiphy, name, name_assign_type,
					   type, flags, params);
	cds_ssr_unprotect(__func__);
	return wdev;

}
#endif

int __wlan_hdd_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
{
	struct net_device *dev = wdev->netdev;
	hdd_context_t *pHddCtx = (hdd_context_t *) wiphy_priv(wiphy);
	hdd_adapter_t *pVirtAdapter = WLAN_HDD_GET_PRIV_PTR(dev);
	int status;

	ENTER();

	if (QDF_GLOBAL_FTM_MODE == hdd_get_conparam()) {
		hdd_err("Command not allowed in FTM mode");
		return -EINVAL;
	}

	/*
	 * Clear SOFTAP_INIT_DONE flag to mark SAP unload, so that we do
	 * not restart SAP after SSR as SAP is already stopped from user space.
	 */
	clear_bit(SOFTAP_INIT_DONE, &pVirtAdapter->event_flags);

	MTRACE(qdf_trace(QDF_MODULE_ID_HDD,
			 TRACE_CODE_HDD_DEL_VIRTUAL_INTF,
			 pVirtAdapter->sessionId, pVirtAdapter->device_mode));
	hdd_debug("Device_mode %s(%d)",
		   hdd_device_mode_to_string(pVirtAdapter->device_mode),
		   pVirtAdapter->device_mode);

	status = wlan_hdd_validate_context(pHddCtx);

	if (0 != status)
		return status;

	/* check state machine state and kickstart modules if they are closed */
	status = hdd_wlan_start_modules(pHddCtx, pVirtAdapter, false);
	if (status)
		return status;

	if (pVirtAdapter->device_mode == QDF_SAP_MODE &&
	    wlan_sap_is_pre_cac_active(pHddCtx->hHal)) {
		hdd_clean_up_pre_cac_interface(pHddCtx);
	} else {
		wlan_hdd_release_intf_addr(pHddCtx,
					 pVirtAdapter->macAddressCurrent.bytes);
		hdd_stop_adapter(pHddCtx, pVirtAdapter, true);
		hdd_close_adapter(pHddCtx, pVirtAdapter, true);
	}

	EXIT();

	return 0;
}

int wlan_hdd_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
{
	int ret;

	cds_ssr_protect(__func__);
	ret = __wlan_hdd_del_virtual_intf(wiphy, wdev);
	cds_ssr_unprotect(__func__);

	return ret;
}

#ifdef WLAN_FEATURE_P2P_DEBUG
/*
 * wlan_hdd_p2p_action_debug() - Log P2P state and update global status
 * @actionFrmType: action frame type
 * @macFrom: peer mac address
 *
 * return: void
 */
static void wlan_hdd_p2p_action_debug(enum action_frm_type actionFrmType,
					uint8_t *macFrom)
{
	if (actionFrmType >= MAX_P2P_ACTION_FRAME_TYPE) {
		hdd_debug("[P2P] unknown[%d] <--- OTA from " MAC_ADDRESS_STR,
			actionFrmType, MAC_ADDR_ARRAY(macFrom));
	} else {
		hdd_debug("[P2P] %s <--- OTA from " MAC_ADDRESS_STR,
			p2p_action_frame_type[actionFrmType],
			MAC_ADDR_ARRAY(macFrom));
		if ((actionFrmType == WLAN_HDD_PROV_DIS_REQ)
		    && (global_p2p_connection_status == P2P_NOT_ACTIVE)) {
			global_p2p_connection_status = P2P_GO_NEG_PROCESS;
			hdd_debug("[P2P State]Inactive state to GO negotiation progress state");
		} else
		if ((actionFrmType == WLAN_HDD_GO_NEG_CNF)
		    && (global_p2p_connection_status == P2P_GO_NEG_PROCESS)) {
			global_p2p_connection_status = P2P_GO_NEG_COMPLETED;
			hdd_debug("[P2P State]GO negotiation progress to GO negotiation completed state");
		} else
		if ((actionFrmType == WLAN_HDD_INVITATION_REQ)
		    && (global_p2p_connection_status == P2P_NOT_ACTIVE)) {
			global_p2p_connection_status = P2P_GO_NEG_COMPLETED;
			hdd_debug("[P2P State]Inactive state to GO negotiation completed state Autonomous GO formation");
		}
	}
}
#else
/*
 * wlan_hdd_p2p_action_debug() - dummy
 * @actionFrmType: action frame type
 * @macFrom: peer mac address
 *
 * return: void
 */
static void wlan_hdd_p2p_action_debug(enum action_frm_type actionFrmType,
					uint8_t *macFrom)
{

}
#endif

static inline bool is_non_probe_req_mgmt_frame(uint8_t type, uint8_t sub_type)
{
	return type == SIR_MAC_MGMT_FRAME &&
		sub_type != SIR_MAC_MGMT_PROBE_REQ;
}

static hdd_adapter_t *get_adapter_from_broadcast_ocb(hdd_context_t *hdd_ctx,
						     uint8_t *broadcast)
{
	hdd_adapter_t *adapter;

	adapter = hdd_get_adapter(hdd_ctx, QDF_OCB_MODE);
	if (!adapter) {
		hdd_err("Dropping the frame. No frame with BCST as destination");
		return NULL;
	}
	if (broadcast) {
		hdd_debug("Set broadcast to true");
		*broadcast = 1;
	}

	return adapter;
}

static hdd_adapter_t *get_adapter_from_mac_addr(hdd_context_t *hdd_ctx,
						uint8_t *dest_addr)
{
	hdd_adapter_t *adapter;

	adapter = hdd_get_adapter_by_macaddr(hdd_ctx, dest_addr);

	return (adapter) ? adapter :
		hdd_get_adapter_by_rand_macaddr(hdd_ctx, dest_addr);
}

static inline bool is_mgmt_non_broadcast_frame(uint8_t type,
					       uint8_t sub_type,
					       uint8_t broadcast)
{
	return (type == SIR_MAC_MGMT_FRAME) &&
		(sub_type == SIR_MAC_MGMT_ACTION) && !broadcast;
}

static inline bool is_public_action_frame(uint8_t *pb_frames,
					  uint32_t frame_len)
{
	if (frame_len <= WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET) {
		hdd_debug("Not a public action frame len: %d", frame_len);
		return false;
	}

	return (pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET] ==
		WLAN_HDD_PUBLIC_ACTION_FRAME);
}

static inline bool is_p2p_action_frame(uint8_t *pb_frames,
				       uint32_t frame_len)
{
	if (frame_len <= (WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET +
	    SIR_MAC_P2P_OUI_SIZE + 2)) {
		hdd_debug("Not a p2p action frame len: %d", frame_len);
		return false;
	}

	if ((pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1]
		 != SIR_MAC_ACTION_VENDOR_SPECIFIC))
		return false;

	return !qdf_mem_cmp(&pb_frames
			    [WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 2],
			    SIR_MAC_P2P_OUI, SIR_MAC_P2P_OUI_SIZE);
}

static inline bool is_tdls_action_frame(uint8_t *pb_frames,
					uint32_t frame_len)
{
	if (frame_len <= WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET) {
		hdd_debug("Not a TDLS action frame len: %d", frame_len);
		return false;
	}

	return (pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET] ==
		    WLAN_HDD_TDLS_ACTION_FRAME);
}

static inline bool is_tdls_disc_resp_frame(uint8_t *pb_frames,
					   uint32_t frame_len)
{
	if (frame_len <= WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1) {
		hdd_debug("Not a TDLS disc resp frame len: %d", frame_len);
		return false;
	}

	return (pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1] ==
		WLAN_HDD_PUBLIC_ACTION_TDLS_DISC_RESP);
}

static inline bool is_qos_action_frame(uint8_t *pb_frames, uint32_t frame_len)
{
	if (frame_len <= WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1) {
		hdd_debug("Not a QOS frame len: %d", frame_len);
		return false;
	}

	return ((pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET] ==
		 WLAN_HDD_QOS_ACTION_FRAME) &&
		(pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1] ==
		 WLAN_HDD_QOS_MAP_CONFIGURE));
}

static inline bool is_extend_roc_timer_for_action(enum action_frm_type
						  frm_type)
{
	switch (frm_type) {
	case WLAN_HDD_GO_NEG_REQ:
	case WLAN_HDD_GO_NEG_RESP:
	case WLAN_HDD_INVITATION_REQ:
	case WLAN_HDD_DEV_DIS_REQ:
	case WLAN_HDD_PROV_DIS_REQ:
		return true;
	default:
		return false;
	}
}

static inline int get_roc_extension_time(enum action_frm_type frm_type)
{
	switch (frm_type) {
	case WLAN_HDD_GO_NEG_REQ:
	case WLAN_HDD_GO_NEG_RESP:
		return 2 * ACTION_FRAME_DEFAULT_WAIT;
	default:
		return ACTION_FRAME_DEFAULT_WAIT;
	}
}

static int extend_roc_timer(hdd_remain_on_chan_ctx_t *rem_chan_ctx,
			    uint16_t extend_time)
{
	QDF_STATUS status;

	if (!rem_chan_ctx) {
		hdd_err("Extend ROC timer failed");
		return -EINVAL;
	}

	if (QDF_TIMER_STATE_RUNNING !=
	    qdf_mc_timer_get_current_state(
		    &rem_chan_ctx->hdd_remain_on_chan_timer)) {
		hdd_debug("Rcvd action frame after timer expired");
		return -EFAULT;
	}

	qdf_mc_timer_stop(&rem_chan_ctx->hdd_remain_on_chan_timer);
	status = qdf_mc_timer_start(&rem_chan_ctx->hdd_remain_on_chan_timer,
				    extend_time);
	if (QDF_STATUS_SUCCESS != status) {
		hdd_err("Remain on Channel timer start failed");
		return -EFAULT;
	}

	return 0;
}

static void buffer_p2p_action_frame(hdd_remain_on_chan_ctx_t *rem_chan_ctx,
				    uint8_t *pb_frames, uint32_t frm_len,
				    uint16_t freq)
{
	if (!rem_chan_ctx) {
		hdd_err("Buffer P2P action frame failed");
		return;
	}

	if (rem_chan_ctx->action_pkt_buff.frame_length != 0) {
		hdd_warn("Frames are pending. dropping frame !!!");
		return;
	}

	rem_chan_ctx->action_pkt_buff.frame_length = frm_len;
	rem_chan_ctx->action_pkt_buff.freq = freq;
	rem_chan_ctx->action_pkt_buff.frame_ptr =
		qdf_mem_malloc(frm_len);
	qdf_mem_copy(rem_chan_ctx->action_pkt_buff.frame_ptr, pb_frames,
		     frm_len);
	hdd_debug("Action Pkt Cached successfully !!!");
}

static inline bool is_rx_action_resp_while_ack_pending(
	enum p2p_action_frame_state frame_state,
	enum action_frm_type frm_type)
{
	return (frm_type == WLAN_HDD_PROV_DIS_RESP &&
		frame_state == HDD_PD_REQ_ACK_PENDING) ||
		(frm_type == WLAN_HDD_GO_NEG_RESP &&
		 frame_state == HDD_GO_NEG_REQ_ACK_PENDING);
}

static bool process_rx_p2p_action_frame(hdd_adapter_t *adapter,
					uint8_t *pb_frames,
					hdd_cfg80211_state_t *cfg_state,
					uint32_t frm_len, uint16_t freq)
{
	uint8_t *macFrom;
	enum action_frm_type frm_type;
	uint16_t extend_time;
	hdd_remain_on_chan_ctx_t *rem_chan_ctx = NULL;

	macFrom = &pb_frames[WLAN_HDD_80211_PEER_ADDR_OFFSET];
	frm_type = pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_TYPE_OFFSET];

	hdd_debug("Rx Action Frame %u", frm_type);
	wlan_hdd_p2p_action_debug(frm_type, macFrom);

	mutex_lock(&cfg_state->remain_on_chan_ctx_lock);
	rem_chan_ctx = cfg_state->remain_on_chan_ctx;
	if (rem_chan_ctx) {
		if (is_extend_roc_timer_for_action(frm_type)) {
			extend_time = get_roc_extension_time(frm_type);
			hdd_debug("Extend RoC timer on reception of Action Frame %d",
				  extend_time);
			if (completion_done(
				&adapter->rem_on_chan_ready_event)) {
				extend_roc_timer(rem_chan_ctx, extend_time);
			} else {
				hdd_err("Buffer packet");
				buffer_p2p_action_frame(rem_chan_ctx,
							pb_frames,
							frm_len, freq);
				mutex_unlock(&cfg_state->
					     remain_on_chan_ctx_lock);
				return false;
			}
		}
	}
	mutex_unlock(&cfg_state->remain_on_chan_ctx_lock);

	if (is_rx_action_resp_while_ack_pending(cfg_state->actionFrmState,
						frm_type)) {
		hdd_debug("ACK_PENDING and But received RESP for Action frame ");
		cfg_state->is_go_neg_ack_received = 1;
		hdd_send_action_cnf(adapter, true);
	}

	return true;
}

static void log_rx_tdls_action_frame(uint8_t *pb_frames, uint32_t frame_len)
{
	enum action_frm_type frm_type;

	if (frame_len <= WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1) {
		hdd_debug("Not a TDLS action frame len: %d", frame_len);
		return;
	}

	frm_type = pb_frames[WLAN_HDD_PUBLIC_ACTION_FRAME_OFFSET + 1];
	if (frm_type >= MAX_TDLS_ACTION_FRAME_TYPE) {
		hdd_debug("[TDLS] unknown[%d] <--- OTA",
			  frm_type);
	} else {
		hdd_debug("[TDLS] %s <--- OTA",
			  tdls_action_frame_type[frm_type]);
	}

	cds_tdls_tx_rx_mgmt_event(SIR_MAC_ACTION_TDLS, SIR_MAC_ACTION_RX,
				  SIR_MAC_MGMT_ACTION, frm_type,
				  &pb_frames[WLAN_HDD_80211_PEER_ADDR_OFFSET]);
}

#ifdef FEATURE_WLAN_TDLS
static void process_tdls_rx_action_frame(hdd_adapter_t *adapter,
					 uint8_t *peer_addr, int8_t rx_rssi)
{
	process_rx_tdls_disc_resp_frame(adapter, peer_addr, rx_rssi);
}
#else
static void process_tdls_rx_action_frame(hdd_adapter_t *adapter,
					 uint8_t *peer_addr, int8_t rx_rssi)
{
}
#endif

static bool process_rx_public_action_frame(hdd_adapter_t *adapter,
					   uint8_t *pb_frames,
					   hdd_cfg80211_state_t *cfg_state,
					   enum action_frm_type frm_type,
					   uint32_t frm_len, uint16_t freq,
					   int8_t rx_rssi)
{
	if (!adapter || !pb_frames || !cfg_state) {
		hdd_err("Invalid parameter");
		return false;
	}

	if (!is_public_action_frame(pb_frames, frm_len))
		goto done;

	if (is_p2p_action_frame(pb_frames, frm_len)) {
		hdd_debug("Process P2P action frame");
		return process_rx_p2p_action_frame(adapter, pb_frames,
						   cfg_state, frm_len, freq);
	}

	if (is_tdls_disc_resp_frame(pb_frames, frm_len)) {
		uint8_t *peer_addr;

		peer_addr = &pb_frames[WLAN_HDD_80211_PEER_ADDR_OFFSET];
		process_tdls_rx_action_frame(adapter, peer_addr, rx_rssi);
	}

done:
	return true;
}

static uint16_t get_rx_frame_freq_from_chan(uint32_t rx_chan)
{
	if (rx_chan <= MAX_NO_OF_2_4_CHANNELS)
		return ieee80211_channel_to_frequency(rx_chan,
						      HDD_NL80211_BAND_2GHZ);

	return ieee80211_channel_to_frequency(rx_chan,
						HDD_NL80211_BAND_5GHZ);
}

static void indicate_rx_mgmt_over_nl80211(hdd_adapter_t *adapter,
					  uint32_t frm_len,
					  uint8_t *pb_frames, uint16_t freq,
					  int8_t rx_rssi)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
	cfg80211_rx_mgmt(adapter->dev->ieee80211_ptr,
			 freq, rx_rssi * 100, pb_frames,
			 frm_len, NL80211_RXMGMT_FLAG_ANSWERED);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
	cfg80211_rx_mgmt(adapter->dev->ieee80211_ptr,
			 freq, rx_rssi * 100, pb_frames,
			 frm_len, NL80211_RXMGMT_FLAG_ANSWERED,
			 GFP_ATOMIC);
#else
	cfg80211_rx_mgmt(adapter->dev->ieee80211_ptr, freq,
			 rx_rssi * 100,
			 pb_frames, frm_len, GFP_ATOMIC);
#endif /* LINUX_VERSION_CODE */
}

void __hdd_indicate_mgmt_frame(hdd_adapter_t *adapter, uint32_t frm_len,
			       uint8_t *pb_frames, uint8_t frame_type,
			       uint32_t rx_chan, int8_t rx_rssi)
{
	uint16_t freq;
	uint8_t type = 0;
	uint8_t sub_type = 0;
	enum action_frm_type frm_type;
	hdd_cfg80211_state_t *cfg_state;
	hdd_context_t *hdd_ctx;
	uint8_t broadcast = 0;
	uint8_t *dest_addr;

	hdd_debug("Frame Type = %d Frame Length = %d",
		  frame_type, frm_len);

	if (NULL == adapter) {
		hdd_err("adapter is NULL");
		return;
	}
	hdd_ctx = WLAN_HDD_GET_CTX(adapter);

	if (0 == frm_len) {
		hdd_err("Frame Length is Invalid ZERO");
		return;
	}

	if (NULL == pb_frames) {
		hdd_err("pb_frames is NULL");
		return;
	}

	type = WLAN_HDD_GET_TYPE_FRM_FC(pb_frames[0]);
	sub_type = WLAN_HDD_GET_SUBTYPE_FRM_FC(pb_frames[0]);
	hdd_debug("Frame Type = %d subType = %d", type, sub_type);

	if (is_non_probe_req_mgmt_frame(type, sub_type) &&
	    !qdf_is_macaddr_broadcast(
	     (struct qdf_mac_addr *)&pb_frames[WLAN_HDD_80211_FRM_DA_OFFSET])) {
		if (frm_len <= WLAN_HDD_80211_FRM_DA_OFFSET +
		    QDF_MAC_ADDR_SIZE) {
			hdd_err("Cannot parse dest mac addr len: %d", frm_len);
			return;
		}
		dest_addr = &pb_frames[WLAN_HDD_80211_FRM_DA_OFFSET];

		if (qdf_is_macaddr_broadcast((struct qdf_mac_addr *)
					     dest_addr)) {
			hdd_debug("Check if rx mgmt OCB frames");
			adapter = get_adapter_from_broadcast_ocb(hdd_ctx,
								 &broadcast);
		} else {
			adapter = get_adapter_from_mac_addr(hdd_ctx,
							    dest_addr);
		}
		if (!adapter) {
			hdd_err("Cannot get adapter for RX mgmt frame");
			return;
		}
	}

	if (!adapter->dev) {
		hdd_err("adapter->dev is NULL");
		return;
	}

	if (WLAN_HDD_ADAPTER_MAGIC != adapter->magic) {
		hdd_err("adapter has invalid magic");
		return;
	}

	freq = get_rx_frame_freq_from_chan(rx_chan);

	cfg_state = WLAN_HDD_GET_CFG_STATE_PTR(adapter);

	if (!is_mgmt_non_broadcast_frame(type, sub_type, broadcast))
		goto indicate;

	if (is_public_action_frame(pb_frames, frm_len)) {
		bool processed;

		processed = process_rx_public_action_frame(adapter, pb_frames,
							   cfg_state, frm_type,
							   frm_len, freq,
							   rx_rssi);
		if (!processed) {
			hdd_err("Unable to process rx public action frame");
			return;
		}
	}

	if (is_tdls_action_frame(pb_frames, frm_len))
		log_rx_tdls_action_frame(pb_frames, frm_len);

	if (is_qos_action_frame(pb_frames, frm_len))
		sme_update_dsc_pto_up_mapping(hdd_ctx->hHal,
					      adapter->hddWmmDscpToUpMap,
					      adapter->sessionId);

indicate:
	hdd_debug("Indicate Frame over NL80211 sessionid : %d, idx :%d",
		  adapter->sessionId, adapter->dev->ifindex);

	indicate_rx_mgmt_over_nl80211(adapter, frm_len, pb_frames,
				      freq, rx_rssi);
}