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