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