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 - 2019 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 - 2019 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 #define IWL_SCAN_LAST_2_4_CHN		14
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 high band APs number */
87 #define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8
88 /* adaptive dwell default low band APs number */
89 #define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2
90 /* adaptive dwell default APs number in social channels (1, 6, 11) */
91 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
92 /* number of scan channels */
93 #define IWL_SCAN_NUM_CHANNELS 112
94 /* adaptive dwell default number of APs override */
95 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_OVERRIDE 10
96 
97 struct iwl_mvm_scan_timing_params {
98 	u32 suspend_time;
99 	u32 max_out_time;
100 };
101 
102 static struct iwl_mvm_scan_timing_params scan_timing[] = {
103 	[IWL_SCAN_TYPE_UNASSOC] = {
104 		.suspend_time = 0,
105 		.max_out_time = 0,
106 	},
107 	[IWL_SCAN_TYPE_WILD] = {
108 		.suspend_time = 30,
109 		.max_out_time = 120,
110 	},
111 	[IWL_SCAN_TYPE_MILD] = {
112 		.suspend_time = 120,
113 		.max_out_time = 120,
114 	},
115 	[IWL_SCAN_TYPE_FRAGMENTED] = {
116 		.suspend_time = 95,
117 		.max_out_time = 44,
118 	},
119 	[IWL_SCAN_TYPE_FAST_BALANCE] = {
120 		.suspend_time = 30,
121 		.max_out_time = 37,
122 	},
123 };
124 
125 struct iwl_mvm_scan_params {
126 	/* For CDB this is low band scan type, for non-CDB - type. */
127 	enum iwl_mvm_scan_type type;
128 	enum iwl_mvm_scan_type hb_type;
129 	u32 n_channels;
130 	u16 delay;
131 	int n_ssids;
132 	struct cfg80211_ssid *ssids;
133 	struct ieee80211_channel **channels;
134 	u32 flags;
135 	u8 *mac_addr;
136 	u8 *mac_addr_mask;
137 	bool no_cck;
138 	bool pass_all;
139 	int n_match_sets;
140 	struct iwl_scan_probe_req preq;
141 	struct cfg80211_match_set *match_sets;
142 	int n_scan_plans;
143 	struct cfg80211_sched_scan_plan *scan_plans;
144 	u32 measurement_dwell;
145 };
146 
147 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
148 {
149 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
150 
151 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
152 		return (void *)&cmd->v8.data;
153 
154 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
155 		return (void *)&cmd->v7.data;
156 
157 	if (iwl_mvm_cdb_scan_api(mvm))
158 		return (void *)&cmd->v6.data;
159 
160 	return (void *)&cmd->v1.data;
161 }
162 
163 static inline struct iwl_scan_umac_chan_param *
164 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
165 {
166 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
167 
168 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
169 		return &cmd->v8.channel;
170 
171 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
172 		return &cmd->v7.channel;
173 
174 	if (iwl_mvm_cdb_scan_api(mvm))
175 		return &cmd->v6.channel;
176 
177 	return &cmd->v1.channel;
178 }
179 
180 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
181 {
182 	if (mvm->scan_rx_ant != ANT_NONE)
183 		return mvm->scan_rx_ant;
184 	return iwl_mvm_get_valid_rx_ant(mvm);
185 }
186 
187 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
188 {
189 	u16 rx_chain;
190 	u8 rx_ant;
191 
192 	rx_ant = iwl_mvm_scan_rx_ant(mvm);
193 	rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
194 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
195 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
196 	rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
197 	return cpu_to_le16(rx_chain);
198 }
199 
200 static inline __le32
201 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
202 			  bool no_cck)
203 {
204 	u32 tx_ant;
205 
206 	iwl_mvm_toggle_tx_ant(mvm, &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 struct iwl_is_dcm_with_go_iterator_data {
239 	struct ieee80211_vif *current_vif;
240 	bool is_dcm_with_p2p_go;
241 };
242 
243 static void iwl_mvm_is_dcm_with_go_iterator(void *_data, u8 *mac,
244 					    struct ieee80211_vif *vif)
245 {
246 	struct iwl_is_dcm_with_go_iterator_data *data = _data;
247 	struct iwl_mvm_vif *other_mvmvif = iwl_mvm_vif_from_mac80211(vif);
248 	struct iwl_mvm_vif *curr_mvmvif =
249 		iwl_mvm_vif_from_mac80211(data->current_vif);
250 
251 	/* exclude the given vif */
252 	if (vif == data->current_vif)
253 		return;
254 
255 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
256 	    other_mvmvif->phy_ctxt && curr_mvmvif->phy_ctxt &&
257 	    other_mvmvif->phy_ctxt->id != curr_mvmvif->phy_ctxt->id)
258 		data->is_dcm_with_p2p_go = true;
259 }
260 
261 static enum
262 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
263 					 struct ieee80211_vif *vif,
264 					 enum iwl_mvm_traffic_load load,
265 					 bool low_latency)
266 {
267 	int global_cnt = 0;
268 
269 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
270 					    IEEE80211_IFACE_ITER_NORMAL,
271 					    iwl_mvm_scan_condition_iterator,
272 					    &global_cnt);
273 	if (!global_cnt)
274 		return IWL_SCAN_TYPE_UNASSOC;
275 
276 	if (fw_has_api(&mvm->fw->ucode_capa,
277 		       IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
278 		if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
279 		    (!vif || vif->type != NL80211_IFTYPE_P2P_DEVICE))
280 			return IWL_SCAN_TYPE_FRAGMENTED;
281 
282 		/* in case of DCM with GO where BSS DTIM interval < 220msec
283 		 * set all scan requests as fast-balance scan
284 		 * */
285 		if (vif && vif->type == NL80211_IFTYPE_STATION &&
286 		    vif->bss_conf.dtim_period < 220) {
287 			struct iwl_is_dcm_with_go_iterator_data data = {
288 				.current_vif = vif,
289 				.is_dcm_with_p2p_go = false,
290 			};
291 
292 			ieee80211_iterate_active_interfaces_atomic(mvm->hw,
293 						IEEE80211_IFACE_ITER_NORMAL,
294 						iwl_mvm_is_dcm_with_go_iterator,
295 						&data);
296 			if (data.is_dcm_with_p2p_go)
297 				return IWL_SCAN_TYPE_FAST_BALANCE;
298 		}
299 	}
300 
301 	if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
302 		return IWL_SCAN_TYPE_MILD;
303 
304 	return IWL_SCAN_TYPE_WILD;
305 }
306 
307 static enum
308 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
309 					struct ieee80211_vif *vif)
310 {
311 	enum iwl_mvm_traffic_load load;
312 	bool low_latency;
313 
314 	load = iwl_mvm_get_traffic_load(mvm);
315 	low_latency = iwl_mvm_low_latency(mvm);
316 
317 	return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
318 }
319 
320 static enum
321 iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
322 					     struct ieee80211_vif *vif,
323 					     enum nl80211_band band)
324 {
325 	enum iwl_mvm_traffic_load load;
326 	bool low_latency;
327 
328 	load = iwl_mvm_get_traffic_load_band(mvm, band);
329 	low_latency = iwl_mvm_low_latency_band(mvm, band);
330 
331 	return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
332 }
333 
334 static int
335 iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
336 			      struct cfg80211_scan_request *req,
337 			      struct iwl_mvm_scan_params *params)
338 {
339 	u32 duration = scan_timing[params->type].max_out_time;
340 
341 	if (!req->duration)
342 		return 0;
343 
344 	if (iwl_mvm_is_cdb_supported(mvm)) {
345 		u32 hb_time = scan_timing[params->hb_type].max_out_time;
346 
347 		duration = min_t(u32, duration, hb_time);
348 	}
349 
350 	if (req->duration_mandatory && req->duration > duration) {
351 		IWL_DEBUG_SCAN(mvm,
352 			       "Measurement scan - too long dwell %hu (max out time %u)\n",
353 			       req->duration,
354 			       duration);
355 		return -EOPNOTSUPP;
356 	}
357 
358 	return min_t(u32, (u32)req->duration, duration);
359 }
360 
361 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
362 {
363 	/* require rrm scan whenever the fw supports it */
364 	return fw_has_capa(&mvm->fw->ucode_capa,
365 			   IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
366 }
367 
368 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
369 {
370 	int max_probe_len;
371 
372 	max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
373 
374 	/* we create the 802.11 header and SSID element */
375 	max_probe_len -= 24 + 2;
376 
377 	/* DS parameter set element is added on 2.4GHZ band if required */
378 	if (iwl_mvm_rrm_scan_needed(mvm))
379 		max_probe_len -= 3;
380 
381 	return max_probe_len;
382 }
383 
384 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
385 {
386 	int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
387 
388 	/* TODO: [BUG] This function should return the maximum allowed size of
389 	 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
390 	 * in the same command. So the correct implementation of this function
391 	 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
392 	 * command has only 512 bytes and it would leave us with about 240
393 	 * bytes for scan IEs, which is clearly not enough. So meanwhile
394 	 * we will report an incorrect value. This may result in a failure to
395 	 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
396 	 * functions with -ENOBUFS, if a large enough probe will be provided.
397 	 */
398 	return max_ie_len;
399 }
400 
401 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
402 					      struct iwl_rx_cmd_buffer *rxb)
403 {
404 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
405 	struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
406 
407 	IWL_DEBUG_SCAN(mvm,
408 		       "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
409 		       notif->status, notif->scanned_channels);
410 
411 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
412 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
413 		ieee80211_sched_scan_results(mvm->hw);
414 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
415 	}
416 }
417 
418 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
419 				 struct iwl_rx_cmd_buffer *rxb)
420 {
421 	IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
422 	ieee80211_sched_scan_results(mvm->hw);
423 }
424 
425 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
426 {
427 	switch (status) {
428 	case IWL_SCAN_EBS_SUCCESS:
429 		return "successful";
430 	case IWL_SCAN_EBS_INACTIVE:
431 		return "inactive";
432 	case IWL_SCAN_EBS_FAILED:
433 	case IWL_SCAN_EBS_CHAN_NOT_FOUND:
434 	default:
435 		return "failed";
436 	}
437 }
438 
439 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
440 					 struct iwl_rx_cmd_buffer *rxb)
441 {
442 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
443 	struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
444 	bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
445 
446 	/* If this happens, the firmware has mistakenly sent an LMAC
447 	 * notification during UMAC scans -- warn and ignore it.
448 	 */
449 	if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
450 				     IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
451 		return;
452 
453 	/* scan status must be locked for proper checking */
454 	lockdep_assert_held(&mvm->mutex);
455 
456 	/* We first check if we were stopping a scan, in which case we
457 	 * just clear the stopping flag.  Then we check if it was a
458 	 * firmware initiated stop, in which case we need to inform
459 	 * mac80211.
460 	 * Note that we can have a stopping and a running scan
461 	 * simultaneously, but we can't have two different types of
462 	 * scans stopping or running at the same time (since LMAC
463 	 * doesn't support it).
464 	 */
465 
466 	if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
467 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
468 
469 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
470 			       aborted ? "aborted" : "completed",
471 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
472 		IWL_DEBUG_SCAN(mvm,
473 			       "Last line %d, Last iteration %d, Time after last iteration %d\n",
474 			       scan_notif->last_schedule_line,
475 			       scan_notif->last_schedule_iteration,
476 			       __le32_to_cpu(scan_notif->time_after_last_iter));
477 
478 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
479 	} else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
480 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
481 			       aborted ? "aborted" : "completed",
482 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
483 
484 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
485 	} else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
486 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
487 
488 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
489 			       aborted ? "aborted" : "completed",
490 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
491 		IWL_DEBUG_SCAN(mvm,
492 			       "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
493 			       scan_notif->last_schedule_line,
494 			       scan_notif->last_schedule_iteration,
495 			       __le32_to_cpu(scan_notif->time_after_last_iter));
496 
497 		mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
498 		ieee80211_sched_scan_stopped(mvm->hw);
499 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
500 	} else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
501 		struct cfg80211_scan_info info = {
502 			.aborted = aborted,
503 		};
504 
505 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
506 			       aborted ? "aborted" : "completed",
507 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
508 
509 		mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
510 		ieee80211_scan_completed(mvm->hw, &info);
511 		cancel_delayed_work(&mvm->scan_timeout_dwork);
512 		iwl_mvm_resume_tcm(mvm);
513 	} else {
514 		IWL_ERR(mvm,
515 			"got scan complete notification but no scan is running\n");
516 	}
517 
518 	mvm->last_ebs_successful =
519 			scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
520 			scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
521 }
522 
523 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
524 {
525 	int i;
526 
527 	for (i = 0; i < PROBE_OPTION_MAX; i++) {
528 		if (!ssid_list[i].len)
529 			break;
530 		if (ssid_list[i].len == ssid_len &&
531 		    !memcmp(ssid_list->ssid, ssid, ssid_len))
532 			return i;
533 	}
534 	return -1;
535 }
536 
537 /* We insert the SSIDs in an inverted order, because the FW will
538  * invert it back.
539  */
540 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
541 				 struct iwl_ssid_ie *ssids,
542 				 u32 *ssid_bitmap)
543 {
544 	int i, j;
545 	int index;
546 	u32 tmp_bitmap = 0;
547 
548 	/*
549 	 * copy SSIDs from match list.
550 	 * iwl_config_sched_scan_profiles() uses the order of these ssids to
551 	 * config match list.
552 	 */
553 	for (i = 0, j = params->n_match_sets - 1;
554 	     j >= 0 && i < PROBE_OPTION_MAX;
555 	     i++, j--) {
556 		/* skip empty SSID matchsets */
557 		if (!params->match_sets[j].ssid.ssid_len)
558 			continue;
559 		ssids[i].id = WLAN_EID_SSID;
560 		ssids[i].len = params->match_sets[j].ssid.ssid_len;
561 		memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
562 		       ssids[i].len);
563 	}
564 
565 	/* add SSIDs from scan SSID list */
566 	for (j = params->n_ssids - 1;
567 	     j >= 0 && i < PROBE_OPTION_MAX;
568 	     i++, j--) {
569 		index = iwl_ssid_exist(params->ssids[j].ssid,
570 				       params->ssids[j].ssid_len,
571 				       ssids);
572 		if (index < 0) {
573 			ssids[i].id = WLAN_EID_SSID;
574 			ssids[i].len = params->ssids[j].ssid_len;
575 			memcpy(ssids[i].ssid, params->ssids[j].ssid,
576 			       ssids[i].len);
577 			tmp_bitmap |= BIT(i);
578 		} else {
579 			tmp_bitmap |= BIT(index);
580 		}
581 	}
582 	if (ssid_bitmap)
583 		*ssid_bitmap = tmp_bitmap;
584 }
585 
586 static int
587 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
588 				   struct cfg80211_sched_scan_request *req)
589 {
590 	struct iwl_scan_offload_profile *profile;
591 	struct iwl_scan_offload_profile_cfg *profile_cfg;
592 	struct iwl_scan_offload_blacklist *blacklist;
593 	struct iwl_host_cmd cmd = {
594 		.id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
595 		.len[1] = sizeof(*profile_cfg),
596 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
597 		.dataflags[1] = IWL_HCMD_DFL_NOCOPY,
598 	};
599 	int blacklist_len;
600 	int i;
601 	int ret;
602 
603 	if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
604 		return -EIO;
605 
606 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
607 		blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
608 	else
609 		blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
610 
611 	blacklist = kcalloc(blacklist_len, sizeof(*blacklist), GFP_KERNEL);
612 	if (!blacklist)
613 		return -ENOMEM;
614 
615 	profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
616 	if (!profile_cfg) {
617 		ret = -ENOMEM;
618 		goto free_blacklist;
619 	}
620 
621 	cmd.data[0] = blacklist;
622 	cmd.len[0] = sizeof(*blacklist) * blacklist_len;
623 	cmd.data[1] = profile_cfg;
624 
625 	/* No blacklist configuration */
626 
627 	profile_cfg->num_profiles = req->n_match_sets;
628 	profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
629 	profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
630 	profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
631 	if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
632 		profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
633 
634 	for (i = 0; i < req->n_match_sets; i++) {
635 		profile = &profile_cfg->profiles[i];
636 		profile->ssid_index = i;
637 		/* Support any cipher and auth algorithm */
638 		profile->unicast_cipher = 0xff;
639 		profile->auth_alg = 0xff;
640 		profile->network_type = IWL_NETWORK_TYPE_ANY;
641 		profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
642 		profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
643 	}
644 
645 	IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
646 
647 	ret = iwl_mvm_send_cmd(mvm, &cmd);
648 	kfree(profile_cfg);
649 free_blacklist:
650 	kfree(blacklist);
651 
652 	return ret;
653 }
654 
655 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
656 				  struct cfg80211_sched_scan_request *req)
657 {
658 	if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
659 		IWL_DEBUG_SCAN(mvm,
660 			       "Sending scheduled scan with filtering, n_match_sets %d\n",
661 			       req->n_match_sets);
662 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
663 		return false;
664 	}
665 
666 	IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
667 
668 	mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
669 	return true;
670 }
671 
672 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
673 {
674 	int ret;
675 	struct iwl_host_cmd cmd = {
676 		.id = SCAN_OFFLOAD_ABORT_CMD,
677 	};
678 	u32 status = CAN_ABORT_STATUS;
679 
680 	ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
681 	if (ret)
682 		return ret;
683 
684 	if (status != CAN_ABORT_STATUS) {
685 		/*
686 		 * The scan abort will return 1 for success or
687 		 * 2 for "failure".  A failure condition can be
688 		 * due to simply not being in an active scan which
689 		 * can occur if we send the scan abort before the
690 		 * microcode has notified us that a scan is completed.
691 		 */
692 		IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
693 		ret = -ENOENT;
694 	}
695 
696 	return ret;
697 }
698 
699 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
700 				     struct iwl_scan_req_tx_cmd *tx_cmd,
701 				     bool no_cck)
702 {
703 	tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
704 					 TX_CMD_FLG_BT_DIS);
705 	tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
706 							   NL80211_BAND_2GHZ,
707 							   no_cck);
708 	tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
709 
710 	tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
711 					 TX_CMD_FLG_BT_DIS);
712 	tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
713 							   NL80211_BAND_5GHZ,
714 							   no_cck);
715 	tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
716 }
717 
718 static void
719 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
720 			       struct ieee80211_channel **channels,
721 			       int n_channels, u32 ssid_bitmap,
722 			       struct iwl_scan_req_lmac *cmd)
723 {
724 	struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
725 	int i;
726 
727 	for (i = 0; i < n_channels; i++) {
728 		channel_cfg[i].channel_num =
729 			cpu_to_le16(channels[i]->hw_value);
730 		channel_cfg[i].iter_count = cpu_to_le16(1);
731 		channel_cfg[i].iter_interval = 0;
732 		channel_cfg[i].flags =
733 			cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
734 				    ssid_bitmap);
735 	}
736 }
737 
738 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
739 					   size_t len, u8 *const pos)
740 {
741 	static const u8 before_ds_params[] = {
742 			WLAN_EID_SSID,
743 			WLAN_EID_SUPP_RATES,
744 			WLAN_EID_REQUEST,
745 			WLAN_EID_EXT_SUPP_RATES,
746 	};
747 	size_t offs;
748 	u8 *newpos = pos;
749 
750 	if (!iwl_mvm_rrm_scan_needed(mvm)) {
751 		memcpy(newpos, ies, len);
752 		return newpos + len;
753 	}
754 
755 	offs = ieee80211_ie_split(ies, len,
756 				  before_ds_params,
757 				  ARRAY_SIZE(before_ds_params),
758 				  0);
759 
760 	memcpy(newpos, ies, offs);
761 	newpos += offs;
762 
763 	/* Add a placeholder for DS Parameter Set element */
764 	*newpos++ = WLAN_EID_DS_PARAMS;
765 	*newpos++ = 1;
766 	*newpos++ = 0;
767 
768 	memcpy(newpos, ies + offs, len - offs);
769 	newpos += len - offs;
770 
771 	return newpos;
772 }
773 
774 #define WFA_TPC_IE_LEN	9
775 
776 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
777 {
778 	pos[0] = WLAN_EID_VENDOR_SPECIFIC;
779 	pos[1] = WFA_TPC_IE_LEN - 2;
780 	pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
781 	pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
782 	pos[4] = WLAN_OUI_MICROSOFT & 0xff;
783 	pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
784 	pos[6] = 0;
785 	/* pos[7] - tx power will be inserted by the FW */
786 	pos[7] = 0;
787 	pos[8] = 0;
788 }
789 
790 static void
791 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
792 			 struct ieee80211_scan_ies *ies,
793 			 struct iwl_mvm_scan_params *params)
794 {
795 	struct ieee80211_mgmt *frame = (void *)params->preq.buf;
796 	u8 *pos, *newpos;
797 	const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
798 		params->mac_addr : NULL;
799 
800 	/*
801 	 * Unfortunately, right now the offload scan doesn't support randomising
802 	 * within the firmware, so until the firmware API is ready we implement
803 	 * it in the driver. This means that the scan iterations won't really be
804 	 * random, only when it's restarted, but at least that helps a bit.
805 	 */
806 	if (mac_addr)
807 		get_random_mask_addr(frame->sa, mac_addr,
808 				     params->mac_addr_mask);
809 	else
810 		memcpy(frame->sa, vif->addr, ETH_ALEN);
811 
812 	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
813 	eth_broadcast_addr(frame->da);
814 	eth_broadcast_addr(frame->bssid);
815 	frame->seq_ctrl = 0;
816 
817 	pos = frame->u.probe_req.variable;
818 	*pos++ = WLAN_EID_SSID;
819 	*pos++ = 0;
820 
821 	params->preq.mac_header.offset = 0;
822 	params->preq.mac_header.len = cpu_to_le16(24 + 2);
823 
824 	/* Insert ds parameter set element on 2.4 GHz band */
825 	newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
826 						 ies->ies[NL80211_BAND_2GHZ],
827 						 ies->len[NL80211_BAND_2GHZ],
828 						 pos);
829 	params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
830 	params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
831 	pos = newpos;
832 
833 	memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
834 	       ies->len[NL80211_BAND_5GHZ]);
835 	params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
836 	params->preq.band_data[1].len =
837 		cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
838 	pos += ies->len[NL80211_BAND_5GHZ];
839 
840 	memcpy(pos, ies->common_ies, ies->common_ie_len);
841 	params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
842 
843 	if (iwl_mvm_rrm_scan_needed(mvm) &&
844 	    !fw_has_capa(&mvm->fw->ucode_capa,
845 			 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
846 		iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
847 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
848 							   WFA_TPC_IE_LEN);
849 	} else {
850 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
851 	}
852 }
853 
854 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
855 				    struct iwl_scan_req_lmac *cmd,
856 				    struct iwl_mvm_scan_params *params)
857 {
858 	cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
859 	cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
860 	cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
861 	cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
862 	cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
863 	cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
864 	cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
865 }
866 
867 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
868 				     struct ieee80211_scan_ies *ies,
869 				     int n_channels)
870 {
871 	return ((n_ssids <= PROBE_OPTION_MAX) &&
872 		(n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
873 		(ies->common_ie_len +
874 		 ies->len[NL80211_BAND_2GHZ] +
875 		 ies->len[NL80211_BAND_5GHZ] <=
876 		 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
877 }
878 
879 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
880 					struct ieee80211_vif *vif)
881 {
882 	const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
883 	bool low_latency;
884 
885 	if (iwl_mvm_is_cdb_supported(mvm))
886 		low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
887 	else
888 		low_latency = iwl_mvm_low_latency(mvm);
889 
890 	/* We can only use EBS if:
891 	 *	1. the feature is supported;
892 	 *	2. the last EBS was successful;
893 	 *	3. if only single scan, the single scan EBS API is supported;
894 	 *	4. it's not a p2p find operation.
895 	 *	5. we are not in low latency mode,
896 	 *	   or if fragmented ebs is supported by the FW
897 	 */
898 	return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
899 		mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
900 		vif->type != NL80211_IFTYPE_P2P_DEVICE &&
901 		(!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
902 }
903 
904 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
905 {
906 	return params->n_scan_plans == 1 &&
907 		params->scan_plans[0].iterations == 1;
908 }
909 
910 static bool iwl_mvm_is_scan_fragmented(enum iwl_mvm_scan_type type)
911 {
912 	return (type == IWL_SCAN_TYPE_FRAGMENTED ||
913 		type == IWL_SCAN_TYPE_FAST_BALANCE);
914 }
915 
916 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
917 				   struct iwl_mvm_scan_params *params,
918 				   struct ieee80211_vif *vif)
919 {
920 	int flags = 0;
921 
922 	if (params->n_ssids == 0)
923 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
924 
925 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
926 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
927 
928 	if (iwl_mvm_is_scan_fragmented(params->type))
929 		flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
930 
931 	if (iwl_mvm_rrm_scan_needed(mvm) &&
932 	    fw_has_capa(&mvm->fw->ucode_capa,
933 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
934 		flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
935 
936 	if (params->pass_all)
937 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
938 	else
939 		flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
940 
941 #ifdef CONFIG_IWLWIFI_DEBUGFS
942 	if (mvm->scan_iter_notif_enabled)
943 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
944 #endif
945 
946 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
947 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
948 
949 	if (iwl_mvm_is_regular_scan(params) &&
950 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
951 	    !iwl_mvm_is_scan_fragmented(params->type))
952 		flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
953 
954 	return flags;
955 }
956 
957 static void
958 iwl_mvm_scan_set_legacy_probe_req(struct iwl_scan_probe_req_v1 *p_req,
959 				  struct iwl_scan_probe_req *src_p_req)
960 {
961 	int i;
962 
963 	p_req->mac_header = src_p_req->mac_header;
964 	for (i = 0; i < SCAN_NUM_BAND_PROBE_DATA_V_1; i++)
965 		p_req->band_data[i] = src_p_req->band_data[i];
966 	p_req->common_data = src_p_req->common_data;
967 	memcpy(p_req->buf, src_p_req->buf, sizeof(p_req->buf));
968 }
969 
970 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
971 			     struct iwl_mvm_scan_params *params)
972 {
973 	struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
974 	struct iwl_scan_probe_req_v1 *preq =
975 		(void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
976 			 mvm->fw->ucode_capa.n_scan_channels);
977 	u32 ssid_bitmap = 0;
978 	int i;
979 	u8 band;
980 
981 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
982 		return -EINVAL;
983 
984 	iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
985 
986 	cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
987 	cmd->iter_num = cpu_to_le32(1);
988 	cmd->n_channels = (u8)params->n_channels;
989 
990 	cmd->delay = cpu_to_le32(params->delay);
991 
992 	cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
993 							      vif));
994 
995 	band = iwl_mvm_phy_band_from_nl80211(params->channels[0]->band);
996 	cmd->flags = cpu_to_le32(band);
997 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
998 					MAC_FILTER_IN_BEACON);
999 	iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
1000 	iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
1001 
1002 	/* this API uses bits 1-20 instead of 0-19 */
1003 	ssid_bitmap <<= 1;
1004 
1005 	for (i = 0; i < params->n_scan_plans; i++) {
1006 		struct cfg80211_sched_scan_plan *scan_plan =
1007 			&params->scan_plans[i];
1008 
1009 		cmd->schedule[i].delay =
1010 			cpu_to_le16(scan_plan->interval);
1011 		cmd->schedule[i].iterations = scan_plan->iterations;
1012 		cmd->schedule[i].full_scan_mul = 1;
1013 	}
1014 
1015 	/*
1016 	 * If the number of iterations of the last scan plan is set to
1017 	 * zero, it should run infinitely. However, this is not always the case.
1018 	 * For example, when regular scan is requested the driver sets one scan
1019 	 * plan with one iteration.
1020 	 */
1021 	if (!cmd->schedule[i - 1].iterations)
1022 		cmd->schedule[i - 1].iterations = 0xff;
1023 
1024 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1025 		cmd->channel_opt[0].flags =
1026 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1027 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1028 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1029 		cmd->channel_opt[0].non_ebs_ratio =
1030 			cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1031 		cmd->channel_opt[1].flags =
1032 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1033 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1034 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1035 		cmd->channel_opt[1].non_ebs_ratio =
1036 			cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1037 	}
1038 
1039 	iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
1040 				       params->n_channels, ssid_bitmap, cmd);
1041 
1042 	iwl_mvm_scan_set_legacy_probe_req(preq, &params->preq);
1043 
1044 	return 0;
1045 }
1046 
1047 static int rate_to_scan_rate_flag(unsigned int rate)
1048 {
1049 	static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1050 		[IWL_RATE_1M_INDEX]	= SCAN_CONFIG_RATE_1M,
1051 		[IWL_RATE_2M_INDEX]	= SCAN_CONFIG_RATE_2M,
1052 		[IWL_RATE_5M_INDEX]	= SCAN_CONFIG_RATE_5M,
1053 		[IWL_RATE_11M_INDEX]	= SCAN_CONFIG_RATE_11M,
1054 		[IWL_RATE_6M_INDEX]	= SCAN_CONFIG_RATE_6M,
1055 		[IWL_RATE_9M_INDEX]	= SCAN_CONFIG_RATE_9M,
1056 		[IWL_RATE_12M_INDEX]	= SCAN_CONFIG_RATE_12M,
1057 		[IWL_RATE_18M_INDEX]	= SCAN_CONFIG_RATE_18M,
1058 		[IWL_RATE_24M_INDEX]	= SCAN_CONFIG_RATE_24M,
1059 		[IWL_RATE_36M_INDEX]	= SCAN_CONFIG_RATE_36M,
1060 		[IWL_RATE_48M_INDEX]	= SCAN_CONFIG_RATE_48M,
1061 		[IWL_RATE_54M_INDEX]	= SCAN_CONFIG_RATE_54M,
1062 	};
1063 
1064 	return rate_to_scan_rate[rate];
1065 }
1066 
1067 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1068 {
1069 	struct ieee80211_supported_band *band;
1070 	unsigned int rates = 0;
1071 	int i;
1072 
1073 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1074 	for (i = 0; i < band->n_bitrates; i++)
1075 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1076 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1077 	for (i = 0; i < band->n_bitrates; i++)
1078 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1079 
1080 	/* Set both basic rates and supported rates */
1081 	rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1082 
1083 	return cpu_to_le32(rates);
1084 }
1085 
1086 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1087 				    struct iwl_scan_dwell *dwell)
1088 {
1089 	dwell->active = IWL_SCAN_DWELL_ACTIVE;
1090 	dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1091 	dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1092 	dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1093 }
1094 
1095 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels,
1096 				  u32 max_channels)
1097 {
1098 	struct ieee80211_supported_band *band;
1099 	int i, j = 0;
1100 
1101 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1102 	for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1103 		channels[j] = band->channels[i].hw_value;
1104 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1105 	for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1106 		channels[j] = band->channels[i].hw_value;
1107 }
1108 
1109 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1110 					u32 flags, u8 channel_flags,
1111 					u32 max_channels)
1112 {
1113 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, NULL);
1114 	struct iwl_scan_config_v1 *cfg = config;
1115 
1116 	cfg->flags = cpu_to_le32(flags);
1117 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1118 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1119 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1120 	cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1121 	cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1122 
1123 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1124 
1125 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1126 
1127 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1128 	cfg->channel_flags = channel_flags;
1129 
1130 	iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1131 }
1132 
1133 static void iwl_mvm_fill_scan_config_v2(struct iwl_mvm *mvm, void *config,
1134 					u32 flags, u8 channel_flags,
1135 					u32 max_channels)
1136 {
1137 	struct iwl_scan_config_v2 *cfg = config;
1138 
1139 	cfg->flags = cpu_to_le32(flags);
1140 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1141 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1142 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1143 
1144 	if (iwl_mvm_is_cdb_supported(mvm)) {
1145 		enum iwl_mvm_scan_type lb_type, hb_type;
1146 
1147 		lb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1148 						     NL80211_BAND_2GHZ);
1149 		hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1150 						     NL80211_BAND_5GHZ);
1151 
1152 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1153 			cpu_to_le32(scan_timing[lb_type].max_out_time);
1154 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1155 			cpu_to_le32(scan_timing[lb_type].suspend_time);
1156 
1157 		cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1158 			cpu_to_le32(scan_timing[hb_type].max_out_time);
1159 		cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1160 			cpu_to_le32(scan_timing[hb_type].suspend_time);
1161 	} else {
1162 		enum iwl_mvm_scan_type type =
1163 			iwl_mvm_get_scan_type(mvm, NULL);
1164 
1165 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1166 			cpu_to_le32(scan_timing[type].max_out_time);
1167 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1168 			cpu_to_le32(scan_timing[type].suspend_time);
1169 	}
1170 
1171 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1172 
1173 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1174 
1175 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1176 	cfg->channel_flags = channel_flags;
1177 
1178 	iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1179 }
1180 
1181 static int iwl_mvm_legacy_config_scan(struct iwl_mvm *mvm)
1182 {
1183 	void *cfg;
1184 	int ret, cmd_size;
1185 	struct iwl_host_cmd cmd = {
1186 		.id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1187 	};
1188 	enum iwl_mvm_scan_type type;
1189 	enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1190 	int num_channels =
1191 		mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1192 		mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1193 	u32 flags;
1194 	u8 channel_flags;
1195 
1196 	if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1197 		num_channels = mvm->fw->ucode_capa.n_scan_channels;
1198 
1199 	if (iwl_mvm_is_cdb_supported(mvm)) {
1200 		type = iwl_mvm_get_scan_type_band(mvm, NULL,
1201 						  NL80211_BAND_2GHZ);
1202 		hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1203 						     NL80211_BAND_5GHZ);
1204 		if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1205 			return 0;
1206 	} else {
1207 		type = iwl_mvm_get_scan_type(mvm, NULL);
1208 		if (type == mvm->scan_type)
1209 			return 0;
1210 	}
1211 
1212 	if (iwl_mvm_cdb_scan_api(mvm))
1213 		cmd_size = sizeof(struct iwl_scan_config_v2);
1214 	else
1215 		cmd_size = sizeof(struct iwl_scan_config_v1);
1216 	cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1217 
1218 	cfg = kzalloc(cmd_size, GFP_KERNEL);
1219 	if (!cfg)
1220 		return -ENOMEM;
1221 
1222 	flags = SCAN_CONFIG_FLAG_ACTIVATE |
1223 		 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1224 		 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1225 		 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1226 		 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1227 		 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1228 		 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1229 		 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1230 		 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1231 		 SCAN_CONFIG_N_CHANNELS(num_channels) |
1232 		 (iwl_mvm_is_scan_fragmented(type) ?
1233 		  SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1234 		  SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1235 
1236 	channel_flags = IWL_CHANNEL_FLAG_EBS |
1237 			IWL_CHANNEL_FLAG_ACCURATE_EBS |
1238 			IWL_CHANNEL_FLAG_EBS_ADD |
1239 			IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1240 
1241 	/*
1242 	 * Check for fragmented scan on LMAC2 - high band.
1243 	 * LMAC1 - low band is checked above.
1244 	 */
1245 	if (iwl_mvm_cdb_scan_api(mvm)) {
1246 		if (iwl_mvm_is_cdb_supported(mvm))
1247 			flags |= (iwl_mvm_is_scan_fragmented(hb_type)) ?
1248 				 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1249 				 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1250 		iwl_mvm_fill_scan_config_v2(mvm, cfg, flags, channel_flags,
1251 					    num_channels);
1252 	} else {
1253 		iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags,
1254 					    num_channels);
1255 	}
1256 
1257 	cmd.data[0] = cfg;
1258 	cmd.len[0] = cmd_size;
1259 	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1260 
1261 	IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1262 
1263 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1264 	if (!ret) {
1265 		mvm->scan_type = type;
1266 		mvm->hb_scan_type = hb_type;
1267 	}
1268 
1269 	kfree(cfg);
1270 	return ret;
1271 }
1272 
1273 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1274 {
1275 	struct iwl_scan_config cfg;
1276 	struct iwl_host_cmd cmd = {
1277 		.id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1278 		.len[0] = sizeof(cfg),
1279 		.data[0] = &cfg,
1280 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1281 	};
1282 
1283 	if (!iwl_mvm_is_reduced_config_scan_supported(mvm))
1284 		return iwl_mvm_legacy_config_scan(mvm);
1285 
1286 	memset(&cfg, 0, sizeof(cfg));
1287 
1288 	cfg.bcast_sta_id = mvm->aux_sta.sta_id;
1289 	cfg.tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1290 	cfg.rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1291 
1292 	IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1293 
1294 	return iwl_mvm_send_cmd(mvm, &cmd);
1295 }
1296 
1297 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1298 {
1299 	int i;
1300 
1301 	for (i = 0; i < mvm->max_scans; i++)
1302 		if (mvm->scan_uid_status[i] == status)
1303 			return i;
1304 
1305 	return -ENOENT;
1306 }
1307 
1308 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1309 				    struct iwl_scan_req_umac *cmd,
1310 				    struct iwl_mvm_scan_params *params)
1311 {
1312 	struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1313 	u8 active_dwell, passive_dwell;
1314 
1315 	timing = &scan_timing[params->type];
1316 	active_dwell = params->measurement_dwell ?
1317 		params->measurement_dwell : IWL_SCAN_DWELL_ACTIVE;
1318 	passive_dwell = params->measurement_dwell ?
1319 		params->measurement_dwell : IWL_SCAN_DWELL_PASSIVE;
1320 
1321 	if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1322 		cmd->v7.adwell_default_n_aps_social =
1323 			IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1324 		cmd->v7.adwell_default_n_aps =
1325 			IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1326 
1327 		if (iwl_mvm_is_adwell_hb_ap_num_supported(mvm))
1328 			cmd->v9.adwell_default_hb_n_aps =
1329 				IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1330 
1331 		/* if custom max budget was configured with debugfs */
1332 		if (IWL_MVM_ADWELL_MAX_BUDGET)
1333 			cmd->v7.adwell_max_budget =
1334 				cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1335 		else if (params->ssids && params->ssids[0].ssid_len)
1336 			cmd->v7.adwell_max_budget =
1337 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1338 		else
1339 			cmd->v7.adwell_max_budget =
1340 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1341 
1342 		cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1343 		cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1344 			cpu_to_le32(timing->max_out_time);
1345 		cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1346 			cpu_to_le32(timing->suspend_time);
1347 
1348 		if (iwl_mvm_is_cdb_supported(mvm)) {
1349 			hb_timing = &scan_timing[params->hb_type];
1350 
1351 			cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1352 				cpu_to_le32(hb_timing->max_out_time);
1353 			cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1354 				cpu_to_le32(hb_timing->suspend_time);
1355 		}
1356 
1357 		if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1358 			cmd->v7.active_dwell = active_dwell;
1359 			cmd->v7.passive_dwell = passive_dwell;
1360 			cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1361 		} else {
1362 			cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1363 			cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1364 			if (iwl_mvm_is_cdb_supported(mvm)) {
1365 				cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1366 					active_dwell;
1367 				cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1368 					passive_dwell;
1369 			}
1370 		}
1371 	} else {
1372 		cmd->v1.extended_dwell = params->measurement_dwell ?
1373 			params->measurement_dwell : IWL_SCAN_DWELL_EXTENDED;
1374 		cmd->v1.active_dwell = active_dwell;
1375 		cmd->v1.passive_dwell = passive_dwell;
1376 		cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1377 
1378 		if (iwl_mvm_is_cdb_supported(mvm)) {
1379 			hb_timing = &scan_timing[params->hb_type];
1380 
1381 			cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1382 					cpu_to_le32(hb_timing->max_out_time);
1383 			cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1384 					cpu_to_le32(hb_timing->suspend_time);
1385 		}
1386 
1387 		if (iwl_mvm_cdb_scan_api(mvm)) {
1388 			cmd->v6.scan_priority =
1389 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1390 			cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1391 				cpu_to_le32(timing->max_out_time);
1392 			cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1393 				cpu_to_le32(timing->suspend_time);
1394 		} else {
1395 			cmd->v1.scan_priority =
1396 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1397 			cmd->v1.max_out_time =
1398 				cpu_to_le32(timing->max_out_time);
1399 			cmd->v1.suspend_time =
1400 				cpu_to_le32(timing->suspend_time);
1401 		}
1402 	}
1403 
1404 	if (iwl_mvm_is_regular_scan(params))
1405 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1406 	else
1407 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1408 }
1409 
1410 static u32 iwl_mvm_scan_umac_ooc_priority(struct iwl_mvm_scan_params *params)
1411 {
1412 	return iwl_mvm_is_regular_scan(params) ?
1413 		IWL_SCAN_PRIORITY_EXT_6 :
1414 		IWL_SCAN_PRIORITY_EXT_2;
1415 }
1416 
1417 static void
1418 iwl_mvm_scan_umac_dwell_v10(struct iwl_mvm *mvm,
1419 			    struct iwl_scan_general_params_v10 *general_params,
1420 			    struct iwl_mvm_scan_params *params)
1421 {
1422 	struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1423 	u8 active_dwell, passive_dwell;
1424 
1425 	timing = &scan_timing[params->type];
1426 	active_dwell = params->measurement_dwell ?
1427 		params->measurement_dwell : IWL_SCAN_DWELL_ACTIVE;
1428 	passive_dwell = params->measurement_dwell ?
1429 		params->measurement_dwell : IWL_SCAN_DWELL_PASSIVE;
1430 
1431 	general_params->adwell_default_social_chn =
1432 		IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1433 	general_params->adwell_default_2g = IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1434 	general_params->adwell_default_5g = IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1435 
1436 	/* if custom max budget was configured with debugfs */
1437 	if (IWL_MVM_ADWELL_MAX_BUDGET)
1438 		general_params->adwell_max_budget =
1439 			cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1440 	else if (params->ssids && params->ssids[0].ssid_len)
1441 		general_params->adwell_max_budget =
1442 			cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1443 	else
1444 		general_params->adwell_max_budget =
1445 			cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1446 
1447 	general_params->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1448 	general_params->max_out_of_time[SCAN_LB_LMAC_IDX] =
1449 		cpu_to_le32(timing->max_out_time);
1450 	general_params->suspend_time[SCAN_LB_LMAC_IDX] =
1451 		cpu_to_le32(timing->suspend_time);
1452 
1453 	hb_timing = &scan_timing[params->hb_type];
1454 
1455 	general_params->max_out_of_time[SCAN_HB_LMAC_IDX] =
1456 		cpu_to_le32(hb_timing->max_out_time);
1457 	general_params->suspend_time[SCAN_HB_LMAC_IDX] =
1458 		cpu_to_le32(hb_timing->suspend_time);
1459 
1460 	general_params->active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1461 	general_params->passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1462 	general_params->active_dwell[SCAN_HB_LMAC_IDX] = active_dwell;
1463 	general_params->passive_dwell[SCAN_HB_LMAC_IDX] = passive_dwell;
1464 }
1465 
1466 struct iwl_mvm_scan_channel_segment {
1467 	u8 start_idx;
1468 	u8 end_idx;
1469 	u8 first_channel_id;
1470 	u8 last_channel_id;
1471 	u8 channel_spacing_shift;
1472 	u8 band;
1473 };
1474 
1475 static const struct iwl_mvm_scan_channel_segment scan_channel_segments[] = {
1476 	{
1477 		.start_idx = 0,
1478 		.end_idx = 13,
1479 		.first_channel_id = 1,
1480 		.last_channel_id = 14,
1481 		.channel_spacing_shift = 0,
1482 		.band = PHY_BAND_24
1483 	},
1484 	{
1485 		.start_idx = 14,
1486 		.end_idx = 41,
1487 		.first_channel_id = 36,
1488 		.last_channel_id = 144,
1489 		.channel_spacing_shift = 2,
1490 		.band = PHY_BAND_5
1491 	},
1492 	{
1493 		.start_idx = 42,
1494 		.end_idx = 50,
1495 		.first_channel_id = 149,
1496 		.last_channel_id = 181,
1497 		.channel_spacing_shift = 2,
1498 		.band = PHY_BAND_5
1499 	},
1500 };
1501 
1502 static int iwl_mvm_scan_ch_and_band_to_idx(u8 channel_id, u8 band)
1503 {
1504 	int i, index;
1505 
1506 	if (!channel_id)
1507 		return -EINVAL;
1508 
1509 	for (i = 0; i < ARRAY_SIZE(scan_channel_segments); i++) {
1510 		const struct iwl_mvm_scan_channel_segment *ch_segment =
1511 			&scan_channel_segments[i];
1512 		u32 ch_offset;
1513 
1514 		if (ch_segment->band != band ||
1515 		    ch_segment->first_channel_id > channel_id ||
1516 		    ch_segment->last_channel_id < channel_id)
1517 			continue;
1518 
1519 		ch_offset = (channel_id - ch_segment->first_channel_id) >>
1520 			ch_segment->channel_spacing_shift;
1521 
1522 		index = scan_channel_segments[i].start_idx + ch_offset;
1523 		if (index < IWL_SCAN_NUM_CHANNELS)
1524 			return index;
1525 
1526 		break;
1527 	}
1528 
1529 	return -EINVAL;
1530 }
1531 
1532 static void iwl_mvm_scan_ch_add_n_aps_override(enum nl80211_iftype vif_type,
1533 					       u8 ch_id, u8 band, u8 *ch_bitmap,
1534 					       size_t bitmap_n_entries)
1535 {
1536 	int i;
1537 	static const u8 p2p_go_friendly_chs[] = {
1538 		36, 40, 44, 48, 149, 153, 157, 161, 165,
1539 	};
1540 
1541 	if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
1542 		return;
1543 
1544 	for (i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
1545 		if (p2p_go_friendly_chs[i] == ch_id) {
1546 			int ch_idx, bitmap_idx;
1547 
1548 			ch_idx = iwl_mvm_scan_ch_and_band_to_idx(ch_id, band);
1549 			if (ch_idx < 0)
1550 				return;
1551 
1552 			bitmap_idx = ch_idx / 8;
1553 			if (bitmap_idx >= bitmap_n_entries)
1554 				return;
1555 
1556 			ch_idx = ch_idx % 8;
1557 			ch_bitmap[bitmap_idx] |= BIT(ch_idx);
1558 
1559 			return;
1560 		}
1561 	}
1562 }
1563 
1564 static void
1565 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1566 			       struct ieee80211_channel **channels,
1567 			       int n_channels, u32 flags,
1568 			       struct iwl_scan_channel_cfg_umac *channel_cfg)
1569 {
1570 	int i;
1571 
1572 	for (i = 0; i < n_channels; i++) {
1573 		channel_cfg[i].flags = cpu_to_le32(flags);
1574 		channel_cfg[i].v1.channel_num = channels[i]->hw_value;
1575 		if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1576 			enum nl80211_band band = channels[i]->band;
1577 
1578 			channel_cfg[i].v2.band =
1579 				iwl_mvm_phy_band_from_nl80211(band);
1580 			channel_cfg[i].v2.iter_count = 1;
1581 			channel_cfg[i].v2.iter_interval = 0;
1582 		} else {
1583 			channel_cfg[i].v1.iter_count = 1;
1584 			channel_cfg[i].v1.iter_interval = 0;
1585 		}
1586 	}
1587 }
1588 
1589 static void
1590 iwl_mvm_umac_scan_cfg_channels_v4(struct iwl_mvm *mvm,
1591 				  struct ieee80211_channel **channels,
1592 				  struct iwl_scan_channel_params_v4 *cp,
1593 				  int n_channels, u32 flags,
1594 				  enum nl80211_iftype vif_type)
1595 {
1596 	u8 *bitmap = cp->adwell_ch_override_bitmap;
1597 	size_t bitmap_n_entries = ARRAY_SIZE(cp->adwell_ch_override_bitmap);
1598 	int i;
1599 
1600 	for (i = 0; i < n_channels; i++) {
1601 		enum nl80211_band band = channels[i]->band;
1602 		struct iwl_scan_channel_cfg_umac *cfg =
1603 			&cp->channel_config[i];
1604 
1605 		cfg->flags = cpu_to_le32(flags);
1606 		cfg->v2.channel_num = channels[i]->hw_value;
1607 		cfg->v2.band = iwl_mvm_phy_band_from_nl80211(band);
1608 		cfg->v2.iter_count = 1;
1609 		cfg->v2.iter_interval = 0;
1610 
1611 		iwl_mvm_scan_ch_add_n_aps_override(vif_type,
1612 						   cfg->v2.channel_num,
1613 						   cfg->v2.band, bitmap,
1614 						   bitmap_n_entries);
1615 	}
1616 }
1617 
1618 static u8 iwl_mvm_scan_umac_chan_flags_v2(struct iwl_mvm *mvm,
1619 					  struct iwl_mvm_scan_params *params,
1620 					  struct ieee80211_vif *vif)
1621 {
1622 	u8 flags = 0;
1623 
1624 	flags |= IWL_SCAN_CHANNEL_FLAG_ENABLE_CHAN_ORDER;
1625 
1626 	if (iwl_mvm_scan_use_ebs(mvm, vif))
1627 		flags |= IWL_SCAN_CHANNEL_FLAG_EBS |
1628 			IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1629 			IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1630 
1631 	/* set fragmented ebs for fragmented scan on HB channels */
1632 	if (iwl_mvm_is_scan_fragmented(params->hb_type))
1633 		flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1634 
1635 	return flags;
1636 }
1637 
1638 static u16 iwl_mvm_scan_umac_flags_v2(struct iwl_mvm *mvm,
1639 				      struct iwl_mvm_scan_params *params,
1640 				      struct ieee80211_vif *vif,
1641 				      int type)
1642 {
1643 	u16 flags = 0;
1644 
1645 	if (params->n_ssids == 0)
1646 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE;
1647 
1648 	if (iwl_mvm_is_scan_fragmented(params->type))
1649 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1;
1650 
1651 	if (iwl_mvm_is_scan_fragmented(params->hb_type))
1652 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2;
1653 
1654 	if (params->pass_all)
1655 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PASS_ALL;
1656 	else
1657 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_MATCH;
1658 
1659 	if (!iwl_mvm_is_regular_scan(params))
1660 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PERIODIC;
1661 
1662 	if (params->measurement_dwell ||
1663 	    mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1664 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTFY_ITER_COMPLETE;
1665 
1666 	if (IWL_MVM_ADWELL_ENABLE)
1667 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_ADAPTIVE_DWELL;
1668 
1669 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1670 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PREEMPTIVE;
1671 
1672 	return flags;
1673 }
1674 
1675 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1676 				   struct iwl_mvm_scan_params *params,
1677 				   struct ieee80211_vif *vif)
1678 {
1679 	u16 flags = 0;
1680 
1681 	if (params->n_ssids == 0)
1682 		flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1683 
1684 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1685 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1686 
1687 	if (iwl_mvm_is_scan_fragmented(params->type))
1688 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1689 
1690 	if (iwl_mvm_is_cdb_supported(mvm) &&
1691 	    iwl_mvm_is_scan_fragmented(params->hb_type))
1692 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1693 
1694 	if (iwl_mvm_rrm_scan_needed(mvm) &&
1695 	    fw_has_capa(&mvm->fw->ucode_capa,
1696 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1697 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1698 
1699 	if (params->pass_all)
1700 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1701 	else
1702 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1703 
1704 	if (!iwl_mvm_is_regular_scan(params))
1705 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1706 
1707 	if (params->measurement_dwell)
1708 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1709 
1710 #ifdef CONFIG_IWLWIFI_DEBUGFS
1711 	if (mvm->scan_iter_notif_enabled)
1712 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1713 #endif
1714 
1715 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1716 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1717 
1718 	if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE)
1719 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1720 
1721 	/*
1722 	 * Extended dwell is relevant only for low band to start with, as it is
1723 	 * being used for social channles only (1, 6, 11), so we can check
1724 	 * only scan type on low band also for CDB.
1725 	 */
1726 	if (iwl_mvm_is_regular_scan(params) &&
1727 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1728 	    !iwl_mvm_is_scan_fragmented(params->type) &&
1729 	    !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1730 	    !iwl_mvm_is_oce_supported(mvm))
1731 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1732 
1733 	if (iwl_mvm_is_oce_supported(mvm)) {
1734 		if ((params->flags &
1735 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1736 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1737 		/* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1738 		 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1739 		 * the same bit, we need to make sure that we use this bit here
1740 		 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1741 		 * used. */
1742 		if ((params->flags &
1743 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1744 		     !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1745 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1746 		if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1747 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1748 	}
1749 
1750 	return flags;
1751 }
1752 
1753 static int
1754 iwl_mvm_fill_scan_sched_params(struct iwl_mvm_scan_params *params,
1755 			       struct iwl_scan_umac_schedule *schedule,
1756 			       __le16 *delay)
1757 {
1758 	int i;
1759 	if (WARN_ON(!params->n_scan_plans ||
1760 		    params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1761 		return -EINVAL;
1762 
1763 	for (i = 0; i < params->n_scan_plans; i++) {
1764 		struct cfg80211_sched_scan_plan *scan_plan =
1765 			&params->scan_plans[i];
1766 
1767 		schedule[i].iter_count = scan_plan->iterations;
1768 		schedule[i].interval =
1769 			cpu_to_le16(scan_plan->interval);
1770 	}
1771 
1772 	/*
1773 	 * If the number of iterations of the last scan plan is set to
1774 	 * zero, it should run infinitely. However, this is not always the case.
1775 	 * For example, when regular scan is requested the driver sets one scan
1776 	 * plan with one iteration.
1777 	 */
1778 	if (!schedule[params->n_scan_plans - 1].iter_count)
1779 		schedule[params->n_scan_plans - 1].iter_count = 0xff;
1780 
1781 	*delay = cpu_to_le16(params->delay);
1782 
1783 	return 0;
1784 }
1785 
1786 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1787 			     struct iwl_mvm_scan_params *params,
1788 			     int type, int uid)
1789 {
1790 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1791 	struct iwl_scan_umac_chan_param *chan_param;
1792 	void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
1793 	void *sec_part = cmd_data + sizeof(struct iwl_scan_channel_cfg_umac) *
1794 		mvm->fw->ucode_capa.n_scan_channels;
1795 	struct iwl_scan_req_umac_tail_v2 *tail_v2 =
1796 		(struct iwl_scan_req_umac_tail_v2 *)sec_part;
1797 	struct iwl_scan_req_umac_tail_v1 *tail_v1;
1798 	struct iwl_ssid_ie *direct_scan;
1799 	int ret = 0;
1800 	u32 ssid_bitmap = 0;
1801 	u8 channel_flags = 0;
1802 	u16 gen_flags;
1803 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1804 
1805 	chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
1806 
1807 	iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1808 
1809 	mvm->scan_uid_status[uid] = type;
1810 
1811 	cmd->uid = cpu_to_le32(uid);
1812 	gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
1813 	cmd->general_flags = cpu_to_le16(gen_flags);
1814 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1815 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
1816 			cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
1817 							IWL_SCAN_NUM_OF_FRAGS;
1818 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
1819 			cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
1820 							IWL_SCAN_NUM_OF_FRAGS;
1821 
1822 		cmd->v8.general_flags2 =
1823 			IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
1824 	}
1825 
1826 	cmd->scan_start_mac_id = scan_vif->id;
1827 
1828 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1829 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1830 
1831 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1832 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1833 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1834 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1835 
1836 		/* set fragmented ebs for fragmented scan on HB channels */
1837 		if (iwl_mvm_is_frag_ebs_supported(mvm)) {
1838 			if (gen_flags &
1839 			    IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
1840 			    (!iwl_mvm_is_cdb_supported(mvm) &&
1841 			     gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
1842 				channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1843 		}
1844 	}
1845 
1846 	chan_param->flags = channel_flags;
1847 	chan_param->count = params->n_channels;
1848 
1849 	ret = iwl_mvm_fill_scan_sched_params(params, tail_v2->schedule,
1850 					     &tail_v2->delay);
1851 	if (ret)
1852 		return ret;
1853 
1854 	if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1855 		tail_v2->preq = params->preq;
1856 		direct_scan = tail_v2->direct_scan;
1857 	} else {
1858 		tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
1859 		iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
1860 						  &params->preq);
1861 		direct_scan = tail_v1->direct_scan;
1862 	}
1863 	iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
1864 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1865 				       params->n_channels, ssid_bitmap,
1866 				       cmd_data);
1867 	return 0;
1868 }
1869 
1870 static void
1871 iwl_mvm_scan_umac_fill_general_p_v10(struct iwl_mvm *mvm,
1872 				     struct iwl_mvm_scan_params *params,
1873 				     struct ieee80211_vif *vif,
1874 				     struct iwl_scan_general_params_v10 *gp,
1875 				     u16 gen_flags)
1876 {
1877 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1878 
1879 	iwl_mvm_scan_umac_dwell_v10(mvm, gp, params);
1880 
1881 	gp->flags = cpu_to_le16(gen_flags);
1882 
1883 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
1884 		gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
1885 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2)
1886 		gp->num_of_fragments[SCAN_HB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
1887 
1888 	gp->scan_start_mac_id = scan_vif->id;
1889 }
1890 
1891 static void
1892 iwl_mvm_scan_umac_fill_probe_p_v3(struct iwl_mvm_scan_params *params,
1893 				  struct iwl_scan_probe_params_v3 *pp)
1894 {
1895 	pp->preq = params->preq;
1896 	pp->ssid_num = params->n_ssids;
1897 	iwl_scan_build_ssids(params, pp->direct_scan, NULL);
1898 }
1899 
1900 static void
1901 iwl_mvm_scan_umac_fill_probe_p_v4(struct iwl_mvm_scan_params *params,
1902 				  struct iwl_scan_probe_params_v4 *pp,
1903 				  u32 *bitmap_ssid)
1904 {
1905 	pp->preq = params->preq;
1906 	iwl_scan_build_ssids(params, pp->direct_scan, bitmap_ssid);
1907 }
1908 
1909 static void
1910 iwl_mvm_scan_umac_fill_ch_p_v4(struct iwl_mvm *mvm,
1911 			       struct iwl_mvm_scan_params *params,
1912 			       struct ieee80211_vif *vif,
1913 			       struct iwl_scan_channel_params_v4 *cp,
1914 			       u32 channel_cfg_flags)
1915 {
1916 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
1917 	cp->count = params->n_channels;
1918 	cp->num_of_aps_override = IWL_SCAN_ADWELL_DEFAULT_N_APS_OVERRIDE;
1919 
1920 	iwl_mvm_umac_scan_cfg_channels_v4(mvm, params->channels, cp,
1921 					  params->n_channels,
1922 					  channel_cfg_flags,
1923 					  vif->type);
1924 }
1925 
1926 
1927 static int iwl_mvm_scan_umac_v12(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1928 				 struct iwl_mvm_scan_params *params, int type,
1929 				 int uid)
1930 {
1931 	struct iwl_scan_req_umac_v12 *cmd = mvm->scan_cmd;
1932 	struct iwl_scan_req_params_v12 *scan_p = &cmd->scan_params;
1933 	int ret;
1934 	u16 gen_flags;
1935 
1936 	mvm->scan_uid_status[uid] = type;
1937 
1938 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
1939 	cmd->uid = cpu_to_le32(uid);
1940 
1941 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
1942 	iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
1943 					     &scan_p->general_params,
1944 					     gen_flags);
1945 
1946 	 ret = iwl_mvm_fill_scan_sched_params(params,
1947 					      scan_p->periodic_params.schedule,
1948 					      &scan_p->periodic_params.delay);
1949 	if (ret)
1950 		return ret;
1951 
1952 	iwl_mvm_scan_umac_fill_probe_p_v3(params, &scan_p->probe_params);
1953 	iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
1954 				       &scan_p->channel_params, 0);
1955 
1956 	return 0;
1957 }
1958 
1959 static int iwl_mvm_scan_umac_v13(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1960 				 struct iwl_mvm_scan_params *params, int type,
1961 				 int uid)
1962 {
1963 	struct iwl_scan_req_umac_v13 *cmd = mvm->scan_cmd;
1964 	struct iwl_scan_req_params_v13 *scan_p = &cmd->scan_params;
1965 	int ret;
1966 	u16 gen_flags;
1967 	u32 bitmap_ssid = 0;
1968 
1969 	mvm->scan_uid_status[uid] = type;
1970 
1971 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
1972 	cmd->uid = cpu_to_le32(uid);
1973 
1974 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
1975 	iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
1976 					     &scan_p->general_params,
1977 					     gen_flags);
1978 
1979 	 ret = iwl_mvm_fill_scan_sched_params(params,
1980 					      scan_p->periodic_params.schedule,
1981 					      &scan_p->periodic_params.delay);
1982 	if (ret)
1983 		return ret;
1984 
1985 	iwl_mvm_scan_umac_fill_probe_p_v4(params, &scan_p->probe_params,
1986 					  &bitmap_ssid);
1987 	iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
1988 				       &scan_p->channel_params, bitmap_ssid);
1989 
1990 	return 0;
1991 }
1992 
1993 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1994 {
1995 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1996 }
1997 
1998 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1999 {
2000 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2001 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2002 
2003 	/* This looks a bit arbitrary, but the idea is that if we run
2004 	 * out of possible simultaneous scans and the userspace is
2005 	 * trying to run a scan type that is already running, we
2006 	 * return -EBUSY.  But if the userspace wants to start a
2007 	 * different type of scan, we stop the opposite type to make
2008 	 * space for the new request.  The reason is backwards
2009 	 * compatibility with old wpa_supplicant that wouldn't stop a
2010 	 * scheduled scan before starting a normal scan.
2011 	 */
2012 
2013 	/* FW supports only a single periodic scan */
2014 	if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2015 	    mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
2016 		return -EBUSY;
2017 
2018 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
2019 		return 0;
2020 
2021 	/* Use a switch, even though this is a bitmask, so that more
2022 	 * than one bits set will fall in default and we will warn.
2023 	 */
2024 	switch (type) {
2025 	case IWL_MVM_SCAN_REGULAR:
2026 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2027 			return -EBUSY;
2028 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2029 	case IWL_MVM_SCAN_SCHED:
2030 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2031 			return -EBUSY;
2032 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2033 	case IWL_MVM_SCAN_NETDETECT:
2034 		/* For non-unified images, there's no need to stop
2035 		 * anything for net-detect since the firmware is
2036 		 * restarted anyway.  This way, any sched scans that
2037 		 * were running will be restarted when we resume.
2038 		 */
2039 		if (!unified_image)
2040 			return 0;
2041 
2042 		/* If this is a unified image and we ran out of scans,
2043 		 * we need to stop something.  Prefer stopping regular
2044 		 * scans, because the results are useless at this
2045 		 * point, and we should be able to keep running
2046 		 * another scheduled scan while suspended.
2047 		 */
2048 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2049 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
2050 						 true);
2051 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2052 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
2053 						 true);
2054 		/* Something is wrong if no scan was running but we
2055 		 * ran out of scans.
2056 		 */
2057 		/* fall through */
2058 	default:
2059 		WARN_ON(1);
2060 		break;
2061 	}
2062 
2063 	return -EIO;
2064 }
2065 
2066 #define SCAN_TIMEOUT 20000
2067 
2068 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
2069 {
2070 	struct delayed_work *delayed_work = to_delayed_work(work);
2071 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
2072 					   scan_timeout_dwork);
2073 
2074 	IWL_ERR(mvm, "regular scan timed out\n");
2075 
2076 	iwl_force_nmi(mvm->trans);
2077 }
2078 
2079 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
2080 				   struct iwl_mvm_scan_params *params,
2081 				   struct ieee80211_vif *vif)
2082 {
2083 	if (iwl_mvm_is_cdb_supported(mvm)) {
2084 		params->type =
2085 			iwl_mvm_get_scan_type_band(mvm, vif,
2086 						   NL80211_BAND_2GHZ);
2087 		params->hb_type =
2088 			iwl_mvm_get_scan_type_band(mvm, vif,
2089 						   NL80211_BAND_5GHZ);
2090 	} else {
2091 		params->type = iwl_mvm_get_scan_type(mvm, vif);
2092 	}
2093 }
2094 
2095 struct iwl_scan_umac_handler {
2096 	u8 version;
2097 	int (*handler)(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2098 		       struct iwl_mvm_scan_params *params, int type, int uid);
2099 };
2100 
2101 #define IWL_SCAN_UMAC_HANDLER(_ver) {		\
2102 	.version = _ver,			\
2103 	.handler = iwl_mvm_scan_umac_v##_ver,	\
2104 }
2105 
2106 static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
2107 	/* set the newest version first to shorten the list traverse time */
2108 	IWL_SCAN_UMAC_HANDLER(13),
2109 	IWL_SCAN_UMAC_HANDLER(12),
2110 };
2111 
2112 static int iwl_mvm_build_scan_cmd(struct iwl_mvm *mvm,
2113 				  struct ieee80211_vif *vif,
2114 				  struct iwl_host_cmd *hcmd,
2115 				  struct iwl_mvm_scan_params *params,
2116 				  int type)
2117 {
2118 	int uid, i;
2119 	u8 scan_ver;
2120 
2121 	lockdep_assert_held(&mvm->mutex);
2122 	memset(mvm->scan_cmd, 0, ksize(mvm->scan_cmd));
2123 
2124 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2125 		hcmd->id = SCAN_OFFLOAD_REQUEST_CMD;
2126 
2127 		return iwl_mvm_scan_lmac(mvm, vif, params);
2128 	}
2129 
2130 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
2131 	if (uid < 0)
2132 		return uid;
2133 
2134 	hcmd->id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
2135 
2136 	scan_ver = iwl_mvm_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2137 					  SCAN_REQ_UMAC);
2138 
2139 	for (i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
2140 		const struct iwl_scan_umac_handler *ver_handler =
2141 			&iwl_scan_umac_handlers[i];
2142 
2143 		if (ver_handler->version != scan_ver)
2144 			continue;
2145 
2146 		return ver_handler->handler(mvm, vif, params, type, uid);
2147 	}
2148 
2149 	return iwl_mvm_scan_umac(mvm, vif, params, type, uid);
2150 }
2151 
2152 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2153 			   struct cfg80211_scan_request *req,
2154 			   struct ieee80211_scan_ies *ies)
2155 {
2156 	struct iwl_host_cmd hcmd = {
2157 		.len = { iwl_mvm_scan_size(mvm), },
2158 		.data = { mvm->scan_cmd, },
2159 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
2160 	};
2161 	struct iwl_mvm_scan_params params = {};
2162 	int ret;
2163 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
2164 
2165 	lockdep_assert_held(&mvm->mutex);
2166 
2167 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2168 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
2169 		return -EBUSY;
2170 	}
2171 
2172 	ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
2173 	if (ret)
2174 		return ret;
2175 
2176 	/* we should have failed registration if scan_cmd was NULL */
2177 	if (WARN_ON(!mvm->scan_cmd))
2178 		return -ENOMEM;
2179 
2180 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2181 		return -ENOBUFS;
2182 
2183 	params.n_ssids = req->n_ssids;
2184 	params.flags = req->flags;
2185 	params.n_channels = req->n_channels;
2186 	params.delay = 0;
2187 	params.ssids = req->ssids;
2188 	params.channels = req->channels;
2189 	params.mac_addr = req->mac_addr;
2190 	params.mac_addr_mask = req->mac_addr_mask;
2191 	params.no_cck = req->no_cck;
2192 	params.pass_all = true;
2193 	params.n_match_sets = 0;
2194 	params.match_sets = NULL;
2195 
2196 	params.scan_plans = &scan_plan;
2197 	params.n_scan_plans = 1;
2198 
2199 	iwl_mvm_fill_scan_type(mvm, &params, vif);
2200 
2201 	ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
2202 	if (ret < 0)
2203 		return ret;
2204 
2205 	params.measurement_dwell = ret;
2206 
2207 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
2208 
2209 	ret = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params,
2210 				     IWL_MVM_SCAN_REGULAR);
2211 
2212 	if (ret)
2213 		return ret;
2214 
2215 	iwl_mvm_pause_tcm(mvm, false);
2216 
2217 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2218 	if (ret) {
2219 		/* If the scan failed, it usually means that the FW was unable
2220 		 * to allocate the time events. Warn on it, but maybe we
2221 		 * should try to send the command again with different params.
2222 		 */
2223 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
2224 		iwl_mvm_resume_tcm(mvm);
2225 		return ret;
2226 	}
2227 
2228 	IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
2229 	mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
2230 	mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
2231 
2232 	schedule_delayed_work(&mvm->scan_timeout_dwork,
2233 			      msecs_to_jiffies(SCAN_TIMEOUT));
2234 
2235 	return 0;
2236 }
2237 
2238 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
2239 			     struct ieee80211_vif *vif,
2240 			     struct cfg80211_sched_scan_request *req,
2241 			     struct ieee80211_scan_ies *ies,
2242 			     int type)
2243 {
2244 	struct iwl_host_cmd hcmd = {
2245 		.len = { iwl_mvm_scan_size(mvm), },
2246 		.data = { mvm->scan_cmd, },
2247 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
2248 	};
2249 	struct iwl_mvm_scan_params params = {};
2250 	int ret;
2251 
2252 	lockdep_assert_held(&mvm->mutex);
2253 
2254 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2255 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
2256 		return -EBUSY;
2257 	}
2258 
2259 	ret = iwl_mvm_check_running_scans(mvm, type);
2260 	if (ret)
2261 		return ret;
2262 
2263 	/* we should have failed registration if scan_cmd was NULL */
2264 	if (WARN_ON(!mvm->scan_cmd))
2265 		return -ENOMEM;
2266 
2267 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2268 		return -ENOBUFS;
2269 
2270 	params.n_ssids = req->n_ssids;
2271 	params.flags = req->flags;
2272 	params.n_channels = req->n_channels;
2273 	params.ssids = req->ssids;
2274 	params.channels = req->channels;
2275 	params.mac_addr = req->mac_addr;
2276 	params.mac_addr_mask = req->mac_addr_mask;
2277 	params.no_cck = false;
2278 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
2279 	params.n_match_sets = req->n_match_sets;
2280 	params.match_sets = req->match_sets;
2281 	if (!req->n_scan_plans)
2282 		return -EINVAL;
2283 
2284 	params.n_scan_plans = req->n_scan_plans;
2285 	params.scan_plans = req->scan_plans;
2286 
2287 	iwl_mvm_fill_scan_type(mvm, &params, vif);
2288 
2289 	/* In theory, LMAC scans can handle a 32-bit delay, but since
2290 	 * waiting for over 18 hours to start the scan is a bit silly
2291 	 * and to keep it aligned with UMAC scans (which only support
2292 	 * 16-bit delays), trim it down to 16-bits.
2293 	 */
2294 	if (req->delay > U16_MAX) {
2295 		IWL_DEBUG_SCAN(mvm,
2296 			       "delay value is > 16-bits, set to max possible\n");
2297 		params.delay = U16_MAX;
2298 	} else {
2299 		params.delay = req->delay;
2300 	}
2301 
2302 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
2303 	if (ret)
2304 		return ret;
2305 
2306 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
2307 
2308 	ret = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
2309 
2310 	if (ret)
2311 		return ret;
2312 
2313 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2314 	if (!ret) {
2315 		IWL_DEBUG_SCAN(mvm,
2316 			       "Sched scan request was sent successfully\n");
2317 		mvm->scan_status |= type;
2318 	} else {
2319 		/* If the scan failed, it usually means that the FW was unable
2320 		 * to allocate the time events. Warn on it, but maybe we
2321 		 * should try to send the command again with different params.
2322 		 */
2323 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
2324 	}
2325 
2326 	return ret;
2327 }
2328 
2329 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
2330 					 struct iwl_rx_cmd_buffer *rxb)
2331 {
2332 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2333 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2334 	u32 uid = __le32_to_cpu(notif->uid);
2335 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
2336 
2337 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
2338 		return;
2339 
2340 	/* if the scan is already stopping, we don't need to notify mac80211 */
2341 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
2342 		struct cfg80211_scan_info info = {
2343 			.aborted = aborted,
2344 			.scan_start_tsf = mvm->scan_start,
2345 		};
2346 
2347 		memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
2348 		ieee80211_scan_completed(mvm->hw, &info);
2349 		mvm->scan_vif = NULL;
2350 		cancel_delayed_work(&mvm->scan_timeout_dwork);
2351 		iwl_mvm_resume_tcm(mvm);
2352 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
2353 		ieee80211_sched_scan_stopped(mvm->hw);
2354 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2355 	}
2356 
2357 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
2358 	IWL_DEBUG_SCAN(mvm,
2359 		       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
2360 		       uid, mvm->scan_uid_status[uid],
2361 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
2362 				"completed" : "aborted",
2363 		       iwl_mvm_ebs_status_str(notif->ebs_status));
2364 	IWL_DEBUG_SCAN(mvm,
2365 		       "Last line %d, Last iteration %d, Time from last iteration %d\n",
2366 		       notif->last_schedule, notif->last_iter,
2367 		       __le32_to_cpu(notif->time_from_last_iter));
2368 
2369 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
2370 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
2371 		mvm->last_ebs_successful = false;
2372 
2373 	mvm->scan_uid_status[uid] = 0;
2374 }
2375 
2376 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
2377 					      struct iwl_rx_cmd_buffer *rxb)
2378 {
2379 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2380 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
2381 
2382 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
2383 
2384 	IWL_DEBUG_SCAN(mvm,
2385 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
2386 		       notif->status, notif->scanned_channels);
2387 
2388 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
2389 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
2390 		ieee80211_sched_scan_results(mvm->hw);
2391 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
2392 	}
2393 
2394 	IWL_DEBUG_SCAN(mvm,
2395 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
2396 		       mvm->scan_start);
2397 }
2398 
2399 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
2400 {
2401 	struct iwl_umac_scan_abort cmd = {};
2402 	int uid, ret;
2403 
2404 	lockdep_assert_held(&mvm->mutex);
2405 
2406 	/* We should always get a valid index here, because we already
2407 	 * checked that this type of scan was running in the generic
2408 	 * code.
2409 	 */
2410 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
2411 	if (WARN_ON_ONCE(uid < 0))
2412 		return uid;
2413 
2414 	cmd.uid = cpu_to_le32(uid);
2415 
2416 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2417 
2418 	ret = iwl_mvm_send_cmd_pdu(mvm,
2419 				   iwl_cmd_id(SCAN_ABORT_UMAC,
2420 					      IWL_ALWAYS_LONG_GROUP, 0),
2421 				   0, sizeof(cmd), &cmd);
2422 	if (!ret)
2423 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
2424 
2425 	return ret;
2426 }
2427 
2428 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
2429 {
2430 	struct iwl_notification_wait wait_scan_done;
2431 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
2432 					      SCAN_OFFLOAD_COMPLETE, };
2433 	int ret;
2434 
2435 	lockdep_assert_held(&mvm->mutex);
2436 
2437 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2438 				   scan_done_notif,
2439 				   ARRAY_SIZE(scan_done_notif),
2440 				   NULL, NULL);
2441 
2442 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2443 
2444 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2445 		ret = iwl_mvm_umac_scan_abort(mvm, type);
2446 	else
2447 		ret = iwl_mvm_lmac_scan_abort(mvm);
2448 
2449 	if (ret) {
2450 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
2451 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2452 		return ret;
2453 	}
2454 
2455 	return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
2456 				     1 * HZ);
2457 }
2458 
2459 #define IWL_SCAN_REQ_UMAC_HANDLE_SIZE(_ver) {				\
2460 	case (_ver): return sizeof(struct iwl_scan_req_umac_v##_ver);	\
2461 }
2462 
2463 static int iwl_scan_req_umac_get_size(u8 scan_ver)
2464 {
2465 	switch (scan_ver) {
2466 		IWL_SCAN_REQ_UMAC_HANDLE_SIZE(13);
2467 		IWL_SCAN_REQ_UMAC_HANDLE_SIZE(12);
2468 	}
2469 
2470 	return 0;
2471 }
2472 
2473 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2474 {
2475 	int base_size, tail_size;
2476 	u8 scan_ver = iwl_mvm_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2477 					     SCAN_REQ_UMAC);
2478 
2479 	base_size = iwl_scan_req_umac_get_size(scan_ver);
2480 	if (base_size)
2481 		return base_size;
2482 
2483 
2484 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
2485 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
2486 	else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
2487 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
2488 	else if (iwl_mvm_cdb_scan_api(mvm))
2489 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
2490 	else
2491 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
2492 
2493 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2494 		if (iwl_mvm_is_scan_ext_chan_supported(mvm))
2495 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
2496 		else
2497 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
2498 
2499 		return base_size +
2500 			sizeof(struct iwl_scan_channel_cfg_umac) *
2501 				mvm->fw->ucode_capa.n_scan_channels +
2502 			tail_size;
2503 	}
2504 	return sizeof(struct iwl_scan_req_lmac) +
2505 		sizeof(struct iwl_scan_channel_cfg_lmac) *
2506 		mvm->fw->ucode_capa.n_scan_channels +
2507 		sizeof(struct iwl_scan_probe_req_v1);
2508 }
2509 
2510 /*
2511  * This function is used in nic restart flow, to inform mac80211 about scans
2512  * that was aborted by restart flow or by an assert.
2513  */
2514 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
2515 {
2516 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2517 		int uid, i;
2518 
2519 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
2520 		if (uid >= 0) {
2521 			struct cfg80211_scan_info info = {
2522 				.aborted = true,
2523 			};
2524 
2525 			ieee80211_scan_completed(mvm->hw, &info);
2526 			mvm->scan_uid_status[uid] = 0;
2527 		}
2528 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
2529 		if (uid >= 0 && !mvm->fw_restart) {
2530 			ieee80211_sched_scan_stopped(mvm->hw);
2531 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2532 			mvm->scan_uid_status[uid] = 0;
2533 		}
2534 
2535 		/* We shouldn't have any UIDs still set.  Loop over all the
2536 		 * UIDs to make sure there's nothing left there and warn if
2537 		 * any is found.
2538 		 */
2539 		for (i = 0; i < mvm->max_scans; i++) {
2540 			if (WARN_ONCE(mvm->scan_uid_status[i],
2541 				      "UMAC scan UID %d status was not cleaned\n",
2542 				      i))
2543 				mvm->scan_uid_status[i] = 0;
2544 		}
2545 	} else {
2546 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
2547 			struct cfg80211_scan_info info = {
2548 				.aborted = true,
2549 			};
2550 
2551 			ieee80211_scan_completed(mvm->hw, &info);
2552 		}
2553 
2554 		/* Sched scan will be restarted by mac80211 in
2555 		 * restart_hw, so do not report if FW is about to be
2556 		 * restarted.
2557 		 */
2558 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
2559 		    !mvm->fw_restart) {
2560 			ieee80211_sched_scan_stopped(mvm->hw);
2561 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2562 		}
2563 	}
2564 }
2565 
2566 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2567 {
2568 	int ret;
2569 
2570 	if (!(mvm->scan_status & type))
2571 		return 0;
2572 
2573 	if (iwl_mvm_is_radio_killed(mvm)) {
2574 		ret = 0;
2575 		goto out;
2576 	}
2577 
2578 	ret = iwl_mvm_scan_stop_wait(mvm, type);
2579 	if (!ret)
2580 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2581 out:
2582 	/* Clear the scan status so the next scan requests will
2583 	 * succeed and mark the scan as stopping, so that the Rx
2584 	 * handler doesn't do anything, as the scan was stopped from
2585 	 * above.
2586 	 */
2587 	mvm->scan_status &= ~type;
2588 
2589 	if (type == IWL_MVM_SCAN_REGULAR) {
2590 		cancel_delayed_work(&mvm->scan_timeout_dwork);
2591 		if (notify) {
2592 			struct cfg80211_scan_info info = {
2593 				.aborted = true,
2594 			};
2595 
2596 			ieee80211_scan_completed(mvm->hw, &info);
2597 		}
2598 	} else if (notify) {
2599 		ieee80211_sched_scan_stopped(mvm->hw);
2600 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2601 	}
2602 
2603 	return ret;
2604 }
2605