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