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