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