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
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
 * Copyright (C) 2024-2026 Intel Corporation
 */
#include <linux/crc32.h>

#include "iwl-utils.h"

#include "mld.h"
#include "scan.h"
#include "hcmd.h"
#include "iface.h"
#include "phy.h"
#include "mlo.h"

#include "fw/api/scan.h"
#include "fw/dbg.h"

#define IWL_SCAN_DWELL_ACTIVE 10
#define IWL_SCAN_DWELL_PASSIVE 110
#define IWL_SCAN_NUM_OF_FRAGS 3

/* adaptive dwell max budget time [TU] for full scan */
#define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300

/* adaptive dwell max budget time [TU] for directed scan */
#define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100

/* adaptive dwell default high band APs number */
#define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8

/* adaptive dwell default low band APs number */
#define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2

/* adaptive dwell default APs number for P2P social channels (1, 6, 11) */
#define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10

/* adaptive dwell number of APs override for P2P friendly GO channels */
#define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY 10

/* adaptive dwell number of APs override for P2P social channels */
#define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS 2

/* adaptive dwell number of APs override mask for p2p friendly GO */
#define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT BIT(20)

/* adaptive dwell number of APs override mask for social channels */
#define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT BIT(21)

/* minimal number of 2GHz and 5GHz channels in the regular scan request */
#define IWL_MLD_6GHZ_PASSIVE_SCAN_MIN_CHANS 4

enum iwl_mld_scan_type {
	IWL_SCAN_TYPE_NOT_SET,
	IWL_SCAN_TYPE_UNASSOC,
	IWL_SCAN_TYPE_WILD,
	IWL_SCAN_TYPE_MILD,
	IWL_SCAN_TYPE_FRAGMENTED,
	IWL_SCAN_TYPE_FAST_BALANCE,
};

struct iwl_mld_scan_timing_params {
	u32 suspend_time;
	u32 max_out_time;
};

static const struct iwl_mld_scan_timing_params scan_timing[] = {
	[IWL_SCAN_TYPE_UNASSOC] = {
		.suspend_time = 0,
		.max_out_time = 0,
	},
	[IWL_SCAN_TYPE_WILD] = {
		.suspend_time = 30,
		.max_out_time = 120,
	},
	[IWL_SCAN_TYPE_MILD] = {
		.suspend_time = 120,
		.max_out_time = 120,
	},
	[IWL_SCAN_TYPE_FRAGMENTED] = {
		.suspend_time = 95,
		.max_out_time = 44,
	},
	[IWL_SCAN_TYPE_FAST_BALANCE] = {
		.suspend_time = 30,
		.max_out_time = 37,
	},
};

struct iwl_mld_scan_params {
	enum iwl_mld_scan_type type;
	u32 n_channels;
	u16 delay;
	int n_ssids;
	struct cfg80211_ssid *ssids;
	struct ieee80211_channel **channels;
	u32 flags;
	u8 *mac_addr;
	u8 *mac_addr_mask;
	bool no_cck;
	bool pass_all;
	int n_match_sets;
	struct iwl_scan_probe_req preq;
	struct cfg80211_match_set *match_sets;
	int n_scan_plans;
	struct cfg80211_sched_scan_plan *scan_plans;
	bool iter_notif;
	bool respect_p2p_go;
	u8 fw_link_id;
	struct cfg80211_scan_6ghz_params *scan_6ghz_params;
	u32 n_6ghz_params;
	bool scan_6ghz;
	bool enable_6ghz_passive;
	u8 bssid[ETH_ALEN] __aligned(2);
};

struct iwl_scan_req_params_ptrs {
	struct iwl_scan_general_params_v11 *general_params;
	struct iwl_scan_channel_params_v8 *channel_params;
	struct iwl_scan_periodic_parms_v1 *periodic_params;
	struct iwl_scan_probe_params_v4 *probe_params;
};

struct iwl_mld_scan_respect_p2p_go_iter_data {
	struct ieee80211_vif *current_vif;
	bool p2p_go;
};

static void iwl_mld_scan_respect_p2p_go_iter(void *_data, u8 *mac,
					     struct ieee80211_vif *vif)
{
	struct iwl_mld_scan_respect_p2p_go_iter_data *data = _data;

	/* exclude the given vif */
	if (vif == data->current_vif)
		return;

	/* TODO: CDB check the band of the GO */
	if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO &&
	    iwl_mld_vif_from_mac80211(vif)->ap_ibss_active)
		data->p2p_go = true;
}

static bool iwl_mld_get_respect_p2p_go(struct iwl_mld *mld,
				       struct ieee80211_vif *vif,
				       bool low_latency)
{
	struct iwl_mld_scan_respect_p2p_go_iter_data data = {
		.current_vif = vif,
		.p2p_go = false,
	};

	if (!low_latency)
		return false;

	ieee80211_iterate_active_interfaces_mtx(mld->hw,
						IEEE80211_IFACE_ITER_NORMAL,
						iwl_mld_scan_respect_p2p_go_iter,
						&data);

	return data.p2p_go;
}

struct iwl_mld_scan_iter_data {
	struct ieee80211_vif *current_vif;
	bool active_vif;
	bool is_dcm_with_p2p_go;
	bool global_low_latency;
};

static void iwl_mld_scan_iterator(void *_data, u8 *mac,
				  struct ieee80211_vif *vif)
{
	struct iwl_mld_scan_iter_data *data = _data;
	struct ieee80211_vif *curr_vif = data->current_vif;
	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
	struct iwl_mld_vif *curr_mld_vif;
	unsigned long curr_vif_active_links;
	u16 link_id;

	data->global_low_latency |= iwl_mld_vif_low_latency(mld_vif);

	if ((ieee80211_vif_is_mld(vif) && vif->active_links) ||
	    (vif->type != NL80211_IFTYPE_P2P_DEVICE &&
	     mld_vif->deflink.active))
		data->active_vif = true;

	if (vif == curr_vif)
		return;

	if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_P2P_GO)
		return;

	/* Currently P2P GO can't be AP MLD so the logic below assumes that */
	WARN_ON_ONCE(ieee80211_vif_is_mld(vif));

	curr_vif_active_links =
		ieee80211_vif_is_mld(curr_vif) ? curr_vif->active_links : 1;

	curr_mld_vif = iwl_mld_vif_from_mac80211(curr_vif);

	for_each_set_bit(link_id, &curr_vif_active_links,
			 IEEE80211_MLD_MAX_NUM_LINKS) {
		struct iwl_mld_link *curr_mld_link =
			iwl_mld_link_dereference_check(curr_mld_vif, link_id);

		if (WARN_ON(!curr_mld_link))
			return;

		if (rcu_access_pointer(curr_mld_link->chan_ctx) &&
		    rcu_access_pointer(mld_vif->deflink.chan_ctx) !=
		    rcu_access_pointer(curr_mld_link->chan_ctx)) {
			data->is_dcm_with_p2p_go = true;
			return;
		}
	}
}

static enum
iwl_mld_scan_type iwl_mld_get_scan_type(struct iwl_mld *mld,
					struct ieee80211_vif *vif,
					struct iwl_mld_scan_iter_data *data)
{
	enum iwl_mld_traffic_load load = mld->scan.traffic_load.status;

	/* A scanning AP interface probably wants to generate a survey to do
	 * ACS (automatic channel selection).
	 * Force a non-fragmented scan in that case.
	 */
	if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_AP)
		return IWL_SCAN_TYPE_WILD;

	if (!data->active_vif)
		return IWL_SCAN_TYPE_UNASSOC;

	if ((load == IWL_MLD_TRAFFIC_HIGH || data->global_low_latency) &&
	    vif->type != NL80211_IFTYPE_P2P_DEVICE)
		return IWL_SCAN_TYPE_FRAGMENTED;

	/* While associated to MLD AP with active EMLSR, set all scan
	 * operations as fast-balance scans.
	 */
	if (iwl_mld_emlsr_active(vif))
		return IWL_SCAN_TYPE_FAST_BALANCE;

	/* In case of DCM with P2P GO set all scan requests as
	 * fast-balance scan
	 */
	if (vif->type == NL80211_IFTYPE_STATION &&
	    data->is_dcm_with_p2p_go)
		return IWL_SCAN_TYPE_FAST_BALANCE;

	if (load >= IWL_MLD_TRAFFIC_MEDIUM || data->global_low_latency)
		return IWL_SCAN_TYPE_MILD;

	return IWL_SCAN_TYPE_WILD;
}

static u8 *
iwl_mld_scan_add_2ghz_elems(struct iwl_mld *mld, const u8 *ies,
			    size_t len, u8 *const pos)
{
	static const u8 before_ds_params[] = {
	    WLAN_EID_SSID,
	    WLAN_EID_SUPP_RATES,
	    WLAN_EID_REQUEST,
	    WLAN_EID_EXT_SUPP_RATES,
	};
	size_t offs;
	u8 *newpos = pos;

	offs = ieee80211_ie_split(ies, len,
				  before_ds_params,
				  ARRAY_SIZE(before_ds_params),
				  0);

	memcpy(newpos, ies, offs);
	newpos += offs;

	/* Add a placeholder for DS Parameter Set element */
	*newpos++ = WLAN_EID_DS_PARAMS;
	*newpos++ = 1;
	*newpos++ = 0;

	memcpy(newpos, ies + offs, len - offs);
	newpos += len - offs;

	return newpos;
}

static void
iwl_mld_scan_add_tpc_report_elem(u8 *pos)
{
	pos[0] = WLAN_EID_VENDOR_SPECIFIC;
	pos[1] = WFA_TPC_IE_LEN - 2;
	pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
	pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
	pos[4] = WLAN_OUI_MICROSOFT & 0xff;
	pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
	pos[6] = 0;
	/* pos[7] - tx power will be inserted by the FW */
	pos[7] = 0;
	pos[8] = 0;
}

static u32
iwl_mld_scan_ooc_priority(enum iwl_mld_scan_status scan_status)
{
	if (scan_status == IWL_MLD_SCAN_REGULAR)
		return IWL_SCAN_PRIORITY_EXT_6;
	if (scan_status == IWL_MLD_SCAN_INT_MLO)
		return IWL_SCAN_PRIORITY_EXT_4;

	return IWL_SCAN_PRIORITY_EXT_2;
}

