xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/scan.c (revision 860a9bed265146b10311bcadbbcef59c3af4454d)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 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 	mvm->scan_uid_status[uid] = type;
2285 
2286 	cmd->uid = cpu_to_le32(uid);
2287 	gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
2288 	cmd->general_flags = cpu_to_le16(gen_flags);
2289 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
2290 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
2291 			cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
2292 							IWL_SCAN_NUM_OF_FRAGS;
2293 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
2294 			cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
2295 							IWL_SCAN_NUM_OF_FRAGS;
2296 
2297 		cmd->v8.general_flags2 =
2298 			IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
2299 	}
2300 
2301 	cmd->scan_start_mac_id = scan_vif->id;
2302 
2303 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
2304 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
2305 
2306 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
2307 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
2308 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
2309 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
2310 
2311 		/* set fragmented ebs for fragmented scan on HB channels */
2312 		if (iwl_mvm_is_frag_ebs_supported(mvm)) {
2313 			if (gen_flags &
2314 			    IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
2315 			    (!iwl_mvm_is_cdb_supported(mvm) &&
2316 			     gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
2317 				channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
2318 		}
2319 	}
2320 
2321 	chan_param->flags = channel_flags;
2322 	chan_param->count = params->n_channels;
2323 
2324 	ret = iwl_mvm_fill_scan_sched_params(params, tail_v2->schedule,
2325 					     &tail_v2->delay);
2326 	if (ret) {
2327 		mvm->scan_uid_status[uid] = 0;
2328 		return ret;
2329 	}
2330 
2331 	if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
2332 		tail_v2->preq = params->preq;
2333 		direct_scan = tail_v2->direct_scan;
2334 	} else {
2335 		tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
2336 		iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
2337 						  &params->preq);
2338 		direct_scan = tail_v1->direct_scan;
2339 	}
2340 	iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
2341 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
2342 				       params->n_channels, ssid_bitmap,
2343 				       cmd_data);
2344 	return 0;
2345 }
2346 
2347 static void
2348 iwl_mvm_scan_umac_fill_general_p_v12(struct iwl_mvm *mvm,
2349 				     struct iwl_mvm_scan_params *params,
2350 				     struct ieee80211_vif *vif,
2351 				     struct iwl_scan_general_params_v11 *gp,
2352 				     u16 gen_flags, u8 gen_flags2,
2353 				     u32 version)
2354 {
2355 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2356 
2357 	iwl_mvm_scan_umac_dwell_v11(mvm, gp, params);
2358 
2359 	IWL_DEBUG_SCAN(mvm, "General: flags=0x%x, flags2=0x%x\n",
2360 		       gen_flags, gen_flags2);
2361 
2362 	gp->flags = cpu_to_le16(gen_flags);
2363 	gp->flags2 = gen_flags2;
2364 
2365 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
2366 		gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2367 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2)
2368 		gp->num_of_fragments[SCAN_HB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2369 
2370 	mvm->scan_link_id = 0;
2371 
2372 	if (version < 16) {
2373 		gp->scan_start_mac_or_link_id = scan_vif->id;
2374 	} else {
2375 		struct iwl_mvm_vif_link_info *link_info =
2376 			scan_vif->link[params->tsf_report_link_id];
2377 
2378 		mvm->scan_link_id = params->tsf_report_link_id;
2379 		if (!WARN_ON(!link_info))
2380 			gp->scan_start_mac_or_link_id = link_info->fw_link_id;
2381 	}
2382 }
2383 
2384 static void
2385 iwl_mvm_scan_umac_fill_probe_p_v3(struct iwl_mvm_scan_params *params,
2386 				  struct iwl_scan_probe_params_v3 *pp)
2387 {
2388 	pp->preq = params->preq;
2389 	pp->ssid_num = params->n_ssids;
2390 	iwl_scan_build_ssids(params, pp->direct_scan, NULL);
2391 }
2392 
2393 static void
2394 iwl_mvm_scan_umac_fill_probe_p_v4(struct iwl_mvm_scan_params *params,
2395 				  struct iwl_scan_probe_params_v4 *pp,
2396 				  u32 *bitmap_ssid)
2397 {
2398 	pp->preq = params->preq;
2399 	iwl_scan_build_ssids(params, pp->direct_scan, bitmap_ssid);
2400 }
2401 
2402 static void
2403 iwl_mvm_scan_umac_fill_ch_p_v4(struct iwl_mvm *mvm,
2404 			       struct iwl_mvm_scan_params *params,
2405 			       struct ieee80211_vif *vif,
2406 			       struct iwl_scan_channel_params_v4 *cp,
2407 			       u32 channel_cfg_flags)
2408 {
2409 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2410 	cp->count = params->n_channels;
2411 	cp->num_of_aps_override = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2412 
2413 	iwl_mvm_umac_scan_cfg_channels_v4(mvm, params->channels, cp,
2414 					  params->n_channels,
2415 					  channel_cfg_flags,
2416 					  vif->type);
2417 }
2418 
2419 static void
2420 iwl_mvm_scan_umac_fill_ch_p_v7(struct iwl_mvm *mvm,
2421 			       struct iwl_mvm_scan_params *params,
2422 			       struct ieee80211_vif *vif,
2423 			       struct iwl_scan_channel_params_v7 *cp,
2424 			       u32 channel_cfg_flags,
2425 			       u32 version)
2426 {
2427 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2428 	cp->count = params->n_channels;
2429 	cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2430 	cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2431 
2432 	iwl_mvm_umac_scan_cfg_channels_v7(mvm, params->channels, cp,
2433 					  params->n_channels,
2434 					  channel_cfg_flags,
2435 					  vif->type, version);
2436 
2437 	if (params->enable_6ghz_passive) {
2438 		struct ieee80211_supported_band *sband =
2439 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
2440 		u32 i;
2441 
2442 		for (i = 0; i < sband->n_channels; i++) {
2443 			struct ieee80211_channel *channel =
2444 				&sband->channels[i];
2445 
2446 			struct iwl_scan_channel_cfg_umac *cfg =
2447 				&cp->channel_config[cp->count];
2448 
2449 			if (!cfg80211_channel_is_psc(channel))
2450 				continue;
2451 
2452 			cfg->v5.channel_num = channel->hw_value;
2453 			cfg->v5.iter_count = 1;
2454 			cfg->v5.iter_interval = 0;
2455 
2456 			if (version < 17) {
2457 				cfg->flags = 0;
2458 				cfg->v2.band = PHY_BAND_6;
2459 			} else {
2460 				cfg->flags = cpu_to_le32(PHY_BAND_6 <<
2461 							 IWL_CHAN_CFG_FLAGS_BAND_POS);
2462 				cfg->v5.psd_20 =
2463 					IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
2464 			}
2465 			cp->count++;
2466 		}
2467 	}
2468 }
2469 
2470 static int iwl_mvm_scan_umac_v12(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2471 				 struct iwl_mvm_scan_params *params, int type,
2472 				 int uid)
2473 {
2474 	struct iwl_scan_req_umac_v12 *cmd = mvm->scan_cmd;
2475 	struct iwl_scan_req_params_v12 *scan_p = &cmd->scan_params;
2476 	int ret;
2477 	u16 gen_flags;
2478 
2479 	mvm->scan_uid_status[uid] = type;
2480 
2481 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(type));
2482 	cmd->uid = cpu_to_le32(uid);
2483 
2484 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2485 	iwl_mvm_scan_umac_fill_general_p_v12(mvm, params, vif,
2486 					     &scan_p->general_params,
2487 					     gen_flags, 0, 12);
2488 
2489 	ret = iwl_mvm_fill_scan_sched_params(params,
2490 					     scan_p->periodic_params.schedule,
2491 					     &scan_p->periodic_params.delay);
2492 	if (ret)
2493 		return ret;
2494 
2495 	iwl_mvm_scan_umac_fill_probe_p_v3(params, &scan_p->probe_params);
2496 	iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
2497 				       &scan_p->channel_params, 0);
2498 
2499 	return 0;
2500 }
2501 
2502 static int iwl_mvm_scan_umac_v14_and_above(struct iwl_mvm *mvm,
2503 					   struct ieee80211_vif *vif,
2504 					   struct iwl_mvm_scan_params *params,
2505 					   int type, int uid, u32 version)
2506 {
2507 	struct iwl_scan_req_umac_v17 *cmd = mvm->scan_cmd;
2508 	struct iwl_scan_req_params_v17 *scan_p = &cmd->scan_params;
2509 	struct iwl_scan_channel_params_v7 *cp = &scan_p->channel_params;
2510 	struct iwl_scan_probe_params_v4 *pb = &scan_p->probe_params;
2511 	int ret;
2512 	u16 gen_flags;
2513 	u8 gen_flags2;
2514 	u32 bitmap_ssid = 0;
2515 
2516 	mvm->scan_uid_status[uid] = type;
2517 
2518 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(type));
2519 	cmd->uid = cpu_to_le32(uid);
2520 
2521 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2522 
2523 	if (version >= 15)
2524 		gen_flags2 = iwl_mvm_scan_umac_flags2(mvm, params, vif, type);
2525 	else
2526 		gen_flags2 = 0;
2527 
2528 	iwl_mvm_scan_umac_fill_general_p_v12(mvm, params, vif,
2529 					     &scan_p->general_params,
2530 					     gen_flags, gen_flags2, version);
2531 
2532 	ret = iwl_mvm_fill_scan_sched_params(params,
2533 					     scan_p->periodic_params.schedule,
2534 					     &scan_p->periodic_params.delay);
2535 	if (ret)
2536 		return ret;
2537 
2538 	if (!params->scan_6ghz) {
2539 		iwl_mvm_scan_umac_fill_probe_p_v4(params,
2540 						  &scan_p->probe_params,
2541 						  &bitmap_ssid);
2542 		iwl_mvm_scan_umac_fill_ch_p_v7(mvm, params, vif,
2543 					       &scan_p->channel_params,
2544 					       bitmap_ssid,
2545 					       version);
2546 		return 0;
2547 	} else {
2548 		pb->preq = params->preq;
2549 	}
2550 
2551 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2552 	cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2553 	cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2554 
2555 	iwl_mvm_umac_scan_fill_6g_chan_list(mvm, params, pb);
2556 
2557 	cp->count = iwl_mvm_umac_scan_cfg_channels_v7_6g(mvm, params,
2558 							 params->n_channels,
2559 							 pb, cp, vif->type,
2560 							 version);
2561 	if (!cp->count) {
2562 		mvm->scan_uid_status[uid] = 0;
2563 		return -EINVAL;
2564 	}
2565 
2566 	if (!params->n_ssids ||
2567 	    (params->n_ssids == 1 && !params->ssids[0].ssid_len))
2568 		cp->flags |= IWL_SCAN_CHANNEL_FLAG_6G_PSC_NO_FILTER;
2569 
2570 	return 0;
2571 }
2572 
2573 static int iwl_mvm_scan_umac_v14(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2574 				 struct iwl_mvm_scan_params *params, int type,
2575 				 int uid)
2576 {
2577 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 14);
2578 }
2579 
2580 static int iwl_mvm_scan_umac_v15(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2581 				 struct iwl_mvm_scan_params *params, int type,
2582 				 int uid)
2583 {
2584 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 15);
2585 }
2586 
2587 static int iwl_mvm_scan_umac_v16(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2588 				 struct iwl_mvm_scan_params *params, int type,
2589 				 int uid)
2590 {
2591 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 16);
2592 }
2593 
2594 static int iwl_mvm_scan_umac_v17(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2595 				 struct iwl_mvm_scan_params *params, int type,
2596 				 int uid)
2597 {
2598 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 17);
2599 }
2600 
2601 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
2602 {
2603 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
2604 }
2605 
2606 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
2607 {
2608 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2609 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2610 
2611 	/* This looks a bit arbitrary, but the idea is that if we run
2612 	 * out of possible simultaneous scans and the userspace is
2613 	 * trying to run a scan type that is already running, we
2614 	 * return -EBUSY.  But if the userspace wants to start a
2615 	 * different type of scan, we stop the opposite type to make
2616 	 * space for the new request.  The reason is backwards
2617 	 * compatibility with old wpa_supplicant that wouldn't stop a
2618 	 * scheduled scan before starting a normal scan.
2619 	 */
2620 
2621 	/* FW supports only a single periodic scan */
2622 	if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2623 	    mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
2624 		return -EBUSY;
2625 
2626 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
2627 		return 0;
2628 
2629 	/* Use a switch, even though this is a bitmask, so that more
2630 	 * than one bits set will fall in default and we will warn.
2631 	 */
2632 	switch (type) {
2633 	case IWL_MVM_SCAN_REGULAR:
2634 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2635 			return -EBUSY;
2636 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2637 	case IWL_MVM_SCAN_SCHED:
2638 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2639 			return -EBUSY;
2640 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2641 	case IWL_MVM_SCAN_NETDETECT:
2642 		/* For non-unified images, there's no need to stop
2643 		 * anything for net-detect since the firmware is
2644 		 * restarted anyway.  This way, any sched scans that
2645 		 * were running will be restarted when we resume.
2646 		 */
2647 		if (!unified_image)
2648 			return 0;
2649 
2650 		/* If this is a unified image and we ran out of scans,
2651 		 * we need to stop something.  Prefer stopping regular
2652 		 * scans, because the results are useless at this
2653 		 * point, and we should be able to keep running
2654 		 * another scheduled scan while suspended.
2655 		 */
2656 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2657 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
2658 						 true);
2659 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2660 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
2661 						 true);
2662 		/* Something is wrong if no scan was running but we
2663 		 * ran out of scans.
2664 		 */
2665 		fallthrough;
2666 	default:
2667 		WARN_ON(1);
2668 		break;
2669 	}
2670 
2671 	return -EIO;
2672 }
2673 
2674 #define SCAN_TIMEOUT 30000
2675 
2676 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
2677 {
2678 	struct delayed_work *delayed_work = to_delayed_work(work);
2679 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
2680 					   scan_timeout_dwork);
2681 
2682 	IWL_ERR(mvm, "regular scan timed out\n");
2683 
2684 	iwl_force_nmi(mvm->trans);
2685 }
2686 
2687 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
2688 				   struct iwl_mvm_scan_params *params,
2689 				   struct ieee80211_vif *vif)
2690 {
2691 	if (iwl_mvm_is_cdb_supported(mvm)) {
2692 		params->type =
2693 			iwl_mvm_get_scan_type_band(mvm, vif,
2694 						   NL80211_BAND_2GHZ);
2695 		params->hb_type =
2696 			iwl_mvm_get_scan_type_band(mvm, vif,
2697 						   NL80211_BAND_5GHZ);
2698 	} else {
2699 		params->type = iwl_mvm_get_scan_type(mvm, vif);
2700 	}
2701 }
2702 
2703 struct iwl_scan_umac_handler {
2704 	u8 version;
2705 	int (*handler)(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2706 		       struct iwl_mvm_scan_params *params, int type, int uid);
2707 };
2708 
2709 #define IWL_SCAN_UMAC_HANDLER(_ver) {		\
2710 	.version = _ver,			\
2711 	.handler = iwl_mvm_scan_umac_v##_ver,	\
2712 }
2713 
2714 static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
2715 	/* set the newest version first to shorten the list traverse time */
2716 	IWL_SCAN_UMAC_HANDLER(17),
2717 	IWL_SCAN_UMAC_HANDLER(16),
2718 	IWL_SCAN_UMAC_HANDLER(15),
2719 	IWL_SCAN_UMAC_HANDLER(14),
2720 	IWL_SCAN_UMAC_HANDLER(12),
2721 };
2722 
2723 static void iwl_mvm_mei_scan_work(struct work_struct *wk)
2724 {
2725 	struct iwl_mei_scan_filter *scan_filter =
2726 		container_of(wk, struct iwl_mei_scan_filter, scan_work);
2727 	struct iwl_mvm *mvm =
2728 		container_of(scan_filter, struct iwl_mvm, mei_scan_filter);
2729 	struct iwl_mvm_csme_conn_info *info;
2730 	struct sk_buff *skb;
2731 	u8 bssid[ETH_ALEN];
2732 
2733 	mutex_lock(&mvm->mutex);
2734 	info = iwl_mvm_get_csme_conn_info(mvm);
2735 	memcpy(bssid, info->conn_info.bssid, ETH_ALEN);
2736 	mutex_unlock(&mvm->mutex);
2737 
2738 	while ((skb = skb_dequeue(&scan_filter->scan_res))) {
2739 		struct ieee80211_mgmt *mgmt = (void *)skb->data;
2740 
2741 		if (!memcmp(mgmt->bssid, bssid, ETH_ALEN))
2742 			ieee80211_rx_irqsafe(mvm->hw, skb);
2743 		else
2744 			kfree_skb(skb);
2745 	}
2746 }
2747 
2748 void iwl_mvm_mei_scan_filter_init(struct iwl_mei_scan_filter *mei_scan_filter)
2749 {
2750 	skb_queue_head_init(&mei_scan_filter->scan_res);
2751 	INIT_WORK(&mei_scan_filter->scan_work, iwl_mvm_mei_scan_work);
2752 }
2753 
2754 /* In case CSME is connected and has link protection set, this function will
2755  * override the scan request to scan only the associated channel and only for
2756  * the associated SSID.
2757  */
2758 static void iwl_mvm_mei_limited_scan(struct iwl_mvm *mvm,
2759 				     struct iwl_mvm_scan_params *params)
2760 {
2761 	struct iwl_mvm_csme_conn_info *info = iwl_mvm_get_csme_conn_info(mvm);
2762 	struct iwl_mei_conn_info *conn_info;
2763 	struct ieee80211_channel *chan;
2764 	int scan_iters, i;
2765 
2766 	if (!info) {
2767 		IWL_DEBUG_SCAN(mvm, "mei_limited_scan: no connection info\n");
2768 		return;
2769 	}
2770 
2771 	conn_info = &info->conn_info;
2772 	if (!info->conn_info.lp_state || !info->conn_info.ssid_len)
2773 		return;
2774 
2775 	if (!params->n_channels || !params->n_ssids)
2776 		return;
2777 
2778 	mvm->mei_scan_filter.is_mei_limited_scan = true;
2779 
2780 	chan = ieee80211_get_channel(mvm->hw->wiphy,
2781 				     ieee80211_channel_to_frequency(conn_info->channel,
2782 								    conn_info->band));
2783 	if (!chan) {
2784 		IWL_DEBUG_SCAN(mvm,
2785 			       "Failed to get CSME channel (chan=%u band=%u)\n",
2786 			       conn_info->channel, conn_info->band);
2787 		return;
2788 	}
2789 
2790 	/* The mei filtered scan must find the AP, otherwise CSME will
2791 	 * take the NIC ownership. Add several iterations on the channel to
2792 	 * make the scan more robust.
2793 	 */
2794 	scan_iters = min(IWL_MEI_SCAN_NUM_ITER, params->n_channels);
2795 	params->n_channels = scan_iters;
2796 	for (i = 0; i < scan_iters; i++)
2797 		params->channels[i] = chan;
2798 
2799 	IWL_DEBUG_SCAN(mvm, "Mei scan: num iterations=%u\n", scan_iters);
2800 
2801 	params->n_ssids = 1;
2802 	params->ssids[0].ssid_len = conn_info->ssid_len;
2803 	memcpy(params->ssids[0].ssid, conn_info->ssid, conn_info->ssid_len);
2804 }
2805 
2806 static int iwl_mvm_build_scan_cmd(struct iwl_mvm *mvm,
2807 				  struct ieee80211_vif *vif,
2808 				  struct iwl_host_cmd *hcmd,
2809 				  struct iwl_mvm_scan_params *params,
2810 				  int type)
2811 {
2812 	int uid, i, err;
2813 	u8 scan_ver;
2814 
2815 	lockdep_assert_held(&mvm->mutex);
2816 	memset(mvm->scan_cmd, 0, mvm->scan_cmd_size);
2817 
2818 	iwl_mvm_mei_limited_scan(mvm, params);
2819 
2820 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2821 		hcmd->id = SCAN_OFFLOAD_REQUEST_CMD;
2822 
2823 		return iwl_mvm_scan_lmac(mvm, vif, params);
2824 	}
2825 
2826 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
2827 	if (uid < 0)
2828 		return uid;
2829 
2830 	hcmd->id = WIDE_ID(IWL_ALWAYS_LONG_GROUP, SCAN_REQ_UMAC);
2831 
2832 	scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC,
2833 					 IWL_FW_CMD_VER_UNKNOWN);
2834 
2835 	for (i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
2836 		const struct iwl_scan_umac_handler *ver_handler =
2837 			&iwl_scan_umac_handlers[i];
2838 
2839 		if (ver_handler->version != scan_ver)
2840 			continue;
2841 
2842 		return ver_handler->handler(mvm, vif, params, type, uid);
2843 	}
2844 
2845 	err = iwl_mvm_scan_umac(mvm, vif, params, type, uid);
2846 	if (err)
2847 		return err;
2848 
2849 	return uid;
2850 }
2851 
2852 struct iwl_mvm_scan_respect_p2p_go_iter_data {
2853 	struct ieee80211_vif *current_vif;
2854 	bool p2p_go;
2855 	enum nl80211_band band;
2856 };
2857 
2858 static void iwl_mvm_scan_respect_p2p_go_iter(void *_data, u8 *mac,
2859 					     struct ieee80211_vif *vif)
2860 {
2861 	struct iwl_mvm_scan_respect_p2p_go_iter_data *data = _data;
2862 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2863 
2864 	/* exclude the given vif */
2865 	if (vif == data->current_vif)
2866 		return;
2867 
2868 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p) {
2869 		u32 link_id;
2870 
2871 		for (link_id = 0;
2872 		     link_id < ARRAY_SIZE(mvmvif->link);
2873 		     link_id++) {
2874 			struct iwl_mvm_vif_link_info *link =
2875 				mvmvif->link[link_id];
2876 
2877 			if (link && link->phy_ctxt->id < NUM_PHY_CTX &&
2878 			    (data->band == NUM_NL80211_BANDS ||
2879 			     link->phy_ctxt->channel->band == data->band)) {
2880 				data->p2p_go = true;
2881 				break;
2882 			}
2883 		}
2884 	}
2885 }
2886 
2887 static bool _iwl_mvm_get_respect_p2p_go(struct iwl_mvm *mvm,
2888 					struct ieee80211_vif *vif,
2889 					bool low_latency,
2890 					enum nl80211_band band)
2891 {
2892 	struct iwl_mvm_scan_respect_p2p_go_iter_data data = {
2893 		.current_vif = vif,
2894 		.p2p_go = false,
2895 		.band = band,
2896 	};
2897 
2898 	if (!low_latency)
2899 		return false;
2900 
2901 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
2902 						   IEEE80211_IFACE_ITER_NORMAL,
2903 						   iwl_mvm_scan_respect_p2p_go_iter,
2904 						   &data);
2905 
2906 	return data.p2p_go;
2907 }
2908 
2909 static bool iwl_mvm_get_respect_p2p_go_band(struct iwl_mvm *mvm,
2910 					    struct ieee80211_vif *vif,
2911 					    enum nl80211_band band)
2912 {
2913 	bool low_latency = iwl_mvm_low_latency_band(mvm, band);
2914 
2915 	return _iwl_mvm_get_respect_p2p_go(mvm, vif, low_latency, band);
2916 }
2917 
2918 static bool iwl_mvm_get_respect_p2p_go(struct iwl_mvm *mvm,
2919 				       struct ieee80211_vif *vif)
2920 {
2921 	bool low_latency = iwl_mvm_low_latency(mvm);
2922 
2923 	return _iwl_mvm_get_respect_p2p_go(mvm, vif, low_latency,
2924 					   NUM_NL80211_BANDS);
2925 }
2926 
2927 static void iwl_mvm_fill_respect_p2p_go(struct iwl_mvm *mvm,
2928 					struct iwl_mvm_scan_params *params,
2929 					struct ieee80211_vif *vif)
2930 {
2931 	if (iwl_mvm_is_cdb_supported(mvm)) {
2932 		params->respect_p2p_go =
2933 			iwl_mvm_get_respect_p2p_go_band(mvm, vif,
2934 							NL80211_BAND_2GHZ);
2935 		params->respect_p2p_go_hb =
2936 			iwl_mvm_get_respect_p2p_go_band(mvm, vif,
2937 							NL80211_BAND_5GHZ);
2938 	} else {
2939 		params->respect_p2p_go = iwl_mvm_get_respect_p2p_go(mvm, vif);
2940 	}
2941 }
2942 
2943 static int _iwl_mvm_single_scan_start(struct iwl_mvm *mvm,
2944 				      struct ieee80211_vif *vif,
2945 				      struct cfg80211_scan_request *req,
2946 				      struct ieee80211_scan_ies *ies,
2947 				      int type)
2948 {
2949 	struct iwl_host_cmd hcmd = {
2950 		.len = { iwl_mvm_scan_size(mvm), },
2951 		.data = { mvm->scan_cmd, },
2952 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
2953 	};
2954 	struct iwl_mvm_scan_params params = {};
2955 	int ret, uid;
2956 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
2957 
2958 	lockdep_assert_held(&mvm->mutex);
2959 
2960 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2961 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
2962 		return -EBUSY;
2963 	}
2964 
2965 	ret = iwl_mvm_check_running_scans(mvm, type);
2966 	if (ret)
2967 		return ret;
2968 
2969 	/* we should have failed registration if scan_cmd was NULL */
2970 	if (WARN_ON(!mvm->scan_cmd))
2971 		return -ENOMEM;
2972 
2973 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2974 		return -ENOBUFS;
2975 
2976 	params.n_ssids = req->n_ssids;
2977 	params.flags = req->flags;
2978 	params.n_channels = req->n_channels;
2979 	params.delay = 0;
2980 	params.ssids = req->ssids;
2981 	params.channels = req->channels;
2982 	params.mac_addr = req->mac_addr;
2983 	params.mac_addr_mask = req->mac_addr_mask;
2984 	params.no_cck = req->no_cck;
2985 	params.pass_all = true;
2986 	params.n_match_sets = 0;
2987 	params.match_sets = NULL;
2988 	ether_addr_copy(params.bssid, req->bssid);
2989 
2990 	params.scan_plans = &scan_plan;
2991 	params.n_scan_plans = 1;
2992 
2993 	params.n_6ghz_params = req->n_6ghz_params;
2994 	params.scan_6ghz_params = req->scan_6ghz_params;
2995 	params.scan_6ghz = req->scan_6ghz;
2996 	iwl_mvm_fill_scan_type(mvm, &params, vif);
2997 	iwl_mvm_fill_respect_p2p_go(mvm, &params, vif);
2998 
2999 	if (req->duration)
3000 		params.iter_notif = true;
3001 
3002 	params.tsf_report_link_id = req->tsf_report_link_id;
3003 	if (params.tsf_report_link_id < 0) {
3004 		if (vif->active_links)
3005 			params.tsf_report_link_id = __ffs(vif->active_links);
3006 		else
3007 			params.tsf_report_link_id = 0;
3008 	}
3009 
3010 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
3011 
3012 	iwl_mvm_scan_6ghz_passive_scan(mvm, &params, vif);
3013 
3014 	uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
3015 
3016 	if (uid < 0)
3017 		return uid;
3018 
3019 	iwl_mvm_pause_tcm(mvm, false);
3020 
3021 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
3022 	if (ret) {
3023 		/* If the scan failed, it usually means that the FW was unable
3024 		 * to allocate the time events. Warn on it, but maybe we
3025 		 * should try to send the command again with different params.
3026 		 */
3027 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
3028 		iwl_mvm_resume_tcm(mvm);
3029 		mvm->scan_uid_status[uid] = 0;
3030 		return ret;
3031 	}
3032 
3033 	IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
3034 	mvm->scan_status |= type;
3035 
3036 	if (type == IWL_MVM_SCAN_REGULAR) {
3037 		mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
3038 		schedule_delayed_work(&mvm->scan_timeout_dwork,
3039 				      msecs_to_jiffies(SCAN_TIMEOUT));
3040 	}
3041 
3042 	if (params.enable_6ghz_passive)
3043 		mvm->last_6ghz_passive_scan_jiffies = jiffies;
3044 
3045 	return 0;
3046 }
3047 
3048 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3049 			   struct cfg80211_scan_request *req,
3050 			   struct ieee80211_scan_ies *ies)
3051 {
3052 	return _iwl_mvm_single_scan_start(mvm, vif, req, ies,
3053 					  IWL_MVM_SCAN_REGULAR);
3054 }
3055 
3056 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
3057 			     struct ieee80211_vif *vif,
3058 			     struct cfg80211_sched_scan_request *req,
3059 			     struct ieee80211_scan_ies *ies,
3060 			     int type)
3061 {
3062 	struct iwl_host_cmd hcmd = {
3063 		.len = { iwl_mvm_scan_size(mvm), },
3064 		.data = { mvm->scan_cmd, },
3065 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
3066 	};
3067 	struct iwl_mvm_scan_params params = {};
3068 	int ret, uid;
3069 	int i, j;
3070 	bool non_psc_included = false;
3071 
3072 	lockdep_assert_held(&mvm->mutex);
3073 
3074 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
3075 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
3076 		return -EBUSY;
3077 	}
3078 
3079 	ret = iwl_mvm_check_running_scans(mvm, type);
3080 	if (ret)
3081 		return ret;
3082 
3083 	/* we should have failed registration if scan_cmd was NULL */
3084 	if (WARN_ON(!mvm->scan_cmd))
3085 		return -ENOMEM;
3086 
3087 
3088 	params.n_ssids = req->n_ssids;
3089 	params.flags = req->flags;
3090 	params.n_channels = req->n_channels;
3091 	params.ssids = req->ssids;
3092 	params.channels = req->channels;
3093 	params.mac_addr = req->mac_addr;
3094 	params.mac_addr_mask = req->mac_addr_mask;
3095 	params.no_cck = false;
3096 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
3097 	params.n_match_sets = req->n_match_sets;
3098 	params.match_sets = req->match_sets;
3099 	eth_broadcast_addr(params.bssid);
3100 	if (!req->n_scan_plans)
3101 		return -EINVAL;
3102 
3103 	params.n_scan_plans = req->n_scan_plans;
3104 	params.scan_plans = req->scan_plans;
3105 
3106 	iwl_mvm_fill_scan_type(mvm, &params, vif);
3107 	iwl_mvm_fill_respect_p2p_go(mvm, &params, vif);
3108 
3109 	/* In theory, LMAC scans can handle a 32-bit delay, but since
3110 	 * waiting for over 18 hours to start the scan is a bit silly
3111 	 * and to keep it aligned with UMAC scans (which only support
3112 	 * 16-bit delays), trim it down to 16-bits.
3113 	 */
3114 	if (req->delay > U16_MAX) {
3115 		IWL_DEBUG_SCAN(mvm,
3116 			       "delay value is > 16-bits, set to max possible\n");
3117 		params.delay = U16_MAX;
3118 	} else {
3119 		params.delay = req->delay;
3120 	}
3121 
3122 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
3123 	if (ret)
3124 		return ret;
3125 
3126 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
3127 
3128 	/* for 6 GHZ band only PSC channels need to be added */
3129 	for (i = 0; i < params.n_channels; i++) {
3130 		struct ieee80211_channel *channel = params.channels[i];
3131 
3132 		if (channel->band == NL80211_BAND_6GHZ &&
3133 		    !cfg80211_channel_is_psc(channel)) {
3134 			non_psc_included = true;
3135 			break;
3136 		}
3137 	}
3138 
3139 	if (non_psc_included) {
3140 		params.channels = kmemdup(params.channels,
3141 					  sizeof(params.channels[0]) *
3142 					  params.n_channels,
3143 					  GFP_KERNEL);
3144 		if (!params.channels)
3145 			return -ENOMEM;
3146 
3147 		for (i = j = 0; i < params.n_channels; i++) {
3148 			if (params.channels[i]->band == NL80211_BAND_6GHZ &&
3149 			    !cfg80211_channel_is_psc(params.channels[i]))
3150 				continue;
3151 			params.channels[j++] = params.channels[i];
3152 		}
3153 		params.n_channels = j;
3154 	}
3155 
3156 	if (non_psc_included &&
3157 	    !iwl_mvm_scan_fits(mvm, req->n_ssids, ies, params.n_channels)) {
3158 		kfree(params.channels);
3159 		return -ENOBUFS;
3160 	}
3161 
3162 	uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
3163 
3164 	if (non_psc_included)
3165 		kfree(params.channels);
3166 	if (uid < 0)
3167 		return uid;
3168 
3169 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
3170 	if (!ret) {
3171 		IWL_DEBUG_SCAN(mvm,
3172 			       "Sched scan request was sent successfully\n");
3173 		mvm->scan_status |= type;
3174 	} else {
3175 		/* If the scan failed, it usually means that the FW was unable
3176 		 * to allocate the time events. Warn on it, but maybe we
3177 		 * should try to send the command again with different params.
3178 		 */
3179 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
3180 		mvm->scan_uid_status[uid] = 0;
3181 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3182 	}
3183 
3184 	return ret;
3185 }
3186 
3187 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
3188 					 struct iwl_rx_cmd_buffer *rxb)
3189 {
3190 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3191 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
3192 	u32 uid = __le32_to_cpu(notif->uid);
3193 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
3194 
3195 	mvm->mei_scan_filter.is_mei_limited_scan = false;
3196 
3197 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
3198 		return;
3199 
3200 	/* if the scan is already stopping, we don't need to notify mac80211 */
3201 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
3202 		struct cfg80211_scan_info info = {
3203 			.aborted = aborted,
3204 			.scan_start_tsf = mvm->scan_start,
3205 		};
3206 		struct iwl_mvm_vif *scan_vif = mvm->scan_vif;
3207 		struct iwl_mvm_vif_link_info *link_info =
3208 			scan_vif->link[mvm->scan_link_id];
3209 
3210 		/* It is possible that by the time the scan is complete the link
3211 		 * was already removed and is not valid.
3212 		 */
3213 		if (link_info)
3214 			memcpy(info.tsf_bssid, link_info->bssid, ETH_ALEN);
3215 		else
3216 			IWL_DEBUG_SCAN(mvm, "Scan link is no longer valid\n");
3217 
3218 		ieee80211_scan_completed(mvm->hw, &info);
3219 		mvm->scan_vif = NULL;
3220 		cancel_delayed_work(&mvm->scan_timeout_dwork);
3221 		iwl_mvm_resume_tcm(mvm);
3222 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
3223 		ieee80211_sched_scan_stopped(mvm->hw);
3224 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3225 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_INT_MLO) {
3226 		IWL_DEBUG_SCAN(mvm, "Internal MLO scan completed\n");
3227 	}
3228 
3229 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
3230 	IWL_DEBUG_SCAN(mvm,
3231 		       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
3232 		       uid, mvm->scan_uid_status[uid],
3233 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
3234 				"completed" : "aborted",
3235 		       iwl_mvm_ebs_status_str(notif->ebs_status));
3236 	IWL_DEBUG_SCAN(mvm,
3237 		       "Last line %d, Last iteration %d, Time from last iteration %d\n",
3238 		       notif->last_schedule, notif->last_iter,
3239 		       __le32_to_cpu(notif->time_from_last_iter));
3240 
3241 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
3242 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
3243 		mvm->last_ebs_successful = false;
3244 
3245 	mvm->scan_uid_status[uid] = 0;
3246 }
3247 
3248 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
3249 					      struct iwl_rx_cmd_buffer *rxb)
3250 {
3251 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3252 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
3253 
3254 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
3255 
3256 	IWL_DEBUG_SCAN(mvm,
3257 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
3258 		       notif->status, notif->scanned_channels);
3259 
3260 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
3261 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
3262 		ieee80211_sched_scan_results(mvm->hw);
3263 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
3264 	}
3265 
3266 	IWL_DEBUG_SCAN(mvm,
3267 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
3268 		       mvm->scan_start);
3269 }
3270 
3271 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
3272 {
3273 	struct iwl_umac_scan_abort cmd = {};
3274 	int uid, ret;
3275 
3276 	lockdep_assert_held(&mvm->mutex);
3277 
3278 	/* We should always get a valid index here, because we already
3279 	 * checked that this type of scan was running in the generic
3280 	 * code.
3281 	 */
3282 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
3283 	if (WARN_ON_ONCE(uid < 0))
3284 		return uid;
3285 
3286 	cmd.uid = cpu_to_le32(uid);
3287 
3288 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
3289 
3290 	ret = iwl_mvm_send_cmd_pdu(mvm,
3291 				   WIDE_ID(IWL_ALWAYS_LONG_GROUP, SCAN_ABORT_UMAC),
3292 				   0, sizeof(cmd), &cmd);
3293 	if (!ret)
3294 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
3295 
3296 	return ret;
3297 }
3298 
3299 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
3300 {
3301 	struct iwl_notification_wait wait_scan_done;
3302 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
3303 					      SCAN_OFFLOAD_COMPLETE, };
3304 	int ret;
3305 
3306 	lockdep_assert_held(&mvm->mutex);
3307 
3308 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
3309 				   scan_done_notif,
3310 				   ARRAY_SIZE(scan_done_notif),
3311 				   NULL, NULL);
3312 
3313 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
3314 
3315 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
3316 		ret = iwl_mvm_umac_scan_abort(mvm, type);
3317 	else
3318 		ret = iwl_mvm_lmac_scan_abort(mvm);
3319 
3320 	if (ret) {
3321 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
3322 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
3323 		return ret;
3324 	}
3325 
3326 	return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
3327 				     1 * HZ);
3328 }
3329 
3330 static size_t iwl_scan_req_umac_get_size(u8 scan_ver)
3331 {
3332 	switch (scan_ver) {
3333 	case 12:
3334 		return sizeof(struct iwl_scan_req_umac_v12);
3335 	case 14:
3336 	case 15:
3337 	case 16:
3338 	case 17:
3339 		return sizeof(struct iwl_scan_req_umac_v17);
3340 	}
3341 
3342 	return 0;
3343 }
3344 
3345 size_t iwl_mvm_scan_size(struct iwl_mvm *mvm)
3346 {
3347 	int base_size, tail_size;
3348 	u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC,
3349 					    IWL_FW_CMD_VER_UNKNOWN);
3350 
3351 	base_size = iwl_scan_req_umac_get_size(scan_ver);
3352 	if (base_size)
3353 		return base_size;
3354 
3355 
3356 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
3357 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
3358 	else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
3359 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
3360 	else if (iwl_mvm_cdb_scan_api(mvm))
3361 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
3362 	else
3363 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
3364 
3365 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
3366 		if (iwl_mvm_is_scan_ext_chan_supported(mvm))
3367 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
3368 		else
3369 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
3370 
3371 		return base_size +
3372 			sizeof(struct iwl_scan_channel_cfg_umac) *
3373 				mvm->fw->ucode_capa.n_scan_channels +
3374 			tail_size;
3375 	}
3376 	return sizeof(struct iwl_scan_req_lmac) +
3377 		sizeof(struct iwl_scan_channel_cfg_lmac) *
3378 		mvm->fw->ucode_capa.n_scan_channels +
3379 		sizeof(struct iwl_scan_probe_req_v1);
3380 }
3381 
3382 /*
3383  * This function is used in nic restart flow, to inform mac80211 about scans
3384  * that was aborted by restart flow or by an assert.
3385  */
3386 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
3387 {
3388 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
3389 		int uid, i;
3390 
3391 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
3392 		if (uid >= 0) {
3393 			struct cfg80211_scan_info info = {
3394 				.aborted = true,
3395 			};
3396 
3397 			cancel_delayed_work(&mvm->scan_timeout_dwork);
3398 
3399 			ieee80211_scan_completed(mvm->hw, &info);
3400 			mvm->scan_uid_status[uid] = 0;
3401 		}
3402 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
3403 		if (uid >= 0) {
3404 			/* Sched scan will be restarted by mac80211 in
3405 			 * restart_hw, so do not report if FW is about to be
3406 			 * restarted.
3407 			 */
3408 			if (!mvm->fw_restart)
3409 				ieee80211_sched_scan_stopped(mvm->hw);
3410 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3411 			mvm->scan_uid_status[uid] = 0;
3412 		}
3413 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_INT_MLO);
3414 		if (uid >= 0) {
3415 			IWL_DEBUG_SCAN(mvm, "Internal MLO scan aborted\n");
3416 			mvm->scan_uid_status[uid] = 0;
3417 		}
3418 
3419 		uid = iwl_mvm_scan_uid_by_status(mvm,
3420 						 IWL_MVM_SCAN_STOPPING_REGULAR);
3421 		if (uid >= 0)
3422 			mvm->scan_uid_status[uid] = 0;
3423 
3424 		uid = iwl_mvm_scan_uid_by_status(mvm,
3425 						 IWL_MVM_SCAN_STOPPING_SCHED);
3426 		if (uid >= 0)
3427 			mvm->scan_uid_status[uid] = 0;
3428 
3429 		uid = iwl_mvm_scan_uid_by_status(mvm,
3430 						 IWL_MVM_SCAN_STOPPING_INT_MLO);
3431 		if (uid >= 0)
3432 			mvm->scan_uid_status[uid] = 0;
3433 
3434 		/* We shouldn't have any UIDs still set.  Loop over all the
3435 		 * UIDs to make sure there's nothing left there and warn if
3436 		 * any is found.
3437 		 */
3438 		for (i = 0; i < mvm->max_scans; i++) {
3439 			if (WARN_ONCE(mvm->scan_uid_status[i],
3440 				      "UMAC scan UID %d status was not cleaned\n",
3441 				      i))
3442 				mvm->scan_uid_status[i] = 0;
3443 		}
3444 	} else {
3445 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
3446 			struct cfg80211_scan_info info = {
3447 				.aborted = true,
3448 			};
3449 
3450 			cancel_delayed_work(&mvm->scan_timeout_dwork);
3451 			ieee80211_scan_completed(mvm->hw, &info);
3452 		}
3453 
3454 		/* Sched scan will be restarted by mac80211 in
3455 		 * restart_hw, so do not report if FW is about to be
3456 		 * restarted.
3457 		 */
3458 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
3459 		    !mvm->fw_restart) {
3460 			ieee80211_sched_scan_stopped(mvm->hw);
3461 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3462 		}
3463 	}
3464 }
3465 
3466 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
3467 {
3468 	int ret;
3469 
3470 	if (!(mvm->scan_status & type))
3471 		return 0;
3472 
3473 	if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
3474 		ret = 0;
3475 		goto out;
3476 	}
3477 
3478 	ret = iwl_mvm_scan_stop_wait(mvm, type);
3479 	if (!ret)
3480 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
3481 out:
3482 	/* Clear the scan status so the next scan requests will
3483 	 * succeed and mark the scan as stopping, so that the Rx
3484 	 * handler doesn't do anything, as the scan was stopped from
3485 	 * above.
3486 	 */
3487 	mvm->scan_status &= ~type;
3488 
3489 	if (type == IWL_MVM_SCAN_REGULAR) {
3490 		cancel_delayed_work(&mvm->scan_timeout_dwork);
3491 		if (notify) {
3492 			struct cfg80211_scan_info info = {
3493 				.aborted = true,
3494 			};
3495 
3496 			ieee80211_scan_completed(mvm->hw, &info);
3497 		}
3498 	} else if (notify) {
3499 		ieee80211_sched_scan_stopped(mvm->hw);
3500 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3501 	}
3502 
3503 	return ret;
3504 }
3505 
3506 int iwl_mvm_int_mlo_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3507 			       struct ieee80211_channel **channels,
3508 			       size_t n_channels)
3509 {
3510 	struct cfg80211_scan_request *req = NULL;
3511 	struct ieee80211_scan_ies ies = {};
3512 	size_t size, i;
3513 	int ret;
3514 
3515 	lockdep_assert_held(&mvm->mutex);
3516 
3517 	IWL_DEBUG_SCAN(mvm, "Starting Internal MLO scan: n_channels=%zu\n",
3518 		       n_channels);
3519 
3520 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
3521 		return -EINVAL;
3522 
3523 	size = struct_size(req, channels, n_channels);
3524 	req = kzalloc(size, GFP_KERNEL);
3525 	if (!req)
3526 		return -ENOMEM;
3527 
3528 	/* set the requested channels */
3529 	for (i = 0; i < n_channels; i++)
3530 		req->channels[i] = channels[i];
3531 
3532 	req->n_channels = n_channels;
3533 
3534 	/* set the rates */
3535 	for (i = 0; i < NUM_NL80211_BANDS; i++)
3536 		if (mvm->hw->wiphy->bands[i])
3537 			req->rates[i] =
3538 				(1 << mvm->hw->wiphy->bands[i]->n_bitrates) - 1;
3539 
3540 	req->wdev = ieee80211_vif_to_wdev(vif);
3541 	req->wiphy = mvm->hw->wiphy;
3542 	req->scan_start = jiffies;
3543 	req->tsf_report_link_id = -1;
3544 
3545 	ret = _iwl_mvm_single_scan_start(mvm, vif, req, &ies,
3546 					 IWL_MVM_SCAN_INT_MLO);
3547 	kfree(req);
3548 
3549 	IWL_DEBUG_SCAN(mvm, "Internal MLO scan: ret=%d\n", ret);
3550 	return ret;
3551 }
3552