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