summaryrefslogtreecommitdiff
path: root/wl_cfg_cellavoid.c
blob: e39663570c4e3af6e57b32c34efd0d958c7bb0bb (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
/*
 * Cellular channel avoidance implementation
 *
 * Copyright (C) 2021, Broadcom.
 *
 *      Unless you and Broadcom execute a separate written software license
 * agreement governing use of this software, this software is licensed to you
 * under the terms of the GNU General Public License version 2 (the "GPL"),
 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
 * following added to such license:
 *
 *      As a special exception, the copyright holders of this software give you
 * permission to link this software with independent modules, and to copy and
 * distribute the resulting executable under terms of your choice, provided that
 * you also meet, for each linked independent module, the terms and conditions of
 * the license of that module.  An independent module is a module which is not
 * derived from this software.  The special exception does not apply to any
 * modifications of the software.
 *
 *
 * <<Broadcom-WL-IPTag/Dual:>>
 */

#include <typedefs.h>
#include <linuxver.h>
#include <linux/kernel.h>

#include <bcmutils.h>
#include <bcmstdlib_s.h>
#include <bcmwifi_channels.h>
#include <bcmendian.h>
#include <ethernet.h>
#ifdef WL_WPS_SYNC
#include <eapol.h>
#endif /* WL_WPS_SYNC */
#include <802.11.h>
#include <bcmiov.h>
#include <linux/if_arp.h>
#include <asm/uaccess.h>

#include <ethernet.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/netdevice.h>
#include <linux/sched.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <linux/ieee80211.h>
#include <linux/wait.h>
#include <net/cfg80211.h>
#include <net/rtnetlink.h>

#include <wlioctl.h>
#include <bcmevent.h>
#include <wldev_common.h>
#include <wl_cfg80211.h>
#include <wl_cfgp2p.h>
#include <wl_cfgscan.h>
#include <wl_cfgvif.h>
#include <bcmdevs.h>
#include <bcmdevs_legacy.h>
#include <linux/list_sort.h>
#include <wl_cfgvendor.h>
#include <wl_cfg_cellavoid.h>

#define INVALID_CHSPEC_BW	(0xFFFF)

#define CELLAVOID_DEFAULT_TXCAP	127u
#define CELLAVOID_MAX_CH 128u
#define WL_CELLAVOID_INFORM(args) WL_ERR(args)

#define CELLAVOID_CSA_CNT		50u

#define CSA_DELAYWORK_CSA_INTERVAL	(CELLAVOID_CSA_CNT * 100u)
#define CSA_DELAYWORK_FAIL_INTERVAL	1000u
#define CSA_DELAYWORK_BUSY_INTERVAL	200u
#define CSA_DELAYWORK_FIRST_INTERVAL	0u

#define CSA_MAX_RETRY_CNT		20u

typedef enum cellavoid_ch_state {
	CELLAVOID_STATE_CH_UNSAFE = 0,
	CELLAVOID_STATE_CH_SAFE = 1
} cellavoid_ch_state_t;

typedef struct wl_cellavoid_chan_param {
	int band;
	int center_channel;
	int8 pwr_cap;
	chanspec_bw_t chspec_bw;
	chanspec_band_t chspec_band;
} wl_cellavoid_chan_param_t;

/* struct for channel info list
 * allocated on the country code change and
 * moved to the safe list (availale channel list)
 * If framework gives unsafe channel list,
 * then corresponding channel infos go to the unsafe list(cellular chan list)
 */
typedef struct wl_cellavoid_chan_info {
	struct list_head list;
	chanspec_t chanspec;
	int8 pwr_cap;
} wl_cellavoid_chan_info_t;

typedef struct wl_cellavoid_csa_info {
	struct list_head list;
	struct net_device *ndev;
	chanspec_t chanspec;
} wl_cellavoid_csa_info_t;

typedef struct wl_cellavoid_req_band {
	struct net_device *ndev;
	uint32 req_band;
} wl_cellavoid_req_band_t;

/* This struct is used in cfg vendor function setting function */
typedef struct wl_cellavoid_param {
	wl_cellavoid_chan_param_t *chan_param;
	u8 chan_cnt;
	u32 mandatory;
} wl_cellavoid_param_t;

typedef struct wl_cellavoid_info {
	osl_t *osh;
	struct mutex sync;
	wl_cellavoid_param_t params;
	u32 mandatory_flag;
	u16 cell_chan_info_cnt;
	struct list_head cell_chan_info_list;
	struct list_head avail_chan_info_list;
	wl_cellavoid_req_band_t req_band[MAX_AP_INTERFACE];
	bool csa_progress;
	u32 csa_info_cnt;
	struct list_head csa_info_list;
	u32 csa_reschedule_cnt;
} wl_cellavoid_info_t;

static int wl_cellavoid_verify_avail_chan_list(struct bcm_cfg80211 *cfg,
	wl_cellavoid_info_t *cellavoid_info);
static void wl_cellavoid_clear_cell_chan_list(wl_cellavoid_info_t *cellavoid_info);
static void wl_cellavoid_free_avail_chan_list(wl_cellavoid_info_t *cellavoid_info);
static int wl_cellavoid_alloc_avail_chan_list(struct wiphy *wiphy,
	wl_cellavoid_info_t *cellavoid_info);
static int wl_cellavoid_restore_txpwrcap(struct bcm_cfg80211 *cfg,
	wl_cellavoid_info_t *cellavoid_info);
static void wl_cellavoid_do_csa_work(struct work_struct *work);
static void wl_cellavoid_free_csa_info_list(wl_cellavoid_info_t *cellavoid_info);
static int wl_cellavoid_set_cell_channels(struct bcm_cfg80211 *cfg, wl_cellavoid_param_t *param);

/* Initialize context */
int
wl_cellavoid_init(struct bcm_cfg80211 *cfg)
{
	wl_cellavoid_info_t *cellavoid_info;
	int ret = BCME_OK;

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	cellavoid_info = (wl_cellavoid_info_t *)
		MALLOCZ(cfg->osh, sizeof(*cellavoid_info));
	if (cellavoid_info == NULL) {
		WL_ERR(("failed to create cellavoid_info\n"));
		return BCME_NOMEM;
	}

	cellavoid_info->params.chan_param =
		(wl_cellavoid_chan_param_t *)MALLOCZ(cfg->osh,
		CELLAVOID_MAX_CH * sizeof(*(cellavoid_info->params.chan_param)));
	if (cellavoid_info->params.chan_param == NULL) {
		MFREE(cfg->osh, cellavoid_info, sizeof(*cellavoid_info));
		WL_ERR(("failed to create cellavoid_info params\n"));
		return BCME_NOMEM;
	}

	INIT_LIST_HEAD(&cellavoid_info->cell_chan_info_list);
	INIT_LIST_HEAD(&cellavoid_info->avail_chan_info_list);
	INIT_LIST_HEAD(&cellavoid_info->csa_info_list);

	INIT_DELAYED_WORK(&cfg->csa_delayed_work, wl_cellavoid_do_csa_work);
	mutex_init(&cellavoid_info->sync);

	cellavoid_info->osh = cfg->osh;
	cfg->cellavoid_info = cellavoid_info;

	return ret;
}

/* deinitialize context */
void
wl_cellavoid_deinit(struct bcm_cfg80211 *cfg)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	if (!cellavoid_info) {
		return;
	}

	cancel_delayed_work(&cfg->csa_delayed_work);

	mutex_lock(&cellavoid_info->sync);
	wl_cellavoid_free_csa_info_list(cellavoid_info);
	wl_cellavoid_clear_cell_chan_list(cellavoid_info);
	wl_cellavoid_free_avail_chan_list(cellavoid_info);
	MFREE(cfg->osh, cellavoid_info->params.chan_param,
		CELLAVOID_MAX_CH * sizeof(*(cellavoid_info->params.chan_param)));
	mutex_unlock(&cellavoid_info->sync);
	MFREE(cfg->osh, cellavoid_info, sizeof(*cellavoid_info));

	cellavoid_info = NULL;
}

