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