blob: 551f66d62045aa5ed8ee08343d209c27d3059c86 [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"
70#include "iwl-eeprom-parse.h"
71#include "fw-api-scan.h"
72
73#define IWL_PLCP_QUIET_THRESH 1
74#define IWL_ACTIVE_QUIET_TIME 10
Haim Dreyfussa1ed4022014-12-01 23:30:07 +020075#define IWL_DENSE_EBS_SCAN_RATIO 5
76#define IWL_SPARSE_EBS_SCAN_RATIO 1
Alexander Bondar8a110d92014-03-12 17:31:19 +020077
78struct iwl_mvm_scan_params {
79 u32 max_out_time;
80 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020081 bool passive_fragmented;
Luciano Coelhof7b788b2015-04-02 00:08:35 +030082 u32 n_channels;
83 u32 delay;
84 int n_ssids;
85 struct cfg80211_ssid *ssids;
86 struct ieee80211_channel **channels;
87 u16 interval; /* interval between scans (in secs) */
88 u32 flags;
89 u8 *mac_addr;
90 u8 *mac_addr_mask;
91 bool no_cck;
92 bool pass_all;
93 int n_match_sets;
94 struct cfg80211_match_set *match_sets;
Alexander Bondar50df8a32014-03-12 20:30:51 +020095 struct _dwell {
96 u16 passive;
97 u16 active;
David Spinadel190f1022015-02-17 12:45:21 +020098 u16 fragmented;
Alexander Bondar50df8a32014-03-12 20:30:51 +020099 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +0200100};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101
David Spinadeld2496222014-05-20 12:46:37 +0300102enum iwl_umac_scan_uid_type {
103 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
104 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
105 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
106 IWL_UMAC_SCAN_UID_SCHED_SCAN,
107};
108
109static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
110 enum iwl_umac_scan_uid_type type, bool notify);
111
112static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
113{
114 if (mvm->scan_rx_ant != ANT_NONE)
115 return mvm->scan_rx_ant;
Moshe Harela0544272014-12-08 21:13:14 +0200116 return iwl_mvm_get_valid_rx_ant(mvm);
David Spinadeld2496222014-05-20 12:46:37 +0300117}
118
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
120{
121 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300122 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100123
David Spinadeld2496222014-05-20 12:46:37 +0300124 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100125 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
126 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
127 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
128 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
129 return cpu_to_le16(rx_chain);
130}
131
David Spinadelfb98be52014-05-04 12:51:10 +0300132static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100133{
David Spinadelfb98be52014-05-04 12:51:10 +0300134 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100135 return cpu_to_le32(PHY_BAND_24);
136 else
137 return cpu_to_le32(PHY_BAND_5);
138}
139
140static inline __le32
141iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
142 bool no_cck)
143{
144 u32 tx_ant;
145
146 mvm->scan_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200147 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100148 mvm->scan_last_antenna_idx);
149 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
150
151 if (band == IEEE80211_BAND_2GHZ && !no_cck)
152 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
153 tx_ant);
154 else
155 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
156}
157
158/*
159 * We insert the SSIDs in an inverted order, because the FW will
160 * invert it back. The most prioritized SSID, which is first in the
161 * request list, is not copied here, but inserted directly to the probe
162 * request.
163 */
David Spinadelfb98be52014-05-04 12:51:10 +0300164static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
Luciano Coelho5ef766f2015-04-02 15:37:05 +0300165 struct iwl_mvm_scan_params *params,
166 u32 *ssid_bitmap)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100167{
Luciano Coelho5ef766f2015-04-02 15:37:05 +0300168 int fw_idx, req_idx, i;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100169
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300170 for (req_idx = params->n_ssids - 1, fw_idx = 0; req_idx >= 0;
David Spinadelfe04e832013-07-04 15:17:48 +0300171 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300172 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300173 cmd_ssid[fw_idx].len = params->ssids[req_idx].ssid_len;
David Spinadelfb98be52014-05-04 12:51:10 +0300174 memcpy(cmd_ssid[fw_idx].ssid,
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300175 params->ssids[req_idx].ssid,
176 params->ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100177 }
Luciano Coelho5ef766f2015-04-02 15:37:05 +0300178
179 for (i = 0; i < params->n_ssids; i++)
180 *ssid_bitmap |= BIT(i);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100181}
182
183/*
184 * If req->n_ssids > 0, it means we should do an active scan.
185 * In case of active scan w/o directed scan, we receive a zero-length SSID
186 * just to notify that this scan is active and not passive.
187 * In order to notify the FW of the number of SSIDs we wish to scan (including
188 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300189 * one for each SSID, and set the active bit (first). If the first SSID is
190 * already included in the probe template, so we need to set only
191 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100192 */
David Spinadel3cae0732014-12-29 15:43:48 +0200193static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
194 enum ieee80211_band band, int n_ssids)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100195{
David Spinadel3cae0732014-12-29 15:43:48 +0200196 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
197 return 10;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100198 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300199 return 20 + 3 * (n_ssids + 1);
200 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100201}
202
David Spinadel3cae0732014-12-29 15:43:48 +0200203static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
204 enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100205{
David Spinadel3cae0732014-12-29 15:43:48 +0200206 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
207 return 110;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100208 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
209}
210
Alexander Bondar8a110d92014-03-12 17:31:19 +0200211static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
212 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200213{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200214 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200215 int *global_cnt = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200216
David Spinadel1e2ebe02014-10-26 15:53:27 +0200217 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
218 mvmvif->phy_ctxt->id < MAX_PHYS)
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200219 *global_cnt += 1;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200220}
221
222static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200223 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200224 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200225 struct iwl_mvm_scan_params *params)
226{
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200227 int global_cnt = 0;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200228 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300229 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200230
231 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
232 IEEE80211_IFACE_ITER_NORMAL,
233 iwl_mvm_scan_condition_iterator,
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200234 &global_cnt);
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300235 params->n_ssids = n_ssids;
236 params->flags = flags;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200237
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200238 if (!global_cnt)
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300239 goto not_bound;
240
David Spinadel2ce89cd2014-07-22 13:11:18 +0300241 params->suspend_time = 30;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200242 params->max_out_time = 120;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300243
244 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300245 if (mvm->fw->ucode_capa.api[0] &
246 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
247 params->suspend_time = 105;
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200248 /*
249 * If there is more than one active interface make
250 * passive scan more fragmented.
251 */
David Spinadelef177082015-02-15 14:45:33 +0200252 frag_passive_dwell = 40;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200253 params->max_out_time = frag_passive_dwell;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300254 } else {
255 params->suspend_time = 120;
256 params->max_out_time = 120;
257 }
258 }
259
260 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
261 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
262 /*
263 * P2P device scan should not be fragmented to avoid negative
264 * impact on P2P device discovery. Configure max_out_time to be
265 * equal to dwell time on passive channel. Take a longest
266 * possible value, one that corresponds to 2GHz band
267 */
268 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
269 u32 passive_dwell =
David Spinadel3cae0732014-12-29 15:43:48 +0200270 iwl_mvm_get_passive_dwell(mvm,
271 IEEE80211_BAND_2GHZ);
David Spinadel2ce89cd2014-07-22 13:11:18 +0300272 params->max_out_time = passive_dwell;
273 } else {
274 params->passive_fragmented = true;
275 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200276 }
277
Luciano Coelhoc1537662015-04-09 14:48:11 +0300278 if ((flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
279 (params->max_out_time > 200))
Johannes Bergab480032014-06-04 10:13:50 +0200280 params->max_out_time = 200;
281
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300282not_bound:
283
Alexander Bondar50df8a32014-03-12 20:30:51 +0200284 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300285 if (params->passive_fragmented)
David Spinadel190f1022015-02-17 12:45:21 +0200286 params->dwell[band].fragmented = frag_passive_dwell;
287
288 params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
289 band);
David Spinadel3cae0732014-12-29 15:43:48 +0200290 params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200291 n_ssids);
292 }
Luciano Coelho0616c622015-04-09 12:18:56 +0300293
294 IWL_DEBUG_SCAN(mvm,
295 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
296 params->max_out_time, params->suspend_time,
297 params->passive_fragmented);
298 IWL_DEBUG_SCAN(mvm,
299 "dwell[IEEE80211_BAND_2GHZ]: passive %d, active %d, fragmented %d\n",
300 params->dwell[IEEE80211_BAND_2GHZ].passive,
301 params->dwell[IEEE80211_BAND_2GHZ].active,
302 params->dwell[IEEE80211_BAND_2GHZ].fragmented);
303 IWL_DEBUG_SCAN(mvm,
304 "dwell[IEEE80211_BAND_5GHZ]: passive %d, active %d, fragmented %d\n",
305 params->dwell[IEEE80211_BAND_5GHZ].passive,
306 params->dwell[IEEE80211_BAND_5GHZ].active,
307 params->dwell[IEEE80211_BAND_5GHZ].fragmented);
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200308}
309
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300310static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
311{
312 /* require rrm scan whenever the fw supports it */
313 return mvm->fw->ucode_capa.capa[0] &
314 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
315}
316
Luciano Coelho999d2562015-03-27 10:28:26 +0300317static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300318{
319 int max_probe_len;
320
Luciano Coelho1f940382015-02-10 13:03:38 +0200321 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300322
323 /* we create the 802.11 header and SSID element */
324 max_probe_len -= 24 + 2;
325
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300326 /* DS parameter set element is added on 2.4GHZ band if required */
327 if (iwl_mvm_rrm_scan_needed(mvm))
328 max_probe_len -= 3;
329
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300330 return max_probe_len;
331}
332
Luciano Coelho999d2562015-03-27 10:28:26 +0300333int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300334{
Luciano Coelho999d2562015-03-27 10:28:26 +0300335 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300336
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300337 /* TODO: [BUG] This function should return the maximum allowed size of
338 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
339 * in the same command. So the correct implementation of this function
340 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
341 * command has only 512 bytes and it would leave us with about 240
342 * bytes for scan IEs, which is clearly not enough. So meanwhile
343 * we will report an incorrect value. This may result in a failure to
344 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
345 * functions with -ENOBUFS, if a large enough probe will be provided.
346 */
347 return max_ie_len;
348}
349
Alexander Bondare5d74642014-12-09 19:15:49 +0200350int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
351 struct iwl_rx_cmd_buffer *rxb,
352 struct iwl_device_cmd *cmd)
353{
354 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Avraham Stern1083fd72015-03-31 16:14:41 +0300355 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
Alexander Bondare5d74642014-12-09 19:15:49 +0200356
357 IWL_DEBUG_SCAN(mvm,
358 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
359 notif->status, notif->scanned_channels);
360 return 0;
361}
362
David Spinadelfb98be52014-05-04 12:51:10 +0300363int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
364 struct iwl_rx_cmd_buffer *rxb,
365 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300366{
David Spinadeld2496222014-05-20 12:46:37 +0300367 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
368 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300369
370 return 0;
371}
372
David Spinadel35a000b2013-08-28 09:29:43 +0300373int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
374 struct iwl_rx_cmd_buffer *rxb,
375 struct iwl_device_cmd *cmd)
376{
377 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200378 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
379 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
380 bool ebs_successful = (scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS);
David Spinadelfb98be52014-05-04 12:51:10 +0300381
Johannes Berga6623e82014-01-27 15:40:53 +0100382 /* scan status must be locked for proper checking */
383 lockdep_assert_held(&mvm->mutex);
384
Luciano Coelho9af91f42015-02-10 10:42:26 +0200385 /* We first check if we were stopping a scan, in which case we
386 * just clear the stopping flag. Then we check if it was a
387 * firmware initiated stop, in which case we need to inform
388 * mac80211.
389 * Note that we can have a stopping and a running scan
390 * simultaneously, but we can't have two different types of
391 * scans stopping or running at the same time (since LMAC
392 * doesn't support it).
393 */
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300394
Luciano Coelho9af91f42015-02-10 10:42:26 +0200395 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
396 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
David Spinadel35a000b2013-08-28 09:29:43 +0300397
Luciano Coelho9af91f42015-02-10 10:42:26 +0200398 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
399 aborted ? "aborted" : "completed",
400 ebs_successful ? "successful" : "failed");
401
402 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
403 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
404 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
405 aborted ? "aborted" : "completed",
406 ebs_successful ? "successful" : "failed");
407
408 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
409 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
410 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
411
412 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
413 aborted ? "aborted" : "completed",
414 ebs_successful ? "successful" : "failed");
415
416 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200417 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200418 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
419 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
420 aborted ? "aborted" : "completed",
421 ebs_successful ? "successful" : "failed");
422
423 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
David Spinadelfb98be52014-05-04 12:51:10 +0300424 ieee80211_scan_completed(mvm->hw,
Luciano Coelho1f940382015-02-10 13:03:38 +0200425 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300426 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200427 }
David Spinadel35a000b2013-08-28 09:29:43 +0300428
Luciano Coelho9af91f42015-02-10 10:42:26 +0200429 mvm->last_ebs_successful = ebs_successful;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300430
David Spinadel35a000b2013-08-28 09:29:43 +0300431 return 0;
432}
433
David Spinadel35a000b2013-08-28 09:29:43 +0300434static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
435{
436 int i;
437
438 for (i = 0; i < PROBE_OPTION_MAX; i++) {
439 if (!ssid_list[i].len)
440 break;
441 if (ssid_list[i].len == ssid_len &&
442 !memcmp(ssid_list->ssid, ssid, ssid_len))
443 return i;
444 }
445 return -1;
446}
447
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300448static void iwl_scan_offload_build_ssid(struct iwl_mvm_scan_params *params,
David Spinadelfb98be52014-05-04 12:51:10 +0300449 struct iwl_ssid_ie *direct_scan,
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300450 u32 *ssid_bitmap)
David Spinadel35a000b2013-08-28 09:29:43 +0300451{
452 int i, j;
453 int index;
454
455 /*
456 * copy SSIDs from match list.
457 * iwl_config_sched_scan_profiles() uses the order of these ssids to
458 * config match list.
459 */
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300460 for (i = 0; i < params->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100461 /* skip empty SSID matchsets */
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300462 if (!params->match_sets[i].ssid.ssid_len)
Johannes Bergea73cbc2014-01-24 10:53:53 +0100463 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300464 direct_scan[i].id = WLAN_EID_SSID;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300465 direct_scan[i].len = params->match_sets[i].ssid.ssid_len;
466 memcpy(direct_scan[i].ssid, params->match_sets[i].ssid.ssid,
David Spinadelfb98be52014-05-04 12:51:10 +0300467 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300468 }
469
470 /* add SSIDs from scan SSID list */
471 *ssid_bitmap = 0;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300472 for (j = 0; j < params->n_ssids && i < PROBE_OPTION_MAX; j++) {
473 index = iwl_ssid_exist(params->ssids[j].ssid,
474 params->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300475 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300476 if (index < 0) {
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300477 if (!params->ssids[j].ssid_len)
David Spinadel35a000b2013-08-28 09:29:43 +0300478 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300479 direct_scan[i].id = WLAN_EID_SSID;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300480 direct_scan[i].len = params->ssids[j].ssid_len;
481 memcpy(direct_scan[i].ssid, params->ssids[j].ssid,
David Spinadelfb98be52014-05-04 12:51:10 +0300482 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300483 *ssid_bitmap |= BIT(i + 1);
484 i++;
485 } else {
486 *ssid_bitmap |= BIT(index + 1);
487 }
488 }
489}
490
David Spinadel35a000b2013-08-28 09:29:43 +0300491int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
492 struct cfg80211_sched_scan_request *req)
493{
494 struct iwl_scan_offload_profile *profile;
495 struct iwl_scan_offload_profile_cfg *profile_cfg;
496 struct iwl_scan_offload_blacklist *blacklist;
497 struct iwl_host_cmd cmd = {
498 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300499 .len[1] = sizeof(*profile_cfg),
500 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
501 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
502 };
503 int blacklist_len;
504 int i;
505 int ret;
506
507 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
508 return -EIO;
509
510 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
511 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
512 else
513 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
514
515 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
516 if (!blacklist)
517 return -ENOMEM;
518
519 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
520 if (!profile_cfg) {
521 ret = -ENOMEM;
522 goto free_blacklist;
523 }
524
525 cmd.data[0] = blacklist;
526 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
527 cmd.data[1] = profile_cfg;
528
529 /* No blacklist configuration */
530
531 profile_cfg->num_profiles = req->n_match_sets;
532 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
533 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
534 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200535 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
536 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300537
538 for (i = 0; i < req->n_match_sets; i++) {
539 profile = &profile_cfg->profiles[i];
540 profile->ssid_index = i;
541 /* Support any cipher and auth algorithm */
542 profile->unicast_cipher = 0xff;
543 profile->auth_alg = 0xff;
544 profile->network_type = IWL_NETWORK_TYPE_ANY;
545 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
546 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
547 }
548
549 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
550
551 ret = iwl_mvm_send_cmd(mvm, &cmd);
552 kfree(profile_cfg);
553free_blacklist:
554 kfree(blacklist);
555
556 return ret;
557}
558
David Spinadeld2496222014-05-20 12:46:37 +0300559static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
560 struct cfg80211_sched_scan_request *req)
561{
562 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
563 IWL_DEBUG_SCAN(mvm,
564 "Sending scheduled scan with filtering, n_match_sets %d\n",
565 req->n_match_sets);
566 return false;
567 }
568
569 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
570 return true;
571}
572
David Spinadelfb98be52014-05-04 12:51:10 +0300573static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +0300574{
575 int ret;
576 struct iwl_host_cmd cmd = {
577 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300578 };
579 u32 status;
580
581 /* Exit instantly with error when device is not ready
582 * to receive scan abort command or it does not perform
583 * scheduled scan currently */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200584 if (!mvm->scan_status)
David Spinadel35a000b2013-08-28 09:29:43 +0300585 return -EIO;
586
587 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
588 if (ret)
589 return ret;
590
591 if (status != CAN_ABORT_STATUS) {
592 /*
593 * The scan abort will return 1 for success or
594 * 2 for "failure". A failure condition can be
595 * due to simply not being in an active scan which
596 * can occur if we send the scan abort before the
597 * microcode has notified us that a scan is completed.
598 */
599 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200600 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +0300601 }
602
603 return ret;
604}
605
David Spinadelfb98be52014-05-04 12:51:10 +0300606int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +0300607{
608 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200609 struct iwl_notification_wait wait_scan_done;
610 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
Luciano Coelho9af91f42015-02-10 10:42:26 +0200611 bool sched = !!(mvm->scan_status & IWL_MVM_SCAN_SCHED);
David Spinadel35a000b2013-08-28 09:29:43 +0300612
613 lockdep_assert_held(&mvm->mutex);
614
David Spinadeld2496222014-05-20 12:46:37 +0300615 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
616 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
617 notify);
618
Luciano Coelho9af91f42015-02-10 10:42:26 +0200619 /* FIXME: For now we only check if no scan is set here, since
620 * we only support LMAC in this flow and it doesn't support
621 * multiple scans.
622 */
623 if (!mvm->scan_status)
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200624 return 0;
625
Luciano Coelho54331db2015-02-05 00:39:22 +0200626 if (iwl_mvm_is_radio_killed(mvm)) {
627 ret = 0;
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200628 goto out;
Luciano Coelho54331db2015-02-05 00:39:22 +0200629 }
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200630
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200631 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
632 scan_done_notif,
633 ARRAY_SIZE(scan_done_notif),
634 NULL, NULL);
635
David Spinadelfb98be52014-05-04 12:51:10 +0300636 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200637 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +0300638 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
639 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200640 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
Luciano Coelho54331db2015-02-05 00:39:22 +0200641 goto out;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200642 }
643
David Spinadelfb98be52014-05-04 12:51:10 +0300644 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
Luciano Coelho9af91f42015-02-10 10:42:26 +0200645 sched ? "scheduled " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200646
647 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
Luciano Coelho54331db2015-02-05 00:39:22 +0200648out:
Luciano Coelho9af91f42015-02-10 10:42:26 +0200649 /* Clear the scan status so the next scan requests will
650 * succeed and mark the scan as stopping, so that the Rx
651 * handler doesn't do anything, as the scan was stopped from
652 * above. Since the rx handler won't do anything now, we have
653 * to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200654 */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200655 if (mvm->scan_status == IWL_MVM_SCAN_REGULAR)
Eliad Peller4ff78182014-06-22 14:44:44 +0300656 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
657
Luciano Coelho9af91f42015-02-10 10:42:26 +0200658 if (sched) {
659 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
660 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_SCHED;
661 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300662 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200663 } else {
664 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
665 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_REGULAR;
666 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300667 ieee80211_scan_completed(mvm->hw, true);
668 }
David Spinadel636a2cd2014-05-01 15:57:22 +0300669
Luciano Coelho54331db2015-02-05 00:39:22 +0200670 return ret;
David Spinadel35a000b2013-08-28 09:29:43 +0300671}
David Spinadelfb98be52014-05-04 12:51:10 +0300672
Luciano Coelho65ff5562015-03-20 13:35:47 +0200673static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
674 struct iwl_scan_req_tx_cmd *tx_cmd,
675 bool no_cck)
David Spinadelfb98be52014-05-04 12:51:10 +0300676{
677 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
678 TX_CMD_FLG_BT_DIS);
679 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
680 IEEE80211_BAND_2GHZ,
681 no_cck);
682 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
683
684 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
685 TX_CMD_FLG_BT_DIS);
686 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
687 IEEE80211_BAND_5GHZ,
688 no_cck);
689 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
690}
691
692static void
693iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
694 struct ieee80211_channel **channels,
695 int n_channels, u32 ssid_bitmap,
Luciano Coelho65ff5562015-03-20 13:35:47 +0200696 struct iwl_scan_req_lmac *cmd)
David Spinadelfb98be52014-05-04 12:51:10 +0300697{
698 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
699 int i;
700
701 for (i = 0; i < n_channels; i++) {
702 channel_cfg[i].channel_num =
703 cpu_to_le16(channels[i]->hw_value);
704 channel_cfg[i].iter_count = cpu_to_le16(1);
705 channel_cfg[i].iter_interval = 0;
706 channel_cfg[i].flags =
707 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
708 ssid_bitmap);
709 }
710}
711
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300712static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
713 size_t len, u8 *const pos)
714{
715 static const u8 before_ds_params[] = {
716 WLAN_EID_SSID,
717 WLAN_EID_SUPP_RATES,
718 WLAN_EID_REQUEST,
719 WLAN_EID_EXT_SUPP_RATES,
720 };
721 size_t offs;
722 u8 *newpos = pos;
723
724 if (!iwl_mvm_rrm_scan_needed(mvm)) {
725 memcpy(newpos, ies, len);
726 return newpos + len;
727 }
728
729 offs = ieee80211_ie_split(ies, len,
730 before_ds_params,
731 ARRAY_SIZE(before_ds_params),
732 0);
733
734 memcpy(newpos, ies, offs);
735 newpos += offs;
736
737 /* Add a placeholder for DS Parameter Set element */
738 *newpos++ = WLAN_EID_DS_PARAMS;
739 *newpos++ = 1;
740 *newpos++ = 0;
741
742 memcpy(newpos, ies + offs, len - offs);
743 newpos += len - offs;
744
745 return newpos;
746}
747
David Spinadelfb98be52014-05-04 12:51:10 +0300748static void
Luciano Coelho65ff5562015-03-20 13:35:47 +0200749iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
750 struct ieee80211_scan_ies *ies,
751 struct iwl_scan_probe_req *preq,
752 const u8 *mac_addr, const u8 *mac_addr_mask)
David Spinadelfb98be52014-05-04 12:51:10 +0300753{
David Spinadelfb98be52014-05-04 12:51:10 +0300754 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300755 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +0300756
Johannes Bergeffd05a2014-11-18 17:21:19 +0100757 /*
758 * Unfortunately, right now the offload scan doesn't support randomising
759 * within the firmware, so until the firmware API is ready we implement
760 * it in the driver. This means that the scan iterations won't really be
761 * random, only when it's restarted, but at least that helps a bit.
762 */
763 if (mac_addr)
764 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
765 else
766 memcpy(frame->sa, vif->addr, ETH_ALEN);
767
David Spinadelfb98be52014-05-04 12:51:10 +0300768 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
769 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +0300770 eth_broadcast_addr(frame->bssid);
771 frame->seq_ctrl = 0;
772
773 pos = frame->u.probe_req.variable;
774 *pos++ = WLAN_EID_SSID;
775 *pos++ = 0;
776
777 preq->mac_header.offset = 0;
778 preq->mac_header.len = cpu_to_le16(24 + 2);
779
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300780 /* Insert ds parameter set element on 2.4 GHz band */
781 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
782 ies->ies[IEEE80211_BAND_2GHZ],
783 ies->len[IEEE80211_BAND_2GHZ],
784 pos);
David Spinadelfb98be52014-05-04 12:51:10 +0300785 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300786 preq->band_data[0].len = cpu_to_le16(newpos - pos);
787 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +0300788
789 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
790 ies->len[IEEE80211_BAND_5GHZ]);
791 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
792 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
793 pos += ies->len[IEEE80211_BAND_5GHZ];
794
795 memcpy(pos, ies->common_ies, ies->common_ie_len);
796 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
797 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
798}
799
800static void
Luciano Coelho65ff5562015-03-20 13:35:47 +0200801iwl_mvm_build_generic_scan_cmd(struct iwl_mvm *mvm,
802 struct iwl_scan_req_lmac *cmd,
803 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300804{
David Spinadelaf913442014-06-12 19:29:40 +0300805 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +0300806 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
807 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
808 if (params->passive_fragmented)
809 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +0200810 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadelfb98be52014-05-04 12:51:10 +0300811 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
812 cmd->max_out_time = cpu_to_le32(params->max_out_time);
813 cmd->suspend_time = cpu_to_le32(params->suspend_time);
814 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +0300815 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +0300816
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300817 if (iwl_mvm_rrm_scan_needed(mvm))
818 cmd->scan_flags |=
819 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +0300820}
821
Luciano Coelho999d2562015-03-27 10:28:26 +0300822static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
823 struct ieee80211_scan_ies *ies,
824 int n_channels)
825{
826 return ((n_ssids <= PROBE_OPTION_MAX) &&
827 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
828 (ies->common_ie_len +
829 ies->len[NL80211_BAND_2GHZ] +
830 ies->len[NL80211_BAND_5GHZ] <=
831 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
832}
833
Luciano Coelho6749dd82015-03-20 15:51:36 +0200834static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Luciano Coelho3db7c6e2015-04-01 17:09:56 +0300835 struct ieee80211_scan_ies *ies,
836 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300837{
Luciano Coelho65ff5562015-03-20 13:35:47 +0200838 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +0300839 struct iwl_scan_probe_req *preq;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300840 u32 flags = 0, ssid_bitmap = 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300841
842 lockdep_assert_held(&mvm->mutex);
843
Luciano Coelho3db7c6e2015-04-01 17:09:56 +0300844 iwl_mvm_build_generic_scan_cmd(mvm, cmd, params);
David Spinadelfb98be52014-05-04 12:51:10 +0300845
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300846 cmd->n_channels = (u8)params->n_channels;
David Spinadelfb98be52014-05-04 12:51:10 +0300847
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300848 cmd->delay = cpu_to_le32(params->delay);
David Spinadelfb98be52014-05-04 12:51:10 +0300849
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300850 if (params->pass_all)
851 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
852 else
853 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
854
855 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300856 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
857
Luciano Coelho3db7c6e2015-04-01 17:09:56 +0300858 if (params->passive_fragmented)
David Spinadelfb98be52014-05-04 12:51:10 +0300859 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
860
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300861 if (params->n_ssids == 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300862 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
863
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300864#ifdef CONFIG_IWLWIFI_DEBUGFS
865 /* TODO: Check if it's okay to have this in regular scans */
866 if (mvm->scan_iter_notif_enabled)
867 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
868#endif
869
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300870 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +0300871
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300872 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
David Spinadelfb98be52014-05-04 12:51:10 +0300873 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
874 MAC_FILTER_IN_BEACON);
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300875 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
Luciano Coelho5ef766f2015-04-02 15:37:05 +0300876 iwl_mvm_scan_fill_ssids(cmd->direct_scan, params, &ssid_bitmap);
877
878 /* this API uses bits 1-20 instead of 0-19 */
879 ssid_bitmap <<= 1;
David Spinadelfb98be52014-05-04 12:51:10 +0300880
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300881 cmd->schedule[0].delay = cpu_to_le16(params->interval);
David Spinadelfb98be52014-05-04 12:51:10 +0300882 cmd->schedule[0].iterations = 1;
883 cmd->schedule[0].full_scan_mul = 0;
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300884 cmd->schedule[1].delay = cpu_to_le16(params->interval);
David Spinadelfb98be52014-05-04 12:51:10 +0300885 cmd->schedule[1].iterations = 0;
886 cmd->schedule[1].full_scan_mul = 0;
887
David Spinadel1f9c4182014-12-01 23:30:07 +0200888 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
889 mvm->last_ebs_successful) {
890 cmd->channel_opt[0].flags =
891 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
892 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
893 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
894 cmd->channel_opt[0].non_ebs_ratio =
895 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
896 cmd->channel_opt[1].flags =
897 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
898 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
899 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
900 cmd->channel_opt[1].non_ebs_ratio =
901 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
902 }
903
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300904 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
905 params->n_channels, ssid_bitmap, cmd);
David Spinadelfb98be52014-05-04 12:51:10 +0300906
David Spinadeld2496222014-05-20 12:46:37 +0300907 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
908 mvm->fw->ucode_capa.n_scan_channels);
909
Luciano Coelho6749dd82015-03-20 15:51:36 +0200910 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300911 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
912 params->mac_addr : NULL,
913 params->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +0300914
Luciano Coelho3db7c6e2015-04-01 17:09:56 +0300915 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300916}
917
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300918static int
919iwl_mvm_sched_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
920 struct ieee80211_scan_ies *ies,
921 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300922{
Luciano Coelho65ff5562015-03-20 13:35:47 +0200923 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +0300924 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +0300925 u32 flags = 0, ssid_bitmap = 0;
926
927 lockdep_assert_held(&mvm->mutex);
928
Luciano Coelho8df3e682015-04-01 17:37:44 +0300929 iwl_mvm_build_generic_scan_cmd(mvm, cmd, params);
David Spinadelfb98be52014-05-04 12:51:10 +0300930
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300931 cmd->n_channels = (u8)params->n_channels;
David Spinadelfb98be52014-05-04 12:51:10 +0300932
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300933 cmd->delay = cpu_to_le32(params->delay);
Luciano Coelho2250fd92014-12-03 10:52:26 +0200934
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300935 if (params->pass_all)
David Spinadelfb98be52014-05-04 12:51:10 +0300936 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
David Spinadel7e2a3882014-12-23 14:38:09 +0200937 else
938 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
David Spinadelfb98be52014-05-04 12:51:10 +0300939
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300940 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300941 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
942
Luciano Coelho8df3e682015-04-01 17:37:44 +0300943 if (params->passive_fragmented)
David Spinadelfb98be52014-05-04 12:51:10 +0300944 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
945
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300946 if (params->n_ssids == 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300947 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
948
Alexander Bondare5d74642014-12-09 19:15:49 +0200949#ifdef CONFIG_IWLWIFI_DEBUGFS
950 if (mvm->scan_iter_notif_enabled)
951 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
952#endif
953
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300954 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +0300955
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300956 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
David Spinadelfb98be52014-05-04 12:51:10 +0300957 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
958 MAC_FILTER_IN_BEACON);
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300959 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
David Spinadelfb98be52014-05-04 12:51:10 +0300960
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300961 iwl_scan_offload_build_ssid(params, cmd->direct_scan, &ssid_bitmap);
962
963 cmd->schedule[0].delay = cpu_to_le16(params->interval);
David Spinadelfb98be52014-05-04 12:51:10 +0300964 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
965 cmd->schedule[0].full_scan_mul = 1;
966
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300967 cmd->schedule[1].delay = cpu_to_le16(params->interval);
David Spinadelfb98be52014-05-04 12:51:10 +0300968 cmd->schedule[1].iterations = 0xff;
969 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
970
David Spinadel1f9c4182014-12-01 23:30:07 +0200971 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
972 mvm->last_ebs_successful) {
973 cmd->channel_opt[0].flags =
974 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
975 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
976 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
977 cmd->channel_opt[0].non_ebs_ratio =
978 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
979 cmd->channel_opt[1].flags =
980 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
981 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
982 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
983 cmd->channel_opt[1].non_ebs_ratio =
984 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
985 }
986
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300987 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
988 params->n_channels, ssid_bitmap, cmd);
David Spinadelfb98be52014-05-04 12:51:10 +0300989
David Spinadeld2496222014-05-20 12:46:37 +0300990 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
991 mvm->fw->ucode_capa.n_scan_channels);
992
Luciano Coelho65ff5562015-03-20 13:35:47 +0200993 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
Luciano Coelhof7b788b2015-04-02 00:08:35 +0300994 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
995 params->mac_addr : NULL,
996 params->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +0300997
Luciano Coelho8df3e682015-04-01 17:37:44 +0300998 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300999}
1000
1001
1002int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1003{
David Spinadeld2496222014-05-20 12:46:37 +03001004 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1005 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1006 true);
1007
Luciano Coelho9af91f42015-02-10 10:42:26 +02001008 if (!(mvm->scan_status & IWL_MVM_SCAN_REGULAR))
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001009 return 0;
1010
1011 if (iwl_mvm_is_radio_killed(mvm)) {
1012 ieee80211_scan_completed(mvm->hw, true);
1013 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001014 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001015 return 0;
1016 }
1017
Luciano Coelho1f940382015-02-10 13:03:38 +02001018 return iwl_mvm_scan_offload_stop(mvm, true);
David Spinadelfb98be52014-05-04 12:51:10 +03001019}
David Spinadeld2496222014-05-20 12:46:37 +03001020
1021/* UMAC scan API */
1022
1023struct iwl_umac_scan_done {
1024 struct iwl_mvm *mvm;
1025 enum iwl_umac_scan_uid_type type;
1026};
1027
1028static int rate_to_scan_rate_flag(unsigned int rate)
1029{
1030 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1031 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1032 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1033 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1034 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1035 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1036 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1037 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1038 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1039 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1040 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1041 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1042 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1043 };
1044
1045 return rate_to_scan_rate[rate];
1046}
1047
1048static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1049{
1050 struct ieee80211_supported_band *band;
1051 unsigned int rates = 0;
1052 int i;
1053
1054 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1055 for (i = 0; i < band->n_bitrates; i++)
1056 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1057 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1058 for (i = 0; i < band->n_bitrates; i++)
1059 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1060
1061 /* Set both basic rates and supported rates */
1062 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1063
1064 return cpu_to_le32(rates);
1065}
1066
1067int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1068{
1069
1070 struct iwl_scan_config *scan_config;
1071 struct ieee80211_supported_band *band;
1072 int num_channels =
1073 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1074 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1075 int ret, i, j = 0, cmd_size, data_size;
1076 struct iwl_host_cmd cmd = {
1077 .id = SCAN_CFG_CMD,
1078 };
1079
1080 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1081 return -ENOBUFS;
1082
1083 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1084
1085 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1086 if (!scan_config)
1087 return -ENOMEM;
1088
1089 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1090 scan_config->hdr.size = cpu_to_le16(data_size);
1091 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1092 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1093 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1094 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1095 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1096 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1097 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1098 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1099 SCAN_CONFIG_N_CHANNELS(num_channels));
Moshe Harela0544272014-12-08 21:13:14 +02001100 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
David Spinadeld2496222014-05-20 12:46:37 +03001101 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1102 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1103 scan_config->out_of_channel_time = cpu_to_le32(170);
1104 scan_config->suspend_time = cpu_to_le32(30);
1105 scan_config->dwell_active = 20;
1106 scan_config->dwell_passive = 110;
1107 scan_config->dwell_fragmented = 20;
1108
1109 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1110
1111 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1112 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1113 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1114 IWL_CHANNEL_FLAG_EBS_ADD |
1115 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1116
1117 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1118 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001119 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001120 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1121 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001122 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001123
1124 cmd.data[0] = scan_config;
1125 cmd.len[0] = cmd_size;
1126 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1127
1128 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1129
1130 ret = iwl_mvm_send_cmd(mvm, &cmd);
1131
1132 kfree(scan_config);
1133 return ret;
1134}
1135
1136static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1137{
1138 int i;
1139
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001140 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001141 if (mvm->scan_uid[i] == uid)
1142 return i;
1143
1144 return i;
1145}
1146
1147static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1148{
1149 return iwl_mvm_find_scan_uid(mvm, 0);
1150}
1151
1152static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1153 enum iwl_umac_scan_uid_type type)
1154{
1155 int i;
1156
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001157 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001158 if (mvm->scan_uid[i] & type)
1159 return true;
1160
1161 return false;
1162}
1163
Alexander Bondar963221b2015-03-26 11:07:35 +02001164static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm,
1165 enum iwl_umac_scan_uid_type type)
1166{
1167 int i;
1168
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001169 for (i = 0; i < mvm->max_scans; i++)
Alexander Bondar963221b2015-03-26 11:07:35 +02001170 if (mvm->scan_uid[i] & type)
1171 return i;
1172
1173 return i;
1174}
1175
David Spinadeld2496222014-05-20 12:46:37 +03001176static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1177 enum iwl_umac_scan_uid_type type)
1178{
1179 u32 uid;
1180
1181 /* make sure exactly one bit is on in scan type */
1182 WARN_ON(hweight8(type) != 1);
1183
1184 /*
1185 * Make sure scan uids are unique. If one scan lasts long time while
1186 * others are completing frequently, the seq number will wrap up and
1187 * we may have more than one scan with the same uid.
1188 */
1189 do {
1190 uid = type | (mvm->scan_seq_num <<
1191 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1192 mvm->scan_seq_num++;
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001193 } while (iwl_mvm_find_scan_uid(mvm, uid) < mvm->max_scans);
David Spinadeld2496222014-05-20 12:46:37 +03001194
1195 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1196
1197 return uid;
1198}
1199
1200static void
1201iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1202 struct iwl_scan_req_umac *cmd,
1203 struct iwl_mvm_scan_params *params)
1204{
1205 memset(cmd, 0, ksize(cmd));
1206 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1207 sizeof(struct iwl_mvm_umac_cmd_hdr));
1208 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1209 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1210 if (params->passive_fragmented)
1211 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +02001212 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadeld2496222014-05-20 12:46:37 +03001213 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1214 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1215 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1216}
1217
1218static void
1219iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1220 struct ieee80211_channel **channels,
1221 int n_channels, u32 ssid_bitmap,
1222 struct iwl_scan_req_umac *cmd)
1223{
1224 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1225 int i;
1226
1227 for (i = 0; i < n_channels; i++) {
1228 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1229 channel_cfg[i].channel_num = channels[i]->hw_value;
1230 channel_cfg[i].iter_count = 1;
1231 channel_cfg[i].iter_interval = 0;
1232 }
1233}
1234
1235static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1236 struct cfg80211_ssid *ssids,
1237 int fragmented)
1238{
1239 int flags = 0;
1240
1241 if (n_ssids == 0)
1242 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1243
1244 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1245 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1246
1247 if (fragmented)
1248 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1249
1250 if (iwl_mvm_rrm_scan_needed(mvm))
1251 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1252
1253 return flags;
1254}
1255
Luciano Coelho6749dd82015-03-20 15:51:36 +02001256static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001257 struct ieee80211_scan_ies *ies,
1258 struct iwl_mvm_scan_params *params)
David Spinadeld2496222014-05-20 12:46:37 +03001259{
David Spinadeld2496222014-05-20 12:46:37 +03001260 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1261 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1262 sizeof(struct iwl_scan_channel_cfg_umac) *
1263 mvm->fw->ucode_capa.n_scan_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001264 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001265 u32 ssid_bitmap = 0;
Luciano Coelho5ef766f2015-04-02 15:37:05 +03001266 int uid_idx;
David Spinadeld2496222014-05-20 12:46:37 +03001267
1268 lockdep_assert_held(&mvm->mutex);
1269
1270 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001271 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001272 return -EBUSY;
1273
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001274 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, params);
David Spinadeld2496222014-05-20 12:46:37 +03001275
1276 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1277 mvm->scan_uid[uid_idx] = uid;
1278 cmd->uid = cpu_to_le32(uid);
1279
1280 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1281
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001282 flags = iwl_mvm_scan_umac_common_flags(mvm, params->n_ssids,
1283 params->ssids,
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001284 params->passive_fragmented);
David Spinadeld2496222014-05-20 12:46:37 +03001285
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001286 if (params->pass_all)
1287 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1288 else
1289 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
David Spinadeld2496222014-05-20 12:46:37 +03001290
1291 cmd->general_flags = cpu_to_le32(flags);
Haim Dreyfuss4db65582015-01-05 15:00:35 +02001292
1293 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
1294 mvm->last_ebs_successful)
1295 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1296 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1297 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1298
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001299 cmd->n_channels = params->n_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001300
Luciano Coelho5ef766f2015-04-02 15:37:05 +03001301 iwl_mvm_scan_fill_ssids(sec_part->direct_scan, params, &ssid_bitmap);
David Spinadeld2496222014-05-20 12:46:37 +03001302
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001303 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1304 params->n_channels, ssid_bitmap, cmd);
David Spinadeld2496222014-05-20 12:46:37 +03001305
1306 sec_part->schedule[0].iter_count = 1;
1307 sec_part->delay = 0;
1308
Luciano Coelho6749dd82015-03-20 15:51:36 +02001309 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001310 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1311 params->mac_addr : NULL,
1312 params->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001313
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001314 return 0;
David Spinadeld2496222014-05-20 12:46:37 +03001315}
1316
Luciano Coelho6749dd82015-03-20 15:51:36 +02001317static int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm,
1318 struct ieee80211_vif *vif,
Luciano Coelho8df3e682015-04-01 17:37:44 +03001319 struct ieee80211_scan_ies *ies,
1320 struct iwl_mvm_scan_params *params)
David Spinadeld2496222014-05-20 12:46:37 +03001321{
David Spinadeld2496222014-05-20 12:46:37 +03001322 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1323 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1324 sizeof(struct iwl_scan_channel_cfg_umac) *
1325 mvm->fw->ucode_capa.n_scan_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001326 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001327 u32 ssid_bitmap = 0;
Luciano Coelho8df3e682015-04-01 17:37:44 +03001328 int uid_idx;
David Spinadeld2496222014-05-20 12:46:37 +03001329
1330 lockdep_assert_held(&mvm->mutex);
1331
1332 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001333 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001334 return -EBUSY;
1335
Luciano Coelho8df3e682015-04-01 17:37:44 +03001336 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, params);
David Spinadeld2496222014-05-20 12:46:37 +03001337
1338 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1339
1340 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1341 mvm->scan_uid[uid_idx] = uid;
1342 cmd->uid = cpu_to_le32(uid);
1343
1344 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1345
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001346 flags = iwl_mvm_scan_umac_common_flags(mvm, params->n_ssids,
1347 params->ssids,
Luciano Coelho8df3e682015-04-01 17:37:44 +03001348 params->passive_fragmented);
David Spinadeld2496222014-05-20 12:46:37 +03001349
1350 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1351
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001352 if (params->pass_all)
David Spinadeld2496222014-05-20 12:46:37 +03001353 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1354 else
1355 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1356
1357 cmd->general_flags = cpu_to_le32(flags);
1358
1359 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1360 mvm->last_ebs_successful)
1361 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1362 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1363 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1364
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001365 cmd->n_channels = params->n_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001366
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001367 iwl_scan_offload_build_ssid(params, sec_part->direct_scan,
1368 &ssid_bitmap);
David Spinadeld2496222014-05-20 12:46:37 +03001369
1370 /* This API uses bits 0-19 instead of 1-20. */
1371 ssid_bitmap = ssid_bitmap >> 1;
1372
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001373 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1374 params->n_channels, ssid_bitmap, cmd);
David Spinadeld2496222014-05-20 12:46:37 +03001375
1376 sec_part->schedule[0].interval =
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001377 cpu_to_le16(params->interval / MSEC_PER_SEC);
David Spinadeld2496222014-05-20 12:46:37 +03001378 sec_part->schedule[0].iter_count = 0xff;
1379
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001380 if (params->delay > U16_MAX) {
Luciano Coelho2250fd92014-12-03 10:52:26 +02001381 IWL_DEBUG_SCAN(mvm,
1382 "delay value is > 16-bits, set to max possible\n");
1383 sec_part->delay = cpu_to_le16(U16_MAX);
1384 } else {
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001385 sec_part->delay = cpu_to_le16(params->delay);
Luciano Coelho2250fd92014-12-03 10:52:26 +02001386 }
David Spinadeld2496222014-05-20 12:46:37 +03001387
Luciano Coelho65ff5562015-03-20 13:35:47 +02001388 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001389 params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1390 params->mac_addr : NULL,
1391 params->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001392
Luciano Coelho8df3e682015-04-01 17:37:44 +03001393 return 0;
David Spinadeld2496222014-05-20 12:46:37 +03001394}
1395
Luciano Coelho6749dd82015-03-20 15:51:36 +02001396static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1397{
1398 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1399}
1400
1401static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1402{
1403 /* This looks a bit arbitrary, but the idea is that if we run
1404 * out of possible simultaneous scans and the userspace is
1405 * trying to run a scan type that is already running, we
1406 * return -EBUSY. But if the userspace wants to start a
1407 * different type of scan, we stop the opposite type to make
1408 * space for the new request. The reason is backwards
1409 * compatibility with old wpa_supplicant that wouldn't stop a
1410 * scheduled scan before starting a normal scan.
1411 */
1412
1413 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1414 return 0;
1415
1416 /* Use a switch, even though this is a bitmask, so that more
1417 * than one bits set will fall in default and we will warn.
1418 */
1419 switch (type) {
1420 case IWL_MVM_SCAN_REGULAR:
1421 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1422 return -EBUSY;
1423 return iwl_mvm_scan_offload_stop(mvm, true);
1424 case IWL_MVM_SCAN_SCHED:
1425 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1426 return -EBUSY;
1427 return iwl_mvm_cancel_scan(mvm);
Luciano Coelho19945df2015-03-20 16:11:28 +02001428 case IWL_MVM_SCAN_NETDETECT:
1429 /* No need to stop anything for net-detect since the
1430 * firmware is restarted anyway. This way, any sched
1431 * scans that were running will be restarted when we
1432 * resume.
1433 */
1434 return 0;
Luciano Coelho6749dd82015-03-20 15:51:36 +02001435 default:
1436 WARN_ON(1);
1437 break;
1438 }
1439
1440 return -EIO;
1441}
1442
1443int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1444 struct cfg80211_scan_request *req,
1445 struct ieee80211_scan_ies *ies)
1446{
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001447 struct iwl_host_cmd hcmd = {
1448 .len = { iwl_mvm_scan_size(mvm), },
1449 .data = { mvm->scan_cmd, },
1450 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1451 };
1452 struct iwl_mvm_scan_params params = {};
Luciano Coelho6749dd82015-03-20 15:51:36 +02001453 int ret;
1454
1455 lockdep_assert_held(&mvm->mutex);
1456
1457 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1458 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1459 return -EBUSY;
1460 }
1461
1462 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1463 if (ret)
1464 return ret;
1465
1466 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1467
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001468 /* we should have failed registration if scan_cmd was NULL */
1469 if (WARN_ON(!mvm->scan_cmd))
1470 return -ENOMEM;
1471
1472 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1473 return -ENOBUFS;
1474
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001475 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
1476
1477 params.n_channels = req->n_channels;
1478 params.delay = 0;
1479 params.interval = 0;
1480 params.ssids = req->ssids;
1481 params.channels = req->channels;
1482 params.mac_addr = req->mac_addr;
1483 params.mac_addr_mask = req->mac_addr_mask;
1484 params.no_cck = req->no_cck;
1485 params.pass_all = true;
1486 params.n_match_sets = 0;
1487 params.match_sets = NULL;
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001488
1489 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1490 hcmd.id = SCAN_REQ_UMAC;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001491 ret = iwl_mvm_scan_umac(mvm, vif, ies, &params);
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001492 } else {
1493 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001494 ret = iwl_mvm_scan_lmac(mvm, vif, ies, &params);
Luciano Coelho3db7c6e2015-04-01 17:09:56 +03001495 }
1496
1497 if (ret)
1498 return ret;
1499
1500 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1501 if (!ret) {
1502 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1503 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1504 } else {
1505 /* If the scan failed, it usually means that the FW was unable
1506 * to allocate the time events. Warn on it, but maybe we
1507 * should try to send the command again with different params.
1508 */
1509 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1510 }
Luciano Coelho6749dd82015-03-20 15:51:36 +02001511
1512 if (ret)
1513 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1514
1515 return ret;
1516}
1517
Luciano Coelho65ff5562015-03-20 13:35:47 +02001518int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1519 struct ieee80211_vif *vif,
1520 struct cfg80211_sched_scan_request *req,
Luciano Coelho19945df2015-03-20 16:11:28 +02001521 struct ieee80211_scan_ies *ies,
1522 int type)
Luciano Coelho65ff5562015-03-20 13:35:47 +02001523{
Luciano Coelho8df3e682015-04-01 17:37:44 +03001524 struct iwl_host_cmd hcmd = {
1525 .len = { iwl_mvm_scan_size(mvm), },
1526 .data = { mvm->scan_cmd, },
1527 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1528 };
1529 struct iwl_mvm_scan_params params = {};
Luciano Coelho65ff5562015-03-20 13:35:47 +02001530 int ret;
1531
Luciano Coelho6749dd82015-03-20 15:51:36 +02001532 lockdep_assert_held(&mvm->mutex);
1533
1534 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1535 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1536 return -EBUSY;
1537 }
1538
Luciano Coelho19945df2015-03-20 16:11:28 +02001539 ret = iwl_mvm_check_running_scans(mvm, type);
Luciano Coelho6749dd82015-03-20 15:51:36 +02001540 if (ret)
1541 return ret;
1542
Luciano Coelho8df3e682015-04-01 17:37:44 +03001543 /* we should have failed registration if scan_cmd was NULL */
1544 if (WARN_ON(!mvm->scan_cmd))
1545 return -ENOMEM;
1546
1547 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1548 return -ENOBUFS;
1549
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001550 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
1551
1552 params.n_channels = req->n_channels;
1553 params.delay = req->delay;
1554 params.ssids = req->ssids;
1555 params.channels = req->channels;
1556 params.mac_addr = req->mac_addr;
1557 params.mac_addr_mask = req->mac_addr_mask;
1558 params.no_cck = false;
1559 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1560 params.n_match_sets = req->n_match_sets;
1561 params.match_sets = req->match_sets;
1562
1563 if (req->interval > U16_MAX) {
1564 IWL_DEBUG_SCAN(mvm,
1565 "interval value is > 16-bits, set to max possible\n");
1566 params.interval = U16_MAX;
1567 } else {
1568 params.interval = req->interval / MSEC_PER_SEC;
1569 }
Luciano Coelho8df3e682015-04-01 17:37:44 +03001570
Luciano Coelho65ff5562015-03-20 13:35:47 +02001571 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1572 if (ret)
1573 return ret;
1574
1575 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
Luciano Coelho8df3e682015-04-01 17:37:44 +03001576 hcmd.id = SCAN_REQ_UMAC;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001577 ret = iwl_mvm_sched_scan_umac(mvm, vif, ies, &params);
Luciano Coelho65ff5562015-03-20 13:35:47 +02001578 } else {
Luciano Coelho8df3e682015-04-01 17:37:44 +03001579 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
Luciano Coelhof7b788b2015-04-02 00:08:35 +03001580 ret = iwl_mvm_sched_scan_lmac(mvm, vif, ies, &params);
Luciano Coelho8df3e682015-04-01 17:37:44 +03001581 }
1582
1583 if (ret)
1584 return ret;
1585
1586 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1587 if (!ret) {
1588 IWL_DEBUG_SCAN(mvm,
1589 "Sched scan request was sent successfully\n");
1590 mvm->scan_status |= type;
1591 } else {
1592 /* If the scan failed, it usually means that the FW was unable
1593 * to allocate the time events. Warn on it, but maybe we
1594 * should try to send the command again with different params.
1595 */
1596 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
Luciano Coelho65ff5562015-03-20 13:35:47 +02001597 }
1598
1599 return ret;
1600}
1601
David Spinadeld2496222014-05-20 12:46:37 +03001602int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1603 struct iwl_rx_cmd_buffer *rxb,
1604 struct iwl_device_cmd *cmd)
1605{
1606 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1607 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1608 u32 uid = __le32_to_cpu(notif->uid);
1609 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1610 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1611
David Spinadel2992a322014-11-10 11:16:53 +02001612 /*
1613 * Scan uid may be set to zero in case of scan abort request from above.
1614 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001615 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001616 return 0;
1617
1618 IWL_DEBUG_SCAN(mvm,
1619 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1620 uid, sched ? "sched" : "regular",
1621 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1622 "completed" : "aborted",
1623 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1624 "success" : "failed");
1625
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001626 if (notif->ebs_status)
1627 mvm->last_ebs_successful = false;
1628
David Spinadeld2496222014-05-20 12:46:37 +03001629 mvm->scan_uid[uid_idx] = 0;
1630
1631 if (!sched) {
1632 ieee80211_scan_completed(mvm->hw,
1633 notif->status ==
1634 IWL_SCAN_OFFLOAD_ABORTED);
1635 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1636 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
1637 ieee80211_sched_scan_stopped(mvm->hw);
1638 } else {
1639 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1640 }
1641
1642 return 0;
1643}
1644
1645static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1646 struct iwl_rx_packet *pkt, void *data)
1647{
1648 struct iwl_umac_scan_done *scan_done = data;
1649 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1650 u32 uid = __le32_to_cpu(notif->uid);
1651 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1652
1653 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1654 return false;
1655
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001656 if (uid_idx >= scan_done->mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001657 return false;
1658
1659 /*
1660 * Clear scan uid of scans that was aborted from above and completed
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001661 * in FW so the RX handler does nothing. Set last_ebs_successful here if
1662 * needed.
David Spinadeld2496222014-05-20 12:46:37 +03001663 */
1664 scan_done->mvm->scan_uid[uid_idx] = 0;
1665
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001666 if (notif->ebs_status)
1667 scan_done->mvm->last_ebs_successful = false;
1668
David Spinadeld2496222014-05-20 12:46:37 +03001669 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
1670}
1671
1672static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
1673{
1674 struct iwl_umac_scan_abort cmd = {
1675 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
1676 sizeof(struct iwl_mvm_umac_cmd_hdr)),
1677 .uid = cpu_to_le32(uid),
1678 };
1679
1680 lockdep_assert_held(&mvm->mutex);
1681
1682 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1683
1684 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
1685}
1686
1687static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
1688 enum iwl_umac_scan_uid_type type, bool notify)
1689{
1690 struct iwl_notification_wait wait_scan_done;
1691 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
1692 struct iwl_umac_scan_done scan_done = {
1693 .mvm = mvm,
1694 .type = type,
1695 };
1696 int i, ret = -EIO;
1697
1698 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1699 scan_done_notif,
1700 ARRAY_SIZE(scan_done_notif),
1701 iwl_scan_umac_done_check, &scan_done);
1702
1703 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1704
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001705 for (i = 0; i < mvm->max_scans; i++) {
David Spinadeld2496222014-05-20 12:46:37 +03001706 if (mvm->scan_uid[i] & type) {
1707 int err;
1708
1709 if (iwl_mvm_is_radio_killed(mvm) &&
1710 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
1711 ieee80211_scan_completed(mvm->hw, true);
1712 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1713 break;
1714 }
1715
1716 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
1717 if (!err)
1718 ret = 0;
1719 }
1720 }
1721
1722 if (ret) {
1723 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
1724 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1725 return ret;
1726 }
1727
1728 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1729 if (ret)
1730 return ret;
1731
1732 if (notify) {
1733 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
1734 ieee80211_sched_scan_stopped(mvm->hw);
1735 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
1736 ieee80211_scan_completed(mvm->hw, true);
1737 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1738 }
1739 }
1740
1741 return ret;
1742}
1743
1744int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1745{
1746 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1747 return sizeof(struct iwl_scan_req_umac) +
1748 sizeof(struct iwl_scan_channel_cfg_umac) *
1749 mvm->fw->ucode_capa.n_scan_channels +
1750 sizeof(struct iwl_scan_req_umac_tail);
1751
Luciano Coelho65ff5562015-03-20 13:35:47 +02001752 return sizeof(struct iwl_scan_req_lmac) +
Luciano Coelho1f940382015-02-10 13:03:38 +02001753 sizeof(struct iwl_scan_channel_cfg_lmac) *
1754 mvm->fw->ucode_capa.n_scan_channels +
1755 sizeof(struct iwl_scan_probe_req);
David Spinadeld2496222014-05-20 12:46:37 +03001756}
David Spinadel4ffb3652015-03-10 10:06:02 +02001757
1758/*
1759 * This function is used in nic restart flow, to inform mac80211 about scans
1760 * that was aborted by restart flow or by an assert.
1761 */
1762void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1763{
1764 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001765 u32 uid, i;
1766
1767 uid = iwl_mvm_find_first_scan(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001768 if (uid < mvm->max_scans) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001769 ieee80211_scan_completed(mvm->hw, true);
Alexander Bondar963221b2015-03-26 11:07:35 +02001770 mvm->scan_uid[uid] = 0;
1771 }
1772 uid = iwl_mvm_find_first_scan(mvm,
1773 IWL_UMAC_SCAN_UID_SCHED_SCAN);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001774 if (uid < mvm->max_scans && !mvm->restart_fw) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001775 ieee80211_sched_scan_stopped(mvm->hw);
Alexander Bondar963221b2015-03-26 11:07:35 +02001776 mvm->scan_uid[uid] = 0;
1777 }
1778
1779 /* We shouldn't have any UIDs still set. Loop over all the
1780 * UIDs to make sure there's nothing left there and warn if
1781 * any is found.
1782 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001783 for (i = 0; i < mvm->max_scans; i++) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001784 if (WARN_ONCE(mvm->scan_uid[i],
1785 "UMAC scan UID %d was not cleaned\n",
1786 mvm->scan_uid[i]))
1787 mvm->scan_uid[i] = 0;
1788 }
David Spinadel4ffb3652015-03-10 10:06:02 +02001789 } else {
Luciano Coelho9af91f42015-02-10 10:42:26 +02001790 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
David Spinadel4ffb3652015-03-10 10:06:02 +02001791 ieee80211_scan_completed(mvm->hw, true);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001792
1793 /* Sched scan will be restarted by mac80211 in
1794 * restart_hw, so do not report if FW is about to be
1795 * restarted.
1796 */
1797 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1798 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadel4ffb3652015-03-10 10:06:02 +02001799 }
1800}