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 #define WFA_TPC_IE_LEN	9
668 
669 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
670 {
671 	pos[0] = WLAN_EID_VENDOR_SPECIFIC;
672 	pos[1] = WFA_TPC_IE_LEN - 2;
673 	pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
674 	pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
675 	pos[4] = WLAN_OUI_MICROSOFT & 0xff;
676 	pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
677 	pos[6] = 0;
678 	/* pos[7] - tx power will be inserted by the FW */
679 	pos[7] = 0;
680 	pos[8] = 0;
681 }
682 
683 static void
684 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
685 			 struct ieee80211_scan_ies *ies,
686 			 struct iwl_mvm_scan_params *params)
687 {
688 	struct ieee80211_mgmt *frame = (void *)params->preq.buf;
689 	u8 *pos, *newpos;
690 	const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
691 		params->mac_addr : NULL;
692 
693 	/*
694 	 * Unfortunately, right now the offload scan doesn't support randomising
695 	 * within the firmware, so until the firmware API is ready we implement
696 	 * it in the driver. This means that the scan iterations won't really be
697 	 * random, only when it's restarted, but at least that helps a bit.
698 	 */
699 	if (mac_addr)
700 		get_random_mask_addr(frame->sa, mac_addr,
701 				     params->mac_addr_mask);
702 	else
703 		memcpy(frame->sa, vif->addr, ETH_ALEN);
704 
705 	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
706 	eth_broadcast_addr(frame->da);
707 	eth_broadcast_addr(frame->bssid);
708 	frame->seq_ctrl = 0;
709 
710 	pos = frame->u.probe_req.variable;
711 	*pos++ = WLAN_EID_SSID;
712 	*pos++ = 0;
713 
714 	params->preq.mac_header.offset = 0;
715 	params->preq.mac_header.len = cpu_to_le16(24 + 2);
716 
717 	/* Insert ds parameter set element on 2.4 GHz band */
718 	newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
719 						 ies->ies[NL80211_BAND_2GHZ],
720 						 ies->len[NL80211_BAND_2GHZ],
721 						 pos);
722 	params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
723 	params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
724 	pos = newpos;
725 
726 	memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
727 	       ies->len[NL80211_BAND_5GHZ]);
728 	params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
729 	params->preq.band_data[1].len =
730 		cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
731 	pos += ies->len[NL80211_BAND_5GHZ];
732 
733 	memcpy(pos, ies->common_ies, ies->common_ie_len);
734 	params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
735 
736 	if (iwl_mvm_rrm_scan_needed(mvm) &&
737 	    !fw_has_capa(&mvm->fw->ucode_capa,
738 			 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
739 		iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
740 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
741 							   WFA_TPC_IE_LEN);
742 	} else {
743 		params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
744 	}
745 }
746 
747 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
748 				    struct iwl_scan_req_lmac *cmd,
749 				    struct iwl_mvm_scan_params *params)
750 {
751 	cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
752 	cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
753 	cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
754 	cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
755 	cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
756 	cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
757 	cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
758 }
759 
760 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
761 				     struct ieee80211_scan_ies *ies,
762 				     int n_channels)
763 {
764 	return ((n_ssids <= PROBE_OPTION_MAX) &&
765 		(n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
766 		(ies->common_ie_len +
767 		 ies->len[NL80211_BAND_2GHZ] +
768 		 ies->len[NL80211_BAND_5GHZ] <=
769 		 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
770 }
771 
772 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
773 					struct ieee80211_vif *vif)
774 {
775 	const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
776 
777 	/* We can only use EBS if:
778 	 *	1. the feature is supported;
779 	 *	2. the last EBS was successful;
780 	 *	3. if only single scan, the single scan EBS API is supported;
781 	 *	4. it's not a p2p find operation.
782 	 */
783 	return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
784 		mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
785 		vif->type != NL80211_IFTYPE_P2P_DEVICE);
786 }
787 
788 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
789 {
790 	return params->n_scan_plans == 1 &&
791 		params->scan_plans[0].iterations == 1;
792 }
793 
794 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
795 				   struct iwl_mvm_scan_params *params,
796 				   struct ieee80211_vif *vif)
797 {
798 	int flags = 0;
799 
800 	if (params->n_ssids == 0)
801 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
802 
803 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
804 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
805 
806 	if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
807 		flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
808 
809 	if (iwl_mvm_rrm_scan_needed(mvm) &&
810 	    fw_has_capa(&mvm->fw->ucode_capa,
811 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
812 		flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
813 
814 	if (params->pass_all)
815 		flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
816 	else
817 		flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
818 
819 #ifdef CONFIG_IWLWIFI_DEBUGFS
820 	if (mvm->scan_iter_notif_enabled)
821 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
822 #endif
823 
824 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
825 		flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
826 
827 	if (iwl_mvm_is_regular_scan(params) &&
828 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
829 	    params->type != IWL_SCAN_TYPE_FRAGMENTED)
830 		flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
831 
832 	return flags;
833 }
834 
835 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
836 			     struct iwl_mvm_scan_params *params)
837 {
838 	struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
839 	struct iwl_scan_probe_req *preq =
840 		(void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
841 			 mvm->fw->ucode_capa.n_scan_channels);
842 	u32 ssid_bitmap = 0;
843 	int i;
844 
845 	lockdep_assert_held(&mvm->mutex);
846 
847 	memset(cmd, 0, ksize(cmd));
848 
849 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
850 		return -EINVAL;
851 
852 	iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
853 
854 	cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
855 	cmd->iter_num = cpu_to_le32(1);
856 	cmd->n_channels = (u8)params->n_channels;
857 
858 	cmd->delay = cpu_to_le32(params->delay);
859 
860 	cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
861 							      vif));
862 
863 	cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
864 	cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
865 					MAC_FILTER_IN_BEACON);
866 	iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
867 	iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
868 
869 	/* this API uses bits 1-20 instead of 0-19 */
870 	ssid_bitmap <<= 1;
871 
872 	for (i = 0; i < params->n_scan_plans; i++) {
873 		struct cfg80211_sched_scan_plan *scan_plan =
874 			&params->scan_plans[i];
875 
876 		cmd->schedule[i].delay =
877 			cpu_to_le16(scan_plan->interval);
878 		cmd->schedule[i].iterations = scan_plan->iterations;
879 		cmd->schedule[i].full_scan_mul = 1;
880 	}
881 
882 	/*
883 	 * If the number of iterations of the last scan plan is set to
884 	 * zero, it should run infinitely. However, this is not always the case.
885 	 * For example, when regular scan is requested the driver sets one scan
886 	 * plan with one iteration.
887 	 */
888 	if (!cmd->schedule[i - 1].iterations)
889 		cmd->schedule[i - 1].iterations = 0xff;
890 
891 	if (iwl_mvm_scan_use_ebs(mvm, vif)) {
892 		cmd->channel_opt[0].flags =
893 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
894 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
895 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
896 		cmd->channel_opt[0].non_ebs_ratio =
897 			cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
898 		cmd->channel_opt[1].flags =
899 			cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
900 				    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
901 				    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
902 		cmd->channel_opt[1].non_ebs_ratio =
903 			cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
904 	}
905 
906 	iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
907 				       params->n_channels, ssid_bitmap, cmd);
908 
909 	*preq = params->preq;
910 
911 	return 0;
912 }
913 
914 static int rate_to_scan_rate_flag(unsigned int rate)
915 {
916 	static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
917 		[IWL_RATE_1M_INDEX]	= SCAN_CONFIG_RATE_1M,
918 		[IWL_RATE_2M_INDEX]	= SCAN_CONFIG_RATE_2M,
919 		[IWL_RATE_5M_INDEX]	= SCAN_CONFIG_RATE_5M,
920 		[IWL_RATE_11M_INDEX]	= SCAN_CONFIG_RATE_11M,
921 		[IWL_RATE_6M_INDEX]	= SCAN_CONFIG_RATE_6M,
922 		[IWL_RATE_9M_INDEX]	= SCAN_CONFIG_RATE_9M,
923 		[IWL_RATE_12M_INDEX]	= SCAN_CONFIG_RATE_12M,
924 		[IWL_RATE_18M_INDEX]	= SCAN_CONFIG_RATE_18M,
925 		[IWL_RATE_24M_INDEX]	= SCAN_CONFIG_RATE_24M,
926 		[IWL_RATE_36M_INDEX]	= SCAN_CONFIG_RATE_36M,
927 		[IWL_RATE_48M_INDEX]	= SCAN_CONFIG_RATE_48M,
928 		[IWL_RATE_54M_INDEX]	= SCAN_CONFIG_RATE_54M,
929 	};
930 
931 	return rate_to_scan_rate[rate];
932 }
933 
934 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
935 {
936 	struct ieee80211_supported_band *band;
937 	unsigned int rates = 0;
938 	int i;
939 
940 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
941 	for (i = 0; i < band->n_bitrates; i++)
942 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
943 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
944 	for (i = 0; i < band->n_bitrates; i++)
945 		rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
946 
947 	/* Set both basic rates and supported rates */
948 	rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
949 
950 	return cpu_to_le32(rates);
951 }
952 
953 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
954 				    struct iwl_scan_dwell *dwell)
955 {
956 	dwell->active = IWL_SCAN_DWELL_ACTIVE;
957 	dwell->passive = IWL_SCAN_DWELL_PASSIVE;
958 	dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
959 	dwell->extended = IWL_SCAN_DWELL_EXTENDED;
960 }
961 
962 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels)
963 {
964 	struct ieee80211_supported_band *band;
965 	int i, j = 0;
966 
967 	band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
968 	for (i = 0; i < band->n_channels; i++, j++)
969 		channels[j] = band->channels[i].hw_value;
970 	band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
971 	for (i = 0; i < band->n_channels; i++, j++)
972 		channels[j] = band->channels[i].hw_value;
973 }
974 
975 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
976 					u32 flags, u8 channel_flags)
977 {
978 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
979 	struct iwl_scan_config_v1 *cfg = config;
980 
981 	cfg->flags = cpu_to_le32(flags);
982 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
983 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
984 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
985 	cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
986 	cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
987 
988 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
989 
990 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
991 
992 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
993 	cfg->channel_flags = channel_flags;
994 
995 	iwl_mvm_fill_channels(mvm, cfg->channel_array);
996 }
997 
998 static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
999 				     u32 flags, u8 channel_flags)
1000 {
1001 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
1002 	struct iwl_scan_config *cfg = config;
1003 
1004 	cfg->flags = cpu_to_le32(flags);
1005 	cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1006 	cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1007 	cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1008 	cfg->out_of_channel_time[0] =
1009 		cpu_to_le32(scan_timing[type].max_out_time);
1010 	cfg->suspend_time[0] = cpu_to_le32(scan_timing[type].suspend_time);
1011 
1012 	if (iwl_mvm_is_cdb_supported(mvm)) {
1013 		cfg->suspend_time[1] =
1014 			cpu_to_le32(scan_timing[type].suspend_time);
1015 		cfg->out_of_channel_time[1] =
1016 			cpu_to_le32(scan_timing[type].max_out_time);
1017 	}
1018 
1019 	iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1020 
1021 	memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1022 
1023 	cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1024 	cfg->channel_flags = channel_flags;
1025 
1026 	iwl_mvm_fill_channels(mvm, cfg->channel_array);
1027 }
1028 
1029 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1030 {
1031 	void *cfg;
1032 	int ret, cmd_size;
1033 	struct iwl_host_cmd cmd = {
1034 		.id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1035 	};
1036 	enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
1037 	int num_channels =
1038 		mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1039 		mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1040 	u32 flags;
1041 	u8 channel_flags;
1042 
1043 	if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1044 		return -ENOBUFS;
1045 
1046 	if (type == mvm->scan_type)
1047 		return 0;
1048 
1049 	if (iwl_mvm_has_new_tx_api(mvm))
1050 		cmd_size = sizeof(struct iwl_scan_config);
1051 	else
1052 		cmd_size = sizeof(struct iwl_scan_config_v1);
1053 	cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1054 
1055 	cfg = kzalloc(cmd_size, GFP_KERNEL);
1056 	if (!cfg)
1057 		return -ENOMEM;
1058 
1059 	flags = SCAN_CONFIG_FLAG_ACTIVATE |
1060 		 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1061 		 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1062 		 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1063 		 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1064 		 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1065 		 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1066 		 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1067 		 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1068 		 SCAN_CONFIG_N_CHANNELS(num_channels) |
1069 		 (type == IWL_SCAN_TYPE_FRAGMENTED ?
1070 		  SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1071 		  SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1072 
1073 	channel_flags = IWL_CHANNEL_FLAG_EBS |
1074 			IWL_CHANNEL_FLAG_ACCURATE_EBS |
1075 			IWL_CHANNEL_FLAG_EBS_ADD |
1076 			IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1077 
1078 	if (iwl_mvm_has_new_tx_api(mvm)) {
1079 		flags |= (type == IWL_SCAN_TYPE_FRAGMENTED) ?
1080 			 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1081 			 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1082 		iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags);
1083 	} else {
1084 		iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags);
1085 	}
1086 
1087 	cmd.data[0] = cfg;
1088 	cmd.len[0] = cmd_size;
1089 	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1090 
1091 	IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1092 
1093 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1094 	if (!ret)
1095 		mvm->scan_type = type;
1096 
1097 	kfree(cfg);
1098 	return ret;
1099 }
1100 
1101 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1102 {
1103 	int i;
1104 
1105 	for (i = 0; i < mvm->max_scans; i++)
1106 		if (mvm->scan_uid_status[i] == status)
1107 			return i;
1108 
1109 	return -ENOENT;
1110 }
1111 
1112 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1113 				    struct iwl_scan_req_umac *cmd,
1114 				    struct iwl_mvm_scan_params *params)
1115 {
1116 	struct iwl_mvm_scan_timing_params *timing = &scan_timing[params->type];
1117 
1118 	if (iwl_mvm_is_regular_scan(params))
1119 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1120 	else
1121 		cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1122 
1123 	if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1124 		if (params->measurement_dwell) {
1125 			cmd->v7.active_dwell = params->measurement_dwell;
1126 			cmd->v7.passive_dwell = params->measurement_dwell;
1127 		} else {
1128 			cmd->v7.active_dwell = IWL_SCAN_DWELL_ACTIVE;
1129 			cmd->v7.passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1130 		}
1131 		cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1132 
1133 		cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1134 		cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1135 			cpu_to_le32(timing->max_out_time);
1136 		cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1137 			cpu_to_le32(timing->suspend_time);
1138 		if (iwl_mvm_is_cdb_supported(mvm)) {
1139 			cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1140 				cpu_to_le32(timing->max_out_time);
1141 			cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1142 				cpu_to_le32(timing->suspend_time);
1143 		}
1144 
1145 		return;
1146 	}
1147 
1148 	if (params->measurement_dwell) {
1149 		cmd->v1.active_dwell = params->measurement_dwell;
1150 		cmd->v1.passive_dwell = params->measurement_dwell;
1151 		cmd->v1.extended_dwell = params->measurement_dwell;
1152 	} else {
1153 		cmd->v1.active_dwell = IWL_SCAN_DWELL_ACTIVE;
1154 		cmd->v1.passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1155 		cmd->v1.extended_dwell = IWL_SCAN_DWELL_EXTENDED;
1156 	}
1157 	cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1158 
1159 	if (iwl_mvm_has_new_tx_api(mvm)) {
1160 		cmd->v6.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1161 		cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1162 			cpu_to_le32(timing->max_out_time);
1163 		cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1164 			cpu_to_le32(timing->suspend_time);
1165 		if (iwl_mvm_is_cdb_supported(mvm)) {
1166 			cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1167 				cpu_to_le32(timing->max_out_time);
1168 			cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1169 				cpu_to_le32(timing->suspend_time);
1170 		}
1171 	} else {
1172 		cmd->v1.max_out_time = cpu_to_le32(timing->max_out_time);
1173 		cmd->v1.suspend_time = cpu_to_le32(timing->suspend_time);
1174 		cmd->v1.scan_priority =
1175 			cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1176 	}
1177 }
1178 
1179 static void
1180 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1181 			       struct ieee80211_channel **channels,
1182 			       int n_channels, u32 ssid_bitmap,
1183 			       struct iwl_scan_channel_cfg_umac *channel_cfg)
1184 {
1185 	int i;
1186 
1187 	for (i = 0; i < n_channels; i++) {
1188 		channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1189 		channel_cfg[i].channel_num = channels[i]->hw_value;
1190 		channel_cfg[i].iter_count = 1;
1191 		channel_cfg[i].iter_interval = 0;
1192 	}
1193 }
1194 
1195 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1196 				   struct iwl_mvm_scan_params *params,
1197 				   struct ieee80211_vif *vif)
1198 {
1199 	u16 flags = 0;
1200 
1201 	if (params->n_ssids == 0)
1202 		flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1203 
1204 	if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1205 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1206 
1207 	if (params->type == IWL_SCAN_TYPE_FRAGMENTED) {
1208 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1209 		if (iwl_mvm_is_cdb_supported(mvm))
1210 			flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1211 	}
1212 
1213 	if (iwl_mvm_rrm_scan_needed(mvm) &&
1214 	    fw_has_capa(&mvm->fw->ucode_capa,
1215 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1216 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1217 
1218 	if (params->pass_all)
1219 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1220 	else
1221 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1222 
1223 	if (!iwl_mvm_is_regular_scan(params))
1224 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1225 
1226 	if (params->measurement_dwell)
1227 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1228 
1229 #ifdef CONFIG_IWLWIFI_DEBUGFS
1230 	if (mvm->scan_iter_notif_enabled)
1231 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1232 #endif
1233 
1234 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1235 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1236 
1237 	if (iwl_mvm_is_regular_scan(params) &&
1238 	    vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1239 	    params->type != IWL_SCAN_TYPE_FRAGMENTED)
1240 		flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1241 
1242 	return flags;
1243 }
1244 
1245 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1246 			     struct iwl_mvm_scan_params *params,
1247 			     int type)
1248 {
1249 	struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1250 	void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
1251 	struct iwl_scan_req_umac_tail *sec_part = cmd_data +
1252 		sizeof(struct iwl_scan_channel_cfg_umac) *
1253 			mvm->fw->ucode_capa.n_scan_channels;
1254 	int uid, i;
1255 	u32 ssid_bitmap = 0;
1256 	u8 channel_flags = 0;
1257 	struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1258 
1259 	lockdep_assert_held(&mvm->mutex);
1260 
1261 	if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1262 		return -EINVAL;
1263 
1264 	uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1265 	if (uid < 0)
1266 		return uid;
1267 
1268 	memset(cmd, 0, ksize(cmd));
1269 
1270 	iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1271 
1272 	mvm->scan_uid_status[uid] = type;
1273 
1274 	cmd->uid = cpu_to_le32(uid);
1275 	cmd->general_flags = cpu_to_le16(iwl_mvm_scan_umac_flags(mvm, params,
1276 								 vif));
1277 	cmd->scan_start_mac_id = scan_vif->id;
1278 
1279 	if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1280 		cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1281 
1282 	if (iwl_mvm_scan_use_ebs(mvm, vif))
1283 		channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1284 				IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1285 				IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1286 
1287 	if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1288 		cmd->v7.channel_flags = channel_flags;
1289 		cmd->v7.n_channels = params->n_channels;
1290 	} else if (iwl_mvm_has_new_tx_api(mvm)) {
1291 		cmd->v6.channel_flags = channel_flags;
1292 		cmd->v6.n_channels = params->n_channels;
1293 	} else {
1294 		cmd->v1.channel_flags = channel_flags;
1295 		cmd->v1.n_channels = params->n_channels;
1296 	}
1297 
1298 	iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1299 
1300 	iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1301 				       params->n_channels, ssid_bitmap,
1302 				       cmd_data);
1303 
1304 	for (i = 0; i < params->n_scan_plans; i++) {
1305 		struct cfg80211_sched_scan_plan *scan_plan =
1306 			&params->scan_plans[i];
1307 
1308 		sec_part->schedule[i].iter_count = scan_plan->iterations;
1309 		sec_part->schedule[i].interval =
1310 			cpu_to_le16(scan_plan->interval);
1311 	}
1312 
1313 	/*
1314 	 * If the number of iterations of the last scan plan is set to
1315 	 * zero, it should run infinitely. However, this is not always the case.
1316 	 * For example, when regular scan is requested the driver sets one scan
1317 	 * plan with one iteration.
1318 	 */
1319 	if (!sec_part->schedule[i - 1].iter_count)
1320 		sec_part->schedule[i - 1].iter_count = 0xff;
1321 
1322 	sec_part->delay = cpu_to_le16(params->delay);
1323 	sec_part->preq = params->preq;
1324 
1325 	return 0;
1326 }
1327 
1328 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1329 {
1330 	return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1331 }
1332 
1333 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1334 {
1335 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1336 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1337 
1338 	/* This looks a bit arbitrary, but the idea is that if we run
1339 	 * out of possible simultaneous scans and the userspace is
1340 	 * trying to run a scan type that is already running, we
1341 	 * return -EBUSY.  But if the userspace wants to start a
1342 	 * different type of scan, we stop the opposite type to make
1343 	 * space for the new request.  The reason is backwards
1344 	 * compatibility with old wpa_supplicant that wouldn't stop a
1345 	 * scheduled scan before starting a normal scan.
1346 	 */
1347 
1348 	if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1349 		return 0;
1350 
1351 	/* Use a switch, even though this is a bitmask, so that more
1352 	 * than one bits set will fall in default and we will warn.
1353 	 */
1354 	switch (type) {
1355 	case IWL_MVM_SCAN_REGULAR:
1356 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1357 			return -EBUSY;
1358 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1359 	case IWL_MVM_SCAN_SCHED:
1360 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1361 			return -EBUSY;
1362 		return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1363 	case IWL_MVM_SCAN_NETDETECT:
1364 		/* For non-unified images, there's no need to stop
1365 		 * anything for net-detect since the firmware is
1366 		 * restarted anyway.  This way, any sched scans that
1367 		 * were running will be restarted when we resume.
1368 		 */
1369 		if (!unified_image)
1370 			return 0;
1371 
1372 		/* If this is a unified image and we ran out of scans,
1373 		 * we need to stop something.  Prefer stopping regular
1374 		 * scans, because the results are useless at this
1375 		 * point, and we should be able to keep running
1376 		 * another scheduled scan while suspended.
1377 		 */
1378 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1379 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
1380 						 true);
1381 		if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1382 			return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
1383 						 true);
1384 
1385 		/* fall through, something is wrong if no scan was
1386 		 * running but we ran out of scans.
1387 		 */
1388 	default:
1389 		WARN_ON(1);
1390 		break;
1391 	}
1392 
1393 	return -EIO;
1394 }
1395 
1396 #define SCAN_TIMEOUT 20000
1397 
1398 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
1399 {
1400 	struct delayed_work *delayed_work = to_delayed_work(work);
1401 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1402 					   scan_timeout_dwork);
1403 
1404 	IWL_ERR(mvm, "regular scan timed out\n");
1405 
1406 	iwl_force_nmi(mvm->trans);
1407 }
1408 
1409 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1410 			   struct cfg80211_scan_request *req,
1411 			   struct ieee80211_scan_ies *ies)
1412 {
1413 	struct iwl_host_cmd hcmd = {
1414 		.len = { iwl_mvm_scan_size(mvm), },
1415 		.data = { mvm->scan_cmd, },
1416 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1417 	};
1418 	struct iwl_mvm_scan_params params = {};
1419 	int ret;
1420 	struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
1421 
1422 	lockdep_assert_held(&mvm->mutex);
1423 
1424 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1425 		IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1426 		return -EBUSY;
1427 	}
1428 
1429 	ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1430 	if (ret)
1431 		return ret;
1432 
1433 	/* we should have failed registration if scan_cmd was NULL */
1434 	if (WARN_ON(!mvm->scan_cmd))
1435 		return -ENOMEM;
1436 
1437 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1438 		return -ENOBUFS;
1439 
1440 	params.n_ssids = req->n_ssids;
1441 	params.flags = req->flags;
1442 	params.n_channels = req->n_channels;
1443 	params.delay = 0;
1444 	params.ssids = req->ssids;
1445 	params.channels = req->channels;
1446 	params.mac_addr = req->mac_addr;
1447 	params.mac_addr_mask = req->mac_addr_mask;
1448 	params.no_cck = req->no_cck;
1449 	params.pass_all = true;
1450 	params.n_match_sets = 0;
1451 	params.match_sets = NULL;
1452 
1453 	params.scan_plans = &scan_plan;
1454 	params.n_scan_plans = 1;
1455 
1456 	params.type =
1457 		iwl_mvm_get_scan_type(mvm,
1458 				      vif->type == NL80211_IFTYPE_P2P_DEVICE);
1459 
1460 	ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
1461 	if (ret < 0)
1462 		return ret;
1463 
1464 	params.measurement_dwell = ret;
1465 
1466 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1467 
1468 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1469 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1470 		ret = iwl_mvm_scan_umac(mvm, vif, &params,
1471 					IWL_MVM_SCAN_REGULAR);
1472 	} else {
1473 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1474 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1475 	}
1476 
1477 	if (ret)
1478 		return ret;
1479 
1480 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1481 	if (ret) {
1482 		/* If the scan failed, it usually means that the FW was unable
1483 		 * to allocate the time events. Warn on it, but maybe we
1484 		 * should try to send the command again with different params.
1485 		 */
1486 		IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1487 		return ret;
1488 	}
1489 
1490 	IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1491 	mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1492 	mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
1493 	iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1494 
1495 	schedule_delayed_work(&mvm->scan_timeout_dwork,
1496 			      msecs_to_jiffies(SCAN_TIMEOUT));
1497 
1498 	return 0;
1499 }
1500 
1501 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1502 			     struct ieee80211_vif *vif,
1503 			     struct cfg80211_sched_scan_request *req,
1504 			     struct ieee80211_scan_ies *ies,
1505 			     int type)
1506 {
1507 	struct iwl_host_cmd hcmd = {
1508 		.len = { iwl_mvm_scan_size(mvm), },
1509 		.data = { mvm->scan_cmd, },
1510 		.dataflags = { IWL_HCMD_DFL_NOCOPY, },
1511 	};
1512 	struct iwl_mvm_scan_params params = {};
1513 	int ret;
1514 
1515 	lockdep_assert_held(&mvm->mutex);
1516 
1517 	if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1518 		IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1519 		return -EBUSY;
1520 	}
1521 
1522 	ret = iwl_mvm_check_running_scans(mvm, type);
1523 	if (ret)
1524 		return ret;
1525 
1526 	/* we should have failed registration if scan_cmd was NULL */
1527 	if (WARN_ON(!mvm->scan_cmd))
1528 		return -ENOMEM;
1529 
1530 	if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1531 		return -ENOBUFS;
1532 
1533 	params.n_ssids = req->n_ssids;
1534 	params.flags = req->flags;
1535 	params.n_channels = req->n_channels;
1536 	params.ssids = req->ssids;
1537 	params.channels = req->channels;
1538 	params.mac_addr = req->mac_addr;
1539 	params.mac_addr_mask = req->mac_addr_mask;
1540 	params.no_cck = false;
1541 	params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
1542 	params.n_match_sets = req->n_match_sets;
1543 	params.match_sets = req->match_sets;
1544 	if (!req->n_scan_plans)
1545 		return -EINVAL;
1546 
1547 	params.n_scan_plans = req->n_scan_plans;
1548 	params.scan_plans = req->scan_plans;
1549 
1550 	params.type =
1551 		iwl_mvm_get_scan_type(mvm,
1552 				      vif->type == NL80211_IFTYPE_P2P_DEVICE);
1553 
1554 	/* In theory, LMAC scans can handle a 32-bit delay, but since
1555 	 * waiting for over 18 hours to start the scan is a bit silly
1556 	 * and to keep it aligned with UMAC scans (which only support
1557 	 * 16-bit delays), trim it down to 16-bits.
1558 	 */
1559 	if (req->delay > U16_MAX) {
1560 		IWL_DEBUG_SCAN(mvm,
1561 			       "delay value is > 16-bits, set to max possible\n");
1562 		params.delay = U16_MAX;
1563 	} else {
1564 		params.delay = req->delay;
1565 	}
1566 
1567 	ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1568 	if (ret)
1569 		return ret;
1570 
1571 	iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1572 
1573 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1574 		hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1575 		ret = iwl_mvm_scan_umac(mvm, vif, &params, type);
1576 	} else {
1577 		hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1578 		ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1579 	}
1580 
1581 	if (ret)
1582 		return ret;
1583 
1584 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1585 	if (!ret) {
1586 		IWL_DEBUG_SCAN(mvm,
1587 			       "Sched scan request was sent successfully\n");
1588 		mvm->scan_status |= type;
1589 	} else {
1590 		/* If the scan failed, it usually means that the FW was unable
1591 		 * to allocate the time events. Warn on it, but maybe we
1592 		 * should try to send the command again with different params.
1593 		 */
1594 		IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1595 	}
1596 
1597 	return ret;
1598 }
1599 
1600 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1601 					 struct iwl_rx_cmd_buffer *rxb)
1602 {
1603 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1604 	struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1605 	u32 uid = __le32_to_cpu(notif->uid);
1606 	bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1607 
1608 	if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1609 		return;
1610 
1611 	/* if the scan is already stopping, we don't need to notify mac80211 */
1612 	if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1613 		struct cfg80211_scan_info info = {
1614 			.aborted = aborted,
1615 			.scan_start_tsf = mvm->scan_start,
1616 		};
1617 
1618 		memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
1619 		ieee80211_scan_completed(mvm->hw, &info);
1620 		mvm->scan_vif = NULL;
1621 		iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1622 		cancel_delayed_work(&mvm->scan_timeout_dwork);
1623 	} else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1624 		ieee80211_sched_scan_stopped(mvm->hw);
1625 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1626 	}
1627 
1628 	mvm->scan_status &= ~mvm->scan_uid_status[uid];
1629 	IWL_DEBUG_SCAN(mvm,
1630 		       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1631 		       uid, mvm->scan_uid_status[uid],
1632 		       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1633 				"completed" : "aborted",
1634 		       iwl_mvm_ebs_status_str(notif->ebs_status));
1635 	IWL_DEBUG_SCAN(mvm,
1636 		       "Last line %d, Last iteration %d, Time from last iteration %d\n",
1637 		       notif->last_schedule, notif->last_iter,
1638 		       __le32_to_cpu(notif->time_from_last_iter));
1639 
1640 	if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1641 	    notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1642 		mvm->last_ebs_successful = false;
1643 
1644 	mvm->scan_uid_status[uid] = 0;
1645 }
1646 
1647 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1648 					      struct iwl_rx_cmd_buffer *rxb)
1649 {
1650 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1651 	struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1652 
1653 	mvm->scan_start = le64_to_cpu(notif->start_tsf);
1654 
1655 	IWL_DEBUG_SCAN(mvm,
1656 		       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
1657 		       notif->status, notif->scanned_channels);
1658 
1659 	if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
1660 		IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
1661 		ieee80211_sched_scan_results(mvm->hw);
1662 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
1663 	}
1664 
1665 	IWL_DEBUG_SCAN(mvm,
1666 		       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
1667 		       mvm->scan_start);
1668 }
1669 
1670 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1671 {
1672 	struct iwl_umac_scan_abort cmd = {};
1673 	int uid, ret;
1674 
1675 	lockdep_assert_held(&mvm->mutex);
1676 
1677 	/* We should always get a valid index here, because we already
1678 	 * checked that this type of scan was running in the generic
1679 	 * code.
1680 	 */
1681 	uid = iwl_mvm_scan_uid_by_status(mvm, type);
1682 	if (WARN_ON_ONCE(uid < 0))
1683 		return uid;
1684 
1685 	cmd.uid = cpu_to_le32(uid);
1686 
1687 	IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1688 
1689 	ret = iwl_mvm_send_cmd_pdu(mvm,
1690 				   iwl_cmd_id(SCAN_ABORT_UMAC,
1691 					      IWL_ALWAYS_LONG_GROUP, 0),
1692 				   0, sizeof(cmd), &cmd);
1693 	if (!ret)
1694 		mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1695 
1696 	return ret;
1697 }
1698 
1699 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1700 {
1701 	struct iwl_notification_wait wait_scan_done;
1702 	static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1703 					      SCAN_OFFLOAD_COMPLETE, };
1704 	int ret;
1705 
1706 	lockdep_assert_held(&mvm->mutex);
1707 
1708 	iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1709 				   scan_done_notif,
1710 				   ARRAY_SIZE(scan_done_notif),
1711 				   NULL, NULL);
1712 
1713 	IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1714 
1715 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1716 		ret = iwl_mvm_umac_scan_abort(mvm, type);
1717 	else
1718 		ret = iwl_mvm_lmac_scan_abort(mvm);
1719 
1720 	if (ret) {
1721 		IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1722 		iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1723 		return ret;
1724 	}
1725 
1726 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1727 
1728 	return ret;
1729 }
1730 
1731 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1732 {
1733 	int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
1734 
1735 	if (iwl_mvm_is_adaptive_dwell_supported(mvm))
1736 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
1737 	else if (iwl_mvm_has_new_tx_api(mvm))
1738 		base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
1739 
1740 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1741 		return base_size +
1742 			sizeof(struct iwl_scan_channel_cfg_umac) *
1743 				mvm->fw->ucode_capa.n_scan_channels +
1744 			sizeof(struct iwl_scan_req_umac_tail);
1745 
1746 	return sizeof(struct iwl_scan_req_lmac) +
1747 		sizeof(struct iwl_scan_channel_cfg_lmac) *
1748 		mvm->fw->ucode_capa.n_scan_channels +
1749 		sizeof(struct iwl_scan_probe_req);
1750 }
1751 
1752 /*
1753  * This function is used in nic restart flow, to inform mac80211 about scans
1754  * that was aborted by restart flow or by an assert.
1755  */
1756 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1757 {
1758 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1759 		int uid, i;
1760 
1761 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
1762 		if (uid >= 0) {
1763 			struct cfg80211_scan_info info = {
1764 				.aborted = true,
1765 			};
1766 
1767 			ieee80211_scan_completed(mvm->hw, &info);
1768 			mvm->scan_uid_status[uid] = 0;
1769 		}
1770 		uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
1771 		if (uid >= 0 && !mvm->fw_restart) {
1772 			ieee80211_sched_scan_stopped(mvm->hw);
1773 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1774 			mvm->scan_uid_status[uid] = 0;
1775 		}
1776 
1777 		/* We shouldn't have any UIDs still set.  Loop over all the
1778 		 * UIDs to make sure there's nothing left there and warn if
1779 		 * any is found.
1780 		 */
1781 		for (i = 0; i < mvm->max_scans; i++) {
1782 			if (WARN_ONCE(mvm->scan_uid_status[i],
1783 				      "UMAC scan UID %d status was not cleaned\n",
1784 				      i))
1785 				mvm->scan_uid_status[i] = 0;
1786 		}
1787 	} else {
1788 		if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
1789 			struct cfg80211_scan_info info = {
1790 				.aborted = true,
1791 			};
1792 
1793 			ieee80211_scan_completed(mvm->hw, &info);
1794 		}
1795 
1796 		/* Sched scan will be restarted by mac80211 in
1797 		 * restart_hw, so do not report if FW is about to be
1798 		 * restarted.
1799 		 */
1800 		if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
1801 		    !mvm->fw_restart) {
1802 			ieee80211_sched_scan_stopped(mvm->hw);
1803 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1804 		}
1805 	}
1806 }
1807 
1808 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
1809 {
1810 	int ret;
1811 
1812 	if (!(mvm->scan_status & type))
1813 		return 0;
1814 
1815 	if (iwl_mvm_is_radio_killed(mvm)) {
1816 		ret = 0;
1817 		goto out;
1818 	}
1819 
1820 	ret = iwl_mvm_scan_stop_wait(mvm, type);
1821 	if (!ret)
1822 		mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
1823 out:
1824 	/* Clear the scan status so the next scan requests will
1825 	 * succeed and mark the scan as stopping, so that the Rx
1826 	 * handler doesn't do anything, as the scan was stopped from
1827 	 * above.
1828 	 */
1829 	mvm->scan_status &= ~type;
1830 
1831 	if (type == IWL_MVM_SCAN_REGULAR) {
1832 		/* Since the rx handler won't do anything now, we have
1833 		 * to release the scan reference here.
1834 		 */
1835 		iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1836 		cancel_delayed_work(&mvm->scan_timeout_dwork);
1837 		if (notify) {
1838 			struct cfg80211_scan_info info = {
1839 				.aborted = true,
1840 			};
1841 
1842 			ieee80211_scan_completed(mvm->hw, &info);
1843 		}
1844 	} else if (notify) {
1845 		ieee80211_sched_scan_stopped(mvm->hw);
1846 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1847 	}
1848 
1849 	return ret;
1850 }
1851