static bool
iwl_mld_scan_is_regular(struct iwl_mld_scan_params *params)
{
	return params->n_scan_plans == 1 &&
		params->scan_plans[0].iterations == 1;
}

static bool
iwl_mld_scan_is_fragmented(enum iwl_mld_scan_type type)
{
	return (type == IWL_SCAN_TYPE_FRAGMENTED ||
		type == IWL_SCAN_TYPE_FAST_BALANCE);
}

static int
iwl_mld_scan_uid_by_status(struct iwl_mld *mld, int status)
{
	for (int i = 0; i < ARRAY_SIZE(mld->scan.uid_status); i++)
		if (mld->scan.uid_status[i] == status)
			return i;

	return -ENOENT;
}

static const char *
iwl_mld_scan_ebs_status_str(enum iwl_scan_ebs_status status)
{
	switch (status) {
	case IWL_SCAN_EBS_SUCCESS:
		return "successful";
	case IWL_SCAN_EBS_INACTIVE:
		return "inactive";
	case IWL_SCAN_EBS_FAILED:
	case IWL_SCAN_EBS_CHAN_NOT_FOUND:
	default:
		return "failed";
	}
}

static int
iwl_mld_scan_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
{
	for (int i = 0; i < PROBE_OPTION_MAX; i++) {
		if (!ssid_list[i].len)
			return -1;
		if (ssid_list[i].len == ssid_len &&
		    !memcmp(ssid_list[i].ssid, ssid, ssid_len))
			return i;
	}

	return -1;
}

static bool
iwl_mld_scan_fits(struct iwl_mld *mld, int n_ssids,
		  struct ieee80211_scan_ies *ies, int n_channels)
{
	return ((n_ssids <= PROBE_OPTION_MAX) &&
		(n_channels <= mld->fw->ucode_capa.n_scan_channels) &&
		(ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
		 ies->len[NL80211_BAND_5GHZ] + ies->len[NL80211_BAND_6GHZ] <=
		 iwl_mld_scan_max_template_size()));
}

static void
iwl_mld_scan_build_probe_req(struct iwl_mld *mld, struct ieee80211_vif *vif,
			     struct ieee80211_scan_ies *ies,
			     struct iwl_mld_scan_params *params)
{
	struct ieee80211_mgmt *frame = (void *)params->preq.buf;
	u8 *pos, *newpos;
	const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
		params->mac_addr : NULL;

	if (mac_addr)
		get_random_mask_addr(frame->sa, mac_addr,
				     params->mac_addr_mask);
	else
		memcpy(frame->sa, vif->addr, ETH_ALEN);

	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
	eth_broadcast_addr(frame->da);
	ether_addr_copy(frame->bssid, params->bssid);
	frame->seq_ctrl = 0;

	pos = frame->u.probe_req.variable;
	*pos++ = WLAN_EID_SSID;
	*pos++ = 0;

	params->preq.mac_header.offset = 0;
	params->preq.mac_header.len = cpu_to_le16(24 + 2);

	/* Insert DS parameter set element on 2.4 GHz band */
	newpos = iwl_mld_scan_add_2ghz_elems(mld,
					     ies->ies[NL80211_BAND_2GHZ],
					     ies->len[NL80211_BAND_2GHZ],
					     pos);
	params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
	params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
	pos = newpos;

	memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
	       ies->len[NL80211_BAND_5GHZ]);
	params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
	params->preq.band_data[1].len =
	    cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
	pos += ies->len[NL80211_BAND_5GHZ];

	memcpy(pos, ies->ies[NL80211_BAND_6GHZ],
	       ies->len[NL80211_BAND_6GHZ]);
	params->preq.band_data[2].offset = cpu_to_le16(pos - params->preq.buf);
	params->preq.band_data[2].len =
		cpu_to_le16(ies->len[NL80211_BAND_6GHZ]);
	pos += ies->len[NL80211_BAND_6GHZ];

	memcpy(pos, ies->common_ies, ies->common_ie_len);
	params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);

	iwl_mld_scan_add_tpc_report_elem(pos + ies->common_ie_len);
	params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
						   WFA_TPC_IE_LEN);
}

static u16
iwl_mld_scan_get_cmd_gen_flags(struct iwl_mld *mld,
			       struct iwl_mld_scan_params *params,
			       struct ieee80211_vif *vif,
			       enum iwl_mld_scan_status scan_status)
{
	u16 flags = 0;

	/* If no direct SSIDs are provided perform a passive scan. Otherwise,
	 * if there is a single SSID which is not the broadcast SSID, assume
	 * that the scan is intended for roaming purposes and thus enable Rx on
	 * all chains to improve chances of hearing the beacons/probe responses.
	 */
	if (params->n_ssids == 0)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE;
	else if (params->n_ssids == 1 && params->ssids[0].ssid_len)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_USE_ALL_RX_CHAINS;

	if (params->pass_all)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PASS_ALL;
	else
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_MATCH;

	if (iwl_mld_scan_is_fragmented(params->type))
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1;

	if (!iwl_mld_scan_is_regular(params))
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PERIODIC;

	if (params->iter_notif ||
	    mld->scan.pass_all_sched_res == SCHED_SCAN_PASS_ALL_STATE_ENABLED)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTFY_ITER_COMPLETE;

	if (scan_status == IWL_MLD_SCAN_SCHED ||
	    scan_status == IWL_MLD_SCAN_NETDETECT)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PREEMPTIVE;

	if (params->flags & (NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP |
			     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE |
			     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_OCE;

	if ((scan_status == IWL_MLD_SCAN_SCHED ||
	     scan_status == IWL_MLD_SCAN_NETDETECT) &&
	    params->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_TRIGGER_UHB_SCAN;

	if (scan_status == IWL_MLD_SCAN_INT_MLO)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTF_START;

	if (params->enable_6ghz_passive)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_6GHZ_PASSIVE_SCAN;

	flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_ADAPTIVE_DWELL;

	return flags;
}

static u8
iwl_mld_scan_get_cmd_gen_flags2(struct iwl_mld *mld,
				struct iwl_mld_scan_params *params,
				struct ieee80211_vif *vif,
				enum iwl_mld_scan_status scan_status,
				u16 gen_flags)
{
	u8 flags = 0;

	/* TODO: CDB */
	if (params->respect_p2p_go)
		flags |= IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_LB |
			IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_HB;

	if (params->scan_6ghz)
		flags |= IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_DONT_TOGGLE_ANT;

	/* For AP interfaces, request survey data for regular scans and if
	 * it is supported. For non-AP interfaces, EBS will be enabled and
	 * the results may be missing information for some channels.
	 */
	if (scan_status == IWL_MLD_SCAN_REGULAR &&
	    ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_AP &&
	    gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE)
		flags |= IWL_UMAC_SCAN_GEN_FLAGS2_COLLECT_CHANNEL_STATS;

	return flags;
}

static void
iwl_mld_scan_cmd_set_dwell(struct iwl_mld *mld,
			   struct iwl_mld_scan_params *params,
			   struct iwl_scan_req_params_ptrs *scan_ptrs)
{
	struct iwl_scan_general_params_v11 *gp = scan_ptrs->general_params;
	const struct iwl_mld_scan_timing_params *timing =
		&scan_timing[params->type];

	gp->adwell_default_social_chn =
	    IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
	gp->adwell_default_2g = IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
	gp->adwell_default_5g = IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;

	if (params->n_ssids && params->ssids[0].ssid_len)
		gp->adwell_max_budget =
			cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
	else
		gp->adwell_max_budget =
			cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);

	gp->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);

	gp->max_out_of_time[SCAN_LB_LMAC_IDX] = cpu_to_le32(timing->max_out_time);
	gp->suspend_time[SCAN_LB_LMAC_IDX] = cpu_to_le32(timing->suspend_time);

	gp->active_dwell[SCAN_LB_LMAC_IDX] = IWL_SCAN_DWELL_ACTIVE;
	gp->passive_dwell[SCAN_LB_LMAC_IDX] = IWL_SCAN_DWELL_PASSIVE;
	gp->active_dwell[SCAN_HB_LMAC_IDX] = IWL_SCAN_DWELL_ACTIVE;
	gp->passive_dwell[SCAN_HB_LMAC_IDX] = IWL_SCAN_DWELL_PASSIVE;

	IWL_DEBUG_SCAN(mld,
		       "Scan: adwell_max_budget=%d max_out_of_time=%d suspend_time=%d\n",
		       gp->adwell_max_budget,
		       gp->max_out_of_time[SCAN_LB_LMAC_IDX],
		       gp->suspend_time[SCAN_LB_LMAC_IDX]);
}

static void
iwl_mld_scan_cmd_set_gen_params(struct iwl_mld *mld,
				struct iwl_mld_scan_params *params,
				struct ieee80211_vif *vif,
				struct iwl_scan_req_params_ptrs *scan_ptrs,
				enum iwl_mld_scan_status scan_status)
{
	struct iwl_scan_general_params_v11 *gp = scan_ptrs->general_params;
	u16 gen_flags = iwl_mld_scan_get_cmd_gen_flags(mld, params, vif,
						       scan_status);
	u8 gen_flags2 = iwl_mld_scan_get_cmd_gen_flags2(mld, params, vif,
							scan_status,
							gen_flags);

	IWL_DEBUG_SCAN(mld, "General: flags=0x%x, flags2=0x%x\n",
		       gen_flags, gen_flags2);

	gp->flags = cpu_to_le16(gen_flags);
	gp->flags2 = gen_flags2;

	iwl_mld_scan_cmd_set_dwell(mld, params, scan_ptrs);

	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
		gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;

	if (params->fw_link_id != IWL_MLD_INVALID_FW_ID)
		gp->scan_start_mac_or_link_id = params->fw_link_id;
}

static int
iwl_mld_scan_cmd_set_sched_params(struct iwl_mld_scan_params *params,
				  struct iwl_scan_req_params_ptrs *scan_ptrs)
{
	struct iwl_scan_umac_schedule *schedule =
		scan_ptrs->periodic_params->schedule;
	__le16 *delay = &scan_ptrs->periodic_params->delay;

