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