blob: 8a1f4a8e80e763d7f55a208e21089aa282aa1a81 [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
Alexander Bondar8a110d92014-03-12 17:31:19 +020075
76struct iwl_mvm_scan_params {
77 u32 max_out_time;
78 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020079 bool passive_fragmented;
80 struct _dwell {
81 u16 passive;
82 u16 active;
83 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +020084};
Johannes Berg8ca151b2013-01-24 14:25:36 +010085
86static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
87{
88 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +030089 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010090
Oren Givon91b05d12013-08-19 08:36:48 +030091 if (mvm->scan_rx_ant != ANT_NONE)
92 rx_ant = mvm->scan_rx_ant;
93 else
Johannes Berg4ed735e2014-02-12 21:47:44 +010094 rx_ant = mvm->fw->valid_rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010095 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
96 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
97 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
98 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
99 return cpu_to_le16(rx_chain);
100}
101
David Spinadelfb98be52014-05-04 12:51:10 +0300102static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103{
David Spinadelfb98be52014-05-04 12:51:10 +0300104 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105 return cpu_to_le32(PHY_BAND_24);
106 else
107 return cpu_to_le32(PHY_BAND_5);
108}
109
110static inline __le32
111iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
112 bool no_cck)
113{
114 u32 tx_ant;
115
116 mvm->scan_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100117 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100118 mvm->scan_last_antenna_idx);
119 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
120
121 if (band == IEEE80211_BAND_2GHZ && !no_cck)
122 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
123 tx_ant);
124 else
125 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
126}
127
128/*
129 * We insert the SSIDs in an inverted order, because the FW will
130 * invert it back. The most prioritized SSID, which is first in the
131 * request list, is not copied here, but inserted directly to the probe
132 * request.
133 */
David Spinadelfb98be52014-05-04 12:51:10 +0300134static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
135 struct cfg80211_ssid *ssids,
136 int n_ssids, int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100137{
138 int fw_idx, req_idx;
139
David Spinadelfb98be52014-05-04 12:51:10 +0300140 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300141 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300142 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
143 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
144 memcpy(cmd_ssid[fw_idx].ssid,
145 ssids[req_idx].ssid,
146 ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100147 }
148}
149
150/*
151 * If req->n_ssids > 0, it means we should do an active scan.
152 * In case of active scan w/o directed scan, we receive a zero-length SSID
153 * just to notify that this scan is active and not passive.
154 * In order to notify the FW of the number of SSIDs we wish to scan (including
155 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300156 * one for each SSID, and set the active bit (first). If the first SSID is
157 * already included in the probe template, so we need to set only
158 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100159 */
160static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
161{
162 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300163 return 20 + 3 * (n_ssids + 1);
164 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100165}
166
167static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
168{
169 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
170}
171
172static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300173 struct cfg80211_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200174 bool basic_ssid,
175 struct iwl_mvm_scan_params *params)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100176{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100177 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
178 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
179 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300180 int type = BIT(req->n_ssids) - 1;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200181 enum ieee80211_band band = req->channels[0]->band;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300182
183 if (!basic_ssid)
184 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185
186 for (i = 0; i < cmd->channel_count; i++) {
187 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300188 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200189 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300190 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200191 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
192 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100193 chan->iteration_count = cpu_to_le16(1);
194 chan++;
195 }
196}
197
198/*
199 * Fill in probe request with the following parameters:
200 * TA is our vif HW address, which mac80211 ensures we have.
201 * Packet is broadcasted, so this is both SA and DA.
202 * The probe request IE is made out of two: first comes the most prioritized
203 * SSID if a directed scan is requested. Second comes whatever extra
204 * information was given to us as the scan request IE.
205 */
206static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
207 int n_ssids, const u8 *ssid, int ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200208 const u8 *band_ie, int band_ie_len,
209 const u8 *common_ie, int common_ie_len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100210 int left)
211{
212 int len = 0;
213 u8 *pos = NULL;
214
215 /* Make sure there is enough space for the probe request,
216 * two mandatory IEs and the data */
217 left -= 24;
218 if (left < 0)
219 return 0;
220
221 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
222 eth_broadcast_addr(frame->da);
223 memcpy(frame->sa, ta, ETH_ALEN);
224 eth_broadcast_addr(frame->bssid);
225 frame->seq_ctrl = 0;
226
227 len += 24;
228
229 /* for passive scans, no need to fill anything */
230 if (n_ssids == 0)
231 return (u16)len;
232
233 /* points to the payload of the request */
234 pos = &frame->u.probe_req.variable[0];
235
236 /* fill in our SSID IE */
237 left -= ssid_len + 2;
238 if (left < 0)
239 return 0;
240 *pos++ = WLAN_EID_SSID;
241 *pos++ = ssid_len;
242 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
243 memcpy(pos, ssid, ssid_len);
244 pos += ssid_len;
245 }
246
247 len += ssid_len + 2;
248
David Spinadel633e2712014-02-06 16:15:23 +0200249 if (WARN_ON(left < band_ie_len + common_ie_len))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100250 return len;
251
David Spinadel633e2712014-02-06 16:15:23 +0200252 if (band_ie && band_ie_len) {
253 memcpy(pos, band_ie, band_ie_len);
254 pos += band_ie_len;
255 len += band_ie_len;
256 }
257
258 if (common_ie && common_ie_len) {
259 memcpy(pos, common_ie, common_ie_len);
260 pos += common_ie_len;
261 len += common_ie_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100262 }
263
264 return (u16)len;
265}
266
Alexander Bondar8a110d92014-03-12 17:31:19 +0200267static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
268 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200269{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200270 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
271 bool *global_bound = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200272
David Spinadel1e2ebe02014-10-26 15:53:27 +0200273 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
274 mvmvif->phy_ctxt->id < MAX_PHYS)
Alexander Bondar8a110d92014-03-12 17:31:19 +0200275 *global_bound = true;
276}
277
278static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200279 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200280 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200281 struct iwl_mvm_scan_params *params)
282{
283 bool global_bound = false;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200284 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300285 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200286
287 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
288 IEEE80211_IFACE_ITER_NORMAL,
289 iwl_mvm_scan_condition_iterator,
290 &global_bound);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200291
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300292 if (!global_bound)
293 goto not_bound;
294
David Spinadel2ce89cd2014-07-22 13:11:18 +0300295 params->suspend_time = 30;
296 params->max_out_time = 170;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300297
298 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300299 if (mvm->fw->ucode_capa.api[0] &
300 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
301 params->suspend_time = 105;
302 params->max_out_time = 70;
303 frag_passive_dwell = 20;
304 } else {
305 params->suspend_time = 120;
306 params->max_out_time = 120;
307 }
308 }
309
310 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
311 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
312 /*
313 * P2P device scan should not be fragmented to avoid negative
314 * impact on P2P device discovery. Configure max_out_time to be
315 * equal to dwell time on passive channel. Take a longest
316 * possible value, one that corresponds to 2GHz band
317 */
318 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
319 u32 passive_dwell =
320 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
321 params->max_out_time = passive_dwell;
322 } else {
323 params->passive_fragmented = true;
324 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200325 }
326
Johannes Bergab480032014-06-04 10:13:50 +0200327 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
328 params->max_out_time = 200;
329
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300330not_bound:
331
Alexander Bondar50df8a32014-03-12 20:30:51 +0200332 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300333 if (params->passive_fragmented)
334 params->dwell[band].passive = frag_passive_dwell;
335 else
336 params->dwell[band].passive =
337 iwl_mvm_get_passive_dwell(band);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200338 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
339 n_ssids);
340 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200341}
342
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300343static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
344{
345 /* require rrm scan whenever the fw supports it */
346 return mvm->fw->ucode_capa.capa[0] &
347 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
348}
349
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300350static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
351 bool is_sched_scan)
352{
353 int max_probe_len;
354
355 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
356 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
357 else
358 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
359
360 /* we create the 802.11 header and SSID element */
361 max_probe_len -= 24 + 2;
362
363 /* basic ssid is added only for hw_scan with and old api */
364 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
365 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
366 !is_sched_scan)
367 max_probe_len -= 32;
368
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300369 /* DS parameter set element is added on 2.4GHZ band if required */
370 if (iwl_mvm_rrm_scan_needed(mvm))
371 max_probe_len -= 3;
372
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300373 return max_probe_len;
374}
375
376int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
377{
378 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
379
380 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
381 return max_ie_len;
382
383 /* TODO: [BUG] This function should return the maximum allowed size of
384 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
385 * in the same command. So the correct implementation of this function
386 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
387 * command has only 512 bytes and it would leave us with about 240
388 * bytes for scan IEs, which is clearly not enough. So meanwhile
389 * we will report an incorrect value. This may result in a failure to
390 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
391 * functions with -ENOBUFS, if a large enough probe will be provided.
392 */
393 return max_ie_len;
394}
395
Johannes Berg8ca151b2013-01-24 14:25:36 +0100396int iwl_mvm_scan_request(struct iwl_mvm *mvm,
397 struct ieee80211_vif *vif,
398 struct cfg80211_scan_request *req)
399{
400 struct iwl_host_cmd hcmd = {
401 .id = SCAN_REQUEST_CMD,
402 .len = { 0, },
403 .data = { mvm->scan_cmd, },
Johannes Berg8ca151b2013-01-24 14:25:36 +0100404 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
405 };
406 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
407 int ret;
408 u32 status;
409 int ssid_len = 0;
410 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300411 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
412 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200413 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100414
415 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200416
417 /* we should have failed registration if scan_cmd was NULL */
418 if (WARN_ON(mvm->scan_cmd == NULL))
419 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100420
421 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
422 mvm->scan_status = IWL_MVM_SCAN_OS;
David Spinadel05646792014-06-02 09:59:49 +0300423 memset(cmd, 0, ksize(cmd));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200424
Johannes Berg8ca151b2013-01-24 14:25:36 +0100425 cmd->channel_count = (u8)req->n_channels;
426 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
427 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
428 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200429
Johannes Bergab480032014-06-04 10:13:50 +0200430 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200431 cmd->max_out_time = cpu_to_le32(params.max_out_time);
432 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200433 if (params.passive_fragmented)
434 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200435
David Spinadelfb98be52014-05-04 12:51:10 +0300436 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100437 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
438 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200439
440 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
441 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
442 else
443 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
444
Johannes Berg8ca151b2013-01-24 14:25:36 +0100445 cmd->repeats = cpu_to_le32(1);
446
447 /*
448 * If the user asked for passive scan, don't change to active scan if
449 * you see any activity on the channel - remain passive.
450 */
451 if (req->n_ssids > 0) {
452 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300453 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300454 if (basic_ssid) {
455 ssid = req->ssids[0].ssid;
456 ssid_len = req->ssids[0].ssid_len;
457 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100458 } else {
459 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300460 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100461 }
462
David Spinadelfb98be52014-05-04 12:51:10 +0300463 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
464 basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100465
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200466 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
Emmanuel Grumbach3856b782014-09-22 12:03:41 +0300467 3 << TX_CMD_FLG_BT_PRIO_POS);
468
Johannes Berg8ca151b2013-01-24 14:25:36 +0100469 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
470 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
471 cmd->tx_cmd.rate_n_flags =
472 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
473 req->no_cck);
474
475 cmd->tx_cmd.len =
476 cpu_to_le16(iwl_mvm_fill_probe_req(
477 (struct ieee80211_mgmt *)cmd->data,
478 vif->addr,
479 req->n_ssids, ssid, ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200480 req->ie, req->ie_len, NULL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481 mvm->fw->ucode_capa.max_probe_length));
482
Alexander Bondar50df8a32014-03-12 20:30:51 +0200483 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100484
485 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
486 le16_to_cpu(cmd->tx_cmd.len) +
487 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
488 hcmd.len[0] = le16_to_cpu(cmd->len);
489
490 status = SCAN_RESPONSE_OK;
491 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
492 if (!ret && status == SCAN_RESPONSE_OK) {
493 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
494 } else {
495 /*
496 * If the scan failed, it usually means that the FW was unable
497 * to allocate the time events. Warn on it, but maybe we
498 * should try to send the command again with different params.
499 */
500 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
501 status, ret);
502 mvm->scan_status = IWL_MVM_SCAN_NONE;
503 ret = -EIO;
504 }
505 return ret;
506}
507
508int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
509 struct iwl_device_cmd *cmd)
510{
511 struct iwl_rx_packet *pkt = rxb_addr(rxb);
512 struct iwl_cmd_response *resp = (void *)pkt->data;
513
514 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
515 le32_to_cpu(resp->status));
516 return 0;
517}
518
519int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
520 struct iwl_device_cmd *cmd)
521{
522 struct iwl_rx_packet *pkt = rxb_addr(rxb);
523 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
524
Arik Nemtsov91b80252014-02-10 12:49:39 +0200525 lockdep_assert_held(&mvm->mutex);
526
Johannes Berg8ca151b2013-01-24 14:25:36 +0100527 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
528 notif->status, notif->scanned_channels);
529
Arik Nemtsov91b80252014-02-10 12:49:39 +0200530 if (mvm->scan_status == IWL_MVM_SCAN_OS)
531 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100532 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
533
Arik Nemtsov519e2022013-10-17 17:51:35 +0300534 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
535
Johannes Berg8ca151b2013-01-24 14:25:36 +0100536 return 0;
537}
538
David Spinadelfb98be52014-05-04 12:51:10 +0300539int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
540 struct iwl_rx_cmd_buffer *rxb,
541 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300542{
543 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadelfb98be52014-05-04 12:51:10 +0300544 u8 client_bitmap = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300545
David Spinadelfb98be52014-05-04 12:51:10 +0300546 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
547 struct iwl_sched_scan_results *notif = (void *)pkt->data;
548
549 client_bitmap = notif->client_bitmap;
550 }
551
552 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
553 client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
554 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
555 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
556 ieee80211_sched_scan_results(mvm->hw);
557 } else {
558 IWL_DEBUG_SCAN(mvm, "Scan results\n");
559 }
David Spinadel35a000b2013-08-28 09:29:43 +0300560 }
561
562 return 0;
563}
564
Johannes Berg8ca151b2013-01-24 14:25:36 +0100565static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
566 struct iwl_rx_packet *pkt, void *data)
567{
568 struct iwl_mvm *mvm =
569 container_of(notif_wait, struct iwl_mvm, notif_wait);
570 struct iwl_scan_complete_notif *notif;
571 u32 *resp;
572
573 switch (pkt->hdr.cmd) {
574 case SCAN_ABORT_CMD:
575 resp = (void *)pkt->data;
576 if (*resp == CAN_ABORT_STATUS) {
577 IWL_DEBUG_SCAN(mvm,
578 "Scan can be aborted, wait until completion\n");
579 return false;
580 }
581
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300582 /*
583 * If scan cannot be aborted, it means that we had a
584 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
585 * ieee80211_scan_completed already.
586 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100587 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
588 *resp);
589 return true;
590
591 case SCAN_COMPLETE_NOTIFICATION:
592 notif = (void *)pkt->data;
593 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
594 notif->status);
595 return true;
596
597 default:
598 WARN_ON(1);
599 return false;
600 };
601}
602
David Spinadelfb98be52014-05-04 12:51:10 +0300603static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100604{
605 struct iwl_notification_wait wait_scan_abort;
606 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
607 SCAN_COMPLETE_NOTIFICATION };
608 int ret;
609
610 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
611 scan_abort_notif,
612 ARRAY_SIZE(scan_abort_notif),
613 iwl_mvm_scan_abort_notif, NULL);
614
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300615 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100616 if (ret) {
617 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200618 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100619 goto out_remove_notif;
620 }
621
Arik Nemtsov91b80252014-02-10 12:49:39 +0200622 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100623
624out_remove_notif:
625 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200626 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100627}
David Spinadel35a000b2013-08-28 09:29:43 +0300628
629int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
630 struct iwl_rx_cmd_buffer *rxb,
631 struct iwl_device_cmd *cmd)
632{
633 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadelfb98be52014-05-04 12:51:10 +0300634 u8 status, ebs_status;
David Spinadel35a000b2013-08-28 09:29:43 +0300635
David Spinadelfb98be52014-05-04 12:51:10 +0300636 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
637 struct iwl_periodic_scan_complete *scan_notif;
638
639 scan_notif = (void *)pkt->data;
640 status = scan_notif->status;
641 ebs_status = scan_notif->ebs_status;
642 } else {
643 struct iwl_scan_offload_complete *scan_notif;
644
645 scan_notif = (void *)pkt->data;
646 status = scan_notif->status;
647 ebs_status = scan_notif->ebs_status;
648 }
Johannes Berga6623e82014-01-27 15:40:53 +0100649 /* scan status must be locked for proper checking */
650 lockdep_assert_held(&mvm->mutex);
651
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300652 IWL_DEBUG_SCAN(mvm,
David Spinadelfb98be52014-05-04 12:51:10 +0300653 "%s completed, status %s, EBS status %s\n",
654 mvm->scan_status == IWL_MVM_SCAN_SCHED ?
655 "Scheduled scan" : "Scan",
656 status == IWL_SCAN_OFFLOAD_COMPLETED ?
657 "completed" : "aborted",
658 ebs_status == IWL_SCAN_EBS_SUCCESS ?
659 "success" : "failed");
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300660
David Spinadel35a000b2013-08-28 09:29:43 +0300661
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200662 /* only call mac80211 completion if the stop was initiated by FW */
663 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100664 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200665 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadelfb98be52014-05-04 12:51:10 +0300666 } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
667 mvm->scan_status = IWL_MVM_SCAN_NONE;
668 ieee80211_scan_completed(mvm->hw,
669 status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300670 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200671 }
David Spinadel35a000b2013-08-28 09:29:43 +0300672
David Spinadelfb98be52014-05-04 12:51:10 +0300673 mvm->last_ebs_successful = !ebs_status;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300674
David Spinadel35a000b2013-08-28 09:29:43 +0300675 return 0;
676}
677
678static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
679 struct ieee80211_vif *vif,
David Spinadel633e2712014-02-06 16:15:23 +0200680 struct ieee80211_scan_ies *ies,
David Spinadel35a000b2013-08-28 09:29:43 +0300681 enum ieee80211_band band,
682 struct iwl_tx_cmd *cmd,
683 u8 *data)
684{
685 u16 cmd_len;
686
687 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
688 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
689 cmd->sta_id = mvm->aux_sta.sta_id;
690
691 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
692
693 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
694 vif->addr,
695 1, NULL, 0,
David Spinadel633e2712014-02-06 16:15:23 +0200696 ies->ies[band], ies->len[band],
697 ies->common_ies, ies->common_ie_len,
David Spinadel35a000b2013-08-28 09:29:43 +0300698 SCAN_OFFLOAD_PROBE_REQ_SIZE);
699 cmd->len = cpu_to_le16(cmd_len);
700}
701
702static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
703 struct ieee80211_vif *vif,
704 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200705 struct iwl_scan_offload_cmd *scan,
706 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300707{
Eliad Peller89879412014-05-26 18:44:35 +0300708 scan->channel_count = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300709 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
710 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
711 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
712 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200713
Alexander Bondar50df8a32014-03-12 20:30:51 +0200714 scan->max_out_time = cpu_to_le32(params->max_out_time);
715 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200716
David Spinadel35a000b2013-08-28 09:29:43 +0300717 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
718 MAC_FILTER_IN_BEACON);
719 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
720 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200721
722 if (params->passive_fragmented)
723 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300724}
725
726static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
727{
728 int i;
729
730 for (i = 0; i < PROBE_OPTION_MAX; i++) {
731 if (!ssid_list[i].len)
732 break;
733 if (ssid_list[i].len == ssid_len &&
734 !memcmp(ssid_list->ssid, ssid, ssid_len))
735 return i;
736 }
737 return -1;
738}
739
740static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
David Spinadelfb98be52014-05-04 12:51:10 +0300741 struct iwl_ssid_ie *direct_scan,
742 u32 *ssid_bitmap, bool basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300743{
744 int i, j;
745 int index;
746
747 /*
748 * copy SSIDs from match list.
749 * iwl_config_sched_scan_profiles() uses the order of these ssids to
750 * config match list.
751 */
752 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100753 /* skip empty SSID matchsets */
754 if (!req->match_sets[i].ssid.ssid_len)
755 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300756 direct_scan[i].id = WLAN_EID_SSID;
757 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
758 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
759 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300760 }
761
762 /* add SSIDs from scan SSID list */
763 *ssid_bitmap = 0;
764 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
765 index = iwl_ssid_exist(req->ssids[j].ssid,
766 req->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300767 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300768 if (index < 0) {
David Spinadelfb98be52014-05-04 12:51:10 +0300769 if (!req->ssids[j].ssid_len && basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300770 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300771 direct_scan[i].id = WLAN_EID_SSID;
772 direct_scan[i].len = req->ssids[j].ssid_len;
773 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
774 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300775 *ssid_bitmap |= BIT(i + 1);
776 i++;
777 } else {
778 *ssid_bitmap |= BIT(index + 1);
779 }
780 }
781}
782
783static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
784 struct cfg80211_sched_scan_request *req,
David Spinadel762533b2014-06-05 11:20:43 +0300785 u8 *channels_buffer,
David Spinadel35a000b2013-08-28 09:29:43 +0300786 enum ieee80211_band band,
Eliad Peller89879412014-05-26 18:44:35 +0300787 int *head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200788 u32 ssid_bitmap,
789 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300790{
David Spinadel762533b2014-06-05 11:20:43 +0300791 u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
792 __le32 *type = (__le32 *)channels_buffer;
793 __le16 *channel_number = (__le16 *)(type + n_channels);
794 __le16 *iter_count = channel_number + n_channels;
795 __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
796 u8 *active_dwell = (u8 *)(iter_interval + n_channels);
797 u8 *passive_dwell = active_dwell + n_channels;
Eliad Peller89879412014-05-26 18:44:35 +0300798 int i, index = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300799
Eliad Peller89879412014-05-26 18:44:35 +0300800 for (i = 0; i < req->n_channels; i++) {
801 struct ieee80211_channel *chan = req->channels[i];
David Spinadel35a000b2013-08-28 09:29:43 +0300802
Eliad Peller89879412014-05-26 18:44:35 +0300803 if (chan->band != band)
804 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300805
Eliad Peller89879412014-05-26 18:44:35 +0300806 index = *head;
807 (*head)++;
David Spinadel35a000b2013-08-28 09:29:43 +0300808
David Spinadel762533b2014-06-05 11:20:43 +0300809 channel_number[index] = cpu_to_le16(chan->hw_value);
810 active_dwell[index] = params->dwell[band].active;
811 passive_dwell[index] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300812
David Spinadel762533b2014-06-05 11:20:43 +0300813 iter_count[index] = cpu_to_le16(1);
814 iter_interval[index] = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300815
Eliad Peller89879412014-05-26 18:44:35 +0300816 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
David Spinadel762533b2014-06-05 11:20:43 +0300817 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300818 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
819
David Spinadel762533b2014-06-05 11:20:43 +0300820 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
821 IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
David Spinadel35a000b2013-08-28 09:29:43 +0300822
Eliad Peller89879412014-05-26 18:44:35 +0300823 if (chan->flags & IEEE80211_CHAN_NO_HT40)
David Spinadel762533b2014-06-05 11:20:43 +0300824 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300825 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
826
827 /* scan for all SSIDs from req->ssids */
David Spinadel762533b2014-06-05 11:20:43 +0300828 type[index] |= cpu_to_le32(ssid_bitmap);
David Spinadel35a000b2013-08-28 09:29:43 +0300829 }
830}
831
832int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
833 struct ieee80211_vif *vif,
834 struct cfg80211_sched_scan_request *req,
David Spinadel633e2712014-02-06 16:15:23 +0200835 struct ieee80211_scan_ies *ies)
David Spinadel35a000b2013-08-28 09:29:43 +0300836{
David Spinadel35a000b2013-08-28 09:29:43 +0300837 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
838 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
839 int head = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300840 u32 ssid_bitmap;
841 int cmd_len;
842 int ret;
David Spinadel762533b2014-06-05 11:20:43 +0300843 u8 *probes;
David Spinadelfb98be52014-05-04 12:51:10 +0300844 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
845 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
David Spinadel35a000b2013-08-28 09:29:43 +0300846
847 struct iwl_scan_offload_cfg *scan_cfg;
848 struct iwl_host_cmd cmd = {
849 .id = SCAN_OFFLOAD_CONFIG_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300850 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200851 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300852
853 lockdep_assert_held(&mvm->mutex);
854
David Spinadel35a000b2013-08-28 09:29:43 +0300855 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
David Spinadel762533b2014-06-05 11:20:43 +0300856 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200857 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300858
859 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
860 if (!scan_cfg)
861 return -ENOMEM;
862
David Spinadel762533b2014-06-05 11:20:43 +0300863 probes = scan_cfg->data +
864 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
865
Johannes Bergab480032014-06-04 10:13:50 +0200866 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200867 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300868 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
869
David Spinadelfb98be52014-05-04 12:51:10 +0300870 iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
871 &ssid_bitmap, basic_ssid);
David Spinadel35a000b2013-08-28 09:29:43 +0300872 /* build tx frames for supported bands */
873 if (band_2ghz) {
874 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
875 IEEE80211_BAND_2GHZ,
876 &scan_cfg->scan_cmd.tx_cmd[0],
David Spinadel762533b2014-06-05 11:20:43 +0300877 probes);
878 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300879 IEEE80211_BAND_2GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200880 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300881 }
882 if (band_5ghz) {
883 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
884 IEEE80211_BAND_5GHZ,
885 &scan_cfg->scan_cmd.tx_cmd[1],
David Spinadel762533b2014-06-05 11:20:43 +0300886 probes +
David Spinadel35a000b2013-08-28 09:29:43 +0300887 SCAN_OFFLOAD_PROBE_REQ_SIZE);
David Spinadel762533b2014-06-05 11:20:43 +0300888 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300889 IEEE80211_BAND_5GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200890 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300891 }
892
893 cmd.data[0] = scan_cfg;
894 cmd.len[0] = cmd_len;
895 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
896
897 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
898
899 ret = iwl_mvm_send_cmd(mvm, &cmd);
900 kfree(scan_cfg);
901 return ret;
902}
903
904int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
905 struct cfg80211_sched_scan_request *req)
906{
907 struct iwl_scan_offload_profile *profile;
908 struct iwl_scan_offload_profile_cfg *profile_cfg;
909 struct iwl_scan_offload_blacklist *blacklist;
910 struct iwl_host_cmd cmd = {
911 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300912 .len[1] = sizeof(*profile_cfg),
913 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
914 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
915 };
916 int blacklist_len;
917 int i;
918 int ret;
919
920 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
921 return -EIO;
922
923 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
924 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
925 else
926 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
927
928 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
929 if (!blacklist)
930 return -ENOMEM;
931
932 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
933 if (!profile_cfg) {
934 ret = -ENOMEM;
935 goto free_blacklist;
936 }
937
938 cmd.data[0] = blacklist;
939 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
940 cmd.data[1] = profile_cfg;
941
942 /* No blacklist configuration */
943
944 profile_cfg->num_profiles = req->n_match_sets;
945 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
946 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
947 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200948 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
949 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300950
951 for (i = 0; i < req->n_match_sets; i++) {
952 profile = &profile_cfg->profiles[i];
953 profile->ssid_index = i;
954 /* Support any cipher and auth algorithm */
955 profile->unicast_cipher = 0xff;
956 profile->auth_alg = 0xff;
957 profile->network_type = IWL_NETWORK_TYPE_ANY;
958 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
959 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
960 }
961
962 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
963
964 ret = iwl_mvm_send_cmd(mvm, &cmd);
965 kfree(profile_cfg);
966free_blacklist:
967 kfree(blacklist);
968
969 return ret;
970}
971
972int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
973 struct cfg80211_sched_scan_request *req)
974{
975 struct iwl_scan_offload_req scan_req = {
976 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
977
978 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
David Spinadelb14fc2b2014-06-25 13:17:53 +0300979 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +0300980 .schedule_line[0].full_scan_mul = 1,
981
982 .schedule_line[1].iterations = 0xff,
David Spinadelb14fc2b2014-06-25 13:17:53 +0300983 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +0300984 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
985 };
986
987 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
988 IWL_DEBUG_SCAN(mvm,
989 "Sending scheduled scan with filtering, filter len %d\n",
990 req->n_match_sets);
David Spinadel35a000b2013-08-28 09:29:43 +0300991 } else {
992 IWL_DEBUG_SCAN(mvm,
993 "Sending Scheduled scan without filtering\n");
Eliad Pellerde33fb52013-11-10 12:59:46 +0200994 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +0300995 }
996
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300997 if (mvm->last_ebs_successful &&
998 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
999 scan_req.flags |=
1000 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1001
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001002 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
David Spinadel35a000b2013-08-28 09:29:43 +03001003 sizeof(scan_req), &scan_req);
1004}
1005
Luciano Coelhob141c232014-10-01 13:22:40 +03001006int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1007 struct ieee80211_vif *vif,
1008 struct cfg80211_sched_scan_request *req,
1009 struct ieee80211_scan_ies *ies)
1010{
1011 int ret;
1012
1013 if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
1014 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1015 if (ret)
1016 return ret;
1017 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1018 } else {
1019 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1020 if (ret)
1021 return ret;
1022 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1023 if (ret)
1024 return ret;
1025 ret = iwl_mvm_sched_scan_start(mvm, req);
1026 }
1027
1028 return ret;
1029}
1030
David Spinadelfb98be52014-05-04 12:51:10 +03001031static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +03001032{
1033 int ret;
1034 struct iwl_host_cmd cmd = {
1035 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +03001036 };
1037 u32 status;
1038
1039 /* Exit instantly with error when device is not ready
1040 * to receive scan abort command or it does not perform
1041 * scheduled scan currently */
David Spinadelfb98be52014-05-04 12:51:10 +03001042 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1043 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1044 mvm->scan_status != IWL_MVM_SCAN_OS))
David Spinadel35a000b2013-08-28 09:29:43 +03001045 return -EIO;
1046
1047 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1048 if (ret)
1049 return ret;
1050
1051 if (status != CAN_ABORT_STATUS) {
1052 /*
1053 * The scan abort will return 1 for success or
1054 * 2 for "failure". A failure condition can be
1055 * due to simply not being in an active scan which
1056 * can occur if we send the scan abort before the
1057 * microcode has notified us that a scan is completed.
1058 */
1059 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001060 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +03001061 }
1062
1063 return ret;
1064}
1065
David Spinadelfb98be52014-05-04 12:51:10 +03001066int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +03001067{
1068 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001069 struct iwl_notification_wait wait_scan_done;
1070 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadelfb98be52014-05-04 12:51:10 +03001071 bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
David Spinadel35a000b2013-08-28 09:29:43 +03001072
1073 lockdep_assert_held(&mvm->mutex);
1074
David Spinadelfb98be52014-05-04 12:51:10 +03001075 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1076 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1077 mvm->scan_status != IWL_MVM_SCAN_OS)) {
1078 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001079 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001080 }
1081
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001082 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1083 scan_done_notif,
1084 ARRAY_SIZE(scan_done_notif),
1085 NULL, NULL);
1086
David Spinadelfb98be52014-05-04 12:51:10 +03001087 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001088 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +03001089 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1090 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001091 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1092 return ret;
1093 }
1094
David Spinadelfb98be52014-05-04 12:51:10 +03001095 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1096 sched ? "offloaded " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001097
1098 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1099 if (ret)
1100 return ret;
1101
1102 /*
1103 * Clear the scan status so the next scan requests will succeed. This
1104 * also ensures the Rx handler doesn't do anything, as the scan was
Eliad Peller4ff78182014-06-22 14:44:44 +03001105 * stopped from above. Since the rx handler won't do anything now,
1106 * we have to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001107 */
Eliad Peller4ff78182014-06-22 14:44:44 +03001108 if (mvm->scan_status == IWL_MVM_SCAN_OS)
1109 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1110
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001111 mvm->scan_status = IWL_MVM_SCAN_NONE;
1112
David Spinadelfb98be52014-05-04 12:51:10 +03001113 if (notify) {
1114 if (sched)
1115 ieee80211_sched_scan_stopped(mvm->hw);
1116 else
1117 ieee80211_scan_completed(mvm->hw, true);
1118 }
David Spinadel636a2cd2014-05-01 15:57:22 +03001119
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001120 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001121}
David Spinadelfb98be52014-05-04 12:51:10 +03001122
1123static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1124 struct iwl_scan_req_tx_cmd *tx_cmd,
1125 bool no_cck)
1126{
1127 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1128 TX_CMD_FLG_BT_DIS);
1129 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1130 IEEE80211_BAND_2GHZ,
1131 no_cck);
1132 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1133
1134 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1135 TX_CMD_FLG_BT_DIS);
1136 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1137 IEEE80211_BAND_5GHZ,
1138 no_cck);
1139 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1140}
1141
1142static void
1143iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1144 struct ieee80211_channel **channels,
1145 int n_channels, u32 ssid_bitmap,
1146 struct iwl_scan_req_unified_lmac *cmd)
1147{
1148 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1149 int i;
1150
1151 for (i = 0; i < n_channels; i++) {
1152 channel_cfg[i].channel_num =
1153 cpu_to_le16(channels[i]->hw_value);
1154 channel_cfg[i].iter_count = cpu_to_le16(1);
1155 channel_cfg[i].iter_interval = 0;
1156 channel_cfg[i].flags =
1157 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1158 ssid_bitmap);
1159 }
1160}
1161
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001162static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
1163 size_t len, u8 *const pos)
1164{
1165 static const u8 before_ds_params[] = {
1166 WLAN_EID_SSID,
1167 WLAN_EID_SUPP_RATES,
1168 WLAN_EID_REQUEST,
1169 WLAN_EID_EXT_SUPP_RATES,
1170 };
1171 size_t offs;
1172 u8 *newpos = pos;
1173
1174 if (!iwl_mvm_rrm_scan_needed(mvm)) {
1175 memcpy(newpos, ies, len);
1176 return newpos + len;
1177 }
1178
1179 offs = ieee80211_ie_split(ies, len,
1180 before_ds_params,
1181 ARRAY_SIZE(before_ds_params),
1182 0);
1183
1184 memcpy(newpos, ies, offs);
1185 newpos += offs;
1186
1187 /* Add a placeholder for DS Parameter Set element */
1188 *newpos++ = WLAN_EID_DS_PARAMS;
1189 *newpos++ = 1;
1190 *newpos++ = 0;
1191
1192 memcpy(newpos, ies + offs, len - offs);
1193 newpos += len - offs;
1194
1195 return newpos;
1196}
1197
David Spinadelfb98be52014-05-04 12:51:10 +03001198static void
1199iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1200 struct ieee80211_scan_ies *ies,
1201 struct iwl_scan_req_unified_lmac *cmd)
1202{
1203 struct iwl_scan_probe_req *preq = (void *)(cmd->data +
1204 sizeof(struct iwl_scan_channel_cfg_lmac) *
1205 mvm->fw->ucode_capa.n_scan_channels);
1206 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001207 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001208
1209 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1210 eth_broadcast_addr(frame->da);
1211 memcpy(frame->sa, vif->addr, ETH_ALEN);
1212 eth_broadcast_addr(frame->bssid);
1213 frame->seq_ctrl = 0;
1214
1215 pos = frame->u.probe_req.variable;
1216 *pos++ = WLAN_EID_SSID;
1217 *pos++ = 0;
1218
1219 preq->mac_header.offset = 0;
1220 preq->mac_header.len = cpu_to_le16(24 + 2);
1221
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001222 /* Insert ds parameter set element on 2.4 GHz band */
1223 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
1224 ies->ies[IEEE80211_BAND_2GHZ],
1225 ies->len[IEEE80211_BAND_2GHZ],
1226 pos);
David Spinadelfb98be52014-05-04 12:51:10 +03001227 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001228 preq->band_data[0].len = cpu_to_le16(newpos - pos);
1229 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001230
1231 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1232 ies->len[IEEE80211_BAND_5GHZ]);
1233 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1234 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1235 pos += ies->len[IEEE80211_BAND_5GHZ];
1236
1237 memcpy(pos, ies->common_ies, ies->common_ie_len);
1238 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1239 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1240}
1241
1242static void
1243iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1244 struct iwl_scan_req_unified_lmac *cmd,
1245 struct iwl_mvm_scan_params *params)
1246{
David Spinadelaf913442014-06-12 19:29:40 +03001247 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +03001248 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1249 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1250 if (params->passive_fragmented)
1251 cmd->fragmented_dwell =
1252 params->dwell[IEEE80211_BAND_2GHZ].passive;
David Spinadelfb98be52014-05-04 12:51:10 +03001253 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1254 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1255 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1256 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +03001257 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +03001258
1259 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1260 mvm->last_ebs_successful) {
1261 cmd->channel_opt[0].flags =
1262 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1263 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1264 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1265 cmd->channel_opt[1].flags =
1266 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1267 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1268 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1269 }
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001270
1271 if (iwl_mvm_rrm_scan_needed(mvm))
1272 cmd->scan_flags |=
1273 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +03001274}
1275
1276int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1277 struct ieee80211_vif *vif,
1278 struct ieee80211_scan_request *req)
1279{
1280 struct iwl_host_cmd hcmd = {
1281 .id = SCAN_OFFLOAD_REQUEST_CMD,
1282 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1283 sizeof(struct iwl_scan_channel_cfg_lmac) *
1284 mvm->fw->ucode_capa.n_scan_channels +
1285 sizeof(struct iwl_scan_probe_req), },
1286 .data = { mvm->scan_cmd, },
1287 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1288 };
1289 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1290 struct iwl_mvm_scan_params params = {};
1291 u32 flags;
1292 int ssid_bitmap = 0;
1293 int ret, i;
1294
1295 lockdep_assert_held(&mvm->mutex);
1296
1297 /* we should have failed registration if scan_cmd was NULL */
1298 if (WARN_ON(mvm->scan_cmd == NULL))
1299 return -ENOMEM;
1300
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001301 if (req->req.n_ssids > PROBE_OPTION_MAX ||
1302 req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1303 req->ies.len[NL80211_BAND_5GHZ] >
1304 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1305 req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1306 return -ENOBUFS;
David Spinadelfb98be52014-05-04 12:51:10 +03001307
1308 mvm->scan_status = IWL_MVM_SCAN_OS;
1309
1310 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1311 &params);
1312
1313 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1314
1315 cmd->n_channels = (u8)req->req.n_channels;
1316
1317 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1318
1319 if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1320 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1321
1322 if (params.passive_fragmented)
1323 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1324
1325 if (req->req.n_ssids == 0)
1326 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1327
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001328 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001329
1330 cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1331 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1332 MAC_FILTER_IN_BEACON);
1333 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1334 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1335 req->req.n_ssids, 0);
1336
1337 cmd->schedule[0].delay = 0;
1338 cmd->schedule[0].iterations = 1;
1339 cmd->schedule[0].full_scan_mul = 0;
1340 cmd->schedule[1].delay = 0;
1341 cmd->schedule[1].iterations = 0;
1342 cmd->schedule[1].full_scan_mul = 0;
1343
1344 for (i = 1; i <= req->req.n_ssids; i++)
1345 ssid_bitmap |= BIT(i);
1346
1347 iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1348 req->req.n_channels, ssid_bitmap,
1349 cmd);
1350
1351 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
1352
1353 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1354 if (!ret) {
1355 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1356 } else {
1357 /*
1358 * If the scan failed, it usually means that the FW was unable
1359 * to allocate the time events. Warn on it, but maybe we
1360 * should try to send the command again with different params.
1361 */
1362 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1363 mvm->scan_status = IWL_MVM_SCAN_NONE;
1364 ret = -EIO;
1365 }
1366 return ret;
1367}
1368
1369int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1370 struct ieee80211_vif *vif,
1371 struct cfg80211_sched_scan_request *req,
1372 struct ieee80211_scan_ies *ies)
1373{
1374 struct iwl_host_cmd hcmd = {
1375 .id = SCAN_OFFLOAD_REQUEST_CMD,
1376 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1377 sizeof(struct iwl_scan_channel_cfg_lmac) *
1378 mvm->fw->ucode_capa.n_scan_channels +
1379 sizeof(struct iwl_scan_probe_req), },
1380 .data = { mvm->scan_cmd, },
1381 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1382 };
1383 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1384 struct iwl_mvm_scan_params params = {};
1385 int ret;
1386 u32 flags = 0, ssid_bitmap = 0;
1387
1388 lockdep_assert_held(&mvm->mutex);
1389
1390 /* we should have failed registration if scan_cmd was NULL */
1391 if (WARN_ON(mvm->scan_cmd == NULL))
1392 return -ENOMEM;
1393
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001394 if (req->n_ssids > PROBE_OPTION_MAX ||
1395 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1396 ies->len[NL80211_BAND_5GHZ] >
1397 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1398 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
David Spinadelfb98be52014-05-04 12:51:10 +03001399 return -ENOBUFS;
1400
1401 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1402
1403 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1404
1405 cmd->n_channels = (u8)req->n_channels;
1406
1407 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
1408 IWL_DEBUG_SCAN(mvm,
1409 "Sending scheduled scan with filtering, n_match_sets %d\n",
1410 req->n_match_sets);
1411 } else {
1412 IWL_DEBUG_SCAN(mvm,
1413 "Sending Scheduled scan without filtering\n");
1414 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1415 }
1416
1417 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1418 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1419
1420 if (params.passive_fragmented)
1421 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1422
1423 if (req->n_ssids == 0)
1424 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1425
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001426 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001427
1428 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1429 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1430 MAC_FILTER_IN_BEACON);
1431 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1432 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1433
David Spinadelb14fc2b2014-06-25 13:17:53 +03001434 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001435 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1436 cmd->schedule[0].full_scan_mul = 1;
1437
David Spinadelb14fc2b2014-06-25 13:17:53 +03001438 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001439 cmd->schedule[1].iterations = 0xff;
1440 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1441
1442 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1443 ssid_bitmap, cmd);
1444
1445 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
1446
1447 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1448 if (!ret) {
1449 IWL_DEBUG_SCAN(mvm,
1450 "Sched scan request was sent successfully\n");
1451 } else {
1452 /*
1453 * If the scan failed, it usually means that the FW was unable
1454 * to allocate the time events. Warn on it, but maybe we
1455 * should try to send the command again with different params.
1456 */
1457 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1458 mvm->scan_status = IWL_MVM_SCAN_NONE;
1459 ret = -EIO;
1460 }
1461 return ret;
1462}
1463
1464
1465int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1466{
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001467 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1468 return 0;
1469
1470 if (iwl_mvm_is_radio_killed(mvm)) {
1471 ieee80211_scan_completed(mvm->hw, true);
1472 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1473 mvm->scan_status = IWL_MVM_SCAN_NONE;
1474 return 0;
1475 }
1476
David Spinadelfb98be52014-05-04 12:51:10 +03001477 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1478 return iwl_mvm_scan_offload_stop(mvm, true);
1479 return iwl_mvm_cancel_regular_scan(mvm);
1480}