void
wl_cellavoid_sync_lock(struct bcm_cfg80211 *cfg)
{

	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;

	if (!cellavoid_info) {
		return;
	}

	mutex_lock(&cellavoid_info->sync);
}

void
wl_cellavoid_sync_unlock(struct bcm_cfg80211 *cfg)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;

	if (!cellavoid_info) {
		return;
	}

	mutex_unlock(&cellavoid_info->sync);
}

/* Called in wl_update_wiphybands function
 * 1) If there's item in unsafe channel list (cellular channel list), then
 * move it to safe channel list(available channel list)
 * 2) Then, free all the channel info items
 * 3) Trigger IOVAR to remove txpwrcap in FW (wl_cellavoid_restore_txpwrcap)
 * 4) Allocate new channel items for the new country code
 * 5) Recreate cellular channel list from saved params
 */
int
wl_cellavoid_reinit(struct bcm_cfg80211 *cfg)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;
	int ret = BCME_ERROR;

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	if (!cellavoid_info) {
		return ret;
	}

	cancel_delayed_work_sync(&cfg->csa_delayed_work);

	mutex_lock(&cellavoid_info->sync);

	wl_cellavoid_free_csa_info_list(cellavoid_info);
	wl_cellavoid_clear_cell_chan_list(cellavoid_info);
	wl_cellavoid_free_avail_chan_list(cellavoid_info);

	ret = wl_cellavoid_restore_txpwrcap(cfg, cellavoid_info);
	if (ret != BCME_OK) {
		goto fail;
	}
	ret = wl_cellavoid_alloc_avail_chan_list(bcmcfg_to_wiphy(cfg), cellavoid_info);
	if (ret != BCME_OK) {
		goto fail;
	}

	/* Recreate cellular channel list from saved params */
	ret = wl_cellavoid_set_cell_channels(cfg, &cellavoid_info->params);
	if (ret != BCME_OK) {
		goto fail;
	}

	mutex_unlock(&cellavoid_info->sync);
	return ret;

fail:
	wl_cellavoid_free_avail_chan_list(cellavoid_info);

	mutex_unlock(&cellavoid_info->sync);
	return ret;
}

static void
wl_cellavoid_free_csa_info_list(wl_cellavoid_info_t *cellavoid_info)
{
	wl_cellavoid_csa_info_t *csa_info, *next;

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(csa_info, next, &cellavoid_info->csa_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		list_del(&csa_info->list);
		MFREE(cellavoid_info->osh, csa_info, sizeof(*csa_info));
	}

	cellavoid_info->csa_info_cnt = 0;
	cellavoid_info->csa_progress = FALSE;

}

void
wl_cellavoid_free_csa_info(void *cai, struct net_device *ndev)
{
	wl_cellavoid_csa_info_t *csa_info, *next;
	wl_cellavoid_info_t *cellavoid_info = cai;

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(csa_info, next, &cellavoid_info->csa_info_list, list) {
		GCC_DIAGNOSTIC_POP();
			if (csa_info->ndev == ndev) {
				list_del(&csa_info->list);
				cellavoid_info->csa_info_cnt--;
				if (cellavoid_info->csa_info_cnt == 0) {
					cellavoid_info->csa_progress = FALSE;
					cellavoid_info->csa_reschedule_cnt = 0;
				}
				MFREE(cellavoid_info->osh, csa_info, sizeof(*csa_info));
				break;
		}
	}
}

static int
wl_cellavoid_add_csa_info(wl_cellavoid_info_t *cellavoid_info,
	struct net_device *ndev, chanspec_t chanspec)
{
	wl_cellavoid_csa_info_t *csa_info = NULL;

	/* Allocate csa info */
	csa_info = (wl_cellavoid_csa_info_t *)
		MALLOCZ(cellavoid_info->osh, sizeof(wl_cellavoid_csa_info_t));
	if (!csa_info) {
		WL_ERR(("failed to allocate chan info\n"));
		return -ENOMEM;
	}

	csa_info->ndev = ndev;
	csa_info->chanspec = chanspec;

	list_add_tail(&csa_info->list, &cellavoid_info->csa_info_list);
	cellavoid_info->csa_info_cnt++;

	return BCME_OK;
}

void
wl_cellavoid_set_csa_done(void *cai)
{
	wl_cellavoid_info_t *cellavoid_info = cai;

	if (cellavoid_info == NULL) {
		return;
	}

	mutex_lock(&cellavoid_info->sync);

	if (list_empty(&cellavoid_info->csa_info_list)) {
		cellavoid_info->csa_progress = FALSE;
	}

	mutex_unlock(&cellavoid_info->sync);
}

static void
wl_cellavoid_do_csa_work(struct work_struct *work)
{
	struct bcm_cfg80211 *cfg;
	wl_cellavoid_info_t *cellavoid_info;
	wl_cellavoid_csa_info_t *csa_info = NULL;
	struct delayed_work *dw = to_delayed_work(work);
	wl_chan_switch_t csa_arg;
	uint delay = CSA_DELAYWORK_BUSY_INTERVAL;
	char smbuf[WLC_IOCTL_SMLEN];
	struct net_info *iter, *next;
	bool found = FALSE;
	int err;

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	cfg = container_of(dw, struct bcm_cfg80211, csa_delayed_work);
	GCC_DIAGNOSTIC_POP();

	cellavoid_info = cfg->cellavoid_info;

	mutex_lock(&cellavoid_info->sync);

	if (!list_empty(&cellavoid_info->csa_info_list)) {
		csa_info = list_entry(cellavoid_info->csa_info_list.next,
			wl_cellavoid_csa_info_t, list);

		/* Need to check ndev is still valid */
		GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
		for_each_ndev(cfg, iter, next) {
			GCC_DIAGNOSTIC_POP();
			if ((iter->ndev) &&
				(iter->ndev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP) &&
				wl_get_drv_status(cfg, CONNECTED, iter->ndev) &&
				iter->ndev == csa_info->ndev) {
				found = TRUE;
			}
		}

		if (found == FALSE) {
			wl_cellavoid_free_csa_info(cellavoid_info, csa_info->ndev);
			delay = CSA_DELAYWORK_BUSY_INTERVAL;
			goto exit;
		}

		if (wl_get_drv_status_all(cfg, SCANNING) ||
			wl_get_drv_status_all(cfg, CONNECTING)) {
			WL_INFORM_MEM(("scanning/connecting, "
				"so delay for a while, target chspec %x\n",
				csa_info->chanspec));
			goto reschedule;
		}

		bzero(&csa_arg, sizeof(csa_arg));
		csa_arg.mode = DOT11_CSA_MODE_ADVISORY;
		csa_arg.count = CELLAVOID_CSA_CNT;
		csa_arg.reg = 0;
		csa_arg.chspec = wl_chspec_host_to_driver(csa_info->chanspec);

		/* TBD, ndev valid check, limit on reschedule count */
		WL_INFORM_MEM(("CSA, target chspec %x\n", csa_info->chanspec));

		err = wldev_iovar_setbuf(csa_info->ndev, "csa", &csa_arg, sizeof(csa_arg),
			smbuf, sizeof(smbuf), NULL);
		if (err == BCME_BUSY) {
			WL_INFORM_MEM(("device is busy, so delay for a while, target chspec %x\n",
				csa_info->chanspec));
			goto reschedule;
		} else {
			wl_cellavoid_free_csa_info(cellavoid_info, csa_info->ndev);
			if (err < 0) {
				WL_ERR(("csa failed, target chanspec %x\n", csa_info->chanspec));
				delay = CSA_DELAYWORK_FAIL_INTERVAL;
			} else {
				delay = CSA_DELAYWORK_CSA_INTERVAL;
			}

		}
	}

exit:
	if (list_empty(&cellavoid_info->csa_info_list)) {
		mutex_unlock(&cellavoid_info->sync);
		return;
	}

reschedule:
	if (cellavoid_info->csa_reschedule_cnt < CSA_MAX_RETRY_CNT) {
		cellavoid_info->csa_reschedule_cnt++;
		schedule_delayed_work(&cfg->csa_delayed_work,
			msecs_to_jiffies((const unsigned int)delay));
	} else {
		WL_ERR(("Hit CSA retry limit\n"));
		wl_cellavoid_free_csa_info_list(cellavoid_info);
	}
	mutex_unlock(&cellavoid_info->sync);
}

