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