	if (WARN_ON(!params->n_scan_plans ||
		    params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
		return -EINVAL;

	for (int i = 0; i < params->n_scan_plans; i++) {
		struct cfg80211_sched_scan_plan *scan_plan =
		    &params->scan_plans[i];

		schedule[i].iter_count = scan_plan->iterations;
		schedule[i].interval =
		    cpu_to_le16(scan_plan->interval);
	}

	/* If the number of iterations of the last scan plan is set to zero,
	 * it should run infinitely. However, this is not always the case.
	 * For example, when regular scan is requested the driver sets one scan
	 * plan with one iteration.
	 */
	if (!schedule[params->n_scan_plans - 1].iter_count)
		schedule[params->n_scan_plans - 1].iter_count = 0xff;

	*delay = cpu_to_le16(params->delay);

	return 0;
}

/* We insert the SSIDs in an inverted order, because the FW will
 * invert it back.
 */
static void
iwl_mld_scan_cmd_build_ssids(struct iwl_mld_scan_params *params,
			     struct iwl_ssid_ie *ssids, u32 *ssid_bitmap)
{
	int i, j;
	int index;
	u32 tmp_bitmap = 0;

	/* copy SSIDs from match list. iwl_config_sched_scan_profiles()
	 * uses the order of these ssids to config match list.
	 */
	for (i = 0, j = params->n_match_sets - 1;
	     j >= 0 && i < PROBE_OPTION_MAX;
	     i++, j--) {
		/* skip empty SSID match_sets */
		if (!params->match_sets[j].ssid.ssid_len)
			continue;

		ssids[i].id = WLAN_EID_SSID;
		ssids[i].len = params->match_sets[j].ssid.ssid_len;
		memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
		       ssids[i].len);
	}

	/* add SSIDs from scan SSID list */
	for (j = params->n_ssids - 1;
	     j >= 0 && i < PROBE_OPTION_MAX;
	     i++, j--) {
		index = iwl_mld_scan_ssid_exist(params->ssids[j].ssid,
						params->ssids[j].ssid_len,
						ssids);
		if (index < 0) {
			ssids[i].id = WLAN_EID_SSID;
			ssids[i].len = params->ssids[j].ssid_len;
			memcpy(ssids[i].ssid, params->ssids[j].ssid,
			       ssids[i].len);
			tmp_bitmap |= BIT(i);
		} else {
			tmp_bitmap |= BIT(index);
		}
	}

	if (ssid_bitmap)
		*ssid_bitmap = tmp_bitmap;
}

static void
iwl_mld_scan_fill_6g_chan_list(struct iwl_mld_scan_params *params,
			       struct iwl_scan_req_params_ptrs *scan_ptrs)
{
	int j, idex_s = 0, idex_b = 0;
	struct cfg80211_scan_6ghz_params *scan_6ghz_params =
		params->scan_6ghz_params;
	struct iwl_scan_probe_params_v4 *pp = scan_ptrs->probe_params;

	for (j = 0;
	     j < params->n_ssids && idex_s < SCAN_SHORT_SSID_MAX_SIZE;
	     j++) {
		if (!params->ssids[j].ssid_len)
			continue;

		pp->short_ssid[idex_s] =
			cpu_to_le32(~crc32_le(~0, params->ssids[j].ssid,
					      params->ssids[j].ssid_len));

		/* hidden 6ghz scan */
		pp->direct_scan[idex_s].id = WLAN_EID_SSID;
		pp->direct_scan[idex_s].len = params->ssids[j].ssid_len;
		memcpy(pp->direct_scan[idex_s].ssid, params->ssids[j].ssid,
		       params->ssids[j].ssid_len);
		idex_s++;
	}

	/* Populate the arrays of the short SSIDs and the BSSIDs using the 6GHz
	 * collocated parameters. This might not be optimal, as this processing
	 * does not (yet) correspond to the actual channels, so it is possible
	 * that some entries would be left out.
	 */
	for (j = 0; j < params->n_6ghz_params; j++) {
		int k;

		/* First, try to place the short SSID */
		if (scan_6ghz_params[j].short_ssid_valid) {
			for (k = 0; k < idex_s; k++) {
				if (pp->short_ssid[k] ==
				    cpu_to_le32(scan_6ghz_params[j].short_ssid))
					break;
			}

			if (k == idex_s && idex_s < SCAN_SHORT_SSID_MAX_SIZE) {
				pp->short_ssid[idex_s++] =
					cpu_to_le32(scan_6ghz_params[j].short_ssid);
			}
		}

		/* try to place BSSID for the same entry */
		for (k = 0; k < idex_b; k++) {
			if (!memcmp(&pp->bssid_array[k],
				    scan_6ghz_params[j].bssid, ETH_ALEN))
				break;
		}

		if (k == idex_b && idex_b < SCAN_BSSID_MAX_SIZE &&
		    !WARN_ONCE(!is_valid_ether_addr(scan_6ghz_params[j].bssid),
			       "scan: invalid BSSID at index %u, index_b=%u\n",
			       j, idex_b)) {
			memcpy(&pp->bssid_array[idex_b++],
			       scan_6ghz_params[j].bssid, ETH_ALEN);
		}
	}

	pp->short_ssid_num = idex_s;
	pp->bssid_num = idex_b;
}

static void
iwl_mld_scan_cmd_set_probe_params(struct iwl_mld_scan_params *params,
				  struct iwl_scan_req_params_ptrs *scan_ptrs,
				  u32 *bitmap_ssid)
{
	struct iwl_scan_probe_params_v4 *pp = scan_ptrs->probe_params;

	pp->preq = params->preq;

	if (params->scan_6ghz) {
		iwl_mld_scan_fill_6g_chan_list(params, scan_ptrs);
		return;
	}

	/* relevant only for 2.4 GHz /5 GHz scan */
	iwl_mld_scan_cmd_build_ssids(params, pp->direct_scan, bitmap_ssid);
}

static bool
iwl_mld_scan_use_ebs(struct iwl_mld *mld, struct ieee80211_vif *vif,
		     bool low_latency)
{
	const struct iwl_ucode_capabilities *capa = &mld->fw->ucode_capa;

	/* We can only use EBS if:
	 *	1. the feature is supported.
	 *	2. the last EBS was successful.
	 *	3. it's not a p2p find operation.
	 *	4. we are not in low latency mode,
	 *	   or if fragmented ebs is supported by the FW
	 *	5. the VIF is not an AP interface (scan wants survey results)
	 */
	return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
		!mld->scan.last_ebs_failed &&
		vif->type != NL80211_IFTYPE_P2P_DEVICE &&
		(!low_latency || fw_has_api(capa, IWL_UCODE_TLV_API_FRAG_EBS)) &&
		ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_AP);
}

static u8
iwl_mld_scan_cmd_set_chan_flags(struct iwl_mld *mld,
				struct iwl_mld_scan_params *params,
				struct ieee80211_vif *vif,
				bool low_latency)
{
	u8 flags = 0;

	flags |= IWL_SCAN_CHANNEL_FLAG_ENABLE_CHAN_ORDER;

	if (iwl_mld_scan_use_ebs(mld, vif, low_latency))
		flags |= IWL_SCAN_CHANNEL_FLAG_EBS |
			 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
			 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;

	/* set fragmented ebs for fragmented scan */
	if (iwl_mld_scan_is_fragmented(params->type))
		flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;

	/* Force EBS in case the scan is a fragmented and there is a need
	 * to take P2P GO operation into consideration during scan operation.
	 */
	/* TODO: CDB */
	if (iwl_mld_scan_is_fragmented(params->type) &&
	    params->respect_p2p_go) {
		IWL_DEBUG_SCAN(mld, "Respect P2P GO. Force EBS\n");
		flags |= IWL_SCAN_CHANNEL_FLAG_FORCE_EBS;
	}

	return flags;
}

static const u8 p2p_go_friendly_chs[] = {
	36, 40, 44, 48, 149, 153, 157, 161, 165,
};

static const u8 social_chs[] = {
	1, 6, 11
};

static u32 iwl_mld_scan_ch_n_aps_flag(enum nl80211_iftype vif_type, u8 ch_id)
{
	if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
		return 0;

	for (int i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
		if (ch_id == p2p_go_friendly_chs[i])
			return IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT;
	}

	for (int i = 0; i < ARRAY_SIZE(social_chs); i++) {
		if (ch_id == social_chs[i])
			return IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT;
	}

	return 0;
}

static void
iwl_mld_scan_cmd_set_channels(struct iwl_mld *mld,
			      struct ieee80211_channel **channels,
			      struct iwl_scan_req_params_ptrs *scan_ptrs,
			      int n_channels, u32 flags,
			      enum nl80211_iftype vif_type)
{
	struct iwl_scan_channel_params_v8 *cp = scan_ptrs->channel_params;

	for (int i = 0; i < n_channels; i++) {
		enum nl80211_band band = channels[i]->band;
		struct iwl_scan_channel_cfg_umac *cfg = &cp->channel_config[i];
		u8 iwl_band = iwl_mld_nl80211_band_to_fw(band);
		u32 n_aps_flag =
			iwl_mld_scan_ch_n_aps_flag(vif_type,
						   channels[i]->hw_value);

		if (IWL_MLD_ADAPTIVE_DWELL_NUM_APS_OVERRIDE)
			n_aps_flag = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT;

		cfg->flags = cpu_to_le32(flags | n_aps_flag);
		cfg->channel_num = channels[i]->hw_value;
		if (cfg80211_channel_is_psc(channels[i]))
			cfg->flags = 0;

		if (band == NL80211_BAND_6GHZ) {
			/* 6 GHz channels should only appear in a scan request
			 * that has scan_6ghz set. The only exception is MLO
			 * scan, which has to be passive.
			 */
			WARN_ON_ONCE(cfg->flags != 0);
			cfg->flags =
				cpu_to_le32(IWL_UHB_CHAN_CFG_FLAG_FORCE_PASSIVE);
		}

		cfg->v2.iter_count = 1;
		cfg->v2.iter_interval = 0;
		cfg->flags |= cpu_to_le32(iwl_band <<
					  IWL_CHAN_CFG_FLAGS_BAND_POS);
	}
}

static u8
iwl_mld_scan_cfg_channels_6g(struct iwl_mld *mld,
			     struct iwl_mld_scan_params *params,
			     u32 n_channels,
			     struct iwl_scan_req_params_ptrs *scan_ptrs,
			     enum nl80211_iftype vif_type)
{
	struct iwl_scan_probe_params_v4 *pp = scan_ptrs->probe_params;
	struct iwl_scan_channel_params_v8 *cp = scan_ptrs->channel_params;
	struct cfg80211_scan_6ghz_params *scan_6ghz_params =
		params->scan_6ghz_params;
	u32 i;
	u8 ch_cnt;

