summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/sd8797/mlan/mlan_wmm.c
blob: 9c9f90be295d6a12f6d9c21c1e387e1655f8a3d5 (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
/** @file mlan_wmm.c
 *
 *  @brief This file contains functions for WMM.
 *
 *  Copyright (C) 2008-2011, Marvell International Ltd. 
 *
 *  This software file (the "File") is distributed by Marvell International
 *  Ltd. under the terms of the GNU General Public License Version 2, June 1991
 *  (the "License").  You may use, redistribute and/or modify this File in
 *  accordance with the terms and conditions of the License, a copy of which
 *  is available by writing to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 *  worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 *  THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 *  ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 *  this warranty disclaimer.
 */

/********************************************************
Change log:
    10/24/2008: initial version
********************************************************/

#include "mlan.h"
#ifdef STA_SUPPORT
#include "mlan_join.h"
#endif
#include "mlan_util.h"
#include "mlan_fw.h"
#include "mlan_main.h"
#include "mlan_wmm.h"
#include "mlan_11n.h"
#include "mlan_sdio.h"

/********************************************************
    Local Variables
********************************************************/

/** Maximum value FW can accept for driver delay in packet transmission */
#define DRV_PKT_DELAY_TO_FW_MAX   512

/*
 * Upper and Lower threshold for packet queuing in the driver
 
 *    - When the number of packets queued reaches the upper limit,
 *      the driver will stop the net queue in the app/kernel space.
 
 *    - When the number of packets drops beneath the lower limit after
 *      having reached the upper limit, the driver will restart the net
 *      queue.
 */

/** Lower threshold for packet queuing in the driver.
  * When the number of packets drops beneath the lower limit after having
  * reached the upper limit, the driver will restart the net queue.
  */
#define WMM_QUEUED_PACKET_LOWER_LIMIT   180

/** Upper threshold for packet queuing in the driver.
  * When the number of packets queued reaches the upper limit, the driver
  * will stop the net queue in the app/kernel space.
  */
#define WMM_QUEUED_PACKET_UPPER_LIMIT   200

/** Offset for TOS field in the IP header */
#define IPTOS_OFFSET 5

/** WMM information IE */
static const t_u8 wmm_info_ie[] = { WMM_IE, 0x07,
    0x00, 0x50, 0xf2, 0x02,
    0x00, 0x01, 0x00
};

/**
 * AC Priorities go from AC_BK to AC_VO.  The ACI enumeration for AC_BK (1)
 *   is higher than the enumeration for AC_BE (0); hence the needed
 *   mapping conversion for wmm AC to priority Queue Index
 */
static const t_u8 wmm_aci_to_qidx_map[] = { WMM_AC_BE,
    WMM_AC_BK,
    WMM_AC_VI,
    WMM_AC_VO
};

/** 
 * This table will be used to store the tid values based on ACs.
 * It is initialized to default values per TID.
 */
t_u8 tos_to_tid[] = {
    /* TID DSCP_P2 DSCP_P1 DSCP_P0 WMM_AC */
    0x01,                       /* 0 1 0 AC_BK */
    0x02,                       /* 0 0 0 AC_BK */
    0x00,                       /* 0 0 1 AC_BE */
    0x03,                       /* 0 1 1 AC_BE */
    0x04,                       /* 1 0 0 AC_VI */
    0x05,                       /* 1 0 1 AC_VI */
    0x06,                       /* 1 1 0 AC_VO */
    0x07                        /* 1 1 1 AC_VO */
};

/** 
 * This table inverses the tos_to_tid operation to get a priority
 * which is in sequential order, and can be compared.
 * Use this to compare the priority of two different TIDs.
 */
t_u8 tos_to_tid_inv[] = { 0x02, /* from tos_to_tid[2] = 0 */
    0x00,                       /* from tos_to_tid[0] = 1 */
    0x01,                       /* from tos_to_tid[1] = 2 */
    0x03,
    0x04,
    0x05,
    0x06,
    0x07
};

/** 
 * This table will provide the tid value for given ac. This table does not
 * change and will be used to copy back the default values to tos_to_tid in
 * case of disconnect.
 */
const t_u8 ac_to_tid[4][2] = { {1, 2}, {0, 3}, {4, 5}, {6, 7} };

/* Map of TOS UP values to WMM AC */
static const mlan_wmm_ac_e tos_to_ac[] = { WMM_AC_BE,
    WMM_AC_BK,
    WMM_AC_BK,
    WMM_AC_BE,
    WMM_AC_VI,
    WMM_AC_VI,
    WMM_AC_VO,
    WMM_AC_VO
};

raListTbl *wlan_wmm_get_ralist_node(pmlan_private priv, t_u8 tid,
                                    t_u8 * ra_addr);

/********************************************************
    Local Functions
********************************************************/
#ifdef DEBUG_LEVEL2
/**
 *  @brief Debug print function to display the priority parameters for a WMM AC
 *
 *  @param pac_param	Pointer to the AC parameters to display
 *
 *  @return		N/A
 */
static void
wlan_wmm_ac_debug_print(const IEEEtypes_WmmAcParameters_t * pac_param)
{
    const char *ac_str[] = { "BK", "BE", "VI", "VO" };

    ENTER();

    PRINTM(MINFO, "WMM AC_%s: ACI=%d, ACM=%d, Aifsn=%d, "
           "EcwMin=%d, EcwMax=%d, TxopLimit=%d\n",
           ac_str[wmm_aci_to_qidx_map[pac_param->aci_aifsn.aci]],
           pac_param->aci_aifsn.aci, pac_param->aci_aifsn.acm,
           pac_param->aci_aifsn.aifsn, pac_param->ecw.ecw_min,
           pac_param->ecw.ecw_max, wlan_le16_to_cpu(pac_param->tx_op_limit));

    LEAVE();
}

/** Print the WMM AC for debug purpose */
#define PRINTM_AC(pac_param) wlan_wmm_ac_debug_print(pac_param)
#else
/** Print the WMM AC for debug purpose */
#define PRINTM_AC(pac_param)
#endif

/**
 *  @brief Allocate route address
 *
 *  @param pmadapter       Pointer to the mlan_adapter structure
 *  @param ra              Pointer to the route address
 *
 *  @return         ra_list
 */
static raListTbl *
wlan_wmm_allocate_ralist_node(pmlan_adapter pmadapter, t_u8 * ra)
{
    raListTbl *ra_list = MNULL;

    ENTER();

    if (pmadapter->callbacks.
        moal_malloc(pmadapter->pmoal_handle, sizeof(raListTbl), MLAN_MEM_DEF,
                    (t_u8 **) & ra_list)) {
        PRINTM(MERROR, "Fail to allocate ra_list\n");
        goto done;
    }
    util_init_list((pmlan_linked_list) ra_list);
    util_init_list_head((t_void *) pmadapter->pmoal_handle,
                        &ra_list->buf_head, MFALSE,
                        pmadapter->callbacks.moal_init_lock);

    memcpy(pmadapter, ra_list->ra, ra, MLAN_MAC_ADDR_LENGTH);

    ra_list->total_pkts = 0;
    ra_list->tx_pause = 0;
    PRINTM(MINFO, "RAList: Allocating buffers for TID %p\n", ra_list);
  done:
    LEAVE();
    return ra_list;
}

/**
 * @brief Map ACs to TID 
 *
 * @param priv             Pointer to the mlan_private driver data struct
 * @param queue_priority   Queue_priority structure
 *
 * @return 	   N/A
 */
static void
wlan_wmm_queue_priorities_tid(pmlan_private priv, t_u8 queue_priority[])
{
    int i;

    ENTER();

    for (i = 0; i < 4; ++i) {
        tos_to_tid[7 - (i * 2)] = ac_to_tid[queue_priority[i]][1];
        tos_to_tid[6 - (i * 2)] = ac_to_tid[queue_priority[i]][0];
    }

    for (i = 0; i < MAX_NUM_TID; i++) {
        tos_to_tid_inv[tos_to_tid[i]] = (t_u8) i;
    }

    /* in case priorities have changed, force highest priority so next packet
       will check from top to re-establish the highest */
    util_scalar_write(priv->adapter->pmoal_handle,
                      &priv->wmm.highest_queued_prio,
                      HIGH_PRIO_TID,
                      priv->adapter->callbacks.moal_spin_lock,
                      priv->adapter->callbacks.moal_spin_unlock);

    LEAVE();
}

/**
 *  @brief Evaluate whether or not an AC is to be downgraded
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *  @param eval_ac  AC to evaluate for downgrading
 *
 *  @return WMM AC  The eval_ac traffic is to be sent on.
 */
static mlan_wmm_ac_e
wlan_wmm_eval_downgrade_ac(pmlan_private priv, mlan_wmm_ac_e eval_ac)
{
    int down_ac;
    mlan_wmm_ac_e ret_ac;
    WmmAcStatus_t *pac_status;

    ENTER();

    pac_status = &priv->wmm.ac_status[eval_ac];

    if (pac_status->disabled == MFALSE) {
        LEAVE();
        /* Okay to use this AC, its enabled */
        return eval_ac;
    }

    /* Setup a default return value of the lowest priority */
    ret_ac = WMM_AC_BK;

    /* 
     *  Find the highest AC that is enabled and does not require admission
     *    control.  The spec disallows downgrading to an AC, which is enabled
     *    due to a completed admission control.  Unadmitted traffic is not
     *    to be sent on an AC with admitted traffic.
     */
    for (down_ac = WMM_AC_BK; down_ac < eval_ac; down_ac++) {
        pac_status = &priv->wmm.ac_status[down_ac];

        if ((pac_status->disabled == MFALSE)
            && (pac_status->flow_required == MFALSE))
            /* AC is enabled and does not require admission control */
            ret_ac = (mlan_wmm_ac_e) down_ac;
    }

    LEAVE();
    return ret_ac;
}

/**
 *  @brief Convert the IP TOS field to an WMM AC Queue assignment
 *
 *  @param pmadapter A pointer to mlan_adapter structure
 *  @param tos       IP TOS field
 *
 *  @return     WMM AC Queue mapping of the IP TOS field
 */
static mlan_wmm_ac_e INLINE
wlan_wmm_convert_tos_to_ac(pmlan_adapter pmadapter, t_u32 tos)
{
    ENTER();

    if (tos >= NELEMENTS(tos_to_ac)) {
        LEAVE();
        return WMM_AC_BE;
    }

    LEAVE();
    return tos_to_ac[tos];
}

/**
 *  @brief  Evaluate a given TID and downgrade it to a lower TID if the
 *          WMM Parameter IE received from the AP indicates that the AP
 *          is disabled (due to call admission control (ACM bit). Mapping
 * 			of TID to AC is taken care internally
 *
 *  @param priv		Pointer to the mlan_private data struct
 *  @param tid  	tid to evaluate for downgrading
 *
 *  @return       Same tid as input if downgrading not required or
 *                the tid the traffic for the given tid should be downgraded to
 */
static t_u8 INLINE
wlan_wmm_downgrade_tid(pmlan_private priv, t_u32 tid)
{
    mlan_wmm_ac_e ac_down;
    pmlan_adapter pmadapter = priv->adapter;

    ENTER();

    ac_down =
        priv->wmm.ac_down_graded_vals[wlan_wmm_convert_tos_to_ac(pmadapter,
                                                                 tid)];
    LEAVE();
    /* 
     * Send the index to tid array, picking from the array will be
     * taken care by dequeuing function
     */
    if (tid == 1 || tid == 2)
        return ac_to_tid[ac_down][(tid + 1) % 2];
    else if (tid >= MAX_NUM_TID)
        return ac_to_tid[ac_down][0];
    else
        return ac_to_tid[ac_down][tid % 2];
}

/**
 *  @brief Delete packets in RA node
 *
 *  @param priv			Pointer to the mlan_private driver data struct
 *  @param ra_list	    	Pointer to raListTbl
 *
 *  @return		N/A
 */
static INLINE void
wlan_wmm_del_pkts_in_ralist_node(pmlan_private priv, raListTbl * ra_list)
{
    pmlan_buffer pmbuf;
    pmlan_adapter pmadapter = priv->adapter;

    ENTER();
    while ((pmbuf =
            (pmlan_buffer) util_peek_list(pmadapter->pmoal_handle,
                                          &ra_list->buf_head, MNULL, MNULL))) {
        util_unlink_list(pmadapter->pmoal_handle, &ra_list->buf_head,
                         (pmlan_linked_list) pmbuf, MNULL, MNULL);
        wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
    }
    util_free_list_head((t_void *) pmadapter->pmoal_handle, &ra_list->buf_head,
                        pmadapter->callbacks.moal_free_lock);

    LEAVE();
}

/**
 *  @brief Delete packets in RA list
 *
 *  @param priv			Pointer to the mlan_private driver data struct
 *  @param ra_list_head	ra list header
 *
 *  @return		N/A
 */
static INLINE void
wlan_wmm_del_pkts_in_ralist(pmlan_private priv, mlan_list_head * ra_list_head)
{
    raListTbl *ra_list;

    ENTER();

    ra_list =
        (raListTbl *) util_peek_list(priv->adapter->pmoal_handle, ra_list_head,
                                     MNULL, MNULL);

    while (ra_list && ra_list != (raListTbl *) ra_list_head) {
        wlan_wmm_del_pkts_in_ralist_node(priv, ra_list);

        ra_list = ra_list->pnext;
    }

    LEAVE();
}

/**
 *  @brief Clean up the wmm queue
 *
 *  @param priv  Pointer to the mlan_private driver data struct
 *
 *  @return      N/A
 */
static void
wlan_wmm_cleanup_queues(pmlan_private priv)
{
    int i;

    ENTER();

    for (i = 0; i < MAX_NUM_TID; i++) {
        wlan_wmm_del_pkts_in_ralist(priv, &priv->wmm.tid_tbl_ptr[i].ra_list);
        priv->wmm.pkts_queued[i] = 0;
    }
    util_scalar_write(priv->adapter->pmoal_handle, &priv->wmm.tx_pkts_queued, 0,
                      MNULL, MNULL);
    util_scalar_write(priv->adapter->pmoal_handle,
                      &priv->wmm.highest_queued_prio, HIGH_PRIO_TID, MNULL,
                      MNULL);

    LEAVE();
}

/**
 *  @brief Delete all route address from RA list
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *
 *  @return         N/A
 */
static void
wlan_wmm_delete_all_ralist(pmlan_private priv)
{
    raListTbl *ra_list;
    int i;
    pmlan_adapter pmadapter = priv->adapter;

    ENTER();

    for (i = 0; i < MAX_NUM_TID; ++i) {
        PRINTM(MINFO, "RAList: Freeing buffers for TID %d\n", i);
        while ((ra_list = (raListTbl *) util_peek_list(pmadapter->pmoal_handle,
                                                       &priv->wmm.
                                                       tid_tbl_ptr[i].ra_list,
                                                       MNULL, MNULL))) {
            util_unlink_list(pmadapter->pmoal_handle,
                             &priv->wmm.tid_tbl_ptr[i].ra_list,
                             (pmlan_linked_list) ra_list, MNULL, MNULL);

            pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle,
                                            (t_u8 *) ra_list);
        }

        util_init_list((pmlan_linked_list)
                       & priv->wmm.tid_tbl_ptr[i].ra_list);
        priv->wmm.tid_tbl_ptr[i].ra_list_curr = MNULL;
    }

    LEAVE();
}

