xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/scan.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <net/mac80211.h>
9 #include <linux/crc32.h>
10 
11 #include "mvm.h"
12 #include "fw/api/scan.h"
13 #include "iwl-io.h"
14 
15 #define IWL_DENSE_EBS_SCAN_RATIO 5
16 #define IWL_SPARSE_EBS_SCAN_RATIO 1
17 
18 #define IWL_SCAN_DWELL_ACTIVE		10
19 #define IWL_SCAN_DWELL_PASSIVE		110
20 #define IWL_SCAN_DWELL_FRAGMENTED	44
21 #define IWL_SCAN_DWELL_EXTENDED		90
22 #define IWL_SCAN_NUM_OF_FRAGS		3
23 
24 /* adaptive dwell max budget time [TU] for full scan */
25 #define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
26 /* adaptive dwell max budget time [TU] for directed scan */
27 #define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
28 /* adaptive dwell default high band APs number */
29 #define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8
30 /* adaptive dwell default low band APs number */
31 #define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2
32 /* adaptive dwell default APs number in social channels (1, 6, 11) */
33 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
34 /* number of scan channels */
35 #define IWL_SCAN_NUM_CHANNELS 112
36 /* adaptive dwell number of APs override mask for p2p friendly GO */
37 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT BIT(20)
38 /* adaptive dwell number of APs override mask for social channels */
39 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT BIT(21)
40 /* adaptive dwell number of APs override for p2p friendly GO channels */
41 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY 10
42 /* adaptive dwell number of APs override for social channels */
43 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS 2
44 
45 /* minimal number of 2GHz and 5GHz channels in the regular scan request */
46 #define IWL_MVM_6GHZ_PASSIVE_SCAN_MIN_CHANS 4
47 
48 /* Number of iterations on the channel for mei filtered scan */
49 #define IWL_MEI_SCAN_NUM_ITER	5U
50 
51 struct iwl_mvm_scan_timing_params {
52 	u32 suspend_time;
53 	u32 max_out_time;
54 };
55 
56 static struct iwl_mvm_scan_timing_params scan_timing[] = {
57 	[IWL_SCAN_TYPE_UNASSOC] = {
58 		.suspend_time = 0,
59 		.max_out_time = 0,
60 	},
61 	[IWL_SCAN_TYPE_WILD] = {
62 		.suspend_time = 30,
63 		.max_out_time = 120,
64 	},
65 	[IWL_SCAN_TYPE_MILD] = {
66 		.suspend_time = 120,
67 		.max_out_time = 120,
68 	},
69 	[IWL_SCAN_TYPE_FRAGMENTED] = {
70 		.suspend_time = 95,
71 		.max_out_time = 44,
72 	},
73 	[IWL_SCAN_TYPE_FAST_BALANCE] = {
74 		.suspend_time = 30,
75 		.max_out_time = 37,
76 	},
77 };
78 
79 struct iwl_mvm_scan_params {
80 	/* For CDB this is low band scan type, for non-CDB - type. */
81 	enum iwl_mvm_scan_type type;
82 	enum iwl_mvm_scan_type hb_type;
83 	u32 n_channels;
84 	u16 delay;
85 	int n_ssids;
86 	struct cfg80211_ssid *ssids;
87 	struct ieee80211_channel **channels;
88 	u32 flags;
89 	u8 *mac_addr;
90 	u8 *mac_addr_mask;
91 	bool no_cck;
92 	bool pass_all;
93 	int n_match_sets;
94 	struct iwl_scan_probe_req preq;
95 	struct cfg80211_match_set *match_sets;
96 	int n_scan_plans;
97 	struct cfg80211_sched_scan_plan *scan_plans;
98 	bool iter_notif;
99 	struct cfg80211_scan_6ghz_params *scan_6ghz_params;
100 	u32 n_6ghz_params;
101 	bool scan_6ghz;
102 	bool enable_6ghz_passive;
103 	bool respect_p2p_go, respect_p2p_go_hb;
104 	s8 tsf_report_link_id;
105 	u8 bssid[ETH_ALEN] __aligned(2);
106 };
107 
108 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
109 {
110 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
111 
112 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
113 		return (void *)&cmd->v8.data;
114 
115 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
116 		return (void *)&cmd->v7.data;
117 
118 	if (iwl_mvm_cdb_scan_api(mvm))
119 		return (void *)&cmd->v6.data;
120 
121 	return (void *)&cmd->v1.data;
122 }
123 
124 static inline struct iwl_scan_umac_chan_param *
125 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
126 {
127 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
128 
129 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
130 		return &cmd->v8.channel;
131 
132 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
133 		return &cmd->v7.channel;
134 
135 	if (iwl_mvm_cdb_scan_api(mvm))
136 		return &cmd->v6.channel;
137 
138 	return &cmd->v1.channel;
139 }
140 
141 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
142 {
143 	if (mvm->scan_rx_ant != ANT_NONE)
144 		return mvm->scan_rx_ant;
145 	return iwl_mvm_get_valid_rx_ant(mvm);
146 }
147 
148 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
149 {
150 	u16 rx_chain;
151 	u8 rx_ant;
152 
153 	rx_ant = iwl_mvm_scan_rx_ant(mvm);
154 	rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
155 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
156 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
157 	rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
158 	return cpu_to_le16(rx_chain);
159 }
160 
161 static inline __le32
162 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
163 			  bool no_cck)
164 {
165 	u32 tx_ant;
166 
167 	iwl_mvm_toggle_tx_ant(mvm, &mvm->scan_last_antenna_idx);
168 	tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
169 
170 	if (band == NL80211_BAND_2GHZ && !no_cck)
171 		return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK_V1 |
172 				   tx_ant);
173 	else
174 		return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
175 }
176 
177 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
178 {
179 	return mvm->tcm.result.global_load;
180 }
181 
182 static enum iwl_mvm_traffic_load
183 iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
184 {
185 	return mvm->tcm.result.band_load[band];
186 }
187 
188 struct iwl_mvm_scan_iter_data {
189 	u32 global_cnt;
190 	struct ieee80211_vif *current_vif;
191 	bool is_dcm_with_p2p_go;
192 };
193 
194 static void iwl_mvm_scan_iterator(void *_data, u8 *mac,
195 				  struct ieee80211_vif *vif)
196 {
197 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
198 	struct iwl_mvm_scan_iter_data *data = _data;
199 	struct iwl_mvm_vif *curr_mvmvif;
200 
201 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE &&
202 	    mvmvif->deflink.phy_ctxt &&
203 	    mvmvif->deflink.phy_ctxt->id < NUM_PHY_CTX)
204 		data->global_cnt += 1;
205 
206 	if (!data->current_vif || vif == data->current_vif)
207 		return;
208 
209 	curr_mvmvif = iwl_mvm_vif_from_mac80211(data->current_vif);
210 
211 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
212 	    mvmvif->deflink.phy_ctxt && curr_mvmvif->deflink.phy_ctxt &&
213 	    mvmvif->deflink.phy_ctxt->id != curr_mvmvif->deflink.phy_ctxt->id)
214 		data->is_dcm_with_p2p_go = true;
215 }
216 
217 static enum
218 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
219 					 struct ieee80211_vif *vif,
220 					 enum iwl_mvm_traffic_load load,
221 					 bool low_latency)
222 {
223 	struct iwl_mvm_scan_iter_data data = {
224 		.current_vif = vif,
225 		.is_dcm_with_p2p_go = false,
226 		.global_cnt = 0,
227 	};
228 
229 	/*
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->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->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 			memcpy(&pp->bssid_array[idex_b++],
1735 			       scan_6ghz_params[j].bssid, ETH_ALEN);
1736 		}
1737 	}
1738 
1739 	pp->short_ssid_num = idex_s;
1740 	pp->bssid_num = idex_b;
1741 }
1742 
1743 /* TODO: this function can be merged with iwl_mvm_scan_umac_fill_ch_p_v7 */
1744 static u32
1745 iwl_mvm_umac_scan_cfg_channels_v7_6g(struct iwl_mvm *mvm,
1746 				     struct iwl_mvm_scan_params *params,
1747 				     u32 n_channels,
1748 				     struct iwl_scan_probe_params_v4 *pp,
1749 				     struct iwl_scan_channel_params_v7 *cp,
1750 				     enum nl80211_iftype vif_type,
1751 				     u32 version)
1752 {
1753 	int i;
1754 	struct cfg80211_scan_6ghz_params *scan_6ghz_params =
1755 		params->scan_6ghz_params;
1756 	u32 ch_cnt;
1757 
1758 	for (i = 0, ch_cnt = 0; i < params->n_channels; i++) {
1759 		struct iwl_scan_channel_cfg_umac *cfg =
1760 			&cp->channel_config[ch_cnt];
1761 
1762 		u32 s_ssid_bitmap = 0, bssid_bitmap = 0, flags = 0;
1763 		u8 j, k, n_s_ssids = 0, n_bssids = 0;
1764 		u8 max_s_ssids, max_bssids;
1765 		bool force_passive = false, found = false, allow_passive = true,
1766 		     unsolicited_probe_on_chan = false, psc_no_listen = false;
1767 		s8 psd_20 = IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
1768 
1769 		/*
1770 		 * Avoid performing passive scan on non PSC channels unless the
1771 		 * scan is specifically a passive scan, i.e., no SSIDs
1772 		 * configured in the scan command.
1773 		 */
1774 		if (!cfg80211_channel_is_psc(params->channels[i]) &&
1775 		    !params->n_6ghz_params && params->n_ssids)
1776 			continue;
1777 
1778 		cfg->v1.channel_num = params->channels[i]->hw_value;
1779 		if (version < 17)
1780 			cfg->v2.band = PHY_BAND_6;
1781 		else
1782 			cfg->flags |= cpu_to_le32(PHY_BAND_6 <<
1783 						  IWL_CHAN_CFG_FLAGS_BAND_POS);
1784 
1785 		cfg->v5.iter_count = 1;
1786 		cfg->v5.iter_interval = 0;
1787 
1788 		for (j = 0; j < params->n_6ghz_params; j++) {
1789 			s8 tmp_psd_20;
1790 
1791 			if (!(scan_6ghz_params[j].channel_idx == i))
1792 				continue;
1793 
1794 			unsolicited_probe_on_chan |=
1795 				scan_6ghz_params[j].unsolicited_probe;
1796 
1797 			/* Use the highest PSD value allowed as advertised by
1798 			 * APs for this channel
1799 			 */
1800 			tmp_psd_20 = scan_6ghz_params[j].psd_20;
1801 			if (tmp_psd_20 !=
1802 			    IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED &&
1803 			    (psd_20 ==
1804 			     IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED ||
1805 			     psd_20 < tmp_psd_20))
1806 				psd_20 = tmp_psd_20;
1807 
1808 			psc_no_listen |= scan_6ghz_params[j].psc_no_listen;
1809 		}
1810 
1811 		/*
1812 		 * In the following cases apply passive scan:
1813 		 * 1. Non fragmented scan:
1814 		 *	- PSC channel with NO_LISTEN_FLAG on should be treated
1815 		 *	  like non PSC channel
1816 		 *	- Non PSC channel with more than 3 short SSIDs or more
1817 		 *	  than 9 BSSIDs.
1818 		 *	- Non PSC Channel with unsolicited probe response and
1819 		 *	  more than 2 short SSIDs or more than 6 BSSIDs.
1820 		 *	- PSC channel with more than 2 short SSIDs or more than
1821 		 *	  6 BSSIDs.
1822 		 * 3. Fragmented scan:
1823 		 *	- PSC channel with more than 1 SSID or 3 BSSIDs.
1824 		 *	- Non PSC channel with more than 2 SSIDs or 6 BSSIDs.
1825 		 *	- Non PSC channel with unsolicited probe response and
1826 		 *	  more than 1 SSID or more than 3 BSSIDs.
1827 		 */
1828 		if (!iwl_mvm_is_scan_fragmented(params->type)) {
1829 			if (!cfg80211_channel_is_psc(params->channels[i]) ||
1830 			    flags & IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN) {
1831 				if (unsolicited_probe_on_chan) {
1832 					max_s_ssids = 2;
1833 					max_bssids = 6;
1834 				} else {
1835 					max_s_ssids = 3;
1836 					max_bssids = 9;
1837 				}
1838 			} else {
1839 				max_s_ssids = 2;
1840 				max_bssids = 6;
1841 			}
1842 		} else if (cfg80211_channel_is_psc(params->channels[i])) {
1843 			max_s_ssids = 1;
1844 			max_bssids = 3;
1845 		} else {
1846 			if (unsolicited_probe_on_chan) {
1847 				max_s_ssids = 1;
1848 				max_bssids = 3;
1849 			} else {
1850 				max_s_ssids = 2;
1851 				max_bssids = 6;
1852 			}
1853 		}
1854 
1855 		/*
1856 		 * The optimize the scan time, i.e., reduce the scan dwell time
1857 		 * on each channel, the below logic tries to set 3 direct BSSID
1858 		 * probe requests for each broadcast probe request with a short
1859 		 * SSID.
1860 		 * TODO: improve this logic
1861 		 */
1862 		for (j = 0; j < params->n_6ghz_params; j++) {
1863 			if (!(scan_6ghz_params[j].channel_idx == i))
1864 				continue;
1865 
1866 			found = false;
1867 
1868 			for (k = 0;
1869 			     k < pp->short_ssid_num && n_s_ssids < max_s_ssids;
1870 			     k++) {
1871 				if (!scan_6ghz_params[j].unsolicited_probe &&
1872 				    le32_to_cpu(pp->short_ssid[k]) ==
1873 				    scan_6ghz_params[j].short_ssid) {
1874 					/* Relevant short SSID bit set */
1875 					if (s_ssid_bitmap & BIT(k)) {
1876 						found = true;
1877 						break;
1878 					}
1879 
1880 					/*
1881 					 * Prefer creating BSSID entries unless
1882 					 * the short SSID probe can be done in
1883 					 * the same channel dwell iteration.
1884 					 *
1885 					 * We also need to create a short SSID
1886 					 * entry for any hidden AP.
1887 					 */
1888 					if (3 * n_s_ssids > n_bssids &&
1889 					    !pp->direct_scan[k].len)
1890 						break;
1891 
1892 					/* Hidden AP, cannot do passive scan */
1893 					if (pp->direct_scan[k].len)
1894 						allow_passive = false;
1895 
1896 					s_ssid_bitmap |= BIT(k);
1897 					n_s_ssids++;
1898 					found = true;
1899 					break;
1900 				}
1901 			}
1902 
1903 			if (found)
1904 				continue;
1905 
1906 			for (k = 0; k < pp->bssid_num; k++) {
1907 				if (!memcmp(&pp->bssid_array[k],
1908 					    scan_6ghz_params[j].bssid,
1909 					    ETH_ALEN)) {
1910 					if (!(bssid_bitmap & BIT(k))) {
1911 						if (n_bssids < max_bssids) {
1912 							bssid_bitmap |= BIT(k);
1913 							n_bssids++;
1914 						} else {
1915 							force_passive = TRUE;
1916 						}
1917 					}
1918 					break;
1919 				}
1920 			}
1921 		}
1922 
1923 		if (cfg80211_channel_is_psc(params->channels[i]) &&
1924 		    psc_no_listen)
1925 			flags |= IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN;
1926 
1927 		if (unsolicited_probe_on_chan)
1928 			flags |= IWL_UHB_CHAN_CFG_FLAG_UNSOLICITED_PROBE_RES;
1929 
1930 		if ((allow_passive && force_passive) ||
1931 		    (!(bssid_bitmap | s_ssid_bitmap) &&
1932 		     !cfg80211_channel_is_psc(params->channels[i])))
1933 			flags |= IWL_UHB_CHAN_CFG_FLAG_FORCE_PASSIVE;
1934 		else
1935 			flags |= bssid_bitmap | (s_ssid_bitmap << 16);
1936 
1937 		cfg->flags |= cpu_to_le32(flags);
1938 		if (version >= 17)
1939 			cfg->v5.psd_20 = psd_20;
1940 
1941 		ch_cnt++;
1942 	}
1943 
1944 	if (params->n_channels > ch_cnt)
1945 		IWL_DEBUG_SCAN(mvm,
1946 			       "6GHz: reducing number channels: (%u->%u)\n",
1947 			       params->n_channels, ch_cnt);
1948 
1949 	return ch_cnt;
1950 }
1951 
1952 static u8 iwl_mvm_scan_umac_chan_flags_v2(struct iwl_mvm *mvm,
1953 					  struct iwl_mvm_scan_params *params,
1954 					  struct ieee80211_vif *vif)
1955 {
1956 	u8 flags = 0;
1957 
1958 	flags |= IWL_SCAN_CHANNEL_FLAG_ENABLE_CHAN_ORDER;
1959 
1960 	if (iwl_mvm_scan_use_ebs(mvm, vif))
1961 		flags |= IWL_SCAN_CHANNEL_FLAG_EBS |
1962 			IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1963 			IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1964 
1965 	/* set fragmented ebs for fragmented scan on HB channels */
1966 	if ((!iwl_mvm_is_cdb_supported(mvm) &&
1967 	     iwl_mvm_is_scan_fragmented(params->type)) ||
1968 	    (iwl_mvm_is_cdb_supported(mvm) &&
1969 	     iwl_mvm_is_scan_fragmented(params->hb_type)))
1970 		flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1971 
1972 	/*
1973 	 * force EBS in case the scan is a fragmented and there is a need to take P2P
1974 	 * GO operation into consideration during scan operation.
1975 	 */
1976 	if ((!iwl_mvm_is_cdb_supported(mvm) &&
1977 	     iwl_mvm_is_scan_fragmented(params->type) && params->respect_p2p_go) ||
1978 	    (iwl_mvm_is_cdb_supported(mvm) &&
1979 	     iwl_mvm_is_scan_fragmented(params->hb_type) &&
1980 	     params->respect_p2p_go_hb)) {
1981 		IWL_DEBUG_SCAN(mvm, "Respect P2P GO. Force EBS\n");
1982 		flags |= IWL_SCAN_CHANNEL_FLAG_FORCE_EBS;
1983 	}
1984 
1985 	return flags;
1986 }
1987 
1988 static void iwl_mvm_scan_6ghz_passive_scan(struct iwl_mvm *mvm,
1989 					   struct iwl_mvm_scan_params *params,
1990 					   struct ieee80211_vif *vif)
1991 {
1992 	struct ieee80211_supported_band *sband =
1993 		&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
1994 	u32 n_disabled, i;
1995 
1996 	params->enable_6ghz_passive = false;
1997 
1998 	if (params->scan_6ghz)
1999 		return;
2000 
2001 	if (!fw_has_capa(&mvm->fw->ucode_capa,
2002 			 IWL_UCODE_TLV_CAPA_PASSIVE_6GHZ_SCAN)) {
2003 		IWL_DEBUG_SCAN(mvm,
2004 			       "6GHz passive scan: Not supported by FW\n");
2005 		return;
2006 	}
2007 
2008 	/* 6GHz passive scan allowed only on station interface  */
2009 	if (vif->type != NL80211_IFTYPE_STATION) {
2010 		IWL_DEBUG_SCAN(mvm,
2011 			       "6GHz passive scan: not station interface\n");
2012 		return;
2013 	}
2014 
2015 	/*
2016 	 * 6GHz passive scan is allowed in a defined time interval following HW
2017 	 * reset or resume flow, or while not associated and a large interval
2018 	 * has passed since the last 6GHz passive scan.
2019 	 */
2020 	if ((vif->cfg.assoc ||
2021 	     time_after(mvm->last_6ghz_passive_scan_jiffies +
2022 			(IWL_MVM_6GHZ_PASSIVE_SCAN_TIMEOUT * HZ), jiffies)) &&
2023 	    (time_before(mvm->last_reset_or_resume_time_jiffies +
2024 			 (IWL_MVM_6GHZ_PASSIVE_SCAN_ASSOC_TIMEOUT * HZ),
2025 			 jiffies))) {
2026 		IWL_DEBUG_SCAN(mvm, "6GHz passive scan: %s\n",
2027 			       vif->cfg.assoc ? "associated" :
2028 			       "timeout did not expire");
2029 		return;
2030 	}
2031 
2032 	/* not enough channels in the regular scan request */
2033 	if (params->n_channels < IWL_MVM_6GHZ_PASSIVE_SCAN_MIN_CHANS) {
2034 		IWL_DEBUG_SCAN(mvm,
2035 			       "6GHz passive scan: not enough channels\n");
2036 		return;
2037 	}
2038 
2039 	for (i = 0; i < params->n_ssids; i++) {
2040 		if (!params->ssids[i].ssid_len)
2041 			break;
2042 	}
2043 
2044 	/* not a wildcard scan, so cannot enable passive 6GHz scan */
2045 	if (i == params->n_ssids) {
2046 		IWL_DEBUG_SCAN(mvm,
2047 			       "6GHz passive scan: no wildcard SSID\n");
2048 		return;
2049 	}
2050 
2051 	if (!sband || !sband->n_channels) {
2052 		IWL_DEBUG_SCAN(mvm,
2053 			       "6GHz passive scan: no 6GHz channels\n");
2054 		return;
2055 	}
2056 
2057 	for (i = 0, n_disabled = 0; i < sband->n_channels; i++) {
2058 		if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED))
2059 			n_disabled++;
2060 	}
2061 
2062 	/*
2063 	 * Not all the 6GHz channels are disabled, so no need for 6GHz passive
2064 	 * scan
2065 	 */
2066 	if (n_disabled != sband->n_channels) {
2067 		IWL_DEBUG_SCAN(mvm,
2068 			       "6GHz passive scan: 6GHz channels enabled\n");
2069 		return;
2070 	}
2071 
2072 	/* all conditions to enable 6ghz passive scan are satisfied */
2073 	IWL_DEBUG_SCAN(mvm, "6GHz passive scan: can be enabled\n");
2074 	params->enable_6ghz_passive = true;
2075 }
2076 
2077 static u16 iwl_mvm_scan_umac_flags_v2(struct iwl_mvm *mvm,
2078 				      struct iwl_mvm_scan_params *params,
2079 				      struct ieee80211_vif *vif,
2080 				      int type)
2081 {
2082 	u16 flags = 0;
2083 
2084 	/*
2085 	 * If no direct SSIDs are provided perform a passive scan. Otherwise,
2086 	 * if there is a single SSID which is not the broadcast SSID, assume
2087 	 * that the scan is intended for roaming purposes and thus enable Rx on
2088 	 * all chains to improve chances of hearing the beacons/probe responses.
2089 	 */
2090 	if (params->n_ssids == 0)
2091 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE;
2092 	else if (params->n_ssids == 1 && params->ssids[0].ssid_len)
2093 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_USE_ALL_RX_CHAINS;
2094 
2095 	if (iwl_mvm_is_scan_fragmented(params->type))
2096 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1;
2097 
2098 	if (iwl_mvm_is_scan_fragmented(params->hb_type))
2099 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2;
2100 
2101 	if (params->pass_all)
2102 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PASS_ALL;
2103 	else
2104 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_MATCH;
2105 
2106 	if (!iwl_mvm_is_regular_scan(params))
2107 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PERIODIC;
2108 
2109 	if (params->iter_notif ||
2110 	    mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
2111 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTFY_ITER_COMPLETE;
2112 
2113 	if (IWL_MVM_ADWELL_ENABLE)
2114 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_ADAPTIVE_DWELL;
2115 
2116 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
2117 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PREEMPTIVE;
2118 
2119 	if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2120 	    params->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ)
2121 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_TRIGGER_UHB_SCAN;
2122 
2123 	if (params->enable_6ghz_passive)
2124 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_6GHZ_PASSIVE_SCAN;
2125 
2126 	if (iwl_mvm_is_oce_supported(mvm) &&
2127 	    (params->flags & (NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP |
2128 			      NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE |
2129 			      NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME)))
2130 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_OCE;
2131 
2132 	return flags;
2133 }
2134 
2135 static u8 iwl_mvm_scan_umac_flags2(struct iwl_mvm *mvm,
2136 				   struct iwl_mvm_scan_params *params,
2137 				   struct ieee80211_vif *vif, int type,
2138 				   u16 gen_flags)
2139 {
2140 	u8 flags = 0;
2141 
2142 	if (iwl_mvm_is_cdb_supported(mvm)) {
2143 		if (params->respect_p2p_go)
2144 			flags |= IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_LB;
2145 		if (params->respect_p2p_go_hb)
2146 			flags |= IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_HB;
2147 	} else {
2148 		if (params->respect_p2p_go)
2149 			flags = IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_LB |
2150 				IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_RESPECT_P2P_GO_HB;
2151 	}
2152 
2153 	if (params->scan_6ghz &&
2154 	    fw_has_capa(&mvm->fw->ucode_capa,
2155 			IWL_UCODE_TLV_CAPA_SCAN_DONT_TOGGLE_ANT))
2156 		flags |= IWL_UMAC_SCAN_GEN_PARAMS_FLAGS2_DONT_TOGGLE_ANT;
2157 
2158 	/* Passive and AP interface -> ACS (automatic channel selection) */
2159 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE &&
2160 	    ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_AP &&
2161 	    iwl_fw_lookup_notif_ver(mvm->fw, SCAN_GROUP, CHANNEL_SURVEY_NOTIF,
2162 				    0) >= 1)
2163 		flags |= IWL_UMAC_SCAN_GEN_FLAGS2_COLLECT_CHANNEL_STATS;
2164 
2165 	return flags;
2166 }
2167 
2168 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
2169 				   struct iwl_mvm_scan_params *params,
2170 				   struct ieee80211_vif *vif)
2171 {
2172 	u16 flags = 0;
2173 
2174 	if (params->n_ssids == 0)
2175 		flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
2176 
2177 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
2178 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
2179 
2180 	if (iwl_mvm_is_scan_fragmented(params->type))
2181 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
2182 
2183 	if (iwl_mvm_is_cdb_supported(mvm) &&
2184 	    iwl_mvm_is_scan_fragmented(params->hb_type))
2185 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
2186 
2187 	if (iwl_mvm_rrm_scan_needed(mvm) &&
2188 	    fw_has_capa(&mvm->fw->ucode_capa,
2189 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
2190 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
2191 
2192 	if (params->pass_all)
2193 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
2194 	else
2195 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
2196 
2197 	if (!iwl_mvm_is_regular_scan(params))
2198 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
2199 
2200 	if (params->iter_notif)
2201 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
2202 
2203 #ifdef CONFIG_IWLWIFI_DEBUGFS
2204 	if (mvm->scan_iter_notif_enabled)
2205 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
2206 #endif
2207 
2208 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
2209 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
2210 
2211 	if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE)
2212 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
2213 
2214 	/*
2215 	 * Extended dwell is relevant only for low band to start with, as it is
2216 	 * being used for social channles only (1, 6, 11), so we can check
2217 	 * only scan type on low band also for CDB.
2218 	 */
2219 	if (iwl_mvm_is_regular_scan(params) &&
2220 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
2221 	    !iwl_mvm_is_scan_fragmented(params->type) &&
2222 	    !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
2223 	    !iwl_mvm_is_oce_supported(mvm))
2224 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
2225 
2226 	if (iwl_mvm_is_oce_supported(mvm)) {
2227 		if ((params->flags &
2228 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
2229 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
2230 		/* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
2231 		 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
2232 		 * the same bit, we need to make sure that we use this bit here
2233 		 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
2234 		 * used. */
2235 		if ((params->flags &
2236 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
2237 		     !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
2238 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
2239 		if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
2240 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
2241 	}
2242 
2243 	return flags;
2244 }
2245 
2246 static int
2247 iwl_mvm_fill_scan_sched_params(struct iwl_mvm_scan_params *params,
2248 			       struct iwl_scan_umac_schedule *schedule,
2249 			       __le16 *delay)
2250 {
2251 	int i;
2252 	if (WARN_ON(!params->n_scan_plans ||
2253 		    params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
2254 		return -EINVAL;
2255 
2256 	for (i = 0; i < params->n_scan_plans; i++) {
2257 		struct cfg80211_sched_scan_plan *scan_plan =
2258 			&params->scan_plans[i];
2259 
2260 		schedule[i].iter_count = scan_plan->iterations;
2261 		schedule[i].interval =
2262 			cpu_to_le16(scan_plan->interval);
2263 	}
2264 
2265 	/*
2266 	 * If the number of iterations of the last scan plan is set to
2267 	 * zero, it should run infinitely. However, this is not always the case.
2268 	 * For example, when regular scan is requested the driver sets one scan
2269 	 * plan with one iteration.
2270 	 */
2271 	if (!schedule[params->n_scan_plans - 1].iter_count)
2272 		schedule[params->n_scan_plans - 1].iter_count = 0xff;
2273 
2274 	*delay = cpu_to_le16(params->delay);
2275 
2276 	return 0;
2277 }
2278 
2279 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2280 			     struct iwl_mvm_scan_params *params,
2281 			     int type, int uid)
2282 {
2283 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
2284 	struct iwl_scan_umac_chan_param *chan_param;
2285 	void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
2286 	void *sec_part = (u8 *)cmd_data + sizeof(struct iwl_scan_channel_cfg_umac) *
2287 		mvm->fw->ucode_capa.n_scan_channels;
2288 	struct iwl_scan_req_umac_tail_v2 *tail_v2 =
2289 		(struct iwl_scan_req_umac_tail_v2 *)sec_part;
2290 	struct iwl_scan_req_umac_tail_v1 *tail_v1;
2291 	struct iwl_ssid_ie *direct_scan;
2292 	int ret = 0;
2293 	u32 ssid_bitmap = 0;
2294 	u8 channel_flags = 0;
2295 	u16 gen_flags;
2296 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2297 
2298 	chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
2299 
2300 	iwl_mvm_scan_umac_dwell(mvm, cmd, params);
2301 
2302 	cmd->uid = cpu_to_le32(uid);
2303 	gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
2304 	cmd->general_flags = cpu_to_le16(gen_flags);
2305 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
2306 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
2307 			cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
2308 							IWL_SCAN_NUM_OF_FRAGS;
2309 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
2310 			cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
2311 							IWL_SCAN_NUM_OF_FRAGS;
2312 
2313 		cmd->v8.general_flags2 =
2314 			IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
2315 	}
2316 
2317 	cmd->scan_start_mac_id = scan_vif->id;
2318 
2319 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
2320 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
2321 
2322 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
2323 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
2324 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
2325 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
2326 
2327 		/* set fragmented ebs for fragmented scan on HB channels */
2328 		if (iwl_mvm_is_frag_ebs_supported(mvm)) {
2329 			if (gen_flags &
2330 			    IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
2331 			    (!iwl_mvm_is_cdb_supported(mvm) &&
2332 			     gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
2333 				channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
2334 		}
2335 	}
2336 
2337 	chan_param->flags = channel_flags;
2338 	chan_param->count = params->n_channels;
2339 
2340 	ret = iwl_mvm_fill_scan_sched_params(params, tail_v2->schedule,
2341 					     &tail_v2->delay);
2342 	if (ret)
2343 		return ret;
2344 
2345 	if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
2346 		tail_v2->preq = params->preq;
2347 		direct_scan = tail_v2->direct_scan;
2348 	} else {
2349 		tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
2350 		iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
2351 						  &params->preq);
2352 		direct_scan = tail_v1->direct_scan;
2353 	}
2354 	iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
2355 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
2356 				       params->n_channels, ssid_bitmap,
2357 				       cmd_data);
2358 	return 0;
2359 }
2360 
2361 static void
2362 iwl_mvm_scan_umac_fill_general_p_v12(struct iwl_mvm *mvm,
2363 				     struct iwl_mvm_scan_params *params,
2364 				     struct ieee80211_vif *vif,
2365 				     struct iwl_scan_general_params_v11 *gp,
2366 				     u16 gen_flags, u8 gen_flags2,
2367 				     u32 version)
2368 {
2369 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2370 
2371 	iwl_mvm_scan_umac_dwell_v11(mvm, gp, params);
2372 
2373 	IWL_DEBUG_SCAN(mvm, "General: flags=0x%x, flags2=0x%x\n",
2374 		       gen_flags, gen_flags2);
2375 
2376 	gp->flags = cpu_to_le16(gen_flags);
2377 	gp->flags2 = gen_flags2;
2378 
2379 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
2380 		gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2381 	if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2)
2382 		gp->num_of_fragments[SCAN_HB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2383 
2384 	mvm->scan_link_id = 0;
2385 
2386 	if (version < 16) {
2387 		gp->scan_start_mac_or_link_id = scan_vif->id;
2388 	} else {
2389 		struct iwl_mvm_vif_link_info *link_info =
2390 			scan_vif->link[params->tsf_report_link_id];
2391 
2392 		mvm->scan_link_id = params->tsf_report_link_id;
2393 		if (!WARN_ON(!link_info))
2394 			gp->scan_start_mac_or_link_id = link_info->fw_link_id;
2395 	}
2396 }
2397 
2398 static void
2399 iwl_mvm_scan_umac_fill_probe_p_v3(struct iwl_mvm_scan_params *params,
2400 				  struct iwl_scan_probe_params_v3 *pp)
2401 {
2402 	pp->preq = params->preq;
2403 	pp->ssid_num = params->n_ssids;
2404 	iwl_scan_build_ssids(params, pp->direct_scan, NULL);
2405 }
2406 
2407 static void
2408 iwl_mvm_scan_umac_fill_probe_p_v4(struct iwl_mvm_scan_params *params,
2409 				  struct iwl_scan_probe_params_v4 *pp,
2410 				  u32 *bitmap_ssid)
2411 {
2412 	pp->preq = params->preq;
2413 	iwl_scan_build_ssids(params, pp->direct_scan, bitmap_ssid);
2414 }
2415 
2416 static void
2417 iwl_mvm_scan_umac_fill_ch_p_v4(struct iwl_mvm *mvm,
2418 			       struct iwl_mvm_scan_params *params,
2419 			       struct ieee80211_vif *vif,
2420 			       struct iwl_scan_channel_params_v4 *cp,
2421 			       u32 channel_cfg_flags)
2422 {
2423 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2424 	cp->count = params->n_channels;
2425 	cp->num_of_aps_override = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2426 
2427 	iwl_mvm_umac_scan_cfg_channels_v4(mvm, params->channels, cp,
2428 					  params->n_channels,
2429 					  channel_cfg_flags,
2430 					  vif->type);
2431 }
2432 
2433 static void
2434 iwl_mvm_scan_umac_fill_ch_p_v7(struct iwl_mvm *mvm,
2435 			       struct iwl_mvm_scan_params *params,
2436 			       struct ieee80211_vif *vif,
2437 			       struct iwl_scan_channel_params_v7 *cp,
2438 			       u32 channel_cfg_flags,
2439 			       u32 version)
2440 {
2441 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2442 	cp->count = params->n_channels;
2443 	cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2444 	cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2445 
2446 	iwl_mvm_umac_scan_cfg_channels_v7(mvm, params->channels, cp,
2447 					  params->n_channels,
2448 					  channel_cfg_flags,
2449 					  vif->type, version);
2450 
2451 	if (params->enable_6ghz_passive) {
2452 		struct ieee80211_supported_band *sband =
2453 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
2454 		u32 i;
2455 
2456 		for (i = 0; i < sband->n_channels; i++) {
2457 			struct ieee80211_channel *channel =
2458 				&sband->channels[i];
2459 
2460 			struct iwl_scan_channel_cfg_umac *cfg =
2461 				&cp->channel_config[cp->count];
2462 
2463 			if (!cfg80211_channel_is_psc(channel))
2464 				continue;
2465 
2466 			cfg->v5.channel_num = channel->hw_value;
2467 			cfg->v5.iter_count = 1;
2468 			cfg->v5.iter_interval = 0;
2469 
2470 			if (version < 17) {
2471 				cfg->flags = 0;
2472 				cfg->v2.band = PHY_BAND_6;
2473 			} else {
2474 				cfg->flags = cpu_to_le32(PHY_BAND_6 <<
2475 							 IWL_CHAN_CFG_FLAGS_BAND_POS);
2476 				cfg->v5.psd_20 =
2477 					IEEE80211_RNR_TBTT_PARAMS_PSD_RESERVED;
2478 			}
2479 			cp->count++;
2480 		}
2481 	}
2482 }
2483 
2484 static int iwl_mvm_scan_umac_v12(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2485 				 struct iwl_mvm_scan_params *params, int type,
2486 				 int uid)
2487 {
2488 	struct iwl_scan_req_umac_v12 *cmd = mvm->scan_cmd;
2489 	struct iwl_scan_req_params_v12 *scan_p = &cmd->scan_params;
2490 	int ret;
2491 	u16 gen_flags;
2492 
2493 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(type));
2494 	cmd->uid = cpu_to_le32(uid);
2495 
2496 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2497 	iwl_mvm_scan_umac_fill_general_p_v12(mvm, params, vif,
2498 					     &scan_p->general_params,
2499 					     gen_flags, 0, 12);
2500 
2501 	ret = iwl_mvm_fill_scan_sched_params(params,
2502 					     scan_p->periodic_params.schedule,
2503 					     &scan_p->periodic_params.delay);
2504 	if (ret)
2505 		return ret;
2506 
2507 	iwl_mvm_scan_umac_fill_probe_p_v3(params, &scan_p->probe_params);
2508 	iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
2509 				       &scan_p->channel_params, 0);
2510 
2511 	return 0;
2512 }
2513 
2514 static int iwl_mvm_scan_umac_v14_and_above(struct iwl_mvm *mvm,
2515 					   struct ieee80211_vif *vif,
2516 					   struct iwl_mvm_scan_params *params,
2517 					   int type, int uid, u32 version)
2518 {
2519 	struct iwl_scan_req_umac_v17 *cmd = mvm->scan_cmd;
2520 	struct iwl_scan_req_params_v17 *scan_p = &cmd->scan_params;
2521 	struct iwl_scan_channel_params_v7 *cp = &scan_p->channel_params;
2522 	struct iwl_scan_probe_params_v4 *pb = &scan_p->probe_params;
2523 	int ret;
2524 	u16 gen_flags;
2525 	u8 gen_flags2;
2526 	u32 bitmap_ssid = 0;
2527 
2528 	cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(type));
2529 	cmd->uid = cpu_to_le32(uid);
2530 
2531 	gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2532 
2533 	if (version >= 15)
2534 		gen_flags2 = iwl_mvm_scan_umac_flags2(mvm, params, vif, type,
2535 						      gen_flags);
2536 	else
2537 		gen_flags2 = 0;
2538 
2539 	iwl_mvm_scan_umac_fill_general_p_v12(mvm, params, vif,
2540 					     &scan_p->general_params,
2541 					     gen_flags, gen_flags2, version);
2542 
2543 	ret = iwl_mvm_fill_scan_sched_params(params,
2544 					     scan_p->periodic_params.schedule,
2545 					     &scan_p->periodic_params.delay);
2546 	if (ret)
2547 		return ret;
2548 
2549 	if (!params->scan_6ghz) {
2550 		iwl_mvm_scan_umac_fill_probe_p_v4(params,
2551 						  &scan_p->probe_params,
2552 						  &bitmap_ssid);
2553 		iwl_mvm_scan_umac_fill_ch_p_v7(mvm, params, vif,
2554 					       &scan_p->channel_params,
2555 					       bitmap_ssid,
2556 					       version);
2557 		return 0;
2558 	} else {
2559 		pb->preq = params->preq;
2560 	}
2561 
2562 	cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2563 	cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2564 	cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2565 
2566 	iwl_mvm_umac_scan_fill_6g_chan_list(mvm, params, pb);
2567 
2568 	cp->count = iwl_mvm_umac_scan_cfg_channels_v7_6g(mvm, params,
2569 							 params->n_channels,
2570 							 pb, cp, vif->type,
2571 							 version);
2572 	if (!cp->count)
2573 		return -EINVAL;
2574 
2575 	if (!params->n_ssids ||
2576 	    (params->n_ssids == 1 && !params->ssids[0].ssid_len))
2577 		cp->flags |= IWL_SCAN_CHANNEL_FLAG_6G_PSC_NO_FILTER;
2578 
2579 	return 0;
2580 }
2581 
2582 static int iwl_mvm_scan_umac_v14(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2583 				 struct iwl_mvm_scan_params *params, int type,
2584 				 int uid)
2585 {
2586 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 14);
2587 }
2588 
2589 static int iwl_mvm_scan_umac_v15(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2590 				 struct iwl_mvm_scan_params *params, int type,
2591 				 int uid)
2592 {
2593 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 15);
2594 }
2595 
2596 static int iwl_mvm_scan_umac_v16(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2597 				 struct iwl_mvm_scan_params *params, int type,
2598 				 int uid)
2599 {
2600 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 16);
2601 }
2602 
2603 static int iwl_mvm_scan_umac_v17(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2604 				 struct iwl_mvm_scan_params *params, int type,
2605 				 int uid)
2606 {
2607 	return iwl_mvm_scan_umac_v14_and_above(mvm, vif, params, type, uid, 17);
2608 }
2609 
2610 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
2611 {
2612 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
2613 }
2614 
2615 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
2616 {
2617 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2618 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2619 
2620 	/* This looks a bit arbitrary, but the idea is that if we run
2621 	 * out of possible simultaneous scans and the userspace is
2622 	 * trying to run a scan type that is already running, we
2623 	 * return -EBUSY.  But if the userspace wants to start a
2624 	 * different type of scan, we stop the opposite type to make
2625 	 * space for the new request.  The reason is backwards
2626 	 * compatibility with old wpa_supplicant that wouldn't stop a
2627 	 * scheduled scan before starting a normal scan.
2628 	 */
2629 
2630 	/* FW supports only a single periodic scan */
2631 	if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2632 	    mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
2633 		return -EBUSY;
2634 
2635 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
2636 		return 0;
2637 
2638 	/* Use a switch, even though this is a bitmask, so that more
2639 	 * than one bits set will fall in default and we will warn.
2640 	 */
2641 	switch (type) {
2642 	case IWL_MVM_SCAN_REGULAR:
2643 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2644 			return -EBUSY;
2645 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2646 	case IWL_MVM_SCAN_SCHED:
2647 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2648 			return -EBUSY;
2649 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2650 	case IWL_MVM_SCAN_NETDETECT:
2651 		/* For non-unified images, there's no need to stop
2652 		 * anything for net-detect since the firmware is
2653 		 * restarted anyway.  This way, any sched scans that
2654 		 * were running will be restarted when we resume.
2655 		 */
2656 		if (!unified_image)
2657 			return 0;
2658 
2659 		/* If this is a unified image and we ran out of scans,
2660 		 * we need to stop something.  Prefer stopping regular
2661 		 * scans, because the results are useless at this
2662 		 * point, and we should be able to keep running
2663 		 * another scheduled scan while suspended.
2664 		 */
2665 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2666 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
2667 						 true);
2668 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2669 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
2670 						 true);
2671 		/* Something is wrong if no scan was running but we
2672 		 * ran out of scans.
2673 		 */
2674 		fallthrough;
2675 	default:
2676 		WARN_ON(1);
2677 		break;
2678 	}
2679 
2680 	return -EIO;
2681 }
2682 
2683 #define SCAN_TIMEOUT 30000
2684 
2685 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
2686 {
2687 	struct delayed_work *delayed_work = to_delayed_work(work);
2688 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
2689 					   scan_timeout_dwork);
2690 
2691 	IWL_ERR(mvm, "regular scan timed out\n");
2692 
2693 	iwl_force_nmi(mvm->trans);
2694 }
2695 
2696 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
2697 				   struct iwl_mvm_scan_params *params,
2698 				   struct ieee80211_vif *vif)
2699 {
2700 	if (iwl_mvm_is_cdb_supported(mvm)) {
2701 		params->type =
2702 			iwl_mvm_get_scan_type_band(mvm, vif,
2703 						   NL80211_BAND_2GHZ);
2704 		params->hb_type =
2705 			iwl_mvm_get_scan_type_band(mvm, vif,
2706 						   NL80211_BAND_5GHZ);
2707 	} else {
2708 		params->type = iwl_mvm_get_scan_type(mvm, vif);
2709 	}
2710 }
2711 
2712 struct iwl_scan_umac_handler {
2713 	u8 version;
2714 	int (*handler)(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2715 		       struct iwl_mvm_scan_params *params, int type, int uid);
2716 };
2717 
2718 #define IWL_SCAN_UMAC_HANDLER(_ver) {		\
2719 	.version = _ver,			\
2720 	.handler = iwl_mvm_scan_umac_v##_ver,	\
2721 }
2722 
2723 static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
2724 	/* set the newest version first to shorten the list traverse time */
2725 	IWL_SCAN_UMAC_HANDLER(17),
2726 	IWL_SCAN_UMAC_HANDLER(16),
2727 	IWL_SCAN_UMAC_HANDLER(15),
2728 	IWL_SCAN_UMAC_HANDLER(14),
2729 	IWL_SCAN_UMAC_HANDLER(12),
2730 };
2731 
2732 static void iwl_mvm_mei_scan_work(struct work_struct *wk)
2733 {
2734 	struct iwl_mei_scan_filter *scan_filter =
2735 		container_of(wk, struct iwl_mei_scan_filter, scan_work);
2736 	struct iwl_mvm *mvm =
2737 		container_of(scan_filter, struct iwl_mvm, mei_scan_filter);
2738 	struct iwl_mvm_csme_conn_info *info;
2739 	struct sk_buff *skb;
2740 	u8 bssid[ETH_ALEN];
2741 
2742 	mutex_lock(&mvm->mutex);
2743 	info = iwl_mvm_get_csme_conn_info(mvm);
2744 	memcpy(bssid, info->conn_info.bssid, ETH_ALEN);
2745 	mutex_unlock(&mvm->mutex);
2746 
2747 	while ((skb = skb_dequeue(&scan_filter->scan_res))) {
2748 		struct ieee80211_mgmt *mgmt = (void *)skb->data;
2749 
2750 		if (!memcmp(mgmt->bssid, bssid, ETH_ALEN))
2751 			ieee80211_rx_irqsafe(mvm->hw, skb);
2752 		else
2753 			kfree_skb(skb);
2754 	}
2755 }
2756 
2757 void iwl_mvm_mei_scan_filter_init(struct iwl_mei_scan_filter *mei_scan_filter)
2758 {
2759 	skb_queue_head_init(&mei_scan_filter->scan_res);
2760 	INIT_WORK(&mei_scan_filter->scan_work, iwl_mvm_mei_scan_work);
2761 }
2762 
2763 /* In case CSME is connected and has link protection set, this function will
2764  * override the scan request to scan only the associated channel and only for
2765  * the associated SSID.
2766  */
2767 static void iwl_mvm_mei_limited_scan(struct iwl_mvm *mvm,
2768 				     struct iwl_mvm_scan_params *params)
2769 {
2770 	struct iwl_mvm_csme_conn_info *info = iwl_mvm_get_csme_conn_info(mvm);
2771 	struct iwl_mei_conn_info *conn_info;
2772 	struct ieee80211_channel *chan;
2773 	int scan_iters, i;
2774 
2775 	if (!info) {
2776 		IWL_DEBUG_SCAN(mvm, "mei_limited_scan: no connection info\n");
2777 		return;
2778 	}
2779 
2780 	conn_info = &info->conn_info;
2781 	if (!info->conn_info.lp_state || !info->conn_info.ssid_len)
2782 		return;
2783 
2784 	if (!params->n_channels || !params->n_ssids)
2785 		return;
2786 
2787 	mvm->mei_scan_filter.is_mei_limited_scan = true;
2788 
2789 	chan = ieee80211_get_channel(mvm->hw->wiphy,
2790 				     ieee80211_channel_to_frequency(conn_info->channel,
2791 								    conn_info->band));
2792 	if (!chan) {
2793 		IWL_DEBUG_SCAN(mvm,
2794 			       "Failed to get CSME channel (chan=%u band=%u)\n",
2795 			       conn_info->channel, conn_info->band);
2796 		return;
2797 	}
2798 
2799 	/* The mei filtered scan must find the AP, otherwise CSME will
2800 	 * take the NIC ownership. Add several iterations on the channel to
2801 	 * make the scan more robust.
2802 	 */
2803 	scan_iters = min(IWL_MEI_SCAN_NUM_ITER, params->n_channels);
2804 	params->n_channels = scan_iters;
2805 	for (i = 0; i < scan_iters; i++)
2806 		params->channels[i] = chan;
2807 
2808 	IWL_DEBUG_SCAN(mvm, "Mei scan: num iterations=%u\n", scan_iters);
2809 
2810 	params->n_ssids = 1;
2811 	params->ssids[0].ssid_len = conn_info->ssid_len;
2812 	memcpy(params->ssids[0].ssid, conn_info->ssid, conn_info->ssid_len);
2813 }
2814 
2815 static int iwl_mvm_build_scan_cmd(struct iwl_mvm *mvm,
2816 				  struct ieee80211_vif *vif,
2817 				  struct iwl_host_cmd *hcmd,
2818 				  struct iwl_mvm_scan_params *params,
2819 				  int type)
2820 {
2821 	int uid, i, err;
2822 	u8 scan_ver;
2823 
2824 	lockdep_assert_held(&mvm->mutex);
2825 	memset(mvm->scan_cmd, 0, mvm->scan_cmd_size);
2826 
2827 	iwl_mvm_mei_limited_scan(mvm, params);
2828 
2829 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2830 		hcmd->id = SCAN_OFFLOAD_REQUEST_CMD;
2831 
2832 		return iwl_mvm_scan_lmac(mvm, vif, params);
2833 	}
2834 
2835 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
2836 	if (uid < 0)
2837 		return uid;
2838 
2839 	hcmd->id = WIDE_ID(IWL_ALWAYS_LONG_GROUP, SCAN_REQ_UMAC);
2840 
2841 	scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC,
2842 					 IWL_FW_CMD_VER_UNKNOWN);
2843 
2844 	for (i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
2845 		const struct iwl_scan_umac_handler *ver_handler =
2846 			&iwl_scan_umac_handlers[i];
2847 
2848 		if (ver_handler->version != scan_ver)
2849 			continue;
2850 
2851 		err = ver_handler->handler(mvm, vif, params, type, uid);
2852 		return err ? : uid;
2853 	}
2854 
2855 	err = iwl_mvm_scan_umac(mvm, vif, params, type, uid);
2856 	if (err)
2857 		return err;
2858 
2859 	return uid;
2860 }
2861 
2862 struct iwl_mvm_scan_respect_p2p_go_iter_data {
2863 	struct ieee80211_vif *current_vif;
2864 	bool p2p_go;
2865 	enum nl80211_band band;
2866 };
2867 
2868 static void iwl_mvm_scan_respect_p2p_go_iter(void *_data, u8 *mac,
2869 					     struct ieee80211_vif *vif)
2870 {
2871 	struct iwl_mvm_scan_respect_p2p_go_iter_data *data = _data;
2872 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2873 
2874 	/* exclude the given vif */
2875 	if (vif == data->current_vif)
2876 		return;
2877 
2878 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p) {
2879 		u32 link_id;
2880 
2881 		for (link_id = 0;
2882 		     link_id < ARRAY_SIZE(mvmvif->link);
2883 		     link_id++) {
2884 			struct iwl_mvm_vif_link_info *link =
2885 				mvmvif->link[link_id];
2886 
2887 			if (link && link->phy_ctxt->id < NUM_PHY_CTX &&
2888 			    (data->band == NUM_NL80211_BANDS ||
2889 			     link->phy_ctxt->channel->band == data->band)) {
2890 				data->p2p_go = true;
2891 				break;
2892 			}
2893 		}
2894 	}
2895 }
2896 
2897 static bool _iwl_mvm_get_respect_p2p_go(struct iwl_mvm *mvm,
2898 					struct ieee80211_vif *vif,
2899 					bool low_latency,
2900 					enum nl80211_band band)
2901 {
2902 	struct iwl_mvm_scan_respect_p2p_go_iter_data data = {
2903 		.current_vif = vif,
2904 		.p2p_go = false,
2905 		.band = band,
2906 	};
2907 
2908 	if (!low_latency)
2909 		return false;
2910 
2911 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
2912 						   IEEE80211_IFACE_ITER_NORMAL,
2913 						   iwl_mvm_scan_respect_p2p_go_iter,
2914 						   &data);
2915 
2916 	return data.p2p_go;
2917 }
2918 
2919 static bool iwl_mvm_get_respect_p2p_go_band(struct iwl_mvm *mvm,
2920 					    struct ieee80211_vif *vif,
2921 					    enum nl80211_band band)
2922 {
2923 	bool low_latency = iwl_mvm_low_latency_band(mvm, band);
2924 
2925 	return _iwl_mvm_get_respect_p2p_go(mvm, vif, low_latency, band);
2926 }
2927 
2928 static bool iwl_mvm_get_respect_p2p_go(struct iwl_mvm *mvm,
2929 				       struct ieee80211_vif *vif)
2930 {
2931 	bool low_latency = iwl_mvm_low_latency(mvm);
2932 
2933 	return _iwl_mvm_get_respect_p2p_go(mvm, vif, low_latency,
2934 					   NUM_NL80211_BANDS);
2935 }
2936 
2937 static void iwl_mvm_fill_respect_p2p_go(struct iwl_mvm *mvm,
2938 					struct iwl_mvm_scan_params *params,
2939 					struct ieee80211_vif *vif)
2940 {
2941 	if (iwl_mvm_is_cdb_supported(mvm)) {
2942 		params->respect_p2p_go =
2943 			iwl_mvm_get_respect_p2p_go_band(mvm, vif,
2944 							NL80211_BAND_2GHZ);
2945 		params->respect_p2p_go_hb =
2946 			iwl_mvm_get_respect_p2p_go_band(mvm, vif,
2947 							NL80211_BAND_5GHZ);
2948 	} else {
2949 		params->respect_p2p_go = iwl_mvm_get_respect_p2p_go(mvm, vif);
2950 	}
2951 }
2952 
2953 static int _iwl_mvm_single_scan_start(struct iwl_mvm *mvm,
2954 				      struct ieee80211_vif *vif,
2955 				      struct cfg80211_scan_request *req,
2956 				      struct ieee80211_scan_ies *ies,
2957 				      int type)
2958 {
2959 	struct iwl_host_cmd hcmd = {
2960 		.len = { iwl_mvm_scan_size(mvm), },
2961 		.data = { mvm->scan_cmd, },
2962 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
2963 	};
2964 	struct iwl_mvm_scan_params params = {};
2965 	int ret, uid;
2966 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
2967 
2968 	lockdep_assert_held(&mvm->mutex);
2969 
2970 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2971 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
2972 		return -EBUSY;
2973 	}
2974 
2975 	ret = iwl_mvm_check_running_scans(mvm, type);
2976 	if (ret)
2977 		return ret;
2978 
2979 	/* we should have failed registration if scan_cmd was NULL */
2980 	if (WARN_ON(!mvm->scan_cmd))
2981 		return -ENOMEM;
2982 
2983 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2984 		return -ENOBUFS;
2985 
2986 	params.n_ssids = req->n_ssids;
2987 	params.flags = req->flags;
2988 	params.n_channels = req->n_channels;
2989 	params.delay = 0;
2990 	params.ssids = req->ssids;
2991 	params.channels = req->channels;
2992 	params.mac_addr = req->mac_addr;
2993 	params.mac_addr_mask = req->mac_addr_mask;
2994 	params.no_cck = req->no_cck;
2995 	params.pass_all = true;
2996 	params.n_match_sets = 0;
2997 	params.match_sets = NULL;
2998 	ether_addr_copy(params.bssid, req->bssid);
2999 
3000 	params.scan_plans = &scan_plan;
3001 	params.n_scan_plans = 1;
3002 
3003 	params.n_6ghz_params = req->n_6ghz_params;
3004 	params.scan_6ghz_params = req->scan_6ghz_params;
3005 	params.scan_6ghz = req->scan_6ghz;
3006 	iwl_mvm_fill_scan_type(mvm, &params, vif);
3007 	iwl_mvm_fill_respect_p2p_go(mvm, &params, vif);
3008 
3009 	if (req->duration)
3010 		params.iter_notif = true;
3011 
3012 	params.tsf_report_link_id = req->tsf_report_link_id;
3013 	if (params.tsf_report_link_id < 0) {
3014 		if (vif->active_links)
3015 			params.tsf_report_link_id = __ffs(vif->active_links);
3016 		else
3017 			params.tsf_report_link_id = 0;
3018 	}
3019 
3020 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
3021 
3022 	iwl_mvm_scan_6ghz_passive_scan(mvm, &params, vif);
3023 
3024 	uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
3025 
3026 	if (uid < 0)
3027 		return uid;
3028 
3029 	iwl_mvm_pause_tcm(mvm, false);
3030 
3031 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
3032 	if (ret) {
3033 		/* If the scan failed, it usually means that the FW was unable
3034 		 * to allocate the time events. Warn on it, but maybe we
3035 		 * should try to send the command again with different params.
3036 		 */
3037 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
3038 		iwl_mvm_resume_tcm(mvm);
3039 		return ret;
3040 	}
3041 
3042 	IWL_DEBUG_SCAN(mvm, "Scan request send success: type=%u, uid=%u\n",
3043 		       type, uid);
3044 
3045 	mvm->scan_uid_status[uid] = type;
3046 	mvm->scan_status |= type;
3047 
3048 	if (type == IWL_MVM_SCAN_REGULAR) {
3049 		mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
3050 		schedule_delayed_work(&mvm->scan_timeout_dwork,
3051 				      msecs_to_jiffies(SCAN_TIMEOUT));
3052 	}
3053 
3054 	if (params.enable_6ghz_passive)
3055 		mvm->last_6ghz_passive_scan_jiffies = jiffies;
3056 
3057 	return 0;
3058 }
3059 
3060 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3061 			   struct cfg80211_scan_request *req,
3062 			   struct ieee80211_scan_ies *ies)
3063 {
3064 	return _iwl_mvm_single_scan_start(mvm, vif, req, ies,
3065 					  IWL_MVM_SCAN_REGULAR);
3066 }
3067 
3068 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
3069 			     struct ieee80211_vif *vif,
3070 			     struct cfg80211_sched_scan_request *req,
3071 			     struct ieee80211_scan_ies *ies,
3072 			     int type)
3073 {
3074 	struct iwl_host_cmd hcmd = {
3075 		.len = { iwl_mvm_scan_size(mvm), },
3076 		.data = { mvm->scan_cmd, },
3077 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
3078 	};
3079 	struct iwl_mvm_scan_params params = {};
3080 	int ret, uid;
3081 	int i, j;
3082 	bool non_psc_included = false;
3083 
3084 	lockdep_assert_held(&mvm->mutex);
3085 
3086 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
3087 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
3088 		return -EBUSY;
3089 	}
3090 
3091 	ret = iwl_mvm_check_running_scans(mvm, type);
3092 	if (ret)
3093 		return ret;
3094 
3095 	/* we should have failed registration if scan_cmd was NULL */
3096 	if (WARN_ON(!mvm->scan_cmd))
3097 		return -ENOMEM;
3098 
3099 
3100 	params.n_ssids = req->n_ssids;
3101 	params.flags = req->flags;
3102 	params.n_channels = req->n_channels;
3103 	params.ssids = req->ssids;
3104 	params.channels = req->channels;
3105 	params.mac_addr = req->mac_addr;
3106 	params.mac_addr_mask = req->mac_addr_mask;
3107 	params.no_cck = false;
3108 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
3109 	params.n_match_sets = req->n_match_sets;
3110 	params.match_sets = req->match_sets;
3111 	eth_broadcast_addr(params.bssid);
3112 	if (!req->n_scan_plans)
3113 		return -EINVAL;
3114 
3115 	params.n_scan_plans = req->n_scan_plans;
3116 	params.scan_plans = req->scan_plans;
3117 
3118 	iwl_mvm_fill_scan_type(mvm, &params, vif);
3119 	iwl_mvm_fill_respect_p2p_go(mvm, &params, vif);
3120 
3121 	/* In theory, LMAC scans can handle a 32-bit delay, but since
3122 	 * waiting for over 18 hours to start the scan is a bit silly
3123 	 * and to keep it aligned with UMAC scans (which only support
3124 	 * 16-bit delays), trim it down to 16-bits.
3125 	 */
3126 	if (req->delay > U16_MAX) {
3127 		IWL_DEBUG_SCAN(mvm,
3128 			       "delay value is > 16-bits, set to max possible\n");
3129 		params.delay = U16_MAX;
3130 	} else {
3131 		params.delay = req->delay;
3132 	}
3133 
3134 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
3135 	if (ret)
3136 		return ret;
3137 
3138 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
3139 
3140 	/* for 6 GHZ band only PSC channels need to be added */
3141 	for (i = 0; i < params.n_channels; i++) {
3142 		struct ieee80211_channel *channel = params.channels[i];
3143 
3144 		if (channel->band == NL80211_BAND_6GHZ &&
3145 		    !cfg80211_channel_is_psc(channel)) {
3146 			non_psc_included = true;
3147 			break;
3148 		}
3149 	}
3150 
3151 	if (non_psc_included) {
3152 		params.channels = kmemdup(params.channels,
3153 					  sizeof(params.channels[0]) *
3154 					  params.n_channels,
3155 					  GFP_KERNEL);
3156 		if (!params.channels)
3157 			return -ENOMEM;
3158 
3159 		for (i = j = 0; i < params.n_channels; i++) {
3160 			if (params.channels[i]->band == NL80211_BAND_6GHZ &&
3161 			    !cfg80211_channel_is_psc(params.channels[i]))
3162 				continue;
3163 			params.channels[j++] = params.channels[i];
3164 		}
3165 		params.n_channels = j;
3166 	}
3167 
3168 	if (non_psc_included &&
3169 	    !iwl_mvm_scan_fits(mvm, req->n_ssids, ies, params.n_channels)) {
3170 		kfree(params.channels);
3171 		return -ENOBUFS;
3172 	}
3173 
3174 	uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
3175 
3176 	if (non_psc_included)
3177 		kfree(params.channels);
3178 	if (uid < 0)
3179 		return uid;
3180 
3181 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
3182 	if (!ret) {
3183 		IWL_DEBUG_SCAN(mvm,
3184 			       "Sched scan request send success: type=%u, uid=%u\n",
3185 			       type, uid);
3186 		mvm->scan_uid_status[uid] = type;
3187 		mvm->scan_status |= type;
3188 	} else {
3189 		/* If the scan failed, it usually means that the FW was unable
3190 		 * to allocate the time events. Warn on it, but maybe we
3191 		 * should try to send the command again with different params.
3192 		 */
3193 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
3194 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3195 	}
3196 
3197 	return ret;
3198 }
3199 
3200 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
3201 					 struct iwl_rx_cmd_buffer *rxb)
3202 {
3203 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3204 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
3205 	u32 uid = __le32_to_cpu(notif->uid);
3206 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
3207 	bool select_links = false;
3208 
3209 	mvm->mei_scan_filter.is_mei_limited_scan = false;
3210 
3211 	IWL_DEBUG_SCAN(mvm,
3212 		       "Scan completed: uid=%u type=%u, status=%s, EBS=%s\n",
3213 		       uid, mvm->scan_uid_status[uid],
3214 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
3215 				"completed" : "aborted",
3216 		       iwl_mvm_ebs_status_str(notif->ebs_status));
3217 
3218 	IWL_DEBUG_SCAN(mvm, "Scan completed: scan_status=0x%x\n",
3219 		       mvm->scan_status);
3220 
3221 	IWL_DEBUG_SCAN(mvm,
3222 		       "Scan completed: line=%u, iter=%u, elapsed time=%u\n",
3223 		       notif->last_schedule, notif->last_iter,
3224 		       __le32_to_cpu(notif->time_from_last_iter));
3225 
3226 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
3227 		return;
3228 
3229 	/* if the scan is already stopping, we don't need to notify mac80211 */
3230 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
3231 		struct cfg80211_scan_info info = {
3232 			.aborted = aborted,
3233 			.scan_start_tsf = mvm->scan_start,
3234 		};
3235 		struct iwl_mvm_vif *scan_vif = mvm->scan_vif;
3236 		struct iwl_mvm_vif_link_info *link_info =
3237 			scan_vif->link[mvm->scan_link_id];
3238 
3239 		/* It is possible that by the time the scan is complete the link
3240 		 * was already removed and is not valid.
3241 		 */
3242 		if (link_info)
3243 			memcpy(info.tsf_bssid, link_info->bssid, ETH_ALEN);
3244 		else
3245 			IWL_DEBUG_SCAN(mvm, "Scan link is no longer valid\n");
3246 
3247 		ieee80211_scan_completed(mvm->hw, &info);
3248 		mvm->scan_vif = NULL;
3249 		cancel_delayed_work(&mvm->scan_timeout_dwork);
3250 		iwl_mvm_resume_tcm(mvm);
3251 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
3252 		ieee80211_sched_scan_stopped(mvm->hw);
3253 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3254 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_INT_MLO) {
3255 		IWL_DEBUG_SCAN(mvm, "Internal MLO scan completed\n");
3256 		/*
3257 		 * Other scan types won't necessarily scan for the MLD links channels.
3258 		 * Therefore, only select links after successful internal scan.
3259 		 */
3260 		select_links = notif->status == IWL_SCAN_OFFLOAD_COMPLETED;
3261 	}
3262 
3263 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
3264 
3265 	IWL_DEBUG_SCAN(mvm, "Scan completed: after update: scan_status=0x%x\n",
3266 		       mvm->scan_status);
3267 
3268 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
3269 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
3270 		mvm->last_ebs_successful = false;
3271 
3272 	mvm->scan_uid_status[uid] = 0;
3273 
3274 	if (select_links)
3275 		wiphy_work_queue(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
3276 }
3277 
3278 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
3279 					      struct iwl_rx_cmd_buffer *rxb)
3280 {
3281 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3282 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
3283 
3284 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
3285 
3286 	IWL_DEBUG_SCAN(mvm,
3287 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
3288 		       notif->status, notif->scanned_channels);
3289 
3290 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
3291 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
3292 		ieee80211_sched_scan_results(mvm->hw);
3293 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
3294 	}
3295 
3296 	IWL_DEBUG_SCAN(mvm,
3297 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
3298 		       mvm->scan_start);
3299 }
3300 
3301 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
3302 {
3303 	struct iwl_umac_scan_abort cmd = {};
3304 	int uid, ret;
3305 
3306 	lockdep_assert_held(&mvm->mutex);
3307 
3308 	/* We should always get a valid index here, because we already
3309 	 * checked that this type of scan was running in the generic
3310 	 * code.
3311 	 */
3312 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
3313 	if (WARN_ON_ONCE(uid < 0))
3314 		return uid;
3315 
3316 	cmd.uid = cpu_to_le32(uid);
3317 
3318 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
3319 
3320 	ret = iwl_mvm_send_cmd_pdu(mvm,
3321 				   WIDE_ID(IWL_ALWAYS_LONG_GROUP, SCAN_ABORT_UMAC),
3322 				   0, sizeof(cmd), &cmd);
3323 	if (!ret)
3324 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
3325 
3326 	return ret;
3327 }
3328 
3329 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
3330 {
3331 	struct iwl_notification_wait wait_scan_done;
3332 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
3333 					      SCAN_OFFLOAD_COMPLETE, };
3334 	int ret;
3335 
3336 	lockdep_assert_held(&mvm->mutex);
3337 
3338 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
3339 				   scan_done_notif,
3340 				   ARRAY_SIZE(scan_done_notif),
3341 				   NULL, NULL);
3342 
3343 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
3344 
3345 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
3346 		ret = iwl_mvm_umac_scan_abort(mvm, type);
3347 	else
3348 		ret = iwl_mvm_lmac_scan_abort(mvm);
3349 
3350 	if (ret) {
3351 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
3352 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
3353 		return ret;
3354 	}
3355 
3356 	return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
3357 				     1 * HZ);
3358 }
3359 
3360 static size_t iwl_scan_req_umac_get_size(u8 scan_ver)
3361 {
3362 	switch (scan_ver) {
3363 	case 12:
3364 		return sizeof(struct iwl_scan_req_umac_v12);
3365 	case 14:
3366 	case 15:
3367 	case 16:
3368 	case 17:
3369 		return sizeof(struct iwl_scan_req_umac_v17);
3370 	}
3371 
3372 	return 0;
3373 }
3374 
3375 size_t iwl_mvm_scan_size(struct iwl_mvm *mvm)
3376 {
3377 	int base_size, tail_size;
3378 	u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC,
3379 					    IWL_FW_CMD_VER_UNKNOWN);
3380 
3381 	base_size = iwl_scan_req_umac_get_size(scan_ver);
3382 	if (base_size)
3383 		return base_size;
3384 
3385 
3386 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
3387 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
3388 	else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
3389 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
3390 	else if (iwl_mvm_cdb_scan_api(mvm))
3391 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
3392 	else
3393 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
3394 
3395 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
3396 		if (iwl_mvm_is_scan_ext_chan_supported(mvm))
3397 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
3398 		else
3399 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
3400 
3401 		return base_size +
3402 			sizeof(struct iwl_scan_channel_cfg_umac) *
3403 				mvm->fw->ucode_capa.n_scan_channels +
3404 			tail_size;
3405 	}
3406 	return sizeof(struct iwl_scan_req_lmac) +
3407 		sizeof(struct iwl_scan_channel_cfg_lmac) *
3408 		mvm->fw->ucode_capa.n_scan_channels +
3409 		sizeof(struct iwl_scan_probe_req_v1);
3410 }
3411 
3412 /*
3413  * This function is used in nic restart flow, to inform mac80211 about scans
3414  * that was aborted by restart flow or by an assert.
3415  */
3416 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
3417 {
3418 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
3419 		int uid, i;
3420 
3421 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
3422 		if (uid >= 0) {
3423 			struct cfg80211_scan_info info = {
3424 				.aborted = true,
3425 			};
3426 
3427 			cancel_delayed_work(&mvm->scan_timeout_dwork);
3428 
3429 			ieee80211_scan_completed(mvm->hw, &info);
3430 			mvm->scan_uid_status[uid] = 0;
3431 		}
3432 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
3433 		if (uid >= 0) {
3434 			/* Sched scan will be restarted by mac80211 in
3435 			 * restart_hw, so do not report if FW is about to be
3436 			 * restarted.
3437 			 */
3438 			if (!mvm->fw_restart)
3439 				ieee80211_sched_scan_stopped(mvm->hw);
3440 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3441 			mvm->scan_uid_status[uid] = 0;
3442 		}
3443 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_INT_MLO);
3444 		if (uid >= 0) {
3445 			IWL_DEBUG_SCAN(mvm, "Internal MLO scan aborted\n");
3446 			mvm->scan_uid_status[uid] = 0;
3447 		}
3448 
3449 		uid = iwl_mvm_scan_uid_by_status(mvm,
3450 						 IWL_MVM_SCAN_STOPPING_REGULAR);
3451 		if (uid >= 0)
3452 			mvm->scan_uid_status[uid] = 0;
3453 
3454 		uid = iwl_mvm_scan_uid_by_status(mvm,
3455 						 IWL_MVM_SCAN_STOPPING_SCHED);
3456 		if (uid >= 0)
3457 			mvm->scan_uid_status[uid] = 0;
3458 
3459 		uid = iwl_mvm_scan_uid_by_status(mvm,
3460 						 IWL_MVM_SCAN_STOPPING_INT_MLO);
3461 		if (uid >= 0)
3462 			mvm->scan_uid_status[uid] = 0;
3463 
3464 		/* We shouldn't have any UIDs still set.  Loop over all the
3465 		 * UIDs to make sure there's nothing left there and warn if
3466 		 * any is found.
3467 		 */
3468 		for (i = 0; i < mvm->max_scans; i++) {
3469 			if (WARN_ONCE(mvm->scan_uid_status[i],
3470 				      "UMAC scan UID %d status was not cleaned\n",
3471 				      i))
3472 				mvm->scan_uid_status[i] = 0;
3473 		}
3474 	} else {
3475 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
3476 			struct cfg80211_scan_info info = {
3477 				.aborted = true,
3478 			};
3479 
3480 			cancel_delayed_work(&mvm->scan_timeout_dwork);
3481 			ieee80211_scan_completed(mvm->hw, &info);
3482 		}
3483 
3484 		/* Sched scan will be restarted by mac80211 in
3485 		 * restart_hw, so do not report if FW is about to be
3486 		 * restarted.
3487 		 */
3488 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
3489 		    !mvm->fw_restart) {
3490 			ieee80211_sched_scan_stopped(mvm->hw);
3491 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3492 		}
3493 	}
3494 }
3495 
3496 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
3497 {
3498 	int ret;
3499 
3500 	IWL_DEBUG_SCAN(mvm,
3501 		       "Request to stop scan: type=0x%x, status=0x%x\n",
3502 		       type, mvm->scan_status);
3503 
3504 	if (!(mvm->scan_status & type))
3505 		return 0;
3506 
3507 	if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
3508 		ret = 0;
3509 		goto out;
3510 	}
3511 
3512 	ret = iwl_mvm_scan_stop_wait(mvm, type);
3513 	if (!ret)
3514 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
3515 	else
3516 		IWL_DEBUG_SCAN(mvm, "Failed to stop scan\n");
3517 
3518 out:
3519 	/* Clear the scan status so the next scan requests will
3520 	 * succeed and mark the scan as stopping, so that the Rx
3521 	 * handler doesn't do anything, as the scan was stopped from
3522 	 * above.
3523 	 */
3524 	mvm->scan_status &= ~type;
3525 
3526 	if (type == IWL_MVM_SCAN_REGULAR) {
3527 		cancel_delayed_work(&mvm->scan_timeout_dwork);
3528 		if (notify) {
3529 			struct cfg80211_scan_info info = {
3530 				.aborted = true,
3531 			};
3532 
3533 			ieee80211_scan_completed(mvm->hw, &info);
3534 		}
3535 	} else if (notify) {
3536 		ieee80211_sched_scan_stopped(mvm->hw);
3537 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
3538 	}
3539 
3540 	return ret;
3541 }
3542 
3543 static int iwl_mvm_int_mlo_scan_start(struct iwl_mvm *mvm,
3544 				      struct ieee80211_vif *vif,
3545 				      struct ieee80211_channel **channels,
3546 				      size_t n_channels)
3547 {
3548 	struct cfg80211_scan_request *req = NULL;
3549 	struct ieee80211_scan_ies ies = {};
3550 	size_t size, i;
3551 	int ret;
3552 
3553 	lockdep_assert_held(&mvm->mutex);
3554 
3555 	IWL_DEBUG_SCAN(mvm, "Starting Internal MLO scan: n_channels=%zu\n",
3556 		       n_channels);
3557 
3558 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
3559 		return -EINVAL;
3560 
3561 	size = struct_size(req, channels, n_channels);
3562 	req = kzalloc(size, GFP_KERNEL);
3563 	if (!req)
3564 		return -ENOMEM;
3565 
3566 	/* set the requested channels */
3567 	for (i = 0; i < n_channels; i++)
3568 		req->channels[i] = channels[i];
3569 
3570 	req->n_channels = n_channels;
3571 
3572 	/* set the rates */
3573 	for (i = 0; i < NUM_NL80211_BANDS; i++)
3574 		if (mvm->hw->wiphy->bands[i])
3575 			req->rates[i] =
3576 				(1 << mvm->hw->wiphy->bands[i]->n_bitrates) - 1;
3577 
3578 	req->wdev = ieee80211_vif_to_wdev(vif);
3579 	req->wiphy = mvm->hw->wiphy;
3580 	req->scan_start = jiffies;
3581 	req->tsf_report_link_id = -1;
3582 
3583 	ret = _iwl_mvm_single_scan_start(mvm, vif, req, &ies,
3584 					 IWL_MVM_SCAN_INT_MLO);
3585 	kfree(req);
3586 
3587 	IWL_DEBUG_SCAN(mvm, "Internal MLO scan: ret=%d\n", ret);
3588 	return ret;
3589 }
3590 
3591 int iwl_mvm_int_mlo_scan(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3592 {
3593 	struct ieee80211_channel *channels[IEEE80211_MLD_MAX_NUM_LINKS];
3594 	unsigned long usable_links = ieee80211_vif_usable_links(vif);
3595 	size_t n_channels = 0;
3596 	u8 link_id;
3597 
3598 	lockdep_assert_held(&mvm->mutex);
3599 
3600 	if (mvm->scan_status & IWL_MVM_SCAN_INT_MLO) {
3601 		IWL_DEBUG_SCAN(mvm, "Internal MLO scan is already running\n");
3602 		return -EBUSY;
3603 	}
3604 
3605 	rcu_read_lock();
3606 
3607 	for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) {
3608 		struct ieee80211_bss_conf *link_conf =
3609 			rcu_dereference(vif->link_conf[link_id]);
3610 
3611 		if (WARN_ON_ONCE(!link_conf))
3612 			continue;
3613 
3614 		channels[n_channels++] = link_conf->chanreq.oper.chan;
3615 	}
3616 
3617 	rcu_read_unlock();
3618 
3619 	if (!n_channels)
3620 		return -EINVAL;
3621 
3622 	return iwl_mvm_int_mlo_scan_start(mvm, vif, channels, n_channels);
3623 }
3624 
3625 static int iwl_mvm_chanidx_from_phy(struct iwl_mvm *mvm,
3626 				    enum nl80211_band band,
3627 				    u16 phy_chan_num)
3628 {
3629 	struct ieee80211_supported_band *sband = mvm->hw->wiphy->bands[band];
3630 	int chan_idx;
3631 
3632 	if (WARN_ON_ONCE(!sband))
3633 		return -EINVAL;
3634 
3635 	for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
3636 		struct ieee80211_channel *channel = &sband->channels[chan_idx];
3637 
3638 		if (channel->hw_value == phy_chan_num)
3639 			return chan_idx;
3640 	}
3641 
3642 	return -EINVAL;
3643 }
3644 
3645 static u32 iwl_mvm_div_by_db(u32 value, u8 db)
3646 {
3647 	/*
3648 	 * 2^32 * 10**(i / 10) for i = [1, 10], skipping 0 and simply stopping
3649 	 * at 10 dB and looping instead of using a much larger table.
3650 	 *
3651 	 * Using 64 bit math is overkill, but means the helper does not require
3652 	 * a limit on the input range.
3653 	 */
3654 	static const u32 db_to_val[] = {
3655 		0xcb59185e, 0xa1866ba8, 0x804dce7a, 0x65ea59fe, 0x50f44d89,
3656 		0x404de61f, 0x331426af, 0x2892c18b, 0x203a7e5b, 0x1999999a,
3657 	};
3658 
3659 	while (value && db > 0) {
3660 		u8 change = min_t(u8, db, ARRAY_SIZE(db_to_val));
3661 
3662 		value = (((u64)value) * db_to_val[change - 1]) >> 32;
3663 
3664 		db -= change;
3665 	}
3666 
3667 	return value;
3668 }
3669 
3670 VISIBLE_IF_IWLWIFI_KUNIT s8
3671 iwl_mvm_average_dbm_values(const struct iwl_umac_scan_channel_survey_notif *notif)
3672 {
3673 	s8 average_magnitude;
3674 	u32 average_factor;
3675 	s8 sum_magnitude = -128;
3676 	u32 sum_factor = 0;
3677 	int i, count = 0;
3678 
3679 	/*
3680 	 * To properly average the decibel values (signal values given in dBm)
3681 	 * we need to do the math in linear space.  Doing a linear average of
3682 	 * dB (dBm) values is a bit annoying though due to the large range of
3683 	 * at least -10 to -110 dBm that will not fit into a 32 bit integer.
3684 	 *
3685 	 * A 64 bit integer should be sufficient, but then we still have the
3686 	 * problem that there are no directly usable utility functions
3687 	 * available.
3688 	 *
3689 	 * So, lets not deal with that and instead do much of the calculation
3690 	 * with a 16.16 fixed point integer along with a base in dBm. 16.16 bit
3691 	 * gives us plenty of head-room for adding up a few values and even
3692 	 * doing some math on it. And the tail should be accurate enough too
3693 	 * (1/2^16 is somewhere around -48 dB, so effectively zero).
3694 	 *
3695 	 * i.e. the real value of sum is:
3696 	 *      sum = sum_factor / 2^16 * 10^(sum_magnitude / 10) mW
3697 	 *
3698 	 * However, that does mean we need to be able to bring two values to
3699 	 * a common base, so we need a helper for that.
3700 	 *
3701 	 * Note that this function takes an input with unsigned negative dBm
3702 	 * values but returns a signed dBm (i.e. a negative value).
3703 	 */
3704 
3705 	for (i = 0; i < ARRAY_SIZE(notif->noise); i++) {
3706 		s8 val_magnitude;
3707 		u32 val_factor;
3708 
3709 		if (notif->noise[i] == 0xff)
3710 			continue;
3711 
3712 		val_factor = 0x10000;
3713 		val_magnitude = -notif->noise[i];
3714 
3715 		if (val_magnitude <= sum_magnitude) {
3716 			u8 div_db = sum_magnitude - val_magnitude;
3717 
3718 			val_factor = iwl_mvm_div_by_db(val_factor, div_db);
3719 			val_magnitude = sum_magnitude;
3720 		} else {
3721 			u8 div_db = val_magnitude - sum_magnitude;
3722 
3723 			sum_factor = iwl_mvm_div_by_db(sum_factor, div_db);
3724 			sum_magnitude = val_magnitude;
3725 		}
3726 
3727 		sum_factor += val_factor;
3728 		count++;
3729 	}
3730 
3731 	/* No valid noise measurement, return a very high noise level */
3732 	if (count == 0)
3733 		return 0;
3734 
3735 	average_magnitude = sum_magnitude;
3736 	average_factor = sum_factor / count;
3737 
3738 	/*
3739 	 * average_factor will be a number smaller than 1.0 (0x10000) at this
3740 	 * point. What we need to do now is to adjust average_magnitude so that
3741 	 * average_factor is between -0.5 dB and 0.5 dB.
3742 	 *
3743 	 * Just do -1 dB steps and find the point where
3744 	 *   -0.5 dB * -i dB = 0x10000 * 10^(-0.5/10) / i dB
3745 	 *                   = div_by_db(0xe429, i)
3746 	 * is smaller than average_factor.
3747 	 */
3748 	for (i = 0; average_factor < iwl_mvm_div_by_db(0xe429, i); i++) {
3749 		/* nothing */
3750 	}
3751 
3752 	return average_magnitude - i;
3753 }
3754 EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mvm_average_dbm_values);
3755 
3756 void iwl_mvm_rx_channel_survey_notif(struct iwl_mvm *mvm,
3757 				     struct iwl_rx_cmd_buffer *rxb)
3758 {
3759 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3760 	const struct iwl_umac_scan_channel_survey_notif *notif =
3761 		(void *)pkt->data;
3762 	struct iwl_mvm_acs_survey_channel *info;
3763 	enum nl80211_band band;
3764 	int chan_idx;
3765 
3766 	lockdep_assert_held(&mvm->mutex);
3767 
3768 	if (!mvm->acs_survey) {
3769 		size_t n_channels = 0;
3770 
3771 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3772 			if (!mvm->hw->wiphy->bands[band])
3773 				continue;
3774 
3775 			n_channels += mvm->hw->wiphy->bands[band]->n_channels;
3776 		}
3777 
3778 		mvm->acs_survey = kzalloc(struct_size(mvm->acs_survey,
3779 						      channels, n_channels),
3780 					  GFP_KERNEL);
3781 
3782 		if (!mvm->acs_survey)
3783 			return;
3784 
3785 		mvm->acs_survey->n_channels = n_channels;
3786 		n_channels = 0;
3787 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
3788 			if (!mvm->hw->wiphy->bands[band])
3789 				continue;
3790 
3791 			mvm->acs_survey->bands[band] =
3792 				&mvm->acs_survey->channels[n_channels];
3793 			n_channels += mvm->hw->wiphy->bands[band]->n_channels;
3794 		}
3795 	}
3796 
3797 	band = iwl_mvm_nl80211_band_from_phy(le32_to_cpu(notif->band));
3798 	chan_idx = iwl_mvm_chanidx_from_phy(mvm, band,
3799 					    le32_to_cpu(notif->channel));
3800 	if (WARN_ON_ONCE(chan_idx < 0))
3801 		return;
3802 
3803 	IWL_DEBUG_SCAN(mvm, "channel survey received for freq %d\n",
3804 		       mvm->hw->wiphy->bands[band]->channels[chan_idx].center_freq);
3805 
3806 	info = &mvm->acs_survey->bands[band][chan_idx];
3807 
3808 	/* Times are all in ms */
3809 	info->time = le32_to_cpu(notif->active_time);
3810 	info->time_busy = le32_to_cpu(notif->busy_time);
3811 	info->time_rx = le32_to_cpu(notif->rx_time);
3812 	info->time_tx = le32_to_cpu(notif->tx_time);
3813 	info->noise = iwl_mvm_average_dbm_values(notif);
3814 }
3815