xref: /openbmc/linux/drivers/net/wireless/intel/iwlwifi/mvm/scan.c (revision b68fc09be48edbc47de1a0f3d42ef8adf6c0ac55)
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  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 
65 #include <linux/etherdevice.h>
66 #include <net/mac80211.h>
67 
68 #include "mvm.h"
69 #include "fw/api/scan.h"
70 #include "iwl-io.h"
71 
72 #define IWL_DENSE_EBS_SCAN_RATIO 5
73 #define IWL_SPARSE_EBS_SCAN_RATIO 1
74 
75 #define IWL_SCAN_DWELL_ACTIVE		10
76 #define IWL_SCAN_DWELL_PASSIVE		110
77 #define IWL_SCAN_DWELL_FRAGMENTED	44
78 #define IWL_SCAN_DWELL_EXTENDED		90
79 #define IWL_SCAN_NUM_OF_FRAGS		3
80 
81 
82 /* adaptive dwell max budget time [TU] for full scan */
83 #define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
84 /* adaptive dwell max budget time [TU] for directed scan */
85 #define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
86 /* adaptive dwell default APs number */
87 #define IWL_SCAN_ADWELL_DEFAULT_N_APS 2
88 /* adaptive dwell default APs number in social channels (1, 6, 11) */
89 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
90 
91 struct iwl_mvm_scan_timing_params {
92 	u32 suspend_time;
93 	u32 max_out_time;
94 };
95 
96 static struct iwl_mvm_scan_timing_params scan_timing[] = {
97 	[IWL_SCAN_TYPE_UNASSOC] = {
98 		.suspend_time = 0,
99 		.max_out_time = 0,
100 	},
101 	[IWL_SCAN_TYPE_WILD] = {
102 		.suspend_time = 30,
103 		.max_out_time = 120,
104 	},
105 	[IWL_SCAN_TYPE_MILD] = {
106 		.suspend_time = 120,
107 		.max_out_time = 120,
108 	},
109 	[IWL_SCAN_TYPE_FRAGMENTED] = {
110 		.suspend_time = 95,
111 		.max_out_time = 44,
112 	},
113 };
114 
115 struct iwl_mvm_scan_params {
116 	/* For CDB this is low band scan type, for non-CDB - type. */
117 	enum iwl_mvm_scan_type type;
118 	enum iwl_mvm_scan_type hb_type;
119 	u32 n_channels;
120 	u16 delay;
121 	int n_ssids;
122 	struct cfg80211_ssid *ssids;
123 	struct ieee80211_channel **channels;
124 	u32 flags;
125 	u8 *mac_addr;
126 	u8 *mac_addr_mask;
127 	bool no_cck;
128 	bool pass_all;
129 	int n_match_sets;
130 	struct iwl_scan_probe_req preq;
131 	struct cfg80211_match_set *match_sets;
132 	int n_scan_plans;
133 	struct cfg80211_sched_scan_plan *scan_plans;
134 	u32 measurement_dwell;
135 };
136 
137 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
138 {
139 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
140 
141 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
142 		return (void *)&cmd->v8.data;
143 
144 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
145 		return (void *)&cmd->v7.data;
146 
147 	if (iwl_mvm_cdb_scan_api(mvm))
148 		return (void *)&cmd->v6.data;
149 
150 	return (void *)&cmd->v1.data;
151 }
152 
153 static inline struct iwl_scan_umac_chan_param *
154 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
155 {
156 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
157 
158 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
159 		return &cmd->v8.channel;
160 
161 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
162 		return &cmd->v7.channel;
163 
164 	if (iwl_mvm_cdb_scan_api(mvm))
165 		return &cmd->v6.channel;
166 
167 	return &cmd->v1.channel;
168 }
169 
170 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
171 {
172 	if (mvm->scan_rx_ant != ANT_NONE)
173 		return mvm->scan_rx_ant;
174 	return iwl_mvm_get_valid_rx_ant(mvm);
175 }
176 
177 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
178 {
179 	u16 rx_chain;
180 	u8 rx_ant;
181 
182 	rx_ant = iwl_mvm_scan_rx_ant(mvm);
183 	rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
184 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
185 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
186 	rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
187 	return cpu_to_le16(rx_chain);
188 }
189 
190 static __le32 iwl_mvm_scan_rxon_flags(enum nl80211_band band)
191 {
192 	if (band == NL80211_BAND_2GHZ)
193 		return cpu_to_le32(PHY_BAND_24);
194 	else
195 		return cpu_to_le32(PHY_BAND_5);
196 }
197 
198 static inline __le32
199 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
200 			  bool no_cck)
201 {
202 	u32 tx_ant;
203 
204 	mvm->scan_last_antenna_idx =
205 		iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
206 				     mvm->scan_last_antenna_idx);
207 	tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
208 
209 	if (band == NL80211_BAND_2GHZ && !no_cck)
210 		return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
211 				   tx_ant);
212 	else
213 		return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
214 }
215 
216 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
217 					    struct ieee80211_vif *vif)
218 {
219 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
220 	int *global_cnt = data;
221 
222 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
223 	    mvmvif->phy_ctxt->id < NUM_PHY_CTX)
224 		*global_cnt += 1;
225 }
226 
227 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
228 {
229 	return mvm->tcm.result.global_load;
230 }
231 
232 static enum iwl_mvm_traffic_load
233 iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
234 {
235 	return mvm->tcm.result.band_load[band];
236 }
237 
238 static enum
239 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm, bool p2p_device,
240 					 enum iwl_mvm_traffic_load load,
241 					 bool low_latency)
242 {
243 	int global_cnt = 0;
244 
245 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
246 					    IEEE80211_IFACE_ITER_NORMAL,
247 					    iwl_mvm_scan_condition_iterator,
248 					    &global_cnt);
249 	if (!global_cnt)
250 		return IWL_SCAN_TYPE_UNASSOC;
251 
252 	if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) && !p2p_device &&
253 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_FRAGMENTED_SCAN))
254 		return IWL_SCAN_TYPE_FRAGMENTED;
255 
256 	if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
257 		return IWL_SCAN_TYPE_MILD;
258 
259 	return IWL_SCAN_TYPE_WILD;
260 }
261 
262 static enum
263 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm, bool p2p_device)
264 {
265 	enum iwl_mvm_traffic_load load;
266 	bool low_latency;
267 
268 	load = iwl_mvm_get_traffic_load(mvm);
269 	low_latency = iwl_mvm_low_latency(mvm);
270 
271 	return _iwl_mvm_get_scan_type(mvm, p2p_device, load, low_latency);
272 }
273 
274 static enum
275 iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
276 					     bool p2p_device,
277 					     enum nl80211_band band)
278 {
279 	enum iwl_mvm_traffic_load load;
280 	bool low_latency;
281 
282 	load = iwl_mvm_get_traffic_load_band(mvm, band);
283 	low_latency = iwl_mvm_low_latency_band(mvm, band);
284 
285 	return _iwl_mvm_get_scan_type(mvm, p2p_device, load, low_latency);
286 }
287 
288 static int
289 iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
290 			      struct cfg80211_scan_request *req,
291 			      struct iwl_mvm_scan_params *params)
292 {
293 	u32 duration = scan_timing[params->type].max_out_time;
294 
295 	if (!req->duration)
296 		return 0;
297 
298 	if (iwl_mvm_is_cdb_supported(mvm)) {
299 		u32 hb_time = scan_timing[params->hb_type].max_out_time;
300 
301 		duration = min_t(u32, duration, hb_time);
302 	}
303 
304 	if (req->duration_mandatory && req->duration > duration) {
305 		IWL_DEBUG_SCAN(mvm,
306 			       "Measurement scan - too long dwell %hu (max out time %u)\n",
307 			       req->duration,
308 			       duration);
309 		return -EOPNOTSUPP;
310 	}
311 
312 	return min_t(u32, (u32)req->duration, duration);
313 }
314 
315 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
316 {
317 	/* require rrm scan whenever the fw supports it */
318 	return fw_has_capa(&mvm->fw->ucode_capa,
319 			   IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
320 }
321 
322 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
323 {
324 	int max_probe_len;
325 
326 	max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
327 
328 	/* we create the 802.11 header and SSID element */
329 	max_probe_len -= 24 + 2;
330 
331 	/* DS parameter set element is added on 2.4GHZ band if required */
332 	if (iwl_mvm_rrm_scan_needed(mvm))
333 		max_probe_len -= 3;
334 
335 	return max_probe_len;
336 }
337 
338 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
339 {
340 	int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
341 
342 	/* TODO: [BUG] This function should return the maximum allowed size of
343 	 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
344 	 * in the same command. So the correct implementation of this function
345 	 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
346 	 * command has only 512 bytes and it would leave us with about 240
347 	 * bytes for scan IEs, which is clearly not enough. So meanwhile
348 	 * we will report an incorrect value. This may result in a failure to
349 	 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
350 	 * functions with -ENOBUFS, if a large enough probe will be provided.
351 	 */
352 	return max_ie_len;
353 }
354 
355 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
356 					      struct iwl_rx_cmd_buffer *rxb)
357 {
358 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
359 	struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
360 
361 	IWL_DEBUG_SCAN(mvm,
362 		       "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
363 		       notif->status, notif->scanned_channels);
364 
365 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
366 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
367 		ieee80211_sched_scan_results(mvm->hw);
368 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
369 	}
370 }
371 
372 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
373 				 struct iwl_rx_cmd_buffer *rxb)
374 {
375 	IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
376 	ieee80211_sched_scan_results(mvm->hw);
377 }
378 
379 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
380 {
381 	switch (status) {
382 	case IWL_SCAN_EBS_SUCCESS:
383 		return "successful";
384 	case IWL_SCAN_EBS_INACTIVE:
385 		return "inactive";
386 	case IWL_SCAN_EBS_FAILED:
387 	case IWL_SCAN_EBS_CHAN_NOT_FOUND:
388 	default:
389 		return "failed";
390 	}
391 }
392 
393 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
394 					 struct iwl_rx_cmd_buffer *rxb)
395 {
396 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
397 	struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
398 	bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
399 
400 	/* If this happens, the firmware has mistakenly sent an LMAC
401 	 * notification during UMAC scans -- warn and ignore it.
402 	 */
403 	if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
404 				     IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
405 		return;
406 
407 	/* scan status must be locked for proper checking */
408 	lockdep_assert_held(&mvm->mutex);
409 
410 	/* We first check if we were stopping a scan, in which case we
411 	 * just clear the stopping flag.  Then we check if it was a
412 	 * firmware initiated stop, in which case we need to inform
413 	 * mac80211.
414 	 * Note that we can have a stopping and a running scan
415 	 * simultaneously, but we can't have two different types of
416 	 * scans stopping or running at the same time (since LMAC
417 	 * doesn't support it).
418 	 */
419 
420 	if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
421 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
422 
423 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
424 			       aborted ? "aborted" : "completed",
425 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
426 		IWL_DEBUG_SCAN(mvm,
427 			       "Last line %d, Last iteration %d, Time after last iteration %d\n",
428 			       scan_notif->last_schedule_line,
429 			       scan_notif->last_schedule_iteration,
430 			       __le32_to_cpu(scan_notif->time_after_last_iter));
431 
432 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
433 	} else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
434 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
435 			       aborted ? "aborted" : "completed",
436 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
437 
438 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
439 	} else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
440 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
441 
442 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
443 			       aborted ? "aborted" : "completed",
444 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
445 		IWL_DEBUG_SCAN(mvm,
446 			       "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
447 			       scan_notif->last_schedule_line,
448 			       scan_notif->last_schedule_iteration,
449 			       __le32_to_cpu(scan_notif->time_after_last_iter));
450 
451 		mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
452 		ieee80211_sched_scan_stopped(mvm->hw);
453 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
454 	} else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
455 		struct cfg80211_scan_info info = {
456 			.aborted = aborted,
457 		};
458 
459 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
460 			       aborted ? "aborted" : "completed",
461 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
462 
463 		mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
464 		ieee80211_scan_completed(mvm->hw, &info);
465 		iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
466 		cancel_delayed_work(&mvm->scan_timeout_dwork);
467 		iwl_mvm_resume_tcm(mvm);
468 	} else {
469 		IWL_ERR(mvm,
470 			"got scan complete notification but no scan is running\n");
471 	}
472 
473 	mvm->last_ebs_successful =
474 			scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
475 			scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
476 }
477 
478 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
479 {
480 	int i;
481 
482 	for (i = 0; i < PROBE_OPTION_MAX; i++) {
483 		if (!ssid_list[i].len)
484 			break;
485 		if (ssid_list[i].len == ssid_len &&
486 		    !memcmp(ssid_list->ssid, ssid, ssid_len))
487 			return i;
488 	}
489 	return -1;
490 }
491 
492 /* We insert the SSIDs in an inverted order, because the FW will
493  * invert it back.
494  */
495 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
496 				 struct iwl_ssid_ie *ssids,
497 				 u32 *ssid_bitmap)
498 {
499 	int i, j;
500 	int index;
501 
502 	/*
503 	 * copy SSIDs from match list.
504 	 * iwl_config_sched_scan_profiles() uses the order of these ssids to
505 	 * config match list.
506 	 */
507 	for (i = 0, j = params->n_match_sets - 1;
508 	     j >= 0 && i < PROBE_OPTION_MAX;
509 	     i++, j--) {
510 		/* skip empty SSID matchsets */
511 		if (!params->match_sets[j].ssid.ssid_len)
512 			continue;
513 		ssids[i].id = WLAN_EID_SSID;
514 		ssids[i].len = params->match_sets[j].ssid.ssid_len;
515 		memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
516 		       ssids[i].len);
517 	}
518 
519 	/* add SSIDs from scan SSID list */
520 	*ssid_bitmap = 0;
521 	for (j = params->n_ssids - 1;
522 	     j >= 0 && i < PROBE_OPTION_MAX;
523 	     i++, j--) {
524 		index = iwl_ssid_exist(params->ssids[j].ssid,
525 				       params->ssids[j].ssid_len,
526 				       ssids);
527 		if (index < 0) {
528 			ssids[i].id = WLAN_EID_SSID;
529 			ssids[i].len = params->ssids[j].ssid_len;
530 			memcpy(ssids[i].ssid, params->ssids[j].ssid,
531 			       ssids[i].len);
532 			*ssid_bitmap |= BIT(i);
533 		} else {
534 			*ssid_bitmap |= BIT(index);
535 		}
536 	}
537 }
538 
539 static int
540 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
541 				   struct cfg80211_sched_scan_request *req)
542 {
543 	struct iwl_scan_offload_profile *profile;
544 	struct iwl_scan_offload_profile_cfg *profile_cfg;
545 	struct iwl_scan_offload_blacklist *blacklist;
546 	struct iwl_host_cmd cmd = {
547 		.id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
548 		.len[1] = sizeof(*profile_cfg),
549 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
550 		.dataflags[1] = IWL_HCMD_DFL_NOCOPY,
551 	};
552 	int blacklist_len;
553 	int i;
554 	int ret;
555 
556 	if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
557 		return -EIO;
558 
559 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
560 		blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
561 	else
562 		blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
563 
564 	blacklist = kcalloc(blacklist_len, sizeof(*blacklist), GFP_KERNEL);
565 	if (!blacklist)
566 		return -ENOMEM;
567 
568 	profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
569 	if (!profile_cfg) {
570 		ret = -ENOMEM;
571 		goto free_blacklist;
572 	}
573 
574 	cmd.data[0] = blacklist;
575 	cmd.len[0] = sizeof(*blacklist) * blacklist_len;
576 	cmd.data[1] = profile_cfg;
577 
578 	/* No blacklist configuration */
579 
580 	profile_cfg->num_profiles = req->n_match_sets;
581 	profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
582 	profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
583 	profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
584 	if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
585 		profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
586 
587 	for (i = 0; i < req->n_match_sets; i++) {
588 		profile = &profile_cfg->profiles[i];
589 		profile->ssid_index = i;
590 		/* Support any cipher and auth algorithm */
591 		profile->unicast_cipher = 0xff;
592 		profile->auth_alg = 0xff;
593 		profile->network_type = IWL_NETWORK_TYPE_ANY;
594 		profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
595 		profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
596 	}
597 
598 	IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
599 
600 	ret = iwl_mvm_send_cmd(mvm, &cmd);
601 	kfree(profile_cfg);
602 free_blacklist:
603 	kfree(blacklist);
604 
605 	return ret;
606 }
607 
608 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
609 				  struct cfg80211_sched_scan_request *req)
610 {
611 	if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
612 		IWL_DEBUG_SCAN(mvm,
613 			       "Sending scheduled scan with filtering, n_match_sets %d\n",
614 			       req->n_match_sets);
615 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
616 		return false;
617 	}
618 
619 	IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
620 
621 	mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
622 	return true;
623 }
624 
625 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
626 {
627 	int ret;
628 	struct iwl_host_cmd cmd = {
629 		.id = SCAN_OFFLOAD_ABORT_CMD,
630 	};
631 	u32 status = CAN_ABORT_STATUS;
632 
633 	ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
634 	if (ret)
635 		return ret;
636 
637 	if (status != CAN_ABORT_STATUS) {
638 		/*
639 		 * The scan abort will return 1 for success or
640 		 * 2 for "failure".  A failure condition can be
641 		 * due to simply not being in an active scan which
642 		 * can occur if we send the scan abort before the
643 		 * microcode has notified us that a scan is completed.
644 		 */
645 		IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
646 		ret = -ENOENT;
647 	}
648 
649 	return ret;
650 }
651 
652 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
653 				     struct iwl_scan_req_tx_cmd *tx_cmd,
654 				     bool no_cck)
655 {
656 	tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
657 					 TX_CMD_FLG_BT_DIS);
658 	tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
659 							   NL80211_BAND_2GHZ,
660 							   no_cck);
661 	tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
662 
663 	tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
664 					 TX_CMD_FLG_BT_DIS);
665 	tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
666 							   NL80211_BAND_5GHZ,
667 							   no_cck);
668 	tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
669 }
670 
671 static void
672 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
673 			       struct ieee80211_channel **channels,
674 			       int n_channels, u32 ssid_bitmap,
675 			       struct iwl_scan_req_lmac *cmd)
676 {
677 	struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
678 	int i;
679 
680 	for (i = 0; i < n_channels; i++) {
681 		channel_cfg[i].channel_num =
682 			cpu_to_le16(channels[i]->hw_value);
683 		channel_cfg[i].iter_count = cpu_to_le16(1);
684 		channel_cfg[i].iter_interval = 0;
685 		channel_cfg[i].flags =
686 			cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
687 				    ssid_bitmap);
688 	}
689 }
690 
691 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
692 					   size_t len, u8 *const pos)
693 {
694 	static const u8 before_ds_params[] = {
695 			WLAN_EID_SSID,
696 			WLAN_EID_SUPP_RATES,
697 			WLAN_EID_REQUEST,
698 			WLAN_EID_EXT_SUPP_RATES,
699 	};
700 	size_t offs;
701 	u8 *newpos = pos;
702 
703 	if (!iwl_mvm_rrm_scan_needed(mvm)) {
704 		memcpy(newpos, ies, len);
705 		return newpos + len;
706 	}
707 
708 	offs = ieee80211_ie_split(ies, len,
709 				  before_ds_params,
710 				  ARRAY_SIZE(before_ds_params),
711 				  0);
712 
713 	memcpy(newpos, ies, offs);
714 	newpos += offs;
715 
716 	/* Add a placeholder for DS Parameter Set element */
717 	*newpos++ = WLAN_EID_DS_PARAMS;
718 	*newpos++ = 1;
719 	*newpos++ = 0;
720 
721 	memcpy(newpos, ies + offs, len - offs);
722 	newpos += len - offs;
723 
724 	return newpos;
725 }
726 
727 #define WFA_TPC_IE_LEN	9
728 
729 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
730 {
731 	pos[0] = WLAN_EID_VENDOR_SPECIFIC;
732 	pos[1] = WFA_TPC_IE_LEN - 2;
733 	pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
734 	pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
735 	pos[4] = WLAN_OUI_MICROSOFT & 0xff;
736 	pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
737 	pos[6] = 0;
738 	/* pos[7] - tx power will be inserted by the FW */
739 	pos[7] = 0;
740 	pos[8] = 0;
741 }
742 
743 static void
744 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
745 			 struct ieee80211_scan_ies *ies,
746 			 struct iwl_mvm_scan_params *params)
747 {
748 	struct ieee80211_mgmt *frame = (void *)params->preq.buf;
749 	u8 *pos, *newpos;
750 	const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
751 		params->mac_addr : NULL;
752 
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,
761 				     params->mac_addr_mask);
762 	else
763 		memcpy(frame->sa, vif->addr, ETH_ALEN);
764 
765 	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
766 	eth_broadcast_addr(frame->da);
767 	eth_broadcast_addr(frame->bssid);
768 	frame->seq_ctrl = 0;
769 
770 	pos = frame->u.probe_req.variable;
771 	*pos++ = WLAN_EID_SSID;
772 	*pos++ = 0;
773 
774 	params->preq.mac_header.offset = 0;
775 	params->preq.mac_header.len = cpu_to_le16(24 + 2);
776 
777 	/* Insert ds parameter set element on 2.4 GHz band */
778 	newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
779 						 ies->ies[NL80211_BAND_2GHZ],
780 						 ies->len[NL80211_BAND_2GHZ],
781 						 pos);
782 	params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
783 	params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
784 	pos = newpos;
785 
786 	memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
787 	       ies->len[NL80211_BAND_5GHZ]);
788 	params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
789 	params->preq.band_data[1].len =
790 		cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
791 	pos += ies->len[NL80211_BAND_5GHZ];
792 
793 	memcpy(pos, ies->common_ies, ies->common_ie_len);
794 	params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
795 
796 	if (iwl_mvm_rrm_scan_needed(mvm) &&
797 	    !fw_has_capa(&mvm->fw->ucode_capa,
798 			 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
799 		iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
800 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
801 							   WFA_TPC_IE_LEN);
802 	} else {
803 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
804 	}
805 }
806 
807 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
808 				    struct iwl_scan_req_lmac *cmd,
809 				    struct iwl_mvm_scan_params *params)
810 {
811 	cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
812 	cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
813 	cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
814 	cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
815 	cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
816 	cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
817 	cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
818 }
819 
820 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
821 				     struct ieee80211_scan_ies *ies,
822 				     int n_channels)
823 {
824 	return ((n_ssids <= PROBE_OPTION_MAX) &&
825 		(n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
826 		(ies->common_ie_len +
827 		 ies->len[NL80211_BAND_2GHZ] +
828 		 ies->len[NL80211_BAND_5GHZ] <=
829 		 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
830 }
831 
832 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
833 					struct ieee80211_vif *vif)
834 {
835 	const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
836 	bool low_latency;
837 
838 	if (iwl_mvm_is_cdb_supported(mvm))
839 		low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
840 	else
841 		low_latency = iwl_mvm_low_latency(mvm);
842 
843 	/* We can only use EBS if:
844 	 *	1. the feature is supported;
845 	 *	2. the last EBS was successful;
846 	 *	3. if only single scan, the single scan EBS API is supported;
847 	 *	4. it's not a p2p find operation.
848 	 *	5. we are not in low latency mode,
849 	 *	   or if fragmented ebs is supported by the FW
850 	 */
851 	return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
852 		mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
853 		vif->type != NL80211_IFTYPE_P2P_DEVICE &&
854 		(!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
855 }
856 
857 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
858 {
859 	return params->n_scan_plans == 1 &&
860 		params->scan_plans[0].iterations == 1;
861 }
862 
863 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
864 				   struct iwl_mvm_scan_params *params,
865 				   struct ieee80211_vif *vif)
866 {
867 	int flags = 0;
868 
869 	if (params->n_ssids == 0)
870 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
871 
872 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
873 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
874 
875 	if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
876 		flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
877 
878 	if (iwl_mvm_rrm_scan_needed(mvm) &&
879 	    fw_has_capa(&mvm->fw->ucode_capa,
880 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
881 		flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
882 
883 	if (params->pass_all)
884 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
885 	else
886 		flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
887 
888 #ifdef CONFIG_IWLWIFI_DEBUGFS
889 	if (mvm->scan_iter_notif_enabled)
890 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
891 #endif
892 
893 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
894 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
895 
896 	if (iwl_mvm_is_regular_scan(params) &&
897 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
898 	    params->type != IWL_SCAN_TYPE_FRAGMENTED)
899 		flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
900 
901 	return flags;
902 }
903 
904 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
905 			     struct iwl_mvm_scan_params *params)
906 {
907 	struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
908 	struct iwl_scan_probe_req *preq =
909 		(void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
910 			 mvm->fw->ucode_capa.n_scan_channels);
911 	u32 ssid_bitmap = 0;
912 	int i;
913 
914 	lockdep_assert_held(&mvm->mutex);
915 
916 	memset(cmd, 0, ksize(cmd));
917 
918 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
919 		return -EINVAL;
920 
921 	iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
922 
923 	cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
924 	cmd->iter_num = cpu_to_le32(1);
925 	cmd->n_channels = (u8)params->n_channels;
926 
927 	cmd->delay = cpu_to_le32(params->delay);
928 
929 	cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
930 							      vif));
931 
932 	cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
933 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
934 					MAC_FILTER_IN_BEACON);
935 	iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
936 	iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
937 
938 	/* this API uses bits 1-20 instead of 0-19 */
939 	ssid_bitmap <<= 1;
940 
941 	for (i = 0; i < params->n_scan_plans; i++) {
942 		struct cfg80211_sched_scan_plan *scan_plan =
943 			&params->scan_plans[i];
944 
945 		cmd->schedule[i].delay =
946 			cpu_to_le16(scan_plan->interval);
947 		cmd->schedule[i].iterations = scan_plan->iterations;
948 		cmd->schedule[i].full_scan_mul = 1;
949 	}
950 
951 	/*
952 	 * If the number of iterations of the last scan plan is set to
953 	 * zero, it should run infinitely. However, this is not always the case.
954 	 * For example, when regular scan is requested the driver sets one scan
955 	 * plan with one iteration.
956 	 */
957 	if (!cmd->schedule[i - 1].iterations)
958 		cmd->schedule[i - 1].iterations = 0xff;
959 
960 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
961 		cmd->channel_opt[0].flags =
962 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
963 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
964 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
965 		cmd->channel_opt[0].non_ebs_ratio =
966 			cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
967 		cmd->channel_opt[1].flags =
968 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
969 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
970 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
971 		cmd->channel_opt[1].non_ebs_ratio =
972 			cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
973 	}
974 
975 	iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
976 				       params->n_channels, ssid_bitmap, cmd);
977 
978 	*preq = params->preq;
979 
980 	return 0;
981 }
982 
983 static int rate_to_scan_rate_flag(unsigned int rate)
984 {
985 	static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
986 		[IWL_RATE_1M_INDEX]	= SCAN_CONFIG_RATE_1M,
987 		[IWL_RATE_2M_INDEX]	= SCAN_CONFIG_RATE_2M,
988 		[IWL_RATE_5M_INDEX]	= SCAN_CONFIG_RATE_5M,
989 		[IWL_RATE_11M_INDEX]	= SCAN_CONFIG_RATE_11M,
990 		[IWL_RATE_6M_INDEX]	= SCAN_CONFIG_RATE_6M,
991 		[IWL_RATE_9M_INDEX]	= SCAN_CONFIG_RATE_9M,
992 		[IWL_RATE_12M_INDEX]	= SCAN_CONFIG_RATE_12M,
993 		[IWL_RATE_18M_INDEX]	= SCAN_CONFIG_RATE_18M,
994 		[IWL_RATE_24M_INDEX]	= SCAN_CONFIG_RATE_24M,
995 		[IWL_RATE_36M_INDEX]	= SCAN_CONFIG_RATE_36M,
996 		[IWL_RATE_48M_INDEX]	= SCAN_CONFIG_RATE_48M,
997 		[IWL_RATE_54M_INDEX]	= SCAN_CONFIG_RATE_54M,
998 	};
999 
1000 	return rate_to_scan_rate[rate];
1001 }
1002 
1003 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1004 {
1005 	struct ieee80211_supported_band *band;
1006 	unsigned int rates = 0;
1007 	int i;
1008 
1009 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1010 	for (i = 0; i < band->n_bitrates; i++)
1011 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1012 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1013 	for (i = 0; i < band->n_bitrates; i++)
1014 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1015 
1016 	/* Set both basic rates and supported rates */
1017 	rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1018 
1019 	return cpu_to_le32(rates);
1020 }
1021 
1022 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1023 				    struct iwl_scan_dwell *dwell)
1024 {
1025 	dwell->active = IWL_SCAN_DWELL_ACTIVE;
1026 	dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1027 	dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1028 	dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1029 }
1030 
1031 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels)
1032 {
1033 	struct ieee80211_supported_band *band;
1034 	int i, j = 0;
1035 
1036 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1037 	for (i = 0; i < band->n_channels; i++, j++)
1038 		channels[j] = band->channels[i].hw_value;
1039 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1040 	for (i = 0; i < band->n_channels; i++, j++)
1041 		channels[j] = band->channels[i].hw_value;
1042 }
1043 
1044 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1045 					u32 flags, u8 channel_flags)
1046 {
1047 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
1048 	struct iwl_scan_config_v1 *cfg = config;
1049 
1050 	cfg->flags = cpu_to_le32(flags);
1051 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1052 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1053 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1054 	cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1055 	cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1056 
1057 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1058 
1059 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1060 
1061 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1062 	cfg->channel_flags = channel_flags;
1063 
1064 	iwl_mvm_fill_channels(mvm, cfg->channel_array);
1065 }
1066 
1067 static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
1068 				     u32 flags, u8 channel_flags)
1069 {
1070 	struct iwl_scan_config *cfg = config;
1071 
1072 	cfg->flags = cpu_to_le32(flags);
1073 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1074 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1075 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1076 
1077 	if (iwl_mvm_is_cdb_supported(mvm)) {
1078 		enum iwl_mvm_scan_type lb_type, hb_type;
1079 
1080 		lb_type = iwl_mvm_get_scan_type_band(mvm, false,
1081 						     NL80211_BAND_2GHZ);
1082 		hb_type = iwl_mvm_get_scan_type_band(mvm, false,
1083 						     NL80211_BAND_5GHZ);
1084 
1085 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1086 			cpu_to_le32(scan_timing[lb_type].max_out_time);
1087 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1088 			cpu_to_le32(scan_timing[lb_type].suspend_time);
1089 
1090 		cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1091 			cpu_to_le32(scan_timing[hb_type].max_out_time);
1092 		cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1093 			cpu_to_le32(scan_timing[hb_type].suspend_time);
1094 	} else {
1095 		enum iwl_mvm_scan_type type =
1096 			iwl_mvm_get_scan_type(mvm, false);
1097 
1098 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1099 			cpu_to_le32(scan_timing[type].max_out_time);
1100 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1101 			cpu_to_le32(scan_timing[type].suspend_time);
1102 	}
1103 
1104 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1105 
1106 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1107 
1108 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1109 	cfg->channel_flags = channel_flags;
1110 
1111 	iwl_mvm_fill_channels(mvm, cfg->channel_array);
1112 }
1113 
1114 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1115 {
1116 	void *cfg;
1117 	int ret, cmd_size;
1118 	struct iwl_host_cmd cmd = {
1119 		.id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1120 	};
1121 	enum iwl_mvm_scan_type type;
1122 	enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1123 	int num_channels =
1124 		mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1125 		mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1126 	u32 flags;
1127 	u8 channel_flags;
1128 
1129 	if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1130 		return -ENOBUFS;
1131 
1132 	if (iwl_mvm_is_cdb_supported(mvm)) {
1133 		type = iwl_mvm_get_scan_type_band(mvm, false,
1134 						  NL80211_BAND_2GHZ);
1135 		hb_type = iwl_mvm_get_scan_type_band(mvm, false,
1136 						     NL80211_BAND_5GHZ);
1137 		if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1138 			return 0;
1139 	} else {
1140 		type = iwl_mvm_get_scan_type(mvm, false);
1141 		if (type == mvm->scan_type)
1142 			return 0;
1143 	}
1144 
1145 	if (iwl_mvm_cdb_scan_api(mvm))
1146 		cmd_size = sizeof(struct iwl_scan_config);
1147 	else
1148 		cmd_size = sizeof(struct iwl_scan_config_v1);
1149 	cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1150 
1151 	cfg = kzalloc(cmd_size, GFP_KERNEL);
1152 	if (!cfg)
1153 		return -ENOMEM;
1154 
1155 	flags = SCAN_CONFIG_FLAG_ACTIVATE |
1156 		 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1157 		 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1158 		 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1159 		 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1160 		 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1161 		 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1162 		 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1163 		 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1164 		 SCAN_CONFIG_N_CHANNELS(num_channels) |
1165 		 (type == IWL_SCAN_TYPE_FRAGMENTED ?
1166 		  SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1167 		  SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1168 
1169 	channel_flags = IWL_CHANNEL_FLAG_EBS |
1170 			IWL_CHANNEL_FLAG_ACCURATE_EBS |
1171 			IWL_CHANNEL_FLAG_EBS_ADD |
1172 			IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1173 
1174 	/*
1175 	 * Check for fragmented scan on LMAC2 - high band.
1176 	 * LMAC1 - low band is checked above.
1177 	 */
1178 	if (iwl_mvm_cdb_scan_api(mvm)) {
1179 		if (iwl_mvm_is_cdb_supported(mvm))
1180 			flags |= (hb_type == IWL_SCAN_TYPE_FRAGMENTED) ?
1181 				 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1182 				 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1183 		iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags);
1184 	} else {
1185 		iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags);
1186 	}
1187 
1188 	cmd.data[0] = cfg;
1189 	cmd.len[0] = cmd_size;
1190 	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1191 
1192 	IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1193 
1194 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1195 	if (!ret) {
1196 		mvm->scan_type = type;
1197 		mvm->hb_scan_type = hb_type;
1198 	}
1199 
1200 	kfree(cfg);
1201 	return ret;
1202 }
1203 
1204 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1205 {
1206 	int i;
1207 
1208 	for (i = 0; i < mvm->max_scans; i++)
1209 		if (mvm->scan_uid_status[i] == status)
1210 			return i;
1211 
1212 	return -ENOENT;
1213 }
1214 
1215 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1216 				    struct iwl_scan_req_umac *cmd,
1217 				    struct iwl_mvm_scan_params *params)
1218 {
1219 	struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1220 	u8 active_dwell, passive_dwell;
1221 
1222 	timing = &scan_timing[params->type];
1223 	active_dwell = params->measurement_dwell ?
1224 		params->measurement_dwell : IWL_SCAN_DWELL_ACTIVE;
1225 	passive_dwell = params->measurement_dwell ?
1226 		params->measurement_dwell : IWL_SCAN_DWELL_PASSIVE;
1227 
1228 	if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1229 		cmd->v7.adwell_default_n_aps_social =
1230 			IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1231 		cmd->v7.adwell_default_n_aps =
1232 			IWL_SCAN_ADWELL_DEFAULT_N_APS;
1233 
1234 		/* if custom max budget was configured with debugfs */
1235 		if (IWL_MVM_ADWELL_MAX_BUDGET)
1236 			cmd->v7.adwell_max_budget =
1237 				cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1238 		else if (params->ssids && params->ssids[0].ssid_len)
1239 			cmd->v7.adwell_max_budget =
1240 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1241 		else
1242 			cmd->v7.adwell_max_budget =
1243 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1244 
1245 		cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1246 		cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1247 			cpu_to_le32(timing->max_out_time);
1248 		cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1249 			cpu_to_le32(timing->suspend_time);
1250 
1251 		if (iwl_mvm_is_cdb_supported(mvm)) {
1252 			hb_timing = &scan_timing[params->hb_type];
1253 
1254 			cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1255 				cpu_to_le32(hb_timing->max_out_time);
1256 			cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1257 				cpu_to_le32(hb_timing->suspend_time);
1258 		}
1259 
1260 		if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1261 			cmd->v7.active_dwell = active_dwell;
1262 			cmd->v7.passive_dwell = passive_dwell;
1263 			cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1264 		} else {
1265 			cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1266 			cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1267 			if (iwl_mvm_is_cdb_supported(mvm)) {
1268 				cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1269 					active_dwell;
1270 				cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1271 					passive_dwell;
1272 			}
1273 		}
1274 	} else {
1275 		cmd->v1.extended_dwell = params->measurement_dwell ?
1276 			params->measurement_dwell : IWL_SCAN_DWELL_EXTENDED;
1277 		cmd->v1.active_dwell = active_dwell;
1278 		cmd->v1.passive_dwell = passive_dwell;
1279 		cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1280 
1281 		if (iwl_mvm_is_cdb_supported(mvm)) {
1282 			hb_timing = &scan_timing[params->hb_type];
1283 
1284 			cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1285 					cpu_to_le32(hb_timing->max_out_time);
1286 			cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1287 					cpu_to_le32(hb_timing->suspend_time);
1288 		}
1289 
1290 		if (iwl_mvm_cdb_scan_api(mvm)) {
1291 			cmd->v6.scan_priority =
1292 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1293 			cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1294 				cpu_to_le32(timing->max_out_time);
1295 			cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1296 				cpu_to_le32(timing->suspend_time);
1297 		} else {
1298 			cmd->v1.scan_priority =
1299 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1300 			cmd->v1.max_out_time =
1301 				cpu_to_le32(timing->max_out_time);
1302 			cmd->v1.suspend_time =
1303 				cpu_to_le32(timing->suspend_time);
1304 		}
1305 	}
1306 
1307 	if (iwl_mvm_is_regular_scan(params))
1308 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1309 	else
1310 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1311 }
1312 
1313 static void
1314 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1315 			       struct ieee80211_channel **channels,
1316 			       int n_channels, u32 ssid_bitmap,
1317 			       struct iwl_scan_channel_cfg_umac *channel_cfg)
1318 {
1319 	int i;
1320 
1321 	for (i = 0; i < n_channels; i++) {
1322 		channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1323 		channel_cfg[i].channel_num = channels[i]->hw_value;
1324 		channel_cfg[i].iter_count = 1;
1325 		channel_cfg[i].iter_interval = 0;
1326 	}
1327 }
1328 
1329 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1330 				   struct iwl_mvm_scan_params *params,
1331 				   struct ieee80211_vif *vif)
1332 {
1333 	u16 flags = 0;
1334 
1335 	if (params->n_ssids == 0)
1336 		flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1337 
1338 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1339 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1340 
1341 	if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
1342 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1343 
1344 	if (iwl_mvm_is_cdb_supported(mvm) &&
1345 	    params->hb_type == IWL_SCAN_TYPE_FRAGMENTED)
1346 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1347 
1348 	if (iwl_mvm_rrm_scan_needed(mvm) &&
1349 	    fw_has_capa(&mvm->fw->ucode_capa,
1350 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1351 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1352 
1353 	if (params->pass_all)
1354 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1355 	else
1356 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1357 
1358 	if (!iwl_mvm_is_regular_scan(params))
1359 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1360 
1361 	if (params->measurement_dwell)
1362 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1363 
1364 #ifdef CONFIG_IWLWIFI_DEBUGFS
1365 	if (mvm->scan_iter_notif_enabled)
1366 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1367 #endif
1368 
1369 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1370 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1371 
1372 	if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE &&
1373 	    vif->type != NL80211_IFTYPE_P2P_DEVICE)
1374 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1375 
1376 	/*
1377 	 * Extended dwell is relevant only for low band to start with, as it is
1378 	 * being used for social channles only (1, 6, 11), so we can check
1379 	 * only scan type on low band also for CDB.
1380 	 */
1381 	if (iwl_mvm_is_regular_scan(params) &&
1382 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1383 	    params->type != IWL_SCAN_TYPE_FRAGMENTED &&
1384 	    !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1385 	    !iwl_mvm_is_oce_supported(mvm))
1386 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1387 
1388 	if (iwl_mvm_is_oce_supported(mvm)) {
1389 		if ((params->flags &
1390 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1391 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1392 		/* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1393 		 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1394 		 * the same bit, we need to make sure that we use this bit here
1395 		 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1396 		 * used. */
1397 		if ((params->flags &
1398 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1399 		     !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1400 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1401 		if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1402 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1403 	}
1404 
1405 	return flags;
1406 }
1407 
1408 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1409 			     struct iwl_mvm_scan_params *params,
1410 			     int type)
1411 {
1412 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1413 	struct iwl_scan_umac_chan_param *chan_param;
1414 	void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
1415 	struct iwl_scan_req_umac_tail *sec_part = cmd_data +
1416 		sizeof(struct iwl_scan_channel_cfg_umac) *
1417 			mvm->fw->ucode_capa.n_scan_channels;
1418 	int uid, i;
1419 	u32 ssid_bitmap = 0;
1420 	u8 channel_flags = 0;
1421 	u16 gen_flags;
1422 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1423 
1424 	chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
1425 
1426 	lockdep_assert_held(&mvm->mutex);
1427 
1428 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1429 		return -EINVAL;
1430 
1431 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1432 	if (uid < 0)
1433 		return uid;
1434 
1435 	memset(cmd, 0, ksize(cmd));
1436 
1437 	iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1438 
1439 	mvm->scan_uid_status[uid] = type;
1440 
1441 	cmd->uid = cpu_to_le32(uid);
1442 	gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
1443 	cmd->general_flags = cpu_to_le16(gen_flags);
1444 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1445 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
1446 			cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
1447 							IWL_SCAN_NUM_OF_FRAGS;
1448 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
1449 			cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
1450 							IWL_SCAN_NUM_OF_FRAGS;
1451 	}
1452 
1453 	cmd->scan_start_mac_id = scan_vif->id;
1454 
1455 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1456 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1457 
1458 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1459 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1460 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1461 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1462 
1463 		/* set fragmented ebs for fragmented scan on HB channels */
1464 		if (iwl_mvm_is_frag_ebs_supported(mvm)) {
1465 			if (gen_flags &
1466 			    IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
1467 			    (!iwl_mvm_is_cdb_supported(mvm) &&
1468 			     gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
1469 				channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1470 		}
1471 	}
1472 
1473 	chan_param->flags = channel_flags;
1474 	chan_param->count = params->n_channels;
1475 
1476 	iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1477 
1478 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1479 				       params->n_channels, ssid_bitmap,
1480 				       cmd_data);
1481 
1482 	for (i = 0; i < params->n_scan_plans; i++) {
1483 		struct cfg80211_sched_scan_plan *scan_plan =
1484 			&params->scan_plans[i];
1485 
1486 		sec_part->schedule[i].iter_count = scan_plan->iterations;
1487 		sec_part->schedule[i].interval =
1488 			cpu_to_le16(scan_plan->interval);
1489 	}
1490 
1491 	/*
1492 	 * If the number of iterations of the last scan plan is set to
1493 	 * zero, it should run infinitely. However, this is not always the case.
1494 	 * For example, when regular scan is requested the driver sets one scan
1495 	 * plan with one iteration.
1496 	 */
1497 	if (!sec_part->schedule[i - 1].iter_count)
1498 		sec_part->schedule[i - 1].iter_count = 0xff;
1499 
1500 	sec_part->delay = cpu_to_le16(params->delay);
1501 	sec_part->preq = params->preq;
1502 
1503 	return 0;
1504 }
1505 
1506 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1507 {
1508 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1509 }
1510 
1511 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1512 {
1513 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1514 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1515 
1516 	/* This looks a bit arbitrary, but the idea is that if we run
1517 	 * out of possible simultaneous scans and the userspace is
1518 	 * trying to run a scan type that is already running, we
1519 	 * return -EBUSY.  But if the userspace wants to start a
1520 	 * different type of scan, we stop the opposite type to make
1521 	 * space for the new request.  The reason is backwards
1522 	 * compatibility with old wpa_supplicant that wouldn't stop a
1523 	 * scheduled scan before starting a normal scan.
1524 	 */
1525 
1526 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1527 		return 0;
1528 
1529 	/* Use a switch, even though this is a bitmask, so that more
1530 	 * than one bits set will fall in default and we will warn.
1531 	 */
1532 	switch (type) {
1533 	case IWL_MVM_SCAN_REGULAR:
1534 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1535 			return -EBUSY;
1536 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1537 	case IWL_MVM_SCAN_SCHED:
1538 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1539 			return -EBUSY;
1540 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1541 	case IWL_MVM_SCAN_NETDETECT:
1542 		/* For non-unified images, there's no need to stop
1543 		 * anything for net-detect since the firmware is
1544 		 * restarted anyway.  This way, any sched scans that
1545 		 * were running will be restarted when we resume.
1546 		 */
1547 		if (!unified_image)
1548 			return 0;
1549 
1550 		/* If this is a unified image and we ran out of scans,
1551 		 * we need to stop something.  Prefer stopping regular
1552 		 * scans, because the results are useless at this
1553 		 * point, and we should be able to keep running
1554 		 * another scheduled scan while suspended.
1555 		 */
1556 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1557 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
1558 						 true);
1559 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1560 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
1561 						 true);
1562 
1563 		/* fall through, something is wrong if no scan was
1564 		 * running but we ran out of scans.
1565 		 */
1566 	default:
1567 		WARN_ON(1);
1568 		break;
1569 	}
1570 
1571 	return -EIO;
1572 }
1573 
1574 #define SCAN_TIMEOUT 20000
1575 
1576 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
1577 {
1578 	struct delayed_work *delayed_work = to_delayed_work(work);
1579 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1580 					   scan_timeout_dwork);
1581 
1582 	IWL_ERR(mvm, "regular scan timed out\n");
1583 
1584 	iwl_force_nmi(mvm->trans);
1585 }
1586 
1587 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
1588 				   struct iwl_mvm_scan_params *params,
1589 				   bool p2p)
1590 {
1591 	if (iwl_mvm_is_cdb_supported(mvm)) {
1592 		params->type =
1593 			iwl_mvm_get_scan_type_band(mvm, p2p,
1594 						   NL80211_BAND_2GHZ);
1595 		params->hb_type =
1596 			iwl_mvm_get_scan_type_band(mvm, p2p,
1597 						   NL80211_BAND_5GHZ);
1598 	} else {
1599 		params->type = iwl_mvm_get_scan_type(mvm, p2p);
1600 	}
1601 }
1602 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1603 			   struct cfg80211_scan_request *req,
1604 			   struct ieee80211_scan_ies *ies)
1605 {
1606 	struct iwl_host_cmd hcmd = {
1607 		.len = { iwl_mvm_scan_size(mvm), },
1608 		.data = { mvm->scan_cmd, },
1609 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1610 	};
1611 	struct iwl_mvm_scan_params params = {};
1612 	int ret;
1613 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
1614 
1615 	lockdep_assert_held(&mvm->mutex);
1616 
1617 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1618 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1619 		return -EBUSY;
1620 	}
1621 
1622 	ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1623 	if (ret)
1624 		return ret;
1625 
1626 	/* we should have failed registration if scan_cmd was NULL */
1627 	if (WARN_ON(!mvm->scan_cmd))
1628 		return -ENOMEM;
1629 
1630 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1631 		return -ENOBUFS;
1632 
1633 	params.n_ssids = req->n_ssids;
1634 	params.flags = req->flags;
1635 	params.n_channels = req->n_channels;
1636 	params.delay = 0;
1637 	params.ssids = req->ssids;
1638 	params.channels = req->channels;
1639 	params.mac_addr = req->mac_addr;
1640 	params.mac_addr_mask = req->mac_addr_mask;
1641 	params.no_cck = req->no_cck;
1642 	params.pass_all = true;
1643 	params.n_match_sets = 0;
1644 	params.match_sets = NULL;
1645 
1646 	params.scan_plans = &scan_plan;
1647 	params.n_scan_plans = 1;
1648 
1649 	iwl_mvm_fill_scan_type(mvm, &params,
1650 			       vif->type == NL80211_IFTYPE_P2P_DEVICE);
1651 
1652 	ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
1653 	if (ret < 0)
1654 		return ret;
1655 
1656 	params.measurement_dwell = ret;
1657 
1658 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1659 
1660 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1661 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1662 		ret = iwl_mvm_scan_umac(mvm, vif, &params,
1663 					IWL_MVM_SCAN_REGULAR);
1664 	} else {
1665 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1666 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1667 	}
1668 
1669 	if (ret)
1670 		return ret;
1671 
1672 	iwl_mvm_pause_tcm(mvm, false);
1673 
1674 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1675 	if (ret) {
1676 		/* If the scan failed, it usually means that the FW was unable
1677 		 * to allocate the time events. Warn on it, but maybe we
1678 		 * should try to send the command again with different params.
1679 		 */
1680 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1681 		iwl_mvm_resume_tcm(mvm);
1682 		return ret;
1683 	}
1684 
1685 	IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1686 	mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1687 	mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
1688 	iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1689 
1690 	schedule_delayed_work(&mvm->scan_timeout_dwork,
1691 			      msecs_to_jiffies(SCAN_TIMEOUT));
1692 
1693 	return 0;
1694 }
1695 
1696 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1697 			     struct ieee80211_vif *vif,
1698 			     struct cfg80211_sched_scan_request *req,
1699 			     struct ieee80211_scan_ies *ies,
1700 			     int type)
1701 {
1702 	struct iwl_host_cmd hcmd = {
1703 		.len = { iwl_mvm_scan_size(mvm), },
1704 		.data = { mvm->scan_cmd, },
1705 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1706 	};
1707 	struct iwl_mvm_scan_params params = {};
1708 	int ret;
1709 
1710 	lockdep_assert_held(&mvm->mutex);
1711 
1712 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1713 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1714 		return -EBUSY;
1715 	}
1716 
1717 	ret = iwl_mvm_check_running_scans(mvm, type);
1718 	if (ret)
1719 		return ret;
1720 
1721 	/* we should have failed registration if scan_cmd was NULL */
1722 	if (WARN_ON(!mvm->scan_cmd))
1723 		return -ENOMEM;
1724 
1725 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1726 		return -ENOBUFS;
1727 
1728 	params.n_ssids = req->n_ssids;
1729 	params.flags = req->flags;
1730 	params.n_channels = req->n_channels;
1731 	params.ssids = req->ssids;
1732 	params.channels = req->channels;
1733 	params.mac_addr = req->mac_addr;
1734 	params.mac_addr_mask = req->mac_addr_mask;
1735 	params.no_cck = false;
1736 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
1737 	params.n_match_sets = req->n_match_sets;
1738 	params.match_sets = req->match_sets;
1739 	if (!req->n_scan_plans)
1740 		return -EINVAL;
1741 
1742 	params.n_scan_plans = req->n_scan_plans;
1743 	params.scan_plans = req->scan_plans;
1744 
1745 	iwl_mvm_fill_scan_type(mvm, &params,
1746 			       vif->type == NL80211_IFTYPE_P2P_DEVICE);
1747 
1748 	/* In theory, LMAC scans can handle a 32-bit delay, but since
1749 	 * waiting for over 18 hours to start the scan is a bit silly
1750 	 * and to keep it aligned with UMAC scans (which only support
1751 	 * 16-bit delays), trim it down to 16-bits.
1752 	 */
1753 	if (req->delay > U16_MAX) {
1754 		IWL_DEBUG_SCAN(mvm,
1755 			       "delay value is > 16-bits, set to max possible\n");
1756 		params.delay = U16_MAX;
1757 	} else {
1758 		params.delay = req->delay;
1759 	}
1760 
1761 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1762 	if (ret)
1763 		return ret;
1764 
1765 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1766 
1767 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1768 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1769 		ret = iwl_mvm_scan_umac(mvm, vif, &params, type);
1770 	} else {
1771 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1772 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1773 	}
1774 
1775 	if (ret)
1776 		return ret;
1777 
1778 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1779 	if (!ret) {
1780 		IWL_DEBUG_SCAN(mvm,
1781 			       "Sched scan request was sent successfully\n");
1782 		mvm->scan_status |= type;
1783 	} else {
1784 		/* If the scan failed, it usually means that the FW was unable
1785 		 * to allocate the time events. Warn on it, but maybe we
1786 		 * should try to send the command again with different params.
1787 		 */
1788 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1789 	}
1790 
1791 	return ret;
1792 }
1793 
1794 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1795 					 struct iwl_rx_cmd_buffer *rxb)
1796 {
1797 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1798 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1799 	u32 uid = __le32_to_cpu(notif->uid);
1800 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1801 
1802 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1803 		return;
1804 
1805 	/* if the scan is already stopping, we don't need to notify mac80211 */
1806 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1807 		struct cfg80211_scan_info info = {
1808 			.aborted = aborted,
1809 			.scan_start_tsf = mvm->scan_start,
1810 		};
1811 
1812 		memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
1813 		ieee80211_scan_completed(mvm->hw, &info);
1814 		mvm->scan_vif = NULL;
1815 		iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1816 		cancel_delayed_work(&mvm->scan_timeout_dwork);
1817 		iwl_mvm_resume_tcm(mvm);
1818 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1819 		ieee80211_sched_scan_stopped(mvm->hw);
1820 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1821 	}
1822 
1823 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
1824 	IWL_DEBUG_SCAN(mvm,
1825 		       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1826 		       uid, mvm->scan_uid_status[uid],
1827 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1828 				"completed" : "aborted",
1829 		       iwl_mvm_ebs_status_str(notif->ebs_status));
1830 	IWL_DEBUG_SCAN(mvm,
1831 		       "Last line %d, Last iteration %d, Time from last iteration %d\n",
1832 		       notif->last_schedule, notif->last_iter,
1833 		       __le32_to_cpu(notif->time_from_last_iter));
1834 
1835 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1836 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1837 		mvm->last_ebs_successful = false;
1838 
1839 	mvm->scan_uid_status[uid] = 0;
1840 }
1841 
1842 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1843 					      struct iwl_rx_cmd_buffer *rxb)
1844 {
1845 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1846 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1847 
1848 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
1849 
1850 	IWL_DEBUG_SCAN(mvm,
1851 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
1852 		       notif->status, notif->scanned_channels);
1853 
1854 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
1855 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
1856 		ieee80211_sched_scan_results(mvm->hw);
1857 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
1858 	}
1859 
1860 	IWL_DEBUG_SCAN(mvm,
1861 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
1862 		       mvm->scan_start);
1863 }
1864 
1865 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1866 {
1867 	struct iwl_umac_scan_abort cmd = {};
1868 	int uid, ret;
1869 
1870 	lockdep_assert_held(&mvm->mutex);
1871 
1872 	/* We should always get a valid index here, because we already
1873 	 * checked that this type of scan was running in the generic
1874 	 * code.
1875 	 */
1876 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
1877 	if (WARN_ON_ONCE(uid < 0))
1878 		return uid;
1879 
1880 	cmd.uid = cpu_to_le32(uid);
1881 
1882 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1883 
1884 	ret = iwl_mvm_send_cmd_pdu(mvm,
1885 				   iwl_cmd_id(SCAN_ABORT_UMAC,
1886 					      IWL_ALWAYS_LONG_GROUP, 0),
1887 				   0, sizeof(cmd), &cmd);
1888 	if (!ret)
1889 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1890 
1891 	return ret;
1892 }
1893 
1894 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1895 {
1896 	struct iwl_notification_wait wait_scan_done;
1897 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1898 					      SCAN_OFFLOAD_COMPLETE, };
1899 	int ret;
1900 
1901 	lockdep_assert_held(&mvm->mutex);
1902 
1903 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1904 				   scan_done_notif,
1905 				   ARRAY_SIZE(scan_done_notif),
1906 				   NULL, NULL);
1907 
1908 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1909 
1910 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1911 		ret = iwl_mvm_umac_scan_abort(mvm, type);
1912 	else
1913 		ret = iwl_mvm_lmac_scan_abort(mvm);
1914 
1915 	if (ret) {
1916 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1917 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1918 		return ret;
1919 	}
1920 
1921 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1922 
1923 	return ret;
1924 }
1925 
1926 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1927 {
1928 	int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
1929 
1930 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
1931 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
1932 	else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
1933 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
1934 	else if (iwl_mvm_cdb_scan_api(mvm))
1935 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
1936 
1937 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1938 		return base_size +
1939 			sizeof(struct iwl_scan_channel_cfg_umac) *
1940 				mvm->fw->ucode_capa.n_scan_channels +
1941 			sizeof(struct iwl_scan_req_umac_tail);
1942 
1943 	return sizeof(struct iwl_scan_req_lmac) +
1944 		sizeof(struct iwl_scan_channel_cfg_lmac) *
1945 		mvm->fw->ucode_capa.n_scan_channels +
1946 		sizeof(struct iwl_scan_probe_req);
1947 }
1948 
1949 /*
1950  * This function is used in nic restart flow, to inform mac80211 about scans
1951  * that was aborted by restart flow or by an assert.
1952  */
1953 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1954 {
1955 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1956 		int uid, i;
1957 
1958 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
1959 		if (uid >= 0) {
1960 			struct cfg80211_scan_info info = {
1961 				.aborted = true,
1962 			};
1963 
1964 			ieee80211_scan_completed(mvm->hw, &info);
1965 			mvm->scan_uid_status[uid] = 0;
1966 		}
1967 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
1968 		if (uid >= 0 && !mvm->fw_restart) {
1969 			ieee80211_sched_scan_stopped(mvm->hw);
1970 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1971 			mvm->scan_uid_status[uid] = 0;
1972 		}
1973 
1974 		/* We shouldn't have any UIDs still set.  Loop over all the
1975 		 * UIDs to make sure there's nothing left there and warn if
1976 		 * any is found.
1977 		 */
1978 		for (i = 0; i < mvm->max_scans; i++) {
1979 			if (WARN_ONCE(mvm->scan_uid_status[i],
1980 				      "UMAC scan UID %d status was not cleaned\n",
1981 				      i))
1982 				mvm->scan_uid_status[i] = 0;
1983 		}
1984 	} else {
1985 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
1986 			struct cfg80211_scan_info info = {
1987 				.aborted = true,
1988 			};
1989 
1990 			ieee80211_scan_completed(mvm->hw, &info);
1991 		}
1992 
1993 		/* Sched scan will be restarted by mac80211 in
1994 		 * restart_hw, so do not report if FW is about to be
1995 		 * restarted.
1996 		 */
1997 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
1998 		    !mvm->fw_restart) {
1999 			ieee80211_sched_scan_stopped(mvm->hw);
2000 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2001 		}
2002 	}
2003 }
2004 
2005 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2006 {
2007 	int ret;
2008 
2009 	if (!(mvm->scan_status & type))
2010 		return 0;
2011 
2012 	if (iwl_mvm_is_radio_killed(mvm)) {
2013 		ret = 0;
2014 		goto out;
2015 	}
2016 
2017 	ret = iwl_mvm_scan_stop_wait(mvm, type);
2018 	if (!ret)
2019 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2020 out:
2021 	/* Clear the scan status so the next scan requests will
2022 	 * succeed and mark the scan as stopping, so that the Rx
2023 	 * handler doesn't do anything, as the scan was stopped from
2024 	 * above.
2025 	 */
2026 	mvm->scan_status &= ~type;
2027 
2028 	if (type == IWL_MVM_SCAN_REGULAR) {
2029 		/* Since the rx handler won't do anything now, we have
2030 		 * to release the scan reference here.
2031 		 */
2032 		iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2033 		cancel_delayed_work(&mvm->scan_timeout_dwork);
2034 		if (notify) {
2035 			struct cfg80211_scan_info info = {
2036 				.aborted = true,
2037 			};
2038 
2039 			ieee80211_scan_completed(mvm->hw, &info);
2040 		}
2041 	} else if (notify) {
2042 		ieee80211_sched_scan_stopped(mvm->hw);
2043 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2044 	}
2045 
2046 	return ret;
2047 }
2048