/**
 *   @brief Get queue RA pointer
 *  
 *   @param priv     Pointer to the mlan_private driver data struct
 *   @param tid      TID
 *   @param ra_addr  Pointer to the route address
 * 
 *   @return         ra_list
 */
static raListTbl *
wlan_wmm_get_queue_raptr(pmlan_private priv, t_u8 tid, t_u8 * ra_addr)
{
    raListTbl *ra_list;
#if defined(UAP_SUPPORT)
    t_u8 bcast_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
#endif

    ENTER();
    ra_list = wlan_wmm_get_ralist_node(priv, tid, ra_addr);
    if (ra_list) {
        LEAVE();
        return ra_list;
    }
#if defined(UAP_SUPPORT)
    if ((GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_UAP) &&
        (0 != memcmp(priv->adapter, ra_addr, bcast_addr, sizeof(bcast_addr)))) {
        if (MNULL == wlan_get_station_entry(priv, ra_addr)) {
            PRINTM(MDATA, "Drop packets to unknown station\n");
            LEAVE();
            return MNULL;
        }
    }
#endif
    wlan_ralist_add(priv, ra_addr);

    ra_list = wlan_wmm_get_ralist_node(priv, tid, ra_addr);
    LEAVE();
    return ra_list;
}

#ifdef STA_SUPPORT
/**
 * 	@brief Sends wmmac host event
 *
 *  @param priv		Pointer to the mlan_private driver data struct
 *  @param typeStr	Type of host event
 *  @param srcAddr	Pointer to the source Address
 *  @param tid    	TID
 *  @param up		User priority
 *  @param status	Status code or Reason code
 *
 *  @return		N/A
 */
static void
wlan_send_wmmac_host_event(pmlan_private priv,
                           char *typeStr,
                           t_u8 * srcAddr, t_u8 tid, t_u8 up, t_u8 status)
{
    t_u8 event_buf[100];
    mlan_event *pevent;
    t_u8 *pOutBuf;

    ENTER();

    /* Format one of the following two output strings: ** -
       TSPEC:ADDTS_RSP:[<status code>]:TID=X:UP=Y ** - TSPEC:DELTS_RX:[<reason
       code>]:TID=X:UP=Y */
    pevent = (mlan_event *) event_buf;
    pOutBuf = pevent->event_buf;

    memcpy(priv->adapter, pOutBuf, (t_u8 *) "TSPEC:", 6);
    pOutBuf += 6;

    memcpy(priv->adapter, pOutBuf, (t_u8 *) typeStr, wlan_strlen(typeStr));
    pOutBuf += wlan_strlen(typeStr);

    *pOutBuf++ = ':';
    *pOutBuf++ = '[';

    if (status >= 100) {
        *pOutBuf++ = (status / 100) + '0';
        status = (status % 100);
    }

    if (status >= 10) {
        *pOutBuf++ = (status / 10) + '0';
        status = (status % 10);
    }

    *pOutBuf++ = status + '0';

    memcpy(priv->adapter, pOutBuf, (t_u8 *) "]:TID", 5);
    pOutBuf += 5;
    *pOutBuf++ = tid + '0';

    memcpy(priv->adapter, pOutBuf, (t_u8 *) ":UP", 3);
    pOutBuf += 3;
    *pOutBuf++ = up + '0';

    *pOutBuf = '\0';

    pevent->bss_index = priv->bss_index;
    pevent->event_id = MLAN_EVENT_ID_DRV_REPORT_STRING;
    pevent->event_len = wlan_strlen((const t_s8 *) (pevent->event_buf));

    wlan_recv_event(priv, MLAN_EVENT_ID_DRV_REPORT_STRING, pevent);
    LEAVE();
}
#endif /* STA_SUPPORT */

/**
 *  @brief This function gets the highest priority list pointer
 *  
 *  @param pmadapter      A pointer to mlan_adapter
 *  @param priv           A pointer to mlan_private
 *  @param tid            A pointer to return tid
 *
 *  @return             raListTbl
 */
static raListTbl *
wlan_wmm_get_highest_priolist_ptr(pmlan_adapter pmadapter,
                                  pmlan_private * priv, int *tid)
{
    pmlan_private priv_tmp;
    raListTbl *ptr, *head;
    mlan_bssprio_node *bssprio_node, *bssprio_head;
    tid_tbl_t *tid_ptr;
    int i, j;
    int next_prio = 0;
    int next_tid = 0;
    ENTER();

    PRINTM(MDAT_D, "POP\n");
    for (j = pmadapter->priv_num - 1; j >= 0; --j) {
        if (!(util_peek_list(pmadapter->pmoal_handle,
                             &pmadapter->bssprio_tbl[j].bssprio_head,
                             pmadapter->callbacks.moal_spin_lock,
                             pmadapter->callbacks.moal_spin_unlock)))
            continue;

        if (pmadapter->bssprio_tbl[j].bssprio_cur == (mlan_bssprio_node *)
            & pmadapter->bssprio_tbl[j].bssprio_head) {
            pmadapter->bssprio_tbl[j].bssprio_cur =
                pmadapter->bssprio_tbl[j].bssprio_cur->pnext;
        }

        bssprio_head = bssprio_node = pmadapter->bssprio_tbl[j].bssprio_cur;

        do {
            priv_tmp = bssprio_node->priv;

            if ((priv_tmp->port_ctrl_mode == MTRUE)
                && (priv_tmp->port_open == MFALSE)) {
                PRINTM(MINFO, "get_highest_prio_ptr(): "
                       "PORT_CLOSED Ignore pkts from BSS%d\n",
                       priv_tmp->bss_index);
                /* Ignore data pkts from a BSS if port is closed */
                goto next_intf;
            }

            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                priv_tmp->wmm.ra_list_spinlock);

            for (i = util_scalar_read(pmadapter->pmoal_handle,
                                      &priv_tmp->wmm.highest_queued_prio,
                                      MNULL, MNULL); i >= LOW_PRIO_TID; --i) {

                tid_ptr = &(priv_tmp)->wmm.tid_tbl_ptr[tos_to_tid[i]];
                if (!util_peek_list
                    (pmadapter->pmoal_handle, &tid_ptr->ra_list, MNULL, MNULL))
                    continue;

                /* 
                 * Always choose the next ra we transmitted 
                 * last time, this way we pick the ra's in 
                 * round robin fashion.
                 */
                head = ptr = tid_ptr->ra_list_curr->pnext;
                if (ptr == (raListTbl *) & tid_ptr->ra_list)
                    head = ptr = ptr->pnext;

                do {
                    if (!ptr->tx_pause &&
                        util_peek_list(pmadapter->pmoal_handle, &ptr->buf_head,
                                       MNULL, MNULL)) {

                        /* Because WMM only support BK/BE/VI/VO, we have 8 tid
                           We should balance the traffic of the same AC */
                        if (i % 2)
                            next_prio = i - 1;
                        else
                            next_prio = i + 1;
                        next_tid = tos_to_tid[next_prio];
                        if (priv_tmp->wmm.pkts_queued[next_tid])
                            util_scalar_write(pmadapter->pmoal_handle,
                                              &priv_tmp->wmm.
                                              highest_queued_prio, next_prio,
                                              MNULL, MNULL);
                        else
                            /* if highest_queued_prio > i, set it to i */
                            util_scalar_conditional_write(pmadapter->
                                                          pmoal_handle,
                                                          &priv_tmp->wmm.
                                                          highest_queued_prio,
                                                          MLAN_SCALAR_COND_GREATER_THAN,
                                                          i, i, MNULL, MNULL);
                        *priv = priv_tmp;
                        *tid = tos_to_tid[i];
                        /* hold priv->ra_list_spinlock to maintain ptr */
                        PRINTM(MDAT_D, "get highest prio ptr %p, tid %d\n",
                               ptr, *tid);
                        LEAVE();
                        return ptr;
                    }

                    if ((ptr = ptr->pnext) == (raListTbl *) & tid_ptr->ra_list)
                        ptr = ptr->pnext;
                } while (ptr != head);
            }

            /* No packet at any TID for this priv.  Mark as such to skip
               checking TIDs for this priv (until pkt is added). */
            util_scalar_write(pmadapter->pmoal_handle,
                              &priv_tmp->wmm.highest_queued_prio,
                              NO_PKT_PRIO_TID, MNULL, MNULL);

            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  priv_tmp->wmm.
                                                  ra_list_spinlock);

          next_intf:
            if ((bssprio_node = bssprio_node->pnext) == (mlan_bssprio_node *)
                & pmadapter->bssprio_tbl[j].bssprio_head)
                bssprio_node = bssprio_node->pnext;
        } while (bssprio_node != bssprio_head);
    }

    LEAVE();
    return MNULL;
}