	for (i = 0, ch_cnt = 0; i < params->n_channels; i++) {
		struct iwl_scan_channel_cfg_umac *cfg =
			&cp->channel_config[ch_cnt];

		u32 s_ssid_bitmap = 0, bssid_bitmap = 0, flags = 0;
		u8 k, n_s_ssids = 0, n_bssids = 0;
		u8 max_s_ssids, max_bssids;
		bool force_passive = false, found = false, allow_passive = true,
		     unsolicited_probe_on_chan = false, psc_no_listen = false;
		s8 psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;

		/* Avoid performing passive scan on non PSC channels unless the
		 * scan is specifically a passive scan, i.e., no SSIDs
		 * configured in the scan command.
		 */
		if (!cfg80211_channel_is_psc(params->channels[i]) &&
		    !params->n_6ghz_params && params->n_ssids)
			continue;

		cfg->channel_num = params->channels[i]->hw_value;
		cfg->flags |=
			cpu_to_le32(PHY_BAND_6 << IWL_CHAN_CFG_FLAGS_BAND_POS);

		cfg->v5.iter_count = 1;
		cfg->v5.iter_interval = 0;

		for (u32 j = 0; j < params->n_6ghz_params; j++) {
			s8 tmp_psd_20;

			if (!(scan_6ghz_params[j].channel_idx == i))
				continue;

			unsolicited_probe_on_chan |=
				scan_6ghz_params[j].unsolicited_probe;

			/* Use the highest PSD value allowed as advertised by
			 * APs for this channel
			 */
			tmp_psd_20 = scan_6ghz_params[j].psd_20;
			if (tmp_psd_20 !=
			    IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED &&
			    (psd_20 ==
			     IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED ||
			     psd_20 < tmp_psd_20))
				psd_20 = tmp_psd_20;

			psc_no_listen |= scan_6ghz_params[j].psc_no_listen;
		}

		/* In the following cases apply passive scan:
		 * 1. Non fragmented scan:
		 *	- PSC channel with NO_LISTEN_FLAG on should be treated
		 *	  like non PSC channel
		 *	- Non PSC channel with more than 3 short SSIDs or more
		 *	  than 9 BSSIDs.
		 *	- Non PSC Channel with unsolicited probe response and
		 *	  more than 2 short SSIDs or more than 6 BSSIDs.
		 *	- PSC channel with more than 2 short SSIDs or more than
		 *	  6 BSSIDs.
		 * 2. Fragmented scan:
		 *	- PSC channel with more than 1 SSID or 3 BSSIDs.
		 *	- Non PSC channel with more than 2 SSIDs or 6 BSSIDs.
		 *	- Non PSC channel with unsolicited probe response and
		 *	  more than 1 SSID or more than 3 BSSIDs.
		 */
		if (!iwl_mld_scan_is_fragmented(params->type)) {
			if (!cfg80211_channel_is_psc(params->channels[i]) ||
			    psc_no_listen) {
				if (unsolicited_probe_on_chan) {
					max_s_ssids = 2;
					max_bssids = 6;
				} else {
					max_s_ssids = 3;
					max_bssids = 9;
				}
			} else {
				max_s_ssids = 2;
				max_bssids = 6;
			}
		} else if (cfg80211_channel_is_psc(params->channels[i])) {
			max_s_ssids = 1;
			max_bssids = 3;
		} else {
			if (unsolicited_probe_on_chan) {
				max_s_ssids = 1;
				max_bssids = 3;
			} else {
				max_s_ssids = 2;
				max_bssids = 6;
			}
		}

		/* To optimize the scan time, i.e., reduce the scan dwell time
		 * on each channel, the below logic tries to set 3 direct BSSID
		 * probe requests for each broadcast probe request with a short
		 * SSID.
		 */
		for (u32 j = 0; j < params->n_6ghz_params; j++) {
			if (!(scan_6ghz_params[j].channel_idx == i))
				continue;

			found = false;

			for (k = 0;
			     k < pp->short_ssid_num && n_s_ssids < max_s_ssids;
			     k++) {
				if (!scan_6ghz_params[j].unsolicited_probe &&
				    le32_to_cpu(pp->short_ssid[k]) ==
				    scan_6ghz_params[j].short_ssid) {
					/* Relevant short SSID bit set */
					if (s_ssid_bitmap & BIT(k)) {
						found = true;
						break;
					}

					/* Prefer creating BSSID entries unless
					 * the short SSID probe can be done in
					 * the same channel dwell iteration.
					 *
					 * We also need to create a short SSID
					 * entry for any hidden AP.
					 */
					if (3 * n_s_ssids > n_bssids &&
					    !pp->direct_scan[k].len)
						break;

					/* Hidden AP, cannot do passive scan */
					if (pp->direct_scan[k].len)
						allow_passive = false;

					s_ssid_bitmap |= BIT(k);
					n_s_ssids++;
					found = true;
					break;
				}
			}

			if (found)
				continue;

			for (k = 0; k < pp->bssid_num; k++) {
				if (memcmp(&pp->bssid_array[k],
					   scan_6ghz_params[j].bssid,
					   ETH_ALEN))
					continue;

				if (bssid_bitmap & BIT(k))
					break;

				if (n_bssids < max_bssids) {
					bssid_bitmap |= BIT(k);
					n_bssids++;
				} else {
					force_passive = TRUE;
				}

				break;
			}
		}

		if (cfg80211_channel_is_psc(params->channels[i]) &&
		    psc_no_listen)
			flags |= IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN;

		if (unsolicited_probe_on_chan)
			flags |= IWL_UHB_CHAN_CFG_FLAG_UNSOLICITED_PROBE_RES;

		if ((allow_passive && force_passive) ||
		    (!(bssid_bitmap | s_ssid_bitmap) &&
		     !cfg80211_channel_is_psc(params->channels[i])))
			flags |= IWL_UHB_CHAN_CFG_FLAG_FORCE_PASSIVE;
		else
			flags |= bssid_bitmap | (s_ssid_bitmap << 16);

		cfg->flags |= cpu_to_le32(flags);
		cfg->v5.psd_20 = psd_20;

		ch_cnt++;
	}

	if (params->n_channels > ch_cnt)
		IWL_DEBUG_SCAN(mld,
			       "6GHz: reducing number channels: (%u->%u)\n",
			       params->n_channels, ch_cnt);

	return ch_cnt;
}

static int
iwl_mld_scan_cmd_set_6ghz_chan_params(struct iwl_mld *mld,
				      struct iwl_mld_scan_params *params,
				      struct ieee80211_vif *vif,
				      struct iwl_scan_req_params_ptrs *scan_ptrs)
{
	struct iwl_scan_channel_params_v8 *cp = scan_ptrs->channel_params;

	/* Explicitly clear the flags since most of them are not
	 * relevant for 6 GHz scan.
	 */
	cp->flags = 0;
	cp->count = iwl_mld_scan_cfg_channels_6g(mld, params,
						 params->n_channels,
						 scan_ptrs, vif->type);
	if (!cp->count)
		return -EINVAL;

	if (!params->n_ssids ||
	    (params->n_ssids == 1 && !params->ssids[0].ssid_len))
		cp->flags |= IWL_SCAN_CHANNEL_FLAG_6G_PSC_NO_FILTER;

	return 0;
}

static int
iwl_mld_scan_cmd_set_chan_params(struct iwl_mld *mld,
				 struct iwl_mld_scan_params *params,
				 struct ieee80211_vif *vif,
				 struct iwl_scan_req_params_ptrs *scan_ptrs,
				 bool low_latency,
				 enum iwl_mld_scan_status scan_status,
				 u32 channel_cfg_flags)
{
	struct iwl_scan_channel_params_v8 *cp = scan_ptrs->channel_params;
	struct ieee80211_supported_band *sband =
		&mld->nvm_data->bands[NL80211_BAND_6GHZ];

	cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
	cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;

	if (IWL_MLD_ADAPTIVE_DWELL_NUM_APS_OVERRIDE)
		cp->n_aps_override[0] = IWL_MLD_ADAPTIVE_DWELL_NUM_APS_OVERRIDE;

	if (params->scan_6ghz)
		return iwl_mld_scan_cmd_set_6ghz_chan_params(mld, params,
							     vif, scan_ptrs);

	/* relevant only for 2.4 GHz/5 GHz scan */
	cp->flags = iwl_mld_scan_cmd_set_chan_flags(mld, params, vif,
						    low_latency);
	cp->count = params->n_channels;

	iwl_mld_scan_cmd_set_channels(mld, params->channels, scan_ptrs,
				      params->n_channels, channel_cfg_flags,
				      vif->type);

	if (!params->enable_6ghz_passive)
		return 0;

	/* fill 6 GHz passive scan cfg */
	for (int i = 0; i < sband->n_channels; i++) {
		struct ieee80211_channel *channel =
			&sband->channels[i];
		struct iwl_scan_channel_cfg_umac *cfg =
			&cp->channel_config[cp->count];

		if (!cfg80211_channel_is_psc(channel))
			continue;

		cfg->channel_num = channel->hw_value;
		cfg->v5.iter_count = 1;
		cfg->v5.iter_interval = 0;
		cfg->v5.psd_20 =
			IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
		cfg->flags = cpu_to_le32(PHY_BAND_6 <<
					 IWL_CHAN_CFG_FLAGS_BAND_POS);
		cp->count++;
	}

	return 0;
}

struct iwl_scan_umac_handler {
	u8 version;
	int (*handler)(struct iwl_mld *mld, struct ieee80211_vif *vif,
		       struct iwl_mld_scan_params *params,
		       enum iwl_mld_scan_status scan_status,
		       int uid, u32 ooc_priority, bool low_latency);
};

#define IWL_SCAN_UMAC_HANDLER(_ver) {		\
	.version = _ver,			\
	.handler = iwl_mld_scan_umac_v##_ver,	\
}