/* Move channel items from unsafe channel list (cellular channel list) to
 * safe channel list(available channel list)
 */
static void
wl_cellavoid_clear_cell_chan_list(wl_cellavoid_info_t *cellavoid_info)
{
	wl_cellavoid_chan_info_t *chan_info, *next;

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		list_del(&chan_info->list);
		/* Restore channel info to the value of safe channel */
		chan_info->pwr_cap = CELLAVOID_DEFAULT_TXCAP;
		list_add(&chan_info->list, &cellavoid_info->avail_chan_info_list);
	}
	cellavoid_info->cell_chan_info_cnt = 0;
	cellavoid_info->mandatory_flag = 0;
}

/* Free all the channel items in the safe channel list(available channel list)
 * Called on country code change
 */
static void
wl_cellavoid_free_avail_chan_list(wl_cellavoid_info_t *cellavoid_info)
{
	wl_cellavoid_chan_info_t *chan_info, *next;

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		list_del(&chan_info->list);
		MFREE(cellavoid_info->osh, chan_info, sizeof(*chan_info));
	}
}

/* Used when framework sets unsafe channel
 * If the chanspec from the framework matches with items
 * in the safe channel list(available channel list)
 * Then, detach the item from the list, and returns the pointer for the channel item
 */
static wl_cellavoid_chan_info_t *
wl_cellavoid_get_chan_info_from_avail_chan_list(wl_cellavoid_info_t *cellavoid_info,
	chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	wl_cellavoid_chan_info_t *ret = NULL;

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (chan_info->chanspec == chanspec) {
			list_del(&chan_info->list);
			WL_INFORM(("%s: removed in list, chanspec: %x\n",
				__FUNCTION__, chanspec));
			ret = chan_info;
			break;
		}
	}

	return ret;
}

/* Check the chanspec is whether safe or unsafe */
static cellavoid_ch_state_t
wl_cellavoid_get_chan_info(wl_cellavoid_info_t *cellavoid_info, chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t *chan_info, *next;

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (chan_info->chanspec == chanspec) {
			return CELLAVOID_STATE_CH_UNSAFE;
		}
	}

	return CELLAVOID_STATE_CH_SAFE;
}

bool
wl_cellavoid_is_safe(void *cai, chanspec_t chanspec)
{
	wl_cellavoid_info_t *cellavoid_info = cai;

	if (wl_cellavoid_get_chan_info(cellavoid_info, chanspec) == CELLAVOID_STATE_CH_UNSAFE) {
		return FALSE;
	} else {
		return TRUE;
	}
}

bool
wl_cellavoid_mandatory_isset(void *cai, enum nl80211_iftype type)
{
	bool mandatory = FALSE;
	wl_cellavoid_info_t *cellavoid_info = cai;

	switch (type) {
		case NL80211_IFTYPE_P2P_GO:
		case NL80211_IFTYPE_P2P_DEVICE:
			if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_WIFI_DIRECT) {
				mandatory = TRUE;
			}
			break;
		case NL80211_IFTYPE_AP:
			if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_SOFTAP) {
				mandatory = TRUE;
			}
			break;

		default:
			break;
	}
	return mandatory;
}

wifi_interface_mode
wl_cellavoid_mandatory_to_usable_channel_filter(void *cai)
{
	wifi_interface_mode mode = 0;
	wl_cellavoid_info_t *cellavoid_info = cai;

	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_WIFI_DIRECT) {
		mode |= ((1U << WIFI_INTERFACE_P2P_GO) | (1U << WIFI_INTERFACE_P2P_CLIENT));
	}
	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_SOFTAP) {
		mode |= (1U << WIFI_INTERFACE_SOFTAP);
	}
	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_NAN) {
		mode |= (1U << WIFI_INTERFACE_NAN);
	}

	return mode;
}

/* Check the chanspec is whether safe or unsafe */
bool
wl_cellavoid_operation_allowed(void *cai, chanspec_t chanspec,
	enum nl80211_iftype type)
{
	bool allowed = TRUE;
	wl_cellavoid_info_t *cellavoid_info = cai;

	if (wl_cellavoid_get_chan_info(cellavoid_info, chanspec) == CELLAVOID_STATE_CH_UNSAFE &&
		wl_cellavoid_mandatory_isset(cellavoid_info, type)) {
		allowed = FALSE;
	}

	return allowed;
}

/* Used when moving channel item to the unsafe channel list (cellular channel list) */
static void
wl_cellavoid_move_chan_info_to_cell_chan_list(wl_cellavoid_info_t *cellavoid_info,
	wl_cellavoid_chan_info_t * chan_info)
{
	list_add(&chan_info->list, &cellavoid_info->cell_chan_info_list);
	cellavoid_info->cell_chan_info_cnt++;
}

/* Used when moving channel item to the safe channel list (avail channel list) */
static void
wl_cellavoid_move_chan_info_to_avail_chan_list(wl_cellavoid_info_t *cellavoid_info,
	wl_cellavoid_chan_info_t * chan_info)
{
	list_add(&chan_info->list, &cellavoid_info->avail_chan_info_list);
}

/* Used when sorting the channel list
 * wider bw, large channel number comes first and
 * narrower bw, small channel number comes later after sorting
 */
static int
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 70))
wl_cellavoid_chan_info_compare(void *priv, const struct list_head *a, const struct list_head *b)
#else
wl_cellavoid_chan_info_compare(void *priv, struct list_head *a, struct list_head *b)
#endif
{
	uint8 i1_chan, i2_chan;
	uint16 i1_bw, i2_bw;

	wl_cellavoid_chan_info_t *info1 = CONTAINEROF(a, wl_cellavoid_chan_info_t, list);
	wl_cellavoid_chan_info_t *info2 = CONTAINEROF(b, wl_cellavoid_chan_info_t, list);

	i1_chan = wf_chspec_ctlchan(info1->chanspec);
	i2_chan = wf_chspec_ctlchan(info2->chanspec);
	i1_bw = CHSPEC_BW(info1->chanspec);
	i2_bw = CHSPEC_BW(info2->chanspec);

	/* Wider BW comes first */
	if (i1_bw > i2_bw) {
		return -1;
	} else if (i1_bw < i2_bw) {
		return 1;
	} else {
		if (i1_chan > i2_chan) {
			return -1;
		} else if (i1_chan < i2_chan) {
			return 1;
		} else {
			return 0;
		}
	}
}

/* Used when sorting the channel list, sort both unsafe channel list (cellular channel list) and
 * safe channel list (avail channel list)
 */
static void
wl_cellavoid_sort_chan_info_list(wl_cellavoid_info_t *cellavoid_info)
{
	/* Sorting ascending */
	list_sort(NULL, &cellavoid_info->cell_chan_info_list, wl_cellavoid_chan_info_compare);
	list_sort(NULL, &cellavoid_info->avail_chan_info_list, wl_cellavoid_chan_info_compare);
}

#ifdef WL_CELLULAR_CHAN_AVOID_DUMP
/* Dump function, shows chanspec/pwrcap item both in the unsafe channel list (cellular channel list)
 * and safe channel list (avail channel list)
 */
static void
wl_cellavoid_dump_chan_info_list(wl_cellavoid_info_t *cellavoid_info)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	char chanspec_str[CHANSPEC_STR_LEN];

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		wf_chspec_ntoa(chan_info->chanspec, chanspec_str);
		WL_MEM(("Cellular : chanspec %s(%x), pwrcap %d\n",
			chanspec_str, chan_info->chanspec, chan_info->pwr_cap));
	}

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		wf_chspec_ntoa(chan_info->chanspec, chanspec_str);
		WL_MEM(("Avail : chanspec %s(%x), pwrcap %d\n",
			chanspec_str, chan_info->chanspec, chan_info->pwr_cap));
	}

}
#endif /* WL_CELLULAR_CHAN_AVOID_DUMP */