/**
 *  @brief This function gets the number of packets in the Tx queue
 *  
 *  @param priv         A pointer to mlan_private
 *  @param ptr          A pointer to RA list table
 *  @param maxBufSize   Maximum buffer size
 *
 *  @return             Packet count
 */
static int
wlan_num_pkts_in_txq(mlan_private * priv, raListTbl * ptr, int maxBufSize)
{
    int count = 0, total_size = 0;
    pmlan_buffer pmbuf;

    ENTER();

    for (pmbuf = (pmlan_buffer) ptr->buf_head.pnext;
         pmbuf != (pmlan_buffer) (&ptr->buf_head); pmbuf = pmbuf->pnext) {

        total_size += pmbuf->data_len;
        if (total_size < maxBufSize)
            ++count;
        else
            break;
    }

    LEAVE();
    return count;
}

/**
 *  @brief This function sends a single packet
 *  
 *  @param priv         A pointer to mlan_private
 *  @param ptr          A pointer to RA list table
 *  @param ptrindex     ptr's TID index
 *
 *  @return             N/A
 */
static void INLINE
wlan_send_single_packet(pmlan_private priv, raListTbl * ptr, int ptrindex)
{
    pmlan_buffer pmbuf;
    pmlan_buffer pmbuf_next;
    mlan_tx_param tx_param;
    pmlan_adapter pmadapter = priv->adapter;
    mlan_status status = MLAN_STATUS_SUCCESS;

    ENTER();

    if ((pmbuf = (pmlan_buffer) util_dequeue_list(pmadapter->pmoal_handle,
                                                  &ptr->buf_head,
                                                  MNULL, MNULL))) {
        PRINTM(MINFO, "Dequeuing the packet %p %p\n", ptr, pmbuf);
        priv->wmm.pkts_queued[ptrindex]--;
        util_scalar_decrement(pmadapter->pmoal_handle,
                              &priv->wmm.tx_pkts_queued, MNULL, MNULL);
        ptr->total_pkts--;
        pmbuf_next = (pmlan_buffer) util_peek_list(pmadapter->pmoal_handle,
                                                   &ptr->buf_head,
                                                   MNULL, MNULL);
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);

        tx_param.next_pkt_len = ((pmbuf_next)
                                 ? pmbuf_next->data_len + sizeof(TxPD) : 0);
        status = wlan_process_tx(priv, pmbuf, &tx_param);

        if (status == MLAN_STATUS_RESOURCE) {
            /** Queue the packet back at the head */
            PRINTM(MDAT_D, "Queuing pkt back to raList %p %p\n", ptr, pmbuf);
            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                priv->wmm.ra_list_spinlock);

            if (!wlan_is_ralist_valid(priv, ptr, ptrindex)) {
                pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                      priv->wmm.
                                                      ra_list_spinlock);
                wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
                LEAVE();
                return;
            }
            priv->wmm.pkts_queued[ptrindex]++;
            util_scalar_increment(pmadapter->pmoal_handle,
                                  &priv->wmm.tx_pkts_queued, MNULL, MNULL);
            util_enqueue_list_head(pmadapter->pmoal_handle, &ptr->buf_head,
                                   (pmlan_linked_list) pmbuf, MNULL, MNULL);

            ptr->total_pkts++;
            pmbuf->flags |= MLAN_BUF_FLAG_REQUEUED_PKT;
            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  priv->wmm.ra_list_spinlock);
        } else {
            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                priv->wmm.ra_list_spinlock);
            if (wlan_is_ralist_valid(priv, ptr, ptrindex)) {
                priv->wmm.packets_out[ptrindex]++;
                priv->wmm.tid_tbl_ptr[ptrindex].ra_list_curr = ptr;
            }
            pmadapter->bssprio_tbl[priv->bss_priority].bssprio_cur =
                pmadapter->bssprio_tbl[priv->bss_priority].bssprio_cur->pnext;
            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  priv->wmm.ra_list_spinlock);
        }
    } else {
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);
        PRINTM(MINFO, "Nothing to send\n");
    }

    LEAVE();
}

/**
 *  @brief This function checks if this mlan_buffer is already processed.
 *  
 *  @param priv     A pointer to mlan_private
 *  @param ptr      A pointer to RA list table
 *
 *  @return 	    MTRUE or MFALSE
 */
static int INLINE
wlan_is_ptr_processed(mlan_private * priv, raListTbl * ptr)
{
    pmlan_buffer pmbuf;

    if ((pmbuf = (pmlan_buffer) util_peek_list(priv->adapter->pmoal_handle,
                                               &ptr->buf_head, MNULL, MNULL))
        && (pmbuf->flags & MLAN_BUF_FLAG_REQUEUED_PKT))
        return MTRUE;

    return MFALSE;
}

/**
 *  @brief This function sends a single packet that has been processed
 *  
 *  @param priv         A pointer to mlan_private
 *  @param ptr          A pointer to RA list table
 *  @param ptrindex     ptr's TID index
 *
 *  @return             N/A
 */
static void INLINE
wlan_send_processed_packet(pmlan_private priv, raListTbl * ptr, int ptrindex)
{
    pmlan_buffer pmbuf_next;
    mlan_tx_param tx_param;
    pmlan_buffer pmbuf;
    pmlan_adapter pmadapter = priv->adapter;
    mlan_status ret = MLAN_STATUS_FAILURE;

    if ((pmbuf = (pmlan_buffer) util_dequeue_list(pmadapter->pmoal_handle,
                                                  &ptr->buf_head,
                                                  MNULL, MNULL))) {
        pmbuf_next =
            (pmlan_buffer) util_peek_list(pmadapter->pmoal_handle,
                                          &ptr->buf_head, MNULL, MNULL);
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);
        tx_param.next_pkt_len =
            ((pmbuf_next) ? pmbuf_next->data_len + sizeof(TxPD) : 0);
        ret =
            wlan_sdio_host_to_card(pmadapter, MLAN_TYPE_DATA, pmbuf, &tx_param);
        switch (ret) {
        case MLAN_STATUS_RESOURCE:
            PRINTM(MINFO, "MLAN_STATUS_RESOURCE is returned\n");
            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                priv->wmm.ra_list_spinlock);

            if (!wlan_is_ralist_valid(priv, ptr, ptrindex)) {
                pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                      priv->wmm.
                                                      ra_list_spinlock);
                wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
                LEAVE();
                return;
            }
            util_enqueue_list_head(pmadapter->pmoal_handle,
                                   &ptr->buf_head,
                                   (pmlan_linked_list) pmbuf, MNULL, MNULL);

            pmbuf->flags |= MLAN_BUF_FLAG_REQUEUED_PKT;
            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  priv->wmm.ra_list_spinlock);
            break;
        case MLAN_STATUS_FAILURE:
            pmadapter->data_sent = MFALSE;
            PRINTM(MERROR, "Error: Failed to write data\n");
            pmadapter->dbg.num_tx_host_to_card_failure++;
            pmbuf->status_code = MLAN_ERROR_DATA_TX_FAIL;
            wlan_write_data_complete(pmadapter, pmbuf, ret);
            break;
        case MLAN_STATUS_PENDING:
            pmadapter->data_sent = MFALSE;
        default:
            break;
        }
        if (ret != MLAN_STATUS_RESOURCE) {
            pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                                priv->wmm.ra_list_spinlock);
            if (wlan_is_ralist_valid(priv, ptr, ptrindex)) {
                priv->wmm.packets_out[ptrindex]++;
                priv->wmm.tid_tbl_ptr[ptrindex].ra_list_curr = ptr;
                ptr->total_pkts--;
            }
            pmadapter->bssprio_tbl[priv->bss_priority].bssprio_cur =
                pmadapter->bssprio_tbl[priv->bss_priority].bssprio_cur->pnext;
            priv->wmm.pkts_queued[ptrindex]--;
            util_scalar_decrement(pmadapter->pmoal_handle,
                                  &priv->wmm.tx_pkts_queued, MNULL, MNULL);
            pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                                  priv->wmm.ra_list_spinlock);
        }
    } else {
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);
    }
}

