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