static int iwl_mld_scan_umac_common(struct iwl_mld *mld,
				    struct ieee80211_vif *vif,
				    struct iwl_mld_scan_params *params,
				    struct iwl_scan_req_params_ptrs *scan_ptrs,
				    enum iwl_mld_scan_status scan_status,
				    bool low_latency)
{
	u32 bitmap_ssid = 0;
	int ret;

	iwl_mld_scan_cmd_set_gen_params(mld, params, vif, scan_ptrs,
					scan_status);

	ret = iwl_mld_scan_cmd_set_sched_params(params, scan_ptrs);
	if (ret)
		return ret;

	iwl_mld_scan_cmd_set_probe_params(params, scan_ptrs, &bitmap_ssid);

	return iwl_mld_scan_cmd_set_chan_params(mld, params, vif, scan_ptrs,
						low_latency, scan_status,
						bitmap_ssid);
}

static int iwl_mld_scan_umac_v18(struct iwl_mld *mld, struct ieee80211_vif *vif,
				 struct iwl_mld_scan_params *params,
				 enum iwl_mld_scan_status scan_status,
				 int uid, u32 ooc_priority, bool low_latency)
{
	struct iwl_scan_req_umac_v18 *cmd = mld->scan.cmd;
	struct iwl_scan_req_params_ptrs scan_ptrs = {
		.general_params = &cmd->scan_params.general_params,
		.probe_params = &cmd->scan_params.probe_params,
		.channel_params = &cmd->scan_params.channel_params,
		.periodic_params = &cmd->scan_params.periodic_params
	};
	int ret;

	if (WARN_ON(params->n_channels > SCAN_MAX_NUM_CHANS_V4))
		return -EINVAL;

	cmd->uid = cpu_to_le32(uid);
	cmd->ooc_priority = cpu_to_le32(ooc_priority);

	ret = iwl_mld_scan_umac_common(mld, vif, params, &scan_ptrs,
				       scan_status, low_latency);
	if (ret)
		return ret;

	return uid;
}

static int iwl_mld_scan_umac_v17(struct iwl_mld *mld, struct ieee80211_vif *vif,
				 struct iwl_mld_scan_params *params,
				 enum iwl_mld_scan_status scan_status,
				 int uid, u32 ooc_priority, bool low_latency)
{
	struct iwl_scan_req_umac_v17 *cmd = mld->scan.cmd;
	struct iwl_scan_req_params_ptrs scan_ptrs = {
		.general_params = &cmd->scan_params.general_params,
		.probe_params = &cmd->scan_params.probe_params,

		/* struct iwl_scan_channel_params_v8 and struct
		 * iwl_scan_channel_params_v7 are almost identical. The only
		 * difference is that the newer version allows configuration of
		 * more channels. So casting here is ok as long as we ensure
		 * that we don't exceed the max number of channels supported by
		 * the older version (see the WARN_ON below).
		 */
		.channel_params = (struct iwl_scan_channel_params_v8 *)
			&cmd->scan_params.channel_params,
		.periodic_params = &cmd->scan_params.periodic_params
	};
	int ret;

	if (WARN_ON(params->n_channels > SCAN_MAX_NUM_CHANS_V3))
		return -EINVAL;

	cmd->uid = cpu_to_le32(uid);
	cmd->ooc_priority = cpu_to_le32(ooc_priority);

	ret = iwl_mld_scan_umac_common(mld, vif, params, &scan_ptrs,
				       scan_status, low_latency);
	if (ret)
		return ret;

	return uid;
}

static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
	/* set the newest version first to shorten the list traverse time */
	IWL_SCAN_UMAC_HANDLER(18),
	IWL_SCAN_UMAC_HANDLER(17),
};

static int
iwl_mld_scan_build_cmd(struct iwl_mld *mld, struct ieee80211_vif *vif,
		       struct iwl_mld_scan_params *params,
		       enum iwl_mld_scan_status scan_status,
		       bool low_latency)
{
	int uid, err;
	u32 ooc_priority;

	memset(mld->scan.cmd, 0, mld->scan.cmd_size);
	uid = iwl_mld_scan_uid_by_status(mld, IWL_MLD_SCAN_NONE);
	if (uid < 0)
		return uid;

	ooc_priority = iwl_mld_scan_ooc_priority(scan_status);

	for (size_t i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
		const struct iwl_scan_umac_handler *ver_handler =
			&iwl_scan_umac_handlers[i];

		if (ver_handler->version != mld->scan.cmd_ver)
			continue;

		err = ver_handler->handler(mld, vif, params, scan_status,
					   uid, ooc_priority, low_latency);
		return err ? : uid;
	}

	IWL_ERR(mld, "No handler for UMAC scan cmd version %d\n",
		mld->scan.cmd_ver);

	return -EINVAL;
}

static bool
iwl_mld_scan_pass_all(struct iwl_mld *mld,
		      struct cfg80211_sched_scan_request *req)
{
	if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
		IWL_DEBUG_SCAN(mld,
			       "Sending scheduled scan with filtering, n_match_sets %d\n",
			       req->n_match_sets);
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_DISABLED;
		return false;
	}

	IWL_DEBUG_SCAN(mld, "Sending Scheduled scan without filtering\n");
	mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_ENABLED;

	return true;
}

static int
iwl_mld_config_sched_scan_profiles(struct iwl_mld *mld,
				   struct cfg80211_sched_scan_request *req)
{
	struct iwl_host_cmd hcmd = {
		.id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
	};
	struct iwl_scan_offload_profile *profile;
	struct iwl_scan_offload_profile_cfg_data *cfg_data;
	struct iwl_scan_offload_profile_cfg *profile_cfg;
	struct iwl_scan_offload_blocklist *blocklist;
	u32 blocklist_size = IWL_SCAN_MAX_BLACKLIST_LEN * sizeof(*blocklist);
	u32 cmd_size = blocklist_size + sizeof(*profile_cfg);
	u8 *cmd;
	int ret;

	if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES_V2))
		return -EIO;

	cmd = kzalloc(cmd_size, GFP_KERNEL);
	if (!cmd)
		return -ENOMEM;

	hcmd.data[0] = cmd;
	hcmd.len[0] = cmd_size;

	blocklist = (struct iwl_scan_offload_blocklist *)cmd;
	profile_cfg = (struct iwl_scan_offload_profile_cfg *)(cmd + blocklist_size);

	/* No blocklist configuration */
	cfg_data = &profile_cfg->data;
	cfg_data->num_profiles = req->n_match_sets;
	cfg_data->active_clients = SCAN_CLIENT_SCHED_SCAN;
	cfg_data->pass_match = SCAN_CLIENT_SCHED_SCAN;
	cfg_data->match_notify = SCAN_CLIENT_SCHED_SCAN;

	if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
		cfg_data->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;

	for (int i = 0; i < req->n_match_sets; i++) {
		profile = &profile_cfg->profiles[i];

		/* Support any cipher and auth algorithm */
		profile->unicast_cipher = 0xff;
		profile->auth_alg = IWL_AUTH_ALGO_UNSUPPORTED |
			IWL_AUTH_ALGO_NONE | IWL_AUTH_ALGO_PSK |
			IWL_AUTH_ALGO_8021X | IWL_AUTH_ALGO_SAE |
			IWL_AUTH_ALGO_8021X_SHA384 | IWL_AUTH_ALGO_OWE;
		profile->network_type = IWL_NETWORK_TYPE_ANY;
		profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
		profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
		profile->ssid_index = i;
	}

	IWL_DEBUG_SCAN(mld,
		       "Sending scheduled scan profile config (n_match_sets=%u)\n",
		       req->n_match_sets);

	ret = iwl_mld_send_cmd(mld, &hcmd);

	kfree(cmd);

	return ret;
}

static int
iwl_mld_sched_scan_handle_non_psc_channels(struct iwl_mld_scan_params *params,
					   bool *non_psc_included)
{
	int i, j;

	*non_psc_included = false;
	/* for 6 GHZ band only PSC channels need to be added */
	for (i = 0; i < params->n_channels; i++) {
		struct ieee80211_channel *channel = params->channels[i];

		if (channel->band == NL80211_BAND_6GHZ &&
		    !cfg80211_channel_is_psc(channel)) {
			*non_psc_included = true;
			break;
		}
	}

	if (!*non_psc_included)
		return 0;

	params->channels =
		kmemdup(params->channels,
			sizeof(params->channels[0]) * params->n_channels,
			GFP_KERNEL);
	if (!params->channels)
		return -ENOMEM;

	for (i = j = 0; i < params->n_channels; i++) {
		if (params->channels[i]->band == NL80211_BAND_6GHZ &&
		    !cfg80211_channel_is_psc(params->channels[i]))
			continue;
		params->channels[j++] = params->channels[i];
	}

	params->n_channels = j;

	return 0;
}

static void
iwl_mld_scan_6ghz_passive_scan(struct iwl_mld *mld,
			       struct iwl_mld_scan_params *params,
			       struct ieee80211_vif *vif)
{
	struct ieee80211_supported_band *sband =
		&mld->nvm_data->bands[NL80211_BAND_6GHZ];
	u32 n_disabled, i;

	params->enable_6ghz_passive = false;

	/* 6 GHz passive scan may be enabled in the first 2.4 GHz/5 GHz scan
	 * phase to discover geo location if no AP's are found. Skip it when
	 * we're in the 6 GHz scan phase.
	 */
	if (params->scan_6ghz)
		return;

	/* 6 GHz passive scan allowed only on station interface  */
	if (vif->type != NL80211_IFTYPE_STATION) {
		IWL_DEBUG_SCAN(mld,
			       "6GHz passive scan: not station interface\n");
		return;
	}

