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