1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 - 2019 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 - 2019 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 
65 #include <linux/etherdevice.h>
66 #include <net/mac80211.h>
67 
68 #include "mvm.h"
69 #include "fw/api/scan.h"
70 #include "iwl-io.h"
71 
72 #define IWL_DENSE_EBS_SCAN_RATIO 5
73 #define IWL_SPARSE_EBS_SCAN_RATIO 1
74 
75 #define IWL_SCAN_DWELL_ACTIVE		10
76 #define IWL_SCAN_DWELL_PASSIVE		110
77 #define IWL_SCAN_DWELL_FRAGMENTED	44
78 #define IWL_SCAN_DWELL_EXTENDED		90
79 #define IWL_SCAN_NUM_OF_FRAGS		3
80 #define IWL_SCAN_LAST_2_4_CHN		14
81 
82 #define IWL_SCAN_BAND_5_2		0
83 #define IWL_SCAN_BAND_2_4		1
84 
85 /* adaptive dwell max budget time [TU] for full scan */
86 #define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
87 /* adaptive dwell max budget time [TU] for directed scan */
88 #define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
89 /* adaptive dwell default high band APs number */
90 #define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8
91 /* adaptive dwell default low band APs number */
92 #define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2
93 /* adaptive dwell default APs number in social channels (1, 6, 11) */
94 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
95 
96 struct iwl_mvm_scan_timing_params {
97 	u32 suspend_time;
98 	u32 max_out_time;
99 };
100 
101 static struct iwl_mvm_scan_timing_params scan_timing[] = {
102 	[IWL_SCAN_TYPE_UNASSOC] = {
103 		.suspend_time = 0,
104 		.max_out_time = 0,
105 	},
106 	[IWL_SCAN_TYPE_WILD] = {
107 		.suspend_time = 30,
108 		.max_out_time = 120,
109 	},
110 	[IWL_SCAN_TYPE_MILD] = {
111 		.suspend_time = 120,
112 		.max_out_time = 120,
113 	},
114 	[IWL_SCAN_TYPE_FRAGMENTED] = {
115 		.suspend_time = 95,
116 		.max_out_time = 44,
117 	},
118 	[IWL_SCAN_TYPE_FAST_BALANCE] = {
119 		.suspend_time = 30,
120 		.max_out_time = 37,
121 	},
122 };
123 
124 struct iwl_mvm_scan_params {
125 	/* For CDB this is low band scan type, for non-CDB - type. */
126 	enum iwl_mvm_scan_type type;
127 	enum iwl_mvm_scan_type hb_type;
128 	u32 n_channels;
129 	u16 delay;
130 	int n_ssids;
131 	struct cfg80211_ssid *ssids;
132 	struct ieee80211_channel **channels;
133 	u32 flags;
134 	u8 *mac_addr;
135 	u8 *mac_addr_mask;
136 	bool no_cck;
137 	bool pass_all;
138 	int n_match_sets;
139 	struct iwl_scan_probe_req preq;
140 	struct cfg80211_match_set *match_sets;
141 	int n_scan_plans;
142 	struct cfg80211_sched_scan_plan *scan_plans;
143 	u32 measurement_dwell;
144 };
145 
146 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
147 {
148 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
149 
150 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
151 		return (void *)&cmd->v8.data;
152 
153 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
154 		return (void *)&cmd->v7.data;
155 
156 	if (iwl_mvm_cdb_scan_api(mvm))
157 		return (void *)&cmd->v6.data;
158 
159 	return (void *)&cmd->v1.data;
160 }
161 
162 static inline struct iwl_scan_umac_chan_param *
163 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
164 {
165 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
166 
167 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
168 		return &cmd->v8.channel;
169 
170 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
171 		return &cmd->v7.channel;
172 
173 	if (iwl_mvm_cdb_scan_api(mvm))
174 		return &cmd->v6.channel;
175 
176 	return &cmd->v1.channel;
177 }
178 
179 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
180 {
181 	if (mvm->scan_rx_ant != ANT_NONE)
182 		return mvm->scan_rx_ant;
183 	return iwl_mvm_get_valid_rx_ant(mvm);
184 }
185 
186 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
187 {
188 	u16 rx_chain;
189 	u8 rx_ant;
190 
191 	rx_ant = iwl_mvm_scan_rx_ant(mvm);
192 	rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
193 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
194 	rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
195 	rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
196 	return cpu_to_le16(rx_chain);
197 }
198 
199 static __le32 iwl_mvm_scan_rxon_flags(enum nl80211_band band)
200 {
201 	if (band == NL80211_BAND_2GHZ)
202 		return cpu_to_le32(PHY_BAND_24);
203 	else
204 		return cpu_to_le32(PHY_BAND_5);
205 }
206 
207 static inline __le32
208 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
209 			  bool no_cck)
210 {
211 	u32 tx_ant;
212 
213 	iwl_mvm_toggle_tx_ant(mvm, &mvm->scan_last_antenna_idx);
214 	tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
215 
216 	if (band == NL80211_BAND_2GHZ && !no_cck)
217 		return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
218 				   tx_ant);
219 	else
220 		return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
221 }
222 
223 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
224 					    struct ieee80211_vif *vif)
225 {
226 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
227 	int *global_cnt = data;
228 
229 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
230 	    mvmvif->phy_ctxt->id < NUM_PHY_CTX)
231 		*global_cnt += 1;
232 }
233 
234 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
235 {
236 	return mvm->tcm.result.global_load;
237 }
238 
239 static enum iwl_mvm_traffic_load
240 iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
241 {
242 	return mvm->tcm.result.band_load[band];
243 }
244 
245 struct iwl_is_dcm_with_go_iterator_data {
246 	struct ieee80211_vif *current_vif;
247 	bool is_dcm_with_p2p_go;
248 };
249 
250 static void iwl_mvm_is_dcm_with_go_iterator(void *_data, u8 *mac,
251 					    struct ieee80211_vif *vif)
252 {
253 	struct iwl_is_dcm_with_go_iterator_data *data = _data;
254 	struct iwl_mvm_vif *other_mvmvif = iwl_mvm_vif_from_mac80211(vif);
255 	struct iwl_mvm_vif *curr_mvmvif =
256 		iwl_mvm_vif_from_mac80211(data->current_vif);
257 
258 	/* exclude the given vif */
259 	if (vif == data->current_vif)
260 		return;
261 
262 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
263 	    other_mvmvif->phy_ctxt && curr_mvmvif->phy_ctxt &&
264 	    other_mvmvif->phy_ctxt->id != curr_mvmvif->phy_ctxt->id)
265 		data->is_dcm_with_p2p_go = true;
266 }
267 
268 static enum
269 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
270 					 struct ieee80211_vif *vif,
271 					 enum iwl_mvm_traffic_load load,
272 					 bool low_latency)
273 {
274 	int global_cnt = 0;
275 
276 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
277 					    IEEE80211_IFACE_ITER_NORMAL,
278 					    iwl_mvm_scan_condition_iterator,
279 					    &global_cnt);
280 	if (!global_cnt)
281 		return IWL_SCAN_TYPE_UNASSOC;
282 
283 	if (fw_has_api(&mvm->fw->ucode_capa,
284 		       IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
285 		if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
286 		    (!vif || vif->type != NL80211_IFTYPE_P2P_DEVICE))
287 			return IWL_SCAN_TYPE_FRAGMENTED;
288 
289 		/* in case of DCM with GO where BSS DTIM interval < 220msec
290 		 * set all scan requests as fast-balance scan
291 		 * */
292 		if (vif && vif->type == NL80211_IFTYPE_STATION &&
293 		    vif->bss_conf.dtim_period < 220) {
294 			struct iwl_is_dcm_with_go_iterator_data data = {
295 				.current_vif = vif,
296 				.is_dcm_with_p2p_go = false,
297 			};
298 
299 			ieee80211_iterate_active_interfaces_atomic(mvm->hw,
300 						IEEE80211_IFACE_ITER_NORMAL,
301 						iwl_mvm_is_dcm_with_go_iterator,
302 						&data);
303 			if (data.is_dcm_with_p2p_go)
304 				return IWL_SCAN_TYPE_FAST_BALANCE;
305 		}
306 	}
307 
308 	if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
309 		return IWL_SCAN_TYPE_MILD;
310 
311 	return IWL_SCAN_TYPE_WILD;
312 }
313 
314 static enum
315 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
316 					struct ieee80211_vif *vif)
317 {
318 	enum iwl_mvm_traffic_load load;
319 	bool low_latency;
320 
321 	load = iwl_mvm_get_traffic_load(mvm);
322 	low_latency = iwl_mvm_low_latency(mvm);
323 
324 	return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
325 }
326 
327 static enum
328 iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
329 					     struct ieee80211_vif *vif,
330 					     enum nl80211_band band)
331 {
332 	enum iwl_mvm_traffic_load load;
333 	bool low_latency;
334 
335 	load = iwl_mvm_get_traffic_load_band(mvm, band);
336 	low_latency = iwl_mvm_low_latency_band(mvm, band);
337 
338 	return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
339 }
340 
341 static int
342 iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
343 			      struct cfg80211_scan_request *req,
344 			      struct iwl_mvm_scan_params *params)
345 {
346 	u32 duration = scan_timing[params->type].max_out_time;
347 
348 	if (!req->duration)
349 		return 0;
350 
351 	if (iwl_mvm_is_cdb_supported(mvm)) {
352 		u32 hb_time = scan_timing[params->hb_type].max_out_time;
353 
354 		duration = min_t(u32, duration, hb_time);
355 	}
356 
357 	if (req->duration_mandatory && req->duration > duration) {
358 		IWL_DEBUG_SCAN(mvm,
359 			       "Measurement scan - too long dwell %hu (max out time %u)\n",
360 			       req->duration,
361 			       duration);
362 		return -EOPNOTSUPP;
363 	}
364 
365 	return min_t(u32, (u32)req->duration, duration);
366 }
367 
368 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
369 {
370 	/* require rrm scan whenever the fw supports it */
371 	return fw_has_capa(&mvm->fw->ucode_capa,
372 			   IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
373 }
374 
375 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
376 {
377 	int max_probe_len;
378 
379 	max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
380 
381 	/* we create the 802.11 header and SSID element */
382 	max_probe_len -= 24 + 2;
383 
384 	/* DS parameter set element is added on 2.4GHZ band if required */
385 	if (iwl_mvm_rrm_scan_needed(mvm))
386 		max_probe_len -= 3;
387 
388 	return max_probe_len;
389 }
390 
391 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
392 {
393 	int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
394 
395 	/* TODO: [BUG] This function should return the maximum allowed size of
396 	 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
397 	 * in the same command. So the correct implementation of this function
398 	 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
399 	 * command has only 512 bytes and it would leave us with about 240
400 	 * bytes for scan IEs, which is clearly not enough. So meanwhile
401 	 * we will report an incorrect value. This may result in a failure to
402 	 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
403 	 * functions with -ENOBUFS, if a large enough probe will be provided.
404 	 */
405 	return max_ie_len;
406 }
407 
408 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
409 					      struct iwl_rx_cmd_buffer *rxb)
410 {
411 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
412 	struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
413 
414 	IWL_DEBUG_SCAN(mvm,
415 		       "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
416 		       notif->status, notif->scanned_channels);
417 
418 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
419 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
420 		ieee80211_sched_scan_results(mvm->hw);
421 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
422 	}
423 }
424 
425 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
426 				 struct iwl_rx_cmd_buffer *rxb)
427 {
428 	IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
429 	ieee80211_sched_scan_results(mvm->hw);
430 }
431 
432 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
433 {
434 	switch (status) {
435 	case IWL_SCAN_EBS_SUCCESS:
436 		return "successful";
437 	case IWL_SCAN_EBS_INACTIVE:
438 		return "inactive";
439 	case IWL_SCAN_EBS_FAILED:
440 	case IWL_SCAN_EBS_CHAN_NOT_FOUND:
441 	default:
442 		return "failed";
443 	}
444 }
445 
446 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
447 					 struct iwl_rx_cmd_buffer *rxb)
448 {
449 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
450 	struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
451 	bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
452 
453 	/* If this happens, the firmware has mistakenly sent an LMAC
454 	 * notification during UMAC scans -- warn and ignore it.
455 	 */
456 	if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
457 				     IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
458 		return;
459 
460 	/* scan status must be locked for proper checking */
461 	lockdep_assert_held(&mvm->mutex);
462 
463 	/* We first check if we were stopping a scan, in which case we
464 	 * just clear the stopping flag.  Then we check if it was a
465 	 * firmware initiated stop, in which case we need to inform
466 	 * mac80211.
467 	 * Note that we can have a stopping and a running scan
468 	 * simultaneously, but we can't have two different types of
469 	 * scans stopping or running at the same time (since LMAC
470 	 * doesn't support it).
471 	 */
472 
473 	if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
474 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
475 
476 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
477 			       aborted ? "aborted" : "completed",
478 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
479 		IWL_DEBUG_SCAN(mvm,
480 			       "Last line %d, Last iteration %d, Time after last iteration %d\n",
481 			       scan_notif->last_schedule_line,
482 			       scan_notif->last_schedule_iteration,
483 			       __le32_to_cpu(scan_notif->time_after_last_iter));
484 
485 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
486 	} else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
487 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
488 			       aborted ? "aborted" : "completed",
489 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
490 
491 		mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
492 	} else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
493 		WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
494 
495 		IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
496 			       aborted ? "aborted" : "completed",
497 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
498 		IWL_DEBUG_SCAN(mvm,
499 			       "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
500 			       scan_notif->last_schedule_line,
501 			       scan_notif->last_schedule_iteration,
502 			       __le32_to_cpu(scan_notif->time_after_last_iter));
503 
504 		mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
505 		ieee80211_sched_scan_stopped(mvm->hw);
506 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
507 	} else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
508 		struct cfg80211_scan_info info = {
509 			.aborted = aborted,
510 		};
511 
512 		IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
513 			       aborted ? "aborted" : "completed",
514 			       iwl_mvm_ebs_status_str(scan_notif->ebs_status));
515 
516 		mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
517 		ieee80211_scan_completed(mvm->hw, &info);
518 		cancel_delayed_work(&mvm->scan_timeout_dwork);
519 		iwl_mvm_resume_tcm(mvm);
520 	} else {
521 		IWL_ERR(mvm,
522 			"got scan complete notification but no scan is running\n");
523 	}
524 
525 	mvm->last_ebs_successful =
526 			scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
527 			scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
528 }
529 
530 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
531 {
532 	int i;
533 
534 	for (i = 0; i < PROBE_OPTION_MAX; i++) {
535 		if (!ssid_list[i].len)
536 			break;
537 		if (ssid_list[i].len == ssid_len &&
538 		    !memcmp(ssid_list->ssid, ssid, ssid_len))
539 			return i;
540 	}
541 	return -1;
542 }
543 
544 /* We insert the SSIDs in an inverted order, because the FW will
545  * invert it back.
546  */
547 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
548 				 struct iwl_ssid_ie *ssids,
549 				 u32 *ssid_bitmap)
550 {
551 	int i, j;
552 	int index;
553 
554 	/*
555 	 * copy SSIDs from match list.
556 	 * iwl_config_sched_scan_profiles() uses the order of these ssids to
557 	 * config match list.
558 	 */
559 	for (i = 0, j = params->n_match_sets - 1;
560 	     j >= 0 && i < PROBE_OPTION_MAX;
561 	     i++, j--) {
562 		/* skip empty SSID matchsets */
563 		if (!params->match_sets[j].ssid.ssid_len)
564 			continue;
565 		ssids[i].id = WLAN_EID_SSID;
566 		ssids[i].len = params->match_sets[j].ssid.ssid_len;
567 		memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
568 		       ssids[i].len);
569 	}
570 
571 	/* add SSIDs from scan SSID list */
572 	*ssid_bitmap = 0;
573 	for (j = params->n_ssids - 1;
574 	     j >= 0 && i < PROBE_OPTION_MAX;
575 	     i++, j--) {
576 		index = iwl_ssid_exist(params->ssids[j].ssid,
577 				       params->ssids[j].ssid_len,
578 				       ssids);
579 		if (index < 0) {
580 			ssids[i].id = WLAN_EID_SSID;
581 			ssids[i].len = params->ssids[j].ssid_len;
582 			memcpy(ssids[i].ssid, params->ssids[j].ssid,
583 			       ssids[i].len);
584 			*ssid_bitmap |= BIT(i);
585 		} else {
586 			*ssid_bitmap |= BIT(index);
587 		}
588 	}
589 }
590 
591 static int
592 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
593 				   struct cfg80211_sched_scan_request *req)
594 {
595 	struct iwl_scan_offload_profile *profile;
596 	struct iwl_scan_offload_profile_cfg *profile_cfg;
597 	struct iwl_scan_offload_blacklist *blacklist;
598 	struct iwl_host_cmd cmd = {
599 		.id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
600 		.len[1] = sizeof(*profile_cfg),
601 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
602 		.dataflags[1] = IWL_HCMD_DFL_NOCOPY,
603 	};
604 	int blacklist_len;
605 	int i;
606 	int ret;
607 
608 	if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
609 		return -EIO;
610 
611 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
612 		blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
613 	else
614 		blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
615 
616 	blacklist = kcalloc(blacklist_len, sizeof(*blacklist), GFP_KERNEL);
617 	if (!blacklist)
618 		return -ENOMEM;
619 
620 	profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
621 	if (!profile_cfg) {
622 		ret = -ENOMEM;
623 		goto free_blacklist;
624 	}
625 
626 	cmd.data[0] = blacklist;
627 	cmd.len[0] = sizeof(*blacklist) * blacklist_len;
628 	cmd.data[1] = profile_cfg;
629 
630 	/* No blacklist configuration */
631 
632 	profile_cfg->num_profiles = req->n_match_sets;
633 	profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
634 	profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
635 	profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
636 	if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
637 		profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
638 
639 	for (i = 0; i < req->n_match_sets; i++) {
640 		profile = &profile_cfg->profiles[i];
641 		profile->ssid_index = i;
642 		/* Support any cipher and auth algorithm */
643 		profile->unicast_cipher = 0xff;
644 		profile->auth_alg = 0xff;
645 		profile->network_type = IWL_NETWORK_TYPE_ANY;
646 		profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
647 		profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
648 	}
649 
650 	IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
651 
652 	ret = iwl_mvm_send_cmd(mvm, &cmd);
653 	kfree(profile_cfg);
654 free_blacklist:
655 	kfree(blacklist);
656 
657 	return ret;
658 }
659 
660 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
661 				  struct cfg80211_sched_scan_request *req)
662 {
663 	if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
664 		IWL_DEBUG_SCAN(mvm,
665 			       "Sending scheduled scan with filtering, n_match_sets %d\n",
666 			       req->n_match_sets);
667 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
668 		return false;
669 	}
670 
671 	IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
672 
673 	mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
674 	return true;
675 }
676 
677 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
678 {
679 	int ret;
680 	struct iwl_host_cmd cmd = {
681 		.id = SCAN_OFFLOAD_ABORT_CMD,
682 	};
683 	u32 status = CAN_ABORT_STATUS;
684 
685 	ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
686 	if (ret)
687 		return ret;
688 
689 	if (status != CAN_ABORT_STATUS) {
690 		/*
691 		 * The scan abort will return 1 for success or
692 		 * 2 for "failure".  A failure condition can be
693 		 * due to simply not being in an active scan which
694 		 * can occur if we send the scan abort before the
695 		 * microcode has notified us that a scan is completed.
696 		 */
697 		IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
698 		ret = -ENOENT;
699 	}
700 
701 	return ret;
702 }
703 
704 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
705 				     struct iwl_scan_req_tx_cmd *tx_cmd,
706 				     bool no_cck)
707 {
708 	tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
709 					 TX_CMD_FLG_BT_DIS);
710 	tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
711 							   NL80211_BAND_2GHZ,
712 							   no_cck);
713 	tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
714 
715 	tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
716 					 TX_CMD_FLG_BT_DIS);
717 	tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
718 							   NL80211_BAND_5GHZ,
719 							   no_cck);
720 	tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
721 }
722 
723 static void
724 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
725 			       struct ieee80211_channel **channels,
726 			       int n_channels, u32 ssid_bitmap,
727 			       struct iwl_scan_req_lmac *cmd)
728 {
729 	struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
730 	int i;
731 
732 	for (i = 0; i < n_channels; i++) {
733 		channel_cfg[i].channel_num =
734 			cpu_to_le16(channels[i]->hw_value);
735 		channel_cfg[i].iter_count = cpu_to_le16(1);
736 		channel_cfg[i].iter_interval = 0;
737 		channel_cfg[i].flags =
738 			cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
739 				    ssid_bitmap);
740 	}
741 }
742 
743 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
744 					   size_t len, u8 *const pos)
745 {
746 	static const u8 before_ds_params[] = {
747 			WLAN_EID_SSID,
748 			WLAN_EID_SUPP_RATES,
749 			WLAN_EID_REQUEST,
750 			WLAN_EID_EXT_SUPP_RATES,
751 	};
752 	size_t offs;
753 	u8 *newpos = pos;
754 
755 	if (!iwl_mvm_rrm_scan_needed(mvm)) {
756 		memcpy(newpos, ies, len);
757 		return newpos + len;
758 	}
759 
760 	offs = ieee80211_ie_split(ies, len,
761 				  before_ds_params,
762 				  ARRAY_SIZE(before_ds_params),
763 				  0);
764 
765 	memcpy(newpos, ies, offs);
766 	newpos += offs;
767 
768 	/* Add a placeholder for DS Parameter Set element */
769 	*newpos++ = WLAN_EID_DS_PARAMS;
770 	*newpos++ = 1;
771 	*newpos++ = 0;
772 
773 	memcpy(newpos, ies + offs, len - offs);
774 	newpos += len - offs;
775 
776 	return newpos;
777 }
778 
779 #define WFA_TPC_IE_LEN	9
780 
781 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
782 {
783 	pos[0] = WLAN_EID_VENDOR_SPECIFIC;
784 	pos[1] = WFA_TPC_IE_LEN - 2;
785 	pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
786 	pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
787 	pos[4] = WLAN_OUI_MICROSOFT & 0xff;
788 	pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
789 	pos[6] = 0;
790 	/* pos[7] - tx power will be inserted by the FW */
791 	pos[7] = 0;
792 	pos[8] = 0;
793 }
794 
795 static void
796 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
797 			 struct ieee80211_scan_ies *ies,
798 			 struct iwl_mvm_scan_params *params)
799 {
800 	struct ieee80211_mgmt *frame = (void *)params->preq.buf;
801 	u8 *pos, *newpos;
802 	const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
803 		params->mac_addr : NULL;
804 
805 	/*
806 	 * Unfortunately, right now the offload scan doesn't support randomising
807 	 * within the firmware, so until the firmware API is ready we implement
808 	 * it in the driver. This means that the scan iterations won't really be
809 	 * random, only when it's restarted, but at least that helps a bit.
810 	 */
811 	if (mac_addr)
812 		get_random_mask_addr(frame->sa, mac_addr,
813 				     params->mac_addr_mask);
814 	else
815 		memcpy(frame->sa, vif->addr, ETH_ALEN);
816 
817 	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
818 	eth_broadcast_addr(frame->da);
819 	eth_broadcast_addr(frame->bssid);
820 	frame->seq_ctrl = 0;
821 
822 	pos = frame->u.probe_req.variable;
823 	*pos++ = WLAN_EID_SSID;
824 	*pos++ = 0;
825 
826 	params->preq.mac_header.offset = 0;
827 	params->preq.mac_header.len = cpu_to_le16(24 + 2);
828 
829 	/* Insert ds parameter set element on 2.4 GHz band */
830 	newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
831 						 ies->ies[NL80211_BAND_2GHZ],
832 						 ies->len[NL80211_BAND_2GHZ],
833 						 pos);
834 	params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
835 	params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
836 	pos = newpos;
837 
838 	memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
839 	       ies->len[NL80211_BAND_5GHZ]);
840 	params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
841 	params->preq.band_data[1].len =
842 		cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
843 	pos += ies->len[NL80211_BAND_5GHZ];
844 
845 	memcpy(pos, ies->common_ies, ies->common_ie_len);
846 	params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
847 
848 	if (iwl_mvm_rrm_scan_needed(mvm) &&
849 	    !fw_has_capa(&mvm->fw->ucode_capa,
850 			 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
851 		iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
852 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
853 							   WFA_TPC_IE_LEN);
854 	} else {
855 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
856 	}
857 }
858 
859 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
860 				    struct iwl_scan_req_lmac *cmd,
861 				    struct iwl_mvm_scan_params *params)
862 {
863 	cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
864 	cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
865 	cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
866 	cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
867 	cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
868 	cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
869 	cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
870 }
871 
872 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
873 				     struct ieee80211_scan_ies *ies,
874 				     int n_channels)
875 {
876 	return ((n_ssids <= PROBE_OPTION_MAX) &&
877 		(n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
878 		(ies->common_ie_len +
879 		 ies->len[NL80211_BAND_2GHZ] +
880 		 ies->len[NL80211_BAND_5GHZ] <=
881 		 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
882 }
883 
884 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
885 					struct ieee80211_vif *vif)
886 {
887 	const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
888 	bool low_latency;
889 
890 	if (iwl_mvm_is_cdb_supported(mvm))
891 		low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
892 	else
893 		low_latency = iwl_mvm_low_latency(mvm);
894 
895 	/* We can only use EBS if:
896 	 *	1. the feature is supported;
897 	 *	2. the last EBS was successful;
898 	 *	3. if only single scan, the single scan EBS API is supported;
899 	 *	4. it's not a p2p find operation.
900 	 *	5. we are not in low latency mode,
901 	 *	   or if fragmented ebs is supported by the FW
902 	 */
903 	return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
904 		mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
905 		vif->type != NL80211_IFTYPE_P2P_DEVICE &&
906 		(!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
907 }
908 
909 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
910 {
911 	return params->n_scan_plans == 1 &&
912 		params->scan_plans[0].iterations == 1;
913 }
914 
915 static bool iwl_mvm_is_scan_fragmented(enum iwl_mvm_scan_type type)
916 {
917 	return (type == IWL_SCAN_TYPE_FRAGMENTED ||
918 		type == IWL_SCAN_TYPE_FAST_BALANCE);
919 }
920 
921 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
922 				   struct iwl_mvm_scan_params *params,
923 				   struct ieee80211_vif *vif)
924 {
925 	int flags = 0;
926 
927 	if (params->n_ssids == 0)
928 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
929 
930 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
931 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
932 
933 	if (iwl_mvm_is_scan_fragmented(params->type))
934 		flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
935 
936 	if (iwl_mvm_rrm_scan_needed(mvm) &&
937 	    fw_has_capa(&mvm->fw->ucode_capa,
938 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
939 		flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
940 
941 	if (params->pass_all)
942 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
943 	else
944 		flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
945 
946 #ifdef CONFIG_IWLWIFI_DEBUGFS
947 	if (mvm->scan_iter_notif_enabled)
948 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
949 #endif
950 
951 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
952 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
953 
954 	if (iwl_mvm_is_regular_scan(params) &&
955 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
956 	    !iwl_mvm_is_scan_fragmented(params->type))
957 		flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
958 
959 	return flags;
960 }
961 
962 static void
963 iwl_mvm_scan_set_legacy_probe_req(struct iwl_scan_probe_req_v1 *p_req,
964 				  struct iwl_scan_probe_req *src_p_req)
965 {
966 	int i;
967 
968 	p_req->mac_header = src_p_req->mac_header;
969 	for (i = 0; i < SCAN_NUM_BAND_PROBE_DATA_V_1; i++)
970 		p_req->band_data[i] = src_p_req->band_data[i];
971 	p_req->common_data = src_p_req->common_data;
972 	memcpy(p_req->buf, src_p_req->buf, sizeof(p_req->buf));
973 }
974 
975 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
976 			     struct iwl_mvm_scan_params *params)
977 {
978 	struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
979 	struct iwl_scan_probe_req_v1 *preq =
980 		(void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
981 			 mvm->fw->ucode_capa.n_scan_channels);
982 	u32 ssid_bitmap = 0;
983 	int i;
984 
985 	lockdep_assert_held(&mvm->mutex);
986 
987 	memset(cmd, 0, ksize(cmd));
988 
989 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
990 		return -EINVAL;
991 
992 	iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
993 
994 	cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
995 	cmd->iter_num = cpu_to_le32(1);
996 	cmd->n_channels = (u8)params->n_channels;
997 
998 	cmd->delay = cpu_to_le32(params->delay);
999 
1000 	cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
1001 							      vif));
1002 
1003 	cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
1004 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1005 					MAC_FILTER_IN_BEACON);
1006 	iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
1007 	iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
1008 
1009 	/* this API uses bits 1-20 instead of 0-19 */
1010 	ssid_bitmap <<= 1;
1011 
1012 	for (i = 0; i < params->n_scan_plans; i++) {
1013 		struct cfg80211_sched_scan_plan *scan_plan =
1014 			&params->scan_plans[i];
1015 
1016 		cmd->schedule[i].delay =
1017 			cpu_to_le16(scan_plan->interval);
1018 		cmd->schedule[i].iterations = scan_plan->iterations;
1019 		cmd->schedule[i].full_scan_mul = 1;
1020 	}
1021 
1022 	/*
1023 	 * If the number of iterations of the last scan plan is set to
1024 	 * zero, it should run infinitely. However, this is not always the case.
1025 	 * For example, when regular scan is requested the driver sets one scan
1026 	 * plan with one iteration.
1027 	 */
1028 	if (!cmd->schedule[i - 1].iterations)
1029 		cmd->schedule[i - 1].iterations = 0xff;
1030 
1031 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1032 		cmd->channel_opt[0].flags =
1033 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1034 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1035 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1036 		cmd->channel_opt[0].non_ebs_ratio =
1037 			cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1038 		cmd->channel_opt[1].flags =
1039 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1040 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1041 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1042 		cmd->channel_opt[1].non_ebs_ratio =
1043 			cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1044 	}
1045 
1046 	iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
1047 				       params->n_channels, ssid_bitmap, cmd);
1048 
1049 	iwl_mvm_scan_set_legacy_probe_req(preq, &params->preq);
1050 
1051 	return 0;
1052 }
1053 
1054 static int rate_to_scan_rate_flag(unsigned int rate)
1055 {
1056 	static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1057 		[IWL_RATE_1M_INDEX]	= SCAN_CONFIG_RATE_1M,
1058 		[IWL_RATE_2M_INDEX]	= SCAN_CONFIG_RATE_2M,
1059 		[IWL_RATE_5M_INDEX]	= SCAN_CONFIG_RATE_5M,
1060 		[IWL_RATE_11M_INDEX]	= SCAN_CONFIG_RATE_11M,
1061 		[IWL_RATE_6M_INDEX]	= SCAN_CONFIG_RATE_6M,
1062 		[IWL_RATE_9M_INDEX]	= SCAN_CONFIG_RATE_9M,
1063 		[IWL_RATE_12M_INDEX]	= SCAN_CONFIG_RATE_12M,
1064 		[IWL_RATE_18M_INDEX]	= SCAN_CONFIG_RATE_18M,
1065 		[IWL_RATE_24M_INDEX]	= SCAN_CONFIG_RATE_24M,
1066 		[IWL_RATE_36M_INDEX]	= SCAN_CONFIG_RATE_36M,
1067 		[IWL_RATE_48M_INDEX]	= SCAN_CONFIG_RATE_48M,
1068 		[IWL_RATE_54M_INDEX]	= SCAN_CONFIG_RATE_54M,
1069 	};
1070 
1071 	return rate_to_scan_rate[rate];
1072 }
1073 
1074 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1075 {
1076 	struct ieee80211_supported_band *band;
1077 	unsigned int rates = 0;
1078 	int i;
1079 
1080 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1081 	for (i = 0; i < band->n_bitrates; i++)
1082 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1083 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1084 	for (i = 0; i < band->n_bitrates; i++)
1085 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1086 
1087 	/* Set both basic rates and supported rates */
1088 	rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1089 
1090 	return cpu_to_le32(rates);
1091 }
1092 
1093 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1094 				    struct iwl_scan_dwell *dwell)
1095 {
1096 	dwell->active = IWL_SCAN_DWELL_ACTIVE;
1097 	dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1098 	dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1099 	dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1100 }
1101 
1102 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels,
1103 				  u32 max_channels)
1104 {
1105 	struct ieee80211_supported_band *band;
1106 	int i, j = 0;
1107 
1108 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1109 	for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1110 		channels[j] = band->channels[i].hw_value;
1111 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1112 	for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1113 		channels[j] = band->channels[i].hw_value;
1114 }
1115 
1116 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1117 					u32 flags, u8 channel_flags,
1118 					u32 max_channels)
1119 {
1120 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, NULL);
1121 	struct iwl_scan_config_v1 *cfg = config;
1122 
1123 	cfg->flags = cpu_to_le32(flags);
1124 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1125 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1126 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1127 	cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1128 	cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1129 
1130 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1131 
1132 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1133 
1134 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1135 	cfg->channel_flags = channel_flags;
1136 
1137 	iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1138 }
1139 
1140 static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
1141 				     u32 flags, u8 channel_flags,
1142 				     u32 max_channels)
1143 {
1144 	struct iwl_scan_config *cfg = config;
1145 
1146 	cfg->flags = cpu_to_le32(flags);
1147 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1148 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1149 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1150 
1151 	if (iwl_mvm_is_cdb_supported(mvm)) {
1152 		enum iwl_mvm_scan_type lb_type, hb_type;
1153 
1154 		lb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1155 						     NL80211_BAND_2GHZ);
1156 		hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1157 						     NL80211_BAND_5GHZ);
1158 
1159 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1160 			cpu_to_le32(scan_timing[lb_type].max_out_time);
1161 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1162 			cpu_to_le32(scan_timing[lb_type].suspend_time);
1163 
1164 		cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1165 			cpu_to_le32(scan_timing[hb_type].max_out_time);
1166 		cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1167 			cpu_to_le32(scan_timing[hb_type].suspend_time);
1168 	} else {
1169 		enum iwl_mvm_scan_type type =
1170 			iwl_mvm_get_scan_type(mvm, NULL);
1171 
1172 		cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1173 			cpu_to_le32(scan_timing[type].max_out_time);
1174 		cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1175 			cpu_to_le32(scan_timing[type].suspend_time);
1176 	}
1177 
1178 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1179 
1180 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1181 
1182 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1183 	cfg->channel_flags = channel_flags;
1184 
1185 	iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1186 }
1187 
1188 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1189 {
1190 	void *cfg;
1191 	int ret, cmd_size;
1192 	struct iwl_host_cmd cmd = {
1193 		.id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1194 	};
1195 	enum iwl_mvm_scan_type type;
1196 	enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1197 	int num_channels =
1198 		mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1199 		mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1200 	u32 flags;
1201 	u8 channel_flags;
1202 
1203 	if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1204 		num_channels = mvm->fw->ucode_capa.n_scan_channels;
1205 
1206 	if (iwl_mvm_is_cdb_supported(mvm)) {
1207 		type = iwl_mvm_get_scan_type_band(mvm, NULL,
1208 						  NL80211_BAND_2GHZ);
1209 		hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1210 						     NL80211_BAND_5GHZ);
1211 		if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1212 			return 0;
1213 	} else {
1214 		type = iwl_mvm_get_scan_type(mvm, NULL);
1215 		if (type == mvm->scan_type)
1216 			return 0;
1217 	}
1218 
1219 	if (iwl_mvm_cdb_scan_api(mvm))
1220 		cmd_size = sizeof(struct iwl_scan_config);
1221 	else
1222 		cmd_size = sizeof(struct iwl_scan_config_v1);
1223 	cmd_size += num_channels;
1224 
1225 	cfg = kzalloc(cmd_size, GFP_KERNEL);
1226 	if (!cfg)
1227 		return -ENOMEM;
1228 
1229 	flags = SCAN_CONFIG_FLAG_ACTIVATE |
1230 		 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1231 		 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1232 		 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1233 		 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1234 		 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1235 		 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1236 		 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1237 		 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1238 		 SCAN_CONFIG_N_CHANNELS(num_channels) |
1239 		 (iwl_mvm_is_scan_fragmented(type) ?
1240 		  SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1241 		  SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1242 
1243 	channel_flags = IWL_CHANNEL_FLAG_EBS |
1244 			IWL_CHANNEL_FLAG_ACCURATE_EBS |
1245 			IWL_CHANNEL_FLAG_EBS_ADD |
1246 			IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1247 
1248 	/*
1249 	 * Check for fragmented scan on LMAC2 - high band.
1250 	 * LMAC1 - low band is checked above.
1251 	 */
1252 	if (iwl_mvm_cdb_scan_api(mvm)) {
1253 		if (iwl_mvm_is_cdb_supported(mvm))
1254 			flags |= (iwl_mvm_is_scan_fragmented(hb_type)) ?
1255 				 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1256 				 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1257 		iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags,
1258 					 num_channels);
1259 	} else {
1260 		iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags,
1261 					    num_channels);
1262 	}
1263 
1264 	cmd.data[0] = cfg;
1265 	cmd.len[0] = cmd_size;
1266 	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1267 
1268 	IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1269 
1270 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1271 	if (!ret) {
1272 		mvm->scan_type = type;
1273 		mvm->hb_scan_type = hb_type;
1274 	}
1275 
1276 	kfree(cfg);
1277 	return ret;
1278 }
1279 
1280 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1281 {
1282 	int i;
1283 
1284 	for (i = 0; i < mvm->max_scans; i++)
1285 		if (mvm->scan_uid_status[i] == status)
1286 			return i;
1287 
1288 	return -ENOENT;
1289 }
1290 
1291 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1292 				    struct iwl_scan_req_umac *cmd,
1293 				    struct iwl_mvm_scan_params *params)
1294 {
1295 	struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1296 	u8 active_dwell, passive_dwell;
1297 
1298 	timing = &scan_timing[params->type];
1299 	active_dwell = params->measurement_dwell ?
1300 		params->measurement_dwell : IWL_SCAN_DWELL_ACTIVE;
1301 	passive_dwell = params->measurement_dwell ?
1302 		params->measurement_dwell : IWL_SCAN_DWELL_PASSIVE;
1303 
1304 	if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1305 		cmd->v7.adwell_default_n_aps_social =
1306 			IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1307 		cmd->v7.adwell_default_n_aps =
1308 			IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1309 
1310 		if (iwl_mvm_is_adwell_hb_ap_num_supported(mvm))
1311 			cmd->v9.adwell_default_hb_n_aps =
1312 				IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1313 
1314 		/* if custom max budget was configured with debugfs */
1315 		if (IWL_MVM_ADWELL_MAX_BUDGET)
1316 			cmd->v7.adwell_max_budget =
1317 				cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1318 		else if (params->ssids && params->ssids[0].ssid_len)
1319 			cmd->v7.adwell_max_budget =
1320 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1321 		else
1322 			cmd->v7.adwell_max_budget =
1323 				cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1324 
1325 		cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1326 		cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1327 			cpu_to_le32(timing->max_out_time);
1328 		cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1329 			cpu_to_le32(timing->suspend_time);
1330 
1331 		if (iwl_mvm_is_cdb_supported(mvm)) {
1332 			hb_timing = &scan_timing[params->hb_type];
1333 
1334 			cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1335 				cpu_to_le32(hb_timing->max_out_time);
1336 			cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1337 				cpu_to_le32(hb_timing->suspend_time);
1338 		}
1339 
1340 		if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1341 			cmd->v7.active_dwell = active_dwell;
1342 			cmd->v7.passive_dwell = passive_dwell;
1343 			cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1344 		} else {
1345 			cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1346 			cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1347 			if (iwl_mvm_is_cdb_supported(mvm)) {
1348 				cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1349 					active_dwell;
1350 				cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1351 					passive_dwell;
1352 			}
1353 		}
1354 	} else {
1355 		cmd->v1.extended_dwell = params->measurement_dwell ?
1356 			params->measurement_dwell : IWL_SCAN_DWELL_EXTENDED;
1357 		cmd->v1.active_dwell = active_dwell;
1358 		cmd->v1.passive_dwell = passive_dwell;
1359 		cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1360 
1361 		if (iwl_mvm_is_cdb_supported(mvm)) {
1362 			hb_timing = &scan_timing[params->hb_type];
1363 
1364 			cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1365 					cpu_to_le32(hb_timing->max_out_time);
1366 			cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1367 					cpu_to_le32(hb_timing->suspend_time);
1368 		}
1369 
1370 		if (iwl_mvm_cdb_scan_api(mvm)) {
1371 			cmd->v6.scan_priority =
1372 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1373 			cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1374 				cpu_to_le32(timing->max_out_time);
1375 			cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1376 				cpu_to_le32(timing->suspend_time);
1377 		} else {
1378 			cmd->v1.scan_priority =
1379 				cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1380 			cmd->v1.max_out_time =
1381 				cpu_to_le32(timing->max_out_time);
1382 			cmd->v1.suspend_time =
1383 				cpu_to_le32(timing->suspend_time);
1384 		}
1385 	}
1386 
1387 	if (iwl_mvm_is_regular_scan(params))
1388 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1389 	else
1390 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1391 }
1392 
1393 static void
1394 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1395 			       struct ieee80211_channel **channels,
1396 			       int n_channels, u32 ssid_bitmap,
1397 			       struct iwl_scan_channel_cfg_umac *channel_cfg)
1398 {
1399 	int i;
1400 
1401 	for (i = 0; i < n_channels; i++) {
1402 		channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1403 		channel_cfg[i].v1.channel_num = channels[i]->hw_value;
1404 		if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1405 			channel_cfg[i].v2.band =
1406 				channels[i]->hw_value <= IWL_SCAN_LAST_2_4_CHN ?
1407 					IWL_SCAN_BAND_2_4 : IWL_SCAN_BAND_5_2;
1408 			channel_cfg[i].v2.iter_count = 1;
1409 			channel_cfg[i].v2.iter_interval = 0;
1410 		} else {
1411 			channel_cfg[i].v1.iter_count = 1;
1412 			channel_cfg[i].v1.iter_interval = 0;
1413 		}
1414 	}
1415 }
1416 
1417 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1418 				   struct iwl_mvm_scan_params *params,
1419 				   struct ieee80211_vif *vif)
1420 {
1421 	u16 flags = 0;
1422 
1423 	if (params->n_ssids == 0)
1424 		flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1425 
1426 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1427 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1428 
1429 	if (iwl_mvm_is_scan_fragmented(params->type))
1430 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1431 
1432 	if (iwl_mvm_is_cdb_supported(mvm) &&
1433 	    iwl_mvm_is_scan_fragmented(params->hb_type))
1434 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1435 
1436 	if (iwl_mvm_rrm_scan_needed(mvm) &&
1437 	    fw_has_capa(&mvm->fw->ucode_capa,
1438 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1439 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1440 
1441 	if (params->pass_all)
1442 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1443 	else
1444 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1445 
1446 	if (!iwl_mvm_is_regular_scan(params))
1447 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1448 
1449 	if (params->measurement_dwell)
1450 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1451 
1452 #ifdef CONFIG_IWLWIFI_DEBUGFS
1453 	if (mvm->scan_iter_notif_enabled)
1454 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1455 #endif
1456 
1457 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1458 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1459 
1460 	if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE &&
1461 	    vif->type != NL80211_IFTYPE_P2P_DEVICE)
1462 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1463 
1464 	/*
1465 	 * Extended dwell is relevant only for low band to start with, as it is
1466 	 * being used for social channles only (1, 6, 11), so we can check
1467 	 * only scan type on low band also for CDB.
1468 	 */
1469 	if (iwl_mvm_is_regular_scan(params) &&
1470 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1471 	    !iwl_mvm_is_scan_fragmented(params->type) &&
1472 	    !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1473 	    !iwl_mvm_is_oce_supported(mvm))
1474 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1475 
1476 	if (iwl_mvm_is_oce_supported(mvm)) {
1477 		if ((params->flags &
1478 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1479 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1480 		/* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1481 		 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1482 		 * the same bit, we need to make sure that we use this bit here
1483 		 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1484 		 * used. */
1485 		if ((params->flags &
1486 		     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1487 		     !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1488 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1489 		if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1490 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1491 	}
1492 
1493 	return flags;
1494 }
1495 
1496 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1497 			     struct iwl_mvm_scan_params *params,
1498 			     int type)
1499 {
1500 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1501 	struct iwl_scan_umac_chan_param *chan_param;
1502 	void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
1503 	void *sec_part = cmd_data + sizeof(struct iwl_scan_channel_cfg_umac) *
1504 		mvm->fw->ucode_capa.n_scan_channels;
1505 	struct iwl_scan_req_umac_tail_v2 *tail_v2 =
1506 		(struct iwl_scan_req_umac_tail_v2 *)sec_part;
1507 	struct iwl_scan_req_umac_tail_v1 *tail_v1;
1508 	struct iwl_ssid_ie *direct_scan;
1509 	int uid, i;
1510 	u32 ssid_bitmap = 0;
1511 	u8 channel_flags = 0;
1512 	u16 gen_flags;
1513 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1514 
1515 	chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
1516 
1517 	lockdep_assert_held(&mvm->mutex);
1518 
1519 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1520 		return -EINVAL;
1521 
1522 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1523 	if (uid < 0)
1524 		return uid;
1525 
1526 	memset(cmd, 0, ksize(cmd));
1527 
1528 	iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1529 
1530 	mvm->scan_uid_status[uid] = type;
1531 
1532 	cmd->uid = cpu_to_le32(uid);
1533 	gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
1534 	cmd->general_flags = cpu_to_le16(gen_flags);
1535 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1536 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
1537 			cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
1538 							IWL_SCAN_NUM_OF_FRAGS;
1539 		if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
1540 			cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
1541 							IWL_SCAN_NUM_OF_FRAGS;
1542 
1543 		cmd->v8.general_flags2 =
1544 			IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
1545 	}
1546 
1547 	cmd->scan_start_mac_id = scan_vif->id;
1548 
1549 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1550 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1551 
1552 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1553 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1554 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1555 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1556 
1557 		/* set fragmented ebs for fragmented scan on HB channels */
1558 		if (iwl_mvm_is_frag_ebs_supported(mvm)) {
1559 			if (gen_flags &
1560 			    IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
1561 			    (!iwl_mvm_is_cdb_supported(mvm) &&
1562 			     gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
1563 				channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1564 		}
1565 	}
1566 
1567 	chan_param->flags = channel_flags;
1568 	chan_param->count = params->n_channels;
1569 
1570 	for (i = 0; i < params->n_scan_plans; i++) {
1571 		struct cfg80211_sched_scan_plan *scan_plan =
1572 			&params->scan_plans[i];
1573 
1574 		tail_v2->schedule[i].iter_count = scan_plan->iterations;
1575 		tail_v2->schedule[i].interval =
1576 			cpu_to_le16(scan_plan->interval);
1577 	}
1578 
1579 	/*
1580 	 * If the number of iterations of the last scan plan is set to
1581 	 * zero, it should run infinitely. However, this is not always the case.
1582 	 * For example, when regular scan is requested the driver sets one scan
1583 	 * plan with one iteration.
1584 	 */
1585 	if (!tail_v2->schedule[i - 1].iter_count)
1586 		tail_v2->schedule[i - 1].iter_count = 0xff;
1587 
1588 	tail_v2->delay = cpu_to_le16(params->delay);
1589 
1590 	if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1591 		tail_v2->preq = params->preq;
1592 		direct_scan = tail_v2->direct_scan;
1593 	} else {
1594 		tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
1595 		iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
1596 						  &params->preq);
1597 		direct_scan = tail_v1->direct_scan;
1598 	}
1599 	iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
1600 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1601 				       params->n_channels, ssid_bitmap,
1602 				       cmd_data);
1603 	return 0;
1604 }
1605 
1606 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1607 {
1608 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1609 }
1610 
1611 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1612 {
1613 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1614 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1615 
1616 	/* This looks a bit arbitrary, but the idea is that if we run
1617 	 * out of possible simultaneous scans and the userspace is
1618 	 * trying to run a scan type that is already running, we
1619 	 * return -EBUSY.  But if the userspace wants to start a
1620 	 * different type of scan, we stop the opposite type to make
1621 	 * space for the new request.  The reason is backwards
1622 	 * compatibility with old wpa_supplicant that wouldn't stop a
1623 	 * scheduled scan before starting a normal scan.
1624 	 */
1625 
1626 	/* FW supports only a single periodic scan */
1627 	if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
1628 	    mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
1629 		return -EBUSY;
1630 
1631 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1632 		return 0;
1633 
1634 	/* Use a switch, even though this is a bitmask, so that more
1635 	 * than one bits set will fall in default and we will warn.
1636 	 */
1637 	switch (type) {
1638 	case IWL_MVM_SCAN_REGULAR:
1639 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1640 			return -EBUSY;
1641 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1642 	case IWL_MVM_SCAN_SCHED:
1643 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1644 			return -EBUSY;
1645 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1646 	case IWL_MVM_SCAN_NETDETECT:
1647 		/* For non-unified images, there's no need to stop
1648 		 * anything for net-detect since the firmware is
1649 		 * restarted anyway.  This way, any sched scans that
1650 		 * were running will be restarted when we resume.
1651 		 */
1652 		if (!unified_image)
1653 			return 0;
1654 
1655 		/* If this is a unified image and we ran out of scans,
1656 		 * we need to stop something.  Prefer stopping regular
1657 		 * scans, because the results are useless at this
1658 		 * point, and we should be able to keep running
1659 		 * another scheduled scan while suspended.
1660 		 */
1661 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1662 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
1663 						 true);
1664 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1665 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
1666 						 true);
1667 		/* Something is wrong if no scan was running but we
1668 		 * ran out of scans.
1669 		 */
1670 		/* fall through */
1671 	default:
1672 		WARN_ON(1);
1673 		break;
1674 	}
1675 
1676 	return -EIO;
1677 }
1678 
1679 #define SCAN_TIMEOUT 20000
1680 
1681 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
1682 {
1683 	struct delayed_work *delayed_work = to_delayed_work(work);
1684 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1685 					   scan_timeout_dwork);
1686 
1687 	IWL_ERR(mvm, "regular scan timed out\n");
1688 
1689 	iwl_force_nmi(mvm->trans);
1690 }
1691 
1692 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
1693 				   struct iwl_mvm_scan_params *params,
1694 				   struct ieee80211_vif *vif)
1695 {
1696 	if (iwl_mvm_is_cdb_supported(mvm)) {
1697 		params->type =
1698 			iwl_mvm_get_scan_type_band(mvm, vif,
1699 						   NL80211_BAND_2GHZ);
1700 		params->hb_type =
1701 			iwl_mvm_get_scan_type_band(mvm, vif,
1702 						   NL80211_BAND_5GHZ);
1703 	} else {
1704 		params->type = iwl_mvm_get_scan_type(mvm, vif);
1705 	}
1706 }
1707 
1708 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1709 			   struct cfg80211_scan_request *req,
1710 			   struct ieee80211_scan_ies *ies)
1711 {
1712 	struct iwl_host_cmd hcmd = {
1713 		.len = { iwl_mvm_scan_size(mvm), },
1714 		.data = { mvm->scan_cmd, },
1715 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1716 	};
1717 	struct iwl_mvm_scan_params params = {};
1718 	int ret;
1719 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
1720 
1721 	lockdep_assert_held(&mvm->mutex);
1722 
1723 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1724 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1725 		return -EBUSY;
1726 	}
1727 
1728 	ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1729 	if (ret)
1730 		return ret;
1731 
1732 	/* we should have failed registration if scan_cmd was NULL */
1733 	if (WARN_ON(!mvm->scan_cmd))
1734 		return -ENOMEM;
1735 
1736 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1737 		return -ENOBUFS;
1738 
1739 	params.n_ssids = req->n_ssids;
1740 	params.flags = req->flags;
1741 	params.n_channels = req->n_channels;
1742 	params.delay = 0;
1743 	params.ssids = req->ssids;
1744 	params.channels = req->channels;
1745 	params.mac_addr = req->mac_addr;
1746 	params.mac_addr_mask = req->mac_addr_mask;
1747 	params.no_cck = req->no_cck;
1748 	params.pass_all = true;
1749 	params.n_match_sets = 0;
1750 	params.match_sets = NULL;
1751 
1752 	params.scan_plans = &scan_plan;
1753 	params.n_scan_plans = 1;
1754 
1755 	iwl_mvm_fill_scan_type(mvm, &params, vif);
1756 
1757 	ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
1758 	if (ret < 0)
1759 		return ret;
1760 
1761 	params.measurement_dwell = ret;
1762 
1763 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1764 
1765 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1766 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1767 		ret = iwl_mvm_scan_umac(mvm, vif, &params,
1768 					IWL_MVM_SCAN_REGULAR);
1769 	} else {
1770 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1771 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1772 	}
1773 
1774 	if (ret)
1775 		return ret;
1776 
1777 	iwl_mvm_pause_tcm(mvm, false);
1778 
1779 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1780 	if (ret) {
1781 		/* If the scan failed, it usually means that the FW was unable
1782 		 * to allocate the time events. Warn on it, but maybe we
1783 		 * should try to send the command again with different params.
1784 		 */
1785 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1786 		iwl_mvm_resume_tcm(mvm);
1787 		return ret;
1788 	}
1789 
1790 	IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1791 	mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1792 	mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
1793 
1794 	schedule_delayed_work(&mvm->scan_timeout_dwork,
1795 			      msecs_to_jiffies(SCAN_TIMEOUT));
1796 
1797 	return 0;
1798 }
1799 
1800 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1801 			     struct ieee80211_vif *vif,
1802 			     struct cfg80211_sched_scan_request *req,
1803 			     struct ieee80211_scan_ies *ies,
1804 			     int type)
1805 {
1806 	struct iwl_host_cmd hcmd = {
1807 		.len = { iwl_mvm_scan_size(mvm), },
1808 		.data = { mvm->scan_cmd, },
1809 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1810 	};
1811 	struct iwl_mvm_scan_params params = {};
1812 	int ret;
1813 
1814 	lockdep_assert_held(&mvm->mutex);
1815 
1816 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1817 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1818 		return -EBUSY;
1819 	}
1820 
1821 	ret = iwl_mvm_check_running_scans(mvm, type);
1822 	if (ret)
1823 		return ret;
1824 
1825 	/* we should have failed registration if scan_cmd was NULL */
1826 	if (WARN_ON(!mvm->scan_cmd))
1827 		return -ENOMEM;
1828 
1829 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1830 		return -ENOBUFS;
1831 
1832 	params.n_ssids = req->n_ssids;
1833 	params.flags = req->flags;
1834 	params.n_channels = req->n_channels;
1835 	params.ssids = req->ssids;
1836 	params.channels = req->channels;
1837 	params.mac_addr = req->mac_addr;
1838 	params.mac_addr_mask = req->mac_addr_mask;
1839 	params.no_cck = false;
1840 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
1841 	params.n_match_sets = req->n_match_sets;
1842 	params.match_sets = req->match_sets;
1843 	if (!req->n_scan_plans)
1844 		return -EINVAL;
1845 
1846 	params.n_scan_plans = req->n_scan_plans;
1847 	params.scan_plans = req->scan_plans;
1848 
1849 	iwl_mvm_fill_scan_type(mvm, &params, vif);
1850 
1851 	/* In theory, LMAC scans can handle a 32-bit delay, but since
1852 	 * waiting for over 18 hours to start the scan is a bit silly
1853 	 * and to keep it aligned with UMAC scans (which only support
1854 	 * 16-bit delays), trim it down to 16-bits.
1855 	 */
1856 	if (req->delay > U16_MAX) {
1857 		IWL_DEBUG_SCAN(mvm,
1858 			       "delay value is > 16-bits, set to max possible\n");
1859 		params.delay = U16_MAX;
1860 	} else {
1861 		params.delay = req->delay;
1862 	}
1863 
1864 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1865 	if (ret)
1866 		return ret;
1867 
1868 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1869 
1870 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1871 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1872 		ret = iwl_mvm_scan_umac(mvm, vif, &params, type);
1873 	} else {
1874 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1875 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1876 	}
1877 
1878 	if (ret)
1879 		return ret;
1880 
1881 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1882 	if (!ret) {
1883 		IWL_DEBUG_SCAN(mvm,
1884 			       "Sched scan request was sent successfully\n");
1885 		mvm->scan_status |= type;
1886 	} else {
1887 		/* If the scan failed, it usually means that the FW was unable
1888 		 * to allocate the time events. Warn on it, but maybe we
1889 		 * should try to send the command again with different params.
1890 		 */
1891 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1892 	}
1893 
1894 	return ret;
1895 }
1896 
1897 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1898 					 struct iwl_rx_cmd_buffer *rxb)
1899 {
1900 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1901 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1902 	u32 uid = __le32_to_cpu(notif->uid);
1903 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1904 
1905 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1906 		return;
1907 
1908 	/* if the scan is already stopping, we don't need to notify mac80211 */
1909 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1910 		struct cfg80211_scan_info info = {
1911 			.aborted = aborted,
1912 			.scan_start_tsf = mvm->scan_start,
1913 		};
1914 
1915 		memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
1916 		ieee80211_scan_completed(mvm->hw, &info);
1917 		mvm->scan_vif = NULL;
1918 		cancel_delayed_work(&mvm->scan_timeout_dwork);
1919 		iwl_mvm_resume_tcm(mvm);
1920 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1921 		ieee80211_sched_scan_stopped(mvm->hw);
1922 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1923 	}
1924 
1925 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
1926 	IWL_DEBUG_SCAN(mvm,
1927 		       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1928 		       uid, mvm->scan_uid_status[uid],
1929 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1930 				"completed" : "aborted",
1931 		       iwl_mvm_ebs_status_str(notif->ebs_status));
1932 	IWL_DEBUG_SCAN(mvm,
1933 		       "Last line %d, Last iteration %d, Time from last iteration %d\n",
1934 		       notif->last_schedule, notif->last_iter,
1935 		       __le32_to_cpu(notif->time_from_last_iter));
1936 
1937 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1938 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1939 		mvm->last_ebs_successful = false;
1940 
1941 	mvm->scan_uid_status[uid] = 0;
1942 }
1943 
1944 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1945 					      struct iwl_rx_cmd_buffer *rxb)
1946 {
1947 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1948 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1949 
1950 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
1951 
1952 	IWL_DEBUG_SCAN(mvm,
1953 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
1954 		       notif->status, notif->scanned_channels);
1955 
1956 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
1957 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
1958 		ieee80211_sched_scan_results(mvm->hw);
1959 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
1960 	}
1961 
1962 	IWL_DEBUG_SCAN(mvm,
1963 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
1964 		       mvm->scan_start);
1965 }
1966 
1967 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1968 {
1969 	struct iwl_umac_scan_abort cmd = {};
1970 	int uid, ret;
1971 
1972 	lockdep_assert_held(&mvm->mutex);
1973 
1974 	/* We should always get a valid index here, because we already
1975 	 * checked that this type of scan was running in the generic
1976 	 * code.
1977 	 */
1978 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
1979 	if (WARN_ON_ONCE(uid < 0))
1980 		return uid;
1981 
1982 	cmd.uid = cpu_to_le32(uid);
1983 
1984 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1985 
1986 	ret = iwl_mvm_send_cmd_pdu(mvm,
1987 				   iwl_cmd_id(SCAN_ABORT_UMAC,
1988 					      IWL_ALWAYS_LONG_GROUP, 0),
1989 				   0, sizeof(cmd), &cmd);
1990 	if (!ret)
1991 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1992 
1993 	return ret;
1994 }
1995 
1996 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1997 {
1998 	struct iwl_notification_wait wait_scan_done;
1999 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
2000 					      SCAN_OFFLOAD_COMPLETE, };
2001 	int ret;
2002 
2003 	lockdep_assert_held(&mvm->mutex);
2004 
2005 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2006 				   scan_done_notif,
2007 				   ARRAY_SIZE(scan_done_notif),
2008 				   NULL, NULL);
2009 
2010 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2011 
2012 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2013 		ret = iwl_mvm_umac_scan_abort(mvm, type);
2014 	else
2015 		ret = iwl_mvm_lmac_scan_abort(mvm);
2016 
2017 	if (ret) {
2018 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
2019 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2020 		return ret;
2021 	}
2022 
2023 	return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
2024 				     1 * HZ);
2025 }
2026 
2027 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2028 {
2029 	int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
2030 	int tail_size;
2031 
2032 	if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
2033 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
2034 	else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
2035 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
2036 	else if (iwl_mvm_cdb_scan_api(mvm))
2037 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
2038 
2039 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2040 		if (iwl_mvm_is_scan_ext_chan_supported(mvm))
2041 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
2042 		else
2043 			tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
2044 
2045 		return base_size +
2046 			sizeof(struct iwl_scan_channel_cfg_umac) *
2047 				mvm->fw->ucode_capa.n_scan_channels +
2048 			tail_size;
2049 	}
2050 	return sizeof(struct iwl_scan_req_lmac) +
2051 		sizeof(struct iwl_scan_channel_cfg_lmac) *
2052 		mvm->fw->ucode_capa.n_scan_channels +
2053 		sizeof(struct iwl_scan_probe_req_v1);
2054 }
2055 
2056 /*
2057  * This function is used in nic restart flow, to inform mac80211 about scans
2058  * that was aborted by restart flow or by an assert.
2059  */
2060 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
2061 {
2062 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2063 		int uid, i;
2064 
2065 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
2066 		if (uid >= 0) {
2067 			struct cfg80211_scan_info info = {
2068 				.aborted = true,
2069 			};
2070 
2071 			ieee80211_scan_completed(mvm->hw, &info);
2072 			mvm->scan_uid_status[uid] = 0;
2073 		}
2074 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
2075 		if (uid >= 0 && !mvm->fw_restart) {
2076 			ieee80211_sched_scan_stopped(mvm->hw);
2077 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2078 			mvm->scan_uid_status[uid] = 0;
2079 		}
2080 
2081 		/* We shouldn't have any UIDs still set.  Loop over all the
2082 		 * UIDs to make sure there's nothing left there and warn if
2083 		 * any is found.
2084 		 */
2085 		for (i = 0; i < mvm->max_scans; i++) {
2086 			if (WARN_ONCE(mvm->scan_uid_status[i],
2087 				      "UMAC scan UID %d status was not cleaned\n",
2088 				      i))
2089 				mvm->scan_uid_status[i] = 0;
2090 		}
2091 	} else {
2092 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
2093 			struct cfg80211_scan_info info = {
2094 				.aborted = true,
2095 			};
2096 
2097 			ieee80211_scan_completed(mvm->hw, &info);
2098 		}
2099 
2100 		/* Sched scan will be restarted by mac80211 in
2101 		 * restart_hw, so do not report if FW is about to be
2102 		 * restarted.
2103 		 */
2104 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
2105 		    !mvm->fw_restart) {
2106 			ieee80211_sched_scan_stopped(mvm->hw);
2107 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2108 		}
2109 	}
2110 }
2111 
2112 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2113 {
2114 	int ret;
2115 
2116 	if (!(mvm->scan_status & type))
2117 		return 0;
2118 
2119 	if (iwl_mvm_is_radio_killed(mvm)) {
2120 		ret = 0;
2121 		goto out;
2122 	}
2123 
2124 	ret = iwl_mvm_scan_stop_wait(mvm, type);
2125 	if (!ret)
2126 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2127 out:
2128 	/* Clear the scan status so the next scan requests will
2129 	 * succeed and mark the scan as stopping, so that the Rx
2130 	 * handler doesn't do anything, as the scan was stopped from
2131 	 * above.
2132 	 */
2133 	mvm->scan_status &= ~type;
2134 
2135 	if (type == IWL_MVM_SCAN_REGULAR) {
2136 		cancel_delayed_work(&mvm->scan_timeout_dwork);
2137 		if (notify) {
2138 			struct cfg80211_scan_info info = {
2139 				.aborted = true,
2140 			};
2141 
2142 			ieee80211_scan_completed(mvm->hw, &info);
2143 		}
2144 	} else if (notify) {
2145 		ieee80211_sched_scan_stopped(mvm->hw);
2146 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2147 	}
2148 
2149 	return ret;
2150 }
2151