	/* 6 GHz passive scan is allowed in a defined time interval following
	 * HW reset or resume flow, or while not associated and a large
	 * interval has passed since the last 6 GHz passive scan.
	 */
	if ((vif->cfg.assoc ||
	     time_after(mld->scan.last_6ghz_passive_jiffies +
			(IWL_MLD_6GHZ_PASSIVE_SCAN_TIMEOUT * HZ), jiffies)) &&
	    (time_before(mld->scan.last_start_time_jiffies +
			 (IWL_MLD_6GHZ_PASSIVE_SCAN_ASSOC_TIMEOUT * HZ),
			 jiffies))) {
		IWL_DEBUG_SCAN(mld, "6GHz passive scan: %s\n",
			       vif->cfg.assoc ? "associated" :
			       "timeout did not expire");
		return;
	}

	/* not enough channels in the regular scan request */
	if (params->n_channels < IWL_MLD_6GHZ_PASSIVE_SCAN_MIN_CHANS) {
		IWL_DEBUG_SCAN(mld,
			       "6GHz passive scan: not enough channels %d\n",
			       params->n_channels);
		return;
	}

	for (i = 0; i < params->n_ssids; i++) {
		if (!params->ssids[i].ssid_len)
			break;
	}

	/* not a wildcard scan, so cannot enable passive 6 GHz scan */
	if (i == params->n_ssids) {
		IWL_DEBUG_SCAN(mld,
			       "6GHz passive scan: no wildcard SSID\n");
		return;
	}

	if (!sband || !sband->n_channels) {
		IWL_DEBUG_SCAN(mld,
			       "6GHz passive scan: no 6GHz channels\n");
		return;
	}

	for (i = 0, n_disabled = 0; i < sband->n_channels; i++) {
		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED))
			n_disabled++;
	}

	/* Not all the 6 GHz channels are disabled, so no need for 6 GHz
	 * passive scan
	 */
	if (n_disabled != sband->n_channels) {
		IWL_DEBUG_SCAN(mld,
			       "6GHz passive scan: 6GHz channels enabled\n");
		return;
	}

	/* all conditions to enable 6 GHz passive scan are satisfied */
	IWL_DEBUG_SCAN(mld, "6GHz passive scan: can be enabled\n");
	params->enable_6ghz_passive = true;
}

static void
iwl_mld_scan_set_link_id(struct iwl_mld *mld, struct ieee80211_vif *vif,
			 struct iwl_mld_scan_params *params,
			 s8 tsf_report_link_id,
			 enum iwl_mld_scan_status scan_status)
{
	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
	struct iwl_mld_link *link;

	if (tsf_report_link_id < 0) {
		if (vif->active_links)
			tsf_report_link_id = __ffs(vif->active_links);
		else
			tsf_report_link_id = 0;
	}

	link = iwl_mld_link_dereference_check(mld_vif, tsf_report_link_id);
	if (!WARN_ON(!link)) {
		params->fw_link_id = link->fw_id;
		/* we to store fw_link_id only for regular scan,
		 * and use it in scan complete notif
		 */
		if (scan_status == IWL_MLD_SCAN_REGULAR)
			mld->scan.fw_link_id = link->fw_id;
	} else {
		mld->scan.fw_link_id = IWL_MLD_INVALID_FW_ID;
		params->fw_link_id = IWL_MLD_INVALID_FW_ID;
	}
}

static int
_iwl_mld_single_scan_start(struct iwl_mld *mld, struct ieee80211_vif *vif,
			   struct cfg80211_scan_request *req,
			   struct ieee80211_scan_ies *ies,
			   enum iwl_mld_scan_status scan_status)
{
	struct iwl_host_cmd hcmd = {
		.id = WIDE_ID(LONG_GROUP, SCAN_REQ_UMAC),
		.len = { mld->scan.cmd_size, },
		.data = { mld->scan.cmd, },
		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
	};
	struct iwl_mld_scan_iter_data scan_iter_data = {
		.current_vif = vif,
	};
	struct cfg80211_sched_scan_plan scan_plan = {.iterations = 1};
	struct iwl_mld_scan_params params = {};
	int ret, uid;

	/* we should have failed registration if scan_cmd was NULL */
	if (WARN_ON(!mld->scan.cmd))
		return -ENOMEM;

	if (!iwl_mld_scan_fits(mld, req->n_ssids, ies, req->n_channels))
		return -ENOBUFS;

	ieee80211_iterate_active_interfaces_mtx(mld->hw,
						IEEE80211_IFACE_ITER_NORMAL,
						iwl_mld_scan_iterator,
						&scan_iter_data);

	params.type = iwl_mld_get_scan_type(mld, vif, &scan_iter_data);
	params.n_ssids = req->n_ssids;
	params.flags = req->flags;
	params.n_channels = req->n_channels;
	params.delay = 0;
	params.ssids = req->ssids;
	params.channels = req->channels;
	params.mac_addr = req->mac_addr;
	params.mac_addr_mask = req->mac_addr_mask;
	params.no_cck = req->no_cck;
	params.pass_all = true;
	params.n_match_sets = 0;
	params.match_sets = NULL;
	params.scan_plans = &scan_plan;
	params.n_scan_plans = 1;

	params.n_6ghz_params = req->n_6ghz_params;
	params.scan_6ghz_params = req->scan_6ghz_params;
	params.scan_6ghz = req->scan_6ghz;

	ether_addr_copy(params.bssid, req->bssid);
	/* TODO: CDB - per-band flag */
	params.respect_p2p_go =
		iwl_mld_get_respect_p2p_go(mld, vif,
					   scan_iter_data.global_low_latency);

	if (req->duration)
		params.iter_notif = true;

	iwl_mld_scan_set_link_id(mld, vif, &params, req->tsf_report_link_id,
				 scan_status);

	iwl_mld_scan_build_probe_req(mld, vif, ies, &params);

	iwl_mld_scan_6ghz_passive_scan(mld, &params, vif);

	uid = iwl_mld_scan_build_cmd(mld, vif, &params, scan_status,
				     scan_iter_data.global_low_latency);
	if (uid < 0)
		return uid;

	ret = iwl_mld_send_cmd(mld, &hcmd);
	if (ret) {
		IWL_ERR(mld, "Scan failed! ret %d\n", ret);
		return ret;
	}

	IWL_DEBUG_SCAN(mld, "Scan request send success: status=%u, uid=%u\n",
		       scan_status, uid);

	mld->scan.uid_status[uid] = scan_status;
	mld->scan.status |= scan_status;

	if (params.enable_6ghz_passive)
		mld->scan.last_6ghz_passive_jiffies = jiffies;

	return 0;
}

static int
iwl_mld_scan_send_abort_cmd_status(struct iwl_mld *mld, int uid, u32 *status)
{
	struct iwl_umac_scan_abort abort_cmd = {
		.uid = cpu_to_le32(uid),
	};
	struct iwl_host_cmd cmd = {
		.id = WIDE_ID(LONG_GROUP, SCAN_ABORT_UMAC),
		.flags = CMD_WANT_SKB,
		.data = { &abort_cmd },
		.len[0] = sizeof(abort_cmd),
	};
	struct iwl_rx_packet *pkt;
	struct iwl_cmd_response *resp;
	u32 resp_len;
	int ret;

	ret = iwl_mld_send_cmd(mld, &cmd);
	if (ret)
		return ret;

	pkt = cmd.resp_pkt;

	resp_len = iwl_rx_packet_payload_len(pkt);
	if (IWL_FW_CHECK(mld, resp_len != sizeof(*resp),
			 "Scan Abort: unexpected response length %d\n",
			 resp_len)) {
		ret = -EIO;
		goto out;
	}

	resp = (void *)pkt->data;
	*status = le32_to_cpu(resp->status);

out:
	iwl_free_resp(&cmd);
	return ret;
}

static int
iwl_mld_scan_abort(struct iwl_mld *mld, int type, int uid, bool *wait)
{
	enum iwl_umac_scan_abort_status status =
		IWL_UMAC_SCAN_ABORT_STATUS_NOT_FOUND;
	int ret;

	*wait = true;

	IWL_DEBUG_SCAN(mld, "Sending scan abort, uid %u\n", uid);

	ret = iwl_mld_scan_send_abort_cmd_status(mld, uid, &status);

	IWL_DEBUG_SCAN(mld, "Scan abort: ret=%d status=%u\n", ret, status);

	/* We don't need to wait to scan complete in the following cases:
	 * 1. Driver failed to send the scan abort cmd.
	 * 2. The FW is no longer familiar with the scan that needs to be
	 *    stopped. It is expected that the scan complete notification was
	 *    already received but not yet processed.
	 *
	 * In both cases the flow should continue similar to the case that the
	 * scan was really aborted.
	 */
	if (ret || status == IWL_UMAC_SCAN_ABORT_STATUS_NOT_FOUND)
		*wait = false;

	return ret;
}

static int
iwl_mld_scan_stop_wait(struct iwl_mld *mld, int type, int uid)
{
	struct iwl_notification_wait wait_scan_done;
	static const u16 scan_comp_notif[] = { SCAN_COMPLETE_UMAC };
	bool wait = true;
	int ret;

	iwl_init_notification_wait(&mld->notif_wait, &wait_scan_done,
				   scan_comp_notif,
				   ARRAY_SIZE(scan_comp_notif),
				   NULL, NULL);

	IWL_DEBUG_SCAN(mld, "Preparing to stop scan, type=%x\n", type);

	ret = iwl_mld_scan_abort(mld, type, uid, &wait);
	if (ret) {
		IWL_DEBUG_SCAN(mld, "couldn't stop scan type=%d\n", type);
		goto return_no_wait;
	}

	if (!wait) {
		IWL_DEBUG_SCAN(mld, "no need to wait for scan type=%d\n", type);
		goto return_no_wait;
	}

	return iwl_wait_notification(&mld->notif_wait, &wait_scan_done, HZ);

return_no_wait:
	iwl_remove_notification(&mld->notif_wait, &wait_scan_done);
	return ret;
}