/* Allocate channel item(chanspec + pwrcap), called upon country code change */
static wl_cellavoid_chan_info_t *
wl_cellavoid_alloc_chan_info(wl_cellavoid_info_t *cellavoid_info, chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t *chan_info = NULL;

	/* Allocate availabe channel info */
	chan_info = (wl_cellavoid_chan_info_t *)
		MALLOCZ(cellavoid_info->osh, sizeof(wl_cellavoid_chan_info_t));
	if (!chan_info) {
		WL_ERR(("failed to allocate chan info\n"));
		return NULL;
	}

	chan_info->chanspec = chanspec;
	chan_info->pwr_cap = CELLAVOID_DEFAULT_TXCAP;

	return chan_info;
}

/* Allocate channel item(chanspec + pwrcap) per band (2g or 5g) */
static int
wl_cellavoid_alloc_avail_chan_list_band(wl_cellavoid_info_t *cellavoid_info,
	struct ieee80211_supported_band *sband)
{
	struct ieee80211_channel *channel;
	wl_cellavoid_chan_info_t *chan_info = NULL;
	int i, j;
	uint16 bandwidth[] = {WL_CHANSPEC_BW_40, WL_CHANSPEC_BW_80};
	uint8 ctlchan;
	chanspec_band_t band;
	chanspec_t chanspec = INVCHANSPEC;

	for (i = 0; i < sband->n_channels; i++) {
		channel = &sband->channels[i];
		/* If channel from Kernel wiphy is disabled state or DFS channel, drop */
		if (channel->flags & IEEE80211_CHAN_DISABLED ||
			IS_RADAR_CHAN(channel->flags)) {
			WL_MEM(("chanspec %x is not allowed\n", channel->hw_value));
			continue;
		}

		/* Allocate channel item (chanspec + pwrcap) */
		chan_info = wl_cellavoid_alloc_chan_info(cellavoid_info, channel->hw_value);
		if (chan_info == NULL) {
			goto free_list;
		}

		/* Move allocated channel item(20Mhz)
		 * to the safe channel list (avail channel list)
		 */
		wl_cellavoid_move_chan_info_to_avail_chan_list(cellavoid_info, chan_info);

		if (sband->band == NL80211_BAND_5GHZ) {
			/* If 5ghz, also create channel item for 40/80 chanspec */
			ctlchan = wf_chspec_ctlchan(chan_info->chanspec);
			band = CHSPEC_BAND(chan_info->chanspec);
			ASSERT(band == WL_CHANSPEC_BAND_5G);
			for (j = 0; j < (sizeof(bandwidth) / sizeof(uint16)); j++) {
#ifdef WL_6G_320_SUPPORT
				chanspec = wf_create_chspec_from_primary(ctlchan,
					bandwidth[j], band, 0);
#else
				chanspec = wf_create_chspec_from_primary(ctlchan,
					bandwidth[j], band);
#endif /* WL_6G_320_SUPPORT */
				if (chanspec == INVCHANSPEC) {
					WL_ERR(("invalid chanspec ctlchan %d, band %d "
						"bandwidth %d\n", ctlchan, band, bandwidth[j]));
					goto free_list;
				}
				/* Allocate channel item (chanspec + pwrcap) for 40/80 chanspec */
				chan_info = wl_cellavoid_alloc_chan_info(cellavoid_info, chanspec);
				if (chan_info == NULL) {
					goto free_list;
				}

				/* Add 40/80 chanspec item to the available channel list */
				wl_cellavoid_move_chan_info_to_avail_chan_list(cellavoid_info,
					chan_info);
			}
		}
	}
	return BCME_OK;

free_list:
	/* If there's an error, free all the items in the safe channel list (avail channel list) */
	wl_cellavoid_free_avail_chan_list(cellavoid_info);
	return BCME_NOMEM;
}

/* This function is used verifying channel items
 * created by wl_cellavoid_alloc_avail_chan_list are valid
 * by comparing the channel item to chan_info_list from FW
 * If it does not match with chan_info_list, drop
 * CH165 only support 20MHz BW, so 165/40, 165/80 chanspecs created by
 * wl_cellavoid_alloc_avail_chan_list_band are dropped by this function
 */
static int
wl_cellavoid_verify_avail_chan_list(struct bcm_cfg80211 *cfg, wl_cellavoid_info_t *cellavoid_info)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	u16 list_count;
	void *dngl_chan_list;
	bool legacy_chan_info = FALSE;
	bool found;
	int i, err;
	chanspec_t chanspec = 0;
	char chanspec_str[CHANSPEC_STR_LEN];

	/* Get chan_info_list or chanspec from FW */
#define LOCAL_BUF_LEN 4096
	dngl_chan_list = MALLOCZ(cfg->osh, LOCAL_BUF_LEN);
	if (dngl_chan_list == NULL) {
		WL_ERR(("failed to allocate local buf\n"));
		return BCME_NOMEM;
	}

	err = wldev_iovar_getbuf_bsscfg(bcmcfg_to_prmry_ndev(cfg), "chan_info_list", NULL,
		0, dngl_chan_list, LOCAL_BUF_LEN, 0, &cfg->ioctl_buf_sync);
	if (err == BCME_UNSUPPORTED) {
		WL_INFORM(("get chan_info_list, UNSUPPORTED\n"));
		err = wldev_iovar_getbuf_bsscfg(bcmcfg_to_prmry_ndev(cfg), "chanspecs", NULL,
			0, dngl_chan_list, LOCAL_BUF_LEN, 0, &cfg->ioctl_buf_sync);
		if (err != BCME_OK) {
			WL_ERR(("get chanspecs err(%d)\n", err));
			MFREE(cfg->osh, dngl_chan_list, LOCAL_BUF_LEN);
			return err;
		}
		/* Update indicating legacy chan info usage */
		legacy_chan_info = TRUE;
	} else if (err != BCME_OK) {
		WL_ERR(("get chan_info_list err(%d)\n", err));
		MFREE(cfg->osh, dngl_chan_list, LOCAL_BUF_LEN);
		return err;
	}

	list_count = legacy_chan_info ? ((wl_uint32_list_t *)dngl_chan_list)->count :
		((wl_chanspec_list_v1_t *)dngl_chan_list)->count;

	/* Comparing the channel item to chan_info_list from FW
	 * If the chanspec in channel item is not supported by FW,
	 * delete it from the safe channel list (avail channel list)
	 */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		found = FALSE;
		for (i = 0; i < dtoh32(list_count); i++) {
			if (legacy_chan_info) {
				chanspec = (chanspec_t)
					dtoh32(((wl_uint32_list_t *)dngl_chan_list)->element[i]);
			} else {
				chanspec = (chanspec_t)dtoh32
				(((wl_chanspec_list_v1_t *)dngl_chan_list)->chspecs[i].chanspec);
			}

			if (chan_info->chanspec == chanspec) {
				found = TRUE;
				break;
			}
		}

		if (found == FALSE) {
			list_del(&chan_info->list);
			wf_chspec_ntoa(chan_info->chanspec, chanspec_str);
			WL_INFORM_MEM(("chanspec %s(%x) is removed from avail list\n",
				chanspec_str, chan_info->chanspec));
			MFREE(cfg->osh, chan_info, sizeof(*chan_info));
		}
	}
	MFREE(cfg->osh, dngl_chan_list, LOCAL_BUF_LEN);
#undef LOCAL_BUF_LEN

	return BCME_OK;
}

/* Allocate channel item(chanspec + pwrcap) from both band (2g and 5g)
 * Channel information comes from wiphy in Kernel
 */
static int
wl_cellavoid_alloc_avail_chan_list(struct wiphy *wiphy, wl_cellavoid_info_t *cellavoid_info)
{
	struct ieee80211_supported_band *sband;
	int ret;

	sband = wiphy->bands[IEEE80211_BAND_2GHZ];
	if (!sband || !sband->n_channels) {
		WL_ERR(("No 2ghz channel exists\n"));
		return BCME_ERROR;
	}

	ret = wl_cellavoid_alloc_avail_chan_list_band(cellavoid_info, sband);
	if (ret) {
		return ret;
	}

	sband = wiphy->bands[IEEE80211_BAND_5GHZ];
	if (!sband || !sband->n_channels) {
		WL_ERR(("No 5ghz channel exists\n"));
		return BCME_OK;
	}

	ret = wl_cellavoid_alloc_avail_chan_list_band(cellavoid_info, sband);
	if (ret) {
		return ret;
	}

	/* Verify channel list from the IOVAR chanspecs */
	return wl_cellavoid_verify_avail_chan_list(wiphy_priv(wiphy), cellavoid_info);
}

