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