int iwl_mld_sched_scan_start(struct iwl_mld *mld,
			     struct ieee80211_vif *vif,
			     struct cfg80211_sched_scan_request *req,
			     struct ieee80211_scan_ies *ies,
			     int type)
{
	struct iwl_host_cmd hcmd = {
		.id = WIDE_ID(LONG_GROUP, SCAN_REQ_UMAC),
		.len = { mld->scan.cmd_size, },
		.data = { mld->scan.cmd, },
		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
	};
	struct iwl_mld_scan_params params = {};
	struct iwl_mld_scan_iter_data scan_iter_data = {
		.current_vif = vif,
	};
	bool non_psc_included = false;
	int ret, uid;

	/* we should have failed registration if scan_cmd was NULL */
	if (WARN_ON(!mld->scan.cmd))
		return -ENOMEM;

	/* FW supports only a single periodic scan */
	if (mld->scan.status & (IWL_MLD_SCAN_SCHED | IWL_MLD_SCAN_NETDETECT))
		return -EBUSY;

	ieee80211_iterate_active_interfaces_mtx(mld->hw,
						IEEE80211_IFACE_ITER_NORMAL,
						iwl_mld_scan_iterator,
						&scan_iter_data);

	params.type = iwl_mld_get_scan_type(mld, vif, &scan_iter_data);
	params.flags = req->flags;
	params.n_ssids = req->n_ssids;
	params.ssids = req->ssids;
	params.n_channels = req->n_channels;
	params.channels = req->channels;
	params.mac_addr = req->mac_addr;
	params.mac_addr_mask = req->mac_addr_mask;
	params.no_cck = false;
	params.pass_all =  iwl_mld_scan_pass_all(mld, req);
	params.n_match_sets = req->n_match_sets;
	params.match_sets = req->match_sets;
	params.n_scan_plans = req->n_scan_plans;
	params.scan_plans = req->scan_plans;
	/* TODO: CDB - per-band flag */
	params.respect_p2p_go =
		iwl_mld_get_respect_p2p_go(mld, vif,
					   scan_iter_data.global_low_latency);

	/* UMAC scan supports up to 16-bit delays, trim it down to 16-bits */
	params.delay = req->delay > U16_MAX ? U16_MAX : req->delay;

	eth_broadcast_addr(params.bssid);

	ret = iwl_mld_config_sched_scan_profiles(mld, req);
	if (ret)
		return ret;

	iwl_mld_scan_build_probe_req(mld, vif, ies, &params);

	ret = iwl_mld_sched_scan_handle_non_psc_channels(&params,
							 &non_psc_included);
	if (ret)
		goto out;

	if (!iwl_mld_scan_fits(mld, req->n_ssids, ies, params.n_channels)) {
		ret = -ENOBUFS;
		goto out;
	}

	uid = iwl_mld_scan_build_cmd(mld, vif, &params, type,
				     scan_iter_data.global_low_latency);
	if (uid < 0) {
		ret = uid;
		goto out;
	}

	ret = iwl_mld_send_cmd(mld, &hcmd);
	if (!ret) {
		IWL_DEBUG_SCAN(mld,
			       "Sched scan request send success: type=%u, uid=%u\n",
			       type, uid);
		mld->scan.uid_status[uid] = type;
		mld->scan.status |= type;
	} else {
		IWL_ERR(mld, "Sched scan failed! ret %d\n", ret);
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_DISABLED;
	}

out:
	if (non_psc_included)
		kfree(params.channels);
	return ret;
}

int iwl_mld_scan_stop(struct iwl_mld *mld, int type, bool notify)
{
	int uid, ret;

	IWL_DEBUG_SCAN(mld,
		       "Request to stop scan: type=0x%x, status=0x%x\n",
		       type, mld->scan.status);

	if (!(mld->scan.status & type))
		return 0;

	uid = iwl_mld_scan_uid_by_status(mld, type);
	/* must be valid, we just checked it's running */
	if (WARN_ON_ONCE(uid < 0))
		return uid;

	ret = iwl_mld_scan_stop_wait(mld, type, uid);
	if (ret)
		IWL_DEBUG_SCAN(mld, "Failed to stop scan\n");

	/* Clear the scan status so the next scan requests will
	 * succeed and mark the scan as stopping, so that the Rx
	 * handler doesn't do anything, as the scan was stopped from
	 * above. Also remove the handler to not notify mac80211
	 * erroneously after a new scan starts, for example.
	 */
	mld->scan.status &= ~type;
	mld->scan.uid_status[uid] = IWL_MLD_SCAN_NONE;
	iwl_mld_cancel_notifications_of_object(mld, IWL_MLD_OBJECT_TYPE_SCAN,
					       uid);

	if (type == IWL_MLD_SCAN_REGULAR) {
		if (notify) {
			struct cfg80211_scan_info info = {
				.aborted = true,
			};

			ieee80211_scan_completed(mld->hw, &info);
		}
	} else if (notify) {
		ieee80211_sched_scan_stopped(mld->hw);
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_DISABLED;
	}

	return ret;
}

int iwl_mld_regular_scan_start(struct iwl_mld *mld, struct ieee80211_vif *vif,
			       struct cfg80211_scan_request *req,
			       struct ieee80211_scan_ies *ies)
{
	/* Clear survey data when starting the first part of a regular scan */
	if (req->first_part && mld->channel_survey)
		memset(mld->channel_survey->channels, 0,
		       sizeof(mld->channel_survey->channels[0]) *
		       mld->channel_survey->n_channels);

	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
		iwl_mld_emlsr_block_tmp_non_bss(mld);

	return _iwl_mld_single_scan_start(mld, vif, req, ies,
					  IWL_MLD_SCAN_REGULAR);
}

static void iwl_mld_int_mlo_scan_start(struct iwl_mld *mld,
				       struct ieee80211_vif *vif,
				       struct ieee80211_channel **channels,
				       size_t n_channels)
{
	struct cfg80211_scan_request *req __free(kfree) = NULL;
	struct ieee80211_scan_ies ies = {};
	size_t size;
	int ret;

	IWL_DEBUG_SCAN(mld, "Starting Internal MLO scan: n_channels=%zu\n",
		       n_channels);

	size = struct_size(req, channels, n_channels);
	req = kzalloc(size, GFP_KERNEL);
	if (!req)
		return;

	/* set the requested channels */
	for (int i = 0; i < n_channels; i++)
		req->channels[i] = channels[i];

	req->n_channels = n_channels;

	/* set the rates */
	for (int i = 0; i < NUM_NL80211_BANDS; i++)
		if (mld->wiphy->bands[i])
			req->rates[i] =
				(1 << mld->wiphy->bands[i]->n_bitrates) - 1;

	req->wdev = ieee80211_vif_to_wdev(vif);
	req->wiphy = mld->wiphy;
	req->scan_start = jiffies;
	req->tsf_report_link_id = -1;

	ret = _iwl_mld_single_scan_start(mld, vif, req, &ies,
					 IWL_MLD_SCAN_INT_MLO);

	IWL_DEBUG_SCAN(mld, "Internal MLO scan: ret=%d\n", ret);
}

#define IWL_MLD_MLO_SCAN_BLOCKOUT_TIME		5 /* seconds */

void iwl_mld_int_mlo_scan(struct iwl_mld *mld, struct ieee80211_vif *vif)
{
	struct ieee80211_channel *channels[IEEE80211_MLD_MAX_NUM_LINKS];
	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
	unsigned long usable_links = ieee80211_vif_usable_links(vif);
	size_t n_channels = 0;
	u8 link_id;

	lockdep_assert_wiphy(mld->wiphy);

	if (!IWL_MLD_AUTO_EML_ENABLE || !vif->cfg.assoc ||
	    !ieee80211_vif_is_mld(vif) || hweight16(vif->valid_links) == 1)
		return;

	if (mld->scan.status & IWL_MLD_SCAN_INT_MLO) {
		IWL_DEBUG_SCAN(mld, "Internal MLO scan is already running\n");
		return;
	}

	if (mld_vif->last_link_activation_time > ktime_get_boottime_seconds() -
						 IWL_MLD_MLO_SCAN_BLOCKOUT_TIME) {
		/* timing doesn't matter much, so use the blockout time */
		wiphy_delayed_work_queue(mld->wiphy,
					 &mld_vif->mlo_scan_start_wk,
					 IWL_MLD_MLO_SCAN_BLOCKOUT_TIME);
		return;
	}

	for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) {
		struct ieee80211_bss_conf *link_conf =
			link_conf_dereference_check(vif, link_id);

		if (WARN_ON_ONCE(!link_conf))
			continue;

		channels[n_channels++] = link_conf->chanreq.oper.chan;
	}

	if (!n_channels)
		return;

	iwl_mld_int_mlo_scan_start(mld, vif, channels, n_channels);
}

void iwl_mld_handle_scan_iter_complete_notif(struct iwl_mld *mld,
					     struct iwl_rx_packet *pkt)
{
	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
	u32 uid = __le32_to_cpu(notif->uid);

	if (IWL_FW_CHECK(mld, uid >= ARRAY_SIZE(mld->scan.uid_status),
			 "FW reports out-of-range scan UID %d\n", uid))
		return;

	if (mld->scan.uid_status[uid] == IWL_MLD_SCAN_REGULAR)
		mld->scan.start_tsf = le64_to_cpu(notif->start_tsf);

	IWL_DEBUG_SCAN(mld,
		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
		       notif->status, notif->scanned_channels);

	if (mld->scan.pass_all_sched_res == SCHED_SCAN_PASS_ALL_STATE_FOUND) {
		IWL_DEBUG_SCAN(mld, "Pass all scheduled scan results found\n");
		ieee80211_sched_scan_results(mld->hw);
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_ENABLED;
	}

	IWL_DEBUG_SCAN(mld,
		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
		       le64_to_cpu(notif->start_tsf));
}

void iwl_mld_handle_match_found_notif(struct iwl_mld *mld,
				      struct iwl_rx_packet *pkt)
{
	IWL_DEBUG_SCAN(mld, "Scheduled scan results\n");
	ieee80211_sched_scan_results(mld->hw);
}

void iwl_mld_handle_scan_start_notif(struct iwl_mld *mld,
				     struct iwl_rx_packet *pkt)
{
	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
	u32 uid = le32_to_cpu(notif->uid);

	if (IWL_FW_CHECK(mld, uid >= ARRAY_SIZE(mld->scan.uid_status),
			 "FW reports out-of-range scan UID %d\n", uid))
		return;

	if (IWL_FW_CHECK(mld, !(mld->scan.uid_status[uid] & mld->scan.status),
			 "FW reports scan UID %d we didn't trigger\n", uid))
		return;

