blob: 7a001fb0d344d4b6519296b4f2658e7cdc21085e [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
David Spinadeld2496222014-05-20 12:46:37 +030086enum iwl_umac_scan_uid_type {
87 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
88 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
89 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
90 IWL_UMAC_SCAN_UID_SCHED_SCAN,
91};
92
93static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
94 enum iwl_umac_scan_uid_type type, bool notify);
95
96static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
97{
98 if (mvm->scan_rx_ant != ANT_NONE)
99 return mvm->scan_rx_ant;
100 return mvm->fw->valid_rx_ant;
101}
102
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
104{
105 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300106 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107
David Spinadeld2496222014-05-20 12:46:37 +0300108 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
110 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
111 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
112 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
113 return cpu_to_le16(rx_chain);
114}
115
David Spinadelfb98be52014-05-04 12:51:10 +0300116static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100117{
David Spinadelfb98be52014-05-04 12:51:10 +0300118 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119 return cpu_to_le32(PHY_BAND_24);
120 else
121 return cpu_to_le32(PHY_BAND_5);
122}
123
124static inline __le32
125iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
126 bool no_cck)
127{
128 u32 tx_ant;
129
130 mvm->scan_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100131 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100132 mvm->scan_last_antenna_idx);
133 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
134
135 if (band == IEEE80211_BAND_2GHZ && !no_cck)
136 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
137 tx_ant);
138 else
139 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
140}
141
142/*
143 * We insert the SSIDs in an inverted order, because the FW will
144 * invert it back. The most prioritized SSID, which is first in the
145 * request list, is not copied here, but inserted directly to the probe
146 * request.
147 */
David Spinadelfb98be52014-05-04 12:51:10 +0300148static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
149 struct cfg80211_ssid *ssids,
150 int n_ssids, int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100151{
152 int fw_idx, req_idx;
153
David Spinadelfb98be52014-05-04 12:51:10 +0300154 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300155 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300156 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
157 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
158 memcpy(cmd_ssid[fw_idx].ssid,
159 ssids[req_idx].ssid,
160 ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 }
162}
163
164/*
165 * If req->n_ssids > 0, it means we should do an active scan.
166 * In case of active scan w/o directed scan, we receive a zero-length SSID
167 * just to notify that this scan is active and not passive.
168 * In order to notify the FW of the number of SSIDs we wish to scan (including
169 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300170 * one for each SSID, and set the active bit (first). If the first SSID is
171 * already included in the probe template, so we need to set only
172 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100173 */
174static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
175{
176 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300177 return 20 + 3 * (n_ssids + 1);
178 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100179}
180
181static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
182{
183 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
184}
185
186static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300187 struct cfg80211_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200188 bool basic_ssid,
189 struct iwl_mvm_scan_params *params)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100190{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100191 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
192 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
193 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300194 int type = BIT(req->n_ssids) - 1;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200195 enum ieee80211_band band = req->channels[0]->band;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300196
197 if (!basic_ssid)
198 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100199
200 for (i = 0; i < cmd->channel_count; i++) {
201 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300202 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200203 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300204 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200205 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
206 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100207 chan->iteration_count = cpu_to_le16(1);
208 chan++;
209 }
210}
211
212/*
213 * Fill in probe request with the following parameters:
214 * TA is our vif HW address, which mac80211 ensures we have.
215 * Packet is broadcasted, so this is both SA and DA.
216 * The probe request IE is made out of two: first comes the most prioritized
217 * SSID if a directed scan is requested. Second comes whatever extra
218 * information was given to us as the scan request IE.
219 */
220static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
221 int n_ssids, const u8 *ssid, int ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200222 const u8 *band_ie, int band_ie_len,
223 const u8 *common_ie, int common_ie_len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100224 int left)
225{
226 int len = 0;
227 u8 *pos = NULL;
228
229 /* Make sure there is enough space for the probe request,
230 * two mandatory IEs and the data */
231 left -= 24;
232 if (left < 0)
233 return 0;
234
235 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
236 eth_broadcast_addr(frame->da);
237 memcpy(frame->sa, ta, ETH_ALEN);
238 eth_broadcast_addr(frame->bssid);
239 frame->seq_ctrl = 0;
240
241 len += 24;
242
243 /* for passive scans, no need to fill anything */
244 if (n_ssids == 0)
245 return (u16)len;
246
247 /* points to the payload of the request */
248 pos = &frame->u.probe_req.variable[0];
249
250 /* fill in our SSID IE */
251 left -= ssid_len + 2;
252 if (left < 0)
253 return 0;
254 *pos++ = WLAN_EID_SSID;
255 *pos++ = ssid_len;
256 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
257 memcpy(pos, ssid, ssid_len);
258 pos += ssid_len;
259 }
260
261 len += ssid_len + 2;
262
David Spinadel633e2712014-02-06 16:15:23 +0200263 if (WARN_ON(left < band_ie_len + common_ie_len))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100264 return len;
265
David Spinadel633e2712014-02-06 16:15:23 +0200266 if (band_ie && band_ie_len) {
267 memcpy(pos, band_ie, band_ie_len);
268 pos += band_ie_len;
269 len += band_ie_len;
270 }
271
272 if (common_ie && common_ie_len) {
273 memcpy(pos, common_ie, common_ie_len);
274 pos += common_ie_len;
275 len += common_ie_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100276 }
277
278 return (u16)len;
279}
280
Alexander Bondar8a110d92014-03-12 17:31:19 +0200281static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
282 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200283{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
285 bool *global_bound = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200286
David Spinadel1e2ebe02014-10-26 15:53:27 +0200287 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
288 mvmvif->phy_ctxt->id < MAX_PHYS)
Alexander Bondar8a110d92014-03-12 17:31:19 +0200289 *global_bound = true;
290}
291
292static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200293 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200294 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200295 struct iwl_mvm_scan_params *params)
296{
297 bool global_bound = false;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200298 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300299 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200300
301 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
302 IEEE80211_IFACE_ITER_NORMAL,
303 iwl_mvm_scan_condition_iterator,
304 &global_bound);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200305
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300306 if (!global_bound)
307 goto not_bound;
308
David Spinadel2ce89cd2014-07-22 13:11:18 +0300309 params->suspend_time = 30;
310 params->max_out_time = 170;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300311
312 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300313 if (mvm->fw->ucode_capa.api[0] &
314 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
315 params->suspend_time = 105;
316 params->max_out_time = 70;
317 frag_passive_dwell = 20;
318 } else {
319 params->suspend_time = 120;
320 params->max_out_time = 120;
321 }
322 }
323
324 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
325 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
326 /*
327 * P2P device scan should not be fragmented to avoid negative
328 * impact on P2P device discovery. Configure max_out_time to be
329 * equal to dwell time on passive channel. Take a longest
330 * possible value, one that corresponds to 2GHz band
331 */
332 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
333 u32 passive_dwell =
334 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
335 params->max_out_time = passive_dwell;
336 } else {
337 params->passive_fragmented = true;
338 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200339 }
340
Johannes Bergab480032014-06-04 10:13:50 +0200341 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
342 params->max_out_time = 200;
343
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300344not_bound:
345
Alexander Bondar50df8a32014-03-12 20:30:51 +0200346 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300347 if (params->passive_fragmented)
348 params->dwell[band].passive = frag_passive_dwell;
349 else
350 params->dwell[band].passive =
351 iwl_mvm_get_passive_dwell(band);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200352 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
353 n_ssids);
354 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200355}
356
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300357static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
358{
359 /* require rrm scan whenever the fw supports it */
360 return mvm->fw->ucode_capa.capa[0] &
361 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
362}
363
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300364static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
365 bool is_sched_scan)
366{
367 int max_probe_len;
368
369 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
370 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
371 else
372 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
373
374 /* we create the 802.11 header and SSID element */
375 max_probe_len -= 24 + 2;
376
377 /* basic ssid is added only for hw_scan with and old api */
378 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
379 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
380 !is_sched_scan)
381 max_probe_len -= 32;
382
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300383 /* DS parameter set element is added on 2.4GHZ band if required */
384 if (iwl_mvm_rrm_scan_needed(mvm))
385 max_probe_len -= 3;
386
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300387 return max_probe_len;
388}
389
390int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
391{
392 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
393
394 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
395 return max_ie_len;
396
397 /* TODO: [BUG] This function should return the maximum allowed size of
398 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
399 * in the same command. So the correct implementation of this function
400 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
401 * command has only 512 bytes and it would leave us with about 240
402 * bytes for scan IEs, which is clearly not enough. So meanwhile
403 * we will report an incorrect value. This may result in a failure to
404 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
405 * functions with -ENOBUFS, if a large enough probe will be provided.
406 */
407 return max_ie_len;
408}
409
Johannes Berg8ca151b2013-01-24 14:25:36 +0100410int iwl_mvm_scan_request(struct iwl_mvm *mvm,
411 struct ieee80211_vif *vif,
412 struct cfg80211_scan_request *req)
413{
414 struct iwl_host_cmd hcmd = {
415 .id = SCAN_REQUEST_CMD,
416 .len = { 0, },
417 .data = { mvm->scan_cmd, },
Johannes Berg8ca151b2013-01-24 14:25:36 +0100418 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
419 };
420 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
421 int ret;
422 u32 status;
423 int ssid_len = 0;
424 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300425 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
426 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200427 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100428
429 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200430
431 /* we should have failed registration if scan_cmd was NULL */
432 if (WARN_ON(mvm->scan_cmd == NULL))
433 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100434
435 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
436 mvm->scan_status = IWL_MVM_SCAN_OS;
David Spinadel05646792014-06-02 09:59:49 +0300437 memset(cmd, 0, ksize(cmd));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200438
Johannes Berg8ca151b2013-01-24 14:25:36 +0100439 cmd->channel_count = (u8)req->n_channels;
440 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
441 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
442 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200443
Johannes Bergab480032014-06-04 10:13:50 +0200444 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200445 cmd->max_out_time = cpu_to_le32(params.max_out_time);
446 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200447 if (params.passive_fragmented)
448 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200449
David Spinadelfb98be52014-05-04 12:51:10 +0300450 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100451 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
452 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200453
454 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
455 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
456 else
457 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
458
Johannes Berg8ca151b2013-01-24 14:25:36 +0100459 cmd->repeats = cpu_to_le32(1);
460
461 /*
462 * If the user asked for passive scan, don't change to active scan if
463 * you see any activity on the channel - remain passive.
464 */
465 if (req->n_ssids > 0) {
466 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300467 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300468 if (basic_ssid) {
469 ssid = req->ssids[0].ssid;
470 ssid_len = req->ssids[0].ssid_len;
471 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100472 } else {
473 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300474 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100475 }
476
David Spinadelfb98be52014-05-04 12:51:10 +0300477 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
478 basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100479
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200480 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
Emmanuel Grumbach3856b782014-09-22 12:03:41 +0300481 3 << TX_CMD_FLG_BT_PRIO_POS);
482
Johannes Berg8ca151b2013-01-24 14:25:36 +0100483 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
484 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
485 cmd->tx_cmd.rate_n_flags =
486 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
487 req->no_cck);
488
489 cmd->tx_cmd.len =
490 cpu_to_le16(iwl_mvm_fill_probe_req(
491 (struct ieee80211_mgmt *)cmd->data,
492 vif->addr,
493 req->n_ssids, ssid, ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200494 req->ie, req->ie_len, NULL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100495 mvm->fw->ucode_capa.max_probe_length));
496
Alexander Bondar50df8a32014-03-12 20:30:51 +0200497 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100498
499 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
500 le16_to_cpu(cmd->tx_cmd.len) +
501 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
502 hcmd.len[0] = le16_to_cpu(cmd->len);
503
504 status = SCAN_RESPONSE_OK;
505 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
506 if (!ret && status == SCAN_RESPONSE_OK) {
507 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
508 } else {
509 /*
510 * If the scan failed, it usually means that the FW was unable
511 * to allocate the time events. Warn on it, but maybe we
512 * should try to send the command again with different params.
513 */
514 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
515 status, ret);
516 mvm->scan_status = IWL_MVM_SCAN_NONE;
517 ret = -EIO;
518 }
519 return ret;
520}
521
522int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
523 struct iwl_device_cmd *cmd)
524{
525 struct iwl_rx_packet *pkt = rxb_addr(rxb);
526 struct iwl_cmd_response *resp = (void *)pkt->data;
527
528 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
529 le32_to_cpu(resp->status));
530 return 0;
531}
532
533int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
534 struct iwl_device_cmd *cmd)
535{
536 struct iwl_rx_packet *pkt = rxb_addr(rxb);
537 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
538
Arik Nemtsov91b80252014-02-10 12:49:39 +0200539 lockdep_assert_held(&mvm->mutex);
540
Johannes Berg8ca151b2013-01-24 14:25:36 +0100541 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
542 notif->status, notif->scanned_channels);
543
Arik Nemtsov91b80252014-02-10 12:49:39 +0200544 if (mvm->scan_status == IWL_MVM_SCAN_OS)
545 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100546 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
547
Arik Nemtsov519e2022013-10-17 17:51:35 +0300548 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
549
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 return 0;
551}
552
David Spinadelfb98be52014-05-04 12:51:10 +0300553int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
554 struct iwl_rx_cmd_buffer *rxb,
555 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300556{
557 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadel35a000b2013-08-28 09:29:43 +0300558
David Spinadeld2496222014-05-20 12:46:37 +0300559 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
560 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadelfb98be52014-05-04 12:51:10 +0300561 struct iwl_sched_scan_results *notif = (void *)pkt->data;
562
David Spinadeld2496222014-05-20 12:46:37 +0300563 if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
564 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300565 }
566
David Spinadeld2496222014-05-20 12:46:37 +0300567 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
568 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300569
570 return 0;
571}
572
Johannes Berg8ca151b2013-01-24 14:25:36 +0100573static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
574 struct iwl_rx_packet *pkt, void *data)
575{
576 struct iwl_mvm *mvm =
577 container_of(notif_wait, struct iwl_mvm, notif_wait);
578 struct iwl_scan_complete_notif *notif;
579 u32 *resp;
580
581 switch (pkt->hdr.cmd) {
582 case SCAN_ABORT_CMD:
583 resp = (void *)pkt->data;
584 if (*resp == CAN_ABORT_STATUS) {
585 IWL_DEBUG_SCAN(mvm,
586 "Scan can be aborted, wait until completion\n");
587 return false;
588 }
589
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300590 /*
591 * If scan cannot be aborted, it means that we had a
592 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
593 * ieee80211_scan_completed already.
594 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100595 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
596 *resp);
597 return true;
598
599 case SCAN_COMPLETE_NOTIFICATION:
600 notif = (void *)pkt->data;
601 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
602 notif->status);
603 return true;
604
605 default:
606 WARN_ON(1);
607 return false;
608 };
609}
610
David Spinadelfb98be52014-05-04 12:51:10 +0300611static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100612{
613 struct iwl_notification_wait wait_scan_abort;
614 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
615 SCAN_COMPLETE_NOTIFICATION };
616 int ret;
617
618 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
619 scan_abort_notif,
620 ARRAY_SIZE(scan_abort_notif),
621 iwl_mvm_scan_abort_notif, NULL);
622
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300623 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100624 if (ret) {
625 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200626 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100627 goto out_remove_notif;
628 }
629
Arik Nemtsov91b80252014-02-10 12:49:39 +0200630 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100631
632out_remove_notif:
633 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200634 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100635}
David Spinadel35a000b2013-08-28 09:29:43 +0300636
637int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
638 struct iwl_rx_cmd_buffer *rxb,
639 struct iwl_device_cmd *cmd)
640{
641 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadelfb98be52014-05-04 12:51:10 +0300642 u8 status, ebs_status;
David Spinadel35a000b2013-08-28 09:29:43 +0300643
David Spinadelfb98be52014-05-04 12:51:10 +0300644 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
645 struct iwl_periodic_scan_complete *scan_notif;
646
647 scan_notif = (void *)pkt->data;
648 status = scan_notif->status;
649 ebs_status = scan_notif->ebs_status;
650 } else {
651 struct iwl_scan_offload_complete *scan_notif;
652
653 scan_notif = (void *)pkt->data;
654 status = scan_notif->status;
655 ebs_status = scan_notif->ebs_status;
656 }
Johannes Berga6623e82014-01-27 15:40:53 +0100657 /* scan status must be locked for proper checking */
658 lockdep_assert_held(&mvm->mutex);
659
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300660 IWL_DEBUG_SCAN(mvm,
David Spinadelfb98be52014-05-04 12:51:10 +0300661 "%s completed, status %s, EBS status %s\n",
662 mvm->scan_status == IWL_MVM_SCAN_SCHED ?
663 "Scheduled scan" : "Scan",
664 status == IWL_SCAN_OFFLOAD_COMPLETED ?
665 "completed" : "aborted",
666 ebs_status == IWL_SCAN_EBS_SUCCESS ?
667 "success" : "failed");
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300668
David Spinadel35a000b2013-08-28 09:29:43 +0300669
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200670 /* only call mac80211 completion if the stop was initiated by FW */
671 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100672 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200673 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadelfb98be52014-05-04 12:51:10 +0300674 } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
675 mvm->scan_status = IWL_MVM_SCAN_NONE;
676 ieee80211_scan_completed(mvm->hw,
677 status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300678 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200679 }
David Spinadel35a000b2013-08-28 09:29:43 +0300680
David Spinadelfb98be52014-05-04 12:51:10 +0300681 mvm->last_ebs_successful = !ebs_status;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300682
David Spinadel35a000b2013-08-28 09:29:43 +0300683 return 0;
684}
685
686static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
687 struct ieee80211_vif *vif,
David Spinadel633e2712014-02-06 16:15:23 +0200688 struct ieee80211_scan_ies *ies,
David Spinadel35a000b2013-08-28 09:29:43 +0300689 enum ieee80211_band band,
690 struct iwl_tx_cmd *cmd,
691 u8 *data)
692{
693 u16 cmd_len;
694
695 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
696 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
697 cmd->sta_id = mvm->aux_sta.sta_id;
698
699 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
700
701 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
702 vif->addr,
703 1, NULL, 0,
David Spinadel633e2712014-02-06 16:15:23 +0200704 ies->ies[band], ies->len[band],
705 ies->common_ies, ies->common_ie_len,
David Spinadel35a000b2013-08-28 09:29:43 +0300706 SCAN_OFFLOAD_PROBE_REQ_SIZE);
707 cmd->len = cpu_to_le16(cmd_len);
708}
709
710static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
711 struct ieee80211_vif *vif,
712 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200713 struct iwl_scan_offload_cmd *scan,
714 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300715{
Eliad Peller89879412014-05-26 18:44:35 +0300716 scan->channel_count = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300717 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
718 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
719 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
720 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200721
Alexander Bondar50df8a32014-03-12 20:30:51 +0200722 scan->max_out_time = cpu_to_le32(params->max_out_time);
723 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200724
David Spinadel35a000b2013-08-28 09:29:43 +0300725 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
726 MAC_FILTER_IN_BEACON);
727 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
728 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200729
730 if (params->passive_fragmented)
731 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300732}
733
734static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
735{
736 int i;
737
738 for (i = 0; i < PROBE_OPTION_MAX; i++) {
739 if (!ssid_list[i].len)
740 break;
741 if (ssid_list[i].len == ssid_len &&
742 !memcmp(ssid_list->ssid, ssid, ssid_len))
743 return i;
744 }
745 return -1;
746}
747
748static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
David Spinadelfb98be52014-05-04 12:51:10 +0300749 struct iwl_ssid_ie *direct_scan,
750 u32 *ssid_bitmap, bool basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300751{
752 int i, j;
753 int index;
754
755 /*
756 * copy SSIDs from match list.
757 * iwl_config_sched_scan_profiles() uses the order of these ssids to
758 * config match list.
759 */
760 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100761 /* skip empty SSID matchsets */
762 if (!req->match_sets[i].ssid.ssid_len)
763 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300764 direct_scan[i].id = WLAN_EID_SSID;
765 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
766 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
767 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300768 }
769
770 /* add SSIDs from scan SSID list */
771 *ssid_bitmap = 0;
772 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
773 index = iwl_ssid_exist(req->ssids[j].ssid,
774 req->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300775 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300776 if (index < 0) {
David Spinadelfb98be52014-05-04 12:51:10 +0300777 if (!req->ssids[j].ssid_len && basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300778 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300779 direct_scan[i].id = WLAN_EID_SSID;
780 direct_scan[i].len = req->ssids[j].ssid_len;
781 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
782 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300783 *ssid_bitmap |= BIT(i + 1);
784 i++;
785 } else {
786 *ssid_bitmap |= BIT(index + 1);
787 }
788 }
789}
790
791static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
792 struct cfg80211_sched_scan_request *req,
David Spinadel762533b2014-06-05 11:20:43 +0300793 u8 *channels_buffer,
David Spinadel35a000b2013-08-28 09:29:43 +0300794 enum ieee80211_band band,
Eliad Peller89879412014-05-26 18:44:35 +0300795 int *head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200796 u32 ssid_bitmap,
797 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300798{
David Spinadel762533b2014-06-05 11:20:43 +0300799 u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
800 __le32 *type = (__le32 *)channels_buffer;
801 __le16 *channel_number = (__le16 *)(type + n_channels);
802 __le16 *iter_count = channel_number + n_channels;
803 __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
804 u8 *active_dwell = (u8 *)(iter_interval + n_channels);
805 u8 *passive_dwell = active_dwell + n_channels;
Eliad Peller89879412014-05-26 18:44:35 +0300806 int i, index = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300807
Eliad Peller89879412014-05-26 18:44:35 +0300808 for (i = 0; i < req->n_channels; i++) {
809 struct ieee80211_channel *chan = req->channels[i];
David Spinadel35a000b2013-08-28 09:29:43 +0300810
Eliad Peller89879412014-05-26 18:44:35 +0300811 if (chan->band != band)
812 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300813
Eliad Peller89879412014-05-26 18:44:35 +0300814 index = *head;
815 (*head)++;
David Spinadel35a000b2013-08-28 09:29:43 +0300816
David Spinadel762533b2014-06-05 11:20:43 +0300817 channel_number[index] = cpu_to_le16(chan->hw_value);
818 active_dwell[index] = params->dwell[band].active;
819 passive_dwell[index] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300820
David Spinadel762533b2014-06-05 11:20:43 +0300821 iter_count[index] = cpu_to_le16(1);
822 iter_interval[index] = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300823
Eliad Peller89879412014-05-26 18:44:35 +0300824 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
David Spinadel762533b2014-06-05 11:20:43 +0300825 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300826 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
827
David Spinadel762533b2014-06-05 11:20:43 +0300828 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
829 IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
David Spinadel35a000b2013-08-28 09:29:43 +0300830
Eliad Peller89879412014-05-26 18:44:35 +0300831 if (chan->flags & IEEE80211_CHAN_NO_HT40)
David Spinadel762533b2014-06-05 11:20:43 +0300832 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300833 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
834
835 /* scan for all SSIDs from req->ssids */
David Spinadel762533b2014-06-05 11:20:43 +0300836 type[index] |= cpu_to_le32(ssid_bitmap);
David Spinadel35a000b2013-08-28 09:29:43 +0300837 }
838}
839
840int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
841 struct ieee80211_vif *vif,
842 struct cfg80211_sched_scan_request *req,
David Spinadel633e2712014-02-06 16:15:23 +0200843 struct ieee80211_scan_ies *ies)
David Spinadel35a000b2013-08-28 09:29:43 +0300844{
David Spinadel35a000b2013-08-28 09:29:43 +0300845 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
846 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
847 int head = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300848 u32 ssid_bitmap;
849 int cmd_len;
850 int ret;
David Spinadel762533b2014-06-05 11:20:43 +0300851 u8 *probes;
David Spinadelfb98be52014-05-04 12:51:10 +0300852 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
853 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
David Spinadel35a000b2013-08-28 09:29:43 +0300854
855 struct iwl_scan_offload_cfg *scan_cfg;
856 struct iwl_host_cmd cmd = {
857 .id = SCAN_OFFLOAD_CONFIG_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300858 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200859 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300860
861 lockdep_assert_held(&mvm->mutex);
862
David Spinadel35a000b2013-08-28 09:29:43 +0300863 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
David Spinadel762533b2014-06-05 11:20:43 +0300864 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200865 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300866
867 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
868 if (!scan_cfg)
869 return -ENOMEM;
870
David Spinadel762533b2014-06-05 11:20:43 +0300871 probes = scan_cfg->data +
872 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
873
Johannes Bergab480032014-06-04 10:13:50 +0200874 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200875 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300876 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
877
David Spinadelfb98be52014-05-04 12:51:10 +0300878 iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
879 &ssid_bitmap, basic_ssid);
David Spinadel35a000b2013-08-28 09:29:43 +0300880 /* build tx frames for supported bands */
881 if (band_2ghz) {
882 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
883 IEEE80211_BAND_2GHZ,
884 &scan_cfg->scan_cmd.tx_cmd[0],
David Spinadel762533b2014-06-05 11:20:43 +0300885 probes);
886 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300887 IEEE80211_BAND_2GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200888 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300889 }
890 if (band_5ghz) {
891 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
892 IEEE80211_BAND_5GHZ,
893 &scan_cfg->scan_cmd.tx_cmd[1],
David Spinadel762533b2014-06-05 11:20:43 +0300894 probes +
David Spinadel35a000b2013-08-28 09:29:43 +0300895 SCAN_OFFLOAD_PROBE_REQ_SIZE);
David Spinadel762533b2014-06-05 11:20:43 +0300896 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300897 IEEE80211_BAND_5GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200898 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300899 }
900
901 cmd.data[0] = scan_cfg;
902 cmd.len[0] = cmd_len;
903 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
904
905 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
906
907 ret = iwl_mvm_send_cmd(mvm, &cmd);
908 kfree(scan_cfg);
909 return ret;
910}
911
912int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
913 struct cfg80211_sched_scan_request *req)
914{
915 struct iwl_scan_offload_profile *profile;
916 struct iwl_scan_offload_profile_cfg *profile_cfg;
917 struct iwl_scan_offload_blacklist *blacklist;
918 struct iwl_host_cmd cmd = {
919 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300920 .len[1] = sizeof(*profile_cfg),
921 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
922 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
923 };
924 int blacklist_len;
925 int i;
926 int ret;
927
928 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
929 return -EIO;
930
931 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
932 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
933 else
934 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
935
936 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
937 if (!blacklist)
938 return -ENOMEM;
939
940 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
941 if (!profile_cfg) {
942 ret = -ENOMEM;
943 goto free_blacklist;
944 }
945
946 cmd.data[0] = blacklist;
947 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
948 cmd.data[1] = profile_cfg;
949
950 /* No blacklist configuration */
951
952 profile_cfg->num_profiles = req->n_match_sets;
953 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
954 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
955 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200956 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
957 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300958
959 for (i = 0; i < req->n_match_sets; i++) {
960 profile = &profile_cfg->profiles[i];
961 profile->ssid_index = i;
962 /* Support any cipher and auth algorithm */
963 profile->unicast_cipher = 0xff;
964 profile->auth_alg = 0xff;
965 profile->network_type = IWL_NETWORK_TYPE_ANY;
966 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
967 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
968 }
969
970 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
971
972 ret = iwl_mvm_send_cmd(mvm, &cmd);
973 kfree(profile_cfg);
974free_blacklist:
975 kfree(blacklist);
976
977 return ret;
978}
979
David Spinadeld2496222014-05-20 12:46:37 +0300980static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
981 struct cfg80211_sched_scan_request *req)
982{
983 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
984 IWL_DEBUG_SCAN(mvm,
985 "Sending scheduled scan with filtering, n_match_sets %d\n",
986 req->n_match_sets);
987 return false;
988 }
989
990 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
991 return true;
992}
993
David Spinadel35a000b2013-08-28 09:29:43 +0300994int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
995 struct cfg80211_sched_scan_request *req)
996{
997 struct iwl_scan_offload_req scan_req = {
998 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
999
1000 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001001 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001002 .schedule_line[0].full_scan_mul = 1,
1003
1004 .schedule_line[1].iterations = 0xff,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001005 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001006 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
1007 };
1008
David Spinadeld2496222014-05-20 12:46:37 +03001009 if (iwl_mvm_scan_pass_all(mvm, req))
Eliad Pellerde33fb52013-11-10 12:59:46 +02001010 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +03001011
Haim Dreyfusse820c2d2014-04-06 11:19:09 +03001012 if (mvm->last_ebs_successful &&
1013 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1014 scan_req.flags |=
1015 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1016
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001017 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
David Spinadel35a000b2013-08-28 09:29:43 +03001018 sizeof(scan_req), &scan_req);
1019}
1020
Luciano Coelhob141c232014-10-01 13:22:40 +03001021int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1022 struct ieee80211_vif *vif,
1023 struct cfg80211_sched_scan_request *req,
1024 struct ieee80211_scan_ies *ies)
1025{
1026 int ret;
1027
David Spinadelb975e552014-11-17 12:30:05 +02001028 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1029 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1030 if (ret)
1031 return ret;
1032 ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
1033 } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadeld2496222014-05-20 12:46:37 +03001034 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001035 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1036 if (ret)
1037 return ret;
1038 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1039 } else {
David Spinadeld2496222014-05-20 12:46:37 +03001040 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001041 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1042 if (ret)
1043 return ret;
1044 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1045 if (ret)
1046 return ret;
1047 ret = iwl_mvm_sched_scan_start(mvm, req);
1048 }
1049
1050 return ret;
1051}
1052
David Spinadelfb98be52014-05-04 12:51:10 +03001053static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +03001054{
1055 int ret;
1056 struct iwl_host_cmd cmd = {
1057 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +03001058 };
1059 u32 status;
1060
1061 /* Exit instantly with error when device is not ready
1062 * to receive scan abort command or it does not perform
1063 * scheduled scan currently */
David Spinadelfb98be52014-05-04 12:51:10 +03001064 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1065 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1066 mvm->scan_status != IWL_MVM_SCAN_OS))
David Spinadel35a000b2013-08-28 09:29:43 +03001067 return -EIO;
1068
1069 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1070 if (ret)
1071 return ret;
1072
1073 if (status != CAN_ABORT_STATUS) {
1074 /*
1075 * The scan abort will return 1 for success or
1076 * 2 for "failure". A failure condition can be
1077 * due to simply not being in an active scan which
1078 * can occur if we send the scan abort before the
1079 * microcode has notified us that a scan is completed.
1080 */
1081 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001082 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +03001083 }
1084
1085 return ret;
1086}
1087
David Spinadelfb98be52014-05-04 12:51:10 +03001088int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +03001089{
1090 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001091 struct iwl_notification_wait wait_scan_done;
1092 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadelfb98be52014-05-04 12:51:10 +03001093 bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
David Spinadel35a000b2013-08-28 09:29:43 +03001094
1095 lockdep_assert_held(&mvm->mutex);
1096
David Spinadeld2496222014-05-20 12:46:37 +03001097 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1098 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
1099 notify);
1100
David Spinadelfb98be52014-05-04 12:51:10 +03001101 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1102 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1103 mvm->scan_status != IWL_MVM_SCAN_OS)) {
1104 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001105 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001106 }
1107
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001108 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1109 scan_done_notif,
1110 ARRAY_SIZE(scan_done_notif),
1111 NULL, NULL);
1112
David Spinadelfb98be52014-05-04 12:51:10 +03001113 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001114 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +03001115 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1116 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001117 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1118 return ret;
1119 }
1120
David Spinadelfb98be52014-05-04 12:51:10 +03001121 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1122 sched ? "offloaded " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001123
1124 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1125 if (ret)
1126 return ret;
1127
1128 /*
1129 * Clear the scan status so the next scan requests will succeed. This
1130 * also ensures the Rx handler doesn't do anything, as the scan was
Eliad Peller4ff78182014-06-22 14:44:44 +03001131 * stopped from above. Since the rx handler won't do anything now,
1132 * we have to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001133 */
Eliad Peller4ff78182014-06-22 14:44:44 +03001134 if (mvm->scan_status == IWL_MVM_SCAN_OS)
1135 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1136
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001137 mvm->scan_status = IWL_MVM_SCAN_NONE;
1138
David Spinadelfb98be52014-05-04 12:51:10 +03001139 if (notify) {
1140 if (sched)
1141 ieee80211_sched_scan_stopped(mvm->hw);
1142 else
1143 ieee80211_scan_completed(mvm->hw, true);
1144 }
David Spinadel636a2cd2014-05-01 15:57:22 +03001145
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001146 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001147}
David Spinadelfb98be52014-05-04 12:51:10 +03001148
1149static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1150 struct iwl_scan_req_tx_cmd *tx_cmd,
1151 bool no_cck)
1152{
1153 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1154 TX_CMD_FLG_BT_DIS);
1155 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1156 IEEE80211_BAND_2GHZ,
1157 no_cck);
1158 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1159
1160 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1161 TX_CMD_FLG_BT_DIS);
1162 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1163 IEEE80211_BAND_5GHZ,
1164 no_cck);
1165 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1166}
1167
1168static void
1169iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1170 struct ieee80211_channel **channels,
1171 int n_channels, u32 ssid_bitmap,
1172 struct iwl_scan_req_unified_lmac *cmd)
1173{
1174 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1175 int i;
1176
1177 for (i = 0; i < n_channels; i++) {
1178 channel_cfg[i].channel_num =
1179 cpu_to_le16(channels[i]->hw_value);
1180 channel_cfg[i].iter_count = cpu_to_le16(1);
1181 channel_cfg[i].iter_interval = 0;
1182 channel_cfg[i].flags =
1183 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1184 ssid_bitmap);
1185 }
1186}
1187
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001188static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
1189 size_t len, u8 *const pos)
1190{
1191 static const u8 before_ds_params[] = {
1192 WLAN_EID_SSID,
1193 WLAN_EID_SUPP_RATES,
1194 WLAN_EID_REQUEST,
1195 WLAN_EID_EXT_SUPP_RATES,
1196 };
1197 size_t offs;
1198 u8 *newpos = pos;
1199
1200 if (!iwl_mvm_rrm_scan_needed(mvm)) {
1201 memcpy(newpos, ies, len);
1202 return newpos + len;
1203 }
1204
1205 offs = ieee80211_ie_split(ies, len,
1206 before_ds_params,
1207 ARRAY_SIZE(before_ds_params),
1208 0);
1209
1210 memcpy(newpos, ies, offs);
1211 newpos += offs;
1212
1213 /* Add a placeholder for DS Parameter Set element */
1214 *newpos++ = WLAN_EID_DS_PARAMS;
1215 *newpos++ = 1;
1216 *newpos++ = 0;
1217
1218 memcpy(newpos, ies + offs, len - offs);
1219 newpos += len - offs;
1220
1221 return newpos;
1222}
1223
David Spinadelfb98be52014-05-04 12:51:10 +03001224static void
1225iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1226 struct ieee80211_scan_ies *ies,
Johannes Bergeffd05a2014-11-18 17:21:19 +01001227 struct iwl_scan_probe_req *preq,
1228 const u8 *mac_addr, const u8 *mac_addr_mask)
David Spinadelfb98be52014-05-04 12:51:10 +03001229{
David Spinadelfb98be52014-05-04 12:51:10 +03001230 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001231 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001232
Johannes Bergeffd05a2014-11-18 17:21:19 +01001233 /*
1234 * Unfortunately, right now the offload scan doesn't support randomising
1235 * within the firmware, so until the firmware API is ready we implement
1236 * it in the driver. This means that the scan iterations won't really be
1237 * random, only when it's restarted, but at least that helps a bit.
1238 */
1239 if (mac_addr)
1240 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
1241 else
1242 memcpy(frame->sa, vif->addr, ETH_ALEN);
1243
David Spinadelfb98be52014-05-04 12:51:10 +03001244 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1245 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +03001246 eth_broadcast_addr(frame->bssid);
1247 frame->seq_ctrl = 0;
1248
1249 pos = frame->u.probe_req.variable;
1250 *pos++ = WLAN_EID_SSID;
1251 *pos++ = 0;
1252
1253 preq->mac_header.offset = 0;
1254 preq->mac_header.len = cpu_to_le16(24 + 2);
1255
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001256 /* Insert ds parameter set element on 2.4 GHz band */
1257 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
1258 ies->ies[IEEE80211_BAND_2GHZ],
1259 ies->len[IEEE80211_BAND_2GHZ],
1260 pos);
David Spinadelfb98be52014-05-04 12:51:10 +03001261 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001262 preq->band_data[0].len = cpu_to_le16(newpos - pos);
1263 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001264
1265 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1266 ies->len[IEEE80211_BAND_5GHZ]);
1267 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1268 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1269 pos += ies->len[IEEE80211_BAND_5GHZ];
1270
1271 memcpy(pos, ies->common_ies, ies->common_ie_len);
1272 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1273 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1274}
1275
1276static void
1277iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1278 struct iwl_scan_req_unified_lmac *cmd,
1279 struct iwl_mvm_scan_params *params)
1280{
David Spinadelaf913442014-06-12 19:29:40 +03001281 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +03001282 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1283 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1284 if (params->passive_fragmented)
1285 cmd->fragmented_dwell =
1286 params->dwell[IEEE80211_BAND_2GHZ].passive;
David Spinadelfb98be52014-05-04 12:51:10 +03001287 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1288 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1289 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1290 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +03001291 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +03001292
1293 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1294 mvm->last_ebs_successful) {
1295 cmd->channel_opt[0].flags =
1296 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1297 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1298 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1299 cmd->channel_opt[1].flags =
1300 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1301 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1302 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1303 }
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001304
1305 if (iwl_mvm_rrm_scan_needed(mvm))
1306 cmd->scan_flags |=
1307 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +03001308}
1309
1310int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1311 struct ieee80211_vif *vif,
1312 struct ieee80211_scan_request *req)
1313{
1314 struct iwl_host_cmd hcmd = {
1315 .id = SCAN_OFFLOAD_REQUEST_CMD,
1316 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1317 sizeof(struct iwl_scan_channel_cfg_lmac) *
1318 mvm->fw->ucode_capa.n_scan_channels +
1319 sizeof(struct iwl_scan_probe_req), },
1320 .data = { mvm->scan_cmd, },
1321 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1322 };
1323 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001324 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001325 struct iwl_mvm_scan_params params = {};
1326 u32 flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001327 u32 ssid_bitmap = 0;
David Spinadelfb98be52014-05-04 12:51:10 +03001328 int ret, i;
1329
1330 lockdep_assert_held(&mvm->mutex);
1331
1332 /* we should have failed registration if scan_cmd was NULL */
1333 if (WARN_ON(mvm->scan_cmd == NULL))
1334 return -ENOMEM;
1335
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001336 if (req->req.n_ssids > PROBE_OPTION_MAX ||
1337 req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1338 req->ies.len[NL80211_BAND_5GHZ] >
1339 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1340 req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1341 return -ENOBUFS;
David Spinadelfb98be52014-05-04 12:51:10 +03001342
1343 mvm->scan_status = IWL_MVM_SCAN_OS;
1344
1345 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1346 &params);
1347
1348 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1349
1350 cmd->n_channels = (u8)req->req.n_channels;
1351
1352 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1353
1354 if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1355 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1356
1357 if (params.passive_fragmented)
1358 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1359
1360 if (req->req.n_ssids == 0)
1361 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1362
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001363 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001364
1365 cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1366 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1367 MAC_FILTER_IN_BEACON);
1368 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1369 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1370 req->req.n_ssids, 0);
1371
1372 cmd->schedule[0].delay = 0;
1373 cmd->schedule[0].iterations = 1;
1374 cmd->schedule[0].full_scan_mul = 0;
1375 cmd->schedule[1].delay = 0;
1376 cmd->schedule[1].iterations = 0;
1377 cmd->schedule[1].full_scan_mul = 0;
1378
1379 for (i = 1; i <= req->req.n_ssids; i++)
1380 ssid_bitmap |= BIT(i);
1381
1382 iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1383 req->req.n_channels, ssid_bitmap,
1384 cmd);
1385
David Spinadeld2496222014-05-20 12:46:37 +03001386 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1387 mvm->fw->ucode_capa.n_scan_channels);
1388
Johannes Bergeffd05a2014-11-18 17:21:19 +01001389 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
1390 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1391 req->req.mac_addr : NULL,
1392 req->req.mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001393
1394 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1395 if (!ret) {
1396 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1397 } else {
1398 /*
1399 * If the scan failed, it usually means that the FW was unable
1400 * to allocate the time events. Warn on it, but maybe we
1401 * should try to send the command again with different params.
1402 */
1403 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1404 mvm->scan_status = IWL_MVM_SCAN_NONE;
1405 ret = -EIO;
1406 }
1407 return ret;
1408}
1409
1410int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1411 struct ieee80211_vif *vif,
1412 struct cfg80211_sched_scan_request *req,
1413 struct ieee80211_scan_ies *ies)
1414{
1415 struct iwl_host_cmd hcmd = {
1416 .id = SCAN_OFFLOAD_REQUEST_CMD,
1417 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1418 sizeof(struct iwl_scan_channel_cfg_lmac) *
1419 mvm->fw->ucode_capa.n_scan_channels +
1420 sizeof(struct iwl_scan_probe_req), },
1421 .data = { mvm->scan_cmd, },
1422 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1423 };
1424 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001425 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001426 struct iwl_mvm_scan_params params = {};
1427 int ret;
1428 u32 flags = 0, ssid_bitmap = 0;
1429
1430 lockdep_assert_held(&mvm->mutex);
1431
1432 /* we should have failed registration if scan_cmd was NULL */
1433 if (WARN_ON(mvm->scan_cmd == NULL))
1434 return -ENOMEM;
1435
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001436 if (req->n_ssids > PROBE_OPTION_MAX ||
1437 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1438 ies->len[NL80211_BAND_5GHZ] >
1439 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1440 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
David Spinadelfb98be52014-05-04 12:51:10 +03001441 return -ENOBUFS;
1442
1443 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1444
1445 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1446
1447 cmd->n_channels = (u8)req->n_channels;
1448
David Spinadeld2496222014-05-20 12:46:37 +03001449 if (iwl_mvm_scan_pass_all(mvm, req))
David Spinadelfb98be52014-05-04 12:51:10 +03001450 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
David Spinadel7e2a3882014-12-23 14:38:09 +02001451 else
1452 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
David Spinadelfb98be52014-05-04 12:51:10 +03001453
1454 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1455 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1456
1457 if (params.passive_fragmented)
1458 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1459
1460 if (req->n_ssids == 0)
1461 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1462
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001463 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001464
1465 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1466 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1467 MAC_FILTER_IN_BEACON);
1468 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1469 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1470
David Spinadelb14fc2b2014-06-25 13:17:53 +03001471 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001472 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1473 cmd->schedule[0].full_scan_mul = 1;
1474
David Spinadelb14fc2b2014-06-25 13:17:53 +03001475 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001476 cmd->schedule[1].iterations = 0xff;
1477 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1478
1479 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1480 ssid_bitmap, cmd);
1481
David Spinadeld2496222014-05-20 12:46:37 +03001482 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1483 mvm->fw->ucode_capa.n_scan_channels);
1484
Johannes Bergeffd05a2014-11-18 17:21:19 +01001485 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
1486 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1487 req->mac_addr : NULL,
1488 req->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001489
1490 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1491 if (!ret) {
1492 IWL_DEBUG_SCAN(mvm,
1493 "Sched scan request was sent successfully\n");
1494 } else {
1495 /*
1496 * If the scan failed, it usually means that the FW was unable
1497 * to allocate the time events. Warn on it, but maybe we
1498 * should try to send the command again with different params.
1499 */
1500 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1501 mvm->scan_status = IWL_MVM_SCAN_NONE;
1502 ret = -EIO;
1503 }
1504 return ret;
1505}
1506
1507
1508int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1509{
David Spinadeld2496222014-05-20 12:46:37 +03001510 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1511 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1512 true);
1513
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001514 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1515 return 0;
1516
1517 if (iwl_mvm_is_radio_killed(mvm)) {
1518 ieee80211_scan_completed(mvm->hw, true);
1519 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1520 mvm->scan_status = IWL_MVM_SCAN_NONE;
1521 return 0;
1522 }
1523
David Spinadelfb98be52014-05-04 12:51:10 +03001524 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1525 return iwl_mvm_scan_offload_stop(mvm, true);
1526 return iwl_mvm_cancel_regular_scan(mvm);
1527}
David Spinadeld2496222014-05-20 12:46:37 +03001528
1529/* UMAC scan API */
1530
1531struct iwl_umac_scan_done {
1532 struct iwl_mvm *mvm;
1533 enum iwl_umac_scan_uid_type type;
1534};
1535
1536static int rate_to_scan_rate_flag(unsigned int rate)
1537{
1538 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1539 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1540 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1541 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1542 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1543 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1544 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1545 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1546 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1547 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1548 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1549 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1550 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1551 };
1552
1553 return rate_to_scan_rate[rate];
1554}
1555
1556static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1557{
1558 struct ieee80211_supported_band *band;
1559 unsigned int rates = 0;
1560 int i;
1561
1562 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1563 for (i = 0; i < band->n_bitrates; i++)
1564 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1565 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1566 for (i = 0; i < band->n_bitrates; i++)
1567 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1568
1569 /* Set both basic rates and supported rates */
1570 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1571
1572 return cpu_to_le32(rates);
1573}
1574
1575int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1576{
1577
1578 struct iwl_scan_config *scan_config;
1579 struct ieee80211_supported_band *band;
1580 int num_channels =
1581 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1582 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1583 int ret, i, j = 0, cmd_size, data_size;
1584 struct iwl_host_cmd cmd = {
1585 .id = SCAN_CFG_CMD,
1586 };
1587
1588 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1589 return -ENOBUFS;
1590
1591 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1592
1593 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1594 if (!scan_config)
1595 return -ENOMEM;
1596
1597 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1598 scan_config->hdr.size = cpu_to_le16(data_size);
1599 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1600 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1601 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1602 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1603 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1604 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1605 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1606 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1607 SCAN_CONFIG_N_CHANNELS(num_channels));
1608 scan_config->tx_chains = cpu_to_le32(mvm->fw->valid_tx_ant);
1609 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1610 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1611 scan_config->out_of_channel_time = cpu_to_le32(170);
1612 scan_config->suspend_time = cpu_to_le32(30);
1613 scan_config->dwell_active = 20;
1614 scan_config->dwell_passive = 110;
1615 scan_config->dwell_fragmented = 20;
1616
1617 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1618
1619 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1620 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1621 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1622 IWL_CHANNEL_FLAG_EBS_ADD |
1623 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1624
1625 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1626 for (i = 0; i < band->n_channels; i++, j++)
1627 scan_config->channel_array[j] = band->channels[i].center_freq;
1628 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1629 for (i = 0; i < band->n_channels; i++, j++)
1630 scan_config->channel_array[j] = band->channels[i].center_freq;
1631
1632 cmd.data[0] = scan_config;
1633 cmd.len[0] = cmd_size;
1634 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1635
1636 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1637
1638 ret = iwl_mvm_send_cmd(mvm, &cmd);
1639
1640 kfree(scan_config);
1641 return ret;
1642}
1643
1644static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1645{
1646 int i;
1647
1648 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1649 if (mvm->scan_uid[i] == uid)
1650 return i;
1651
1652 return i;
1653}
1654
1655static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1656{
1657 return iwl_mvm_find_scan_uid(mvm, 0);
1658}
1659
1660static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1661 enum iwl_umac_scan_uid_type type)
1662{
1663 int i;
1664
1665 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1666 if (mvm->scan_uid[i] & type)
1667 return true;
1668
1669 return false;
1670}
1671
1672static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1673 enum iwl_umac_scan_uid_type type)
1674{
1675 u32 uid;
1676
1677 /* make sure exactly one bit is on in scan type */
1678 WARN_ON(hweight8(type) != 1);
1679
1680 /*
1681 * Make sure scan uids are unique. If one scan lasts long time while
1682 * others are completing frequently, the seq number will wrap up and
1683 * we may have more than one scan with the same uid.
1684 */
1685 do {
1686 uid = type | (mvm->scan_seq_num <<
1687 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1688 mvm->scan_seq_num++;
1689 } while (iwl_mvm_find_scan_uid(mvm, uid) <
1690 IWL_MVM_MAX_SIMULTANEOUS_SCANS);
1691
1692 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1693
1694 return uid;
1695}
1696
1697static void
1698iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1699 struct iwl_scan_req_umac *cmd,
1700 struct iwl_mvm_scan_params *params)
1701{
1702 memset(cmd, 0, ksize(cmd));
1703 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1704 sizeof(struct iwl_mvm_umac_cmd_hdr));
1705 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1706 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1707 if (params->passive_fragmented)
1708 cmd->fragmented_dwell =
1709 params->dwell[IEEE80211_BAND_2GHZ].passive;
1710 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1711 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1712 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1713}
1714
1715static void
1716iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1717 struct ieee80211_channel **channels,
1718 int n_channels, u32 ssid_bitmap,
1719 struct iwl_scan_req_umac *cmd)
1720{
1721 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1722 int i;
1723
1724 for (i = 0; i < n_channels; i++) {
1725 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1726 channel_cfg[i].channel_num = channels[i]->hw_value;
1727 channel_cfg[i].iter_count = 1;
1728 channel_cfg[i].iter_interval = 0;
1729 }
1730}
1731
1732static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1733 struct cfg80211_ssid *ssids,
1734 int fragmented)
1735{
1736 int flags = 0;
1737
1738 if (n_ssids == 0)
1739 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1740
1741 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1742 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1743
1744 if (fragmented)
1745 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1746
1747 if (iwl_mvm_rrm_scan_needed(mvm))
1748 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1749
1750 return flags;
1751}
1752
1753int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1754 struct ieee80211_scan_request *req)
1755{
1756 struct iwl_host_cmd hcmd = {
1757 .id = SCAN_REQ_UMAC,
1758 .len = { iwl_mvm_scan_size(mvm), },
1759 .data = { mvm->scan_cmd, },
1760 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1761 };
1762 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1763 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1764 sizeof(struct iwl_scan_channel_cfg_umac) *
1765 mvm->fw->ucode_capa.n_scan_channels;
1766 struct iwl_mvm_scan_params params = {};
1767 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001768 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001769 int ret, i, uid_idx;
1770
1771 lockdep_assert_held(&mvm->mutex);
1772
1773 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1774 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1775 return -EBUSY;
1776
1777 /* we should have failed registration if scan_cmd was NULL */
1778 if (WARN_ON(mvm->scan_cmd == NULL))
1779 return -ENOMEM;
1780
1781 if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
1782 req->ies.common_ie_len +
1783 req->ies.len[NL80211_BAND_2GHZ] +
1784 req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
1785 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
1786 mvm->fw->ucode_capa.n_scan_channels))
1787 return -ENOBUFS;
1788
1789 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1790 &params);
1791
1792 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1793
1794 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1795 mvm->scan_uid[uid_idx] = uid;
1796 cmd->uid = cpu_to_le32(uid);
1797
1798 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1799
1800 flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
1801 req->req.ssids,
1802 params.passive_fragmented);
1803
1804 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1805
1806 cmd->general_flags = cpu_to_le32(flags);
1807 cmd->n_channels = req->req.n_channels;
1808
1809 for (i = 0; i < req->req.n_ssids; i++)
1810 ssid_bitmap |= BIT(i);
1811
1812 iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
1813 req->req.n_channels, ssid_bitmap, cmd);
1814
1815 sec_part->schedule[0].iter_count = 1;
1816 sec_part->delay = 0;
1817
Johannes Bergeffd05a2014-11-18 17:21:19 +01001818 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
1819 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1820 req->req.mac_addr : NULL,
1821 req->req.mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001822
1823 iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
1824 req->req.n_ssids, 0);
1825
1826 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1827 if (!ret) {
1828 IWL_DEBUG_SCAN(mvm,
1829 "Scan request was sent successfully\n");
1830 } else {
1831 /*
1832 * If the scan failed, it usually means that the FW was unable
1833 * to allocate the time events. Warn on it, but maybe we
1834 * should try to send the command again with different params.
1835 */
1836 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1837 }
1838 return ret;
1839}
1840
1841int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1842 struct cfg80211_sched_scan_request *req,
1843 struct ieee80211_scan_ies *ies)
1844{
1845
1846 struct iwl_host_cmd hcmd = {
1847 .id = SCAN_REQ_UMAC,
1848 .len = { iwl_mvm_scan_size(mvm), },
1849 .data = { mvm->scan_cmd, },
1850 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1851 };
1852 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1853 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1854 sizeof(struct iwl_scan_channel_cfg_umac) *
1855 mvm->fw->ucode_capa.n_scan_channels;
1856 struct iwl_mvm_scan_params params = {};
1857 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001858 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001859 int ret, uid_idx;
1860
1861 lockdep_assert_held(&mvm->mutex);
1862
1863 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1864 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1865 return -EBUSY;
1866
1867 /* we should have failed registration if scan_cmd was NULL */
1868 if (WARN_ON(mvm->scan_cmd == NULL))
1869 return -ENOMEM;
1870
1871 if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1872 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1873 ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1874 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
1875 mvm->fw->ucode_capa.n_scan_channels))
1876 return -ENOBUFS;
1877
1878 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
1879 &params);
1880
1881 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1882
1883 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1884
1885 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1886 mvm->scan_uid[uid_idx] = uid;
1887 cmd->uid = cpu_to_le32(uid);
1888
1889 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1890
1891 flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
1892 params.passive_fragmented);
1893
1894 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1895
1896 if (iwl_mvm_scan_pass_all(mvm, req))
1897 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1898 else
1899 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1900
1901 cmd->general_flags = cpu_to_le32(flags);
1902
1903 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1904 mvm->last_ebs_successful)
1905 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1906 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1907 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1908
1909 cmd->n_channels = req->n_channels;
1910
1911 iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
1912 false);
1913
1914 /* This API uses bits 0-19 instead of 1-20. */
1915 ssid_bitmap = ssid_bitmap >> 1;
1916
1917 iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1918 ssid_bitmap, cmd);
1919
1920 sec_part->schedule[0].interval =
1921 cpu_to_le16(req->interval / MSEC_PER_SEC);
1922 sec_part->schedule[0].iter_count = 0xff;
1923
1924 sec_part->delay = 0;
1925
Johannes Bergeffd05a2014-11-18 17:21:19 +01001926 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
1927 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1928 req->mac_addr : NULL,
1929 req->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001930
1931 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1932 if (!ret) {
1933 IWL_DEBUG_SCAN(mvm,
1934 "Sched scan request was sent successfully\n");
1935 } else {
1936 /*
1937 * If the scan failed, it usually means that the FW was unable
1938 * to allocate the time events. Warn on it, but maybe we
1939 * should try to send the command again with different params.
1940 */
1941 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1942 }
1943 return ret;
1944}
1945
1946int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1947 struct iwl_rx_cmd_buffer *rxb,
1948 struct iwl_device_cmd *cmd)
1949{
1950 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1951 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1952 u32 uid = __le32_to_cpu(notif->uid);
1953 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1954 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1955
David Spinadel2992a322014-11-10 11:16:53 +02001956 /*
1957 * Scan uid may be set to zero in case of scan abort request from above.
1958 */
1959 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
David Spinadeld2496222014-05-20 12:46:37 +03001960 return 0;
1961
1962 IWL_DEBUG_SCAN(mvm,
1963 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1964 uid, sched ? "sched" : "regular",
1965 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1966 "completed" : "aborted",
1967 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1968 "success" : "failed");
1969
1970 mvm->last_ebs_successful = !notif->ebs_status;
1971 mvm->scan_uid[uid_idx] = 0;
1972
1973 if (!sched) {
1974 ieee80211_scan_completed(mvm->hw,
1975 notif->status ==
1976 IWL_SCAN_OFFLOAD_ABORTED);
1977 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1978 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
1979 ieee80211_sched_scan_stopped(mvm->hw);
1980 } else {
1981 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1982 }
1983
1984 return 0;
1985}
1986
1987static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1988 struct iwl_rx_packet *pkt, void *data)
1989{
1990 struct iwl_umac_scan_done *scan_done = data;
1991 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1992 u32 uid = __le32_to_cpu(notif->uid);
1993 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1994
1995 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1996 return false;
1997
1998 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1999 return false;
2000
2001 /*
2002 * Clear scan uid of scans that was aborted from above and completed
2003 * in FW so the RX handler does nothing.
2004 */
2005 scan_done->mvm->scan_uid[uid_idx] = 0;
2006
2007 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
2008}
2009
2010static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
2011{
2012 struct iwl_umac_scan_abort cmd = {
2013 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
2014 sizeof(struct iwl_mvm_umac_cmd_hdr)),
2015 .uid = cpu_to_le32(uid),
2016 };
2017
2018 lockdep_assert_held(&mvm->mutex);
2019
2020 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2021
2022 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
2023}
2024
2025static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
2026 enum iwl_umac_scan_uid_type type, bool notify)
2027{
2028 struct iwl_notification_wait wait_scan_done;
2029 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
2030 struct iwl_umac_scan_done scan_done = {
2031 .mvm = mvm,
2032 .type = type,
2033 };
2034 int i, ret = -EIO;
2035
2036 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2037 scan_done_notif,
2038 ARRAY_SIZE(scan_done_notif),
2039 iwl_scan_umac_done_check, &scan_done);
2040
2041 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2042
2043 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
2044 if (mvm->scan_uid[i] & type) {
2045 int err;
2046
2047 if (iwl_mvm_is_radio_killed(mvm) &&
2048 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
2049 ieee80211_scan_completed(mvm->hw, true);
2050 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2051 break;
2052 }
2053
2054 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
2055 if (!err)
2056 ret = 0;
2057 }
2058 }
2059
2060 if (ret) {
2061 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
2062 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2063 return ret;
2064 }
2065
2066 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
2067 if (ret)
2068 return ret;
2069
2070 if (notify) {
2071 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
2072 ieee80211_sched_scan_stopped(mvm->hw);
2073 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
2074 ieee80211_scan_completed(mvm->hw, true);
2075 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2076 }
2077 }
2078
2079 return ret;
2080}
2081
2082int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2083{
2084 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
2085 return sizeof(struct iwl_scan_req_umac) +
2086 sizeof(struct iwl_scan_channel_cfg_umac) *
2087 mvm->fw->ucode_capa.n_scan_channels +
2088 sizeof(struct iwl_scan_req_umac_tail);
2089
2090 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
2091 return sizeof(struct iwl_scan_req_unified_lmac) +
2092 sizeof(struct iwl_scan_channel_cfg_lmac) *
2093 mvm->fw->ucode_capa.n_scan_channels +
2094 sizeof(struct iwl_scan_probe_req);
2095
2096 return sizeof(struct iwl_scan_cmd) +
2097 mvm->fw->ucode_capa.max_probe_length +
2098 mvm->fw->ucode_capa.n_scan_channels *
2099 sizeof(struct iwl_scan_channel);
2100}