/**
 *  @brief This function dequeues a packet
 *  
 *  @param pmadapter  A pointer to mlan_adapter
 *
 *  @return 	    MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
static int
wlan_dequeue_tx_packet(pmlan_adapter pmadapter)
{
    raListTbl *ptr;
    pmlan_private priv = MNULL;
    int ptrindex = 0;
    t_u8 ra[MLAN_MAC_ADDR_LENGTH];
    int tid_del = 0;
    int tid = 0;
    t_u8 avail_ports = 0;
    int i;

    ENTER();

    if (!(ptr = wlan_wmm_get_highest_priolist_ptr(pmadapter, &priv, &ptrindex))) {
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    /* Note:- Spinlock is locked in wlan_wmm_get_highest_priolist_ptr when it
       returns a pointer (for the priv it returns), and is unlocked in
       wlan_send_processed_packet, wlan_send_single_packet or
       wlan_11n_aggregate_pkt.  The spinlock would be required for some parts
       of both of function.  But, the the bulk of these function will execute
       w/o spinlock.  Unlocking the spinlock inside these function will help
       us avoid taking the spinlock again, check to see if the ptr is still
       valid and then proceed. This is done purely to increase execution time. 
     */

    /* Note:- Also, anybody adding code which does not get into
       wlan_send_processed_packet, wlan_send_single_packet, or
       wlan_11n_aggregate_pkt should make sure ra_list_spinlock is freed.
       Otherwise there would be a lock up. */

    tid = wlan_get_tid(priv->adapter, ptr);
    if (tid >= MAX_NUM_TID)
        tid = wlan_wmm_downgrade_tid(priv, tid);

    for (i = 1; i < pmadapter->mp_end_port; i++) {
        if ((pmadapter->mp_wr_bitmap >> i) & 1)
            avail_ports++;
    }

    PRINTM(MINFO,
           "mp_wr_bitmap=0x%08x avail_ports=%d tid=%d tx_eligibility[%d]=%d\n",
           pmadapter->mp_wr_bitmap, avail_ports,
           tid, tid, pmadapter->tx_eligibility[tid]);

    if (avail_ports < pmadapter->tx_eligibility[tid]) {
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);
        pmadapter->data_sent = MTRUE;
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }

    if (wlan_is_ptr_processed(priv, ptr)) {
        wlan_send_processed_packet(priv, ptr, ptrindex);
        LEAVE();
        return MLAN_STATUS_SUCCESS;
    }

    if (!ptr->is_11n_enabled || wlan_is_bastream_setup(priv, ptr, tid)
#ifdef STA_SUPPORT
        || ((priv->sec_info.ewpa_enabled == MFALSE) &&
            ((priv->sec_info.wpa_enabled
              || priv->sec_info.wpa2_enabled) &&
             priv->wpa_is_gtk_set == MFALSE))
        || priv->wps.session_enable
#endif /* STA_SUPPORT */
        ) {
        if (ptr->is_11n_enabled && wlan_is_bastream_setup(priv, ptr, tid)
            && wlan_is_amsdu_in_ampdu_allowed(priv, ptr, tid)
            && wlan_is_amsdu_allowed(priv, ptr, tid)
            && (wlan_num_pkts_in_txq(priv, ptr, pmadapter->tx_buf_size) >=
                MIN_NUM_AMSDU)) {
            wlan_11n_aggregate_pkt(priv, ptr, INTF_HEADER_LEN, ptrindex);
        } else
            wlan_send_single_packet(priv, ptr, ptrindex);
    } else {
        if (wlan_is_ampdu_allowed(priv, ptr, tid) &&
            (ptr->packet_count > ptr->ba_packet_threshold)) {
            if (wlan_is_bastream_avail(priv)) {
                PRINTM(MINFO, "BA setup threshold %d reached. tid=%d\n",
                       ptr->packet_count, tid);
                wlan_11n_create_txbastream_tbl(priv,
                                               ptr->ra, tid,
                                               BA_STREAM_SETUP_INPROGRESS);
                wlan_send_addba(priv, tid, ptr->ra);
            } else if (wlan_find_stream_to_delete(priv, ptr, tid, &tid_del, ra)) {
                PRINTM(MDAT_D, "tid_del=%d tid=%d\n", tid_del, tid);
                wlan_11n_create_txbastream_tbl(priv,
                                               ptr->ra, tid,
                                               BA_STREAM_SETUP_INPROGRESS);
                wlan_send_delba(priv, tid_del, ra, 1);
            }
        }
        if (wlan_is_amsdu_allowed(priv, ptr, tid) &&
            (wlan_num_pkts_in_txq(priv, ptr,
                                  pmadapter->tx_buf_size) >= MIN_NUM_AMSDU)) {
            wlan_11n_aggregate_pkt(priv, ptr, INTF_HEADER_LEN, ptrindex);
        } else {
            wlan_send_single_packet(priv, ptr, ptrindex);
        }
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief update tx_pause flag in ra_list
 *
 *  @param priv		  A pointer to mlan_private
 *  @param mac        peer mac address
 *  @param tx_pause   tx_pause flag (0/1)
 *
 *  @return           N/A
 */
t_void
wlan_updata_ralist_tx_pause(pmlan_private priv, t_u8 * mac, t_u8 tx_pause)
{
    raListTbl *ra_list;
    int i;
    pmlan_adapter pmadapter = priv->adapter;
    t_u32 pkt_cnt = 0;
    t_u32 tx_pkts_queued = 0;
    ENTER();

    pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                        priv->wmm.ra_list_spinlock);
    for (i = 0; i < MAX_NUM_TID; ++i) {
        ra_list = wlan_wmm_get_ralist_node(priv, i, mac);
        if (ra_list) {
            pkt_cnt += ra_list->total_pkts;
            ra_list->tx_pause = tx_pause;
        }
    }
    if (pkt_cnt) {
        tx_pkts_queued = util_scalar_read(pmadapter->pmoal_handle,
                                          &priv->wmm.tx_pkts_queued, MNULL,
                                          MNULL);
        if (tx_pause)
            tx_pkts_queued -= pkt_cnt;
        else
            tx_pkts_queued += pkt_cnt;
        util_scalar_write(priv->adapter->pmoal_handle,
                          &priv->wmm.tx_pkts_queued, tx_pkts_queued, MNULL,
                          MNULL);
        util_scalar_write(priv->adapter->pmoal_handle,
                          &priv->wmm.highest_queued_prio, HIGH_PRIO_TID, MNULL,
                          MNULL);
    }
    pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                          priv->wmm.ra_list_spinlock);
    LEAVE();
}

#ifdef STA_SUPPORT
#endif /* STA_SUPPORT */
/********************************************************
    Global Functions
********************************************************/

/**
 *  @brief Get the threshold value for BA setup using system time.
 *
 *  @param pmadapter       Pointer to the mlan_adapter structure
 *
 *  @return         threshold value.
 */
t_u8
wlan_get_random_ba_threshold(pmlan_adapter pmadapter)
{
    t_u32 sec, usec;
    t_u8 ba_threshold = 0;

    ENTER();

    /* setup ba_packet_threshold here random number between
       [BA_SETUP_PACKET_OFFSET,
       BA_SETUP_PACKET_OFFSET+BA_SETUP_MAX_PACKET_THRESHOLD-1] */

#define BA_SETUP_MAX_PACKET_THRESHOLD   16
#define BA_SETUP_PACKET_OFFSET          16

    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle, &sec,
                                              &usec);
    sec = (sec & 0xFFFF) + (sec >> 16);
    usec = (usec & 0xFFFF) + (usec >> 16);

    ba_threshold =
        (((sec << 16) + usec) % BA_SETUP_MAX_PACKET_THRESHOLD) +
        BA_SETUP_PACKET_OFFSET;
    PRINTM(MINFO, "setup BA after %d packets\n", ba_threshold);

    LEAVE();
    return ba_threshold;
}

/**
 *  @brief  This function cleans Tx/Rx queues
 *
 *  @param priv		A pointer to mlan_private
 *
 *  @return		N/A
 */
t_void
wlan_clean_txrx(pmlan_private priv)
{
    mlan_adapter *pmadapter = priv->adapter;
    t_u8 i = 0;

    ENTER();

    if (GET_BSS_ROLE(priv) == MLAN_BSS_ROLE_STA) {
        wlan_cleanup_bypass_txq(pmadapter);
    }

    wlan_11n_cleanup_reorder_tbl(priv);

    pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                        priv->wmm.ra_list_spinlock);

    wlan_wmm_cleanup_queues(priv);
    wlan_11n_deleteall_txbastream_tbl(priv);
#ifdef SDIO_MULTI_PORT_TX_AGGR
    MP_TX_AGGR_BUF_RESET(priv->adapter);
#endif
#ifdef SDIO_MULTI_PORT_RX_AGGR
    MP_RX_AGGR_BUF_RESET(priv->adapter);
#endif
    wlan_wmm_delete_all_ralist(priv);
    memcpy(pmadapter, tos_to_tid, ac_to_tid, sizeof(tos_to_tid));
    for (i = 0; i < MAX_NUM_TID; i++) {
        tos_to_tid_inv[tos_to_tid[i]] = (t_u8) i;
    }
#if defined(UAP_SUPPORT)
    priv->num_drop_pkts = 0;
#endif
    pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                          priv->wmm.ra_list_spinlock);

    LEAVE();
}

/**
 *  @brief Set the WMM queue priorities to their default values
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *
 *  @return         N/A
 */
void
wlan_wmm_default_queue_priorities(pmlan_private priv)
{
    ENTER();

    /* Default queue priorities: VO->VI->BE->BK */
    priv->wmm.queue_priority[0] = WMM_AC_VO;
    priv->wmm.queue_priority[1] = WMM_AC_VI;
    priv->wmm.queue_priority[2] = WMM_AC_BE;
    priv->wmm.queue_priority[3] = WMM_AC_BK;

    LEAVE();
}

/**
 *  @brief Initialize WMM priority queues
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *  @param pwmm_ie  Pointer to the IEEEtypes_WmmParameter_t data struct
 *
 *  @return         N/A
 */
void
wlan_wmm_setup_queue_priorities(pmlan_private priv,
                                IEEEtypes_WmmParameter_t * pwmm_ie)
{
    t_u16 cw_min, avg_back_off, tmp[4];
    t_u32 i, j, num_ac;
    t_u8 ac_idx;

    ENTER();

    if (!pwmm_ie || priv->wmm_enabled == MFALSE) {
        /* WMM is not enabled, just set the defaults and return */
        wlan_wmm_default_queue_priorities(priv);
        LEAVE();
        return;
    }

    HEXDUMP("WMM: setup_queue_priorities: param IE",
            (t_u8 *) pwmm_ie, sizeof(IEEEtypes_WmmParameter_t));

    PRINTM(MINFO, "WMM Parameter IE: version=%d, "
           "qos_info Parameter Set Count=%d, Reserved=%#x\n",
           pwmm_ie->vend_hdr.version, pwmm_ie->qos_info.para_set_count,
           pwmm_ie->reserved);

    for (num_ac = 0; num_ac < NELEMENTS(pwmm_ie->ac_params); num_ac++) {
        cw_min = (1 << pwmm_ie->ac_params[num_ac].ecw.ecw_min) - 1;
        avg_back_off
            = (cw_min >> 1) + pwmm_ie->ac_params[num_ac].aci_aifsn.aifsn;

        ac_idx = wmm_aci_to_qidx_map[pwmm_ie->ac_params[num_ac].aci_aifsn.aci];
        priv->wmm.queue_priority[ac_idx] = ac_idx;
        tmp[ac_idx] = avg_back_off;

        PRINTM(MCMND, "WMM: CWmax=%d CWmin=%d Avg Back-off=%d\n",
               (1 << pwmm_ie->ac_params[num_ac].ecw.ecw_max) - 1,
               cw_min, avg_back_off);
        PRINTM_AC(&pwmm_ie->ac_params[num_ac]);
    }

    HEXDUMP("WMM: avg_back_off", (t_u8 *) tmp, sizeof(tmp));
    HEXDUMP("WMM: queue_priority", priv->wmm.queue_priority,
            sizeof(priv->wmm.queue_priority));

    /* Bubble sort */
    for (i = 0; i < num_ac; i++) {
        for (j = 1; j < num_ac - i; j++) {
            if (tmp[j - 1] > tmp[j]) {
                SWAP_U16(tmp[j - 1], tmp[j]);
                SWAP_U8(priv->wmm.queue_priority[j - 1],
                        priv->wmm.queue_priority[j]);
            } else if (tmp[j - 1] == tmp[j]) {
                if (priv->wmm.queue_priority[j - 1]
                    < priv->wmm.queue_priority[j]) {
                    SWAP_U8(priv->wmm.queue_priority[j - 1],
                            priv->wmm.queue_priority[j]);
                }
            }
        }
    }

    wlan_wmm_queue_priorities_tid(priv, priv->wmm.queue_priority);

    HEXDUMP("WMM: avg_back_off, sort", (t_u8 *) tmp, sizeof(tmp));
    DBG_HEXDUMP(MCMD_D, "WMM: queue_priority, sort", priv->wmm.queue_priority,
                sizeof(priv->wmm.queue_priority));
    LEAVE();
}

