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