/* Used to remove existing pwrcap in the FW */
static int
wl_cellavoid_restore_txpwrcap(struct bcm_cfg80211 *cfg, wl_cellavoid_info_t *cellavoid_info)
{
	int ret;
	int hdr_size, payload_size, total_size;
	bcm_iov_buf_t *iov_buf = NULL;
	wl_cell_avoid_ch_info_v1_t *subcmd;

	hdr_size = OFFSETOF(bcm_iov_buf_t, data);
	payload_size = sizeof(*subcmd);
	total_size = hdr_size + payload_size;

	iov_buf = (bcm_iov_buf_t *)MALLOCZ(cfg->osh, total_size);
	if (iov_buf == NULL) {
		WL_ERR(("memory alloc failure size %d\n", total_size));
		return BCME_NOMEM;
	}

	iov_buf->version = htod16(WL_CELL_AVOID_IOV_VERSION_1);
	iov_buf->id = htod16(WL_CELL_AVOID_SUBCMD_CH_INFO);
	iov_buf->len = htod16(payload_size);

	subcmd = (wl_cell_avoid_ch_info_v1_t *)iov_buf->data;
	subcmd->version = htod16(WL_CELL_AVOID_SUB_IOV_VERSION_1);
	subcmd->length = htod16(payload_size);
	/* This will remove cellular channel info in FW and FW will reset txpwrcap */
	subcmd->flags = htod16(WL_CELL_AVOID_REMOVE_CH_INFO);

	ret = wldev_iovar_setbuf(bcmcfg_to_prmry_ndev(cfg), "cellavoid", (char *)iov_buf,
		total_size, cfg->ioctl_buf, WLC_IOCTL_SMLEN, NULL);
	if (ret != BCME_OK) {
		WL_ERR(("fail to restore txpwrcap ret : %d\n", ret));
	}

	MFREE(cfg->osh, iov_buf, total_size);

	return ret;
}

/* Used to deliver chanspec + pwrcap to the FW */
static int
wl_cellavoid_apply_txpwrcap(struct bcm_cfg80211 *cfg, wl_cellavoid_info_t *cellavoid_info)
{
	int ret;
	int hdr_size, payload_size, total_size;
	bcm_iov_buf_t *iov_buf = NULL;
	wl_cell_avoid_ch_info_v1_t *subcmd;
	wl_cellavoid_chan_info_t *chan_info, *next;
	int i = 0;

	/* If there isn't any unsafe channel(cellular channel),
	 * then remove cellular channel + pwrcap info in FW
	 */
	if (cellavoid_info->cell_chan_info_cnt == 0) {
		return wl_cellavoid_restore_txpwrcap(cfg, cellavoid_info);
	}

	hdr_size = OFFSETOF(bcm_iov_buf_t, data);
	payload_size = sizeof(*subcmd) +
		cellavoid_info->cell_chan_info_cnt * sizeof(wl_cell_pwrcap_chanspec_v1_t);
	total_size = hdr_size + payload_size;

	iov_buf = (bcm_iov_buf_t *)MALLOCZ(cfg->osh, total_size);
	if (iov_buf == NULL) {
		WL_ERR(("memory alloc failure size %d\n", total_size));
		return -ENOMEM;
	}

	iov_buf->version = htod16(WL_CELL_AVOID_IOV_VERSION_1);
	iov_buf->id = htod16(WL_CELL_AVOID_SUBCMD_CH_INFO);
	iov_buf->len = htod16(payload_size);

	subcmd = (wl_cell_avoid_ch_info_v1_t *)iov_buf->data;
	subcmd->version = htod16(WL_CELL_AVOID_SUB_IOV_VERSION_1);
	subcmd->length = htod16(payload_size);
	subcmd->flags = htod16(cellavoid_info->mandatory_flag);
	subcmd->cnt =  htod16(cellavoid_info->cell_chan_info_cnt);

	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		subcmd->list[i].chanspec = htod16(chan_info->chanspec);
		subcmd->list[i].pwrcap = chan_info->pwr_cap;
		i++;
	}
	ASSERT(cellavoid_info->cell_chan_info_cnt == i);

	ret = wldev_iovar_setbuf(bcmcfg_to_prmry_ndev(cfg), "cellavoid", (char *)iov_buf,
		total_size, cfg->ioctl_buf, WLC_IOCTL_MEDLEN, NULL);
	if (ret != BCME_OK) {
		WL_ERR(("fail to set txpwrcap ret : %d\n", ret));
	}

	return ret;
}

int
wl_cellavoid_set_requested_freq_bands(struct net_device *ndev,
	void *cai, u32 *pElem_freq, u32 freq_list_len)
{
	int i, j;
	chanspec_t chanspec;
	wl_cellavoid_info_t *cellavoid_info = cai;

	for (i = 0; i < MAX_AP_INTERFACE; i++) {
		if (cellavoid_info->req_band[i].ndev == NULL) {
			break;
		}
	}

	if (i == MAX_AP_INTERFACE) {
		for (i = 0; i < MAX_AP_INTERFACE; i++) {
			WL_ERR(("No empty slot, name %s, req_band %x in slot %d\n",
				cellavoid_info->req_band[i].ndev->name,
				cellavoid_info->req_band[i].req_band, i));
			cellavoid_info->req_band[i].ndev = NULL;
			cellavoid_info->req_band[i].req_band = WLC_BAND_INVALID;
		}

		/* Reset i = 0 */
		i = 0;
	}

	cellavoid_info->req_band[i].ndev = ndev;
	cellavoid_info->req_band[i].req_band = 0;

	for (j = 0; j < freq_list_len; j++) {
		chanspec = wl_freq_to_chanspec(pElem_freq[j]);
		/* mark all the bands found */
		cellavoid_info->req_band[i].req_band |=
			CHSPEC_TO_WLC_BAND(CHSPEC_BAND(chanspec));
	}

	WL_INFORM_MEM(("name %s, req_band %x is in slot %d\n",
		cellavoid_info->req_band[i].ndev->name, cellavoid_info->req_band[i].req_band, i));

	return BCME_OK;
}

void
wl_cellavoid_clear_requested_freq_bands(struct net_device *ndev, void *cai)
{
	int i;
	wl_cellavoid_info_t *cellavoid_info = cai;

	for (i = 0; i < MAX_AP_INTERFACE; i++) {
		if (cellavoid_info->req_band[i].ndev == ndev) {
			break;
		}
	}

	if (i == MAX_AP_INTERFACE) {
		for (i = 0; i < MAX_AP_INTERFACE; i++) {
			WL_ERR(("No empty slot, id %d, name %s, req_band %x\n",
				i, cellavoid_info->req_band[i].ndev->name,
				cellavoid_info->req_band[i].req_band));
		}
		return;
	}

	WL_INFORM_MEM(("name %s, req_band %x in slot %d cleared\n",
		cellavoid_info->req_band[i].ndev->name, cellavoid_info->req_band[i].req_band, i));

	cellavoid_info->req_band[i].ndev = NULL;
	cellavoid_info->req_band[i].req_band = WLC_BAND_INVALID;
}

static int
wl_cellavoid_find_requested_freq_bands(struct net_device *ndev, wl_cellavoid_info_t *cellavoid_info)
{
	int i;

	for (i = 0; i < MAX_AP_INTERFACE; i++) {
		if (cellavoid_info->req_band[i].ndev == ndev) {
			break;
		}
	}

	if (i == MAX_AP_INTERFACE) {
		for (i = 0; i < MAX_AP_INTERFACE; i++) {
			WL_ERR(("can not find valid slot, id %d, name %s, req_band %x\n",
				i, cellavoid_info->req_band[i].ndev->name,
				cellavoid_info->req_band[i].req_band));

		}
		return WLC_BAND_INVALID;
	}

	return cellavoid_info->req_band[i].req_band;
}

