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