blob: 199f6fce09684a568f006c4beb41cebe66b49010 [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.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 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.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 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;
82 struct _dwell {
83 u16 passive;
84 u16 active;
85 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +020086};
Johannes Berg8ca151b2013-01-24 14:25:36 +010087
David Spinadeld2496222014-05-20 12:46:37 +030088enum iwl_umac_scan_uid_type {
89 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
90 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
91 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
92 IWL_UMAC_SCAN_UID_SCHED_SCAN,
93};
94
95static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
96 enum iwl_umac_scan_uid_type type, bool notify);
97
98static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
99{
100 if (mvm->scan_rx_ant != ANT_NONE)
101 return mvm->scan_rx_ant;
Moshe Harela0544272014-12-08 21:13:14 +0200102 return iwl_mvm_get_valid_rx_ant(mvm);
David Spinadeld2496222014-05-20 12:46:37 +0300103}
104
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
106{
107 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300108 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109
David Spinadeld2496222014-05-20 12:46:37 +0300110 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100111 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
112 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
113 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
114 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
115 return cpu_to_le16(rx_chain);
116}
117
David Spinadelfb98be52014-05-04 12:51:10 +0300118static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119{
David Spinadelfb98be52014-05-04 12:51:10 +0300120 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100121 return cpu_to_le32(PHY_BAND_24);
122 else
123 return cpu_to_le32(PHY_BAND_5);
124}
125
126static inline __le32
127iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
128 bool no_cck)
129{
130 u32 tx_ant;
131
132 mvm->scan_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200133 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100134 mvm->scan_last_antenna_idx);
135 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
136
137 if (band == IEEE80211_BAND_2GHZ && !no_cck)
138 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
139 tx_ant);
140 else
141 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
142}
143
144/*
145 * We insert the SSIDs in an inverted order, because the FW will
146 * invert it back. The most prioritized SSID, which is first in the
147 * request list, is not copied here, but inserted directly to the probe
148 * request.
149 */
David Spinadelfb98be52014-05-04 12:51:10 +0300150static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
151 struct cfg80211_ssid *ssids,
152 int n_ssids, int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100153{
154 int fw_idx, req_idx;
155
David Spinadelfb98be52014-05-04 12:51:10 +0300156 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300157 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300158 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
159 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
160 memcpy(cmd_ssid[fw_idx].ssid,
161 ssids[req_idx].ssid,
162 ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100163 }
164}
165
166/*
167 * If req->n_ssids > 0, it means we should do an active scan.
168 * In case of active scan w/o directed scan, we receive a zero-length SSID
169 * just to notify that this scan is active and not passive.
170 * In order to notify the FW of the number of SSIDs we wish to scan (including
171 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300172 * one for each SSID, and set the active bit (first). If the first SSID is
173 * already included in the probe template, so we need to set only
174 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100175 */
176static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
177{
178 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300179 return 20 + 3 * (n_ssids + 1);
180 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100181}
182
183static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
184{
185 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
186}
187
188static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300189 struct cfg80211_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200190 bool basic_ssid,
191 struct iwl_mvm_scan_params *params)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100192{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100193 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
194 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
195 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300196 int type = BIT(req->n_ssids) - 1;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200197 enum ieee80211_band band = req->channels[0]->band;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300198
199 if (!basic_ssid)
200 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100201
202 for (i = 0; i < cmd->channel_count; i++) {
203 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300204 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200205 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300206 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200207 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
208 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100209 chan->iteration_count = cpu_to_le16(1);
210 chan++;
211 }
212}
213
214/*
215 * Fill in probe request with the following parameters:
216 * TA is our vif HW address, which mac80211 ensures we have.
217 * Packet is broadcasted, so this is both SA and DA.
218 * The probe request IE is made out of two: first comes the most prioritized
219 * SSID if a directed scan is requested. Second comes whatever extra
220 * information was given to us as the scan request IE.
221 */
222static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
223 int n_ssids, const u8 *ssid, int ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200224 const u8 *band_ie, int band_ie_len,
225 const u8 *common_ie, int common_ie_len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100226 int left)
227{
228 int len = 0;
229 u8 *pos = NULL;
230
231 /* Make sure there is enough space for the probe request,
232 * two mandatory IEs and the data */
233 left -= 24;
234 if (left < 0)
235 return 0;
236
237 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
238 eth_broadcast_addr(frame->da);
239 memcpy(frame->sa, ta, ETH_ALEN);
240 eth_broadcast_addr(frame->bssid);
241 frame->seq_ctrl = 0;
242
243 len += 24;
244
245 /* for passive scans, no need to fill anything */
246 if (n_ssids == 0)
247 return (u16)len;
248
249 /* points to the payload of the request */
250 pos = &frame->u.probe_req.variable[0];
251
252 /* fill in our SSID IE */
253 left -= ssid_len + 2;
254 if (left < 0)
255 return 0;
256 *pos++ = WLAN_EID_SSID;
257 *pos++ = ssid_len;
258 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
259 memcpy(pos, ssid, ssid_len);
260 pos += ssid_len;
261 }
262
263 len += ssid_len + 2;
264
David Spinadel633e2712014-02-06 16:15:23 +0200265 if (WARN_ON(left < band_ie_len + common_ie_len))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100266 return len;
267
David Spinadel633e2712014-02-06 16:15:23 +0200268 if (band_ie && band_ie_len) {
269 memcpy(pos, band_ie, band_ie_len);
270 pos += band_ie_len;
271 len += band_ie_len;
272 }
273
274 if (common_ie && common_ie_len) {
275 memcpy(pos, common_ie, common_ie_len);
276 pos += common_ie_len;
277 len += common_ie_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100278 }
279
280 return (u16)len;
281}
282
Alexander Bondar8a110d92014-03-12 17:31:19 +0200283static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
284 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200285{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200286 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200287 int *global_cnt = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200288
David Spinadel1e2ebe02014-10-26 15:53:27 +0200289 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
290 mvmvif->phy_ctxt->id < MAX_PHYS)
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200291 *global_cnt += 1;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200292}
293
294static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200295 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200296 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200297 struct iwl_mvm_scan_params *params)
298{
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200299 int global_cnt = 0;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200300 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300301 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200302
303 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
304 IEEE80211_IFACE_ITER_NORMAL,
305 iwl_mvm_scan_condition_iterator,
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200306 &global_cnt);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200307
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200308 if (!global_cnt)
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300309 goto not_bound;
310
David Spinadel2ce89cd2014-07-22 13:11:18 +0300311 params->suspend_time = 30;
312 params->max_out_time = 170;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300313
314 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300315 if (mvm->fw->ucode_capa.api[0] &
316 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
317 params->suspend_time = 105;
318 params->max_out_time = 70;
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200319 /*
320 * If there is more than one active interface make
321 * passive scan more fragmented.
322 */
323 frag_passive_dwell = (global_cnt < 2) ? 40 : 20;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300324 } else {
325 params->suspend_time = 120;
326 params->max_out_time = 120;
327 }
328 }
329
330 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
331 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
332 /*
333 * P2P device scan should not be fragmented to avoid negative
334 * impact on P2P device discovery. Configure max_out_time to be
335 * equal to dwell time on passive channel. Take a longest
336 * possible value, one that corresponds to 2GHz band
337 */
338 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
339 u32 passive_dwell =
340 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
341 params->max_out_time = passive_dwell;
342 } else {
343 params->passive_fragmented = true;
344 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200345 }
346
Johannes Bergab480032014-06-04 10:13:50 +0200347 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
348 params->max_out_time = 200;
349
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300350not_bound:
351
Alexander Bondar50df8a32014-03-12 20:30:51 +0200352 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300353 if (params->passive_fragmented)
354 params->dwell[band].passive = frag_passive_dwell;
355 else
356 params->dwell[band].passive =
357 iwl_mvm_get_passive_dwell(band);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200358 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
359 n_ssids);
360 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200361}
362
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300363static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
364{
365 /* require rrm scan whenever the fw supports it */
366 return mvm->fw->ucode_capa.capa[0] &
367 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
368}
369
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300370static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
371 bool is_sched_scan)
372{
373 int max_probe_len;
374
375 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
376 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
377 else
378 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
379
380 /* we create the 802.11 header and SSID element */
381 max_probe_len -= 24 + 2;
382
383 /* basic ssid is added only for hw_scan with and old api */
384 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
385 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
386 !is_sched_scan)
387 max_probe_len -= 32;
388
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300389 /* DS parameter set element is added on 2.4GHZ band if required */
390 if (iwl_mvm_rrm_scan_needed(mvm))
391 max_probe_len -= 3;
392
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300393 return max_probe_len;
394}
395
396int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
397{
398 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
399
400 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
401 return max_ie_len;
402
403 /* TODO: [BUG] This function should return the maximum allowed size of
404 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
405 * in the same command. So the correct implementation of this function
406 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
407 * command has only 512 bytes and it would leave us with about 240
408 * bytes for scan IEs, which is clearly not enough. So meanwhile
409 * we will report an incorrect value. This may result in a failure to
410 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
411 * functions with -ENOBUFS, if a large enough probe will be provided.
412 */
413 return max_ie_len;
414}
415
Johannes Berg8ca151b2013-01-24 14:25:36 +0100416int iwl_mvm_scan_request(struct iwl_mvm *mvm,
417 struct ieee80211_vif *vif,
418 struct cfg80211_scan_request *req)
419{
420 struct iwl_host_cmd hcmd = {
421 .id = SCAN_REQUEST_CMD,
422 .len = { 0, },
423 .data = { mvm->scan_cmd, },
Johannes Berg8ca151b2013-01-24 14:25:36 +0100424 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
425 };
426 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
427 int ret;
428 u32 status;
429 int ssid_len = 0;
430 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300431 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
432 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200433 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100434
435 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200436
437 /* we should have failed registration if scan_cmd was NULL */
438 if (WARN_ON(mvm->scan_cmd == NULL))
439 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100440
441 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
442 mvm->scan_status = IWL_MVM_SCAN_OS;
David Spinadel05646792014-06-02 09:59:49 +0300443 memset(cmd, 0, ksize(cmd));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200444
Johannes Berg8ca151b2013-01-24 14:25:36 +0100445 cmd->channel_count = (u8)req->n_channels;
446 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
447 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
448 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200449
Johannes Bergab480032014-06-04 10:13:50 +0200450 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200451 cmd->max_out_time = cpu_to_le32(params.max_out_time);
452 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200453 if (params.passive_fragmented)
454 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200455
David Spinadelfb98be52014-05-04 12:51:10 +0300456 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100457 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
458 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200459
460 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
461 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
462 else
463 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
464
Johannes Berg8ca151b2013-01-24 14:25:36 +0100465 cmd->repeats = cpu_to_le32(1);
466
467 /*
468 * If the user asked for passive scan, don't change to active scan if
469 * you see any activity on the channel - remain passive.
470 */
471 if (req->n_ssids > 0) {
472 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300473 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300474 if (basic_ssid) {
475 ssid = req->ssids[0].ssid;
476 ssid_len = req->ssids[0].ssid_len;
477 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100478 } else {
479 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300480 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481 }
482
David Spinadelfb98be52014-05-04 12:51:10 +0300483 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
484 basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100485
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200486 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
Emmanuel Grumbach3856b782014-09-22 12:03:41 +0300487 3 << TX_CMD_FLG_BT_PRIO_POS);
488
Johannes Berg8ca151b2013-01-24 14:25:36 +0100489 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
490 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
491 cmd->tx_cmd.rate_n_flags =
492 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
493 req->no_cck);
494
495 cmd->tx_cmd.len =
496 cpu_to_le16(iwl_mvm_fill_probe_req(
497 (struct ieee80211_mgmt *)cmd->data,
498 vif->addr,
499 req->n_ssids, ssid, ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200500 req->ie, req->ie_len, NULL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100501 mvm->fw->ucode_capa.max_probe_length));
502
Alexander Bondar50df8a32014-03-12 20:30:51 +0200503 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100504
505 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
506 le16_to_cpu(cmd->tx_cmd.len) +
507 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
508 hcmd.len[0] = le16_to_cpu(cmd->len);
509
510 status = SCAN_RESPONSE_OK;
511 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
512 if (!ret && status == SCAN_RESPONSE_OK) {
513 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
514 } else {
515 /*
516 * If the scan failed, it usually means that the FW was unable
517 * to allocate the time events. Warn on it, but maybe we
518 * should try to send the command again with different params.
519 */
520 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
521 status, ret);
522 mvm->scan_status = IWL_MVM_SCAN_NONE;
523 ret = -EIO;
524 }
525 return ret;
526}
527
528int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
529 struct iwl_device_cmd *cmd)
530{
531 struct iwl_rx_packet *pkt = rxb_addr(rxb);
532 struct iwl_cmd_response *resp = (void *)pkt->data;
533
534 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
535 le32_to_cpu(resp->status));
536 return 0;
537}
538
Alexander Bondare5d74642014-12-09 19:15:49 +0200539int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
540 struct iwl_rx_cmd_buffer *rxb,
541 struct iwl_device_cmd *cmd)
542{
543 struct iwl_rx_packet *pkt = rxb_addr(rxb);
544 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
545
546 IWL_DEBUG_SCAN(mvm,
547 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
548 notif->status, notif->scanned_channels);
549 return 0;
550}
551
Johannes Berg8ca151b2013-01-24 14:25:36 +0100552int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
553 struct iwl_device_cmd *cmd)
554{
555 struct iwl_rx_packet *pkt = rxb_addr(rxb);
556 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
557
Arik Nemtsov91b80252014-02-10 12:49:39 +0200558 lockdep_assert_held(&mvm->mutex);
559
Johannes Berg8ca151b2013-01-24 14:25:36 +0100560 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
561 notif->status, notif->scanned_channels);
562
Arik Nemtsov91b80252014-02-10 12:49:39 +0200563 if (mvm->scan_status == IWL_MVM_SCAN_OS)
564 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100565 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
566
Arik Nemtsov519e2022013-10-17 17:51:35 +0300567 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
568
Johannes Berg8ca151b2013-01-24 14:25:36 +0100569 return 0;
570}
571
David Spinadelfb98be52014-05-04 12:51:10 +0300572int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
573 struct iwl_rx_cmd_buffer *rxb,
574 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300575{
576 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadel35a000b2013-08-28 09:29:43 +0300577
David Spinadeld2496222014-05-20 12:46:37 +0300578 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
579 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadelfb98be52014-05-04 12:51:10 +0300580 struct iwl_sched_scan_results *notif = (void *)pkt->data;
581
David Spinadeld2496222014-05-20 12:46:37 +0300582 if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
583 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300584 }
585
David Spinadeld2496222014-05-20 12:46:37 +0300586 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
587 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300588
589 return 0;
590}
591
Johannes Berg8ca151b2013-01-24 14:25:36 +0100592static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
593 struct iwl_rx_packet *pkt, void *data)
594{
595 struct iwl_mvm *mvm =
596 container_of(notif_wait, struct iwl_mvm, notif_wait);
597 struct iwl_scan_complete_notif *notif;
598 u32 *resp;
599
600 switch (pkt->hdr.cmd) {
601 case SCAN_ABORT_CMD:
602 resp = (void *)pkt->data;
603 if (*resp == CAN_ABORT_STATUS) {
604 IWL_DEBUG_SCAN(mvm,
605 "Scan can be aborted, wait until completion\n");
606 return false;
607 }
608
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300609 /*
610 * If scan cannot be aborted, it means that we had a
611 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
612 * ieee80211_scan_completed already.
613 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100614 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
615 *resp);
616 return true;
617
618 case SCAN_COMPLETE_NOTIFICATION:
619 notif = (void *)pkt->data;
620 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
621 notif->status);
622 return true;
623
624 default:
625 WARN_ON(1);
626 return false;
627 };
628}
629
David Spinadelfb98be52014-05-04 12:51:10 +0300630static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100631{
632 struct iwl_notification_wait wait_scan_abort;
633 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
634 SCAN_COMPLETE_NOTIFICATION };
635 int ret;
636
637 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
638 scan_abort_notif,
639 ARRAY_SIZE(scan_abort_notif),
640 iwl_mvm_scan_abort_notif, NULL);
641
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300642 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100643 if (ret) {
644 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200645 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100646 goto out_remove_notif;
647 }
648
Arik Nemtsov91b80252014-02-10 12:49:39 +0200649 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100650
651out_remove_notif:
652 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200653 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100654}
David Spinadel35a000b2013-08-28 09:29:43 +0300655
656int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
657 struct iwl_rx_cmd_buffer *rxb,
658 struct iwl_device_cmd *cmd)
659{
660 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadelfb98be52014-05-04 12:51:10 +0300661 u8 status, ebs_status;
David Spinadel35a000b2013-08-28 09:29:43 +0300662
David Spinadelfb98be52014-05-04 12:51:10 +0300663 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
664 struct iwl_periodic_scan_complete *scan_notif;
665
666 scan_notif = (void *)pkt->data;
667 status = scan_notif->status;
668 ebs_status = scan_notif->ebs_status;
669 } else {
670 struct iwl_scan_offload_complete *scan_notif;
671
672 scan_notif = (void *)pkt->data;
673 status = scan_notif->status;
674 ebs_status = scan_notif->ebs_status;
675 }
Johannes Berga6623e82014-01-27 15:40:53 +0100676 /* scan status must be locked for proper checking */
677 lockdep_assert_held(&mvm->mutex);
678
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300679 IWL_DEBUG_SCAN(mvm,
David Spinadelfb98be52014-05-04 12:51:10 +0300680 "%s completed, status %s, EBS status %s\n",
681 mvm->scan_status == IWL_MVM_SCAN_SCHED ?
682 "Scheduled scan" : "Scan",
683 status == IWL_SCAN_OFFLOAD_COMPLETED ?
684 "completed" : "aborted",
685 ebs_status == IWL_SCAN_EBS_SUCCESS ?
686 "success" : "failed");
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300687
David Spinadel35a000b2013-08-28 09:29:43 +0300688
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200689 /* only call mac80211 completion if the stop was initiated by FW */
690 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100691 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200692 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadelfb98be52014-05-04 12:51:10 +0300693 } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
694 mvm->scan_status = IWL_MVM_SCAN_NONE;
695 ieee80211_scan_completed(mvm->hw,
696 status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300697 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200698 }
David Spinadel35a000b2013-08-28 09:29:43 +0300699
David Spinadelfb98be52014-05-04 12:51:10 +0300700 mvm->last_ebs_successful = !ebs_status;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300701
David Spinadel35a000b2013-08-28 09:29:43 +0300702 return 0;
703}
704
705static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
706 struct ieee80211_vif *vif,
David Spinadel633e2712014-02-06 16:15:23 +0200707 struct ieee80211_scan_ies *ies,
David Spinadel35a000b2013-08-28 09:29:43 +0300708 enum ieee80211_band band,
709 struct iwl_tx_cmd *cmd,
710 u8 *data)
711{
712 u16 cmd_len;
713
714 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
715 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
716 cmd->sta_id = mvm->aux_sta.sta_id;
717
718 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
719
720 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
721 vif->addr,
722 1, NULL, 0,
David Spinadel633e2712014-02-06 16:15:23 +0200723 ies->ies[band], ies->len[band],
724 ies->common_ies, ies->common_ie_len,
David Spinadel35a000b2013-08-28 09:29:43 +0300725 SCAN_OFFLOAD_PROBE_REQ_SIZE);
726 cmd->len = cpu_to_le16(cmd_len);
727}
728
729static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
730 struct ieee80211_vif *vif,
731 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200732 struct iwl_scan_offload_cmd *scan,
733 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300734{
Eliad Peller89879412014-05-26 18:44:35 +0300735 scan->channel_count = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300736 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
737 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
738 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
739 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200740
Alexander Bondar50df8a32014-03-12 20:30:51 +0200741 scan->max_out_time = cpu_to_le32(params->max_out_time);
742 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200743
David Spinadel35a000b2013-08-28 09:29:43 +0300744 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
745 MAC_FILTER_IN_BEACON);
746 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
747 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200748
749 if (params->passive_fragmented)
750 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300751}
752
753static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
754{
755 int i;
756
757 for (i = 0; i < PROBE_OPTION_MAX; i++) {
758 if (!ssid_list[i].len)
759 break;
760 if (ssid_list[i].len == ssid_len &&
761 !memcmp(ssid_list->ssid, ssid, ssid_len))
762 return i;
763 }
764 return -1;
765}
766
767static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
David Spinadelfb98be52014-05-04 12:51:10 +0300768 struct iwl_ssid_ie *direct_scan,
769 u32 *ssid_bitmap, bool basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300770{
771 int i, j;
772 int index;
773
774 /*
775 * copy SSIDs from match list.
776 * iwl_config_sched_scan_profiles() uses the order of these ssids to
777 * config match list.
778 */
779 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100780 /* skip empty SSID matchsets */
781 if (!req->match_sets[i].ssid.ssid_len)
782 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300783 direct_scan[i].id = WLAN_EID_SSID;
784 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
785 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
786 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300787 }
788
789 /* add SSIDs from scan SSID list */
790 *ssid_bitmap = 0;
791 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
792 index = iwl_ssid_exist(req->ssids[j].ssid,
793 req->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300794 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300795 if (index < 0) {
David Spinadelfb98be52014-05-04 12:51:10 +0300796 if (!req->ssids[j].ssid_len && basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300797 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300798 direct_scan[i].id = WLAN_EID_SSID;
799 direct_scan[i].len = req->ssids[j].ssid_len;
800 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
801 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300802 *ssid_bitmap |= BIT(i + 1);
803 i++;
804 } else {
805 *ssid_bitmap |= BIT(index + 1);
806 }
807 }
808}
809
810static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
811 struct cfg80211_sched_scan_request *req,
David Spinadel762533b2014-06-05 11:20:43 +0300812 u8 *channels_buffer,
David Spinadel35a000b2013-08-28 09:29:43 +0300813 enum ieee80211_band band,
Eliad Peller89879412014-05-26 18:44:35 +0300814 int *head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200815 u32 ssid_bitmap,
816 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300817{
David Spinadel762533b2014-06-05 11:20:43 +0300818 u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
819 __le32 *type = (__le32 *)channels_buffer;
820 __le16 *channel_number = (__le16 *)(type + n_channels);
821 __le16 *iter_count = channel_number + n_channels;
822 __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
823 u8 *active_dwell = (u8 *)(iter_interval + n_channels);
824 u8 *passive_dwell = active_dwell + n_channels;
Eliad Peller89879412014-05-26 18:44:35 +0300825 int i, index = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300826
Eliad Peller89879412014-05-26 18:44:35 +0300827 for (i = 0; i < req->n_channels; i++) {
828 struct ieee80211_channel *chan = req->channels[i];
David Spinadel35a000b2013-08-28 09:29:43 +0300829
Eliad Peller89879412014-05-26 18:44:35 +0300830 if (chan->band != band)
831 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300832
Eliad Peller89879412014-05-26 18:44:35 +0300833 index = *head;
834 (*head)++;
David Spinadel35a000b2013-08-28 09:29:43 +0300835
David Spinadel762533b2014-06-05 11:20:43 +0300836 channel_number[index] = cpu_to_le16(chan->hw_value);
837 active_dwell[index] = params->dwell[band].active;
838 passive_dwell[index] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300839
David Spinadel762533b2014-06-05 11:20:43 +0300840 iter_count[index] = cpu_to_le16(1);
841 iter_interval[index] = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300842
Eliad Peller89879412014-05-26 18:44:35 +0300843 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
David Spinadel762533b2014-06-05 11:20:43 +0300844 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300845 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
846
David Spinadel762533b2014-06-05 11:20:43 +0300847 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
848 IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
David Spinadel35a000b2013-08-28 09:29:43 +0300849
Eliad Peller89879412014-05-26 18:44:35 +0300850 if (chan->flags & IEEE80211_CHAN_NO_HT40)
David Spinadel762533b2014-06-05 11:20:43 +0300851 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300852 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
853
854 /* scan for all SSIDs from req->ssids */
David Spinadel762533b2014-06-05 11:20:43 +0300855 type[index] |= cpu_to_le32(ssid_bitmap);
David Spinadel35a000b2013-08-28 09:29:43 +0300856 }
857}
858
859int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
860 struct ieee80211_vif *vif,
861 struct cfg80211_sched_scan_request *req,
David Spinadel633e2712014-02-06 16:15:23 +0200862 struct ieee80211_scan_ies *ies)
David Spinadel35a000b2013-08-28 09:29:43 +0300863{
David Spinadel35a000b2013-08-28 09:29:43 +0300864 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
865 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
866 int head = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300867 u32 ssid_bitmap;
868 int cmd_len;
869 int ret;
David Spinadel762533b2014-06-05 11:20:43 +0300870 u8 *probes;
David Spinadelfb98be52014-05-04 12:51:10 +0300871 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
872 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
David Spinadel35a000b2013-08-28 09:29:43 +0300873
874 struct iwl_scan_offload_cfg *scan_cfg;
875 struct iwl_host_cmd cmd = {
876 .id = SCAN_OFFLOAD_CONFIG_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300877 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200878 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300879
880 lockdep_assert_held(&mvm->mutex);
881
David Spinadel35a000b2013-08-28 09:29:43 +0300882 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
David Spinadel762533b2014-06-05 11:20:43 +0300883 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200884 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300885
886 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
887 if (!scan_cfg)
888 return -ENOMEM;
889
David Spinadel762533b2014-06-05 11:20:43 +0300890 probes = scan_cfg->data +
891 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
892
Johannes Bergab480032014-06-04 10:13:50 +0200893 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200894 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300895 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
896
David Spinadelfb98be52014-05-04 12:51:10 +0300897 iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
898 &ssid_bitmap, basic_ssid);
David Spinadel35a000b2013-08-28 09:29:43 +0300899 /* build tx frames for supported bands */
900 if (band_2ghz) {
901 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
902 IEEE80211_BAND_2GHZ,
903 &scan_cfg->scan_cmd.tx_cmd[0],
David Spinadel762533b2014-06-05 11:20:43 +0300904 probes);
905 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300906 IEEE80211_BAND_2GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200907 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300908 }
909 if (band_5ghz) {
910 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
911 IEEE80211_BAND_5GHZ,
912 &scan_cfg->scan_cmd.tx_cmd[1],
David Spinadel762533b2014-06-05 11:20:43 +0300913 probes +
David Spinadel35a000b2013-08-28 09:29:43 +0300914 SCAN_OFFLOAD_PROBE_REQ_SIZE);
David Spinadel762533b2014-06-05 11:20:43 +0300915 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300916 IEEE80211_BAND_5GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200917 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300918 }
919
920 cmd.data[0] = scan_cfg;
921 cmd.len[0] = cmd_len;
922 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
923
924 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
925
926 ret = iwl_mvm_send_cmd(mvm, &cmd);
927 kfree(scan_cfg);
928 return ret;
929}
930
931int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
932 struct cfg80211_sched_scan_request *req)
933{
934 struct iwl_scan_offload_profile *profile;
935 struct iwl_scan_offload_profile_cfg *profile_cfg;
936 struct iwl_scan_offload_blacklist *blacklist;
937 struct iwl_host_cmd cmd = {
938 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300939 .len[1] = sizeof(*profile_cfg),
940 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
941 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
942 };
943 int blacklist_len;
944 int i;
945 int ret;
946
947 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
948 return -EIO;
949
950 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
951 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
952 else
953 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
954
955 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
956 if (!blacklist)
957 return -ENOMEM;
958
959 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
960 if (!profile_cfg) {
961 ret = -ENOMEM;
962 goto free_blacklist;
963 }
964
965 cmd.data[0] = blacklist;
966 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
967 cmd.data[1] = profile_cfg;
968
969 /* No blacklist configuration */
970
971 profile_cfg->num_profiles = req->n_match_sets;
972 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
973 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
974 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200975 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
976 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300977
978 for (i = 0; i < req->n_match_sets; i++) {
979 profile = &profile_cfg->profiles[i];
980 profile->ssid_index = i;
981 /* Support any cipher and auth algorithm */
982 profile->unicast_cipher = 0xff;
983 profile->auth_alg = 0xff;
984 profile->network_type = IWL_NETWORK_TYPE_ANY;
985 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
986 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
987 }
988
989 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
990
991 ret = iwl_mvm_send_cmd(mvm, &cmd);
992 kfree(profile_cfg);
993free_blacklist:
994 kfree(blacklist);
995
996 return ret;
997}
998
David Spinadeld2496222014-05-20 12:46:37 +0300999static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
1000 struct cfg80211_sched_scan_request *req)
1001{
1002 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
1003 IWL_DEBUG_SCAN(mvm,
1004 "Sending scheduled scan with filtering, n_match_sets %d\n",
1005 req->n_match_sets);
1006 return false;
1007 }
1008
1009 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
1010 return true;
1011}
1012
David Spinadel35a000b2013-08-28 09:29:43 +03001013int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1014 struct cfg80211_sched_scan_request *req)
1015{
1016 struct iwl_scan_offload_req scan_req = {
1017 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
1018
1019 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001020 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001021 .schedule_line[0].full_scan_mul = 1,
1022
1023 .schedule_line[1].iterations = 0xff,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001024 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001025 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
1026 };
1027
David Spinadeld2496222014-05-20 12:46:37 +03001028 if (iwl_mvm_scan_pass_all(mvm, req))
Eliad Pellerde33fb52013-11-10 12:59:46 +02001029 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +03001030
Haim Dreyfusse820c2d2014-04-06 11:19:09 +03001031 if (mvm->last_ebs_successful &&
1032 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1033 scan_req.flags |=
1034 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1035
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001036 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
David Spinadel35a000b2013-08-28 09:29:43 +03001037 sizeof(scan_req), &scan_req);
1038}
1039
Luciano Coelhob141c232014-10-01 13:22:40 +03001040int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1041 struct ieee80211_vif *vif,
1042 struct cfg80211_sched_scan_request *req,
1043 struct ieee80211_scan_ies *ies)
1044{
1045 int ret;
1046
David Spinadelb975e552014-11-17 12:30:05 +02001047 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1048 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1049 if (ret)
1050 return ret;
1051 ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
1052 } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadeld2496222014-05-20 12:46:37 +03001053 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001054 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1055 if (ret)
1056 return ret;
1057 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1058 } else {
David Spinadeld2496222014-05-20 12:46:37 +03001059 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001060 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1061 if (ret)
1062 return ret;
1063 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1064 if (ret)
1065 return ret;
1066 ret = iwl_mvm_sched_scan_start(mvm, req);
1067 }
1068
1069 return ret;
1070}
1071
David Spinadelfb98be52014-05-04 12:51:10 +03001072static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +03001073{
1074 int ret;
1075 struct iwl_host_cmd cmd = {
1076 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +03001077 };
1078 u32 status;
1079
1080 /* Exit instantly with error when device is not ready
1081 * to receive scan abort command or it does not perform
1082 * scheduled scan currently */
David Spinadelfb98be52014-05-04 12:51:10 +03001083 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1084 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1085 mvm->scan_status != IWL_MVM_SCAN_OS))
David Spinadel35a000b2013-08-28 09:29:43 +03001086 return -EIO;
1087
1088 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1089 if (ret)
1090 return ret;
1091
1092 if (status != CAN_ABORT_STATUS) {
1093 /*
1094 * The scan abort will return 1 for success or
1095 * 2 for "failure". A failure condition can be
1096 * due to simply not being in an active scan which
1097 * can occur if we send the scan abort before the
1098 * microcode has notified us that a scan is completed.
1099 */
1100 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001101 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +03001102 }
1103
1104 return ret;
1105}
1106
David Spinadelfb98be52014-05-04 12:51:10 +03001107int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +03001108{
1109 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001110 struct iwl_notification_wait wait_scan_done;
1111 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadelfb98be52014-05-04 12:51:10 +03001112 bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
David Spinadel35a000b2013-08-28 09:29:43 +03001113
1114 lockdep_assert_held(&mvm->mutex);
1115
David Spinadeld2496222014-05-20 12:46:37 +03001116 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1117 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
1118 notify);
1119
David Spinadelfb98be52014-05-04 12:51:10 +03001120 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1121 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1122 mvm->scan_status != IWL_MVM_SCAN_OS)) {
1123 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001124 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001125 }
1126
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001127 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1128 scan_done_notif,
1129 ARRAY_SIZE(scan_done_notif),
1130 NULL, NULL);
1131
David Spinadelfb98be52014-05-04 12:51:10 +03001132 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001133 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +03001134 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1135 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001136 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1137 return ret;
1138 }
1139
David Spinadelfb98be52014-05-04 12:51:10 +03001140 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1141 sched ? "offloaded " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001142
1143 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1144 if (ret)
1145 return ret;
1146
1147 /*
1148 * Clear the scan status so the next scan requests will succeed. This
1149 * also ensures the Rx handler doesn't do anything, as the scan was
Eliad Peller4ff78182014-06-22 14:44:44 +03001150 * stopped from above. Since the rx handler won't do anything now,
1151 * we have to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001152 */
Eliad Peller4ff78182014-06-22 14:44:44 +03001153 if (mvm->scan_status == IWL_MVM_SCAN_OS)
1154 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1155
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001156 mvm->scan_status = IWL_MVM_SCAN_NONE;
1157
David Spinadelfb98be52014-05-04 12:51:10 +03001158 if (notify) {
1159 if (sched)
1160 ieee80211_sched_scan_stopped(mvm->hw);
1161 else
1162 ieee80211_scan_completed(mvm->hw, true);
1163 }
David Spinadel636a2cd2014-05-01 15:57:22 +03001164
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001165 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001166}
David Spinadelfb98be52014-05-04 12:51:10 +03001167
1168static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1169 struct iwl_scan_req_tx_cmd *tx_cmd,
1170 bool no_cck)
1171{
1172 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1173 TX_CMD_FLG_BT_DIS);
1174 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1175 IEEE80211_BAND_2GHZ,
1176 no_cck);
1177 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1178
1179 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1180 TX_CMD_FLG_BT_DIS);
1181 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1182 IEEE80211_BAND_5GHZ,
1183 no_cck);
1184 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1185}
1186
1187static void
1188iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1189 struct ieee80211_channel **channels,
1190 int n_channels, u32 ssid_bitmap,
1191 struct iwl_scan_req_unified_lmac *cmd)
1192{
1193 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1194 int i;
1195
1196 for (i = 0; i < n_channels; i++) {
1197 channel_cfg[i].channel_num =
1198 cpu_to_le16(channels[i]->hw_value);
1199 channel_cfg[i].iter_count = cpu_to_le16(1);
1200 channel_cfg[i].iter_interval = 0;
1201 channel_cfg[i].flags =
1202 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1203 ssid_bitmap);
1204 }
1205}
1206
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001207static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
1208 size_t len, u8 *const pos)
1209{
1210 static const u8 before_ds_params[] = {
1211 WLAN_EID_SSID,
1212 WLAN_EID_SUPP_RATES,
1213 WLAN_EID_REQUEST,
1214 WLAN_EID_EXT_SUPP_RATES,
1215 };
1216 size_t offs;
1217 u8 *newpos = pos;
1218
1219 if (!iwl_mvm_rrm_scan_needed(mvm)) {
1220 memcpy(newpos, ies, len);
1221 return newpos + len;
1222 }
1223
1224 offs = ieee80211_ie_split(ies, len,
1225 before_ds_params,
1226 ARRAY_SIZE(before_ds_params),
1227 0);
1228
1229 memcpy(newpos, ies, offs);
1230 newpos += offs;
1231
1232 /* Add a placeholder for DS Parameter Set element */
1233 *newpos++ = WLAN_EID_DS_PARAMS;
1234 *newpos++ = 1;
1235 *newpos++ = 0;
1236
1237 memcpy(newpos, ies + offs, len - offs);
1238 newpos += len - offs;
1239
1240 return newpos;
1241}
1242
David Spinadelfb98be52014-05-04 12:51:10 +03001243static void
1244iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1245 struct ieee80211_scan_ies *ies,
Johannes Bergeffd05a2014-11-18 17:21:19 +01001246 struct iwl_scan_probe_req *preq,
1247 const u8 *mac_addr, const u8 *mac_addr_mask)
David Spinadelfb98be52014-05-04 12:51:10 +03001248{
David Spinadelfb98be52014-05-04 12:51:10 +03001249 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001250 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001251
Johannes Bergeffd05a2014-11-18 17:21:19 +01001252 /*
1253 * Unfortunately, right now the offload scan doesn't support randomising
1254 * within the firmware, so until the firmware API is ready we implement
1255 * it in the driver. This means that the scan iterations won't really be
1256 * random, only when it's restarted, but at least that helps a bit.
1257 */
1258 if (mac_addr)
1259 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
1260 else
1261 memcpy(frame->sa, vif->addr, ETH_ALEN);
1262
David Spinadelfb98be52014-05-04 12:51:10 +03001263 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1264 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +03001265 eth_broadcast_addr(frame->bssid);
1266 frame->seq_ctrl = 0;
1267
1268 pos = frame->u.probe_req.variable;
1269 *pos++ = WLAN_EID_SSID;
1270 *pos++ = 0;
1271
1272 preq->mac_header.offset = 0;
1273 preq->mac_header.len = cpu_to_le16(24 + 2);
1274
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001275 /* Insert ds parameter set element on 2.4 GHz band */
1276 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
1277 ies->ies[IEEE80211_BAND_2GHZ],
1278 ies->len[IEEE80211_BAND_2GHZ],
1279 pos);
David Spinadelfb98be52014-05-04 12:51:10 +03001280 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001281 preq->band_data[0].len = cpu_to_le16(newpos - pos);
1282 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001283
1284 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1285 ies->len[IEEE80211_BAND_5GHZ]);
1286 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1287 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1288 pos += ies->len[IEEE80211_BAND_5GHZ];
1289
1290 memcpy(pos, ies->common_ies, ies->common_ie_len);
1291 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1292 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1293}
1294
1295static void
1296iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1297 struct iwl_scan_req_unified_lmac *cmd,
1298 struct iwl_mvm_scan_params *params)
1299{
David Spinadelaf913442014-06-12 19:29:40 +03001300 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +03001301 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1302 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1303 if (params->passive_fragmented)
1304 cmd->fragmented_dwell =
1305 params->dwell[IEEE80211_BAND_2GHZ].passive;
David Spinadelfb98be52014-05-04 12:51:10 +03001306 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1307 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1308 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1309 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +03001310 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +03001311
1312 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1313 mvm->last_ebs_successful) {
1314 cmd->channel_opt[0].flags =
1315 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1316 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1317 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
Haim Dreyfussa1ed4022014-12-01 23:30:07 +02001318 cmd->channel_opt[0].non_ebs_ratio =
1319 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
David Spinadelaf913442014-06-12 19:29:40 +03001320 cmd->channel_opt[1].flags =
1321 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1322 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1323 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
Haim Dreyfussa1ed4022014-12-01 23:30:07 +02001324 cmd->channel_opt[1].non_ebs_ratio =
1325 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
David Spinadelaf913442014-06-12 19:29:40 +03001326 }
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001327
1328 if (iwl_mvm_rrm_scan_needed(mvm))
1329 cmd->scan_flags |=
1330 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +03001331}
1332
1333int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1334 struct ieee80211_vif *vif,
1335 struct ieee80211_scan_request *req)
1336{
1337 struct iwl_host_cmd hcmd = {
1338 .id = SCAN_OFFLOAD_REQUEST_CMD,
1339 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1340 sizeof(struct iwl_scan_channel_cfg_lmac) *
1341 mvm->fw->ucode_capa.n_scan_channels +
1342 sizeof(struct iwl_scan_probe_req), },
1343 .data = { mvm->scan_cmd, },
1344 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1345 };
1346 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001347 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001348 struct iwl_mvm_scan_params params = {};
1349 u32 flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001350 u32 ssid_bitmap = 0;
David Spinadelfb98be52014-05-04 12:51:10 +03001351 int ret, i;
1352
1353 lockdep_assert_held(&mvm->mutex);
1354
1355 /* we should have failed registration if scan_cmd was NULL */
1356 if (WARN_ON(mvm->scan_cmd == NULL))
1357 return -ENOMEM;
1358
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001359 if (req->req.n_ssids > PROBE_OPTION_MAX ||
1360 req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1361 req->ies.len[NL80211_BAND_5GHZ] >
1362 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1363 req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1364 return -ENOBUFS;
David Spinadelfb98be52014-05-04 12:51:10 +03001365
1366 mvm->scan_status = IWL_MVM_SCAN_OS;
1367
1368 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1369 &params);
1370
1371 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1372
1373 cmd->n_channels = (u8)req->req.n_channels;
1374
1375 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1376
1377 if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1378 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1379
1380 if (params.passive_fragmented)
1381 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1382
1383 if (req->req.n_ssids == 0)
1384 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1385
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001386 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001387
1388 cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1389 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1390 MAC_FILTER_IN_BEACON);
1391 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1392 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1393 req->req.n_ssids, 0);
1394
1395 cmd->schedule[0].delay = 0;
1396 cmd->schedule[0].iterations = 1;
1397 cmd->schedule[0].full_scan_mul = 0;
1398 cmd->schedule[1].delay = 0;
1399 cmd->schedule[1].iterations = 0;
1400 cmd->schedule[1].full_scan_mul = 0;
1401
1402 for (i = 1; i <= req->req.n_ssids; i++)
1403 ssid_bitmap |= BIT(i);
1404
1405 iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1406 req->req.n_channels, ssid_bitmap,
1407 cmd);
1408
David Spinadeld2496222014-05-20 12:46:37 +03001409 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1410 mvm->fw->ucode_capa.n_scan_channels);
1411
Johannes Bergeffd05a2014-11-18 17:21:19 +01001412 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
1413 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1414 req->req.mac_addr : NULL,
1415 req->req.mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001416
1417 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1418 if (!ret) {
1419 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1420 } else {
1421 /*
1422 * If the scan failed, it usually means that the FW was unable
1423 * to allocate the time events. Warn on it, but maybe we
1424 * should try to send the command again with different params.
1425 */
1426 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1427 mvm->scan_status = IWL_MVM_SCAN_NONE;
1428 ret = -EIO;
1429 }
1430 return ret;
1431}
1432
1433int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1434 struct ieee80211_vif *vif,
1435 struct cfg80211_sched_scan_request *req,
1436 struct ieee80211_scan_ies *ies)
1437{
1438 struct iwl_host_cmd hcmd = {
1439 .id = SCAN_OFFLOAD_REQUEST_CMD,
1440 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1441 sizeof(struct iwl_scan_channel_cfg_lmac) *
1442 mvm->fw->ucode_capa.n_scan_channels +
1443 sizeof(struct iwl_scan_probe_req), },
1444 .data = { mvm->scan_cmd, },
1445 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1446 };
1447 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001448 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001449 struct iwl_mvm_scan_params params = {};
1450 int ret;
1451 u32 flags = 0, ssid_bitmap = 0;
1452
1453 lockdep_assert_held(&mvm->mutex);
1454
1455 /* we should have failed registration if scan_cmd was NULL */
1456 if (WARN_ON(mvm->scan_cmd == NULL))
1457 return -ENOMEM;
1458
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001459 if (req->n_ssids > PROBE_OPTION_MAX ||
1460 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1461 ies->len[NL80211_BAND_5GHZ] >
1462 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1463 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
David Spinadelfb98be52014-05-04 12:51:10 +03001464 return -ENOBUFS;
1465
1466 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1467
1468 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1469
1470 cmd->n_channels = (u8)req->n_channels;
1471
David Spinadeld2496222014-05-20 12:46:37 +03001472 if (iwl_mvm_scan_pass_all(mvm, req))
David Spinadelfb98be52014-05-04 12:51:10 +03001473 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
David Spinadelfb98be52014-05-04 12:51:10 +03001474
1475 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1476 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1477
1478 if (params.passive_fragmented)
1479 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1480
1481 if (req->n_ssids == 0)
1482 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1483
Alexander Bondare5d74642014-12-09 19:15:49 +02001484#ifdef CONFIG_IWLWIFI_DEBUGFS
1485 if (mvm->scan_iter_notif_enabled)
1486 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
1487#endif
1488
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001489 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001490
1491 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1492 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1493 MAC_FILTER_IN_BEACON);
1494 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1495 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1496
David Spinadelb14fc2b2014-06-25 13:17:53 +03001497 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001498 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1499 cmd->schedule[0].full_scan_mul = 1;
1500
David Spinadelb14fc2b2014-06-25 13:17:53 +03001501 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001502 cmd->schedule[1].iterations = 0xff;
1503 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1504
1505 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1506 ssid_bitmap, cmd);
1507
David Spinadeld2496222014-05-20 12:46:37 +03001508 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1509 mvm->fw->ucode_capa.n_scan_channels);
1510
Johannes Bergeffd05a2014-11-18 17:21:19 +01001511 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
1512 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1513 req->mac_addr : NULL,
1514 req->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001515
1516 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1517 if (!ret) {
1518 IWL_DEBUG_SCAN(mvm,
1519 "Sched scan request was sent successfully\n");
1520 } else {
1521 /*
1522 * If the scan failed, it usually means that the FW was unable
1523 * to allocate the time events. Warn on it, but maybe we
1524 * should try to send the command again with different params.
1525 */
1526 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1527 mvm->scan_status = IWL_MVM_SCAN_NONE;
1528 ret = -EIO;
1529 }
1530 return ret;
1531}
1532
1533
1534int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1535{
David Spinadeld2496222014-05-20 12:46:37 +03001536 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1537 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1538 true);
1539
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001540 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1541 return 0;
1542
1543 if (iwl_mvm_is_radio_killed(mvm)) {
1544 ieee80211_scan_completed(mvm->hw, true);
1545 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1546 mvm->scan_status = IWL_MVM_SCAN_NONE;
1547 return 0;
1548 }
1549
David Spinadelfb98be52014-05-04 12:51:10 +03001550 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1551 return iwl_mvm_scan_offload_stop(mvm, true);
1552 return iwl_mvm_cancel_regular_scan(mvm);
1553}
David Spinadeld2496222014-05-20 12:46:37 +03001554
1555/* UMAC scan API */
1556
1557struct iwl_umac_scan_done {
1558 struct iwl_mvm *mvm;
1559 enum iwl_umac_scan_uid_type type;
1560};
1561
1562static int rate_to_scan_rate_flag(unsigned int rate)
1563{
1564 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1565 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1566 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1567 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1568 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1569 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1570 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1571 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1572 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1573 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1574 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1575 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1576 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1577 };
1578
1579 return rate_to_scan_rate[rate];
1580}
1581
1582static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1583{
1584 struct ieee80211_supported_band *band;
1585 unsigned int rates = 0;
1586 int i;
1587
1588 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1589 for (i = 0; i < band->n_bitrates; i++)
1590 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1591 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1592 for (i = 0; i < band->n_bitrates; i++)
1593 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1594
1595 /* Set both basic rates and supported rates */
1596 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1597
1598 return cpu_to_le32(rates);
1599}
1600
1601int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1602{
1603
1604 struct iwl_scan_config *scan_config;
1605 struct ieee80211_supported_band *band;
1606 int num_channels =
1607 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1608 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1609 int ret, i, j = 0, cmd_size, data_size;
1610 struct iwl_host_cmd cmd = {
1611 .id = SCAN_CFG_CMD,
1612 };
1613
1614 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1615 return -ENOBUFS;
1616
1617 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1618
1619 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1620 if (!scan_config)
1621 return -ENOMEM;
1622
1623 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1624 scan_config->hdr.size = cpu_to_le16(data_size);
1625 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1626 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1627 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1628 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1629 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1630 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1631 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1632 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1633 SCAN_CONFIG_N_CHANNELS(num_channels));
Moshe Harela0544272014-12-08 21:13:14 +02001634 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
David Spinadeld2496222014-05-20 12:46:37 +03001635 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1636 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1637 scan_config->out_of_channel_time = cpu_to_le32(170);
1638 scan_config->suspend_time = cpu_to_le32(30);
1639 scan_config->dwell_active = 20;
1640 scan_config->dwell_passive = 110;
1641 scan_config->dwell_fragmented = 20;
1642
1643 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1644
1645 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1646 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1647 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1648 IWL_CHANNEL_FLAG_EBS_ADD |
1649 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1650
1651 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1652 for (i = 0; i < band->n_channels; i++, j++)
1653 scan_config->channel_array[j] = band->channels[i].center_freq;
1654 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1655 for (i = 0; i < band->n_channels; i++, j++)
1656 scan_config->channel_array[j] = band->channels[i].center_freq;
1657
1658 cmd.data[0] = scan_config;
1659 cmd.len[0] = cmd_size;
1660 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1661
1662 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1663
1664 ret = iwl_mvm_send_cmd(mvm, &cmd);
1665
1666 kfree(scan_config);
1667 return ret;
1668}
1669
1670static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1671{
1672 int i;
1673
1674 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1675 if (mvm->scan_uid[i] == uid)
1676 return i;
1677
1678 return i;
1679}
1680
1681static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1682{
1683 return iwl_mvm_find_scan_uid(mvm, 0);
1684}
1685
1686static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1687 enum iwl_umac_scan_uid_type type)
1688{
1689 int i;
1690
1691 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1692 if (mvm->scan_uid[i] & type)
1693 return true;
1694
1695 return false;
1696}
1697
1698static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1699 enum iwl_umac_scan_uid_type type)
1700{
1701 u32 uid;
1702
1703 /* make sure exactly one bit is on in scan type */
1704 WARN_ON(hweight8(type) != 1);
1705
1706 /*
1707 * Make sure scan uids are unique. If one scan lasts long time while
1708 * others are completing frequently, the seq number will wrap up and
1709 * we may have more than one scan with the same uid.
1710 */
1711 do {
1712 uid = type | (mvm->scan_seq_num <<
1713 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1714 mvm->scan_seq_num++;
1715 } while (iwl_mvm_find_scan_uid(mvm, uid) <
1716 IWL_MVM_MAX_SIMULTANEOUS_SCANS);
1717
1718 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1719
1720 return uid;
1721}
1722
1723static void
1724iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1725 struct iwl_scan_req_umac *cmd,
1726 struct iwl_mvm_scan_params *params)
1727{
1728 memset(cmd, 0, ksize(cmd));
1729 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1730 sizeof(struct iwl_mvm_umac_cmd_hdr));
1731 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1732 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1733 if (params->passive_fragmented)
1734 cmd->fragmented_dwell =
1735 params->dwell[IEEE80211_BAND_2GHZ].passive;
1736 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1737 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1738 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1739}
1740
1741static void
1742iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1743 struct ieee80211_channel **channels,
1744 int n_channels, u32 ssid_bitmap,
1745 struct iwl_scan_req_umac *cmd)
1746{
1747 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1748 int i;
1749
1750 for (i = 0; i < n_channels; i++) {
1751 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1752 channel_cfg[i].channel_num = channels[i]->hw_value;
1753 channel_cfg[i].iter_count = 1;
1754 channel_cfg[i].iter_interval = 0;
1755 }
1756}
1757
1758static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1759 struct cfg80211_ssid *ssids,
1760 int fragmented)
1761{
1762 int flags = 0;
1763
1764 if (n_ssids == 0)
1765 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1766
1767 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1768 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1769
1770 if (fragmented)
1771 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1772
1773 if (iwl_mvm_rrm_scan_needed(mvm))
1774 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1775
1776 return flags;
1777}
1778
1779int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1780 struct ieee80211_scan_request *req)
1781{
1782 struct iwl_host_cmd hcmd = {
1783 .id = SCAN_REQ_UMAC,
1784 .len = { iwl_mvm_scan_size(mvm), },
1785 .data = { mvm->scan_cmd, },
1786 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1787 };
1788 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1789 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1790 sizeof(struct iwl_scan_channel_cfg_umac) *
1791 mvm->fw->ucode_capa.n_scan_channels;
1792 struct iwl_mvm_scan_params params = {};
1793 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001794 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001795 int ret, i, uid_idx;
1796
1797 lockdep_assert_held(&mvm->mutex);
1798
1799 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1800 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1801 return -EBUSY;
1802
1803 /* we should have failed registration if scan_cmd was NULL */
1804 if (WARN_ON(mvm->scan_cmd == NULL))
1805 return -ENOMEM;
1806
1807 if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
1808 req->ies.common_ie_len +
1809 req->ies.len[NL80211_BAND_2GHZ] +
1810 req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
1811 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
1812 mvm->fw->ucode_capa.n_scan_channels))
1813 return -ENOBUFS;
1814
1815 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1816 &params);
1817
1818 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1819
1820 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1821 mvm->scan_uid[uid_idx] = uid;
1822 cmd->uid = cpu_to_le32(uid);
1823
1824 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1825
1826 flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
1827 req->req.ssids,
1828 params.passive_fragmented);
1829
1830 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1831
1832 cmd->general_flags = cpu_to_le32(flags);
1833 cmd->n_channels = req->req.n_channels;
1834
1835 for (i = 0; i < req->req.n_ssids; i++)
1836 ssid_bitmap |= BIT(i);
1837
1838 iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
1839 req->req.n_channels, ssid_bitmap, cmd);
1840
1841 sec_part->schedule[0].iter_count = 1;
1842 sec_part->delay = 0;
1843
Johannes Bergeffd05a2014-11-18 17:21:19 +01001844 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
1845 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1846 req->req.mac_addr : NULL,
1847 req->req.mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001848
1849 iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
1850 req->req.n_ssids, 0);
1851
1852 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1853 if (!ret) {
1854 IWL_DEBUG_SCAN(mvm,
1855 "Scan request was sent successfully\n");
1856 } else {
1857 /*
1858 * If the scan failed, it usually means that the FW was unable
1859 * to allocate the time events. Warn on it, but maybe we
1860 * should try to send the command again with different params.
1861 */
1862 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1863 }
1864 return ret;
1865}
1866
1867int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1868 struct cfg80211_sched_scan_request *req,
1869 struct ieee80211_scan_ies *ies)
1870{
1871
1872 struct iwl_host_cmd hcmd = {
1873 .id = SCAN_REQ_UMAC,
1874 .len = { iwl_mvm_scan_size(mvm), },
1875 .data = { mvm->scan_cmd, },
1876 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1877 };
1878 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1879 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1880 sizeof(struct iwl_scan_channel_cfg_umac) *
1881 mvm->fw->ucode_capa.n_scan_channels;
1882 struct iwl_mvm_scan_params params = {};
1883 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001884 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001885 int ret, uid_idx;
1886
1887 lockdep_assert_held(&mvm->mutex);
1888
1889 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1890 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1891 return -EBUSY;
1892
1893 /* we should have failed registration if scan_cmd was NULL */
1894 if (WARN_ON(mvm->scan_cmd == NULL))
1895 return -ENOMEM;
1896
1897 if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1898 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1899 ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1900 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
1901 mvm->fw->ucode_capa.n_scan_channels))
1902 return -ENOBUFS;
1903
1904 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
1905 &params);
1906
1907 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1908
1909 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1910
1911 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1912 mvm->scan_uid[uid_idx] = uid;
1913 cmd->uid = cpu_to_le32(uid);
1914
1915 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1916
1917 flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
1918 params.passive_fragmented);
1919
1920 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1921
1922 if (iwl_mvm_scan_pass_all(mvm, req))
1923 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1924 else
1925 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1926
1927 cmd->general_flags = cpu_to_le32(flags);
1928
1929 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1930 mvm->last_ebs_successful)
1931 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1932 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1933 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1934
1935 cmd->n_channels = req->n_channels;
1936
1937 iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
1938 false);
1939
1940 /* This API uses bits 0-19 instead of 1-20. */
1941 ssid_bitmap = ssid_bitmap >> 1;
1942
1943 iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1944 ssid_bitmap, cmd);
1945
1946 sec_part->schedule[0].interval =
1947 cpu_to_le16(req->interval / MSEC_PER_SEC);
1948 sec_part->schedule[0].iter_count = 0xff;
1949
1950 sec_part->delay = 0;
1951
Johannes Bergeffd05a2014-11-18 17:21:19 +01001952 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
1953 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1954 req->mac_addr : NULL,
1955 req->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001956
1957 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1958 if (!ret) {
1959 IWL_DEBUG_SCAN(mvm,
1960 "Sched scan request was sent successfully\n");
1961 } else {
1962 /*
1963 * If the scan failed, it usually means that the FW was unable
1964 * to allocate the time events. Warn on it, but maybe we
1965 * should try to send the command again with different params.
1966 */
1967 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1968 }
1969 return ret;
1970}
1971
1972int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1973 struct iwl_rx_cmd_buffer *rxb,
1974 struct iwl_device_cmd *cmd)
1975{
1976 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1977 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1978 u32 uid = __le32_to_cpu(notif->uid);
1979 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1980 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1981
David Spinadel2992a322014-11-10 11:16:53 +02001982 /*
1983 * Scan uid may be set to zero in case of scan abort request from above.
1984 */
1985 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
David Spinadeld2496222014-05-20 12:46:37 +03001986 return 0;
1987
1988 IWL_DEBUG_SCAN(mvm,
1989 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1990 uid, sched ? "sched" : "regular",
1991 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1992 "completed" : "aborted",
1993 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1994 "success" : "failed");
1995
1996 mvm->last_ebs_successful = !notif->ebs_status;
1997 mvm->scan_uid[uid_idx] = 0;
1998
1999 if (!sched) {
2000 ieee80211_scan_completed(mvm->hw,
2001 notif->status ==
2002 IWL_SCAN_OFFLOAD_ABORTED);
2003 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2004 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
2005 ieee80211_sched_scan_stopped(mvm->hw);
2006 } else {
2007 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
2008 }
2009
2010 return 0;
2011}
2012
2013static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
2014 struct iwl_rx_packet *pkt, void *data)
2015{
2016 struct iwl_umac_scan_done *scan_done = data;
2017 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2018 u32 uid = __le32_to_cpu(notif->uid);
2019 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
2020
2021 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
2022 return false;
2023
2024 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
2025 return false;
2026
2027 /*
2028 * Clear scan uid of scans that was aborted from above and completed
2029 * in FW so the RX handler does nothing.
2030 */
2031 scan_done->mvm->scan_uid[uid_idx] = 0;
2032
2033 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
2034}
2035
2036static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
2037{
2038 struct iwl_umac_scan_abort cmd = {
2039 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
2040 sizeof(struct iwl_mvm_umac_cmd_hdr)),
2041 .uid = cpu_to_le32(uid),
2042 };
2043
2044 lockdep_assert_held(&mvm->mutex);
2045
2046 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2047
2048 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
2049}
2050
2051static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
2052 enum iwl_umac_scan_uid_type type, bool notify)
2053{
2054 struct iwl_notification_wait wait_scan_done;
2055 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
2056 struct iwl_umac_scan_done scan_done = {
2057 .mvm = mvm,
2058 .type = type,
2059 };
2060 int i, ret = -EIO;
2061
2062 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2063 scan_done_notif,
2064 ARRAY_SIZE(scan_done_notif),
2065 iwl_scan_umac_done_check, &scan_done);
2066
2067 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2068
2069 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
2070 if (mvm->scan_uid[i] & type) {
2071 int err;
2072
2073 if (iwl_mvm_is_radio_killed(mvm) &&
2074 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
2075 ieee80211_scan_completed(mvm->hw, true);
2076 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2077 break;
2078 }
2079
2080 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
2081 if (!err)
2082 ret = 0;
2083 }
2084 }
2085
2086 if (ret) {
2087 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
2088 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2089 return ret;
2090 }
2091
2092 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
2093 if (ret)
2094 return ret;
2095
2096 if (notify) {
2097 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
2098 ieee80211_sched_scan_stopped(mvm->hw);
2099 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
2100 ieee80211_scan_completed(mvm->hw, true);
2101 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2102 }
2103 }
2104
2105 return ret;
2106}
2107
2108int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2109{
2110 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
2111 return sizeof(struct iwl_scan_req_umac) +
2112 sizeof(struct iwl_scan_channel_cfg_umac) *
2113 mvm->fw->ucode_capa.n_scan_channels +
2114 sizeof(struct iwl_scan_req_umac_tail);
2115
2116 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
2117 return sizeof(struct iwl_scan_req_unified_lmac) +
2118 sizeof(struct iwl_scan_channel_cfg_lmac) *
2119 mvm->fw->ucode_capa.n_scan_channels +
2120 sizeof(struct iwl_scan_probe_req);
2121
2122 return sizeof(struct iwl_scan_cmd) +
2123 mvm->fw->ucode_capa.max_probe_length +
2124 mvm->fw->ucode_capa.n_scan_channels *
2125 sizeof(struct iwl_scan_channel);
2126}