	IWL_DEBUG_SCAN(mld, "Scan started: uid=%u type=%u\n", uid,
		       mld->scan.uid_status[uid]);
	if (IWL_FW_CHECK(mld, mld->scan.uid_status[uid] != IWL_MLD_SCAN_INT_MLO,
			 "FW reports scan start notification %d we didn't trigger\n",
			 mld->scan.uid_status[uid]))
		return;

	mld->scan.last_mlo_scan_start_time = ktime_get_boottime_ns();
}

void iwl_mld_handle_scan_complete_notif(struct iwl_mld *mld,
					struct iwl_rx_packet *pkt)
{
	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
	u32 uid = __le32_to_cpu(notif->uid);

	if (IWL_FW_CHECK(mld, uid >= ARRAY_SIZE(mld->scan.uid_status),
			 "FW reports out-of-range scan UID %d\n", uid))
		return;

	IWL_DEBUG_SCAN(mld,
		       "Scan completed: uid=%u type=%u, status=%s, EBS=%s\n",
		       uid, mld->scan.uid_status[uid],
		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
				"completed" : "aborted",
		       iwl_mld_scan_ebs_status_str(notif->ebs_status));
	IWL_DEBUG_SCAN(mld, "Scan completed: scan_status=0x%x\n",
		       mld->scan.status);
	IWL_DEBUG_SCAN(mld,
		       "Scan completed: line=%u, iter=%u, elapsed time=%u\n",
		       notif->last_schedule, notif->last_iter,
		       __le32_to_cpu(notif->time_from_last_iter));

	if (IWL_FW_CHECK(mld, !(mld->scan.uid_status[uid] & mld->scan.status),
			 "FW reports scan UID %d we didn't trigger\n", uid))
		return;

	/* if the scan is already stopping, we don't need to notify mac80211 */
	if (mld->scan.uid_status[uid] == IWL_MLD_SCAN_REGULAR) {
		struct cfg80211_scan_info info = {
			.aborted = aborted,
			.scan_start_tsf = mld->scan.start_tsf,
		};
		int fw_link_id = mld->scan.fw_link_id;
		struct ieee80211_bss_conf *link_conf = NULL;

		if (fw_link_id != IWL_MLD_INVALID_FW_ID)
			link_conf = iwl_mld_fw_id_to_link_conf(mld, fw_link_id);

		/* It is possible that by the time the scan is complete the
		 * link was already removed and is not valid.
		 */
		if (link_conf)
			ether_addr_copy(info.tsf_bssid, link_conf->bssid);
		else
			IWL_DEBUG_SCAN(mld, "Scan link is no longer valid\n");

		ieee80211_scan_completed(mld->hw, &info);

		/* Scan is over, we can check again the tpt counters */
		iwl_mld_stop_ignoring_tpt_updates(mld);
	} else if (mld->scan.uid_status[uid] == IWL_MLD_SCAN_SCHED) {
		ieee80211_sched_scan_stopped(mld->hw);
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_DISABLED;
	} else if (mld->scan.uid_status[uid] == IWL_MLD_SCAN_INT_MLO) {
		IWL_DEBUG_SCAN(mld, "Internal MLO scan completed\n");

		/*
		 * We limit link selection to internal MLO scans as otherwise
		 * we do not know whether all channels were covered.
		 */
		iwl_mld_select_links(mld);
	}

	mld->scan.status &= ~mld->scan.uid_status[uid];

	IWL_DEBUG_SCAN(mld, "Scan completed: after update: scan_status=0x%x\n",
		       mld->scan.status);

	mld->scan.uid_status[uid] = IWL_MLD_SCAN_NONE;

	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
		mld->scan.last_ebs_failed = true;
}

/* This function is used in nic restart flow, to inform mac80211 about scans
 * that were aborted by restart flow or by an assert.
 */
void iwl_mld_report_scan_aborted(struct iwl_mld *mld)
{
	int uid;

	uid = iwl_mld_scan_uid_by_status(mld, IWL_MLD_SCAN_REGULAR);
	if (uid >= 0) {
		struct cfg80211_scan_info info = {
			.aborted = true,
		};

		ieee80211_scan_completed(mld->hw, &info);
		mld->scan.uid_status[uid] = IWL_MLD_SCAN_NONE;
	}

	uid = iwl_mld_scan_uid_by_status(mld, IWL_MLD_SCAN_SCHED);
	if (uid >= 0) {
		mld->scan.pass_all_sched_res = SCHED_SCAN_PASS_ALL_STATE_DISABLED;
		mld->scan.uid_status[uid] = IWL_MLD_SCAN_NONE;

		/* sched scan will be restarted by mac80211 in reconfig.
		 * report to mac80211 that sched scan stopped only if we won't
		 * restart the firmware.
		 */
		if (!iwlwifi_mod_params.fw_restart)
			ieee80211_sched_scan_stopped(mld->hw);
	}

	uid = iwl_mld_scan_uid_by_status(mld, IWL_MLD_SCAN_INT_MLO);
	if (uid >= 0) {
		IWL_DEBUG_SCAN(mld, "Internal MLO scan aborted\n");
		mld->scan.uid_status[uid] = IWL_MLD_SCAN_NONE;
	}

	BUILD_BUG_ON(IWL_MLD_SCAN_NONE != 0);
	memset(mld->scan.uid_status, 0, sizeof(mld->scan.uid_status));
}

int iwl_mld_alloc_scan_cmd(struct iwl_mld *mld)
{
	u8 scan_cmd_ver = iwl_fw_lookup_cmd_ver(mld->fw, SCAN_REQ_UMAC,
						IWL_FW_CMD_VER_UNKNOWN);
	size_t scan_cmd_size;

	if (scan_cmd_ver == 17) {
		scan_cmd_size = sizeof(struct iwl_scan_req_umac_v17);
	} else if (scan_cmd_ver == 18) {
		scan_cmd_size = sizeof(struct iwl_scan_req_umac_v18);
	} else {
		IWL_ERR(mld, "Unexpected scan cmd version %d\n", scan_cmd_ver);
		return -EINVAL;
	}

	mld->scan.cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
	if (!mld->scan.cmd)
		return -ENOMEM;

	mld->scan.cmd_size = scan_cmd_size;
	mld->scan.cmd_ver = scan_cmd_ver;

	return 0;
}

static int iwl_mld_chanidx_from_phy(struct iwl_mld *mld,
				    enum nl80211_band band,
				    u16 phy_chan_num)
{
	struct ieee80211_supported_band *sband = mld->wiphy->bands[band];

	if (WARN_ON_ONCE(!sband))
		return -EINVAL;

	for (int chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
		struct ieee80211_channel *channel = &sband->channels[chan_idx];

		if (channel->hw_value == phy_chan_num)
			return chan_idx;
	}

	return -EINVAL;
}

void iwl_mld_handle_channel_survey_notif(struct iwl_mld *mld,
					 struct iwl_rx_packet *pkt)
{
	const struct iwl_umac_scan_channel_survey_notif *notif =
		(void *)pkt->data;
	struct iwl_mld_survey_channel *info;
	enum nl80211_band band;
	int chan_idx;

	if (!mld->channel_survey) {
		size_t n_channels = 0;

		for (band = 0; band < NUM_NL80211_BANDS; band++) {
			if (!mld->wiphy->bands[band])
				continue;

			n_channels += mld->wiphy->bands[band]->n_channels;
		}

		mld->channel_survey = kzalloc_flex(*mld->channel_survey,
						   channels, n_channels);

		if (!mld->channel_survey)
			return;

		mld->channel_survey->n_channels = n_channels;
		n_channels = 0;
		for (band = 0; band < NUM_NL80211_BANDS; band++) {
			if (!mld->wiphy->bands[band])
				continue;

			mld->channel_survey->bands[band] =
				&mld->channel_survey->channels[n_channels];
			n_channels += mld->wiphy->bands[band]->n_channels;
		}
	}

	band = iwl_mld_phy_band_to_nl80211(le32_to_cpu(notif->band));
	chan_idx = iwl_mld_chanidx_from_phy(mld, band,
					    le32_to_cpu(notif->channel));
	if (WARN_ON_ONCE(chan_idx < 0))
		return;

	IWL_DEBUG_SCAN(mld, "channel survey received for freq %d\n",
		       mld->wiphy->bands[band]->channels[chan_idx].center_freq);

	info = &mld->channel_survey->bands[band][chan_idx];

	/* Times are all in ms */
	info->time = le32_to_cpu(notif->active_time);
	info->time_busy = le32_to_cpu(notif->busy_time);
	info->noise =
		iwl_average_neg_dbm(notif->noise, ARRAY_SIZE(notif->noise));
}

int iwl_mld_mac80211_get_survey(struct ieee80211_hw *hw, int idx,
				struct survey_info *survey)
{
	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
	int curr_idx = 0;

	if (!mld->channel_survey)
		return -ENOENT;

	/* Iterate bands/channels to find the requested index.
	 * Logically this returns the entry with index "idx" from a flattened
	 * survey result array that only contains channels with information.
	 * The current index into this flattened array is tracked in curr_idx.
	 */
	for (enum nl80211_band band = 0; band < NUM_NL80211_BANDS; band++) {
		struct ieee80211_supported_band *sband =
			mld->wiphy->bands[band];

		if (!sband)
			continue;

		for (int per_band_idx = 0;
		     per_band_idx < sband->n_channels;
		     per_band_idx++) {
			struct iwl_mld_survey_channel *info =
				&mld->channel_survey->bands[band][per_band_idx];

			/* Skip entry entirely, it was not reported/scanned,
			 * do not increase curr_idx for this entry.
			 */
			if (!info->time)
				continue;

			/* Search did not reach the requested entry yet,
			 * increment curr_idx and continue.
			 */
			if (idx != curr_idx) {
				curr_idx++;
				continue;
			}

			/* Found (the next) channel to report */
			survey->channel = &sband->channels[per_band_idx];
			survey->filled = SURVEY_INFO_TIME |
					 SURVEY_INFO_TIME_BUSY;
			survey->time = info->time;
			survey->time_busy = info->time_busy;
			survey->noise = info->noise;
			if (survey->noise < 0)
				survey->filled |= SURVEY_INFO_NOISE_DBM;

			return 0;
		}
	}

	return -ENOENT;
}