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