static wl_cellavoid_chan_info_t *
wl_cellavoid_find_chinfo_sameband(wl_cellavoid_info_t *cellavoid_info,
	chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	wl_cellavoid_chan_info_t *ret = NULL;

	/* Find channel info with same band in available channel list(safe channel) first */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (CHSPEC_TO_WLC_BAND(chan_info->chanspec) ==
			CHSPEC_TO_WLC_BAND(chanspec)) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in avail list\n", chan_info->chanspec));
			goto exit;
		}
	}

	/* If it's not found and mandatory flag is set return null */
	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_SOFTAP) {
		WL_INFORM_MEM(("No chanspec in avail list and mandatory flag set\n"));
		goto exit;
	}

	/* If it's not found and mandatory flag is zeo,
	 * Find the current chanspec from cellular channel list(unsafe list)
	 * This is to reduce the number of channel switch trial
	 */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (chan_info->chanspec == chanspec) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in cellular list\n",
				chan_info->chanspec));
			goto exit;
		}
	}

exit:
	if (ret == NULL) {
		WL_INFORM_MEM(("No chanspec in avail list/cellular list\n"));
	}

	return ret;
}

static wl_cellavoid_chan_info_t *
wl_cellavoid_find_chinfo_fromchspec(wl_cellavoid_info_t *cellavoid_info,
	chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	wl_cellavoid_chan_info_t *ret = NULL;

	/* Find in available channel list(safe channel) first */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		/* List is always sorted to come wide bw comes first,
		 * so the first one is the widest one
		 */
		if (wf_chspec_ctlchan(chan_info->chanspec) == wf_chspec_ctlchan(chanspec)) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in avail list\n", chan_info->chanspec));
			goto exit;
		}
	}

	/* If it's not found and mandatory flag is set return null */
	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_SOFTAP) {
		WL_INFORM_MEM(("No chanspec in avail list and mandatory flag set\n"));
		goto exit;
	}

	/* If it's not found and mandatory flag is zeo,
	 * pick up the chanspec from cellular channel list(unsafe list)
	 */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		/* List is always sorted to come wide bw comes first,
		 * so the first one is the widest one
		 */
		if (wf_chspec_ctlchan(chan_info->chanspec) == wf_chspec_ctlchan(chanspec)) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in cellular list\n",
				chan_info->chanspec));
			goto exit;
		}
	}

exit:
	if (ret == NULL) {
		wl_cellavoid_chan_info_t *chan_info, *next;
		char chanspec_str[CHANSPEC_STR_LEN];

		GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
		list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list,
				list) {
			GCC_DIAGNOSTIC_POP();
			wf_chspec_ntoa(chan_info->chanspec, chanspec_str);
			WL_INFORM_MEM(("Cellular : chanspec %s(%x), pwrcap %d\n",
				chanspec_str, chan_info->chanspec, chan_info->pwr_cap));
		}

		GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
		list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list,
				list) {
			GCC_DIAGNOSTIC_POP();
			wf_chspec_ntoa(chan_info->chanspec, chanspec_str);
			WL_INFORM_MEM(("Avail : chanspec %s(%x), pwrcap %d\n",
				chanspec_str, chan_info->chanspec, chan_info->pwr_cap));
		}
		WL_INFORM_MEM(("No chanspec in avail list/cellular list\n"));
	}

	return ret;
}

static wl_cellavoid_chan_info_t *
wl_cellavoid_find_chinfo_fromband(wl_cellavoid_info_t *cellavoid_info, int band)
{
	wl_cellavoid_chan_info_t *chan_info, *next;
	wl_cellavoid_chan_info_t *ret = NULL;

	/* Find in available channel list(safe channel) first */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->avail_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (CHSPEC_TO_WLC_BAND(chan_info->chanspec) == band) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in avail list\n", chan_info->chanspec));
			goto exit;
		}
	}

	/* If it's not found and mandatory flag is set return null */
	if (cellavoid_info->mandatory_flag & WL_CELL_AVOID_SOFTAP) {
		WL_INFORM_MEM(("No chanspec in avail list and mandatory flag set\n"));
		goto exit;
	}

	/* If it's not found and mandatory flag is zeo,
	 * pick up the chanspec from cellular channel list(unsafe list)
	 */
	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
	list_for_each_entry_safe(chan_info, next, &cellavoid_info->cell_chan_info_list, list) {
		GCC_DIAGNOSTIC_POP();
		if (CHSPEC_TO_WLC_BAND(chan_info->chanspec) == band) {
			ret = chan_info;
			WL_INFORM_MEM(("chanspec %x found in cellular list\n",
				chan_info->chanspec));
			goto exit;
		}
	}

exit:
	if (ret == NULL) {
		WL_INFORM_MEM(("No chanspec in avail list/cellular list\n"));
	}

	return ret;
}

chanspec_t
wl_cellavoid_find_chspec_fromband(void *cai, int band)
{
	wl_cellavoid_chan_info_t* chan_info;
	chanspec_t chanspec;
	wl_cellavoid_info_t *cellavoid_info = cai;

	chan_info = wl_cellavoid_find_chinfo_fromband(cellavoid_info, band);
	if (chan_info == NULL) {
		chanspec = INVCHANSPEC;
	} else {
		chanspec = chan_info->chanspec;
	}

	return chanspec;
}
chanspec_t
wl_cellavoid_find_widechspec_fromchspec(void *cai, chanspec_t chanspec)
{
	wl_cellavoid_chan_info_t* chan_info;
	chanspec_t wide_chanspec;
	wl_cellavoid_info_t *cellavoid_info = cai;

	chan_info = wl_cellavoid_find_chinfo_fromchspec(cellavoid_info, chanspec);
	if (chan_info == NULL) {
		wide_chanspec = INVCHANSPEC;
	} else {
		wide_chanspec = chan_info->chanspec;
	}

	return wide_chanspec;
}

static wl_cellavoid_chan_info_t *
wl_cellavoid_find_ap_chan_info(struct bcm_cfg80211 *cfg, chanspec_t ap_chspec,
	chanspec_t sta_chspec, int csa_target_band)
{
	int ap_band, sta_band = WLC_BAND_INVALID;
	wl_cellavoid_chan_info_t *chan_info = NULL;

	WL_INFORM_MEM(("AP chspec %x, STA chspec %x, CSA target %x\n",
		ap_chspec, sta_chspec, csa_target_band));

	if (csa_target_band == WLC_BAND_INVALID) {
		return NULL;
	}

	/* This will be checked later */
	if (csa_target_band & WLC_BAND_6G) {
		csa_target_band &= ~WLC_BAND_6G;
	}

	ap_band = CHSPEC_TO_WLC_BAND(ap_chspec);
	if (sta_chspec) {
		sta_band = CHSPEC_TO_WLC_BAND(sta_chspec);
	}

	/* Same band CSA first */
	if (csa_target_band & ap_band) {
		if (sta_band == ap_band) {
			/* SCC in this core */
			WL_INFORM_MEM(("STA in the same core, band %d\n", sta_band));
			chan_info = wl_cellavoid_find_chinfo_fromchspec(cfg->cellavoid_info,
					sta_chspec);
		} else {
			/* No STA in this core */
			WL_INFORM_MEM(("No STA in the same core, band %d\n", ap_band));
			chan_info = wl_cellavoid_find_chinfo_sameband(cfg->cellavoid_info,
				ap_chspec);
		}
		csa_target_band &= ~ap_band;
	}

	/* If there's no target to CSA, try in different core */
	if (chan_info == NULL && csa_target_band != 0) {
		if (csa_target_band == sta_band) {
			/* STA in the another core, so check STA chanspec is available to use
			 * Skip DFS case
			 */
			WL_INFORM_MEM(("STA in the another core. band %d\n", csa_target_band));
			if (!is_chanspec_dfs(cfg, sta_chspec)) {
				chan_info = wl_cellavoid_find_chinfo_fromchspec(cfg->cellavoid_info,
					sta_chspec);
			}
		} else {
			/* No STA in another core */
			WL_INFORM_MEM(("No STA in the another core, band %d\n", csa_target_band));
			chan_info = wl_cellavoid_find_chinfo_fromband(cfg->cellavoid_info,
				csa_target_band);
		}
	}

	if (chan_info) {
		WL_INFORM_MEM(("Found chan info %x\n", chan_info->chanspec));
	}

	return chan_info;
}