/**
 *  @brief Downgrade WMM priority queue
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *
 *  @return         N/A
 */
void
wlan_wmm_setup_ac_downgrade(pmlan_private priv)
{
    int ac_val;

    ENTER();

    PRINTM(MINFO, "WMM: AC Priorities: BK(0), BE(1), VI(2), VO(3)\n");

    if (priv->wmm_enabled == MFALSE) {
        /* WMM is not enabled, default priorities */
        for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
            priv->wmm.ac_down_graded_vals[ac_val] = (mlan_wmm_ac_e) ac_val;
        }
    } else {
        for (ac_val = WMM_AC_BK; ac_val <= WMM_AC_VO; ac_val++) {
            priv->wmm.ac_down_graded_vals[ac_val]
                = wlan_wmm_eval_downgrade_ac(priv, (mlan_wmm_ac_e) ac_val);
            PRINTM(MINFO, "WMM: AC PRIO %d maps to %d\n",
                   ac_val, priv->wmm.ac_down_graded_vals[ac_val]);
        }
    }

    LEAVE();
}

/**
 *  @brief  Allocate and add a RA list for all TIDs with the given RA
 *
 *  @param priv  Pointer to the mlan_private driver data struct
 *  @param ra	 Address of the receiver STA (AP in case of infra)
 *
 *  @return      N/A
 */
void
wlan_ralist_add(mlan_private * priv, t_u8 * ra)
{
    int i;
    raListTbl *ra_list;
    pmlan_adapter pmadapter = priv->adapter;

    ENTER();

    for (i = 0; i < MAX_NUM_TID; ++i) {
        ra_list = wlan_wmm_allocate_ralist_node(pmadapter, ra);
        PRINTM(MINFO, "Creating RA List %p for tid %d\n", ra_list, i);
        if (!ra_list)
            break;
        ra_list->max_amsdu = 0;
        if (queuing_ra_based(priv)) {
            ra_list->is_11n_enabled = wlan_is_11n_enabled(priv, ra);
            if (ra_list->is_11n_enabled)
                ra_list->max_amsdu = get_station_max_amsdu_size(priv, ra);
            ra_list->tx_pause = wlan_is_tx_pause(priv, ra);
        } else {
            ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
            if (ra_list->is_11n_enabled)
                ra_list->max_amsdu = priv->max_amsdu;
        }

        PRINTM(MDATA, "ralist %p: is_11n_enabled=%d max_amsdu=%d\n",
               ra_list, ra_list->is_11n_enabled, ra_list->max_amsdu);

        if (ra_list->is_11n_enabled) {
            ra_list->packet_count = 0;
            ra_list->ba_packet_threshold =
                wlan_get_random_ba_threshold(pmadapter);
        }

        util_enqueue_list_tail(pmadapter->pmoal_handle,
                               &priv->wmm.tid_tbl_ptr[i].ra_list,
                               (pmlan_linked_list) ra_list, MNULL, MNULL);

        if (!priv->wmm.tid_tbl_ptr[i].ra_list_curr)
            priv->wmm.tid_tbl_ptr[i].ra_list_curr = ra_list;
    }

    LEAVE();
}

/**
 *  @brief Initialize the WMM state information and the WMM data path queues.
 *
 *  @param pmadapter  Pointer to the mlan_adapter data structure
 *
 *  @return         N/A
 */
t_void
wlan_wmm_init(pmlan_adapter pmadapter)
{
    int i, j;
    pmlan_private priv;

    ENTER();

    for (j = 0; j < pmadapter->priv_num; ++j) {
        if ((priv = pmadapter->priv[j])) {
            for (i = 0; i < MAX_NUM_TID; ++i) {
                priv->aggr_prio_tbl[i].amsdu = BA_STREAM_NOT_ALLOWED;
#ifdef WIFI_DIRECT_SUPPORT
                if (priv->bss_type == MLAN_BSS_TYPE_WIFIDIRECT)
                    priv->aggr_prio_tbl[i].ampdu_ap =
                        priv->aggr_prio_tbl[i].ampdu_user =
                        BA_STREAM_NOT_ALLOWED;
                else
#endif
                    priv->aggr_prio_tbl[i].ampdu_ap =
                        priv->aggr_prio_tbl[i].ampdu_user = tos_to_tid_inv[i];
                priv->wmm.pkts_queued[i] = 0;
                priv->wmm.tid_tbl_ptr[i].ra_list_curr = MNULL;
            }

            priv->aggr_prio_tbl[6].ampdu_ap
                = priv->aggr_prio_tbl[6].ampdu_user = BA_STREAM_NOT_ALLOWED;

            priv->aggr_prio_tbl[7].ampdu_ap
                = priv->aggr_prio_tbl[7].ampdu_user = BA_STREAM_NOT_ALLOWED;

            priv->add_ba_param.timeout = MLAN_DEFAULT_BLOCK_ACK_TIMEOUT;
            priv->add_ba_param.tx_win_size = MLAN_AMPDU_DEF_TXWINSIZE;
            priv->add_ba_param.rx_win_size = MLAN_AMPDU_DEF_RXWINSIZE;
            priv->add_ba_param.tx_amsdu = MTRUE;
            priv->add_ba_param.rx_amsdu = MTRUE;
            memset(priv->adapter, priv->rx_seq, 0xff, sizeof(priv->rx_seq));
            wlan_wmm_default_queue_priorities(priv);
        }
    }

    LEAVE();
}

/**
 *  @brief Setup the queue priorities and downgrade any queues as required
 *         by the WMM info.  Setups default values if WMM is not active
 *         for this association.
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *
 *  @return         N/A
 */
void
wlan_wmm_setup_queues(pmlan_private priv)
{
    ENTER();
    wlan_wmm_setup_queue_priorities(priv, MNULL);
    wlan_wmm_setup_ac_downgrade(priv);
    LEAVE();
}

#ifdef STA_SUPPORT
/**
 *  @brief  Send a command to firmware to retrieve the current WMM status
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *
 *  @return         MLAN_STATUS_SUCCESS; MLAN_STATUS_FAILURE
 */
mlan_status
wlan_cmd_wmm_status_change(pmlan_private priv)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;

    ENTER();

    ret = wlan_prepare_cmd(priv, HostCmd_CMD_WMM_GET_STATUS, 0, 0, 0, MNULL);
    LEAVE();
    return ret;
}
#endif

/**
 *  @brief Check if wmm TX queue is empty
 *
 *  @param pmadapter  Pointer to the mlan_adapter driver data struct
 *
 *  @return         MFALSE if not empty; MTRUE if empty
 */
int
wlan_wmm_lists_empty(pmlan_adapter pmadapter)
{
    int j;
    pmlan_private priv;

    ENTER();

    for (j = 0; j < pmadapter->priv_num; ++j) {
        if ((priv = pmadapter->priv[j])) {
            if ((priv->port_ctrl_mode == MTRUE) && (priv->port_open == MFALSE)) {
                PRINTM(MINFO,
                       "wmm_lists_empty: PORT_CLOSED Ignore pkts from BSS%d\n",
                       j);
                continue;
            }

            if (util_scalar_read(pmadapter->pmoal_handle,
                                 &priv->wmm.tx_pkts_queued,
                                 pmadapter->callbacks.moal_spin_lock,
                                 pmadapter->callbacks.moal_spin_unlock)) {
                LEAVE();
                return MFALSE;
            }
        }
    }

    LEAVE();
    return MTRUE;
}

/**
 *   @brief Get ralist node
 *  
 *   @param priv     Pointer to the mlan_private driver data struct
 *   @param tid      TID
 *   @param ra_addr  Pointer to the route address
 * 
 *   @return         ra_list or MNULL
 */
raListTbl *
wlan_wmm_get_ralist_node(pmlan_private priv, t_u8 tid, t_u8 * ra_addr)
{
    raListTbl *ra_list;
    ENTER();
    ra_list =
        (raListTbl *) util_peek_list(priv->adapter->pmoal_handle,
                                     &priv->wmm.tid_tbl_ptr[tid].ra_list, MNULL,
                                     MNULL);
    while (ra_list && (ra_list != (raListTbl *)
                       & priv->wmm.tid_tbl_ptr[tid].ra_list)) {
        if (!memcmp(priv->adapter, ra_list->ra, ra_addr, MLAN_MAC_ADDR_LENGTH)) {
            LEAVE();
            return ra_list;
        }
        ra_list = ra_list->pnext;
    }
    LEAVE();
    return MNULL;
}

/**
 *   @brief Check if RA list is valid or not
 *  
 *   @param priv     Pointer to the mlan_private driver data struct
 *   @param ra_list  Pointer to raListTbl
 *   @param ptrindex TID pointer index
 * 
 *   @return         MTRUE- valid. MFALSE- invalid. 
 */
int
wlan_is_ralist_valid(mlan_private * priv, raListTbl * ra_list, int ptrindex)
{
    raListTbl *rlist;

    ENTER();

    rlist =
        (raListTbl *) util_peek_list(priv->adapter->pmoal_handle,
                                     &priv->wmm.tid_tbl_ptr[ptrindex].ra_list,
                                     MNULL, MNULL);

    while (rlist && (rlist != (raListTbl *)
                     & priv->wmm.tid_tbl_ptr[ptrindex].ra_list)) {
        if (rlist == ra_list) {
            LEAVE();
            return MTRUE;
        }

        rlist = rlist->pnext;
    }
    LEAVE();
    return MFALSE;
}

/**
 *  @brief  Update an existing raList with a new RA and 11n capability
 *
 *  @param priv     Pointer to the mlan_private driver data struct
 *  @param old_ra   Old receiver address
 *  @param new_ra   New receiver address
 *
 *  @return         integer count of updated nodes
 */
int
wlan_ralist_update(mlan_private * priv, t_u8 * old_ra, t_u8 * new_ra)
{
    t_u8 tid;
    int update_count;
    raListTbl *ra_list;

    ENTER();

    update_count = 0;

    for (tid = 0; tid < MAX_NUM_TID; ++tid) {

        ra_list = wlan_wmm_get_ralist_node(priv, tid, old_ra);

        if (ra_list) {
            update_count++;

            if (queuing_ra_based(priv))
                ra_list->is_11n_enabled = wlan_is_11n_enabled(priv, new_ra);
            else
                ra_list->is_11n_enabled = IS_11N_ENABLED(priv);
            ra_list->packet_count = 0;
            ra_list->ba_packet_threshold =
                wlan_get_random_ba_threshold(priv->adapter);
            PRINTM(MINFO,
                   "ralist_update: %p, %d, %02x:%02x:%02x:%02x:%02x:%02x-->"
                   "%02x:%02x:%02x:%02x:%02x:%02x\n", ra_list,
                   ra_list->is_11n_enabled, ra_list->ra[0], ra_list->ra[1],
                   ra_list->ra[2], ra_list->ra[3], ra_list->ra[4],
                   ra_list->ra[5], new_ra[0], new_ra[1], new_ra[2], new_ra[3],
                   new_ra[4], new_ra[5]);

            memcpy(priv->adapter, ra_list->ra, new_ra, MLAN_MAC_ADDR_LENGTH);
        }
    }

    LEAVE();
    return update_count;
}

