iwlwifi: mvm: Fix fall-through warnings for Clang
[linux-2.6-microblaze.git] / drivers / net / wireless / intel / iwlwifi / mvm / scan.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <net/mac80211.h>
9 #include <linux/crc32.h>
10
11 #include "mvm.h"
12 #include "fw/api/scan.h"
13 #include "iwl-io.h"
14
15 #define IWL_DENSE_EBS_SCAN_RATIO 5
16 #define IWL_SPARSE_EBS_SCAN_RATIO 1
17
18 #define IWL_SCAN_DWELL_ACTIVE           10
19 #define IWL_SCAN_DWELL_PASSIVE          110
20 #define IWL_SCAN_DWELL_FRAGMENTED       44
21 #define IWL_SCAN_DWELL_EXTENDED         90
22 #define IWL_SCAN_NUM_OF_FRAGS           3
23 #define IWL_SCAN_LAST_2_4_CHN           14
24
25 /* adaptive dwell max budget time [TU] for full scan */
26 #define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
27 /* adaptive dwell max budget time [TU] for directed scan */
28 #define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
29 /* adaptive dwell default high band APs number */
30 #define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8
31 /* adaptive dwell default low band APs number */
32 #define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2
33 /* adaptive dwell default APs number in social channels (1, 6, 11) */
34 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
35 /* number of scan channels */
36 #define IWL_SCAN_NUM_CHANNELS 112
37 /* adaptive dwell number of APs override mask for p2p friendly GO */
38 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT BIT(20)
39 /* adaptive dwell number of APs override mask for social channels */
40 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT BIT(21)
41 /* adaptive dwell number of APs override for p2p friendly GO channels */
42 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY 10
43 /* adaptive dwell number of APs override for social channels */
44 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS 2
45
46 struct iwl_mvm_scan_timing_params {
47         u32 suspend_time;
48         u32 max_out_time;
49 };
50
51 static struct iwl_mvm_scan_timing_params scan_timing[] = {
52         [IWL_SCAN_TYPE_UNASSOC] = {
53                 .suspend_time = 0,
54                 .max_out_time = 0,
55         },
56         [IWL_SCAN_TYPE_WILD] = {
57                 .suspend_time = 30,
58                 .max_out_time = 120,
59         },
60         [IWL_SCAN_TYPE_MILD] = {
61                 .suspend_time = 120,
62                 .max_out_time = 120,
63         },
64         [IWL_SCAN_TYPE_FRAGMENTED] = {
65                 .suspend_time = 95,
66                 .max_out_time = 44,
67         },
68         [IWL_SCAN_TYPE_FAST_BALANCE] = {
69                 .suspend_time = 30,
70                 .max_out_time = 37,
71         },
72 };
73
74 struct iwl_mvm_scan_params {
75         /* For CDB this is low band scan type, for non-CDB - type. */
76         enum iwl_mvm_scan_type type;
77         enum iwl_mvm_scan_type hb_type;
78         u32 n_channels;
79         u16 delay;
80         int n_ssids;
81         struct cfg80211_ssid *ssids;
82         struct ieee80211_channel **channels;
83         u32 flags;
84         u8 *mac_addr;
85         u8 *mac_addr_mask;
86         bool no_cck;
87         bool pass_all;
88         int n_match_sets;
89         struct iwl_scan_probe_req preq;
90         struct cfg80211_match_set *match_sets;
91         int n_scan_plans;
92         struct cfg80211_sched_scan_plan *scan_plans;
93         bool iter_notif;
94         struct cfg80211_scan_6ghz_params *scan_6ghz_params;
95         u32 n_6ghz_params;
96         bool scan_6ghz;
97 };
98
99 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
100 {
101         struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
102
103         if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
104                 return (void *)&cmd->v8.data;
105
106         if (iwl_mvm_is_adaptive_dwell_supported(mvm))
107                 return (void *)&cmd->v7.data;
108
109         if (iwl_mvm_cdb_scan_api(mvm))
110                 return (void *)&cmd->v6.data;
111
112         return (void *)&cmd->v1.data;
113 }
114
115 static inline struct iwl_scan_umac_chan_param *
116 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
117 {
118         struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
119
120         if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
121                 return &cmd->v8.channel;
122
123         if (iwl_mvm_is_adaptive_dwell_supported(mvm))
124                 return &cmd->v7.channel;
125
126         if (iwl_mvm_cdb_scan_api(mvm))
127                 return &cmd->v6.channel;
128
129         return &cmd->v1.channel;
130 }
131
132 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
133 {
134         if (mvm->scan_rx_ant != ANT_NONE)
135                 return mvm->scan_rx_ant;
136         return iwl_mvm_get_valid_rx_ant(mvm);
137 }
138
139 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
140 {
141         u16 rx_chain;
142         u8 rx_ant;
143
144         rx_ant = iwl_mvm_scan_rx_ant(mvm);
145         rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
146         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
147         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
148         rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
149         return cpu_to_le16(rx_chain);
150 }
151
152 static inline __le32
153 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
154                           bool no_cck)
155 {
156         u32 tx_ant;
157
158         iwl_mvm_toggle_tx_ant(mvm, &mvm->scan_last_antenna_idx);
159         tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
160
161         if (band == NL80211_BAND_2GHZ && !no_cck)
162                 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
163                                    tx_ant);
164         else
165                 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
166 }
167
168 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
169                                             struct ieee80211_vif *vif)
170 {
171         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
172         int *global_cnt = data;
173
174         if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
175             mvmvif->phy_ctxt->id < NUM_PHY_CTX)
176                 *global_cnt += 1;
177 }
178
179 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
180 {
181         return mvm->tcm.result.global_load;
182 }
183
184 static enum iwl_mvm_traffic_load
185 iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
186 {
187         return mvm->tcm.result.band_load[band];
188 }
189
190 struct iwl_is_dcm_with_go_iterator_data {
191         struct ieee80211_vif *current_vif;
192         bool is_dcm_with_p2p_go;
193 };
194
195 static void iwl_mvm_is_dcm_with_go_iterator(void *_data, u8 *mac,
196                                             struct ieee80211_vif *vif)
197 {
198         struct iwl_is_dcm_with_go_iterator_data *data = _data;
199         struct iwl_mvm_vif *other_mvmvif = iwl_mvm_vif_from_mac80211(vif);
200         struct iwl_mvm_vif *curr_mvmvif =
201                 iwl_mvm_vif_from_mac80211(data->current_vif);
202
203         /* exclude the given vif */
204         if (vif == data->current_vif)
205                 return;
206
207         if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
208             other_mvmvif->phy_ctxt && curr_mvmvif->phy_ctxt &&
209             other_mvmvif->phy_ctxt->id != curr_mvmvif->phy_ctxt->id)
210                 data->is_dcm_with_p2p_go = true;
211 }
212
213 static enum
214 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
215                                          struct ieee80211_vif *vif,
216                                          enum iwl_mvm_traffic_load load,
217                                          bool low_latency)
218 {
219         int global_cnt = 0;
220
221         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
222                                             IEEE80211_IFACE_ITER_NORMAL,
223                                             iwl_mvm_scan_condition_iterator,
224                                             &global_cnt);
225         if (!global_cnt)
226                 return IWL_SCAN_TYPE_UNASSOC;
227
228         if (fw_has_api(&mvm->fw->ucode_capa,
229                        IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
230                 if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
231                     (!vif || vif->type != NL80211_IFTYPE_P2P_DEVICE))
232                         return IWL_SCAN_TYPE_FRAGMENTED;
233
234                 /* in case of DCM with GO where BSS DTIM interval < 220msec
235                  * set all scan requests as fast-balance scan
236                  * */
237                 if (vif && vif->type == NL80211_IFTYPE_STATION &&
238                     vif->bss_conf.dtim_period < 220) {
239                         struct iwl_is_dcm_with_go_iterator_data data = {
240                                 .current_vif = vif,
241                                 .is_dcm_with_p2p_go = false,
242                         };
243
244                         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
245                                                 IEEE80211_IFACE_ITER_NORMAL,
246                                                 iwl_mvm_is_dcm_with_go_iterator,
247                                                 &data);
248                         if (data.is_dcm_with_p2p_go)
249                                 return IWL_SCAN_TYPE_FAST_BALANCE;
250                 }
251         }
252
253         if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
254                 return IWL_SCAN_TYPE_MILD;
255
256         return IWL_SCAN_TYPE_WILD;
257 }
258
259 static enum
260 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
261                                         struct ieee80211_vif *vif)
262 {
263         enum iwl_mvm_traffic_load load;
264         bool low_latency;
265
266         load = iwl_mvm_get_traffic_load(mvm);
267         low_latency = iwl_mvm_low_latency(mvm);
268
269         return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
270 }
271
272 static enum
273 iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
274                                              struct ieee80211_vif *vif,
275                                              enum nl80211_band band)
276 {
277         enum iwl_mvm_traffic_load load;
278         bool low_latency;
279
280         load = iwl_mvm_get_traffic_load_band(mvm, band);
281         low_latency = iwl_mvm_low_latency_band(mvm, band);
282
283         return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
284 }
285
286 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
287 {
288         /* require rrm scan whenever the fw supports it */
289         return fw_has_capa(&mvm->fw->ucode_capa,
290                            IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
291 }
292
293 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
294 {
295         int max_probe_len;
296
297         max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
298
299         /* we create the 802.11 header and SSID element */
300         max_probe_len -= 24 + 2;
301
302         /* DS parameter set element is added on 2.4GHZ band if required */
303         if (iwl_mvm_rrm_scan_needed(mvm))
304                 max_probe_len -= 3;
305
306         return max_probe_len;
307 }
308
309 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
310 {
311         int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
312
313         /* TODO: [BUG] This function should return the maximum allowed size of
314          * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
315          * in the same command. So the correct implementation of this function
316          * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
317          * command has only 512 bytes and it would leave us with about 240
318          * bytes for scan IEs, which is clearly not enough. So meanwhile
319          * we will report an incorrect value. This may result in a failure to
320          * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
321          * functions with -ENOBUFS, if a large enough probe will be provided.
322          */
323         return max_ie_len;
324 }
325
326 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
327                                               struct iwl_rx_cmd_buffer *rxb)
328 {
329         struct iwl_rx_packet *pkt = rxb_addr(rxb);
330         struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
331
332         IWL_DEBUG_SCAN(mvm,
333                        "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
334                        notif->status, notif->scanned_channels);
335
336         if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
337                 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
338                 ieee80211_sched_scan_results(mvm->hw);
339                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
340         }
341 }
342
343 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
344                                  struct iwl_rx_cmd_buffer *rxb)
345 {
346         IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
347         ieee80211_sched_scan_results(mvm->hw);
348 }
349
350 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
351 {
352         switch (status) {
353         case IWL_SCAN_EBS_SUCCESS:
354                 return "successful";
355         case IWL_SCAN_EBS_INACTIVE:
356                 return "inactive";
357         case IWL_SCAN_EBS_FAILED:
358         case IWL_SCAN_EBS_CHAN_NOT_FOUND:
359         default:
360                 return "failed";
361         }
362 }
363
364 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
365                                          struct iwl_rx_cmd_buffer *rxb)
366 {
367         struct iwl_rx_packet *pkt = rxb_addr(rxb);
368         struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
369         bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
370
371         /* If this happens, the firmware has mistakenly sent an LMAC
372          * notification during UMAC scans -- warn and ignore it.
373          */
374         if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
375                                      IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
376                 return;
377
378         /* scan status must be locked for proper checking */
379         lockdep_assert_held(&mvm->mutex);
380
381         /* We first check if we were stopping a scan, in which case we
382          * just clear the stopping flag.  Then we check if it was a
383          * firmware initiated stop, in which case we need to inform
384          * mac80211.
385          * Note that we can have a stopping and a running scan
386          * simultaneously, but we can't have two different types of
387          * scans stopping or running at the same time (since LMAC
388          * doesn't support it).
389          */
390
391         if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
392                 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
393
394                 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
395                                aborted ? "aborted" : "completed",
396                                iwl_mvm_ebs_status_str(scan_notif->ebs_status));
397                 IWL_DEBUG_SCAN(mvm,
398                                "Last line %d, Last iteration %d, Time after last iteration %d\n",
399                                scan_notif->last_schedule_line,
400                                scan_notif->last_schedule_iteration,
401                                __le32_to_cpu(scan_notif->time_after_last_iter));
402
403                 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
404         } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
405                 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
406                                aborted ? "aborted" : "completed",
407                                iwl_mvm_ebs_status_str(scan_notif->ebs_status));
408
409                 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
410         } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
411                 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
412
413                 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
414                                aborted ? "aborted" : "completed",
415                                iwl_mvm_ebs_status_str(scan_notif->ebs_status));
416                 IWL_DEBUG_SCAN(mvm,
417                                "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
418                                scan_notif->last_schedule_line,
419                                scan_notif->last_schedule_iteration,
420                                __le32_to_cpu(scan_notif->time_after_last_iter));
421
422                 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
423                 ieee80211_sched_scan_stopped(mvm->hw);
424                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
425         } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
426                 struct cfg80211_scan_info info = {
427                         .aborted = aborted,
428                 };
429
430                 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
431                                aborted ? "aborted" : "completed",
432                                iwl_mvm_ebs_status_str(scan_notif->ebs_status));
433
434                 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
435                 ieee80211_scan_completed(mvm->hw, &info);
436                 cancel_delayed_work(&mvm->scan_timeout_dwork);
437                 iwl_mvm_resume_tcm(mvm);
438         } else {
439                 IWL_ERR(mvm,
440                         "got scan complete notification but no scan is running\n");
441         }
442
443         mvm->last_ebs_successful =
444                         scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
445                         scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
446 }
447
448 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
449 {
450         int i;
451
452         for (i = 0; i < PROBE_OPTION_MAX; i++) {
453                 if (!ssid_list[i].len)
454                         break;
455                 if (ssid_list[i].len == ssid_len &&
456                     !memcmp(ssid_list->ssid, ssid, ssid_len))
457                         return i;
458         }
459         return -1;
460 }
461
462 /* We insert the SSIDs in an inverted order, because the FW will
463  * invert it back.
464  */
465 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
466                                  struct iwl_ssid_ie *ssids,
467                                  u32 *ssid_bitmap)
468 {
469         int i, j;
470         int index;
471         u32 tmp_bitmap = 0;
472
473         /*
474          * copy SSIDs from match list.
475          * iwl_config_sched_scan_profiles() uses the order of these ssids to
476          * config match list.
477          */
478         for (i = 0, j = params->n_match_sets - 1;
479              j >= 0 && i < PROBE_OPTION_MAX;
480              i++, j--) {
481                 /* skip empty SSID matchsets */
482                 if (!params->match_sets[j].ssid.ssid_len)
483                         continue;
484                 ssids[i].id = WLAN_EID_SSID;
485                 ssids[i].len = params->match_sets[j].ssid.ssid_len;
486                 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
487                        ssids[i].len);
488         }
489
490         /* add SSIDs from scan SSID list */
491         for (j = params->n_ssids - 1;
492              j >= 0 && i < PROBE_OPTION_MAX;
493              i++, j--) {
494                 index = iwl_ssid_exist(params->ssids[j].ssid,
495                                        params->ssids[j].ssid_len,
496                                        ssids);
497                 if (index < 0) {
498                         ssids[i].id = WLAN_EID_SSID;
499                         ssids[i].len = params->ssids[j].ssid_len;
500                         memcpy(ssids[i].ssid, params->ssids[j].ssid,
501                                ssids[i].len);
502                         tmp_bitmap |= BIT(i);
503                 } else {
504                         tmp_bitmap |= BIT(index);
505                 }
506         }
507         if (ssid_bitmap)
508                 *ssid_bitmap = tmp_bitmap;
509 }
510
511 static int
512 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
513                                    struct cfg80211_sched_scan_request *req)
514 {
515         struct iwl_scan_offload_profile *profile;
516         struct iwl_scan_offload_profile_cfg_v1 *profile_cfg_v1;
517         struct iwl_scan_offload_blocklist *blocklist;
518         struct iwl_scan_offload_profile_cfg_data *data;
519         int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
520         int profile_cfg_size = sizeof(*data) +
521                 sizeof(*profile) * max_profiles;
522         struct iwl_host_cmd cmd = {
523                 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
524                 .len[1] = profile_cfg_size,
525                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
526                 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
527         };
528         int blocklist_len;
529         int i;
530         int ret;
531
532         if (WARN_ON(req->n_match_sets > max_profiles))
533                 return -EIO;
534
535         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
536                 blocklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
537         else
538                 blocklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
539
540         blocklist = kcalloc(blocklist_len, sizeof(*blocklist), GFP_KERNEL);
541         if (!blocklist)
542                 return -ENOMEM;
543
544         profile_cfg_v1 = kzalloc(profile_cfg_size, GFP_KERNEL);
545         if (!profile_cfg_v1) {
546                 ret = -ENOMEM;
547                 goto free_blocklist;
548         }
549
550         cmd.data[0] = blocklist;
551         cmd.len[0] = sizeof(*blocklist) * blocklist_len;
552         cmd.data[1] = profile_cfg_v1;
553
554         /* if max_profile is MAX_PROFILES_V2, we have the new API */
555         if (max_profiles == IWL_SCAN_MAX_PROFILES_V2) {
556                 struct iwl_scan_offload_profile_cfg *profile_cfg =
557                         (struct iwl_scan_offload_profile_cfg *)profile_cfg_v1;
558
559                 data = &profile_cfg->data;
560         } else {
561                 data = &profile_cfg_v1->data;
562         }
563
564         /* No blocklist configuration */
565         data->num_profiles = req->n_match_sets;
566         data->active_clients = SCAN_CLIENT_SCHED_SCAN;
567         data->pass_match = SCAN_CLIENT_SCHED_SCAN;
568         data->match_notify = SCAN_CLIENT_SCHED_SCAN;
569
570         if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
571                 data->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
572
573         for (i = 0; i < req->n_match_sets; i++) {
574                 profile = &profile_cfg_v1->profiles[i];
575                 profile->ssid_index = i;
576                 /* Support any cipher and auth algorithm */
577                 profile->unicast_cipher = 0xff;
578                 profile->auth_alg = 0xff;
579                 profile->network_type = IWL_NETWORK_TYPE_ANY;
580                 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
581                 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
582         }
583
584         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
585
586         ret = iwl_mvm_send_cmd(mvm, &cmd);
587         kfree(profile_cfg_v1);
588 free_blocklist:
589         kfree(blocklist);
590
591         return ret;
592 }
593
594 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
595                                   struct cfg80211_sched_scan_request *req)
596 {
597         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
598                 IWL_DEBUG_SCAN(mvm,
599                                "Sending scheduled scan with filtering, n_match_sets %d\n",
600                                req->n_match_sets);
601                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
602                 return false;
603         }
604
605         IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
606
607         mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
608         return true;
609 }
610
611 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
612 {
613         int ret;
614         struct iwl_host_cmd cmd = {
615                 .id = SCAN_OFFLOAD_ABORT_CMD,
616         };
617         u32 status = CAN_ABORT_STATUS;
618
619         ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
620         if (ret)
621                 return ret;
622
623         if (status != CAN_ABORT_STATUS) {
624                 /*
625                  * The scan abort will return 1 for success or
626                  * 2 for "failure".  A failure condition can be
627                  * due to simply not being in an active scan which
628                  * can occur if we send the scan abort before the
629                  * microcode has notified us that a scan is completed.
630                  */
631                 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
632                 ret = -ENOENT;
633         }
634
635         return ret;
636 }
637
638 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
639                                      struct iwl_scan_req_tx_cmd *tx_cmd,
640                                      bool no_cck)
641 {
642         tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
643                                          TX_CMD_FLG_BT_DIS);
644         tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
645                                                            NL80211_BAND_2GHZ,
646                                                            no_cck);
647
648         if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
649                                   ADD_STA,
650                                   0) < 12) {
651                 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
652                 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
653
654         /*
655          * Fw doesn't use this sta anymore, pending deprecation via HOST API
656          * change
657          */
658         } else {
659                 tx_cmd[0].sta_id = 0xff;
660                 tx_cmd[1].sta_id = 0xff;
661         }
662
663         tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
664                                          TX_CMD_FLG_BT_DIS);
665
666         tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
667                                                            NL80211_BAND_5GHZ,
668                                                            no_cck);
669 }
670
671 static void
672 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
673                                struct ieee80211_channel **channels,
674                                int n_channels, u32 ssid_bitmap,
675                                struct iwl_scan_req_lmac *cmd)
676 {
677         struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
678         int i;
679
680         for (i = 0; i < n_channels; i++) {
681                 channel_cfg[i].channel_num =
682                         cpu_to_le16(channels[i]->hw_value);
683                 channel_cfg[i].iter_count = cpu_to_le16(1);
684                 channel_cfg[i].iter_interval = 0;
685                 channel_cfg[i].flags =
686                         cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
687                                     ssid_bitmap);
688         }
689 }
690
691 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
692                                            size_t len, u8 *const pos)
693 {
694         static const u8 before_ds_params[] = {
695                         WLAN_EID_SSID,
696                         WLAN_EID_SUPP_RATES,
697                         WLAN_EID_REQUEST,
698                         WLAN_EID_EXT_SUPP_RATES,
699         };
700         size_t offs;
701         u8 *newpos = pos;
702
703         if (!iwl_mvm_rrm_scan_needed(mvm)) {
704                 memcpy(newpos, ies, len);
705                 return newpos + len;
706         }
707
708         offs = ieee80211_ie_split(ies, len,
709                                   before_ds_params,
710                                   ARRAY_SIZE(before_ds_params),
711                                   0);
712
713         memcpy(newpos, ies, offs);
714         newpos += offs;
715
716         /* Add a placeholder for DS Parameter Set element */
717         *newpos++ = WLAN_EID_DS_PARAMS;
718         *newpos++ = 1;
719         *newpos++ = 0;
720
721         memcpy(newpos, ies + offs, len - offs);
722         newpos += len - offs;
723
724         return newpos;
725 }
726
727 #define WFA_TPC_IE_LEN  9
728
729 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
730 {
731         pos[0] = WLAN_EID_VENDOR_SPECIFIC;
732         pos[1] = WFA_TPC_IE_LEN - 2;
733         pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
734         pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
735         pos[4] = WLAN_OUI_MICROSOFT & 0xff;
736         pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
737         pos[6] = 0;
738         /* pos[7] - tx power will be inserted by the FW */
739         pos[7] = 0;
740         pos[8] = 0;
741 }
742
743 static void
744 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
745                          struct ieee80211_scan_ies *ies,
746                          struct iwl_mvm_scan_params *params)
747 {
748         struct ieee80211_mgmt *frame = (void *)params->preq.buf;
749         u8 *pos, *newpos;
750         const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
751                 params->mac_addr : NULL;
752
753         /*
754          * Unfortunately, right now the offload scan doesn't support randomising
755          * within the firmware, so until the firmware API is ready we implement
756          * it in the driver. This means that the scan iterations won't really be
757          * random, only when it's restarted, but at least that helps a bit.
758          */
759         if (mac_addr)
760                 get_random_mask_addr(frame->sa, mac_addr,
761                                      params->mac_addr_mask);
762         else
763                 memcpy(frame->sa, vif->addr, ETH_ALEN);
764
765         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
766         eth_broadcast_addr(frame->da);
767         eth_broadcast_addr(frame->bssid);
768         frame->seq_ctrl = 0;
769
770         pos = frame->u.probe_req.variable;
771         *pos++ = WLAN_EID_SSID;
772         *pos++ = 0;
773
774         params->preq.mac_header.offset = 0;
775         params->preq.mac_header.len = cpu_to_le16(24 + 2);
776
777         /* Insert ds parameter set element on 2.4 GHz band */
778         newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
779                                                  ies->ies[NL80211_BAND_2GHZ],
780                                                  ies->len[NL80211_BAND_2GHZ],
781                                                  pos);
782         params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
783         params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
784         pos = newpos;
785
786         memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
787                ies->len[NL80211_BAND_5GHZ]);
788         params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
789         params->preq.band_data[1].len =
790                 cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
791         pos += ies->len[NL80211_BAND_5GHZ];
792
793         memcpy(pos, ies->ies[NL80211_BAND_6GHZ],
794                ies->len[NL80211_BAND_6GHZ]);
795         params->preq.band_data[2].offset = cpu_to_le16(pos - params->preq.buf);
796         params->preq.band_data[2].len =
797                 cpu_to_le16(ies->len[NL80211_BAND_6GHZ]);
798         pos += ies->len[NL80211_BAND_6GHZ];
799         memcpy(pos, ies->common_ies, ies->common_ie_len);
800         params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
801
802         if (iwl_mvm_rrm_scan_needed(mvm) &&
803             !fw_has_capa(&mvm->fw->ucode_capa,
804                          IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
805                 iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
806                 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
807                                                            WFA_TPC_IE_LEN);
808         } else {
809                 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
810         }
811 }
812
813 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
814                                     struct iwl_scan_req_lmac *cmd,
815                                     struct iwl_mvm_scan_params *params)
816 {
817         cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
818         cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
819         cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
820         cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
821         cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
822         cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
823         cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
824 }
825
826 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
827                                      struct ieee80211_scan_ies *ies,
828                                      int n_channels)
829 {
830         return ((n_ssids <= PROBE_OPTION_MAX) &&
831                 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
832                 (ies->common_ie_len +
833                  ies->len[NL80211_BAND_2GHZ] +
834                  ies->len[NL80211_BAND_5GHZ] <=
835                  iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
836 }
837
838 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
839                                         struct ieee80211_vif *vif)
840 {
841         const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
842         bool low_latency;
843
844         if (iwl_mvm_is_cdb_supported(mvm))
845                 low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
846         else
847                 low_latency = iwl_mvm_low_latency(mvm);
848
849         /* We can only use EBS if:
850          *      1. the feature is supported;
851          *      2. the last EBS was successful;
852          *      3. if only single scan, the single scan EBS API is supported;
853          *      4. it's not a p2p find operation.
854          *      5. we are not in low latency mode,
855          *         or if fragmented ebs is supported by the FW
856          */
857         return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
858                 mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
859                 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
860                 (!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
861 }
862
863 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
864 {
865         return params->n_scan_plans == 1 &&
866                 params->scan_plans[0].iterations == 1;
867 }
868
869 static bool iwl_mvm_is_scan_fragmented(enum iwl_mvm_scan_type type)
870 {
871         return (type == IWL_SCAN_TYPE_FRAGMENTED ||
872                 type == IWL_SCAN_TYPE_FAST_BALANCE);
873 }
874
875 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
876                                    struct iwl_mvm_scan_params *params,
877                                    struct ieee80211_vif *vif)
878 {
879         int flags = 0;
880
881         if (params->n_ssids == 0)
882                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
883
884         if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
885                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
886
887         if (iwl_mvm_is_scan_fragmented(params->type))
888                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
889
890         if (iwl_mvm_rrm_scan_needed(mvm) &&
891             fw_has_capa(&mvm->fw->ucode_capa,
892                         IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
893                 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
894
895         if (params->pass_all)
896                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
897         else
898                 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
899
900 #ifdef CONFIG_IWLWIFI_DEBUGFS
901         if (mvm->scan_iter_notif_enabled)
902                 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
903 #endif
904
905         if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
906                 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
907
908         if (iwl_mvm_is_regular_scan(params) &&
909             vif->type != NL80211_IFTYPE_P2P_DEVICE &&
910             !iwl_mvm_is_scan_fragmented(params->type))
911                 flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
912
913         return flags;
914 }
915
916 static void
917 iwl_mvm_scan_set_legacy_probe_req(struct iwl_scan_probe_req_v1 *p_req,
918                                   struct iwl_scan_probe_req *src_p_req)
919 {
920         int i;
921
922         p_req->mac_header = src_p_req->mac_header;
923         for (i = 0; i < SCAN_NUM_BAND_PROBE_DATA_V_1; i++)
924                 p_req->band_data[i] = src_p_req->band_data[i];
925         p_req->common_data = src_p_req->common_data;
926         memcpy(p_req->buf, src_p_req->buf, sizeof(p_req->buf));
927 }
928
929 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
930                              struct iwl_mvm_scan_params *params)
931 {
932         struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
933         struct iwl_scan_probe_req_v1 *preq =
934                 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
935                          mvm->fw->ucode_capa.n_scan_channels);
936         u32 ssid_bitmap = 0;
937         int i;
938         u8 band;
939
940         if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
941                 return -EINVAL;
942
943         iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
944
945         cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
946         cmd->iter_num = cpu_to_le32(1);
947         cmd->n_channels = (u8)params->n_channels;
948
949         cmd->delay = cpu_to_le32(params->delay);
950
951         cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
952                                                               vif));
953
954         band = iwl_mvm_phy_band_from_nl80211(params->channels[0]->band);
955         cmd->flags = cpu_to_le32(band);
956         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
957                                         MAC_FILTER_IN_BEACON);
958         iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
959         iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
960
961         /* this API uses bits 1-20 instead of 0-19 */
962         ssid_bitmap <<= 1;
963
964         for (i = 0; i < params->n_scan_plans; i++) {
965                 struct cfg80211_sched_scan_plan *scan_plan =
966                         &params->scan_plans[i];
967
968                 cmd->schedule[i].delay =
969                         cpu_to_le16(scan_plan->interval);
970                 cmd->schedule[i].iterations = scan_plan->iterations;
971                 cmd->schedule[i].full_scan_mul = 1;
972         }
973
974         /*
975          * If the number of iterations of the last scan plan is set to
976          * zero, it should run infinitely. However, this is not always the case.
977          * For example, when regular scan is requested the driver sets one scan
978          * plan with one iteration.
979          */
980         if (!cmd->schedule[i - 1].iterations)
981                 cmd->schedule[i - 1].iterations = 0xff;
982
983         if (iwl_mvm_scan_use_ebs(mvm, vif)) {
984                 cmd->channel_opt[0].flags =
985                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
986                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
987                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
988                 cmd->channel_opt[0].non_ebs_ratio =
989                         cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
990                 cmd->channel_opt[1].flags =
991                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
992                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
993                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
994                 cmd->channel_opt[1].non_ebs_ratio =
995                         cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
996         }
997
998         iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
999                                        params->n_channels, ssid_bitmap, cmd);
1000
1001         iwl_mvm_scan_set_legacy_probe_req(preq, &params->preq);
1002
1003         return 0;
1004 }
1005
1006 static int rate_to_scan_rate_flag(unsigned int rate)
1007 {
1008         static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1009                 [IWL_RATE_1M_INDEX]     = SCAN_CONFIG_RATE_1M,
1010                 [IWL_RATE_2M_INDEX]     = SCAN_CONFIG_RATE_2M,
1011                 [IWL_RATE_5M_INDEX]     = SCAN_CONFIG_RATE_5M,
1012                 [IWL_RATE_11M_INDEX]    = SCAN_CONFIG_RATE_11M,
1013                 [IWL_RATE_6M_INDEX]     = SCAN_CONFIG_RATE_6M,
1014                 [IWL_RATE_9M_INDEX]     = SCAN_CONFIG_RATE_9M,
1015                 [IWL_RATE_12M_INDEX]    = SCAN_CONFIG_RATE_12M,
1016                 [IWL_RATE_18M_INDEX]    = SCAN_CONFIG_RATE_18M,
1017                 [IWL_RATE_24M_INDEX]    = SCAN_CONFIG_RATE_24M,
1018                 [IWL_RATE_36M_INDEX]    = SCAN_CONFIG_RATE_36M,
1019                 [IWL_RATE_48M_INDEX]    = SCAN_CONFIG_RATE_48M,
1020                 [IWL_RATE_54M_INDEX]    = SCAN_CONFIG_RATE_54M,
1021         };
1022
1023         return rate_to_scan_rate[rate];
1024 }
1025
1026 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1027 {
1028         struct ieee80211_supported_band *band;
1029         unsigned int rates = 0;
1030         int i;
1031
1032         band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1033         for (i = 0; i < band->n_bitrates; i++)
1034                 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1035         band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1036         for (i = 0; i < band->n_bitrates; i++)
1037                 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1038
1039         /* Set both basic rates and supported rates */
1040         rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1041
1042         return cpu_to_le32(rates);
1043 }
1044
1045 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1046                                     struct iwl_scan_dwell *dwell)
1047 {
1048         dwell->active = IWL_SCAN_DWELL_ACTIVE;
1049         dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1050         dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1051         dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1052 }
1053
1054 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels,
1055                                   u32 max_channels)
1056 {
1057         struct ieee80211_supported_band *band;
1058         int i, j = 0;
1059
1060         band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1061         for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1062                 channels[j] = band->channels[i].hw_value;
1063         band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1064         for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1065                 channels[j] = band->channels[i].hw_value;
1066 }
1067
1068 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1069                                         u32 flags, u8 channel_flags,
1070                                         u32 max_channels)
1071 {
1072         enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, NULL);
1073         struct iwl_scan_config_v1 *cfg = config;
1074
1075         cfg->flags = cpu_to_le32(flags);
1076         cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1077         cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1078         cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1079         cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1080         cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1081
1082         iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1083
1084         memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1085
1086         /* This function should not be called when using ADD_STA ver >=12 */
1087         WARN_ON_ONCE(iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1088                                            ADD_STA, 0) >= 12);
1089
1090         cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1091         cfg->channel_flags = channel_flags;
1092
1093         iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1094 }
1095
1096 static void iwl_mvm_fill_scan_config_v2(struct iwl_mvm *mvm, void *config,
1097                                         u32 flags, u8 channel_flags,
1098                                         u32 max_channels)
1099 {
1100         struct iwl_scan_config_v2 *cfg = config;
1101
1102         cfg->flags = cpu_to_le32(flags);
1103         cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1104         cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1105         cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1106
1107         if (iwl_mvm_is_cdb_supported(mvm)) {
1108                 enum iwl_mvm_scan_type lb_type, hb_type;
1109
1110                 lb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1111                                                      NL80211_BAND_2GHZ);
1112                 hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1113                                                      NL80211_BAND_5GHZ);
1114
1115                 cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1116                         cpu_to_le32(scan_timing[lb_type].max_out_time);
1117                 cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1118                         cpu_to_le32(scan_timing[lb_type].suspend_time);
1119
1120                 cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1121                         cpu_to_le32(scan_timing[hb_type].max_out_time);
1122                 cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1123                         cpu_to_le32(scan_timing[hb_type].suspend_time);
1124         } else {
1125                 enum iwl_mvm_scan_type type =
1126                         iwl_mvm_get_scan_type(mvm, NULL);
1127
1128                 cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1129                         cpu_to_le32(scan_timing[type].max_out_time);
1130                 cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1131                         cpu_to_le32(scan_timing[type].suspend_time);
1132         }
1133
1134         iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1135
1136         memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1137
1138         /* This function should not be called when using ADD_STA ver >=12 */
1139         WARN_ON_ONCE(iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1140                                            ADD_STA, 0) >= 12);
1141
1142         cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1143         cfg->channel_flags = channel_flags;
1144
1145         iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1146 }
1147
1148 static int iwl_mvm_legacy_config_scan(struct iwl_mvm *mvm)
1149 {
1150         void *cfg;
1151         int ret, cmd_size;
1152         struct iwl_host_cmd cmd = {
1153                 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1154         };
1155         enum iwl_mvm_scan_type type;
1156         enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1157         int num_channels =
1158                 mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1159                 mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1160         u32 flags;
1161         u8 channel_flags;
1162
1163         if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1164                 num_channels = mvm->fw->ucode_capa.n_scan_channels;
1165
1166         if (iwl_mvm_is_cdb_supported(mvm)) {
1167                 type = iwl_mvm_get_scan_type_band(mvm, NULL,
1168                                                   NL80211_BAND_2GHZ);
1169                 hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1170                                                      NL80211_BAND_5GHZ);
1171                 if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1172                         return 0;
1173         } else {
1174                 type = iwl_mvm_get_scan_type(mvm, NULL);
1175                 if (type == mvm->scan_type)
1176                         return 0;
1177         }
1178
1179         if (iwl_mvm_cdb_scan_api(mvm))
1180                 cmd_size = sizeof(struct iwl_scan_config_v2);
1181         else
1182                 cmd_size = sizeof(struct iwl_scan_config_v1);
1183         cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1184
1185         cfg = kzalloc(cmd_size, GFP_KERNEL);
1186         if (!cfg)
1187                 return -ENOMEM;
1188
1189         flags = SCAN_CONFIG_FLAG_ACTIVATE |
1190                  SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1191                  SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1192                  SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1193                  SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1194                  SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1195                  SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1196                  SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1197                  SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1198                  SCAN_CONFIG_N_CHANNELS(num_channels) |
1199                  (iwl_mvm_is_scan_fragmented(type) ?
1200                   SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1201                   SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1202
1203         channel_flags = IWL_CHANNEL_FLAG_EBS |
1204                         IWL_CHANNEL_FLAG_ACCURATE_EBS |
1205                         IWL_CHANNEL_FLAG_EBS_ADD |
1206                         IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1207
1208         /*
1209          * Check for fragmented scan on LMAC2 - high band.
1210          * LMAC1 - low band is checked above.
1211          */
1212         if (iwl_mvm_cdb_scan_api(mvm)) {
1213                 if (iwl_mvm_is_cdb_supported(mvm))
1214                         flags |= (iwl_mvm_is_scan_fragmented(hb_type)) ?
1215                                  SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1216                                  SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1217                 iwl_mvm_fill_scan_config_v2(mvm, cfg, flags, channel_flags,
1218                                             num_channels);
1219         } else {
1220                 iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags,
1221                                             num_channels);
1222         }
1223
1224         cmd.data[0] = cfg;
1225         cmd.len[0] = cmd_size;
1226         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1227
1228         IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1229
1230         ret = iwl_mvm_send_cmd(mvm, &cmd);
1231         if (!ret) {
1232                 mvm->scan_type = type;
1233                 mvm->hb_scan_type = hb_type;
1234         }
1235
1236         kfree(cfg);
1237         return ret;
1238 }
1239
1240 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1241 {
1242         struct iwl_scan_config cfg;
1243         struct iwl_host_cmd cmd = {
1244                 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1245                 .len[0] = sizeof(cfg),
1246                 .data[0] = &cfg,
1247                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1248         };
1249
1250         if (!iwl_mvm_is_reduced_config_scan_supported(mvm))
1251                 return iwl_mvm_legacy_config_scan(mvm);
1252
1253         memset(&cfg, 0, sizeof(cfg));
1254
1255         if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1256                                   ADD_STA, 0) < 12)
1257                 cfg.bcast_sta_id = mvm->aux_sta.sta_id;
1258         /*
1259          * Fw doesn't use this sta anymore, pending deprecation via HOST API
1260          * change.
1261          */
1262         else
1263                 cfg.bcast_sta_id = 0xff;
1264
1265         cfg.tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1266         cfg.rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1267
1268         IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1269
1270         return iwl_mvm_send_cmd(mvm, &cmd);
1271 }
1272
1273 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1274 {
1275         int i;
1276
1277         for (i = 0; i < mvm->max_scans; i++)
1278                 if (mvm->scan_uid_status[i] == status)
1279                         return i;
1280
1281         return -ENOENT;
1282 }
1283
1284 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1285                                     struct iwl_scan_req_umac *cmd,
1286                                     struct iwl_mvm_scan_params *params)
1287 {
1288         struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1289         u8 active_dwell, passive_dwell;
1290
1291         timing = &scan_timing[params->type];
1292         active_dwell = IWL_SCAN_DWELL_ACTIVE;
1293         passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1294
1295         if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1296                 cmd->v7.adwell_default_n_aps_social =
1297                         IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1298                 cmd->v7.adwell_default_n_aps =
1299                         IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1300
1301                 if (iwl_mvm_is_adwell_hb_ap_num_supported(mvm))
1302                         cmd->v9.adwell_default_hb_n_aps =
1303                                 IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1304
1305                 /* if custom max budget was configured with debugfs */
1306                 if (IWL_MVM_ADWELL_MAX_BUDGET)
1307                         cmd->v7.adwell_max_budget =
1308                                 cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1309                 else if (params->ssids && params->ssids[0].ssid_len)
1310                         cmd->v7.adwell_max_budget =
1311                                 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1312                 else
1313                         cmd->v7.adwell_max_budget =
1314                                 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1315
1316                 cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1317                 cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1318                         cpu_to_le32(timing->max_out_time);
1319                 cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1320                         cpu_to_le32(timing->suspend_time);
1321
1322                 if (iwl_mvm_is_cdb_supported(mvm)) {
1323                         hb_timing = &scan_timing[params->hb_type];
1324
1325                         cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1326                                 cpu_to_le32(hb_timing->max_out_time);
1327                         cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1328                                 cpu_to_le32(hb_timing->suspend_time);
1329                 }
1330
1331                 if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1332                         cmd->v7.active_dwell = active_dwell;
1333                         cmd->v7.passive_dwell = passive_dwell;
1334                         cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1335                 } else {
1336                         cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1337                         cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1338                         if (iwl_mvm_is_cdb_supported(mvm)) {
1339                                 cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1340                                         active_dwell;
1341                                 cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1342                                         passive_dwell;
1343                         }
1344                 }
1345         } else {
1346                 cmd->v1.extended_dwell = IWL_SCAN_DWELL_EXTENDED;
1347                 cmd->v1.active_dwell = active_dwell;
1348                 cmd->v1.passive_dwell = passive_dwell;
1349                 cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1350
1351                 if (iwl_mvm_is_cdb_supported(mvm)) {
1352                         hb_timing = &scan_timing[params->hb_type];
1353
1354                         cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1355                                         cpu_to_le32(hb_timing->max_out_time);
1356                         cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1357                                         cpu_to_le32(hb_timing->suspend_time);
1358                 }
1359
1360                 if (iwl_mvm_cdb_scan_api(mvm)) {
1361                         cmd->v6.scan_priority =
1362                                 cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1363                         cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1364                                 cpu_to_le32(timing->max_out_time);
1365                         cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1366                                 cpu_to_le32(timing->suspend_time);
1367                 } else {
1368                         cmd->v1.scan_priority =
1369                                 cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1370                         cmd->v1.max_out_time =
1371                                 cpu_to_le32(timing->max_out_time);
1372                         cmd->v1.suspend_time =
1373                                 cpu_to_le32(timing->suspend_time);
1374                 }
1375         }
1376
1377         if (iwl_mvm_is_regular_scan(params))
1378                 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1379         else
1380                 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1381 }
1382
1383 static u32 iwl_mvm_scan_umac_ooc_priority(struct iwl_mvm_scan_params *params)
1384 {
1385         return iwl_mvm_is_regular_scan(params) ?
1386                 IWL_SCAN_PRIORITY_EXT_6 :
1387                 IWL_SCAN_PRIORITY_EXT_2;
1388 }
1389
1390 static void
1391 iwl_mvm_scan_umac_dwell_v10(struct iwl_mvm *mvm,
1392                             struct iwl_scan_general_params_v10 *general_params,
1393                             struct iwl_mvm_scan_params *params)
1394 {
1395         struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1396         u8 active_dwell, passive_dwell;
1397
1398         timing = &scan_timing[params->type];
1399         active_dwell = IWL_SCAN_DWELL_ACTIVE;
1400         passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1401
1402         general_params->adwell_default_social_chn =
1403                 IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1404         general_params->adwell_default_2g = IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1405         general_params->adwell_default_5g = IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1406
1407         /* if custom max budget was configured with debugfs */
1408         if (IWL_MVM_ADWELL_MAX_BUDGET)
1409                 general_params->adwell_max_budget =
1410                         cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1411         else if (params->ssids && params->ssids[0].ssid_len)
1412                 general_params->adwell_max_budget =
1413                         cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1414         else
1415                 general_params->adwell_max_budget =
1416                         cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1417
1418         general_params->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1419         general_params->max_out_of_time[SCAN_LB_LMAC_IDX] =
1420                 cpu_to_le32(timing->max_out_time);
1421         general_params->suspend_time[SCAN_LB_LMAC_IDX] =
1422                 cpu_to_le32(timing->suspend_time);
1423
1424         hb_timing = &scan_timing[params->hb_type];
1425
1426         general_params->max_out_of_time[SCAN_HB_LMAC_IDX] =
1427                 cpu_to_le32(hb_timing->max_out_time);
1428         general_params->suspend_time[SCAN_HB_LMAC_IDX] =
1429                 cpu_to_le32(hb_timing->suspend_time);
1430
1431         general_params->active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1432         general_params->passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1433         general_params->active_dwell[SCAN_HB_LMAC_IDX] = active_dwell;
1434         general_params->passive_dwell[SCAN_HB_LMAC_IDX] = passive_dwell;
1435 }
1436
1437 struct iwl_mvm_scan_channel_segment {
1438         u8 start_idx;
1439         u8 end_idx;
1440         u8 first_channel_id;
1441         u8 last_channel_id;
1442         u8 channel_spacing_shift;
1443         u8 band;
1444 };
1445
1446 static const struct iwl_mvm_scan_channel_segment scan_channel_segments[] = {
1447         {
1448                 .start_idx = 0,
1449                 .end_idx = 13,
1450                 .first_channel_id = 1,
1451                 .last_channel_id = 14,
1452                 .channel_spacing_shift = 0,
1453                 .band = PHY_BAND_24
1454         },
1455         {
1456                 .start_idx = 14,
1457                 .end_idx = 41,
1458                 .first_channel_id = 36,
1459                 .last_channel_id = 144,
1460                 .channel_spacing_shift = 2,
1461                 .band = PHY_BAND_5
1462         },
1463         {
1464                 .start_idx = 42,
1465                 .end_idx = 50,
1466                 .first_channel_id = 149,
1467                 .last_channel_id = 181,
1468                 .channel_spacing_shift = 2,
1469                 .band = PHY_BAND_5
1470         },
1471         {
1472                 .start_idx = 51,
1473                 .end_idx = 111,
1474                 .first_channel_id = 1,
1475                 .last_channel_id = 241,
1476                 .channel_spacing_shift = 2,
1477                 .band = PHY_BAND_6
1478         },
1479 };
1480
1481 static int iwl_mvm_scan_ch_and_band_to_idx(u8 channel_id, u8 band)
1482 {
1483         int i, index;
1484
1485         if (!channel_id)
1486                 return -EINVAL;
1487
1488         for (i = 0; i < ARRAY_SIZE(scan_channel_segments); i++) {
1489                 const struct iwl_mvm_scan_channel_segment *ch_segment =
1490                         &scan_channel_segments[i];
1491                 u32 ch_offset;
1492
1493                 if (ch_segment->band != band ||
1494                     ch_segment->first_channel_id > channel_id ||
1495                     ch_segment->last_channel_id < channel_id)
1496                         continue;
1497
1498                 ch_offset = (channel_id - ch_segment->first_channel_id) >>
1499                         ch_segment->channel_spacing_shift;
1500
1501                 index = scan_channel_segments[i].start_idx + ch_offset;
1502                 if (index < IWL_SCAN_NUM_CHANNELS)
1503                         return index;
1504
1505                 break;
1506         }
1507
1508         return -EINVAL;
1509 }
1510
1511 static const u8 p2p_go_friendly_chs[] = {
1512         36, 40, 44, 48, 149, 153, 157, 161, 165,
1513 };
1514
1515 static const u8 social_chs[] = {
1516         1, 6, 11
1517 };
1518
1519 static void iwl_mvm_scan_ch_add_n_aps_override(enum nl80211_iftype vif_type,
1520                                                u8 ch_id, u8 band, u8 *ch_bitmap,
1521                                                size_t bitmap_n_entries)
1522 {
1523         int i;
1524
1525         if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
1526                 return;
1527
1528         for (i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
1529                 if (p2p_go_friendly_chs[i] == ch_id) {
1530                         int ch_idx, bitmap_idx;
1531
1532                         ch_idx = iwl_mvm_scan_ch_and_band_to_idx(ch_id, band);
1533                         if (ch_idx < 0)
1534                                 return;
1535
1536                         bitmap_idx = ch_idx / 8;
1537                         if (bitmap_idx >= bitmap_n_entries)
1538                                 return;
1539
1540                         ch_idx = ch_idx % 8;
1541                         ch_bitmap[bitmap_idx] |= BIT(ch_idx);
1542
1543                         return;
1544                 }
1545         }
1546 }
1547
1548 static u32 iwl_mvm_scan_ch_n_aps_flag(enum nl80211_iftype vif_type, u8 ch_id)
1549 {
1550         int i;
1551         u32 flags = 0;
1552
1553         if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
1554                 goto out;
1555
1556         for (i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
1557                 if (p2p_go_friendly_chs[i] == ch_id) {
1558                         flags |= IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT;
1559                         break;
1560                 }
1561         }
1562
1563         if (flags)
1564                 goto out;
1565
1566         for (i = 0; i < ARRAY_SIZE(social_chs); i++) {
1567                 if (social_chs[i] == ch_id) {
1568                         flags |= IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT;
1569                         break;
1570                 }
1571         }
1572
1573 out:
1574         return flags;
1575 }
1576
1577 static void
1578 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1579                                struct ieee80211_channel **channels,
1580                                int n_channels, u32 flags,
1581                                struct iwl_scan_channel_cfg_umac *channel_cfg)
1582 {
1583         int i;
1584
1585         for (i = 0; i < n_channels; i++) {
1586                 channel_cfg[i].flags = cpu_to_le32(flags);
1587                 channel_cfg[i].v1.channel_num = channels[i]->hw_value;
1588                 if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1589                         enum nl80211_band band = channels[i]->band;
1590
1591                         channel_cfg[i].v2.band =
1592                                 iwl_mvm_phy_band_from_nl80211(band);
1593                         channel_cfg[i].v2.iter_count = 1;
1594                         channel_cfg[i].v2.iter_interval = 0;
1595                 } else {
1596                         channel_cfg[i].v1.iter_count = 1;
1597                         channel_cfg[i].v1.iter_interval = 0;
1598                 }
1599         }
1600 }
1601
1602 static void
1603 iwl_mvm_umac_scan_cfg_channels_v4(struct iwl_mvm *mvm,
1604                                   struct ieee80211_channel **channels,
1605                                   struct iwl_scan_channel_params_v4 *cp,
1606                                   int n_channels, u32 flags,
1607                                   enum nl80211_iftype vif_type)
1608 {
1609         u8 *bitmap = cp->adwell_ch_override_bitmap;
1610         size_t bitmap_n_entries = ARRAY_SIZE(cp->adwell_ch_override_bitmap);
1611         int i;
1612
1613         for (i = 0; i < n_channels; i++) {
1614                 enum nl80211_band band = channels[i]->band;
1615                 struct iwl_scan_channel_cfg_umac *cfg =
1616                         &cp->channel_config[i];
1617
1618                 cfg->flags = cpu_to_le32(flags);
1619                 cfg->v2.channel_num = channels[i]->hw_value;
1620                 cfg->v2.band = iwl_mvm_phy_band_from_nl80211(band);
1621                 cfg->v2.iter_count = 1;
1622                 cfg->v2.iter_interval = 0;
1623
1624                 iwl_mvm_scan_ch_add_n_aps_override(vif_type,
1625                                                    cfg->v2.channel_num,
1626                                                    cfg->v2.band, bitmap,
1627                                                    bitmap_n_entries);
1628         }
1629 }
1630
1631 static void
1632 iwl_mvm_umac_scan_cfg_channels_v6(struct iwl_mvm *mvm,
1633                                   struct ieee80211_channel **channels,
1634                                   struct iwl_scan_channel_params_v6 *cp,
1635                                   int n_channels, u32 flags,
1636                                   enum nl80211_iftype vif_type)
1637 {
1638         int i;
1639
1640         for (i = 0; i < n_channels; i++) {
1641                 enum nl80211_band band = channels[i]->band;
1642                 struct iwl_scan_channel_cfg_umac *cfg = &cp->channel_config[i];
1643                 u32 n_aps_flag =
1644                         iwl_mvm_scan_ch_n_aps_flag(vif_type,
1645                                                    cfg->v2.channel_num);
1646
1647                 cfg->flags = cpu_to_le32(flags | n_aps_flag);
1648                 cfg->v2.channel_num = channels[i]->hw_value;
1649                 cfg->v2.band = iwl_mvm_phy_band_from_nl80211(band);
1650                 if (cfg80211_channel_is_psc(channels[i]))
1651                         cfg->flags = 0;
1652                 cfg->v2.iter_count = 1;
1653                 cfg->v2.iter_interval = 0;
1654         }
1655 }
1656
1657 static int
1658 iwl_mvm_umac_scan_fill_6g_chan_list(struct iwl_mvm_scan_params *params,
1659                                     __le32 *cmd_short_ssid, u8 *cmd_bssid,
1660                                     u8 *scan_ssid_num, u8 *bssid_num)
1661 {
1662         int j, idex_s = 0, idex_b = 0;
1663         struct cfg80211_scan_6ghz_params *scan_6ghz_params =
1664                 params->scan_6ghz_params;
1665
1666         if (!params->n_6ghz_params) {
1667                 for (j = 0; j < params->n_ssids; j++) {
1668                         cmd_short_ssid[idex_s++] =
1669                                 cpu_to_le32(~crc32_le(~0, params->ssids[j].ssid,
1670                                                       params->ssids[j].ssid_len));
1671                         (*scan_ssid_num)++;
1672                 }
1673                 return 0;
1674         }
1675
1676         /*
1677          * Populate the arrays of the short SSIDs and the BSSIDs using the 6GHz
1678          * collocated parameters. This might not be optimal, as this processing
1679          * does not (yet) correspond to the actual channels, so it is possible
1680          * that some entries would be left out.
1681          *
1682          * TODO: improve this logic.
1683          */
1684         for (j = 0; j < params->n_6ghz_params; j++) {
1685                 int k;
1686
1687                 /* First, try to place the short SSID */
1688                 if (scan_6ghz_params[j].short_ssid_valid) {
1689                         for (k = 0; k < idex_s; k++) {
1690                                 if (cmd_short_ssid[k] ==
1691                                     cpu_to_le32(scan_6ghz_params[j].short_ssid))
1692                                         break;
1693                         }
1694
1695                         if (k == idex_s && idex_s < SCAN_SHORT_SSID_MAX_SIZE) {
1696                                 cmd_short_ssid[idex_s++] =
1697                                         cpu_to_le32(scan_6ghz_params[j].short_ssid);
1698                                 (*scan_ssid_num)++;
1699                         }
1700                 }
1701
1702                 /* try to place BSSID for the same entry */
1703                 for (k = 0; k < idex_b; k++) {
1704                         if (!memcmp(&cmd_bssid[ETH_ALEN * k],
1705                                     scan_6ghz_params[j].bssid, ETH_ALEN))
1706                                 break;
1707                 }
1708
1709                 if (k == idex_b && idex_b < SCAN_BSSID_MAX_SIZE) {
1710                         memcpy(&cmd_bssid[ETH_ALEN * idex_b++],
1711                                scan_6ghz_params[j].bssid, ETH_ALEN);
1712                         (*bssid_num)++;
1713                 }
1714         }
1715         return 0;
1716 }
1717
1718 /* TODO: this function can be merged with iwl_mvm_scan_umac_fill_ch_p_v6 */
1719 static void
1720 iwl_mvm_umac_scan_cfg_channels_v6_6g(struct iwl_mvm_scan_params *params,
1721                                      u32 n_channels, __le32 *cmd_short_ssid,
1722                                      u8 *cmd_bssid, u8 scan_ssid_num,
1723                                      u8 bssid_num,
1724                                      struct iwl_scan_channel_params_v6 *cp,
1725                                      enum nl80211_iftype vif_type)
1726 {
1727         struct iwl_scan_channel_cfg_umac *channel_cfg = cp->channel_config;
1728         int i;
1729         struct cfg80211_scan_6ghz_params *scan_6ghz_params =
1730                 params->scan_6ghz_params;
1731
1732         for (i = 0; i < params->n_channels; i++) {
1733                 struct iwl_scan_channel_cfg_umac *cfg =
1734                         &cp->channel_config[i];
1735
1736                 u32 s_ssid_bitmap = 0, bssid_bitmap = 0, flags = 0;
1737                 u8 j, k, s_max = 0, b_max = 0, n_used_bssid_entries;
1738                 bool force_passive, found = false,
1739                      unsolicited_probe_on_chan = false, psc_no_listen = false;
1740
1741                 cfg->v1.channel_num = params->channels[i]->hw_value;
1742                 cfg->v2.band = 2;
1743                 cfg->v2.iter_count = 1;
1744                 cfg->v2.iter_interval = 0;
1745
1746                 /*
1747                  * The optimize the scan time, i.e., reduce the scan dwell time
1748                  * on each channel, the below logic tries to set 3 direct BSSID
1749                  * probe requests for each broadcast probe request with a short
1750                  * SSID.
1751                  * TODO: improve this logic
1752                  */
1753                 n_used_bssid_entries = 3;
1754                 for (j = 0; j < params->n_6ghz_params; j++) {
1755                         if (!(scan_6ghz_params[j].channel_idx == i))
1756                                 continue;
1757
1758                         found = false;
1759                         unsolicited_probe_on_chan |=
1760                                 scan_6ghz_params[j].unsolicited_probe;
1761                         psc_no_listen |= scan_6ghz_params[j].psc_no_listen;
1762
1763                         for (k = 0; k < scan_ssid_num; k++) {
1764                                 if (!scan_6ghz_params[j].unsolicited_probe &&
1765                                     le32_to_cpu(cmd_short_ssid[k]) ==
1766                                     scan_6ghz_params[j].short_ssid) {
1767                                         /* Relevant short SSID bit set */
1768                                         if (s_ssid_bitmap & BIT(k)) {
1769                                                 found = true;
1770                                                 break;
1771                                         }
1772
1773                                         /*
1774                                          * Use short SSID only to create a new
1775                                          * iteration during channel dwell.
1776                                          */
1777                                         if (n_used_bssid_entries >= 3) {
1778                                                 s_ssid_bitmap |= BIT(k);
1779                                                 s_max++;
1780                                                 n_used_bssid_entries -= 3;
1781                                                 found = true;
1782                                                 break;
1783                                         }
1784                                 }
1785                         }
1786
1787                         if (found)
1788                                 continue;
1789
1790                         for (k = 0; k < bssid_num; k++) {
1791                                 if (!memcmp(&cmd_bssid[ETH_ALEN * k],
1792                                             scan_6ghz_params[j].bssid,
1793                                             ETH_ALEN)) {
1794                                         if (!(bssid_bitmap & BIT(k))) {
1795                                                 bssid_bitmap |= BIT(k);
1796                                                 b_max++;
1797                                                 n_used_bssid_entries++;
1798                                         }
1799                                         break;
1800                                 }
1801                         }
1802                 }
1803
1804                 flags = bssid_bitmap | (s_ssid_bitmap << 16);
1805
1806                 if (cfg80211_channel_is_psc(params->channels[i]) &&
1807                     psc_no_listen)
1808                         flags |= IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN;
1809
1810                 if (unsolicited_probe_on_chan)
1811                         flags |= IWL_UHB_CHAN_CFG_FLAG_UNSOLICITED_PROBE_RES;
1812
1813                 /*
1814                  * In the following cases apply passive scan:
1815                  * 1. Non fragmented scan:
1816                  *      - PSC channel with NO_LISTEN_FLAG on should be treated
1817                  *        like non PSC channel
1818                  *      - Non PSC channel with more than 3 short SSIDs or more
1819                  *        than 9 BSSIDs.
1820                  *      - Non PSC Channel with unsolicited probe response and
1821                  *        more than 2 short SSIDs or more than 6 BSSIDs.
1822                  *      - PSC channel with more than 2 short SSIDs or more than
1823                  *        6 BSSIDs.
1824                  * 3. Fragmented scan:
1825                  *      - PSC channel with more than 1 SSID or 3 BSSIDs.
1826                  *      - Non PSC channel with more than 2 SSIDs or 6 BSSIDs.
1827                  *      - Non PSC channel with unsolicited probe response and
1828                  *        more than 1 SSID or more than 3 BSSIDs.
1829                  */
1830                 if (!iwl_mvm_is_scan_fragmented(params->type)) {
1831                         if (!cfg80211_channel_is_psc(params->channels[i]) ||
1832                             flags & IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN) {
1833                                 force_passive = (s_max > 3 || b_max > 9);
1834                                 force_passive |= (unsolicited_probe_on_chan &&
1835                                                   (s_max > 2 || b_max > 6));
1836                         } else {
1837                                 force_passive = (s_max > 2 || b_max > 6);
1838                         }
1839                 } else if (cfg80211_channel_is_psc(params->channels[i])) {
1840                         force_passive = (s_max > 1 || b_max > 3);
1841                 } else {
1842                         force_passive = (s_max > 2 || b_max > 6);
1843                         force_passive |= (unsolicited_probe_on_chan &&
1844                                           (s_max > 1 || b_max > 3));
1845                 }
1846                 if (force_passive ||
1847                     (!flags && !cfg80211_channel_is_psc(params->channels[i])))
1848                         flags |= IWL_UHB_CHAN_CFG_FLAG_FORCE_PASSIVE;
1849
1850                 channel_cfg[i].flags |= cpu_to_le32(flags);
1851         }
1852 }
1853
1854 static u8 iwl_mvm_scan_umac_chan_flags_v2(struct iwl_mvm *mvm,
1855                                           struct iwl_mvm_scan_params *params,
1856                                           struct ieee80211_vif *vif)
1857 {
1858         u8 flags = 0;
1859
1860         flags |= IWL_SCAN_CHANNEL_FLAG_ENABLE_CHAN_ORDER;
1861
1862         if (iwl_mvm_scan_use_ebs(mvm, vif))
1863                 flags |= IWL_SCAN_CHANNEL_FLAG_EBS |
1864                         IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1865                         IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1866
1867         /* set fragmented ebs for fragmented scan on HB channels */
1868         if (iwl_mvm_is_scan_fragmented(params->hb_type))
1869                 flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1870
1871         return flags;
1872 }
1873
1874 static u16 iwl_mvm_scan_umac_flags_v2(struct iwl_mvm *mvm,
1875                                       struct iwl_mvm_scan_params *params,
1876                                       struct ieee80211_vif *vif,
1877                                       int type)
1878 {
1879         u16 flags = 0;
1880
1881         if (params->n_ssids == 0)
1882                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE;
1883
1884         if (iwl_mvm_is_scan_fragmented(params->type))
1885                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1;
1886
1887         if (iwl_mvm_is_scan_fragmented(params->hb_type))
1888                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2;
1889
1890         if (params->pass_all)
1891                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PASS_ALL;
1892         else
1893                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_MATCH;
1894
1895         if (!iwl_mvm_is_regular_scan(params))
1896                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PERIODIC;
1897
1898         if (params->iter_notif ||
1899             mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1900                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTFY_ITER_COMPLETE;
1901
1902         if (IWL_MVM_ADWELL_ENABLE)
1903                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_ADAPTIVE_DWELL;
1904
1905         if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1906                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PREEMPTIVE;
1907
1908         if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
1909             params->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ)
1910                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_TRIGGER_UHB_SCAN;
1911
1912         return flags;
1913 }
1914
1915 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1916                                    struct iwl_mvm_scan_params *params,
1917                                    struct ieee80211_vif *vif)
1918 {
1919         u16 flags = 0;
1920
1921         if (params->n_ssids == 0)
1922                 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1923
1924         if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1925                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1926
1927         if (iwl_mvm_is_scan_fragmented(params->type))
1928                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1929
1930         if (iwl_mvm_is_cdb_supported(mvm) &&
1931             iwl_mvm_is_scan_fragmented(params->hb_type))
1932                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1933
1934         if (iwl_mvm_rrm_scan_needed(mvm) &&
1935             fw_has_capa(&mvm->fw->ucode_capa,
1936                         IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1937                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1938
1939         if (params->pass_all)
1940                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1941         else
1942                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1943
1944         if (!iwl_mvm_is_regular_scan(params))
1945                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1946
1947         if (params->iter_notif)
1948                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1949
1950 #ifdef CONFIG_IWLWIFI_DEBUGFS
1951         if (mvm->scan_iter_notif_enabled)
1952                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1953 #endif
1954
1955         if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1956                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1957
1958         if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE)
1959                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1960
1961         /*
1962          * Extended dwell is relevant only for low band to start with, as it is
1963          * being used for social channles only (1, 6, 11), so we can check
1964          * only scan type on low band also for CDB.
1965          */
1966         if (iwl_mvm_is_regular_scan(params) &&
1967             vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1968             !iwl_mvm_is_scan_fragmented(params->type) &&
1969             !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1970             !iwl_mvm_is_oce_supported(mvm))
1971                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1972
1973         if (iwl_mvm_is_oce_supported(mvm)) {
1974                 if ((params->flags &
1975                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1976                         flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1977                 /* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1978                  * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1979                  * the same bit, we need to make sure that we use this bit here
1980                  * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1981                  * used. */
1982                 if ((params->flags &
1983                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1984                      !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1985                         flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1986                 if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1987                         flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1988         }
1989
1990         return flags;
1991 }
1992
1993 static int
1994 iwl_mvm_fill_scan_sched_params(struct iwl_mvm_scan_params *params,
1995                                struct iwl_scan_umac_schedule *schedule,
1996                                __le16 *delay)
1997 {
1998         int i;
1999         if (WARN_ON(!params->n_scan_plans ||
2000                     params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
2001                 return -EINVAL;
2002
2003         for (i = 0; i < params->n_scan_plans; i++) {
2004                 struct cfg80211_sched_scan_plan *scan_plan =
2005                         &params->scan_plans[i];
2006
2007                 schedule[i].iter_count = scan_plan->iterations;
2008                 schedule[i].interval =
2009                         cpu_to_le16(scan_plan->interval);
2010         }
2011
2012         /*
2013          * If the number of iterations of the last scan plan is set to
2014          * zero, it should run infinitely. However, this is not always the case.
2015          * For example, when regular scan is requested the driver sets one scan
2016          * plan with one iteration.
2017          */
2018         if (!schedule[params->n_scan_plans - 1].iter_count)
2019                 schedule[params->n_scan_plans - 1].iter_count = 0xff;
2020
2021         *delay = cpu_to_le16(params->delay);
2022
2023         return 0;
2024 }
2025
2026 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2027                              struct iwl_mvm_scan_params *params,
2028                              int type, int uid)
2029 {
2030         struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
2031         struct iwl_scan_umac_chan_param *chan_param;
2032         void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
2033         void *sec_part = cmd_data + sizeof(struct iwl_scan_channel_cfg_umac) *
2034                 mvm->fw->ucode_capa.n_scan_channels;
2035         struct iwl_scan_req_umac_tail_v2 *tail_v2 =
2036                 (struct iwl_scan_req_umac_tail_v2 *)sec_part;
2037         struct iwl_scan_req_umac_tail_v1 *tail_v1;
2038         struct iwl_ssid_ie *direct_scan;
2039         int ret = 0;
2040         u32 ssid_bitmap = 0;
2041         u8 channel_flags = 0;
2042         u16 gen_flags;
2043         struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2044
2045         chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
2046
2047         iwl_mvm_scan_umac_dwell(mvm, cmd, params);
2048
2049         mvm->scan_uid_status[uid] = type;
2050
2051         cmd->uid = cpu_to_le32(uid);
2052         gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
2053         cmd->general_flags = cpu_to_le16(gen_flags);
2054         if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
2055                 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
2056                         cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
2057                                                         IWL_SCAN_NUM_OF_FRAGS;
2058                 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
2059                         cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
2060                                                         IWL_SCAN_NUM_OF_FRAGS;
2061
2062                 cmd->v8.general_flags2 =
2063                         IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
2064         }
2065
2066         cmd->scan_start_mac_id = scan_vif->id;
2067
2068         if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
2069                 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
2070
2071         if (iwl_mvm_scan_use_ebs(mvm, vif)) {
2072                 channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
2073                                 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
2074                                 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
2075
2076                 /* set fragmented ebs for fragmented scan on HB channels */
2077                 if (iwl_mvm_is_frag_ebs_supported(mvm)) {
2078                         if (gen_flags &
2079                             IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
2080                             (!iwl_mvm_is_cdb_supported(mvm) &&
2081                              gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
2082                                 channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
2083                 }
2084         }
2085
2086         chan_param->flags = channel_flags;
2087         chan_param->count = params->n_channels;
2088
2089         ret = iwl_mvm_fill_scan_sched_params(params, tail_v2->schedule,
2090                                              &tail_v2->delay);
2091         if (ret) {
2092                 mvm->scan_uid_status[uid] = 0;
2093                 return ret;
2094         }
2095
2096         if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
2097                 tail_v2->preq = params->preq;
2098                 direct_scan = tail_v2->direct_scan;
2099         } else {
2100                 tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
2101                 iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
2102                                                   &params->preq);
2103                 direct_scan = tail_v1->direct_scan;
2104         }
2105         iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
2106         iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
2107                                        params->n_channels, ssid_bitmap,
2108                                        cmd_data);
2109         return 0;
2110 }
2111
2112 static void
2113 iwl_mvm_scan_umac_fill_general_p_v10(struct iwl_mvm *mvm,
2114                                      struct iwl_mvm_scan_params *params,
2115                                      struct ieee80211_vif *vif,
2116                                      struct iwl_scan_general_params_v10 *gp,
2117                                      u16 gen_flags)
2118 {
2119         struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2120
2121         iwl_mvm_scan_umac_dwell_v10(mvm, gp, params);
2122
2123         gp->flags = cpu_to_le16(gen_flags);
2124
2125         if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
2126                 gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2127         if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2)
2128                 gp->num_of_fragments[SCAN_HB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2129
2130         gp->scan_start_mac_id = scan_vif->id;
2131 }
2132
2133 static void
2134 iwl_mvm_scan_umac_fill_probe_p_v3(struct iwl_mvm_scan_params *params,
2135                                   struct iwl_scan_probe_params_v3 *pp)
2136 {
2137         pp->preq = params->preq;
2138         pp->ssid_num = params->n_ssids;
2139         iwl_scan_build_ssids(params, pp->direct_scan, NULL);
2140 }
2141
2142 static void
2143 iwl_mvm_scan_umac_fill_probe_p_v4(struct iwl_mvm_scan_params *params,
2144                                   struct iwl_scan_probe_params_v4 *pp,
2145                                   u32 *bitmap_ssid)
2146 {
2147         pp->preq = params->preq;
2148         iwl_scan_build_ssids(params, pp->direct_scan, bitmap_ssid);
2149 }
2150
2151 static void
2152 iwl_mvm_scan_umac_fill_ch_p_v4(struct iwl_mvm *mvm,
2153                                struct iwl_mvm_scan_params *params,
2154                                struct ieee80211_vif *vif,
2155                                struct iwl_scan_channel_params_v4 *cp,
2156                                u32 channel_cfg_flags)
2157 {
2158         cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2159         cp->count = params->n_channels;
2160         cp->num_of_aps_override = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2161
2162         iwl_mvm_umac_scan_cfg_channels_v4(mvm, params->channels, cp,
2163                                           params->n_channels,
2164                                           channel_cfg_flags,
2165                                           vif->type);
2166 }
2167
2168 static void
2169 iwl_mvm_scan_umac_fill_ch_p_v6(struct iwl_mvm *mvm,
2170                                struct iwl_mvm_scan_params *params,
2171                                struct ieee80211_vif *vif,
2172                                struct iwl_scan_channel_params_v6 *cp,
2173                                u32 channel_cfg_flags)
2174 {
2175         cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2176         cp->count = params->n_channels;
2177         cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2178         cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2179
2180         iwl_mvm_umac_scan_cfg_channels_v6(mvm, params->channels, cp,
2181                                           params->n_channels,
2182                                           channel_cfg_flags,
2183                                           vif->type);
2184 }
2185
2186 static int iwl_mvm_scan_umac_v12(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2187                                  struct iwl_mvm_scan_params *params, int type,
2188                                  int uid)
2189 {
2190         struct iwl_scan_req_umac_v12 *cmd = mvm->scan_cmd;
2191         struct iwl_scan_req_params_v12 *scan_p = &cmd->scan_params;
2192         int ret;
2193         u16 gen_flags;
2194
2195         mvm->scan_uid_status[uid] = type;
2196
2197         cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
2198         cmd->uid = cpu_to_le32(uid);
2199
2200         gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2201         iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
2202                                              &scan_p->general_params,
2203                                              gen_flags);
2204
2205          ret = iwl_mvm_fill_scan_sched_params(params,
2206                                               scan_p->periodic_params.schedule,
2207                                               &scan_p->periodic_params.delay);
2208         if (ret)
2209                 return ret;
2210
2211         iwl_mvm_scan_umac_fill_probe_p_v3(params, &scan_p->probe_params);
2212         iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
2213                                        &scan_p->channel_params, 0);
2214
2215         return 0;
2216 }
2217
2218 static int iwl_mvm_scan_umac_v14(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2219                                  struct iwl_mvm_scan_params *params, int type,
2220                                  int uid)
2221 {
2222         struct iwl_scan_req_umac_v14 *cmd = mvm->scan_cmd;
2223         struct iwl_scan_req_params_v14 *scan_p = &cmd->scan_params;
2224         struct iwl_scan_channel_params_v6 *cp = &scan_p->channel_params;
2225         struct iwl_scan_probe_params_v4 *pb = &scan_p->probe_params;
2226         int ret;
2227         u16 gen_flags;
2228         u32 bitmap_ssid = 0;
2229
2230         mvm->scan_uid_status[uid] = type;
2231
2232         cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
2233         cmd->uid = cpu_to_le32(uid);
2234
2235         gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2236         iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
2237                                              &scan_p->general_params,
2238                                              gen_flags);
2239
2240          ret = iwl_mvm_fill_scan_sched_params(params,
2241                                               scan_p->periodic_params.schedule,
2242                                               &scan_p->periodic_params.delay);
2243         if (ret)
2244                 return ret;
2245
2246         iwl_mvm_scan_umac_fill_probe_p_v4(params, &scan_p->probe_params,
2247                                           &bitmap_ssid);
2248         if (!params->scan_6ghz) {
2249                 iwl_mvm_scan_umac_fill_ch_p_v6(mvm, params, vif,
2250                                                &scan_p->channel_params, bitmap_ssid);
2251
2252                 return 0;
2253         }
2254         cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2255         cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2256         cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2257
2258         ret = iwl_mvm_umac_scan_fill_6g_chan_list(params, pb->short_ssid,
2259                                                   pb->bssid_array[0],
2260                                                   &pb->short_ssid_num,
2261                                                   &pb->bssid_num);
2262         if (ret)
2263                 return ret;
2264
2265         iwl_mvm_umac_scan_cfg_channels_v6_6g(params,
2266                                              params->n_channels,
2267                                              pb->short_ssid,
2268                                              pb->bssid_array[0],
2269                                              pb->short_ssid_num,
2270                                              pb->bssid_num, cp,
2271                                              vif->type);
2272         cp->count = params->n_channels;
2273         if (!params->n_ssids ||
2274             (params->n_ssids == 1 && !params->ssids[0].ssid_len))
2275                 cp->flags |= IWL_SCAN_CHANNEL_FLAG_6G_PSC_NO_FILTER;
2276
2277         return 0;
2278 }
2279
2280 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
2281 {
2282         return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
2283 }
2284
2285 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
2286 {
2287         bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2288                                          IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2289
2290         /* This looks a bit arbitrary, but the idea is that if we run
2291          * out of possible simultaneous scans and the userspace is
2292          * trying to run a scan type that is already running, we
2293          * return -EBUSY.  But if the userspace wants to start a
2294          * different type of scan, we stop the opposite type to make
2295          * space for the new request.  The reason is backwards
2296          * compatibility with old wpa_supplicant that wouldn't stop a
2297          * scheduled scan before starting a normal scan.
2298          */
2299
2300         /* FW supports only a single periodic scan */
2301         if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2302             mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
2303                 return -EBUSY;
2304
2305         if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
2306                 return 0;
2307
2308         /* Use a switch, even though this is a bitmask, so that more
2309          * than one bits set will fall in default and we will warn.
2310          */
2311         switch (type) {
2312         case IWL_MVM_SCAN_REGULAR:
2313                 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2314                         return -EBUSY;
2315                 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2316         case IWL_MVM_SCAN_SCHED:
2317                 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2318                         return -EBUSY;
2319                 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2320         case IWL_MVM_SCAN_NETDETECT:
2321                 /* For non-unified images, there's no need to stop
2322                  * anything for net-detect since the firmware is
2323                  * restarted anyway.  This way, any sched scans that
2324                  * were running will be restarted when we resume.
2325                  */
2326                 if (!unified_image)
2327                         return 0;
2328
2329                 /* If this is a unified image and we ran out of scans,
2330                  * we need to stop something.  Prefer stopping regular
2331                  * scans, because the results are useless at this
2332                  * point, and we should be able to keep running
2333                  * another scheduled scan while suspended.
2334                  */
2335                 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2336                         return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
2337                                                  true);
2338                 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2339                         return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
2340                                                  true);
2341                 /* Something is wrong if no scan was running but we
2342                  * ran out of scans.
2343                  */
2344                 fallthrough;
2345         default:
2346                 WARN_ON(1);
2347                 break;
2348         }
2349
2350         return -EIO;
2351 }
2352
2353 #define SCAN_TIMEOUT 20000
2354
2355 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
2356 {
2357         struct delayed_work *delayed_work = to_delayed_work(work);
2358         struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
2359                                            scan_timeout_dwork);
2360
2361         IWL_ERR(mvm, "regular scan timed out\n");
2362
2363         iwl_force_nmi(mvm->trans);
2364 }
2365
2366 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
2367                                    struct iwl_mvm_scan_params *params,
2368                                    struct ieee80211_vif *vif)
2369 {
2370         if (iwl_mvm_is_cdb_supported(mvm)) {
2371                 params->type =
2372                         iwl_mvm_get_scan_type_band(mvm, vif,
2373                                                    NL80211_BAND_2GHZ);
2374                 params->hb_type =
2375                         iwl_mvm_get_scan_type_band(mvm, vif,
2376                                                    NL80211_BAND_5GHZ);
2377         } else {
2378                 params->type = iwl_mvm_get_scan_type(mvm, vif);
2379         }
2380 }
2381
2382 struct iwl_scan_umac_handler {
2383         u8 version;
2384         int (*handler)(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2385                        struct iwl_mvm_scan_params *params, int type, int uid);
2386 };
2387
2388 #define IWL_SCAN_UMAC_HANDLER(_ver) {           \
2389         .version = _ver,                        \
2390         .handler = iwl_mvm_scan_umac_v##_ver,   \
2391 }
2392
2393 static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
2394         /* set the newest version first to shorten the list traverse time */
2395         IWL_SCAN_UMAC_HANDLER(14),
2396         IWL_SCAN_UMAC_HANDLER(12),
2397 };
2398
2399 static int iwl_mvm_build_scan_cmd(struct iwl_mvm *mvm,
2400                                   struct ieee80211_vif *vif,
2401                                   struct iwl_host_cmd *hcmd,
2402                                   struct iwl_mvm_scan_params *params,
2403                                   int type)
2404 {
2405         int uid, i, err;
2406         u8 scan_ver;
2407
2408         lockdep_assert_held(&mvm->mutex);
2409         memset(mvm->scan_cmd, 0, ksize(mvm->scan_cmd));
2410
2411         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2412                 hcmd->id = SCAN_OFFLOAD_REQUEST_CMD;
2413
2414                 return iwl_mvm_scan_lmac(mvm, vif, params);
2415         }
2416
2417         uid = iwl_mvm_scan_uid_by_status(mvm, 0);
2418         if (uid < 0)
2419                 return uid;
2420
2421         hcmd->id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
2422
2423         scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2424                                          SCAN_REQ_UMAC,
2425                                          IWL_FW_CMD_VER_UNKNOWN);
2426
2427         for (i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
2428                 const struct iwl_scan_umac_handler *ver_handler =
2429                         &iwl_scan_umac_handlers[i];
2430
2431                 if (ver_handler->version != scan_ver)
2432                         continue;
2433
2434                 return ver_handler->handler(mvm, vif, params, type, uid);
2435         }
2436
2437         err = iwl_mvm_scan_umac(mvm, vif, params, type, uid);
2438         if (err)
2439                 return err;
2440
2441         return uid;
2442 }
2443
2444 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2445                            struct cfg80211_scan_request *req,
2446                            struct ieee80211_scan_ies *ies)
2447 {
2448         struct iwl_host_cmd hcmd = {
2449                 .len = { iwl_mvm_scan_size(mvm), },
2450                 .data = { mvm->scan_cmd, },
2451                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
2452         };
2453         struct iwl_mvm_scan_params params = {};
2454         int ret, uid;
2455         struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
2456
2457         lockdep_assert_held(&mvm->mutex);
2458
2459         if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2460                 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
2461                 return -EBUSY;
2462         }
2463
2464         ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
2465         if (ret)
2466                 return ret;
2467
2468         /* we should have failed registration if scan_cmd was NULL */
2469         if (WARN_ON(!mvm->scan_cmd))
2470                 return -ENOMEM;
2471
2472         if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2473                 return -ENOBUFS;
2474
2475         params.n_ssids = req->n_ssids;
2476         params.flags = req->flags;
2477         params.n_channels = req->n_channels;
2478         params.delay = 0;
2479         params.ssids = req->ssids;
2480         params.channels = req->channels;
2481         params.mac_addr = req->mac_addr;
2482         params.mac_addr_mask = req->mac_addr_mask;
2483         params.no_cck = req->no_cck;
2484         params.pass_all = true;
2485         params.n_match_sets = 0;
2486         params.match_sets = NULL;
2487
2488         params.scan_plans = &scan_plan;
2489         params.n_scan_plans = 1;
2490
2491         params.n_6ghz_params = req->n_6ghz_params;
2492         params.scan_6ghz_params = req->scan_6ghz_params;
2493         params.scan_6ghz = req->scan_6ghz;
2494         iwl_mvm_fill_scan_type(mvm, &params, vif);
2495
2496         if (req->duration)
2497                 params.iter_notif = true;
2498
2499         iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
2500
2501         uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params,
2502                                      IWL_MVM_SCAN_REGULAR);
2503
2504         if (uid < 0)
2505                 return uid;
2506
2507         iwl_mvm_pause_tcm(mvm, false);
2508
2509         ret = iwl_mvm_send_cmd(mvm, &hcmd);
2510         if (ret) {
2511                 /* If the scan failed, it usually means that the FW was unable
2512                  * to allocate the time events. Warn on it, but maybe we
2513                  * should try to send the command again with different params.
2514                  */
2515                 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
2516                 iwl_mvm_resume_tcm(mvm);
2517                 mvm->scan_uid_status[uid] = 0;
2518                 return ret;
2519         }
2520
2521         IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
2522         mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
2523         mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
2524
2525         schedule_delayed_work(&mvm->scan_timeout_dwork,
2526                               msecs_to_jiffies(SCAN_TIMEOUT));
2527
2528         return 0;
2529 }
2530
2531 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
2532                              struct ieee80211_vif *vif,
2533                              struct cfg80211_sched_scan_request *req,
2534                              struct ieee80211_scan_ies *ies,
2535                              int type)
2536 {
2537         struct iwl_host_cmd hcmd = {
2538                 .len = { iwl_mvm_scan_size(mvm), },
2539                 .data = { mvm->scan_cmd, },
2540                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
2541         };
2542         struct iwl_mvm_scan_params params = {};
2543         int ret, uid;
2544         int i, j;
2545         bool non_psc_included = false;
2546
2547         lockdep_assert_held(&mvm->mutex);
2548
2549         if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2550                 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
2551                 return -EBUSY;
2552         }
2553
2554         ret = iwl_mvm_check_running_scans(mvm, type);
2555         if (ret)
2556                 return ret;
2557
2558         /* we should have failed registration if scan_cmd was NULL */
2559         if (WARN_ON(!mvm->scan_cmd))
2560                 return -ENOMEM;
2561
2562
2563         params.n_ssids = req->n_ssids;
2564         params.flags = req->flags;
2565         params.n_channels = req->n_channels;
2566         params.ssids = req->ssids;
2567         params.channels = req->channels;
2568         params.mac_addr = req->mac_addr;
2569         params.mac_addr_mask = req->mac_addr_mask;
2570         params.no_cck = false;
2571         params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
2572         params.n_match_sets = req->n_match_sets;
2573         params.match_sets = req->match_sets;
2574         if (!req->n_scan_plans)
2575                 return -EINVAL;
2576
2577         params.n_scan_plans = req->n_scan_plans;
2578         params.scan_plans = req->scan_plans;
2579
2580         iwl_mvm_fill_scan_type(mvm, &params, vif);
2581
2582         /* In theory, LMAC scans can handle a 32-bit delay, but since
2583          * waiting for over 18 hours to start the scan is a bit silly
2584          * and to keep it aligned with UMAC scans (which only support
2585          * 16-bit delays), trim it down to 16-bits.
2586          */
2587         if (req->delay > U16_MAX) {
2588                 IWL_DEBUG_SCAN(mvm,
2589                                "delay value is > 16-bits, set to max possible\n");
2590                 params.delay = U16_MAX;
2591         } else {
2592                 params.delay = req->delay;
2593         }
2594
2595         ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
2596         if (ret)
2597                 return ret;
2598
2599         iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
2600
2601         /* for 6 GHZ band only PSC channels need to be added */
2602         for (i = 0; i < params.n_channels; i++) {
2603                 struct ieee80211_channel *channel = params.channels[i];
2604
2605                 if (channel->band == NL80211_BAND_6GHZ &&
2606                     !cfg80211_channel_is_psc(channel)) {
2607                         non_psc_included = true;
2608                         break;
2609                 }
2610         }
2611
2612         if (non_psc_included) {
2613                 params.channels = kmemdup(params.channels,
2614                                           sizeof(params.channels[0]) *
2615                                           params.n_channels,
2616                                           GFP_KERNEL);
2617                 if (!params.channels)
2618                         return -ENOMEM;
2619
2620                 for (i = j = 0; i < params.n_channels; i++) {
2621                         if (params.channels[i]->band == NL80211_BAND_6GHZ &&
2622                             !cfg80211_channel_is_psc(params.channels[i]))
2623                                 continue;
2624                         params.channels[j++] = params.channels[i];
2625                 }
2626                 params.n_channels = j;
2627         }
2628
2629         if (non_psc_included &&
2630             !iwl_mvm_scan_fits(mvm, req->n_ssids, ies, params.n_channels)) {
2631                 kfree(params.channels);
2632                 return -ENOBUFS;
2633         }
2634
2635         uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, &params, type);
2636
2637         if (non_psc_included)
2638                 kfree(params.channels);
2639         if (uid < 0)
2640                 return uid;
2641
2642         ret = iwl_mvm_send_cmd(mvm, &hcmd);
2643         if (!ret) {
2644                 IWL_DEBUG_SCAN(mvm,
2645                                "Sched scan request was sent successfully\n");
2646                 mvm->scan_status |= type;
2647         } else {
2648                 /* If the scan failed, it usually means that the FW was unable
2649                  * to allocate the time events. Warn on it, but maybe we
2650                  * should try to send the command again with different params.
2651                  */
2652                 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
2653                 mvm->scan_uid_status[uid] = 0;
2654                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2655         }
2656
2657         return ret;
2658 }
2659
2660 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
2661                                          struct iwl_rx_cmd_buffer *rxb)
2662 {
2663         struct iwl_rx_packet *pkt = rxb_addr(rxb);
2664         struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2665         u32 uid = __le32_to_cpu(notif->uid);
2666         bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
2667
2668         if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
2669                 return;
2670
2671         /* if the scan is already stopping, we don't need to notify mac80211 */
2672         if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
2673                 struct cfg80211_scan_info info = {
2674                         .aborted = aborted,
2675                         .scan_start_tsf = mvm->scan_start,
2676                 };
2677
2678                 memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
2679                 ieee80211_scan_completed(mvm->hw, &info);
2680                 mvm->scan_vif = NULL;
2681                 cancel_delayed_work(&mvm->scan_timeout_dwork);
2682                 iwl_mvm_resume_tcm(mvm);
2683         } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
2684                 ieee80211_sched_scan_stopped(mvm->hw);
2685                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2686         }
2687
2688         mvm->scan_status &= ~mvm->scan_uid_status[uid];
2689         IWL_DEBUG_SCAN(mvm,
2690                        "Scan completed, uid %u type %u, status %s, EBS status %s\n",
2691                        uid, mvm->scan_uid_status[uid],
2692                        notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
2693                                 "completed" : "aborted",
2694                        iwl_mvm_ebs_status_str(notif->ebs_status));
2695         IWL_DEBUG_SCAN(mvm,
2696                        "Last line %d, Last iteration %d, Time from last iteration %d\n",
2697                        notif->last_schedule, notif->last_iter,
2698                        __le32_to_cpu(notif->time_from_last_iter));
2699
2700         if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
2701             notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
2702                 mvm->last_ebs_successful = false;
2703
2704         mvm->scan_uid_status[uid] = 0;
2705 }
2706
2707 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
2708                                               struct iwl_rx_cmd_buffer *rxb)
2709 {
2710         struct iwl_rx_packet *pkt = rxb_addr(rxb);
2711         struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
2712
2713         mvm->scan_start = le64_to_cpu(notif->start_tsf);
2714
2715         IWL_DEBUG_SCAN(mvm,
2716                        "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
2717                        notif->status, notif->scanned_channels);
2718
2719         if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
2720                 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
2721                 ieee80211_sched_scan_results(mvm->hw);
2722                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
2723         }
2724
2725         IWL_DEBUG_SCAN(mvm,
2726                        "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
2727                        mvm->scan_start);
2728 }
2729
2730 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
2731 {
2732         struct iwl_umac_scan_abort cmd = {};
2733         int uid, ret;
2734
2735         lockdep_assert_held(&mvm->mutex);
2736
2737         /* We should always get a valid index here, because we already
2738          * checked that this type of scan was running in the generic
2739          * code.
2740          */
2741         uid = iwl_mvm_scan_uid_by_status(mvm, type);
2742         if (WARN_ON_ONCE(uid < 0))
2743                 return uid;
2744
2745         cmd.uid = cpu_to_le32(uid);
2746
2747         IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2748
2749         ret = iwl_mvm_send_cmd_pdu(mvm,
2750                                    iwl_cmd_id(SCAN_ABORT_UMAC,
2751                                               IWL_ALWAYS_LONG_GROUP, 0),
2752                                    0, sizeof(cmd), &cmd);
2753         if (!ret)
2754                 mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
2755
2756         return ret;
2757 }
2758
2759 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
2760 {
2761         struct iwl_notification_wait wait_scan_done;
2762         static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
2763                                               SCAN_OFFLOAD_COMPLETE, };
2764         int ret;
2765
2766         lockdep_assert_held(&mvm->mutex);
2767
2768         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2769                                    scan_done_notif,
2770                                    ARRAY_SIZE(scan_done_notif),
2771                                    NULL, NULL);
2772
2773         IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2774
2775         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2776                 ret = iwl_mvm_umac_scan_abort(mvm, type);
2777         else
2778                 ret = iwl_mvm_lmac_scan_abort(mvm);
2779
2780         if (ret) {
2781                 IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
2782                 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2783                 return ret;
2784         }
2785
2786         return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
2787                                      1 * HZ);
2788 }
2789
2790 #define IWL_SCAN_REQ_UMAC_HANDLE_SIZE(_ver) {                           \
2791         case (_ver): return sizeof(struct iwl_scan_req_umac_v##_ver);   \
2792 }
2793
2794 static int iwl_scan_req_umac_get_size(u8 scan_ver)
2795 {
2796         switch (scan_ver) {
2797                 IWL_SCAN_REQ_UMAC_HANDLE_SIZE(14);
2798                 IWL_SCAN_REQ_UMAC_HANDLE_SIZE(12);
2799         }
2800
2801         return 0;
2802 }
2803
2804 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2805 {
2806         int base_size, tail_size;
2807         u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2808                                             SCAN_REQ_UMAC,
2809                                             IWL_FW_CMD_VER_UNKNOWN);
2810
2811         base_size = iwl_scan_req_umac_get_size(scan_ver);
2812         if (base_size)
2813                 return base_size;
2814
2815
2816         if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
2817                 base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
2818         else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
2819                 base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
2820         else if (iwl_mvm_cdb_scan_api(mvm))
2821                 base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
2822         else
2823                 base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
2824
2825         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2826                 if (iwl_mvm_is_scan_ext_chan_supported(mvm))
2827                         tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
2828                 else
2829                         tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
2830
2831                 return base_size +
2832                         sizeof(struct iwl_scan_channel_cfg_umac) *
2833                                 mvm->fw->ucode_capa.n_scan_channels +
2834                         tail_size;
2835         }
2836         return sizeof(struct iwl_scan_req_lmac) +
2837                 sizeof(struct iwl_scan_channel_cfg_lmac) *
2838                 mvm->fw->ucode_capa.n_scan_channels +
2839                 sizeof(struct iwl_scan_probe_req_v1);
2840 }
2841
2842 /*
2843  * This function is used in nic restart flow, to inform mac80211 about scans
2844  * that was aborted by restart flow or by an assert.
2845  */
2846 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
2847 {
2848         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2849                 int uid, i;
2850
2851                 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
2852                 if (uid >= 0) {
2853                         struct cfg80211_scan_info info = {
2854                                 .aborted = true,
2855                         };
2856
2857                         ieee80211_scan_completed(mvm->hw, &info);
2858                         mvm->scan_uid_status[uid] = 0;
2859                 }
2860                 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
2861                 if (uid >= 0 && !mvm->fw_restart) {
2862                         ieee80211_sched_scan_stopped(mvm->hw);
2863                         mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2864                         mvm->scan_uid_status[uid] = 0;
2865                 }
2866                 uid = iwl_mvm_scan_uid_by_status(mvm,
2867                                                  IWL_MVM_SCAN_STOPPING_REGULAR);
2868                 if (uid >= 0)
2869                         mvm->scan_uid_status[uid] = 0;
2870
2871                 uid = iwl_mvm_scan_uid_by_status(mvm,
2872                                                  IWL_MVM_SCAN_STOPPING_SCHED);
2873                 if (uid >= 0)
2874                         mvm->scan_uid_status[uid] = 0;
2875
2876                 /* We shouldn't have any UIDs still set.  Loop over all the
2877                  * UIDs to make sure there's nothing left there and warn if
2878                  * any is found.
2879                  */
2880                 for (i = 0; i < mvm->max_scans; i++) {
2881                         if (WARN_ONCE(mvm->scan_uid_status[i],
2882                                       "UMAC scan UID %d status was not cleaned\n",
2883                                       i))
2884                                 mvm->scan_uid_status[i] = 0;
2885                 }
2886         } else {
2887                 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
2888                         struct cfg80211_scan_info info = {
2889                                 .aborted = true,
2890                         };
2891
2892                         ieee80211_scan_completed(mvm->hw, &info);
2893                 }
2894
2895                 /* Sched scan will be restarted by mac80211 in
2896                  * restart_hw, so do not report if FW is about to be
2897                  * restarted.
2898                  */
2899                 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
2900                     !mvm->fw_restart) {
2901                         ieee80211_sched_scan_stopped(mvm->hw);
2902                         mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2903                 }
2904         }
2905 }
2906
2907 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2908 {
2909         int ret;
2910
2911         if (!(mvm->scan_status & type))
2912                 return 0;
2913
2914         if (iwl_mvm_is_radio_killed(mvm)) {
2915                 ret = 0;
2916                 goto out;
2917         }
2918
2919         ret = iwl_mvm_scan_stop_wait(mvm, type);
2920         if (!ret)
2921                 mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2922 out:
2923         /* Clear the scan status so the next scan requests will
2924          * succeed and mark the scan as stopping, so that the Rx
2925          * handler doesn't do anything, as the scan was stopped from
2926          * above.
2927          */
2928         mvm->scan_status &= ~type;
2929
2930         if (type == IWL_MVM_SCAN_REGULAR) {
2931                 cancel_delayed_work(&mvm->scan_timeout_dwork);
2932                 if (notify) {
2933                         struct cfg80211_scan_info info = {
2934                                 .aborted = true,
2935                         };
2936
2937                         ieee80211_scan_completed(mvm->hw, &info);
2938                 }
2939         } else if (notify) {
2940                 ieee80211_sched_scan_stopped(mvm->hw);
2941                 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2942         }
2943
2944         return ret;
2945 }