/* After making the safe channel/unsafe channel list,
 * perform actions needs to be done (AP->CSA and others)
 * Then, deliver chanspec + pwrcap information to the FW
 * by calling wl_cellavoid_apply_txpwrcap
 */
static int
wl_cellavoid_handle_apsta_concurrency(struct bcm_cfg80211 *cfg)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;
	wl_ap_oper_data_t ap_oper_data = {0};
	cellavoid_ch_state_t ch_state;
	int i, ap_band = WLC_BAND_INVALID;
	int req_band, csa_target_band;
	uint32 sta_cnt;
	chanspec_t sta_chanspec;
	wl_cellavoid_chan_info_t *csa_chan_info = NULL;
	char chanspec_str1[CHANSPEC_STR_LEN], chanspec_str2[CHANSPEC_STR_LEN];
	int ret = BCME_OK;

	sta_cnt = wl_cfgvif_get_iftype_count(cfg, WL_IF_TYPE_STA);
	if (sta_cnt == MAX_STA_INTERFACE) {
		/* This is STA+STA case */
		return BCME_OK;
	}

	/* Check whether AP and STA is already operational */
	wl_get_ap_chanspecs(cfg, &ap_oper_data);
	sta_chanspec = wl_cfg80211_get_sta_chanspec(cfg);

	/* If there's any AP interface */
	if (ap_oper_data.count > 0) {
		for (i = 0; i < ap_oper_data.count; i++) {
			ch_state = wl_cellavoid_get_chan_info(cellavoid_info,
				ap_oper_data.iface[i].chspec);

			/* If AP is on the safe channel, skip this AP */
			if (ch_state == CELLAVOID_STATE_CH_SAFE) {
				continue;
			}

			/* AP channel is unsafe channel(cellular channel) */
			/* Get AP band */
			ap_band = CHSPEC_TO_WLC_BAND(ap_oper_data.iface[i].chspec);
			if (ap_oper_data.count == MAX_AP_INTERFACE) {
				/* 2AP case, CSA in the same wlc */
				WL_INFORM_MEM(("AP/AP, CSA only in the same band, AP chanspec %x\n",
					ap_oper_data.iface[i].chspec));
				csa_target_band = ap_band;
				if (csa_target_band == WLC_BAND_5G) {
					csa_target_band |= WLC_BAND_6G;
				}
			} else {
				/* 1 AP case, AP bsscfg can move to any wlc */
				WL_INFORM_MEM(("AP, CSA to any band, AP chanspec %x\n",
					ap_oper_data.iface[i].chspec));
				csa_target_band = WLC_BAND_2G | WLC_BAND_5G | WLC_BAND_6G;
			}

			/* Take ACS band info and
			 * filtering csa_target_band with requested ACS band
			 */
			req_band =
				wl_cellavoid_find_requested_freq_bands(ap_oper_data.iface[i].ndev,
				cellavoid_info);

			if (req_band != WLC_BAND_INVALID) {
				/* If ACS band info is valid, apply it as the filter */
				WL_INFORM_MEM(("csa_target_band %x, reqband %x\n",
					csa_target_band, req_band));
				csa_target_band &= req_band;
			}

			/* Handle STA concurrency scenario and get chan into to switch channel */
			csa_chan_info = wl_cellavoid_find_ap_chan_info(cfg,
				ap_oper_data.iface[i].chspec, sta_chanspec, csa_target_band);

			/* If channel exists, schedule channel swith for this AP */
			if (csa_chan_info) {
				/* Schedule CSA only when the target chanspec is different
				 * from cur chanspec
				 */
				if (ap_oper_data.iface[i].chspec != csa_chan_info->chanspec) {
					wf_chspec_ntoa(ap_oper_data.iface[i].chspec, chanspec_str1);
					wf_chspec_ntoa(csa_chan_info->chanspec, chanspec_str2);
					WL_INFORM_MEM(("add csa item, chanspec org %s(%x) -> "
						"target %s(%x)\n", chanspec_str1,
						ap_oper_data.iface[i].chspec, chanspec_str2,
						csa_chan_info->chanspec));
					ret = wl_cellavoid_add_csa_info(cellavoid_info,
						ap_oper_data.iface[i].ndev,
						csa_chan_info->chanspec);
					if (ret != BCME_OK) {
						WL_ERR(("add csa info failed\n"));
						break;
					}
				}
			} else {
				WL_INFORM_MEM(("AP %s is not allowed to work, cur chanspec %x\n",
					ap_oper_data.iface[i].ndev->name,
					ap_oper_data.iface[i].chspec));
			}
		}
	}

	if (ret != BCME_OK) {
		wl_cellavoid_free_csa_info_list(cellavoid_info);
	} else {
		if (!list_empty(&cellavoid_info->csa_info_list)) {
			WL_INFORM_MEM(("scheduling csa work, item count %d\n",
				cellavoid_info->csa_info_cnt));
			cellavoid_info->csa_progress = TRUE;
			cellavoid_info->csa_reschedule_cnt = 0;
			schedule_delayed_work(&cfg->csa_delayed_work,
				msecs_to_jiffies((const unsigned int)CSA_DELAYWORK_FIRST_INTERVAL));
		}
	}

	return ret;
}

/* Used by cfg vendor interface to verify the param is correct and update bw/chanspec band */
static int
wl_cellavoid_validate_param(struct bcm_cfg80211 *cfg, wl_cellavoid_param_t *param)
{
	int ret = BCME_OK;
	int i, param_ch;
	chanspec_band_t param_band;
	chanspec_bw_t bw;

	for (i = 0; i < param->chan_cnt; i++) {
		bw = INVALID_CHSPEC_BW;

		/* Not supported DFS band */
		if (wl_cfgscan_is_dfs_set(param->chan_param[i].band)) {
			WL_ERR(("Not supported DFS band\n"));
			ret = -EINVAL;
			goto exit;
		}

		param_ch = param->chan_param[i].center_channel;
		param_band = (param->chan_param[i].band == WIFI_BAND_BG) ?
			WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G;

		if (param_band == WL_CHANSPEC_BAND_2G) {
			if (wf_valid_20MHz_chan(param_ch, param_band)) {
				bw = WL_CHANSPEC_BW_20;
			}
		} else {
			if (wf_valid_40MHz_center_chan(param_ch, param_band)) {
				bw = WL_CHANSPEC_BW_40;
			} else if (wf_valid_80MHz_center_chan(param_ch, param_band)) {
				bw = WL_CHANSPEC_BW_80;
			} else if (wf_valid_160MHz_center_chan(param_ch, param_band)) {
				bw = WL_CHANSPEC_BW_160;
			} else if (wf_valid_20MHz_chan(param_ch, param_band)) {
				bw = WL_CHANSPEC_BW_20;
			}
		}

		if (bw == INVALID_CHSPEC_BW) {
			WL_ERR(("Not supported band %d, channel %d, pwrcap %d\n",
				param->chan_param[i].band, param->chan_param[i].center_channel,
				param->chan_param[i].pwr_cap));
			ret = -EINVAL;
			goto exit;
		}

		param->chan_param[i].chspec_bw = bw;
		param->chan_param[i].chspec_band = param_band;
	}

exit:
	return ret;
}