/**
 *  @brief Add packet to WMM queue
 *
 *  @param pmadapter  Pointer to the mlan_adapter driver data struct
 *  @param pmbuf      Pointer to the mlan_buffer data struct
 *
 *  @return         N/A
 */
t_void
wlan_wmm_add_buf_txqueue(pmlan_adapter pmadapter, pmlan_buffer pmbuf)
{
    pmlan_private priv = pmadapter->priv[pmbuf->bss_index];
    t_u32 tid;
    raListTbl *ra_list;
    t_u8 ra[MLAN_MAC_ADDR_LENGTH], tid_down;

    ENTER();

    pmbuf->buf_type = MLAN_BUF_TYPE_DATA;
    if (!priv->media_connected) {
        PRINTM(MWARN, "Drop packet in disconnect state\n");
        wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
        LEAVE();
        return;
    }
    tid = pmbuf->priority;
    pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                        priv->wmm.ra_list_spinlock);
    tid_down = wlan_wmm_downgrade_tid(priv, tid);

    /* In case of infra as we have already created the list during association
       we just don't have to call get_queue_raptr, we will have only 1 raptr
       for a tid in case of infra */
    if (!queuing_ra_based(priv)) {
        ra_list = (raListTbl *) util_peek_list(pmadapter->pmoal_handle,
                                               &priv->wmm.tid_tbl_ptr[tid_down].
                                               ra_list, MNULL, MNULL);
    } else {
        memcpy(pmadapter, ra, pmbuf->pbuf + pmbuf->data_offset,
               MLAN_MAC_ADDR_LENGTH);
        /** put multicast/broadcast packet in the same ralist */
        if (ra[0] & 0x01)
            memset(pmadapter, ra, 0xff, sizeof(ra));
        ra_list = wlan_wmm_get_queue_raptr(priv, tid_down, ra);
    }

    if (!ra_list) {
        PRINTM(MWARN, "Drop packet, ra_list=%p, "
               "media_connected=%d\n", ra_list, priv->media_connected);
        pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                              priv->wmm.ra_list_spinlock);
        wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
        LEAVE();
        return;
    }

    PRINTM(MDATA, "Adding pkt to ra_list %p %p priority=%d, tid_down=%d\n",
           ra_list, pmbuf, pmbuf->priority, tid_down);
    util_enqueue_list_tail(pmadapter->pmoal_handle, &ra_list->buf_head,
                           (pmlan_linked_list) pmbuf, MNULL, MNULL);

    ra_list->total_pkts++;
    ra_list->packet_count++;

    priv->wmm.pkts_queued[tid_down]++;
    if (!ra_list->tx_pause) {
        util_scalar_increment(pmadapter->pmoal_handle,
                              &priv->wmm.tx_pkts_queued, MNULL, MNULL);
        /* if highest_queued_prio < prio(tid_down), set it to prio(tid_down) */
        util_scalar_conditional_write(pmadapter->pmoal_handle,
                                      &priv->wmm.highest_queued_prio,
                                      MLAN_SCALAR_COND_LESS_THAN,
                                      tos_to_tid_inv[tid_down],
                                      tos_to_tid_inv[tid_down], MNULL, MNULL);
    }
    /* Record the current time the packet was queued; used to determine the
       amount of time the packet was queued in the driver before it was sent to 
       the firmware.  The delay is then sent along with the packet to the
       firmware for aggregate delay calculation for stats and MSDU lifetime
       expiry. */
    pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                          priv->wmm.ra_list_spinlock);
    pmadapter->callbacks.moal_get_system_time(pmadapter->pmoal_handle,
                                              &pmbuf->in_ts_sec,
                                              &pmbuf->in_ts_usec);

    LEAVE();
}

