iwlwifi: mvm: combine SSID functions for sched and regular scans
[linux-block.git] / drivers / net / wireless / iwlwifi / mvm / scan.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9af91f46 9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
8ca151b5
JB
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
410dc5aa 26 * in the file called COPYING.
8ca151b5
JB
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
51368bf7 34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9af91f46 35 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
8ca151b5
JB
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/etherdevice.h>
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "iwl-eeprom-parse.h"
71#include "fw-api-scan.h"
72
73#define IWL_PLCP_QUIET_THRESH 1
74#define IWL_ACTIVE_QUIET_TIME 10
a1ed4025
HD
75#define IWL_DENSE_EBS_SCAN_RATIO 5
76#define IWL_SPARSE_EBS_SCAN_RATIO 1
8a110d9b
AB
77
78struct iwl_mvm_scan_params {
79 u32 max_out_time;
80 u32 suspend_time;
50df8a30 81 bool passive_fragmented;
f7b788b4
LC
82 u32 n_channels;
83 u32 delay;
84 int n_ssids;
85 struct cfg80211_ssid *ssids;
86 struct ieee80211_channel **channels;
87 u16 interval; /* interval between scans (in secs) */
88 u32 flags;
89 u8 *mac_addr;
90 u8 *mac_addr_mask;
91 bool no_cck;
92 bool pass_all;
93 int n_match_sets;
94 struct cfg80211_match_set *match_sets;
50df8a30
AB
95 struct _dwell {
96 u16 passive;
97 u16 active;
190f1029 98 u16 fragmented;
50df8a30 99 } dwell[IEEE80211_NUM_BANDS];
8a110d9b 100};
8ca151b5 101
d2496221
DS
102enum iwl_umac_scan_uid_type {
103 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
104 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
105 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
106 IWL_UMAC_SCAN_UID_SCHED_SCAN,
107};
108
109static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
110 enum iwl_umac_scan_uid_type type, bool notify);
111
112static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
113{
114 if (mvm->scan_rx_ant != ANT_NONE)
115 return mvm->scan_rx_ant;
a0544272 116 return iwl_mvm_get_valid_rx_ant(mvm);
d2496221
DS
117}
118
8ca151b5
JB
119static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
120{
121 u16 rx_chain;
91b05d10 122 u8 rx_ant;
8ca151b5 123
d2496221 124 rx_ant = iwl_mvm_scan_rx_ant(mvm);
8ca151b5
JB
125 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
126 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
127 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
128 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
129 return cpu_to_le16(rx_chain);
130}
131
fb98be5e 132static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
8ca151b5 133{
fb98be5e 134 if (band == IEEE80211_BAND_2GHZ)
8ca151b5
JB
135 return cpu_to_le32(PHY_BAND_24);
136 else
137 return cpu_to_le32(PHY_BAND_5);
138}
139
140static inline __le32
141iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
142 bool no_cck)
143{
144 u32 tx_ant;
145
146 mvm->scan_last_antenna_idx =
a0544272 147 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
8ca151b5
JB
148 mvm->scan_last_antenna_idx);
149 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
150
151 if (band == IEEE80211_BAND_2GHZ && !no_cck)
152 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
153 tx_ant);
154 else
155 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
156}
157
8ca151b5
JB
158/*
159 * If req->n_ssids > 0, it means we should do an active scan.
160 * In case of active scan w/o directed scan, we receive a zero-length SSID
161 * just to notify that this scan is active and not passive.
162 * In order to notify the FW of the number of SSIDs we wish to scan (including
163 * the zero-length one), we need to set the corresponding bits in chan->type,
20f1a5de
DS
164 * one for each SSID, and set the active bit (first). If the first SSID is
165 * already included in the probe template, so we need to set only
166 * req->n_ssids - 1 bits in addition to the first bit.
8ca151b5 167 */
3cae0734
DS
168static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
169 enum ieee80211_band band, int n_ssids)
8ca151b5 170{
3cae0734
DS
171 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
172 return 10;
8ca151b5 173 if (band == IEEE80211_BAND_2GHZ)
39745337
DS
174 return 20 + 3 * (n_ssids + 1);
175 return 10 + 2 * (n_ssids + 1);
8ca151b5
JB
176}
177
3cae0734
DS
178static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
179 enum ieee80211_band band)
8ca151b5 180{
3cae0734
DS
181 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
182 return 110;
8ca151b5
JB
183 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
184}
185
8a110d9b
AB
186static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
187 struct ieee80211_vif *vif)
61f6325d 188{
8a110d9b 189 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
c3f8d0a3 190 int *global_cnt = data;
61f6325d 191
1e2ebe0e
DS
192 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
193 mvmvif->phy_ctxt->id < MAX_PHYS)
c3f8d0a3 194 *global_cnt += 1;
8a110d9b
AB
195}
196
197static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
50df8a30 198 struct ieee80211_vif *vif,
ab480030 199 int n_ssids, u32 flags,
8a110d9b
AB
200 struct iwl_mvm_scan_params *params)
201{
c3f8d0a3 202 int global_cnt = 0;
50df8a30 203 enum ieee80211_band band;
2ce89cd6 204 u8 frag_passive_dwell = 0;
8a110d9b
AB
205
206 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
207 IEEE80211_IFACE_ITER_NORMAL,
208 iwl_mvm_scan_condition_iterator,
c3f8d0a3 209 &global_cnt);
f7b788b4
LC
210 params->n_ssids = n_ssids;
211 params->flags = flags;
ef67f18d 212
c3f8d0a3 213 if (!global_cnt)
ef67f18d
AB
214 goto not_bound;
215
2ce89cd6 216 params->suspend_time = 30;
44e9cd7e 217 params->max_out_time = 120;
ef67f18d
AB
218
219 if (iwl_mvm_low_latency(mvm)) {
2ce89cd6
DS
220 if (mvm->fw->ucode_capa.api[0] &
221 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
222 params->suspend_time = 105;
c3f8d0a3
HD
223 /*
224 * If there is more than one active interface make
225 * passive scan more fragmented.
226 */
ef17708e 227 frag_passive_dwell = 40;
44e9cd7e 228 params->max_out_time = frag_passive_dwell;
2ce89cd6
DS
229 } else {
230 params->suspend_time = 120;
231 params->max_out_time = 120;
232 }
233 }
234
235 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
236 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
237 /*
238 * P2P device scan should not be fragmented to avoid negative
239 * impact on P2P device discovery. Configure max_out_time to be
240 * equal to dwell time on passive channel. Take a longest
241 * possible value, one that corresponds to 2GHz band
242 */
243 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
244 u32 passive_dwell =
3cae0734
DS
245 iwl_mvm_get_passive_dwell(mvm,
246 IEEE80211_BAND_2GHZ);
2ce89cd6
DS
247 params->max_out_time = passive_dwell;
248 } else {
249 params->passive_fragmented = true;
250 }
50df8a30 251 }
8a110d9b 252
c1537664
LC
253 if ((flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
254 (params->max_out_time > 200))
ab480030
JB
255 params->max_out_time = 200;
256
ef67f18d
AB
257not_bound:
258
50df8a30 259 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
2ce89cd6 260 if (params->passive_fragmented)
190f1029
DS
261 params->dwell[band].fragmented = frag_passive_dwell;
262
263 params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
264 band);
3cae0734 265 params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
50df8a30
AB
266 n_ssids);
267 }
0616c62c
LC
268
269 IWL_DEBUG_SCAN(mvm,
270 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
271 params->max_out_time, params->suspend_time,
272 params->passive_fragmented);
273 IWL_DEBUG_SCAN(mvm,
274 "dwell[IEEE80211_BAND_2GHZ]: passive %d, active %d, fragmented %d\n",
275 params->dwell[IEEE80211_BAND_2GHZ].passive,
276 params->dwell[IEEE80211_BAND_2GHZ].active,
277 params->dwell[IEEE80211_BAND_2GHZ].fragmented);
278 IWL_DEBUG_SCAN(mvm,
279 "dwell[IEEE80211_BAND_5GHZ]: passive %d, active %d, fragmented %d\n",
280 params->dwell[IEEE80211_BAND_5GHZ].passive,
281 params->dwell[IEEE80211_BAND_5GHZ].active,
282 params->dwell[IEEE80211_BAND_5GHZ].fragmented);
61f6325d
HD
283}
284
73897bd1
AO
285static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
286{
287 /* require rrm scan whenever the fw supports it */
288 return mvm->fw->ucode_capa.capa[0] &
289 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
290}
291
999d2568 292static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
48849a41
AO
293{
294 int max_probe_len;
295
1f940386 296 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
48849a41
AO
297
298 /* we create the 802.11 header and SSID element */
299 max_probe_len -= 24 + 2;
300
66dc5272
AO
301 /* DS parameter set element is added on 2.4GHZ band if required */
302 if (iwl_mvm_rrm_scan_needed(mvm))
303 max_probe_len -= 3;
304
48849a41
AO
305 return max_probe_len;
306}
307
999d2568 308int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
48849a41 309{
999d2568 310 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
48849a41 311
48849a41
AO
312 /* TODO: [BUG] This function should return the maximum allowed size of
313 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
314 * in the same command. So the correct implementation of this function
315 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
316 * command has only 512 bytes and it would leave us with about 240
317 * bytes for scan IEs, which is clearly not enough. So meanwhile
318 * we will report an incorrect value. This may result in a failure to
319 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
320 * functions with -ENOBUFS, if a large enough probe will be provided.
321 */
322 return max_ie_len;
323}
324
e5d74646
AB
325int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
326 struct iwl_rx_cmd_buffer *rxb,
327 struct iwl_device_cmd *cmd)
328{
329 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1083fd73 330 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
e5d74646
AB
331
332 IWL_DEBUG_SCAN(mvm,
333 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
334 notif->status, notif->scanned_channels);
335 return 0;
336}
337
fb98be5e
DS
338int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
339 struct iwl_rx_cmd_buffer *rxb,
340 struct iwl_device_cmd *cmd)
35a000b7 341{
d2496221
DS
342 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
343 ieee80211_sched_scan_results(mvm->hw);
35a000b7
DS
344
345 return 0;
346}
347
35a000b7
DS
348int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
349 struct iwl_rx_cmd_buffer *rxb,
350 struct iwl_device_cmd *cmd)
351{
352 struct iwl_rx_packet *pkt = rxb_addr(rxb);
9af91f46
LC
353 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
354 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
355 bool ebs_successful = (scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS);
fb98be5e 356
a6623e84
JB
357 /* scan status must be locked for proper checking */
358 lockdep_assert_held(&mvm->mutex);
359
9af91f46
LC
360 /* We first check if we were stopping a scan, in which case we
361 * just clear the stopping flag. Then we check if it was a
362 * firmware initiated stop, in which case we need to inform
363 * mac80211.
364 * Note that we can have a stopping and a running scan
365 * simultaneously, but we can't have two different types of
366 * scans stopping or running at the same time (since LMAC
367 * doesn't support it).
368 */
369
370 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
371 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
372
373 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
374 aborted ? "aborted" : "completed",
375 ebs_successful ? "successful" : "failed");
e820c2da 376
9af91f46
LC
377 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
378 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
379 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
380 aborted ? "aborted" : "completed",
381 ebs_successful ? "successful" : "failed");
35a000b7 382
9af91f46
LC
383 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
384 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
385 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
386
387 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
388 aborted ? "aborted" : "completed",
389 ebs_successful ? "successful" : "failed");
390
391 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
33ea27f6 392 ieee80211_sched_scan_stopped(mvm->hw);
9af91f46
LC
393 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
394 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
395 aborted ? "aborted" : "completed",
396 ebs_successful ? "successful" : "failed");
397
398 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
fb98be5e 399 ieee80211_scan_completed(mvm->hw,
1f940386 400 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
4ff78181 401 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
33ea27f6 402 }
35a000b7 403
9af91f46 404 mvm->last_ebs_successful = ebs_successful;
e820c2da 405
35a000b7
DS
406 return 0;
407}
408
35a000b7
DS
409static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
410{
411 int i;
412
413 for (i = 0; i < PROBE_OPTION_MAX; i++) {
414 if (!ssid_list[i].len)
415 break;
416 if (ssid_list[i].len == ssid_len &&
417 !memcmp(ssid_list->ssid, ssid, ssid_len))
418 return i;
419 }
420 return -1;
421}
422
e2ec4f6d
LC
423/* We insert the SSIDs in an inverted order, because the FW will
424 * invert it back.
425 */
426static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
427 struct iwl_ssid_ie *ssids,
428 u32 *ssid_bitmap)
35a000b7
DS
429{
430 int i, j;
431 int index;
432
433 /*
434 * copy SSIDs from match list.
435 * iwl_config_sched_scan_profiles() uses the order of these ssids to
436 * config match list.
437 */
1c1b5b26
LC
438 for (i = 0, j = params->n_match_sets - 1;
439 j >= 0 && i < PROBE_OPTION_MAX;
440 i++, j--) {
ea73cbce 441 /* skip empty SSID matchsets */
1c1b5b26 442 if (!params->match_sets[j].ssid.ssid_len)
ea73cbce 443 continue;
e2ec4f6d
LC
444 ssids[i].id = WLAN_EID_SSID;
445 ssids[i].len = params->match_sets[j].ssid.ssid_len;
446 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
447 ssids[i].len);
35a000b7
DS
448 }
449
450 /* add SSIDs from scan SSID list */
451 *ssid_bitmap = 0;
1c1b5b26
LC
452 for (j = params->n_ssids - 1;
453 j >= 0 && i < PROBE_OPTION_MAX;
454 i++, j--) {
f7b788b4
LC
455 index = iwl_ssid_exist(params->ssids[j].ssid,
456 params->ssids[j].ssid_len,
e2ec4f6d 457 ssids);
35a000b7 458 if (index < 0) {
f7b788b4 459 if (!params->ssids[j].ssid_len)
35a000b7 460 continue;
e2ec4f6d
LC
461 ssids[i].id = WLAN_EID_SSID;
462 ssids[i].len = params->ssids[j].ssid_len;
463 memcpy(ssids[i].ssid, params->ssids[j].ssid,
464 ssids[i].len);
465 *ssid_bitmap |= BIT(i);
35a000b7 466 } else {
e2ec4f6d 467 *ssid_bitmap |= BIT(index);
35a000b7
DS
468 }
469 }
470}
471
35a000b7
DS
472int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
473 struct cfg80211_sched_scan_request *req)
474{
475 struct iwl_scan_offload_profile *profile;
476 struct iwl_scan_offload_profile_cfg *profile_cfg;
477 struct iwl_scan_offload_blacklist *blacklist;
478 struct iwl_host_cmd cmd = {
479 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
35a000b7
DS
480 .len[1] = sizeof(*profile_cfg),
481 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
482 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
483 };
484 int blacklist_len;
485 int i;
486 int ret;
487
488 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
489 return -EIO;
490
491 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
492 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
493 else
494 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
495
496 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
497 if (!blacklist)
498 return -ENOMEM;
499
500 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
501 if (!profile_cfg) {
502 ret = -ENOMEM;
503 goto free_blacklist;
504 }
505
506 cmd.data[0] = blacklist;
507 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
508 cmd.data[1] = profile_cfg;
509
510 /* No blacklist configuration */
511
512 profile_cfg->num_profiles = req->n_match_sets;
513 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
514 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
515 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
6e0bbe5e
DS
516 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
517 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
35a000b7
DS
518
519 for (i = 0; i < req->n_match_sets; i++) {
520 profile = &profile_cfg->profiles[i];
521 profile->ssid_index = i;
522 /* Support any cipher and auth algorithm */
523 profile->unicast_cipher = 0xff;
524 profile->auth_alg = 0xff;
525 profile->network_type = IWL_NETWORK_TYPE_ANY;
526 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
527 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
528 }
529
530 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
531
532 ret = iwl_mvm_send_cmd(mvm, &cmd);
533 kfree(profile_cfg);
534free_blacklist:
535 kfree(blacklist);
536
537 return ret;
538}
539
d2496221
DS
540static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
541 struct cfg80211_sched_scan_request *req)
542{
543 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
544 IWL_DEBUG_SCAN(mvm,
545 "Sending scheduled scan with filtering, n_match_sets %d\n",
546 req->n_match_sets);
547 return false;
548 }
549
550 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
551 return true;
552}
553
fb98be5e 554static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
35a000b7
DS
555{
556 int ret;
557 struct iwl_host_cmd cmd = {
558 .id = SCAN_OFFLOAD_ABORT_CMD,
35a000b7
DS
559 };
560 u32 status;
561
562 /* Exit instantly with error when device is not ready
563 * to receive scan abort command or it does not perform
564 * scheduled scan currently */
9af91f46 565 if (!mvm->scan_status)
35a000b7
DS
566 return -EIO;
567
568 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
569 if (ret)
570 return ret;
571
572 if (status != CAN_ABORT_STATUS) {
573 /*
574 * The scan abort will return 1 for success or
575 * 2 for "failure". A failure condition can be
576 * due to simply not being in an active scan which
577 * can occur if we send the scan abort before the
578 * microcode has notified us that a scan is completed.
579 */
580 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
33ea27f6 581 ret = -ENOENT;
35a000b7
DS
582 }
583
584 return ret;
585}
586
fb98be5e 587int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
35a000b7
DS
588{
589 int ret;
33ea27f6
AN
590 struct iwl_notification_wait wait_scan_done;
591 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
9af91f46 592 bool sched = !!(mvm->scan_status & IWL_MVM_SCAN_SCHED);
35a000b7
DS
593
594 lockdep_assert_held(&mvm->mutex);
595
d2496221
DS
596 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
597 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
598 notify);
599
9af91f46
LC
600 /* FIXME: For now we only check if no scan is set here, since
601 * we only support LMAC in this flow and it doesn't support
602 * multiple scans.
603 */
604 if (!mvm->scan_status)
90ea15c1
EG
605 return 0;
606
54331db9
LC
607 if (iwl_mvm_is_radio_killed(mvm)) {
608 ret = 0;
90ea15c1 609 goto out;
54331db9 610 }
90ea15c1 611
33ea27f6
AN
612 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
613 scan_done_notif,
614 ARRAY_SIZE(scan_done_notif),
615 NULL, NULL);
616
fb98be5e 617 ret = iwl_mvm_send_scan_offload_abort(mvm);
33ea27f6 618 if (ret) {
fb98be5e
DS
619 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
620 sched ? "offloaded " : "", ret);
33ea27f6 621 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
54331db9 622 goto out;
33ea27f6
AN
623 }
624
fb98be5e 625 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
9af91f46 626 sched ? "scheduled " : "");
33ea27f6
AN
627
628 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
54331db9 629out:
9af91f46
LC
630 /* Clear the scan status so the next scan requests will
631 * succeed and mark the scan as stopping, so that the Rx
632 * handler doesn't do anything, as the scan was stopped from
633 * above. Since the rx handler won't do anything now, we have
634 * to release the scan reference here.
33ea27f6 635 */
9af91f46 636 if (mvm->scan_status == IWL_MVM_SCAN_REGULAR)
4ff78181
EP
637 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
638
9af91f46
LC
639 if (sched) {
640 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
641 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_SCHED;
642 if (notify)
fb98be5e 643 ieee80211_sched_scan_stopped(mvm->hw);
9af91f46
LC
644 } else {
645 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
646 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_REGULAR;
647 if (notify)
fb98be5e
DS
648 ieee80211_scan_completed(mvm->hw, true);
649 }
636a2cdc 650
54331db9 651 return ret;
35a000b7 652}
fb98be5e 653
65ff556b
LC
654static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
655 struct iwl_scan_req_tx_cmd *tx_cmd,
656 bool no_cck)
fb98be5e
DS
657{
658 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
659 TX_CMD_FLG_BT_DIS);
660 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
661 IEEE80211_BAND_2GHZ,
662 no_cck);
663 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
664
665 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
666 TX_CMD_FLG_BT_DIS);
667 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
668 IEEE80211_BAND_5GHZ,
669 no_cck);
670 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
671}
672
673static void
674iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
675 struct ieee80211_channel **channels,
676 int n_channels, u32 ssid_bitmap,
65ff556b 677 struct iwl_scan_req_lmac *cmd)
fb98be5e
DS
678{
679 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
680 int i;
681
682 for (i = 0; i < n_channels; i++) {
683 channel_cfg[i].channel_num =
684 cpu_to_le16(channels[i]->hw_value);
685 channel_cfg[i].iter_count = cpu_to_le16(1);
686 channel_cfg[i].iter_interval = 0;
687 channel_cfg[i].flags =
688 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
689 ssid_bitmap);
690 }
691}
692
66dc5272
AO
693static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
694 size_t len, u8 *const pos)
695{
696 static const u8 before_ds_params[] = {
697 WLAN_EID_SSID,
698 WLAN_EID_SUPP_RATES,
699 WLAN_EID_REQUEST,
700 WLAN_EID_EXT_SUPP_RATES,
701 };
702 size_t offs;
703 u8 *newpos = pos;
704
705 if (!iwl_mvm_rrm_scan_needed(mvm)) {
706 memcpy(newpos, ies, len);
707 return newpos + len;
708 }
709
710 offs = ieee80211_ie_split(ies, len,
711 before_ds_params,
712 ARRAY_SIZE(before_ds_params),
713 0);
714
715 memcpy(newpos, ies, offs);
716 newpos += offs;
717
718 /* Add a placeholder for DS Parameter Set element */
719 *newpos++ = WLAN_EID_DS_PARAMS;
720 *newpos++ = 1;
721 *newpos++ = 0;
722
723 memcpy(newpos, ies + offs, len - offs);
724 newpos += len - offs;
725
726 return newpos;
727}
728
fb98be5e 729static void
65ff556b
LC
730iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
731 struct ieee80211_scan_ies *ies,
732 struct iwl_scan_probe_req *preq,
733 const u8 *mac_addr, const u8 *mac_addr_mask)
fb98be5e 734{
fb98be5e 735 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
66dc5272 736 u8 *pos, *newpos;
fb98be5e 737
effd05ac
JB
738 /*
739 * Unfortunately, right now the offload scan doesn't support randomising
740 * within the firmware, so until the firmware API is ready we implement
741 * it in the driver. This means that the scan iterations won't really be
742 * random, only when it's restarted, but at least that helps a bit.
743 */
744 if (mac_addr)
745 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
746 else
747 memcpy(frame->sa, vif->addr, ETH_ALEN);
748
fb98be5e
DS
749 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
750 eth_broadcast_addr(frame->da);
fb98be5e
DS
751 eth_broadcast_addr(frame->bssid);
752 frame->seq_ctrl = 0;
753
754 pos = frame->u.probe_req.variable;
755 *pos++ = WLAN_EID_SSID;
756 *pos++ = 0;
757
758 preq->mac_header.offset = 0;
759 preq->mac_header.len = cpu_to_le16(24 + 2);
760
66dc5272
AO
761 /* Insert ds parameter set element on 2.4 GHz band */
762 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
763 ies->ies[IEEE80211_BAND_2GHZ],
764 ies->len[IEEE80211_BAND_2GHZ],
765 pos);
fb98be5e 766 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
66dc5272
AO
767 preq->band_data[0].len = cpu_to_le16(newpos - pos);
768 pos = newpos;
fb98be5e
DS
769
770 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
771 ies->len[IEEE80211_BAND_5GHZ]);
772 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
773 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
774 pos += ies->len[IEEE80211_BAND_5GHZ];
775
776 memcpy(pos, ies->common_ies, ies->common_ie_len);
777 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
778 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
779}
780
781static void
65ff556b
LC
782iwl_mvm_build_generic_scan_cmd(struct iwl_mvm *mvm,
783 struct iwl_scan_req_lmac *cmd,
784 struct iwl_mvm_scan_params *params)
fb98be5e 785{
af91344c 786 memset(cmd, 0, ksize(cmd));
2ce89cd6
DS
787 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
788 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
789 if (params->passive_fragmented)
790 cmd->fragmented_dwell =
190f1029 791 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
fb98be5e
DS
792 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
793 cmd->max_out_time = cpu_to_le32(params->max_out_time);
794 cmd->suspend_time = cpu_to_le32(params->suspend_time);
795 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
fb98be5e 796 cmd->iter_num = cpu_to_le32(1);
af91344c 797
73897bd1
AO
798 if (iwl_mvm_rrm_scan_needed(mvm))
799 cmd->scan_flags |=
800 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
fb98be5e
DS
801}
802
999d2568
LC
803static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
804 struct ieee80211_scan_ies *ies,
805 int n_channels)
806{
807 return ((n_ssids <= PROBE_OPTION_MAX) &&
808 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
809 (ies->common_ie_len +
810 ies->len[NL80211_BAND_2GHZ] +
811 ies->len[NL80211_BAND_5GHZ] <=
812 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
813}
814
6749dd80 815static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3db7c6e7
LC
816 struct ieee80211_scan_ies *ies,
817 struct iwl_mvm_scan_params *params)
fb98be5e 818{
65ff556b 819 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
d2496221 820 struct iwl_scan_probe_req *preq;
f7b788b4 821 u32 flags = 0, ssid_bitmap = 0;
fb98be5e
DS
822
823 lockdep_assert_held(&mvm->mutex);
824
3db7c6e7 825 iwl_mvm_build_generic_scan_cmd(mvm, cmd, params);
fb98be5e 826
f7b788b4
LC
827 cmd->n_channels = (u8)params->n_channels;
828
829 cmd->delay = cpu_to_le32(params->delay);
fb98be5e 830
f7b788b4
LC
831 if (params->pass_all)
832 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
833 else
834 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
fb98be5e 835
f7b788b4 836 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
fb98be5e
DS
837 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
838
3db7c6e7 839 if (params->passive_fragmented)
fb98be5e
DS
840 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
841
f7b788b4 842 if (params->n_ssids == 0)
fb98be5e
DS
843 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
844
f7b788b4
LC
845#ifdef CONFIG_IWLWIFI_DEBUGFS
846 /* TODO: Check if it's okay to have this in regular scans */
847 if (mvm->scan_iter_notif_enabled)
848 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
849#endif
850
73897bd1 851 cmd->scan_flags |= cpu_to_le32(flags);
fb98be5e 852
f7b788b4 853 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
fb98be5e
DS
854 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
855 MAC_FILTER_IN_BEACON);
f7b788b4 856 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
e2ec4f6d 857 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
5ef766fe
LC
858
859 /* this API uses bits 1-20 instead of 0-19 */
860 ssid_bitmap <<= 1;
fb98be5e 861
f7b788b4 862 cmd->schedule[0].delay = cpu_to_le16(params->interval);
fb98be5e
DS
863 cmd->schedule[0].iterations = 1;
864 cmd->schedule[0].full_scan_mul = 0;
f7b788b4 865 cmd->schedule[1].delay = cpu_to_le16(params->interval);
fb98be5e
DS
866 cmd->schedule[1].iterations = 0;
867 cmd->schedule[1].full_scan_mul = 0;
868
1f9c418f
DS
869 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
870 mvm->last_ebs_successful) {
871 cmd->channel_opt[0].flags =
872 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
873 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
874 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
875 cmd->channel_opt[0].non_ebs_ratio =
876 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
877 cmd->channel_opt[1].flags =
878 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
879 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
880 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
881 cmd->channel_opt[1].non_ebs_ratio =
882 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
883 }
884
f7b788b4
LC
885 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
886 params->n_channels, ssid_bitmap, cmd);
fb98be5e 887
d2496221
DS
888 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
889 mvm->fw->ucode_capa.n_scan_channels);
890
6749dd80 891 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
f7b788b4
LC
892 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
893 params->mac_addr : NULL,
894 params->mac_addr_mask);
fb98be5e 895
3db7c6e7 896 return 0;
fb98be5e
DS
897}
898
f7b788b4
LC
899static int
900iwl_mvm_sched_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
901 struct ieee80211_scan_ies *ies,
902 struct iwl_mvm_scan_params *params)
fb98be5e 903{
65ff556b 904 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
d2496221 905 struct iwl_scan_probe_req *preq;
fb98be5e
DS
906 u32 flags = 0, ssid_bitmap = 0;
907
908 lockdep_assert_held(&mvm->mutex);
909
8df3e68f 910 iwl_mvm_build_generic_scan_cmd(mvm, cmd, params);
fb98be5e 911
f7b788b4 912 cmd->n_channels = (u8)params->n_channels;
fb98be5e 913
f7b788b4 914 cmd->delay = cpu_to_le32(params->delay);
2250fd94 915
f7b788b4 916 if (params->pass_all)
fb98be5e 917 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
7e2a3883
DS
918 else
919 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
fb98be5e 920
f7b788b4 921 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
fb98be5e
DS
922 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
923
8df3e68f 924 if (params->passive_fragmented)
fb98be5e
DS
925 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
926
f7b788b4 927 if (params->n_ssids == 0)
fb98be5e
DS
928 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
929
e5d74646
AB
930#ifdef CONFIG_IWLWIFI_DEBUGFS
931 if (mvm->scan_iter_notif_enabled)
932 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
933#endif
934
73897bd1 935 cmd->scan_flags |= cpu_to_le32(flags);
fb98be5e 936
f7b788b4 937 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
fb98be5e
DS
938 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
939 MAC_FILTER_IN_BEACON);
f7b788b4
LC
940 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
941
e2ec4f6d
LC
942 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
943
944 /* this API uses bits 1-20 instead of 0-19 */
945 ssid_bitmap <<= 1;
fb98be5e 946
f7b788b4 947 cmd->schedule[0].delay = cpu_to_le16(params->interval);
fb98be5e
DS
948 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
949 cmd->schedule[0].full_scan_mul = 1;
950
f7b788b4 951 cmd->schedule[1].delay = cpu_to_le16(params->interval);
fb98be5e
DS
952 cmd->schedule[1].iterations = 0xff;
953 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
954
1f9c418f
DS
955 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
956 mvm->last_ebs_successful) {
957 cmd->channel_opt[0].flags =
958 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
959 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
960 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
961 cmd->channel_opt[0].non_ebs_ratio =
962 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
963 cmd->channel_opt[1].flags =
964 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
965 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
966 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
967 cmd->channel_opt[1].non_ebs_ratio =
968 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
969 }
970
f7b788b4
LC
971 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
972 params->n_channels, ssid_bitmap, cmd);
fb98be5e 973
d2496221
DS
974 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
975 mvm->fw->ucode_capa.n_scan_channels);
976
65ff556b 977 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
f7b788b4
LC
978 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
979 params->mac_addr : NULL,
980 params->mac_addr_mask);
fb98be5e 981
8df3e68f 982 return 0;
fb98be5e
DS
983}
984
985
986int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
987{
d2496221
DS
988 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
989 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
990 true);
991
9af91f46 992 if (!(mvm->scan_status & IWL_MVM_SCAN_REGULAR))
9b520d84
EG
993 return 0;
994
995 if (iwl_mvm_is_radio_killed(mvm)) {
996 ieee80211_scan_completed(mvm->hw, true);
997 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
9af91f46 998 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
9b520d84
EG
999 return 0;
1000 }
1001
1f940386 1002 return iwl_mvm_scan_offload_stop(mvm, true);
fb98be5e 1003}
d2496221
DS
1004
1005/* UMAC scan API */
1006
1007struct iwl_umac_scan_done {
1008 struct iwl_mvm *mvm;
1009 enum iwl_umac_scan_uid_type type;
1010};
1011
1012static int rate_to_scan_rate_flag(unsigned int rate)
1013{
1014 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1015 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1016 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1017 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1018 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1019 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1020 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1021 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1022 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1023 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1024 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1025 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1026 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1027 };
1028
1029 return rate_to_scan_rate[rate];
1030}
1031
1032static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1033{
1034 struct ieee80211_supported_band *band;
1035 unsigned int rates = 0;
1036 int i;
1037
1038 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1039 for (i = 0; i < band->n_bitrates; i++)
1040 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1041 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1042 for (i = 0; i < band->n_bitrates; i++)
1043 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1044
1045 /* Set both basic rates and supported rates */
1046 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1047
1048 return cpu_to_le32(rates);
1049}
1050
1051int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1052{
1053
1054 struct iwl_scan_config *scan_config;
1055 struct ieee80211_supported_band *band;
1056 int num_channels =
1057 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1058 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1059 int ret, i, j = 0, cmd_size, data_size;
1060 struct iwl_host_cmd cmd = {
1061 .id = SCAN_CFG_CMD,
1062 };
1063
1064 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1065 return -ENOBUFS;
1066
1067 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1068
1069 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1070 if (!scan_config)
1071 return -ENOMEM;
1072
1073 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1074 scan_config->hdr.size = cpu_to_le16(data_size);
1075 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1076 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1077 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1078 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1079 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1080 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1081 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1082 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1083 SCAN_CONFIG_N_CHANNELS(num_channels));
a0544272 1084 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
d2496221
DS
1085 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1086 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1087 scan_config->out_of_channel_time = cpu_to_le32(170);
1088 scan_config->suspend_time = cpu_to_le32(30);
1089 scan_config->dwell_active = 20;
1090 scan_config->dwell_passive = 110;
1091 scan_config->dwell_fragmented = 20;
1092
1093 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1094
1095 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1096 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1097 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1098 IWL_CHANNEL_FLAG_EBS_ADD |
1099 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1100
1101 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1102 for (i = 0; i < band->n_channels; i++, j++)
a25d40e2 1103 scan_config->channel_array[j] = band->channels[i].hw_value;
d2496221
DS
1104 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1105 for (i = 0; i < band->n_channels; i++, j++)
a25d40e2 1106 scan_config->channel_array[j] = band->channels[i].hw_value;
d2496221
DS
1107
1108 cmd.data[0] = scan_config;
1109 cmd.len[0] = cmd_size;
1110 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1111
1112 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1113
1114 ret = iwl_mvm_send_cmd(mvm, &cmd);
1115
1116 kfree(scan_config);
1117 return ret;
1118}
1119
1120static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1121{
1122 int i;
1123
507e4cda 1124 for (i = 0; i < mvm->max_scans; i++)
d2496221
DS
1125 if (mvm->scan_uid[i] == uid)
1126 return i;
1127
1128 return i;
1129}
1130
1131static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1132{
1133 return iwl_mvm_find_scan_uid(mvm, 0);
1134}
1135
1136static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1137 enum iwl_umac_scan_uid_type type)
1138{
1139 int i;
1140
507e4cda 1141 for (i = 0; i < mvm->max_scans; i++)
d2496221
DS
1142 if (mvm->scan_uid[i] & type)
1143 return true;
1144
1145 return false;
1146}
1147
963221be
AB
1148static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm,
1149 enum iwl_umac_scan_uid_type type)
1150{
1151 int i;
1152
507e4cda 1153 for (i = 0; i < mvm->max_scans; i++)
963221be
AB
1154 if (mvm->scan_uid[i] & type)
1155 return i;
1156
1157 return i;
1158}
1159
d2496221
DS
1160static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1161 enum iwl_umac_scan_uid_type type)
1162{
1163 u32 uid;
1164
1165 /* make sure exactly one bit is on in scan type */
1166 WARN_ON(hweight8(type) != 1);
1167
1168 /*
1169 * Make sure scan uids are unique. If one scan lasts long time while
1170 * others are completing frequently, the seq number will wrap up and
1171 * we may have more than one scan with the same uid.
1172 */
1173 do {
1174 uid = type | (mvm->scan_seq_num <<
1175 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1176 mvm->scan_seq_num++;
507e4cda 1177 } while (iwl_mvm_find_scan_uid(mvm, uid) < mvm->max_scans);
d2496221
DS
1178
1179 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1180
1181 return uid;
1182}
1183
1184static void
1185iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1186 struct iwl_scan_req_umac *cmd,
1187 struct iwl_mvm_scan_params *params)
1188{
1189 memset(cmd, 0, ksize(cmd));
1190 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1191 sizeof(struct iwl_mvm_umac_cmd_hdr));
1192 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1193 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1194 if (params->passive_fragmented)
1195 cmd->fragmented_dwell =
190f1029 1196 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
d2496221
DS
1197 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1198 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1199 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1200}
1201
1202static void
1203iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1204 struct ieee80211_channel **channels,
1205 int n_channels, u32 ssid_bitmap,
1206 struct iwl_scan_req_umac *cmd)
1207{
1208 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1209 int i;
1210
1211 for (i = 0; i < n_channels; i++) {
1212 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1213 channel_cfg[i].channel_num = channels[i]->hw_value;
1214 channel_cfg[i].iter_count = 1;
1215 channel_cfg[i].iter_interval = 0;
1216 }
1217}
1218
1219static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1220 struct cfg80211_ssid *ssids,
1221 int fragmented)
1222{
1223 int flags = 0;
1224
1225 if (n_ssids == 0)
1226 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1227
1228 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1229 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1230
1231 if (fragmented)
1232 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1233
1234 if (iwl_mvm_rrm_scan_needed(mvm))
1235 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1236
1237 return flags;
1238}
1239
6749dd80 1240static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3db7c6e7
LC
1241 struct ieee80211_scan_ies *ies,
1242 struct iwl_mvm_scan_params *params)
d2496221 1243{
d2496221
DS
1244 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1245 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1246 sizeof(struct iwl_scan_channel_cfg_umac) *
1247 mvm->fw->ucode_capa.n_scan_channels;
d2496221 1248 u32 uid, flags;
c8660dd5 1249 u32 ssid_bitmap = 0;
5ef766fe 1250 int uid_idx;
d2496221
DS
1251
1252 lockdep_assert_held(&mvm->mutex);
1253
1254 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
507e4cda 1255 if (uid_idx >= mvm->max_scans)
d2496221
DS
1256 return -EBUSY;
1257
3db7c6e7 1258 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, params);
d2496221
DS
1259
1260 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1261 mvm->scan_uid[uid_idx] = uid;
1262 cmd->uid = cpu_to_le32(uid);
1263
1264 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1265
f7b788b4
LC
1266 flags = iwl_mvm_scan_umac_common_flags(mvm, params->n_ssids,
1267 params->ssids,
3db7c6e7 1268 params->passive_fragmented);
d2496221 1269
f7b788b4
LC
1270 if (params->pass_all)
1271 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1272 else
1273 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
d2496221
DS
1274
1275 cmd->general_flags = cpu_to_le32(flags);
4db6558c
HD
1276
1277 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
1278 mvm->last_ebs_successful)
1279 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1280 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1281 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1282
f7b788b4 1283 cmd->n_channels = params->n_channels;
d2496221 1284
e2ec4f6d 1285 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
d2496221 1286
f7b788b4
LC
1287 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1288 params->n_channels, ssid_bitmap, cmd);
d2496221
DS
1289
1290 sec_part->schedule[0].iter_count = 1;
1291 sec_part->delay = 0;
1292
6749dd80 1293 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
f7b788b4
LC
1294 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1295 params->mac_addr : NULL,
1296 params->mac_addr_mask);
d2496221 1297
3db7c6e7 1298 return 0;
d2496221
DS
1299}
1300
6749dd80
LC
1301static int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm,
1302 struct ieee80211_vif *vif,
8df3e68f
LC
1303 struct ieee80211_scan_ies *ies,
1304 struct iwl_mvm_scan_params *params)
d2496221 1305{
d2496221
DS
1306 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1307 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1308 sizeof(struct iwl_scan_channel_cfg_umac) *
1309 mvm->fw->ucode_capa.n_scan_channels;
d2496221 1310 u32 uid, flags;
c8660dd5 1311 u32 ssid_bitmap = 0;
8df3e68f 1312 int uid_idx;
d2496221
DS
1313
1314 lockdep_assert_held(&mvm->mutex);
1315
1316 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
507e4cda 1317 if (uid_idx >= mvm->max_scans)
d2496221
DS
1318 return -EBUSY;
1319
8df3e68f 1320 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, params);
d2496221
DS
1321
1322 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1323
1324 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1325 mvm->scan_uid[uid_idx] = uid;
1326 cmd->uid = cpu_to_le32(uid);
1327
1328 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1329
f7b788b4
LC
1330 flags = iwl_mvm_scan_umac_common_flags(mvm, params->n_ssids,
1331 params->ssids,
8df3e68f 1332 params->passive_fragmented);
d2496221
DS
1333
1334 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1335
f7b788b4 1336 if (params->pass_all)
d2496221
DS
1337 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1338 else
1339 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1340
1341 cmd->general_flags = cpu_to_le32(flags);
1342
1343 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1344 mvm->last_ebs_successful)
1345 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1346 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1347 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1348
f7b788b4 1349 cmd->n_channels = params->n_channels;
d2496221 1350
e2ec4f6d 1351 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
d2496221 1352
f7b788b4
LC
1353 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1354 params->n_channels, ssid_bitmap, cmd);
d2496221
DS
1355
1356 sec_part->schedule[0].interval =
f7b788b4 1357 cpu_to_le16(params->interval / MSEC_PER_SEC);
d2496221
DS
1358 sec_part->schedule[0].iter_count = 0xff;
1359
f7b788b4 1360 if (params->delay > U16_MAX) {
2250fd94
LC
1361 IWL_DEBUG_SCAN(mvm,
1362 "delay value is > 16-bits, set to max possible\n");
1363 sec_part->delay = cpu_to_le16(U16_MAX);
1364 } else {
f7b788b4 1365 sec_part->delay = cpu_to_le16(params->delay);
2250fd94 1366 }
d2496221 1367
65ff556b 1368 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
f7b788b4
LC
1369 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1370 params->mac_addr : NULL,
1371 params->mac_addr_mask);
d2496221 1372
8df3e68f 1373 return 0;
d2496221
DS
1374}
1375
6749dd80
LC
1376static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1377{
1378 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1379}
1380
1381static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1382{
1383 /* This looks a bit arbitrary, but the idea is that if we run
1384 * out of possible simultaneous scans and the userspace is
1385 * trying to run a scan type that is already running, we
1386 * return -EBUSY. But if the userspace wants to start a
1387 * different type of scan, we stop the opposite type to make
1388 * space for the new request. The reason is backwards
1389 * compatibility with old wpa_supplicant that wouldn't stop a
1390 * scheduled scan before starting a normal scan.
1391 */
1392
1393 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1394 return 0;
1395
1396 /* Use a switch, even though this is a bitmask, so that more
1397 * than one bits set will fall in default and we will warn.
1398 */
1399 switch (type) {
1400 case IWL_MVM_SCAN_REGULAR:
1401 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1402 return -EBUSY;
1403 return iwl_mvm_scan_offload_stop(mvm, true);
1404 case IWL_MVM_SCAN_SCHED:
1405 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1406 return -EBUSY;
1407 return iwl_mvm_cancel_scan(mvm);
19945dfb
LC
1408 case IWL_MVM_SCAN_NETDETECT:
1409 /* No need to stop anything for net-detect since the
1410 * firmware is restarted anyway. This way, any sched
1411 * scans that were running will be restarted when we
1412 * resume.
1413 */
1414 return 0;
6749dd80
LC
1415 default:
1416 WARN_ON(1);
1417 break;
1418 }
1419
1420 return -EIO;
1421}
1422
1423int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1424 struct cfg80211_scan_request *req,
1425 struct ieee80211_scan_ies *ies)
1426{
3db7c6e7
LC
1427 struct iwl_host_cmd hcmd = {
1428 .len = { iwl_mvm_scan_size(mvm), },
1429 .data = { mvm->scan_cmd, },
1430 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1431 };
1432 struct iwl_mvm_scan_params params = {};
6749dd80
LC
1433 int ret;
1434
1435 lockdep_assert_held(&mvm->mutex);
1436
1437 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1438 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1439 return -EBUSY;
1440 }
1441
1442 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1443 if (ret)
1444 return ret;
1445
1446 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1447
3db7c6e7
LC
1448 /* we should have failed registration if scan_cmd was NULL */
1449 if (WARN_ON(!mvm->scan_cmd))
1450 return -ENOMEM;
1451
1452 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1453 return -ENOBUFS;
1454
f7b788b4
LC
1455 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
1456
1457 params.n_channels = req->n_channels;
1458 params.delay = 0;
1459 params.interval = 0;
1460 params.ssids = req->ssids;
1461 params.channels = req->channels;
1462 params.mac_addr = req->mac_addr;
1463 params.mac_addr_mask = req->mac_addr_mask;
1464 params.no_cck = req->no_cck;
1465 params.pass_all = true;
1466 params.n_match_sets = 0;
1467 params.match_sets = NULL;
3db7c6e7
LC
1468
1469 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1470 hcmd.id = SCAN_REQ_UMAC;
f7b788b4 1471 ret = iwl_mvm_scan_umac(mvm, vif, ies, &params);
3db7c6e7
LC
1472 } else {
1473 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
f7b788b4 1474 ret = iwl_mvm_scan_lmac(mvm, vif, ies, &params);
3db7c6e7
LC
1475 }
1476
1477 if (ret)
1478 return ret;
1479
1480 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1481 if (!ret) {
1482 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1483 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1484 } else {
1485 /* If the scan failed, it usually means that the FW was unable
1486 * to allocate the time events. Warn on it, but maybe we
1487 * should try to send the command again with different params.
1488 */
1489 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1490 }
6749dd80
LC
1491
1492 if (ret)
1493 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1494
1495 return ret;
1496}
1497
65ff556b
LC
1498int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1499 struct ieee80211_vif *vif,
1500 struct cfg80211_sched_scan_request *req,
19945dfb
LC
1501 struct ieee80211_scan_ies *ies,
1502 int type)
65ff556b 1503{
8df3e68f
LC
1504 struct iwl_host_cmd hcmd = {
1505 .len = { iwl_mvm_scan_size(mvm), },
1506 .data = { mvm->scan_cmd, },
1507 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1508 };
1509 struct iwl_mvm_scan_params params = {};
65ff556b
LC
1510 int ret;
1511
6749dd80
LC
1512 lockdep_assert_held(&mvm->mutex);
1513
1514 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1515 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1516 return -EBUSY;
1517 }
1518
19945dfb 1519 ret = iwl_mvm_check_running_scans(mvm, type);
6749dd80
LC
1520 if (ret)
1521 return ret;
1522
8df3e68f
LC
1523 /* we should have failed registration if scan_cmd was NULL */
1524 if (WARN_ON(!mvm->scan_cmd))
1525 return -ENOMEM;
1526
1527 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1528 return -ENOBUFS;
1529
f7b788b4
LC
1530 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
1531
1532 params.n_channels = req->n_channels;
1533 params.delay = req->delay;
1534 params.ssids = req->ssids;
1535 params.channels = req->channels;
1536 params.mac_addr = req->mac_addr;
1537 params.mac_addr_mask = req->mac_addr_mask;
1538 params.no_cck = false;
1539 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1540 params.n_match_sets = req->n_match_sets;
1541 params.match_sets = req->match_sets;
1542
1543 if (req->interval > U16_MAX) {
1544 IWL_DEBUG_SCAN(mvm,
1545 "interval value is > 16-bits, set to max possible\n");
1546 params.interval = U16_MAX;
1547 } else {
1548 params.interval = req->interval / MSEC_PER_SEC;
1549 }
8df3e68f 1550
65ff556b
LC
1551 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1552 if (ret)
1553 return ret;
1554
1555 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
8df3e68f 1556 hcmd.id = SCAN_REQ_UMAC;
f7b788b4 1557 ret = iwl_mvm_sched_scan_umac(mvm, vif, ies, &params);
65ff556b 1558 } else {
8df3e68f 1559 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
f7b788b4 1560 ret = iwl_mvm_sched_scan_lmac(mvm, vif, ies, &params);
8df3e68f
LC
1561 }
1562
1563 if (ret)
1564 return ret;
1565
1566 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1567 if (!ret) {
1568 IWL_DEBUG_SCAN(mvm,
1569 "Sched scan request was sent successfully\n");
1570 mvm->scan_status |= type;
1571 } else {
1572 /* If the scan failed, it usually means that the FW was unable
1573 * to allocate the time events. Warn on it, but maybe we
1574 * should try to send the command again with different params.
1575 */
1576 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
65ff556b
LC
1577 }
1578
1579 return ret;
1580}
1581
d2496221
DS
1582int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1583 struct iwl_rx_cmd_buffer *rxb,
1584 struct iwl_device_cmd *cmd)
1585{
1586 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1587 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1588 u32 uid = __le32_to_cpu(notif->uid);
1589 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1590 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1591
2992a324
DS
1592 /*
1593 * Scan uid may be set to zero in case of scan abort request from above.
1594 */
507e4cda 1595 if (uid_idx >= mvm->max_scans)
d2496221
DS
1596 return 0;
1597
1598 IWL_DEBUG_SCAN(mvm,
1599 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1600 uid, sched ? "sched" : "regular",
1601 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1602 "completed" : "aborted",
1603 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1604 "success" : "failed");
1605
5a4b2afa
HD
1606 if (notif->ebs_status)
1607 mvm->last_ebs_successful = false;
1608
d2496221
DS
1609 mvm->scan_uid[uid_idx] = 0;
1610
1611 if (!sched) {
1612 ieee80211_scan_completed(mvm->hw,
1613 notif->status ==
1614 IWL_SCAN_OFFLOAD_ABORTED);
1615 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1616 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
1617 ieee80211_sched_scan_stopped(mvm->hw);
1618 } else {
1619 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1620 }
1621
1622 return 0;
1623}
1624
1625static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1626 struct iwl_rx_packet *pkt, void *data)
1627{
1628 struct iwl_umac_scan_done *scan_done = data;
1629 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1630 u32 uid = __le32_to_cpu(notif->uid);
1631 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1632
1633 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1634 return false;
1635
507e4cda 1636 if (uid_idx >= scan_done->mvm->max_scans)
d2496221
DS
1637 return false;
1638
1639 /*
1640 * Clear scan uid of scans that was aborted from above and completed
5a4b2afa
HD
1641 * in FW so the RX handler does nothing. Set last_ebs_successful here if
1642 * needed.
d2496221
DS
1643 */
1644 scan_done->mvm->scan_uid[uid_idx] = 0;
1645
5a4b2afa
HD
1646 if (notif->ebs_status)
1647 scan_done->mvm->last_ebs_successful = false;
1648
d2496221
DS
1649 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
1650}
1651
1652static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
1653{
1654 struct iwl_umac_scan_abort cmd = {
1655 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
1656 sizeof(struct iwl_mvm_umac_cmd_hdr)),
1657 .uid = cpu_to_le32(uid),
1658 };
1659
1660 lockdep_assert_held(&mvm->mutex);
1661
1662 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1663
1664 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
1665}
1666
1667static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
1668 enum iwl_umac_scan_uid_type type, bool notify)
1669{
1670 struct iwl_notification_wait wait_scan_done;
1671 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
1672 struct iwl_umac_scan_done scan_done = {
1673 .mvm = mvm,
1674 .type = type,
1675 };
1676 int i, ret = -EIO;
1677
1678 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1679 scan_done_notif,
1680 ARRAY_SIZE(scan_done_notif),
1681 iwl_scan_umac_done_check, &scan_done);
1682
1683 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1684
507e4cda 1685 for (i = 0; i < mvm->max_scans; i++) {
d2496221
DS
1686 if (mvm->scan_uid[i] & type) {
1687 int err;
1688
1689 if (iwl_mvm_is_radio_killed(mvm) &&
1690 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
1691 ieee80211_scan_completed(mvm->hw, true);
1692 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1693 break;
1694 }
1695
1696 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
1697 if (!err)
1698 ret = 0;
1699 }
1700 }
1701
1702 if (ret) {
1703 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
1704 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1705 return ret;
1706 }
1707
1708 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1709 if (ret)
1710 return ret;
1711
1712 if (notify) {
1713 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
1714 ieee80211_sched_scan_stopped(mvm->hw);
1715 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
1716 ieee80211_scan_completed(mvm->hw, true);
1717 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1718 }
1719 }
1720
1721 return ret;
1722}
1723
1724int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1725{
1726 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1727 return sizeof(struct iwl_scan_req_umac) +
1728 sizeof(struct iwl_scan_channel_cfg_umac) *
1729 mvm->fw->ucode_capa.n_scan_channels +
1730 sizeof(struct iwl_scan_req_umac_tail);
1731
65ff556b 1732 return sizeof(struct iwl_scan_req_lmac) +
1f940386
LC
1733 sizeof(struct iwl_scan_channel_cfg_lmac) *
1734 mvm->fw->ucode_capa.n_scan_channels +
1735 sizeof(struct iwl_scan_probe_req);
d2496221 1736}
4ffb3650
DS
1737
1738/*
1739 * This function is used in nic restart flow, to inform mac80211 about scans
1740 * that was aborted by restart flow or by an assert.
1741 */
1742void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1743{
1744 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
963221be
AB
1745 u32 uid, i;
1746
1747 uid = iwl_mvm_find_first_scan(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
507e4cda 1748 if (uid < mvm->max_scans) {
4ffb3650 1749 ieee80211_scan_completed(mvm->hw, true);
963221be
AB
1750 mvm->scan_uid[uid] = 0;
1751 }
1752 uid = iwl_mvm_find_first_scan(mvm,
1753 IWL_UMAC_SCAN_UID_SCHED_SCAN);
507e4cda 1754 if (uid < mvm->max_scans && !mvm->restart_fw) {
4ffb3650 1755 ieee80211_sched_scan_stopped(mvm->hw);
963221be
AB
1756 mvm->scan_uid[uid] = 0;
1757 }
1758
1759 /* We shouldn't have any UIDs still set. Loop over all the
1760 * UIDs to make sure there's nothing left there and warn if
1761 * any is found.
1762 */
507e4cda 1763 for (i = 0; i < mvm->max_scans; i++) {
963221be
AB
1764 if (WARN_ONCE(mvm->scan_uid[i],
1765 "UMAC scan UID %d was not cleaned\n",
1766 mvm->scan_uid[i]))
1767 mvm->scan_uid[i] = 0;
1768 }
4ffb3650 1769 } else {
9af91f46 1770 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
4ffb3650 1771 ieee80211_scan_completed(mvm->hw, true);
9af91f46
LC
1772
1773 /* Sched scan will be restarted by mac80211 in
1774 * restart_hw, so do not report if FW is about to be
1775 * restarted.
1776 */
1777 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1778 ieee80211_sched_scan_stopped(mvm->hw);
4ffb3650
DS
1779 }
1780}