blob: 7777d56cbb606f8618e50370c4ed47ee18258de8 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Luciano Coelho9af91f42015-02-10 10:42:26 +02009 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Luciano Coelho9af91f42015-02-10 10:42:26 +020035 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * 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"
Johannes Berg8ca151b2013-01-24 14:25:36 +010070#include "fw-api-scan.h"
71
Haim Dreyfussa1ed4022014-12-01 23:30:07 +020072#define IWL_DENSE_EBS_SCAN_RATIO 5
73#define IWL_SPARSE_EBS_SCAN_RATIO 1
Alexander Bondar8a110d92014-03-12 17:31:19 +020074
75struct iwl_mvm_scan_params {
76 u32 max_out_time;
77 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020078 bool passive_fragmented;
Luciano Coelhof7b788b2015-04-02 00:08:35 +030079 u32 n_channels;
Luciano Coelho283c2492015-04-17 11:18:46 +030080 u16 delay;
Luciano Coelhof7b788b2015-04-02 00:08:35 +030081 int n_ssids;
82 struct cfg80211_ssid *ssids;
83 struct ieee80211_channel **channels;
84 u16 interval; /* interval between scans (in secs) */
85 u32 flags;
86 u8 *mac_addr;
87 u8 *mac_addr_mask;
88 bool no_cck;
89 bool pass_all;
90 int n_match_sets;
Luciano Coelho45d1b122015-04-15 16:34:13 +030091 struct iwl_scan_probe_req preq;
Luciano Coelhof7b788b2015-04-02 00:08:35 +030092 struct cfg80211_match_set *match_sets;
Alexander Bondar50df8a32014-03-12 20:30:51 +020093 struct _dwell {
94 u16 passive;
95 u16 active;
David Spinadel190f1022015-02-17 12:45:21 +020096 u16 fragmented;
Alexander Bondar50df8a32014-03-12 20:30:51 +020097 } dwell[IEEE80211_NUM_BANDS];
Luciano Coelhocf5d3172015-04-16 20:13:28 +030098 struct {
99 u8 iterations;
100 u8 full_scan_mul; /* not used for UMAC */
101 } schedule[2];
Alexander Bondar8a110d92014-03-12 17:31:19 +0200102};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103
Luciano Coelho9345c592015-05-07 10:10:57 +0300104static int iwl_umac_scan_stop(struct iwl_mvm *mvm, int type, bool notify);
David Spinadeld2496222014-05-20 12:46:37 +0300105
106static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
107{
108 if (mvm->scan_rx_ant != ANT_NONE)
109 return mvm->scan_rx_ant;
Moshe Harela0544272014-12-08 21:13:14 +0200110 return iwl_mvm_get_valid_rx_ant(mvm);
David Spinadeld2496222014-05-20 12:46:37 +0300111}
112
Johannes Berg8ca151b2013-01-24 14:25:36 +0100113static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
114{
115 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300116 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100117
David Spinadeld2496222014-05-20 12:46:37 +0300118 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
120 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
121 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
122 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
123 return cpu_to_le16(rx_chain);
124}
125
David Spinadelfb98be52014-05-04 12:51:10 +0300126static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100127{
David Spinadelfb98be52014-05-04 12:51:10 +0300128 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100129 return cpu_to_le32(PHY_BAND_24);
130 else
131 return cpu_to_le32(PHY_BAND_5);
132}
133
134static inline __le32
135iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
136 bool no_cck)
137{
138 u32 tx_ant;
139
140 mvm->scan_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200141 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100142 mvm->scan_last_antenna_idx);
143 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
144
145 if (band == IEEE80211_BAND_2GHZ && !no_cck)
146 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
147 tx_ant);
148 else
149 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
150}
151
152/*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100153 * If req->n_ssids > 0, it means we should do an active scan.
154 * In case of active scan w/o directed scan, we receive a zero-length SSID
155 * just to notify that this scan is active and not passive.
156 * In order to notify the FW of the number of SSIDs we wish to scan (including
157 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300158 * one for each SSID, and set the active bit (first). If the first SSID is
159 * already included in the probe template, so we need to set only
160 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 */
David Spinadel3cae0732014-12-29 15:43:48 +0200162static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
163 enum ieee80211_band band, int n_ssids)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100164{
David Spinadel3cae0732014-12-29 15:43:48 +0200165 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
166 return 10;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100167 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300168 return 20 + 3 * (n_ssids + 1);
169 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100170}
171
David Spinadel3cae0732014-12-29 15:43:48 +0200172static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
173 enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100174{
David Spinadel3cae0732014-12-29 15:43:48 +0200175 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
176 return 110;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100177 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
178}
179
Alexander Bondar8a110d92014-03-12 17:31:19 +0200180static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
181 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200182{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200183 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200184 int *global_cnt = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200185
David Spinadel1e2ebe02014-10-26 15:53:27 +0200186 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
187 mvmvif->phy_ctxt->id < MAX_PHYS)
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200188 *global_cnt += 1;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200189}
190
Luciano Coelho4b817052015-04-15 16:21:16 +0300191static void iwl_mvm_scan_calc_dwell(struct iwl_mvm *mvm,
192 struct ieee80211_vif *vif,
193 struct iwl_mvm_scan_params *params)
Alexander Bondar8a110d92014-03-12 17:31:19 +0200194{
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200195 int global_cnt = 0;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200196 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300197 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200198
199 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
200 IEEE80211_IFACE_ITER_NORMAL,
201 iwl_mvm_scan_condition_iterator,
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200202 &global_cnt);
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200203 if (!global_cnt)
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300204 goto not_bound;
205
David Spinadel2ce89cd2014-07-22 13:11:18 +0300206 params->suspend_time = 30;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200207 params->max_out_time = 120;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300208
209 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300210 if (mvm->fw->ucode_capa.api[0] &
211 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
212 params->suspend_time = 105;
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200213 /*
214 * If there is more than one active interface make
215 * passive scan more fragmented.
216 */
David Spinadelef177082015-02-15 14:45:33 +0200217 frag_passive_dwell = 40;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200218 params->max_out_time = frag_passive_dwell;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300219 } else {
220 params->suspend_time = 120;
221 params->max_out_time = 120;
222 }
223 }
224
225 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
226 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
227 /*
228 * P2P device scan should not be fragmented to avoid negative
229 * impact on P2P device discovery. Configure max_out_time to be
230 * equal to dwell time on passive channel. Take a longest
231 * possible value, one that corresponds to 2GHz band
232 */
233 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
234 u32 passive_dwell =
David Spinadel3cae0732014-12-29 15:43:48 +0200235 iwl_mvm_get_passive_dwell(mvm,
236 IEEE80211_BAND_2GHZ);
David Spinadel2ce89cd2014-07-22 13:11:18 +0300237 params->max_out_time = passive_dwell;
238 } else {
239 params->passive_fragmented = true;
240 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200241 }
242
Luciano Coelho4b817052015-04-15 16:21:16 +0300243 if ((params->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
Luciano Coelhoc1537662015-04-09 14:48:11 +0300244 (params->max_out_time > 200))
Johannes Bergab480032014-06-04 10:13:50 +0200245 params->max_out_time = 200;
246
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300247not_bound:
248
Alexander Bondar50df8a32014-03-12 20:30:51 +0200249 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300250 if (params->passive_fragmented)
David Spinadel190f1022015-02-17 12:45:21 +0200251 params->dwell[band].fragmented = frag_passive_dwell;
252
253 params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
254 band);
Luciano Coelho4b817052015-04-15 16:21:16 +0300255 params->dwell[band].active =
256 iwl_mvm_get_active_dwell(mvm, band, params->n_ssids);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200257 }
Luciano Coelho0616c622015-04-09 12:18:56 +0300258
259 IWL_DEBUG_SCAN(mvm,
260 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
261 params->max_out_time, params->suspend_time,
262 params->passive_fragmented);
263 IWL_DEBUG_SCAN(mvm,
264 "dwell[IEEE80211_BAND_2GHZ]: passive %d, active %d, fragmented %d\n",
265 params->dwell[IEEE80211_BAND_2GHZ].passive,
266 params->dwell[IEEE80211_BAND_2GHZ].active,
267 params->dwell[IEEE80211_BAND_2GHZ].fragmented);
268 IWL_DEBUG_SCAN(mvm,
269 "dwell[IEEE80211_BAND_5GHZ]: passive %d, active %d, fragmented %d\n",
270 params->dwell[IEEE80211_BAND_5GHZ].passive,
271 params->dwell[IEEE80211_BAND_5GHZ].active,
272 params->dwell[IEEE80211_BAND_5GHZ].fragmented);
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200273}
274
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300275static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
276{
277 /* require rrm scan whenever the fw supports it */
278 return mvm->fw->ucode_capa.capa[0] &
279 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
280}
281
Luciano Coelho999d2562015-03-27 10:28:26 +0300282static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300283{
284 int max_probe_len;
285
Luciano Coelho1f940382015-02-10 13:03:38 +0200286 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300287
288 /* we create the 802.11 header and SSID element */
289 max_probe_len -= 24 + 2;
290
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300291 /* DS parameter set element is added on 2.4GHZ band if required */
292 if (iwl_mvm_rrm_scan_needed(mvm))
293 max_probe_len -= 3;
294
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300295 return max_probe_len;
296}
297
Luciano Coelho999d2562015-03-27 10:28:26 +0300298int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300299{
Luciano Coelho999d2562015-03-27 10:28:26 +0300300 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300301
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300302 /* TODO: [BUG] This function should return the maximum allowed size of
303 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
304 * in the same command. So the correct implementation of this function
305 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
306 * command has only 512 bytes and it would leave us with about 240
307 * bytes for scan IEs, which is clearly not enough. So meanwhile
308 * we will report an incorrect value. This may result in a failure to
309 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
310 * functions with -ENOBUFS, if a large enough probe will be provided.
311 */
312 return max_ie_len;
313}
314
Alexander Bondare5d74642014-12-09 19:15:49 +0200315int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
316 struct iwl_rx_cmd_buffer *rxb,
317 struct iwl_device_cmd *cmd)
318{
319 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Avraham Stern1083fd72015-03-31 16:14:41 +0300320 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
Alexander Bondare5d74642014-12-09 19:15:49 +0200321
322 IWL_DEBUG_SCAN(mvm,
323 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
324 notif->status, notif->scanned_channels);
325 return 0;
326}
327
David Spinadelfb98be52014-05-04 12:51:10 +0300328int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
329 struct iwl_rx_cmd_buffer *rxb,
330 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300331{
David Spinadeld2496222014-05-20 12:46:37 +0300332 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
333 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300334
335 return 0;
336}
337
David Spinadel35a000b2013-08-28 09:29:43 +0300338int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
339 struct iwl_rx_cmd_buffer *rxb,
340 struct iwl_device_cmd *cmd)
341{
342 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200343 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
344 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
345 bool ebs_successful = (scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS);
David Spinadelfb98be52014-05-04 12:51:10 +0300346
Johannes Berga6623e82014-01-27 15:40:53 +0100347 /* scan status must be locked for proper checking */
348 lockdep_assert_held(&mvm->mutex);
349
Luciano Coelho9af91f42015-02-10 10:42:26 +0200350 /* We first check if we were stopping a scan, in which case we
351 * just clear the stopping flag. Then we check if it was a
352 * firmware initiated stop, in which case we need to inform
353 * mac80211.
354 * Note that we can have a stopping and a running scan
355 * simultaneously, but we can't have two different types of
356 * scans stopping or running at the same time (since LMAC
357 * doesn't support it).
358 */
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300359
Luciano Coelho9af91f42015-02-10 10:42:26 +0200360 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
361 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
David Spinadel35a000b2013-08-28 09:29:43 +0300362
Luciano Coelho9af91f42015-02-10 10:42:26 +0200363 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
364 aborted ? "aborted" : "completed",
365 ebs_successful ? "successful" : "failed");
366
367 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
368 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
369 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
370 aborted ? "aborted" : "completed",
371 ebs_successful ? "successful" : "failed");
372
373 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
374 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
375 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
376
377 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
378 aborted ? "aborted" : "completed",
379 ebs_successful ? "successful" : "failed");
380
381 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200382 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200383 } 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 ebs_successful ? "successful" : "failed");
387
388 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
David Spinadelfb98be52014-05-04 12:51:10 +0300389 ieee80211_scan_completed(mvm->hw,
Luciano Coelho1f940382015-02-10 13:03:38 +0200390 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300391 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200392 }
David Spinadel35a000b2013-08-28 09:29:43 +0300393
Luciano Coelho9af91f42015-02-10 10:42:26 +0200394 mvm->last_ebs_successful = ebs_successful;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300395
David Spinadel35a000b2013-08-28 09:29:43 +0300396 return 0;
397}
398
David Spinadel35a000b2013-08-28 09:29:43 +0300399static 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
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300413/* We insert the SSIDs in an inverted order, because the FW will
414 * invert it back.
415 */
416static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
417 struct iwl_ssid_ie *ssids,
418 u32 *ssid_bitmap)
David Spinadel35a000b2013-08-28 09:29:43 +0300419{
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 */
Luciano Coelho1c1b5b22015-04-02 16:31:42 +0300428 for (i = 0, j = params->n_match_sets - 1;
429 j >= 0 && i < PROBE_OPTION_MAX;
430 i++, j--) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100431 /* skip empty SSID matchsets */
Luciano Coelho1c1b5b22015-04-02 16:31:42 +0300432 if (!params->match_sets[j].ssid.ssid_len)
Johannes Bergea73cbc2014-01-24 10:53:53 +0100433 continue;
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300434 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);
David Spinadel35a000b2013-08-28 09:29:43 +0300438 }
439
440 /* add SSIDs from scan SSID list */
441 *ssid_bitmap = 0;
Luciano Coelho1c1b5b22015-04-02 16:31:42 +0300442 for (j = params->n_ssids - 1;
443 j >= 0 && i < PROBE_OPTION_MAX;
444 i++, j--) {
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300445 index = iwl_ssid_exist(params->ssids[j].ssid,
446 params->ssids[j].ssid_len,
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300447 ssids);
David Spinadel35a000b2013-08-28 09:29:43 +0300448 if (index < 0) {
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300449 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);
David Spinadel35a000b2013-08-28 09:29:43 +0300454 } else {
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300455 *ssid_bitmap |= BIT(index);
David Spinadel35a000b2013-08-28 09:29:43 +0300456 }
457 }
458}
459
David Spinadel35a000b2013-08-28 09:29:43 +0300460int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
461 struct cfg80211_sched_scan_request *req)
462{
463 struct iwl_scan_offload_profile *profile;
464 struct iwl_scan_offload_profile_cfg *profile_cfg;
465 struct iwl_scan_offload_blacklist *blacklist;
466 struct iwl_host_cmd cmd = {
467 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300468 .len[1] = sizeof(*profile_cfg),
469 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
470 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
471 };
472 int blacklist_len;
473 int i;
474 int ret;
475
476 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
477 return -EIO;
478
479 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
480 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
481 else
482 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
483
484 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
485 if (!blacklist)
486 return -ENOMEM;
487
488 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
489 if (!profile_cfg) {
490 ret = -ENOMEM;
491 goto free_blacklist;
492 }
493
494 cmd.data[0] = blacklist;
495 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
496 cmd.data[1] = profile_cfg;
497
498 /* No blacklist configuration */
499
500 profile_cfg->num_profiles = req->n_match_sets;
501 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
502 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
503 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200504 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
505 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300506
507 for (i = 0; i < req->n_match_sets; i++) {
508 profile = &profile_cfg->profiles[i];
509 profile->ssid_index = i;
510 /* Support any cipher and auth algorithm */
511 profile->unicast_cipher = 0xff;
512 profile->auth_alg = 0xff;
513 profile->network_type = IWL_NETWORK_TYPE_ANY;
514 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
515 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
516 }
517
518 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
519
520 ret = iwl_mvm_send_cmd(mvm, &cmd);
521 kfree(profile_cfg);
522free_blacklist:
523 kfree(blacklist);
524
525 return ret;
526}
527
David Spinadeld2496222014-05-20 12:46:37 +0300528static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
529 struct cfg80211_sched_scan_request *req)
530{
531 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
532 IWL_DEBUG_SCAN(mvm,
533 "Sending scheduled scan with filtering, n_match_sets %d\n",
534 req->n_match_sets);
535 return false;
536 }
537
538 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
539 return true;
540}
541
David Spinadelfb98be52014-05-04 12:51:10 +0300542static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +0300543{
544 int ret;
545 struct iwl_host_cmd cmd = {
546 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300547 };
548 u32 status;
549
550 /* Exit instantly with error when device is not ready
551 * to receive scan abort command or it does not perform
552 * scheduled scan currently */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200553 if (!mvm->scan_status)
David Spinadel35a000b2013-08-28 09:29:43 +0300554 return -EIO;
555
556 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
557 if (ret)
558 return ret;
559
560 if (status != CAN_ABORT_STATUS) {
561 /*
562 * The scan abort will return 1 for success or
563 * 2 for "failure". A failure condition can be
564 * due to simply not being in an active scan which
565 * can occur if we send the scan abort before the
566 * microcode has notified us that a scan is completed.
567 */
568 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200569 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +0300570 }
571
572 return ret;
573}
574
David Spinadelfb98be52014-05-04 12:51:10 +0300575int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +0300576{
577 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200578 struct iwl_notification_wait wait_scan_done;
579 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
Luciano Coelho9af91f42015-02-10 10:42:26 +0200580 bool sched = !!(mvm->scan_status & IWL_MVM_SCAN_SCHED);
David Spinadel35a000b2013-08-28 09:29:43 +0300581
582 lockdep_assert_held(&mvm->mutex);
583
David Spinadeld2496222014-05-20 12:46:37 +0300584 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
Luciano Coelho9345c592015-05-07 10:10:57 +0300585 return iwl_umac_scan_stop(mvm, IWL_MVM_SCAN_SCHED, notify);
David Spinadeld2496222014-05-20 12:46:37 +0300586
Luciano Coelho9af91f42015-02-10 10:42:26 +0200587 /* FIXME: For now we only check if no scan is set here, since
588 * we only support LMAC in this flow and it doesn't support
589 * multiple scans.
590 */
591 if (!mvm->scan_status)
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200592 return 0;
593
Luciano Coelho54331db2015-02-05 00:39:22 +0200594 if (iwl_mvm_is_radio_killed(mvm)) {
595 ret = 0;
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200596 goto out;
Luciano Coelho54331db2015-02-05 00:39:22 +0200597 }
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200598
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200599 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
600 scan_done_notif,
601 ARRAY_SIZE(scan_done_notif),
602 NULL, NULL);
603
David Spinadelfb98be52014-05-04 12:51:10 +0300604 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200605 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +0300606 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
607 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200608 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
Luciano Coelho54331db2015-02-05 00:39:22 +0200609 goto out;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200610 }
611
David Spinadelfb98be52014-05-04 12:51:10 +0300612 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
Luciano Coelho9af91f42015-02-10 10:42:26 +0200613 sched ? "scheduled " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200614
615 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
Luciano Coelho54331db2015-02-05 00:39:22 +0200616out:
Luciano Coelho9af91f42015-02-10 10:42:26 +0200617 /* Clear the scan status so the next scan requests will
618 * succeed and mark the scan as stopping, so that the Rx
619 * handler doesn't do anything, as the scan was stopped from
620 * above. Since the rx handler won't do anything now, we have
621 * to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200622 */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200623 if (mvm->scan_status == IWL_MVM_SCAN_REGULAR)
Eliad Peller4ff78182014-06-22 14:44:44 +0300624 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
625
Luciano Coelho9af91f42015-02-10 10:42:26 +0200626 if (sched) {
627 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
628 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_SCHED;
629 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300630 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200631 } else {
632 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
633 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_REGULAR;
634 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300635 ieee80211_scan_completed(mvm->hw, true);
636 }
David Spinadel636a2cd2014-05-01 15:57:22 +0300637
Luciano Coelho54331db2015-02-05 00:39:22 +0200638 return ret;
David Spinadel35a000b2013-08-28 09:29:43 +0300639}
David Spinadelfb98be52014-05-04 12:51:10 +0300640
Luciano Coelho65ff5562015-03-20 13:35:47 +0200641static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
642 struct iwl_scan_req_tx_cmd *tx_cmd,
643 bool no_cck)
David Spinadelfb98be52014-05-04 12:51:10 +0300644{
645 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
646 TX_CMD_FLG_BT_DIS);
647 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
648 IEEE80211_BAND_2GHZ,
649 no_cck);
650 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
651
652 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
653 TX_CMD_FLG_BT_DIS);
654 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
655 IEEE80211_BAND_5GHZ,
656 no_cck);
657 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
658}
659
660static void
661iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
662 struct ieee80211_channel **channels,
663 int n_channels, u32 ssid_bitmap,
Luciano Coelho65ff5562015-03-20 13:35:47 +0200664 struct iwl_scan_req_lmac *cmd)
David Spinadelfb98be52014-05-04 12:51:10 +0300665{
666 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
667 int i;
668
669 for (i = 0; i < n_channels; i++) {
670 channel_cfg[i].channel_num =
671 cpu_to_le16(channels[i]->hw_value);
672 channel_cfg[i].iter_count = cpu_to_le16(1);
673 channel_cfg[i].iter_interval = 0;
674 channel_cfg[i].flags =
675 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
676 ssid_bitmap);
677 }
678}
679
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300680static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
681 size_t len, u8 *const pos)
682{
683 static const u8 before_ds_params[] = {
684 WLAN_EID_SSID,
685 WLAN_EID_SUPP_RATES,
686 WLAN_EID_REQUEST,
687 WLAN_EID_EXT_SUPP_RATES,
688 };
689 size_t offs;
690 u8 *newpos = pos;
691
692 if (!iwl_mvm_rrm_scan_needed(mvm)) {
693 memcpy(newpos, ies, len);
694 return newpos + len;
695 }
696
697 offs = ieee80211_ie_split(ies, len,
698 before_ds_params,
699 ARRAY_SIZE(before_ds_params),
700 0);
701
702 memcpy(newpos, ies, offs);
703 newpos += offs;
704
705 /* Add a placeholder for DS Parameter Set element */
706 *newpos++ = WLAN_EID_DS_PARAMS;
707 *newpos++ = 1;
708 *newpos++ = 0;
709
710 memcpy(newpos, ies + offs, len - offs);
711 newpos += len - offs;
712
713 return newpos;
714}
715
David Spinadelfb98be52014-05-04 12:51:10 +0300716static void
Luciano Coelho65ff5562015-03-20 13:35:47 +0200717iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
718 struct ieee80211_scan_ies *ies,
Luciano Coelho45d1b122015-04-15 16:34:13 +0300719 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300720{
Luciano Coelho45d1b122015-04-15 16:34:13 +0300721 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300722 u8 *pos, *newpos;
Luciano Coelho45d1b122015-04-15 16:34:13 +0300723 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
724 params->mac_addr : NULL;
David Spinadelfb98be52014-05-04 12:51:10 +0300725
Johannes Bergeffd05a2014-11-18 17:21:19 +0100726 /*
727 * Unfortunately, right now the offload scan doesn't support randomising
728 * within the firmware, so until the firmware API is ready we implement
729 * it in the driver. This means that the scan iterations won't really be
730 * random, only when it's restarted, but at least that helps a bit.
731 */
732 if (mac_addr)
Luciano Coelho45d1b122015-04-15 16:34:13 +0300733 get_random_mask_addr(frame->sa, mac_addr,
734 params->mac_addr_mask);
Johannes Bergeffd05a2014-11-18 17:21:19 +0100735 else
736 memcpy(frame->sa, vif->addr, ETH_ALEN);
737
David Spinadelfb98be52014-05-04 12:51:10 +0300738 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
739 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +0300740 eth_broadcast_addr(frame->bssid);
741 frame->seq_ctrl = 0;
742
743 pos = frame->u.probe_req.variable;
744 *pos++ = WLAN_EID_SSID;
745 *pos++ = 0;
746
Luciano Coelho45d1b122015-04-15 16:34:13 +0300747 params->preq.mac_header.offset = 0;
748 params->preq.mac_header.len = cpu_to_le16(24 + 2);
David Spinadelfb98be52014-05-04 12:51:10 +0300749
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300750 /* Insert ds parameter set element on 2.4 GHz band */
751 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
752 ies->ies[IEEE80211_BAND_2GHZ],
753 ies->len[IEEE80211_BAND_2GHZ],
754 pos);
Luciano Coelho45d1b122015-04-15 16:34:13 +0300755 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
756 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300757 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +0300758
759 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
760 ies->len[IEEE80211_BAND_5GHZ]);
Luciano Coelho45d1b122015-04-15 16:34:13 +0300761 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
762 params->preq.band_data[1].len =
763 cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
David Spinadelfb98be52014-05-04 12:51:10 +0300764 pos += ies->len[IEEE80211_BAND_5GHZ];
765
766 memcpy(pos, ies->common_ies, ies->common_ie_len);
Luciano Coelho45d1b122015-04-15 16:34:13 +0300767 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
768 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
David Spinadelfb98be52014-05-04 12:51:10 +0300769}
770
Luciano Coelho133c8252015-04-20 15:29:03 +0300771static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
772 struct iwl_scan_req_lmac *cmd,
773 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300774{
David Spinadel2ce89cd2014-07-22 13:11:18 +0300775 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
776 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
777 if (params->passive_fragmented)
778 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +0200779 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadelfb98be52014-05-04 12:51:10 +0300780 cmd->max_out_time = cpu_to_le32(params->max_out_time);
781 cmd->suspend_time = cpu_to_le32(params->suspend_time);
782 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +0300783}
784
Luciano Coelho999d2562015-03-27 10:28:26 +0300785static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
786 struct ieee80211_scan_ies *ies,
787 int n_channels)
788{
789 return ((n_ssids <= PROBE_OPTION_MAX) &&
790 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
791 (ies->common_ie_len +
792 ies->len[NL80211_BAND_2GHZ] +
793 ies->len[NL80211_BAND_5GHZ] <=
794 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
795}
796
Luciano Coelho7d7de1e2015-04-17 09:53:55 +0300797static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm, int n_iterations)
798{
799 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
800
801 /* We can only use EBS if:
802 * 1. the feature is supported;
803 * 2. the last EBS was successful;
804 * 3. if only single scan, the single scan EBS API is supported.
805 */
806 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
807 mvm->last_ebs_successful &&
808 (n_iterations > 1 ||
809 (capa->api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS)));
810}
811
Luciano Coelho6f4dcc72015-04-20 14:37:16 +0300812static int iwl_mvm_scan_total_iterations(struct iwl_mvm_scan_params *params)
813{
814 return params->schedule[0].iterations + params->schedule[1].iterations;
815}
816
Luciano Coelho87515402015-04-20 14:47:49 +0300817static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
818 struct iwl_mvm_scan_params *params)
819{
820 int flags = 0;
821
822 if (params->n_ssids == 0)
823 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
824
825 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
826 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
827
828 if (params->passive_fragmented)
829 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
830
831 if (iwl_mvm_rrm_scan_needed(mvm))
832 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
833
834 if (params->pass_all)
835 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
836 else
837 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
838
839#ifdef CONFIG_IWLWIFI_DEBUGFS
840 if (mvm->scan_iter_notif_enabled)
841 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
842#endif
843
844 return flags;
845}
846
Luciano Coelho6749dd82015-03-20 15:51:36 +0200847static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Luciano Coelho3db7c6e2015-04-01 17:09:56 +0300848 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300849{
Luciano Coelho65ff5562015-03-20 13:35:47 +0200850 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
Luciano Coelho45d1b122015-04-15 16:34:13 +0300851 struct iwl_scan_probe_req *preq =
852 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
853 mvm->fw->ucode_capa.n_scan_channels);
Luciano Coelho87515402015-04-20 14:47:49 +0300854 u32 ssid_bitmap = 0;
Luciano Coelho6f4dcc72015-04-20 14:37:16 +0300855 int n_iterations = iwl_mvm_scan_total_iterations(params);
David Spinadelfb98be52014-05-04 12:51:10 +0300856
857 lockdep_assert_held(&mvm->mutex);
858
Luciano Coelho133c8252015-04-20 15:29:03 +0300859 memset(cmd, 0, ksize(cmd));
David Spinadelfb98be52014-05-04 12:51:10 +0300860
Luciano Coelho133c8252015-04-20 15:29:03 +0300861 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
862
863 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
864 cmd->iter_num = cpu_to_le32(1);
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300865 cmd->n_channels = (u8)params->n_channels;
David Spinadelfb98be52014-05-04 12:51:10 +0300866
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300867 cmd->delay = cpu_to_le32(params->delay);
Luciano Coelho2250fd92014-12-03 10:52:26 +0200868
Luciano Coelho87515402015-04-20 14:47:49 +0300869 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params));
David Spinadelfb98be52014-05-04 12:51:10 +0300870
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300871 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
David Spinadelfb98be52014-05-04 12:51:10 +0300872 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
873 MAC_FILTER_IN_BEACON);
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300874 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +0300875 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
876
877 /* this API uses bits 1-20 instead of 0-19 */
878 ssid_bitmap <<= 1;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300879
880 cmd->schedule[0].delay = cpu_to_le16(params->interval);
Luciano Coelhocf5d3172015-04-16 20:13:28 +0300881 cmd->schedule[0].iterations = params->schedule[0].iterations;
882 cmd->schedule[0].full_scan_mul = params->schedule[0].full_scan_mul;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300883 cmd->schedule[1].delay = cpu_to_le16(params->interval);
Luciano Coelhocf5d3172015-04-16 20:13:28 +0300884 cmd->schedule[1].iterations = params->schedule[1].iterations;
885 cmd->schedule[1].full_scan_mul = params->schedule[1].iterations;
David Spinadelfb98be52014-05-04 12:51:10 +0300886
Luciano Coelho7d7de1e2015-04-17 09:53:55 +0300887 if (iwl_mvm_scan_use_ebs(mvm, n_iterations)) {
David Spinadel1f9c4182014-12-01 23:30:07 +0200888 cmd->channel_opt[0].flags =
889 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
890 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
891 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
892 cmd->channel_opt[0].non_ebs_ratio =
893 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
894 cmd->channel_opt[1].flags =
895 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
896 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
897 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
898 cmd->channel_opt[1].non_ebs_ratio =
899 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
900 }
901
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300902 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
903 params->n_channels, ssid_bitmap, cmd);
David Spinadelfb98be52014-05-04 12:51:10 +0300904
Luciano Coelho45d1b122015-04-15 16:34:13 +0300905 *preq = params->preq;
David Spinadelfb98be52014-05-04 12:51:10 +0300906
Luciano Coelho8df3e682015-04-01 17:37:44 +0300907 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300908}
909
David Spinadelfb98be52014-05-04 12:51:10 +0300910int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
911{
David Spinadeld2496222014-05-20 12:46:37 +0300912 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
Luciano Coelho9345c592015-05-07 10:10:57 +0300913 return iwl_umac_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
David Spinadeld2496222014-05-20 12:46:37 +0300914
Luciano Coelho9af91f42015-02-10 10:42:26 +0200915 if (!(mvm->scan_status & IWL_MVM_SCAN_REGULAR))
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +0200916 return 0;
917
918 if (iwl_mvm_is_radio_killed(mvm)) {
919 ieee80211_scan_completed(mvm->hw, true);
920 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200921 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +0200922 return 0;
923 }
924
Luciano Coelho1f940382015-02-10 13:03:38 +0200925 return iwl_mvm_scan_offload_stop(mvm, true);
David Spinadelfb98be52014-05-04 12:51:10 +0300926}
David Spinadeld2496222014-05-20 12:46:37 +0300927
928/* UMAC scan API */
929
930struct iwl_umac_scan_done {
931 struct iwl_mvm *mvm;
Luciano Coelho9345c592015-05-07 10:10:57 +0300932 int type;
David Spinadeld2496222014-05-20 12:46:37 +0300933};
934
935static int rate_to_scan_rate_flag(unsigned int rate)
936{
937 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
938 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
939 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
940 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
941 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
942 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
943 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
944 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
945 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
946 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
947 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
948 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
949 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
950 };
951
952 return rate_to_scan_rate[rate];
953}
954
955static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
956{
957 struct ieee80211_supported_band *band;
958 unsigned int rates = 0;
959 int i;
960
961 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
962 for (i = 0; i < band->n_bitrates; i++)
963 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
964 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
965 for (i = 0; i < band->n_bitrates; i++)
966 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
967
968 /* Set both basic rates and supported rates */
969 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
970
971 return cpu_to_le32(rates);
972}
973
974int iwl_mvm_config_scan(struct iwl_mvm *mvm)
975{
976
977 struct iwl_scan_config *scan_config;
978 struct ieee80211_supported_band *band;
979 int num_channels =
980 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
981 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
982 int ret, i, j = 0, cmd_size, data_size;
983 struct iwl_host_cmd cmd = {
984 .id = SCAN_CFG_CMD,
985 };
986
987 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
988 return -ENOBUFS;
989
990 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
991
992 scan_config = kzalloc(cmd_size, GFP_KERNEL);
993 if (!scan_config)
994 return -ENOMEM;
995
996 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
997 scan_config->hdr.size = cpu_to_le16(data_size);
998 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
999 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1000 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1001 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1002 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1003 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1004 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1005 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1006 SCAN_CONFIG_N_CHANNELS(num_channels));
Moshe Harela0544272014-12-08 21:13:14 +02001007 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
David Spinadeld2496222014-05-20 12:46:37 +03001008 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1009 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1010 scan_config->out_of_channel_time = cpu_to_le32(170);
1011 scan_config->suspend_time = cpu_to_le32(30);
1012 scan_config->dwell_active = 20;
1013 scan_config->dwell_passive = 110;
1014 scan_config->dwell_fragmented = 20;
1015
1016 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1017
1018 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1019 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1020 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1021 IWL_CHANNEL_FLAG_EBS_ADD |
1022 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1023
1024 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1025 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001026 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001027 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1028 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001029 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001030
1031 cmd.data[0] = scan_config;
1032 cmd.len[0] = cmd_size;
1033 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1034
1035 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1036
1037 ret = iwl_mvm_send_cmd(mvm, &cmd);
1038
1039 kfree(scan_config);
1040 return ret;
1041}
1042
1043static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1044{
1045 int i;
1046
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001047 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001048 if (mvm->scan_uid[i] == uid)
1049 return i;
1050
1051 return i;
1052}
1053
1054static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1055{
1056 return iwl_mvm_find_scan_uid(mvm, 0);
1057}
1058
Luciano Coelho9345c592015-05-07 10:10:57 +03001059static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm, int type)
David Spinadeld2496222014-05-20 12:46:37 +03001060{
1061 int i;
1062
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001063 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001064 if (mvm->scan_uid[i] & type)
1065 return true;
1066
1067 return false;
1068}
1069
Luciano Coelho9345c592015-05-07 10:10:57 +03001070static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm, int type)
Alexander Bondar963221b2015-03-26 11:07:35 +02001071{
1072 int i;
1073
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001074 for (i = 0; i < mvm->max_scans; i++)
Alexander Bondar963221b2015-03-26 11:07:35 +02001075 if (mvm->scan_uid[i] & type)
1076 return i;
1077
1078 return i;
1079}
1080
Luciano Coelho9345c592015-05-07 10:10:57 +03001081static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm, int type)
David Spinadeld2496222014-05-20 12:46:37 +03001082{
1083 u32 uid;
1084
1085 /* make sure exactly one bit is on in scan type */
1086 WARN_ON(hweight8(type) != 1);
1087
1088 /*
1089 * Make sure scan uids are unique. If one scan lasts long time while
1090 * others are completing frequently, the seq number will wrap up and
1091 * we may have more than one scan with the same uid.
1092 */
1093 do {
1094 uid = type | (mvm->scan_seq_num <<
1095 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1096 mvm->scan_seq_num++;
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001097 } while (iwl_mvm_find_scan_uid(mvm, uid) < mvm->max_scans);
David Spinadeld2496222014-05-20 12:46:37 +03001098
1099 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1100
1101 return uid;
1102}
1103
Luciano Coelho133c8252015-04-20 15:29:03 +03001104static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
David Spinadeld2496222014-05-20 12:46:37 +03001105 struct iwl_scan_req_umac *cmd,
1106 struct iwl_mvm_scan_params *params)
1107{
David Spinadeld2496222014-05-20 12:46:37 +03001108 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1109 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1110 if (params->passive_fragmented)
1111 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +02001112 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadeld2496222014-05-20 12:46:37 +03001113 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1114 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1115 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
Luciano Coelho133c8252015-04-20 15:29:03 +03001116
1117 if (iwl_mvm_scan_total_iterations(params) == 0)
1118 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1119 else
1120 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
David Spinadeld2496222014-05-20 12:46:37 +03001121}
1122
1123static void
1124iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1125 struct ieee80211_channel **channels,
1126 int n_channels, u32 ssid_bitmap,
1127 struct iwl_scan_req_umac *cmd)
1128{
1129 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1130 int i;
1131
1132 for (i = 0; i < n_channels; i++) {
1133 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1134 channel_cfg[i].channel_num = channels[i]->hw_value;
1135 channel_cfg[i].iter_count = 1;
1136 channel_cfg[i].iter_interval = 0;
1137 }
1138}
1139
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001140static u32 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1141 struct iwl_mvm_scan_params *params)
David Spinadeld2496222014-05-20 12:46:37 +03001142{
1143 int flags = 0;
1144
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001145 if (params->n_ssids == 0)
David Spinadeld2496222014-05-20 12:46:37 +03001146 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1147
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001148 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
David Spinadeld2496222014-05-20 12:46:37 +03001149 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1150
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001151 if (params->passive_fragmented)
David Spinadeld2496222014-05-20 12:46:37 +03001152 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1153
1154 if (iwl_mvm_rrm_scan_needed(mvm))
1155 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1156
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001157 if (params->pass_all)
1158 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1159 else
1160 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1161
1162 if (iwl_mvm_scan_total_iterations(params) > 1)
1163 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1164
David Spinadeld2496222014-05-20 12:46:37 +03001165 return flags;
1166}
1167
Luciano Coelho6749dd82015-03-20 15:51:36 +02001168static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001169 struct iwl_mvm_scan_params *params)
David Spinadeld2496222014-05-20 12:46:37 +03001170{
David Spinadeld2496222014-05-20 12:46:37 +03001171 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1172 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1173 sizeof(struct iwl_scan_channel_cfg_umac) *
1174 mvm->fw->ucode_capa.n_scan_channels;
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001175 u32 uid;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001176 u32 ssid_bitmap = 0;
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001177 int n_iterations = iwl_mvm_scan_total_iterations(params);
Luciano Coelho5ef766f2015-04-02 15:37:05 +03001178 int uid_idx;
David Spinadeld2496222014-05-20 12:46:37 +03001179
1180 lockdep_assert_held(&mvm->mutex);
1181
1182 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001183 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001184 return -EBUSY;
1185
Luciano Coelho133c8252015-04-20 15:29:03 +03001186 memset(cmd, 0, ksize(cmd));
1187 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1188 sizeof(struct iwl_mvm_umac_cmd_hdr));
David Spinadeld2496222014-05-20 12:46:37 +03001189
Luciano Coelho133c8252015-04-20 15:29:03 +03001190 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1191
1192 if (n_iterations == 1)
Luciano Coelho9345c592015-05-07 10:10:57 +03001193 uid = iwl_generate_scan_uid(mvm, IWL_MVM_SCAN_REGULAR);
Luciano Coelho133c8252015-04-20 15:29:03 +03001194 else
Luciano Coelho9345c592015-05-07 10:10:57 +03001195 uid = iwl_generate_scan_uid(mvm, IWL_MVM_SCAN_SCHED);
Luciano Coelho8423cdc2015-04-17 14:49:58 +03001196
1197 mvm->scan_uid[uid_idx] = uid;
1198 cmd->uid = cpu_to_le32(uid);
1199
Luciano Coelho6f4dcc72015-04-20 14:37:16 +03001200 cmd->general_flags = cpu_to_le32(iwl_mvm_scan_umac_flags(mvm, params));
Haim Dreyfuss4db65582015-01-05 15:00:35 +02001201
Luciano Coelho8423cdc2015-04-17 14:49:58 +03001202 if (iwl_mvm_scan_use_ebs(mvm, n_iterations))
Haim Dreyfuss4db65582015-01-05 15:00:35 +02001203 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1204 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1205 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1206
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001207 cmd->n_channels = params->n_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001208
Luciano Coelhoe2ec4f62015-04-02 17:49:04 +03001209 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
David Spinadeld2496222014-05-20 12:46:37 +03001210
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001211 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1212 params->n_channels, ssid_bitmap, cmd);
David Spinadeld2496222014-05-20 12:46:37 +03001213
Luciano Coelhoa54cb642015-05-25 09:36:53 +03001214 /* With UMAC we use only one schedule for now, so use the sum
1215 * of the iterations (with a a maximum of 255).
Luciano Coelho8423cdc2015-04-17 14:49:58 +03001216 */
1217 sec_part->schedule[0].iter_count =
1218 (n_iterations > 255) ? 255 : n_iterations;
Luciano Coelhocf5d3172015-04-16 20:13:28 +03001219 sec_part->schedule[0].interval = cpu_to_le16(params->interval);
1220
Luciano Coelho283c2492015-04-17 11:18:46 +03001221 sec_part->delay = cpu_to_le16(params->delay);
Luciano Coelho45d1b122015-04-15 16:34:13 +03001222 sec_part->preq = params->preq;
David Spinadeld2496222014-05-20 12:46:37 +03001223
Luciano Coelho8df3e682015-04-01 17:37:44 +03001224 return 0;
David Spinadeld2496222014-05-20 12:46:37 +03001225}
1226
Luciano Coelho6749dd82015-03-20 15:51:36 +02001227static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1228{
1229 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1230}
1231
1232static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1233{
1234 /* This looks a bit arbitrary, but the idea is that if we run
1235 * out of possible simultaneous scans and the userspace is
1236 * trying to run a scan type that is already running, we
1237 * return -EBUSY. But if the userspace wants to start a
1238 * different type of scan, we stop the opposite type to make
1239 * space for the new request. The reason is backwards
1240 * compatibility with old wpa_supplicant that wouldn't stop a
1241 * scheduled scan before starting a normal scan.
1242 */
1243
1244 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1245 return 0;
1246
1247 /* Use a switch, even though this is a bitmask, so that more
1248 * than one bits set will fall in default and we will warn.
1249 */
1250 switch (type) {
1251 case IWL_MVM_SCAN_REGULAR:
1252 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1253 return -EBUSY;
1254 return iwl_mvm_scan_offload_stop(mvm, true);
1255 case IWL_MVM_SCAN_SCHED:
1256 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1257 return -EBUSY;
1258 return iwl_mvm_cancel_scan(mvm);
Luciano Coelho19945df2015-03-20 16:11:28 +02001259 case IWL_MVM_SCAN_NETDETECT:
1260 /* No need to stop anything for net-detect since the
1261 * firmware is restarted anyway. This way, any sched
1262 * scans that were running will be restarted when we
1263 * resume.
1264 */
1265 return 0;
Luciano Coelho6749dd82015-03-20 15:51:36 +02001266 default:
1267 WARN_ON(1);
1268 break;
1269 }
1270
1271 return -EIO;
1272}
1273
1274int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1275 struct cfg80211_scan_request *req,
1276 struct ieee80211_scan_ies *ies)
1277{
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001278 struct iwl_host_cmd hcmd = {
1279 .len = { iwl_mvm_scan_size(mvm), },
1280 .data = { mvm->scan_cmd, },
1281 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1282 };
1283 struct iwl_mvm_scan_params params = {};
Luciano Coelho6749dd82015-03-20 15:51:36 +02001284 int ret;
1285
1286 lockdep_assert_held(&mvm->mutex);
1287
1288 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1289 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1290 return -EBUSY;
1291 }
1292
1293 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1294 if (ret)
1295 return ret;
1296
1297 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1298
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001299 /* we should have failed registration if scan_cmd was NULL */
1300 if (WARN_ON(!mvm->scan_cmd))
1301 return -ENOMEM;
1302
1303 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1304 return -ENOBUFS;
1305
Luciano Coelho4b817052015-04-15 16:21:16 +03001306 params.n_ssids = req->n_ssids;
1307 params.flags = req->flags;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001308 params.n_channels = req->n_channels;
1309 params.delay = 0;
1310 params.interval = 0;
1311 params.ssids = req->ssids;
1312 params.channels = req->channels;
1313 params.mac_addr = req->mac_addr;
1314 params.mac_addr_mask = req->mac_addr_mask;
1315 params.no_cck = req->no_cck;
1316 params.pass_all = true;
1317 params.n_match_sets = 0;
1318 params.match_sets = NULL;
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001319
Luciano Coelhocf5d3172015-04-16 20:13:28 +03001320 params.schedule[0].iterations = 1;
1321 params.schedule[0].full_scan_mul = 0;
1322 params.schedule[1].iterations = 0;
1323 params.schedule[1].full_scan_mul = 0;
1324
Luciano Coelho4b817052015-04-15 16:21:16 +03001325 iwl_mvm_scan_calc_dwell(mvm, vif, &params);
1326
Luciano Coelho45d1b122015-04-15 16:34:13 +03001327 iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1328
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001329 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1330 hcmd.id = SCAN_REQ_UMAC;
Luciano Coelho45d1b122015-04-15 16:34:13 +03001331 ret = iwl_mvm_scan_umac(mvm, vif, &params);
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001332 } else {
1333 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
Luciano Coelho45d1b122015-04-15 16:34:13 +03001334 ret = iwl_mvm_scan_lmac(mvm, vif, &params);
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001335 }
1336
1337 if (ret)
1338 return ret;
1339
1340 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1341 if (!ret) {
1342 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1343 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1344 } else {
1345 /* If the scan failed, it usually means that the FW was unable
1346 * to allocate the time events. Warn on it, but maybe we
1347 * should try to send the command again with different params.
1348 */
1349 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1350 }
Luciano Coelho6749dd82015-03-20 15:51:36 +02001351
1352 if (ret)
1353 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1354
1355 return ret;
1356}
1357
Luciano Coelho65ff5562015-03-20 13:35:47 +02001358int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1359 struct ieee80211_vif *vif,
1360 struct cfg80211_sched_scan_request *req,
Luciano Coelho19945df2015-03-20 16:11:28 +02001361 struct ieee80211_scan_ies *ies,
1362 int type)
Luciano Coelho65ff5562015-03-20 13:35:47 +02001363{
Luciano Coelho8df3e682015-04-01 17:37:44 +03001364 struct iwl_host_cmd hcmd = {
1365 .len = { iwl_mvm_scan_size(mvm), },
1366 .data = { mvm->scan_cmd, },
1367 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1368 };
1369 struct iwl_mvm_scan_params params = {};
Luciano Coelho65ff5562015-03-20 13:35:47 +02001370 int ret;
1371
Luciano Coelho6749dd82015-03-20 15:51:36 +02001372 lockdep_assert_held(&mvm->mutex);
1373
1374 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1375 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1376 return -EBUSY;
1377 }
1378
Luciano Coelho19945df2015-03-20 16:11:28 +02001379 ret = iwl_mvm_check_running_scans(mvm, type);
Luciano Coelho6749dd82015-03-20 15:51:36 +02001380 if (ret)
1381 return ret;
1382
Luciano Coelho8df3e682015-04-01 17:37:44 +03001383 /* we should have failed registration if scan_cmd was NULL */
1384 if (WARN_ON(!mvm->scan_cmd))
1385 return -ENOMEM;
1386
1387 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1388 return -ENOBUFS;
1389
Luciano Coelho4b817052015-04-15 16:21:16 +03001390 params.n_ssids = req->n_ssids;
1391 params.flags = req->flags;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001392 params.n_channels = req->n_channels;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001393 params.ssids = req->ssids;
1394 params.channels = req->channels;
1395 params.mac_addr = req->mac_addr;
1396 params.mac_addr_mask = req->mac_addr_mask;
1397 params.no_cck = false;
1398 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1399 params.n_match_sets = req->n_match_sets;
1400 params.match_sets = req->match_sets;
1401
Luciano Coelhocf5d3172015-04-16 20:13:28 +03001402 params.schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1403 params.schedule[0].full_scan_mul = 1;
1404 params.schedule[1].iterations = 0xff;
1405 params.schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1406
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001407 if (req->interval > U16_MAX) {
1408 IWL_DEBUG_SCAN(mvm,
1409 "interval value is > 16-bits, set to max possible\n");
1410 params.interval = U16_MAX;
1411 } else {
1412 params.interval = req->interval / MSEC_PER_SEC;
1413 }
Luciano Coelho8df3e682015-04-01 17:37:44 +03001414
Luciano Coelho283c2492015-04-17 11:18:46 +03001415 /* In theory, LMAC scans can handle a 32-bit delay, but since
1416 * waiting for over 18 hours to start the scan is a bit silly
1417 * and to keep it aligned with UMAC scans (which only support
1418 * 16-bit delays), trim it down to 16-bits.
1419 */
1420 if (req->delay > U16_MAX) {
1421 IWL_DEBUG_SCAN(mvm,
1422 "delay value is > 16-bits, set to max possible\n");
1423 params.delay = U16_MAX;
1424 } else {
1425 params.delay = req->delay;
1426 }
1427
Luciano Coelho4b817052015-04-15 16:21:16 +03001428 iwl_mvm_scan_calc_dwell(mvm, vif, &params);
1429
Luciano Coelho65ff5562015-03-20 13:35:47 +02001430 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1431 if (ret)
1432 return ret;
1433
Luciano Coelho45d1b122015-04-15 16:34:13 +03001434 iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1435
Luciano Coelho65ff5562015-03-20 13:35:47 +02001436 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
Luciano Coelho8df3e682015-04-01 17:37:44 +03001437 hcmd.id = SCAN_REQ_UMAC;
Luciano Coelho8423cdc2015-04-17 14:49:58 +03001438 ret = iwl_mvm_scan_umac(mvm, vif, &params);
Luciano Coelho65ff5562015-03-20 13:35:47 +02001439 } else {
Luciano Coelho8df3e682015-04-01 17:37:44 +03001440 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
Luciano Coelho7d7de1e2015-04-17 09:53:55 +03001441 ret = iwl_mvm_scan_lmac(mvm, vif, &params);
Luciano Coelho8df3e682015-04-01 17:37:44 +03001442 }
1443
1444 if (ret)
1445 return ret;
1446
1447 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1448 if (!ret) {
1449 IWL_DEBUG_SCAN(mvm,
1450 "Sched scan request was sent successfully\n");
1451 mvm->scan_status |= type;
1452 } else {
1453 /* If the scan failed, it usually means that the FW was unable
1454 * to allocate the time events. Warn on it, but maybe we
1455 * should try to send the command again with different params.
1456 */
1457 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
Luciano Coelho65ff5562015-03-20 13:35:47 +02001458 }
1459
1460 return ret;
1461}
1462
David Spinadeld2496222014-05-20 12:46:37 +03001463int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1464 struct iwl_rx_cmd_buffer *rxb,
1465 struct iwl_device_cmd *cmd)
1466{
1467 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1468 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1469 u32 uid = __le32_to_cpu(notif->uid);
Luciano Coelho9345c592015-05-07 10:10:57 +03001470 bool sched = !!(uid & IWL_MVM_SCAN_SCHED);
David Spinadeld2496222014-05-20 12:46:37 +03001471 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1472
David Spinadel2992a322014-11-10 11:16:53 +02001473 /*
1474 * Scan uid may be set to zero in case of scan abort request from above.
1475 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001476 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001477 return 0;
1478
1479 IWL_DEBUG_SCAN(mvm,
1480 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1481 uid, sched ? "sched" : "regular",
1482 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1483 "completed" : "aborted",
1484 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1485 "success" : "failed");
1486
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001487 if (notif->ebs_status)
1488 mvm->last_ebs_successful = false;
1489
David Spinadeld2496222014-05-20 12:46:37 +03001490 mvm->scan_uid[uid_idx] = 0;
1491
1492 if (!sched) {
1493 ieee80211_scan_completed(mvm->hw,
1494 notif->status ==
1495 IWL_SCAN_OFFLOAD_ABORTED);
1496 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Luciano Coelho9345c592015-05-07 10:10:57 +03001497 } else if (!iwl_mvm_find_scan_type(mvm, IWL_MVM_SCAN_SCHED)) {
David Spinadeld2496222014-05-20 12:46:37 +03001498 ieee80211_sched_scan_stopped(mvm->hw);
1499 } else {
1500 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1501 }
1502
1503 return 0;
1504}
1505
1506static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1507 struct iwl_rx_packet *pkt, void *data)
1508{
1509 struct iwl_umac_scan_done *scan_done = data;
1510 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1511 u32 uid = __le32_to_cpu(notif->uid);
1512 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1513
1514 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1515 return false;
1516
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001517 if (uid_idx >= scan_done->mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001518 return false;
1519
1520 /*
1521 * Clear scan uid of scans that was aborted from above and completed
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001522 * in FW so the RX handler does nothing. Set last_ebs_successful here if
1523 * needed.
David Spinadeld2496222014-05-20 12:46:37 +03001524 */
1525 scan_done->mvm->scan_uid[uid_idx] = 0;
1526
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001527 if (notif->ebs_status)
1528 scan_done->mvm->last_ebs_successful = false;
1529
David Spinadeld2496222014-05-20 12:46:37 +03001530 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
1531}
1532
1533static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
1534{
1535 struct iwl_umac_scan_abort cmd = {
1536 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
1537 sizeof(struct iwl_mvm_umac_cmd_hdr)),
1538 .uid = cpu_to_le32(uid),
1539 };
1540
1541 lockdep_assert_held(&mvm->mutex);
1542
1543 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1544
1545 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
1546}
1547
Luciano Coelho9345c592015-05-07 10:10:57 +03001548static int iwl_umac_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
David Spinadeld2496222014-05-20 12:46:37 +03001549{
1550 struct iwl_notification_wait wait_scan_done;
1551 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
1552 struct iwl_umac_scan_done scan_done = {
1553 .mvm = mvm,
1554 .type = type,
1555 };
1556 int i, ret = -EIO;
1557
1558 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1559 scan_done_notif,
1560 ARRAY_SIZE(scan_done_notif),
1561 iwl_scan_umac_done_check, &scan_done);
1562
1563 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1564
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001565 for (i = 0; i < mvm->max_scans; i++) {
David Spinadeld2496222014-05-20 12:46:37 +03001566 if (mvm->scan_uid[i] & type) {
1567 int err;
1568
1569 if (iwl_mvm_is_radio_killed(mvm) &&
Luciano Coelho9345c592015-05-07 10:10:57 +03001570 (type & IWL_MVM_SCAN_REGULAR)) {
David Spinadeld2496222014-05-20 12:46:37 +03001571 ieee80211_scan_completed(mvm->hw, true);
1572 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1573 break;
1574 }
1575
1576 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
1577 if (!err)
1578 ret = 0;
1579 }
1580 }
1581
1582 if (ret) {
1583 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
1584 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1585 return ret;
1586 }
1587
1588 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1589 if (ret)
1590 return ret;
1591
1592 if (notify) {
Luciano Coelho9345c592015-05-07 10:10:57 +03001593 if (type & IWL_MVM_SCAN_SCHED)
David Spinadeld2496222014-05-20 12:46:37 +03001594 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9345c592015-05-07 10:10:57 +03001595 if (type & IWL_MVM_SCAN_REGULAR) {
David Spinadeld2496222014-05-20 12:46:37 +03001596 ieee80211_scan_completed(mvm->hw, true);
1597 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1598 }
1599 }
1600
1601 return ret;
1602}
1603
1604int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1605{
1606 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1607 return sizeof(struct iwl_scan_req_umac) +
1608 sizeof(struct iwl_scan_channel_cfg_umac) *
1609 mvm->fw->ucode_capa.n_scan_channels +
1610 sizeof(struct iwl_scan_req_umac_tail);
1611
Luciano Coelho65ff5562015-03-20 13:35:47 +02001612 return sizeof(struct iwl_scan_req_lmac) +
Luciano Coelho1f940382015-02-10 13:03:38 +02001613 sizeof(struct iwl_scan_channel_cfg_lmac) *
1614 mvm->fw->ucode_capa.n_scan_channels +
1615 sizeof(struct iwl_scan_probe_req);
David Spinadeld2496222014-05-20 12:46:37 +03001616}
David Spinadel4ffb3652015-03-10 10:06:02 +02001617
1618/*
1619 * This function is used in nic restart flow, to inform mac80211 about scans
1620 * that was aborted by restart flow or by an assert.
1621 */
1622void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1623{
1624 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001625 u32 uid, i;
1626
Luciano Coelho9345c592015-05-07 10:10:57 +03001627 uid = iwl_mvm_find_first_scan(mvm, IWL_MVM_SCAN_REGULAR);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001628 if (uid < mvm->max_scans) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001629 ieee80211_scan_completed(mvm->hw, true);
Alexander Bondar963221b2015-03-26 11:07:35 +02001630 mvm->scan_uid[uid] = 0;
1631 }
Luciano Coelho9345c592015-05-07 10:10:57 +03001632 uid = iwl_mvm_find_first_scan(mvm, IWL_MVM_SCAN_SCHED);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001633 if (uid < mvm->max_scans && !mvm->restart_fw) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001634 ieee80211_sched_scan_stopped(mvm->hw);
Alexander Bondar963221b2015-03-26 11:07:35 +02001635 mvm->scan_uid[uid] = 0;
1636 }
1637
1638 /* We shouldn't have any UIDs still set. Loop over all the
1639 * UIDs to make sure there's nothing left there and warn if
1640 * any is found.
1641 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001642 for (i = 0; i < mvm->max_scans; i++) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001643 if (WARN_ONCE(mvm->scan_uid[i],
1644 "UMAC scan UID %d was not cleaned\n",
1645 mvm->scan_uid[i]))
1646 mvm->scan_uid[i] = 0;
1647 }
David Spinadel4ffb3652015-03-10 10:06:02 +02001648 } else {
Luciano Coelho9af91f42015-02-10 10:42:26 +02001649 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
David Spinadel4ffb3652015-03-10 10:06:02 +02001650 ieee80211_scan_completed(mvm->hw, true);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001651
1652 /* Sched scan will be restarted by mac80211 in
1653 * restart_hw, so do not report if FW is about to be
1654 * restarted.
1655 */
1656 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1657 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadel4ffb3652015-03-10 10:06:02 +02001658 }
1659}