#ifdef STA_SUPPORT
/**
 *  @brief Process the GET_WMM_STATUS command response from firmware
 *
 *  The GET_WMM_STATUS response may contain multiple TLVs for:
 *      - AC Queue status TLVs
 *      - Current WMM Parameter IE TLV
 *      - Admission Control action frame TLVs
 *
 *  This function parses the TLVs and then calls further functions
 *   to process any changes in the queue prioritize or state.
 *
 *  @param priv      Pointer to the mlan_private driver data struct
 *  @param ptlv      Pointer to the tlv block returned in the response.
 *  @param resp_len  Length of TLV block
 *
 *  @return MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_get_status(pmlan_private priv, t_u8 * ptlv, int resp_len)
{
    t_u8 *pcurrent = ptlv;
    t_u32 tlv_len;
    t_u8 sendWmmEvent;
    MrvlIEtypes_Data_t *pTlvHdr;
    MrvlIEtypes_WmmQueueStatus_t *pTlvWmmQStatus;
    IEEEtypes_WmmParameter_t *pWmmParamIe = MNULL;
    WmmAcStatus_t *pac_status;

    MrvlIETypes_ActionFrame_t *pTlvAction;
    IEEEtypes_Action_WMM_AddTsRsp_t *pAddTsRsp;
    IEEEtypes_Action_WMM_DelTs_t *pDelTs;

    ENTER();

    sendWmmEvent = MFALSE;

    PRINTM(MINFO, "WMM: WMM_GET_STATUS cmdresp received: %d\n", resp_len);
    HEXDUMP("CMD_RESP: WMM_GET_STATUS", pcurrent, resp_len);

    while (resp_len >= sizeof(pTlvHdr->header)) {
        pTlvHdr = (MrvlIEtypes_Data_t *) pcurrent;
        tlv_len = wlan_le16_to_cpu(pTlvHdr->header.len);

        switch (wlan_le16_to_cpu(pTlvHdr->header.type)) {
        case TLV_TYPE_WMMQSTATUS:
            pTlvWmmQStatus = (MrvlIEtypes_WmmQueueStatus_t *) pTlvHdr;
            PRINTM(MEVENT, "WMM_STATUS: QSTATUS TLV: %d\n",
                   pTlvWmmQStatus->queue_index);

            PRINTM(MINFO,
                   "CMD_RESP: WMM_GET_STATUS: QSTATUS TLV: %d, %d, %d\n",
                   pTlvWmmQStatus->queue_index,
                   pTlvWmmQStatus->flow_required, pTlvWmmQStatus->disabled);

            pac_status = &priv->wmm.ac_status[pTlvWmmQStatus->queue_index];
            pac_status->disabled = pTlvWmmQStatus->disabled;
            pac_status->flow_required = pTlvWmmQStatus->flow_required;
            pac_status->flow_created = pTlvWmmQStatus->flow_created;
            break;

        case WMM_IE:
            /* 
             * Point the regular IEEE IE 2 bytes into the Marvell IE
             *   and setup the IEEE IE type and length byte fields
             */

            PRINTM(MEVENT, "WMM STATUS: WMM IE\n");

            HEXDUMP("WMM: WMM TLV:", (t_u8 *) pTlvHdr, tlv_len + 4);

            pWmmParamIe = (IEEEtypes_WmmParameter_t *) (pcurrent + 2);
            pWmmParamIe->vend_hdr.len = (t_u8) tlv_len;
            pWmmParamIe->vend_hdr.element_id = WMM_IE;

            PRINTM(MINFO, "CMD_RESP: WMM_GET_STATUS: WMM Parameter Set: %d\n",
                   pWmmParamIe->qos_info.para_set_count);

            memcpy(priv->adapter,
                   (t_u8 *) & priv->curr_bss_params.bss_descriptor.wmm_ie,
                   pWmmParamIe, MIN(sizeof(IEEEtypes_WmmParameter_t),
                                    (pWmmParamIe->vend_hdr.len + 2)));
            sendWmmEvent = MTRUE;
            break;

        case TLV_TYPE_IEEE_ACTION_FRAME:
            PRINTM(MEVENT, "WMM_STATUS: IEEE Action Frame\n");
            pTlvAction = (MrvlIETypes_ActionFrame_t *) pcurrent;

            if (pTlvAction->actionFrame.wmmAc.tspecAct.category
                == IEEE_MGMT_ACTION_CATEGORY_WMM_TSPEC) {

                switch (pTlvAction->actionFrame.wmmAc.tspecAct.action) {
                case TSPEC_ACTION_CODE_ADDTS_RSP:
                    pAddTsRsp = &pTlvAction->actionFrame.wmmAc.addTsRsp;
                    wlan_send_wmmac_host_event(priv, "ADDTS_RSP",
                                               pTlvAction->srcAddr,
                                               pAddTsRsp->tspecIE.TspecBody.
                                               TSInfo.TID,
                                               pAddTsRsp->tspecIE.TspecBody.
                                               TSInfo.UserPri,
                                               pAddTsRsp->statusCode);
                    break;

                case TSPEC_ACTION_CODE_DELTS:
                    pDelTs = &pTlvAction->actionFrame.wmmAc.delTs;
                    wlan_send_wmmac_host_event(priv, "DELTS_RX",
                                               pTlvAction->srcAddr,
                                               pDelTs->tspecIE.TspecBody.TSInfo.
                                               TID,
                                               pDelTs->tspecIE.TspecBody.TSInfo.
                                               UserPri, pDelTs->reasonCode);
                    break;

                case TSPEC_ACTION_CODE_ADDTS_REQ:
                default:
                    break;
                }
            }
            break;

        default:
            break;
        }

        pcurrent += (tlv_len + sizeof(pTlvHdr->header));
        resp_len -= (tlv_len + sizeof(pTlvHdr->header));
    }

    wlan_wmm_setup_queue_priorities(priv, pWmmParamIe);
    wlan_wmm_setup_ac_downgrade(priv);

    if (sendWmmEvent) {
        wlan_recv_event(priv, MLAN_EVENT_ID_FW_WMM_CONFIG_CHANGE, MNULL);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief Call back from the command module to allow insertion of a WMM TLV
 *
 *  If the BSS we are associating to supports WMM, add the required WMM
 *    Information IE to the association request command buffer in the form
 *    of a Marvell extended IEEE IE.
 *
 *  @param priv        Pointer to the mlan_private driver data struct
 *  @param ppAssocBuf  Output parameter: Pointer to the TLV output buffer,
 *                     modified on return to point after the appended WMM TLV
 *  @param pWmmIE      Pointer to the WMM IE for the BSS we are joining
 *  @param pHTCap      Pointer to the HT IE for the BSS we are joining
 *
 *  @return Length of data appended to the association tlv buffer
 */
t_u32
wlan_wmm_process_association_req(pmlan_private priv,
                                 t_u8 ** ppAssocBuf,
                                 IEEEtypes_WmmParameter_t * pWmmIE,
                                 IEEEtypes_HTCap_t * pHTCap)
{
    MrvlIEtypes_WmmParamSet_t *pwmm_tlv;
    t_u32 ret_len = 0;

    ENTER();

    /* Null checks */
    if (!ppAssocBuf) {
        LEAVE();
        return 0;
    }
    if (!(*ppAssocBuf)) {
        LEAVE();
        return 0;
    }

    if (!pWmmIE) {
        LEAVE();
        return 0;
    }

    PRINTM(MINFO, "WMM: process assoc req: bss->wmmIe=0x%x\n",
           pWmmIE->vend_hdr.element_id);

    if ((priv->wmm_required
         || (pHTCap && (pHTCap->ieee_hdr.element_id == HT_CAPABILITY)
             && (priv->adapter->config_bands & BAND_GN
                 || priv->adapter->config_bands & BAND_AN))
        )
        && pWmmIE->vend_hdr.element_id == WMM_IE) {
        pwmm_tlv = (MrvlIEtypes_WmmParamSet_t *) * ppAssocBuf;
        pwmm_tlv->header.type = (t_u16) wmm_info_ie[0];
        pwmm_tlv->header.type = wlan_cpu_to_le16(pwmm_tlv->header.type);
        pwmm_tlv->header.len = (t_u16) wmm_info_ie[1];
        memcpy(priv->adapter, pwmm_tlv->wmm_ie, &wmm_info_ie[2],
               pwmm_tlv->header.len);
        if (pWmmIE->qos_info.qos_uapsd)
            memcpy(priv->adapter,
                   (t_u8 *) (pwmm_tlv->wmm_ie + pwmm_tlv->header.len -
                             sizeof(priv->wmm_qosinfo)), &priv->wmm_qosinfo,
                   sizeof(priv->wmm_qosinfo));

        ret_len = sizeof(pwmm_tlv->header) + pwmm_tlv->header.len;
        pwmm_tlv->header.len = wlan_cpu_to_le16(pwmm_tlv->header.len);

        HEXDUMP("ASSOC_CMD: WMM IE", (t_u8 *) pwmm_tlv, ret_len);
        *ppAssocBuf += ret_len;
    }

    LEAVE();
    return ret_len;
}
#endif /* STA_SUPPORT */

/**
 *   @brief Compute the time delay in the driver queues for a given packet.
 *
 *   When the packet is received at the OS/Driver interface, the current
 *     time is set in the packet structure.  The difference between the present
 *     time and that received time is computed in this function and limited
 *     based on pre-compiled limits in the driver.
 *
 *   @param priv   Ptr to the mlan_private driver data struct
 *   @param pmbuf  Ptr to the mlan_buffer which has been previously timestamped
 *
 *   @return  Time delay of the packet in 2ms units after having limit applied
 */
t_u8
wlan_wmm_compute_driver_packet_delay(pmlan_private priv,
                                     const pmlan_buffer pmbuf)
{
    t_u8 ret_val = 0;
    t_u32 out_ts_sec, out_ts_usec, queue_delay;

    ENTER();

    priv->adapter->callbacks.moal_get_system_time(priv->adapter->pmoal_handle,
                                                  &out_ts_sec, &out_ts_usec);

    queue_delay = (out_ts_sec - pmbuf->in_ts_sec) * 1000;
    queue_delay += (out_ts_usec - pmbuf->in_ts_usec) / 1000;

    /* 
     * Queue delay is passed as a uint8 in units of 2ms (ms shifted
     *  by 1). Min value (other than 0) is therefore 2ms, max is 510ms.
     *
     * Pass max value if queue_delay is beyond the uint8 range
     */
    ret_val = (t_u8) (MIN(queue_delay, priv->wmm.drv_pkt_delay_max) >> 1);

    PRINTM(MINFO, "WMM: Pkt Delay: %d ms, %d ms sent to FW\n",
           queue_delay, ret_val);

    LEAVE();
    return ret_val;
}

/**
 *  @brief Transmit the highest priority packet awaiting in the WMM Queues
 *
 *  @param pmadapter Pointer to the mlan_adapter driver data struct
 *
 *  @return        N/A
 */
void
wlan_wmm_process_tx(pmlan_adapter pmadapter)
{
    ENTER();

    do {
        if (wlan_dequeue_tx_packet(pmadapter))
            break;
        /* Check if busy */
    } while (!pmadapter->data_sent && !pmadapter->tx_lock_flag
             && !wlan_wmm_lists_empty(pmadapter));

    LEAVE();
    return;
}

/** 
 *  @brief select wmm queue
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param tid          TID 0-7
 *
 *  @return             wmm_queue priority (0-3)
 */
t_u8
wlan_wmm_select_queue(mlan_private * pmpriv, t_u8 tid)
{
    pmlan_adapter pmadapter = pmpriv->adapter;
    t_u8 i;
    mlan_wmm_ac_e ac_down =
        pmpriv->wmm.
        ac_down_graded_vals[wlan_wmm_convert_tos_to_ac(pmadapter, tid)];

    ENTER();

    for (i = 0; i < 4; i++) {
        if (pmpriv->wmm.queue_priority[i] == ac_down) {
            LEAVE();
            return i;
        }
    }
    LEAVE();
    return 0;
}

#if defined(UAP_SUPPORT)
/**
 *  @brief Delete tx packets in RA list
 *
 *  @param priv			Pointer to the mlan_private driver data struct
 *  @param ra_list_head	ra list header
 *  @param tid          tid
 *  @param tx_pause     tx_pause flag
 *
 *  @return		N/A
 */
static INLINE t_u8
wlan_del_tx_pkts_in_ralist(pmlan_private priv,
                           mlan_list_head * ra_list_head,
                           int tid, t_u8 tx_pause)
{
    raListTbl *ra_list = MNULL;
    pmlan_adapter pmadapter = priv->adapter;
    pmlan_buffer pmbuf = MNULL;
    t_u8 ret = MFALSE;
    ENTER();
    ra_list =
        (raListTbl *) util_peek_list(priv->adapter->pmoal_handle, ra_list_head,
                                     MNULL, MNULL);
    while (ra_list && ra_list != (raListTbl *) ra_list_head) {
        if (ra_list->total_pkts && (ra_list->tx_pause ||
                                    (!tx_pause &&
                                     ra_list->total_pkts > RX_LOW_THRESHOLD))) {
            if ((pmbuf =
                 (pmlan_buffer) util_dequeue_list(pmadapter->pmoal_handle,
                                                  &ra_list->buf_head, MNULL,
                                                  MNULL))) {
                PRINTM(MDATA,
                       "Drop pkts: tid=%d tx_pause=%d pkts=%d brd_pkts=%d %02x:%02x:%02x:%02x:%02x:%02x\n",
                       tid, tx_pause, ra_list->total_pkts,
                       pmadapter->pending_bridge_pkts, ra_list->ra[0],
                       ra_list->ra[1], ra_list->ra[2], ra_list->ra[3],
                       ra_list->ra[4], ra_list->ra[5]);
                wlan_write_data_complete(pmadapter, pmbuf, MLAN_STATUS_FAILURE);
                priv->wmm.pkts_queued[tid]--;
                priv->num_drop_pkts++;
                ra_list->total_pkts--;
                if (!ra_list->tx_pause)
                    util_scalar_decrement(pmadapter->pmoal_handle,
                                          &priv->wmm.tx_pkts_queued, MNULL,
                                          MNULL);
                ret = MTRUE;
                break;
            }
        }
        ra_list = ra_list->pnext;
    }

    LEAVE();
    return ret;
}

/**
 *  @brief Drop tx pkts
 *
 *  @param priv			Pointer to the mlan_private driver data struct
 *
 *  @return		N/A
 */
t_void
wlan_drop_tx_pkts(pmlan_private priv)
{
    int j;
    static int i = 0;
    t_u8 tx_pause = 0;
    pmlan_adapter pmadapter = priv->adapter;
    pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                        priv->wmm.ra_list_spinlock);
    if (wlan_get_tx_pause_station_entry(priv))
        tx_pause = 1;
    for (j = 0; j < MAX_NUM_TID; j++, i++) {
        if (i == MAX_NUM_TID)
            i = 0;
        if (wlan_del_tx_pkts_in_ralist
            (priv, &priv->wmm.tid_tbl_ptr[i].ra_list, i, tx_pause)) {
            i++;
            break;
        }
    }
    pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                          priv->wmm.ra_list_spinlock);
    return;
}

/**
 *  @brief Remove peer ralist
 *
 *  @param priv		  A pointer to mlan_private
 *  @param mac        peer mac address
 *
 *  @return           N/A
 */
t_void
wlan_wmm_delete_peer_ralist(pmlan_private priv, t_u8 * mac)
{
    raListTbl *ra_list;
    int i;
    pmlan_adapter pmadapter = priv->adapter;
    t_u32 pkt_cnt = 0;
    t_u32 tx_pkts_queued = 0;

    ENTER();
    pmadapter->callbacks.moal_spin_lock(pmadapter->pmoal_handle,
                                        priv->wmm.ra_list_spinlock);
    for (i = 0; i < MAX_NUM_TID; ++i) {
        ra_list = wlan_wmm_get_ralist_node(priv, i, mac);
        if (ra_list) {
            PRINTM(MINFO, "delete sta ralist %p\n", ra_list);
            if (!ra_list->tx_pause)
                pkt_cnt += ra_list->total_pkts;
            wlan_wmm_del_pkts_in_ralist_node(priv, ra_list);

            util_unlink_list(pmadapter->pmoal_handle,
                             &priv->wmm.tid_tbl_ptr[i].ra_list,
                             (pmlan_linked_list) ra_list, MNULL, MNULL);
            pmadapter->callbacks.moal_mfree(pmadapter->pmoal_handle,
                                            (t_u8 *) ra_list);
            if (priv->wmm.tid_tbl_ptr[i].ra_list_curr == ra_list)
                priv->wmm.tid_tbl_ptr[i].ra_list_curr =
                    (raListTbl *) & priv->wmm.tid_tbl_ptr[i].ra_list;
        }
    }
    if (pkt_cnt) {
        tx_pkts_queued = util_scalar_read(pmadapter->pmoal_handle,
                                          &priv->wmm.tx_pkts_queued, MNULL,
                                          MNULL);
        tx_pkts_queued -= pkt_cnt;
        util_scalar_write(priv->adapter->pmoal_handle,
                          &priv->wmm.tx_pkts_queued, tx_pkts_queued, MNULL,
                          MNULL);
        util_scalar_write(priv->adapter->pmoal_handle,
                          &priv->wmm.highest_queued_prio, HIGH_PRIO_TID, MNULL,
                          MNULL);
    }
    pmadapter->callbacks.moal_spin_unlock(pmadapter->pmoal_handle,
                                          priv->wmm.ra_list_spinlock);
    LEAVE();
}
#endif

#ifdef STA_SUPPORT

