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