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