/**
 *  @brief This function prepares the command of ADDTS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf    A pointer to data buffer
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wmm_addts_req(IN pmlan_private pmpriv,
                       OUT HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_wmm_addts *paddts = (mlan_ds_wmm_addts *) pdata_buf;
    HostCmd_DS_WMM_ADDTS_REQ *pcmd_addts = &cmd->params.add_ts;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_WMM_ADDTS_REQ);
    cmd->size = wlan_cpu_to_le16(sizeof(pcmd_addts->dialog_token)
                                 + sizeof(pcmd_addts->timeout_ms)
                                 + sizeof(pcmd_addts->command_result)
                                 + sizeof(pcmd_addts->ieee_status_code)
                                 + paddts->ie_data_len + S_DS_GEN);
    cmd->result = 0;

    pcmd_addts->timeout_ms = wlan_cpu_to_le32(paddts->timeout);
    pcmd_addts->dialog_token = paddts->dialog_tok;
    memcpy(pmpriv->adapter,
           pcmd_addts->tspec_data,
           paddts->ie_data, MIN(WMM_TSPEC_SIZE, paddts->ie_data_len));

    LEAVE();

    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of ADDTS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_addts_req(IN pmlan_private pmpriv,
                       const IN HostCmd_DS_COMMAND * resp,
                       OUT mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_wmm_cfg *pwmm = MNULL;
    mlan_ds_wmm_addts *paddts = MNULL;
    const HostCmd_DS_WMM_ADDTS_REQ *presp_addts = &resp->params.add_ts;

    ENTER();

    if (pioctl_buf) {
        pwmm = (mlan_ds_wmm_cfg *) pioctl_buf->pbuf;
        paddts = (mlan_ds_wmm_addts *) & pwmm->param.addts;
        paddts->result = presp_addts->command_result;
        paddts->dialog_tok = presp_addts->dialog_token;
        paddts->status_code = (t_u32) presp_addts->ieee_status_code;

        if (presp_addts->command_result == MLAN_CMD_RESULT_SUCCESS) {
            /* The tspecData field is potentially variable in size due to extra 
               IEs that may have been in the ADDTS response action frame.
               Calculate the data length from the firmware command response. */
            paddts->ie_data_len
                = (t_u8) (resp->size - sizeof(presp_addts->command_result)
                          - sizeof(presp_addts->timeout_ms)
                          - sizeof(presp_addts->dialog_token)
                          - sizeof(presp_addts->ieee_status_code)
                          - S_DS_GEN);

            /* Copy the TSPEC data include any extra IEs after the TSPEC */
            memcpy(pmpriv->adapter,
                   paddts->ie_data,
                   presp_addts->tspec_data, paddts->ie_data_len);
        } else {
            paddts->ie_data_len = 0;
        }
        PRINTM(MINFO, "TSPEC: ADDTS ret = %d,%d sz=%d\n",
               paddts->result, paddts->status_code, paddts->ie_data_len);

        HEXDUMP("TSPEC: ADDTS data", paddts->ie_data, paddts->ie_data_len);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares the command of DELTS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf    A pointer to data buffer
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wmm_delts_req(IN pmlan_private pmpriv,
                       OUT HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_wmm_delts *pdelts = (mlan_ds_wmm_delts *) pdata_buf;
    HostCmd_DS_WMM_DELTS_REQ *pcmd_delts = &cmd->params.del_ts;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_WMM_DELTS_REQ);
    cmd->size = wlan_cpu_to_le16(sizeof(pcmd_delts->dialog_token)
                                 + sizeof(pcmd_delts->command_result)
                                 + sizeof(pcmd_delts->ieee_reason_code)
                                 + pdelts->ie_data_len + S_DS_GEN);
    cmd->result = 0;
    pcmd_delts->ieee_reason_code = (t_u8) pdelts->status_code;
    memcpy(pmpriv->adapter,
           pcmd_delts->tspec_data,
           pdelts->ie_data, MIN(WMM_TSPEC_SIZE, pdelts->ie_data_len));

    LEAVE();

    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of DELTS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_delts_req(IN pmlan_private pmpriv,
                       const IN HostCmd_DS_COMMAND * resp,
                       OUT mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_wmm_cfg *pwmm;
    IEEEtypes_WMM_TSPEC_t *pTspecIE;
    const HostCmd_DS_WMM_DELTS_REQ *presp_delts = &resp->params.del_ts;

    ENTER();

    if (pioctl_buf) {
        pwmm = (mlan_ds_wmm_cfg *) pioctl_buf->pbuf;
        pwmm->param.delts.result = presp_delts->command_result;

        PRINTM(MINFO, "TSPEC: DELTS result = %d\n",
               presp_delts->command_result);

        if (presp_delts->command_result == 0) {
            pTspecIE = (IEEEtypes_WMM_TSPEC_t *) presp_delts->tspec_data;
            wlan_send_wmmac_host_event(pmpriv,
                                       "DELTS_TX",
                                       MNULL,
                                       pTspecIE->TspecBody.TSInfo.TID,
                                       pTspecIE->TspecBody.TSInfo.UserPri,
                                       presp_delts->ieee_reason_code);

        }
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares the command of WMM_QUEUE_CONFIG
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf    A pointer to data buffer
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wmm_queue_config(IN pmlan_private pmpriv,
                          OUT HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_wmm_queue_config *pqcfg = (mlan_ds_wmm_queue_config *) pdata_buf;
    HostCmd_DS_WMM_QUEUE_CONFIG *pcmd_qcfg = &cmd->params.queue_config;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_WMM_QUEUE_CONFIG);
    cmd->size = wlan_cpu_to_le16(sizeof(pcmd_qcfg->action)
                                 + sizeof(pcmd_qcfg->access_category)
                                 + sizeof(pcmd_qcfg->msdu_lifetime_expiry)
                                 + S_DS_GEN);
    cmd->result = 0;

    pcmd_qcfg->action = pqcfg->action;
    pcmd_qcfg->access_category = pqcfg->access_category;
    pcmd_qcfg->msdu_lifetime_expiry =
        wlan_cpu_to_le16(pqcfg->msdu_lifetime_expiry);

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of WMM_QUEUE_CONFIG
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_queue_config(IN pmlan_private pmpriv,
                          const IN HostCmd_DS_COMMAND * resp,
                          OUT mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_wmm_cfg *pwmm = MNULL;
    const HostCmd_DS_WMM_QUEUE_CONFIG *presp_qcfg = &resp->params.queue_config;

    ENTER();

    if (pioctl_buf) {
        pwmm = (mlan_ds_wmm_cfg *) pioctl_buf->pbuf;
        pwmm->param.q_cfg.action = presp_qcfg->action;
        pwmm->param.q_cfg.access_category = presp_qcfg->access_category;
        pwmm->param.q_cfg.msdu_lifetime_expiry =
            wlan_le16_to_cpu(presp_qcfg->msdu_lifetime_expiry);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares the command of WMM_QUEUE_STATS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf    A pointer to data buffer
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wmm_queue_stats(IN pmlan_private pmpriv,
                         OUT HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_wmm_queue_stats *pqstats = (mlan_ds_wmm_queue_stats *) pdata_buf;
    HostCmd_DS_WMM_QUEUE_STATS *pcmd_qstats = &cmd->params.queue_stats;
    t_u8 id;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_WMM_QUEUE_STATS);
    cmd->size = wlan_cpu_to_le16(sizeof(HostCmd_DS_WMM_QUEUE_STATS)
                                 + S_DS_GEN);
    cmd->result = 0;

    pcmd_qstats->action = pqstats->action;
    pcmd_qstats->select_is_userpri = 1;
    pcmd_qstats->select_bin = pqstats->user_priority;
    pcmd_qstats->pkt_count = wlan_cpu_to_le16(pqstats->pkt_count);
    pcmd_qstats->pkt_loss = wlan_cpu_to_le16(pqstats->pkt_loss);
    pcmd_qstats->avg_queue_delay = wlan_cpu_to_le32(pqstats->avg_queue_delay);
    pcmd_qstats->avg_tx_delay = wlan_cpu_to_le32(pqstats->avg_tx_delay);
    pcmd_qstats->used_time = wlan_cpu_to_le16(pqstats->used_time);
    pcmd_qstats->policed_time = wlan_cpu_to_le16(pqstats->policed_time);
    for (id = 0; id < MLAN_WMM_STATS_PKTS_HIST_BINS; id++) {
        pcmd_qstats->delay_histogram[id] =
            wlan_cpu_to_le16(pqstats->delay_histogram[id]);
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of WMM_QUEUE_STATS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_queue_stats(IN pmlan_private pmpriv,
                         const IN HostCmd_DS_COMMAND * resp,
                         OUT mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_wmm_cfg *pwmm = MNULL;
    mlan_ds_wmm_queue_stats *pqstats = MNULL;
    const HostCmd_DS_WMM_QUEUE_STATS *presp_qstats = &resp->params.queue_stats;
    t_u8 id;

    ENTER();

    if (pioctl_buf) {
        pwmm = (mlan_ds_wmm_cfg *) pioctl_buf->pbuf;
        pqstats = (mlan_ds_wmm_queue_stats *) & pwmm->param.q_stats;

        pqstats->action = presp_qstats->action;
        pqstats->user_priority = presp_qstats->select_bin;
        pqstats->pkt_count = wlan_le16_to_cpu(presp_qstats->pkt_count);
        pqstats->pkt_loss = wlan_le16_to_cpu(presp_qstats->pkt_loss);
        pqstats->avg_queue_delay
            = wlan_le32_to_cpu(presp_qstats->avg_queue_delay);
        pqstats->avg_tx_delay = wlan_le32_to_cpu(presp_qstats->avg_tx_delay);
        pqstats->used_time = wlan_le16_to_cpu(presp_qstats->used_time);
        pqstats->policed_time = wlan_le16_to_cpu(presp_qstats->policed_time);
        for (id = 0; id < MLAN_WMM_STATS_PKTS_HIST_BINS; id++) {
            pqstats->delay_histogram[id]
                = wlan_le16_to_cpu(presp_qstats->delay_histogram[id]);
        }
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function prepares the command of WMM_TS_STATUS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param cmd          A pointer to HostCmd_DS_COMMAND structure
 *  @param pdata_buf    A pointer to data buffer
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_cmd_wmm_ts_status(IN pmlan_private pmpriv,
                       OUT HostCmd_DS_COMMAND * cmd, IN t_void * pdata_buf)
{
    mlan_ds_wmm_ts_status *pts_status = (mlan_ds_wmm_ts_status *) pdata_buf;
    HostCmd_DS_WMM_TS_STATUS *pcmd_ts_status = &cmd->params.ts_status;

    ENTER();

    cmd->command = wlan_cpu_to_le16(HostCmd_CMD_WMM_TS_STATUS);
    cmd->size = wlan_cpu_to_le16(sizeof(HostCmd_DS_WMM_TS_STATUS)
                                 + S_DS_GEN);
    cmd->result = 0;

    memcpy(pmpriv->adapter, (t_void *) pcmd_ts_status, (t_void *) pts_status,
           sizeof(HostCmd_DS_WMM_TS_STATUS));

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}

/**
 *  @brief This function handles the command response of WMM_TS_STATUS
 *
 *  @param pmpriv       A pointer to mlan_private structure
 *  @param resp         A pointer to HostCmd_DS_COMMAND
 *  @param pioctl_buf   A pointer to mlan_ioctl_req structure
 *
 *  @return             MLAN_STATUS_SUCCESS
 */
mlan_status
wlan_ret_wmm_ts_status(IN pmlan_private pmpriv,
                       IN HostCmd_DS_COMMAND * resp,
                       OUT mlan_ioctl_req * pioctl_buf)
{
    mlan_ds_wmm_cfg *pwmm = MNULL;
    HostCmd_DS_WMM_TS_STATUS *presp_ts_status = &resp->params.ts_status;

    ENTER();

    if (pioctl_buf) {
        pwmm = (mlan_ds_wmm_cfg *) pioctl_buf->pbuf;
        presp_ts_status->medium_time
            = wlan_le16_to_cpu(presp_ts_status->medium_time);
        memcpy(pmpriv->adapter,
               (t_void *) & pwmm->param.ts_status,
               (t_void *) presp_ts_status, sizeof(mlan_ds_wmm_ts_status));
    }

    LEAVE();
    return MLAN_STATUS_SUCCESS;
}
#endif /* STA_SUPPORT */