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