static int
wl_cellavoid_save_input_params(struct bcm_cfg80211 *cfg, wl_cellavoid_param_t *param)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;

	if (!cellavoid_info || !param) {
		return -EPERM;
	}

	if (cellavoid_info->csa_progress) {
		return -EBUSY;
	}

	/* Backup the input parameter,
	 * this will be used to make safe/unsafe channel list upon the country code change
	 */
	cellavoid_info->params.chan_cnt = param->chan_cnt;
	cellavoid_info->params.mandatory = param->mandatory;

	(void)memcpy_s(cellavoid_info->params.chan_param,
		CELLAVOID_MAX_CH * sizeof(*(cellavoid_info->params.chan_param)),
		param->chan_param, param->chan_cnt * sizeof(*(param->chan_param)));

	return BCME_OK;
}

static int
wl_cellavoid_set_cell_channels(struct bcm_cfg80211 *cfg, wl_cellavoid_param_t *param)
{
	wl_cellavoid_info_t *cellavoid_info = cfg->cellavoid_info;
	int ret = BCME_OK, i, j, cnt;
	int param_ch;
	chanspec_band_t param_band;
	chanspec_bw_t param_bw;
	wl_cellavoid_chan_info_t *chan_info;
	chanspec_t chspecs[WF_NUM_SIDEBANDS_160MHZ];

	WL_INFORM(("%s: Enter\n", __FUNCTION__));
	if (!cellavoid_info || !param) {
		return -EPERM;
	}

	/* CSA triggered by cellular channel is on-going */
	if (cellavoid_info->csa_progress) {
		return -EBUSY;
	}

	/* Clear unsafe channel list, move back unsafe channel list to safe channel list */
	wl_cellavoid_clear_cell_chan_list(cellavoid_info);

	/* Set mandatory flag */
	cellavoid_info->mandatory_flag = param->mandatory;

	/* Check channel params and set unsafe channel list */
	for (i = 0; i < param->chan_cnt; i++) {
		param_ch = param->chan_param[i].center_channel;
		param_band = param->chan_param[i].chspec_band;
		param_bw = param->chan_param[i].chspec_bw;

		if (wf_valid_160MHz_center_chan(param_ch, param_band)) {
			WL_INFORM_MEM(("160MHz channel is not supported ch : %d band %x\n",
				param_ch, param_band));
			continue;
		}

		bzero(chspecs, sizeof(chspecs));

		ret = wl_get_all_sideband_chanspecs(param_ch, param_band, param_bw,
			chspecs, &cnt);
		if (ret != BCME_OK || cnt == 0) {
			WL_ERR(("channel is not supported ch : %d band %d\n",
				param_ch, param_band));
			continue;
		}

		for (j = 0; j < cnt; j++) {
			/* Find chanspecs in the safe channel list(avail channel list)
			 * If the chanspec exists, detach the channel item
			 * from the safe channel list(avail channel list)
			 */
			chan_info = wl_cellavoid_get_chan_info_from_avail_chan_list(cellavoid_info,
				chspecs[j]);
			if (chan_info == NULL) {
				WL_MEM(("no chan info for chanspec %x\n", chspecs[j]));
				continue;
			}

			/* If the chanspec exists, set txpwr cap for this chanspec */
			chan_info->pwr_cap = param->chan_param[i].pwr_cap;

			/* Move this chan info to the unsafe channel list(cellular channel list */
			wl_cellavoid_move_chan_info_to_cell_chan_list(cellavoid_info, chan_info);
		}
	}

	/* Sort the safe/unsafe channel list for dump */
	wl_cellavoid_sort_chan_info_list(cellavoid_info);

#ifdef WL_CELLULAR_CHAN_AVOID_DUMP
	wl_cellavoid_dump_chan_info_list(cellavoid_info);
#endif /* WL_CELLULAR_CHAN_AVOID_DUMP */

	/* Perform actions needs to be done (AP->CSA)
	 */
	ret = wl_cellavoid_handle_apsta_concurrency(cfg);
	if (ret != BCME_OK) {
		WL_ERR(("returned fail %d\n", ret));
		goto fail;
	}

	/* Deliver chanspec + pwrcap information to the FW */
	ret = wl_cellavoid_apply_txpwrcap(cfg, cellavoid_info);

	return ret;

fail:
	wl_cellavoid_clear_cell_chan_list(cellavoid_info);

	return ret;
}

static int
wl_cellavoid_update_param(struct bcm_cfg80211 *cfg, wl_cellavoid_param_t *param)
{
	int err = BCME_OK;

	wl_cellavoid_sync_lock(cfg);

	err = wl_cellavoid_save_input_params(cfg, param);
	if (err == BCME_OK) {
		err = wl_cellavoid_set_cell_channels(cfg, param);
		if (err) {
			WL_INFORM_MEM(("Failed to set cellular channel list, err:%d\n", err));
		}
	} else {
		WL_INFORM_MEM(("Failed to save input params, err:%d\n", err));
	}

	wl_cellavoid_sync_unlock(cfg);

	return err;
}

/* cfg vendor interface function */
int
wl_cfgvendor_cellavoid_set_cell_channels(struct wiphy *wiphy,
		struct wireless_dev *wdev, const void  *data, int len)
{
	int err = BCME_OK, rem, rem1, rem2, type;
	wl_cellavoid_param_t param;
	wl_cellavoid_chan_param_t* cur_chan_param = NULL;
	const struct nlattr *iter, *iter1, *iter2;
	struct bcm_cfg80211 *cfg = wiphy_priv(wiphy);

	BCM_REFERENCE(wdev);

	bzero(&param, sizeof(param));
	if (len <= 0) {
		WL_ERR(("Length of the nlattr is not valid len : %d\n", len));
		err = -EINVAL;
		goto exit;
	}
	nla_for_each_attr(iter, data, len, rem) {
		type = nla_type(iter);
		switch (type) {
		case CELLAVOID_ATTRIBUTE_CNT:
			param.chan_cnt = nla_get_u8(iter);
			if (param.chan_cnt > CELLAVOID_MAX_CH) {
				err = -EINVAL;
				goto exit;
			}
			param.chan_param = (wl_cellavoid_chan_param_t *)MALLOCZ(cfg->osh,
					sizeof(wl_cellavoid_chan_param_t) * param.chan_cnt);
			if (param.chan_param == NULL) {
				WL_ERR(("failed to allocate target param for (%d)\n",
					param.chan_cnt));
				err = -ENOMEM;
				goto exit;
			}

			break;
		case CELLAVOID_ATTRIBUTE_MANDATORY:
			param.mandatory = nla_get_u32(iter);
			break;
		case CELLAVOID_ATTRIBUTE_CONFIG:
			if (param.chan_param == NULL) {
				WL_ERR(("chan_param is NULL (%d)\n", param.chan_cnt));
				err = -ENOMEM;
				goto exit;
			}
			cur_chan_param = param.chan_param;
			nla_for_each_nested(iter1, iter, rem1) {
				if ((uint8 *)cur_chan_param >= ((uint8 *)param.chan_param +
					sizeof(wl_cellavoid_chan_param_t) * param.chan_cnt)) {
					WL_ERR(("increased addr is over its max size\n"));
					err = -EINVAL;
					goto exit;
				}
				nla_for_each_nested(iter2, iter1, rem2) {
					type = nla_type(iter2);
					switch (type) {
						case CELLAVOID_ATTRIBUTE_BAND:
							cur_chan_param->band = nla_get_u32(iter2);
							break;
						case CELLAVOID_ATTRIBUTE_CHANNEL:
							cur_chan_param->center_channel =
								nla_get_u32(iter2);
							break;
						case CELLAVOID_ATTRIBUTE_PWRCAP:
							cur_chan_param->pwr_cap =
								nla_get_u32(iter2);
							break;
					}
				}
				cur_chan_param++;
			}
			break;
		}
	}

	WL_INFORM_MEM(("CELLAVOID PARAM - CNT:%d MANDATORY:%d\n",
		param.chan_cnt, param.mandatory));

	err = wl_cellavoid_validate_param(cfg, &param);
	if (err) {
		err = -EINVAL;
		goto exit;
	}

	err = wl_cellavoid_update_param(cfg, &param);

exit:
	/* free the config param table */
	if (param.chan_param) {
		MFREE(cfg->osh, param.chan_param,
			sizeof(wl_cellavoid_chan_param_t) * param.chan_cnt);
	}
	return err;
}