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