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