blob: 844bf7c4c8def639dbc65329fe9f4e157715cbdf [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
David Spinadel1f9c4182014-12-01 23:30:07 +020075#define IWL_DENSE_EBS_SCAN_RATIO 5
76#define IWL_SPARSE_EBS_SCAN_RATIO 1
Alexander Bondar8a110d92014-03-12 17:31:19 +020077
78struct iwl_mvm_scan_params {
79 u32 max_out_time;
80 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020081 bool passive_fragmented;
82 struct _dwell {
83 u16 passive;
84 u16 active;
85 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +020086};
Johannes Berg8ca151b2013-01-24 14:25:36 +010087
David Spinadeld2496222014-05-20 12:46:37 +030088enum iwl_umac_scan_uid_type {
89 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
90 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
91 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
92 IWL_UMAC_SCAN_UID_SCHED_SCAN,
93};
94
95static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
96 enum iwl_umac_scan_uid_type type, bool notify);
97
98static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
99{
100 if (mvm->scan_rx_ant != ANT_NONE)
101 return mvm->scan_rx_ant;
102 return mvm->fw->valid_rx_ant;
103}
104
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
106{
107 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300108 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109
David Spinadeld2496222014-05-20 12:46:37 +0300110 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100111 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
112 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
113 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
114 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
115 return cpu_to_le16(rx_chain);
116}
117
David Spinadelfb98be52014-05-04 12:51:10 +0300118static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119{
David Spinadelfb98be52014-05-04 12:51:10 +0300120 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100121 return cpu_to_le32(PHY_BAND_24);
122 else
123 return cpu_to_le32(PHY_BAND_5);
124}
125
126static inline __le32
127iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
128 bool no_cck)
129{
130 u32 tx_ant;
131
132 mvm->scan_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100133 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100134 mvm->scan_last_antenna_idx);
135 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
136
137 if (band == IEEE80211_BAND_2GHZ && !no_cck)
138 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
139 tx_ant);
140 else
141 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
142}
143
144/*
145 * We insert the SSIDs in an inverted order, because the FW will
146 * invert it back. The most prioritized SSID, which is first in the
147 * request list, is not copied here, but inserted directly to the probe
148 * request.
149 */
David Spinadelfb98be52014-05-04 12:51:10 +0300150static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
151 struct cfg80211_ssid *ssids,
152 int n_ssids, int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100153{
154 int fw_idx, req_idx;
155
David Spinadelfb98be52014-05-04 12:51:10 +0300156 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300157 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300158 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
159 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
160 memcpy(cmd_ssid[fw_idx].ssid,
161 ssids[req_idx].ssid,
162 ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100163 }
164}
165
166/*
167 * If req->n_ssids > 0, it means we should do an active scan.
168 * In case of active scan w/o directed scan, we receive a zero-length SSID
169 * just to notify that this scan is active and not passive.
170 * In order to notify the FW of the number of SSIDs we wish to scan (including
171 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300172 * one for each SSID, and set the active bit (first). If the first SSID is
173 * already included in the probe template, so we need to set only
174 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100175 */
David Spinadel720daf22014-12-29 15:43:48 +0200176static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
177 enum ieee80211_band band, int n_ssids)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100178{
David Spinadel720daf22014-12-29 15:43:48 +0200179 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
180 return 10;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100181 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300182 return 20 + 3 * (n_ssids + 1);
183 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100184}
185
David Spinadel720daf22014-12-29 15:43:48 +0200186static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
187 enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100188{
David Spinadel720daf22014-12-29 15:43:48 +0200189 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
190 return 110;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100191 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
192}
193
194static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300195 struct cfg80211_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200196 bool basic_ssid,
197 struct iwl_mvm_scan_params *params)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100198{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100199 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
200 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
201 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300202 int type = BIT(req->n_ssids) - 1;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200203 enum ieee80211_band band = req->channels[0]->band;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300204
205 if (!basic_ssid)
206 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100207
208 for (i = 0; i < cmd->channel_count; i++) {
209 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300210 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200211 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300212 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200213 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
214 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100215 chan->iteration_count = cpu_to_le16(1);
216 chan++;
217 }
218}
219
220/*
221 * Fill in probe request with the following parameters:
222 * TA is our vif HW address, which mac80211 ensures we have.
223 * Packet is broadcasted, so this is both SA and DA.
224 * The probe request IE is made out of two: first comes the most prioritized
225 * SSID if a directed scan is requested. Second comes whatever extra
226 * information was given to us as the scan request IE.
227 */
228static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
229 int n_ssids, const u8 *ssid, int ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200230 const u8 *band_ie, int band_ie_len,
231 const u8 *common_ie, int common_ie_len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100232 int left)
233{
234 int len = 0;
235 u8 *pos = NULL;
236
237 /* Make sure there is enough space for the probe request,
238 * two mandatory IEs and the data */
239 left -= 24;
240 if (left < 0)
241 return 0;
242
243 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
244 eth_broadcast_addr(frame->da);
245 memcpy(frame->sa, ta, ETH_ALEN);
246 eth_broadcast_addr(frame->bssid);
247 frame->seq_ctrl = 0;
248
249 len += 24;
250
251 /* for passive scans, no need to fill anything */
252 if (n_ssids == 0)
253 return (u16)len;
254
255 /* points to the payload of the request */
256 pos = &frame->u.probe_req.variable[0];
257
258 /* fill in our SSID IE */
259 left -= ssid_len + 2;
260 if (left < 0)
261 return 0;
262 *pos++ = WLAN_EID_SSID;
263 *pos++ = ssid_len;
264 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
265 memcpy(pos, ssid, ssid_len);
266 pos += ssid_len;
267 }
268
269 len += ssid_len + 2;
270
David Spinadel633e2712014-02-06 16:15:23 +0200271 if (WARN_ON(left < band_ie_len + common_ie_len))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100272 return len;
273
David Spinadel633e2712014-02-06 16:15:23 +0200274 if (band_ie && band_ie_len) {
275 memcpy(pos, band_ie, band_ie_len);
276 pos += band_ie_len;
277 len += band_ie_len;
278 }
279
280 if (common_ie && common_ie_len) {
281 memcpy(pos, common_ie, common_ie_len);
282 pos += common_ie_len;
283 len += common_ie_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100284 }
285
286 return (u16)len;
287}
288
Alexander Bondar8a110d92014-03-12 17:31:19 +0200289static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
290 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200291{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200292 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
293 bool *global_bound = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200294
David Spinadel1e2ebe02014-10-26 15:53:27 +0200295 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
296 mvmvif->phy_ctxt->id < MAX_PHYS)
Alexander Bondar8a110d92014-03-12 17:31:19 +0200297 *global_bound = true;
298}
299
300static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200301 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200302 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200303 struct iwl_mvm_scan_params *params)
304{
305 bool global_bound = false;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200306 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300307 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200308
309 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
310 IEEE80211_IFACE_ITER_NORMAL,
311 iwl_mvm_scan_condition_iterator,
312 &global_bound);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200313
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300314 if (!global_bound)
315 goto not_bound;
316
David Spinadel2ce89cd2014-07-22 13:11:18 +0300317 params->suspend_time = 30;
318 params->max_out_time = 170;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300319
320 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300321 if (mvm->fw->ucode_capa.api[0] &
322 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
323 params->suspend_time = 105;
324 params->max_out_time = 70;
325 frag_passive_dwell = 20;
326 } else {
327 params->suspend_time = 120;
328 params->max_out_time = 120;
329 }
330 }
331
332 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
333 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
334 /*
335 * P2P device scan should not be fragmented to avoid negative
336 * impact on P2P device discovery. Configure max_out_time to be
337 * equal to dwell time on passive channel. Take a longest
338 * possible value, one that corresponds to 2GHz band
339 */
340 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
341 u32 passive_dwell =
David Spinadel720daf22014-12-29 15:43:48 +0200342 iwl_mvm_get_passive_dwell(mvm,
343 IEEE80211_BAND_2GHZ);
David Spinadel2ce89cd2014-07-22 13:11:18 +0300344 params->max_out_time = passive_dwell;
345 } else {
346 params->passive_fragmented = true;
347 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200348 }
349
Johannes Bergab480032014-06-04 10:13:50 +0200350 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
351 params->max_out_time = 200;
352
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300353not_bound:
354
Alexander Bondar50df8a32014-03-12 20:30:51 +0200355 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300356 if (params->passive_fragmented)
357 params->dwell[band].passive = frag_passive_dwell;
358 else
359 params->dwell[band].passive =
David Spinadel720daf22014-12-29 15:43:48 +0200360 iwl_mvm_get_passive_dwell(mvm, band);
361 params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200362 n_ssids);
363 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200364}
365
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300366static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
367{
368 /* require rrm scan whenever the fw supports it */
369 return mvm->fw->ucode_capa.capa[0] &
370 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
371}
372
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300373static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
374 bool is_sched_scan)
375{
376 int max_probe_len;
377
378 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
379 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
380 else
381 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
382
383 /* we create the 802.11 header and SSID element */
384 max_probe_len -= 24 + 2;
385
386 /* basic ssid is added only for hw_scan with and old api */
387 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
388 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
389 !is_sched_scan)
390 max_probe_len -= 32;
391
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300392 /* DS parameter set element is added on 2.4GHZ band if required */
393 if (iwl_mvm_rrm_scan_needed(mvm))
394 max_probe_len -= 3;
395
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300396 return max_probe_len;
397}
398
399int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
400{
401 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
402
403 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
404 return max_ie_len;
405
406 /* TODO: [BUG] This function should return the maximum allowed size of
407 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
408 * in the same command. So the correct implementation of this function
409 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
410 * command has only 512 bytes and it would leave us with about 240
411 * bytes for scan IEs, which is clearly not enough. So meanwhile
412 * we will report an incorrect value. This may result in a failure to
413 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
414 * functions with -ENOBUFS, if a large enough probe will be provided.
415 */
416 return max_ie_len;
417}
418
Johannes Berg8ca151b2013-01-24 14:25:36 +0100419int iwl_mvm_scan_request(struct iwl_mvm *mvm,
420 struct ieee80211_vif *vif,
421 struct cfg80211_scan_request *req)
422{
423 struct iwl_host_cmd hcmd = {
424 .id = SCAN_REQUEST_CMD,
425 .len = { 0, },
426 .data = { mvm->scan_cmd, },
Johannes Berg8ca151b2013-01-24 14:25:36 +0100427 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
428 };
429 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
430 int ret;
431 u32 status;
432 int ssid_len = 0;
433 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300434 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
435 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200436 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100437
438 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200439
440 /* we should have failed registration if scan_cmd was NULL */
441 if (WARN_ON(mvm->scan_cmd == NULL))
442 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100443
444 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
445 mvm->scan_status = IWL_MVM_SCAN_OS;
David Spinadel05646792014-06-02 09:59:49 +0300446 memset(cmd, 0, ksize(cmd));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200447
Johannes Berg8ca151b2013-01-24 14:25:36 +0100448 cmd->channel_count = (u8)req->n_channels;
449 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
450 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
451 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200452
Johannes Bergab480032014-06-04 10:13:50 +0200453 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200454 cmd->max_out_time = cpu_to_le32(params.max_out_time);
455 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200456 if (params.passive_fragmented)
457 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200458
David Spinadelfb98be52014-05-04 12:51:10 +0300459 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100460 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
461 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200462
463 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
464 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
465 else
466 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
467
Johannes Berg8ca151b2013-01-24 14:25:36 +0100468 cmd->repeats = cpu_to_le32(1);
469
470 /*
471 * If the user asked for passive scan, don't change to active scan if
472 * you see any activity on the channel - remain passive.
473 */
474 if (req->n_ssids > 0) {
475 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300476 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300477 if (basic_ssid) {
478 ssid = req->ssids[0].ssid;
479 ssid_len = req->ssids[0].ssid_len;
480 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481 } else {
482 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300483 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100484 }
485
David Spinadelfb98be52014-05-04 12:51:10 +0300486 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
487 basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100488
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200489 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
Emmanuel Grumbach3856b782014-09-22 12:03:41 +0300490 3 << TX_CMD_FLG_BT_PRIO_POS);
491
Johannes Berg8ca151b2013-01-24 14:25:36 +0100492 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
493 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
494 cmd->tx_cmd.rate_n_flags =
495 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
496 req->no_cck);
497
498 cmd->tx_cmd.len =
499 cpu_to_le16(iwl_mvm_fill_probe_req(
500 (struct ieee80211_mgmt *)cmd->data,
501 vif->addr,
502 req->n_ssids, ssid, ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200503 req->ie, req->ie_len, NULL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100504 mvm->fw->ucode_capa.max_probe_length));
505
Alexander Bondar50df8a32014-03-12 20:30:51 +0200506 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100507
508 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
509 le16_to_cpu(cmd->tx_cmd.len) +
510 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
511 hcmd.len[0] = le16_to_cpu(cmd->len);
512
513 status = SCAN_RESPONSE_OK;
514 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
515 if (!ret && status == SCAN_RESPONSE_OK) {
516 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
517 } else {
518 /*
519 * If the scan failed, it usually means that the FW was unable
520 * to allocate the time events. Warn on it, but maybe we
521 * should try to send the command again with different params.
522 */
523 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
524 status, ret);
525 mvm->scan_status = IWL_MVM_SCAN_NONE;
526 ret = -EIO;
527 }
528 return ret;
529}
530
531int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
532 struct iwl_device_cmd *cmd)
533{
534 struct iwl_rx_packet *pkt = rxb_addr(rxb);
535 struct iwl_cmd_response *resp = (void *)pkt->data;
536
537 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
538 le32_to_cpu(resp->status));
539 return 0;
540}
541
542int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
543 struct iwl_device_cmd *cmd)
544{
545 struct iwl_rx_packet *pkt = rxb_addr(rxb);
546 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
547
Arik Nemtsov91b80252014-02-10 12:49:39 +0200548 lockdep_assert_held(&mvm->mutex);
549
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
551 notif->status, notif->scanned_channels);
552
Arik Nemtsov91b80252014-02-10 12:49:39 +0200553 if (mvm->scan_status == IWL_MVM_SCAN_OS)
554 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100555 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
556
Arik Nemtsov519e2022013-10-17 17:51:35 +0300557 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
558
Johannes Berg8ca151b2013-01-24 14:25:36 +0100559 return 0;
560}
561
David Spinadelfb98be52014-05-04 12:51:10 +0300562int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
563 struct iwl_rx_cmd_buffer *rxb,
564 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300565{
566 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadel35a000b2013-08-28 09:29:43 +0300567
David Spinadeld2496222014-05-20 12:46:37 +0300568 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
569 !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadelfb98be52014-05-04 12:51:10 +0300570 struct iwl_sched_scan_results *notif = (void *)pkt->data;
571
David Spinadeld2496222014-05-20 12:46:37 +0300572 if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
573 return 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300574 }
575
David Spinadeld2496222014-05-20 12:46:37 +0300576 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
577 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300578
579 return 0;
580}
581
Johannes Berg8ca151b2013-01-24 14:25:36 +0100582static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
583 struct iwl_rx_packet *pkt, void *data)
584{
585 struct iwl_mvm *mvm =
586 container_of(notif_wait, struct iwl_mvm, notif_wait);
587 struct iwl_scan_complete_notif *notif;
588 u32 *resp;
589
590 switch (pkt->hdr.cmd) {
591 case SCAN_ABORT_CMD:
592 resp = (void *)pkt->data;
593 if (*resp == CAN_ABORT_STATUS) {
594 IWL_DEBUG_SCAN(mvm,
595 "Scan can be aborted, wait until completion\n");
596 return false;
597 }
598
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300599 /*
600 * If scan cannot be aborted, it means that we had a
601 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
602 * ieee80211_scan_completed already.
603 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100604 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
605 *resp);
606 return true;
607
608 case SCAN_COMPLETE_NOTIFICATION:
609 notif = (void *)pkt->data;
610 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
611 notif->status);
612 return true;
613
614 default:
615 WARN_ON(1);
616 return false;
617 };
618}
619
David Spinadelfb98be52014-05-04 12:51:10 +0300620static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100621{
622 struct iwl_notification_wait wait_scan_abort;
623 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
624 SCAN_COMPLETE_NOTIFICATION };
625 int ret;
626
627 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
628 scan_abort_notif,
629 ARRAY_SIZE(scan_abort_notif),
630 iwl_mvm_scan_abort_notif, NULL);
631
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300632 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100633 if (ret) {
634 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200635 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100636 goto out_remove_notif;
637 }
638
Arik Nemtsov91b80252014-02-10 12:49:39 +0200639 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100640
641out_remove_notif:
642 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200643 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100644}
David Spinadel35a000b2013-08-28 09:29:43 +0300645
646int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
647 struct iwl_rx_cmd_buffer *rxb,
648 struct iwl_device_cmd *cmd)
649{
650 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadelfb98be52014-05-04 12:51:10 +0300651 u8 status, ebs_status;
David Spinadel35a000b2013-08-28 09:29:43 +0300652
David Spinadelfb98be52014-05-04 12:51:10 +0300653 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
654 struct iwl_periodic_scan_complete *scan_notif;
655
656 scan_notif = (void *)pkt->data;
657 status = scan_notif->status;
658 ebs_status = scan_notif->ebs_status;
659 } else {
660 struct iwl_scan_offload_complete *scan_notif;
661
662 scan_notif = (void *)pkt->data;
663 status = scan_notif->status;
664 ebs_status = scan_notif->ebs_status;
665 }
Johannes Berga6623e82014-01-27 15:40:53 +0100666 /* scan status must be locked for proper checking */
667 lockdep_assert_held(&mvm->mutex);
668
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300669 IWL_DEBUG_SCAN(mvm,
David Spinadelfb98be52014-05-04 12:51:10 +0300670 "%s completed, status %s, EBS status %s\n",
671 mvm->scan_status == IWL_MVM_SCAN_SCHED ?
672 "Scheduled scan" : "Scan",
673 status == IWL_SCAN_OFFLOAD_COMPLETED ?
674 "completed" : "aborted",
675 ebs_status == IWL_SCAN_EBS_SUCCESS ?
676 "success" : "failed");
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300677
David Spinadel35a000b2013-08-28 09:29:43 +0300678
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200679 /* only call mac80211 completion if the stop was initiated by FW */
680 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100681 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200682 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadelfb98be52014-05-04 12:51:10 +0300683 } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
684 mvm->scan_status = IWL_MVM_SCAN_NONE;
685 ieee80211_scan_completed(mvm->hw,
686 status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300687 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200688 }
David Spinadel35a000b2013-08-28 09:29:43 +0300689
David Spinadelfb98be52014-05-04 12:51:10 +0300690 mvm->last_ebs_successful = !ebs_status;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300691
David Spinadel35a000b2013-08-28 09:29:43 +0300692 return 0;
693}
694
695static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
696 struct ieee80211_vif *vif,
David Spinadel633e2712014-02-06 16:15:23 +0200697 struct ieee80211_scan_ies *ies,
David Spinadel35a000b2013-08-28 09:29:43 +0300698 enum ieee80211_band band,
699 struct iwl_tx_cmd *cmd,
700 u8 *data)
701{
702 u16 cmd_len;
703
704 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
705 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
706 cmd->sta_id = mvm->aux_sta.sta_id;
707
708 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
709
710 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
711 vif->addr,
712 1, NULL, 0,
David Spinadel633e2712014-02-06 16:15:23 +0200713 ies->ies[band], ies->len[band],
714 ies->common_ies, ies->common_ie_len,
David Spinadel35a000b2013-08-28 09:29:43 +0300715 SCAN_OFFLOAD_PROBE_REQ_SIZE);
716 cmd->len = cpu_to_le16(cmd_len);
717}
718
719static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
720 struct ieee80211_vif *vif,
721 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200722 struct iwl_scan_offload_cmd *scan,
723 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300724{
Eliad Peller89879412014-05-26 18:44:35 +0300725 scan->channel_count = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300726 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
727 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
728 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
729 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200730
Alexander Bondar50df8a32014-03-12 20:30:51 +0200731 scan->max_out_time = cpu_to_le32(params->max_out_time);
732 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200733
David Spinadel35a000b2013-08-28 09:29:43 +0300734 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
735 MAC_FILTER_IN_BEACON);
736 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
737 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200738
739 if (params->passive_fragmented)
740 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300741}
742
743static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
744{
745 int i;
746
747 for (i = 0; i < PROBE_OPTION_MAX; i++) {
748 if (!ssid_list[i].len)
749 break;
750 if (ssid_list[i].len == ssid_len &&
751 !memcmp(ssid_list->ssid, ssid, ssid_len))
752 return i;
753 }
754 return -1;
755}
756
757static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
David Spinadelfb98be52014-05-04 12:51:10 +0300758 struct iwl_ssid_ie *direct_scan,
759 u32 *ssid_bitmap, bool basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300760{
761 int i, j;
762 int index;
763
764 /*
765 * copy SSIDs from match list.
766 * iwl_config_sched_scan_profiles() uses the order of these ssids to
767 * config match list.
768 */
769 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100770 /* skip empty SSID matchsets */
771 if (!req->match_sets[i].ssid.ssid_len)
772 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300773 direct_scan[i].id = WLAN_EID_SSID;
774 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
775 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
776 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300777 }
778
779 /* add SSIDs from scan SSID list */
780 *ssid_bitmap = 0;
781 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
782 index = iwl_ssid_exist(req->ssids[j].ssid,
783 req->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300784 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300785 if (index < 0) {
David Spinadelfb98be52014-05-04 12:51:10 +0300786 if (!req->ssids[j].ssid_len && basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300787 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300788 direct_scan[i].id = WLAN_EID_SSID;
789 direct_scan[i].len = req->ssids[j].ssid_len;
790 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
791 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300792 *ssid_bitmap |= BIT(i + 1);
793 i++;
794 } else {
795 *ssid_bitmap |= BIT(index + 1);
796 }
797 }
798}
799
800static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
801 struct cfg80211_sched_scan_request *req,
David Spinadel762533b2014-06-05 11:20:43 +0300802 u8 *channels_buffer,
David Spinadel35a000b2013-08-28 09:29:43 +0300803 enum ieee80211_band band,
Eliad Peller89879412014-05-26 18:44:35 +0300804 int *head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200805 u32 ssid_bitmap,
806 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300807{
David Spinadel762533b2014-06-05 11:20:43 +0300808 u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
809 __le32 *type = (__le32 *)channels_buffer;
810 __le16 *channel_number = (__le16 *)(type + n_channels);
811 __le16 *iter_count = channel_number + n_channels;
812 __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
813 u8 *active_dwell = (u8 *)(iter_interval + n_channels);
814 u8 *passive_dwell = active_dwell + n_channels;
Eliad Peller89879412014-05-26 18:44:35 +0300815 int i, index = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300816
Eliad Peller89879412014-05-26 18:44:35 +0300817 for (i = 0; i < req->n_channels; i++) {
818 struct ieee80211_channel *chan = req->channels[i];
David Spinadel35a000b2013-08-28 09:29:43 +0300819
Eliad Peller89879412014-05-26 18:44:35 +0300820 if (chan->band != band)
821 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300822
Eliad Peller89879412014-05-26 18:44:35 +0300823 index = *head;
824 (*head)++;
David Spinadel35a000b2013-08-28 09:29:43 +0300825
David Spinadel762533b2014-06-05 11:20:43 +0300826 channel_number[index] = cpu_to_le16(chan->hw_value);
827 active_dwell[index] = params->dwell[band].active;
828 passive_dwell[index] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300829
David Spinadel762533b2014-06-05 11:20:43 +0300830 iter_count[index] = cpu_to_le16(1);
831 iter_interval[index] = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300832
Eliad Peller89879412014-05-26 18:44:35 +0300833 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
David Spinadel762533b2014-06-05 11:20:43 +0300834 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300835 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
836
David Spinadel762533b2014-06-05 11:20:43 +0300837 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
838 IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
David Spinadel35a000b2013-08-28 09:29:43 +0300839
Eliad Peller89879412014-05-26 18:44:35 +0300840 if (chan->flags & IEEE80211_CHAN_NO_HT40)
David Spinadel762533b2014-06-05 11:20:43 +0300841 type[index] |=
David Spinadel35a000b2013-08-28 09:29:43 +0300842 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
843
844 /* scan for all SSIDs from req->ssids */
David Spinadel762533b2014-06-05 11:20:43 +0300845 type[index] |= cpu_to_le32(ssid_bitmap);
David Spinadel35a000b2013-08-28 09:29:43 +0300846 }
847}
848
849int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
850 struct ieee80211_vif *vif,
851 struct cfg80211_sched_scan_request *req,
David Spinadel633e2712014-02-06 16:15:23 +0200852 struct ieee80211_scan_ies *ies)
David Spinadel35a000b2013-08-28 09:29:43 +0300853{
David Spinadel35a000b2013-08-28 09:29:43 +0300854 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
855 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
856 int head = 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300857 u32 ssid_bitmap;
858 int cmd_len;
859 int ret;
David Spinadel762533b2014-06-05 11:20:43 +0300860 u8 *probes;
David Spinadelfb98be52014-05-04 12:51:10 +0300861 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
862 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
David Spinadel35a000b2013-08-28 09:29:43 +0300863
864 struct iwl_scan_offload_cfg *scan_cfg;
865 struct iwl_host_cmd cmd = {
866 .id = SCAN_OFFLOAD_CONFIG_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300867 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200868 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300869
870 lockdep_assert_held(&mvm->mutex);
871
David Spinadel35a000b2013-08-28 09:29:43 +0300872 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
David Spinadel762533b2014-06-05 11:20:43 +0300873 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200874 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300875
876 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
877 if (!scan_cfg)
878 return -ENOMEM;
879
David Spinadel762533b2014-06-05 11:20:43 +0300880 probes = scan_cfg->data +
881 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
882
Johannes Bergab480032014-06-04 10:13:50 +0200883 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200884 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300885 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
886
David Spinadelfb98be52014-05-04 12:51:10 +0300887 iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
888 &ssid_bitmap, basic_ssid);
David Spinadel35a000b2013-08-28 09:29:43 +0300889 /* build tx frames for supported bands */
890 if (band_2ghz) {
891 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
892 IEEE80211_BAND_2GHZ,
893 &scan_cfg->scan_cmd.tx_cmd[0],
David Spinadel762533b2014-06-05 11:20:43 +0300894 probes);
895 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300896 IEEE80211_BAND_2GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200897 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300898 }
899 if (band_5ghz) {
900 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
901 IEEE80211_BAND_5GHZ,
902 &scan_cfg->scan_cmd.tx_cmd[1],
David Spinadel762533b2014-06-05 11:20:43 +0300903 probes +
David Spinadel35a000b2013-08-28 09:29:43 +0300904 SCAN_OFFLOAD_PROBE_REQ_SIZE);
David Spinadel762533b2014-06-05 11:20:43 +0300905 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
Eliad Peller89879412014-05-26 18:44:35 +0300906 IEEE80211_BAND_5GHZ, &head,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200907 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300908 }
909
910 cmd.data[0] = scan_cfg;
911 cmd.len[0] = cmd_len;
912 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
913
914 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
915
916 ret = iwl_mvm_send_cmd(mvm, &cmd);
917 kfree(scan_cfg);
918 return ret;
919}
920
921int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
922 struct cfg80211_sched_scan_request *req)
923{
924 struct iwl_scan_offload_profile *profile;
925 struct iwl_scan_offload_profile_cfg *profile_cfg;
926 struct iwl_scan_offload_blacklist *blacklist;
927 struct iwl_host_cmd cmd = {
928 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300929 .len[1] = sizeof(*profile_cfg),
930 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
931 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
932 };
933 int blacklist_len;
934 int i;
935 int ret;
936
937 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
938 return -EIO;
939
940 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
941 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
942 else
943 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
944
945 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
946 if (!blacklist)
947 return -ENOMEM;
948
949 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
950 if (!profile_cfg) {
951 ret = -ENOMEM;
952 goto free_blacklist;
953 }
954
955 cmd.data[0] = blacklist;
956 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
957 cmd.data[1] = profile_cfg;
958
959 /* No blacklist configuration */
960
961 profile_cfg->num_profiles = req->n_match_sets;
962 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
963 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
964 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200965 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
966 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300967
968 for (i = 0; i < req->n_match_sets; i++) {
969 profile = &profile_cfg->profiles[i];
970 profile->ssid_index = i;
971 /* Support any cipher and auth algorithm */
972 profile->unicast_cipher = 0xff;
973 profile->auth_alg = 0xff;
974 profile->network_type = IWL_NETWORK_TYPE_ANY;
975 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
976 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
977 }
978
979 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
980
981 ret = iwl_mvm_send_cmd(mvm, &cmd);
982 kfree(profile_cfg);
983free_blacklist:
984 kfree(blacklist);
985
986 return ret;
987}
988
David Spinadeld2496222014-05-20 12:46:37 +0300989static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
990 struct cfg80211_sched_scan_request *req)
991{
992 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
993 IWL_DEBUG_SCAN(mvm,
994 "Sending scheduled scan with filtering, n_match_sets %d\n",
995 req->n_match_sets);
996 return false;
997 }
998
999 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
1000 return true;
1001}
1002
David Spinadel35a000b2013-08-28 09:29:43 +03001003int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1004 struct cfg80211_sched_scan_request *req)
1005{
1006 struct iwl_scan_offload_req scan_req = {
1007 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
1008
1009 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001010 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001011 .schedule_line[0].full_scan_mul = 1,
1012
1013 .schedule_line[1].iterations = 0xff,
David Spinadelb14fc2b2014-06-25 13:17:53 +03001014 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
David Spinadel35a000b2013-08-28 09:29:43 +03001015 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
1016 };
1017
David Spinadeld2496222014-05-20 12:46:37 +03001018 if (iwl_mvm_scan_pass_all(mvm, req))
Eliad Pellerde33fb52013-11-10 12:59:46 +02001019 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +03001020
Haim Dreyfusse820c2d2014-04-06 11:19:09 +03001021 if (mvm->last_ebs_successful &&
1022 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1023 scan_req.flags |=
1024 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1025
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001026 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
David Spinadel35a000b2013-08-28 09:29:43 +03001027 sizeof(scan_req), &scan_req);
1028}
1029
Luciano Coelhob141c232014-10-01 13:22:40 +03001030int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1031 struct ieee80211_vif *vif,
1032 struct cfg80211_sched_scan_request *req,
1033 struct ieee80211_scan_ies *ies)
1034{
1035 int ret;
1036
David Spinadelb975e552014-11-17 12:30:05 +02001037 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1038 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1039 if (ret)
1040 return ret;
1041 ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
1042 } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
David Spinadeld2496222014-05-20 12:46:37 +03001043 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001044 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1045 if (ret)
1046 return ret;
1047 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1048 } else {
David Spinadeld2496222014-05-20 12:46:37 +03001049 mvm->scan_status = IWL_MVM_SCAN_SCHED;
Luciano Coelhob141c232014-10-01 13:22:40 +03001050 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1051 if (ret)
1052 return ret;
1053 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1054 if (ret)
1055 return ret;
1056 ret = iwl_mvm_sched_scan_start(mvm, req);
1057 }
1058
1059 return ret;
1060}
1061
David Spinadelfb98be52014-05-04 12:51:10 +03001062static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +03001063{
1064 int ret;
1065 struct iwl_host_cmd cmd = {
1066 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +03001067 };
1068 u32 status;
1069
1070 /* Exit instantly with error when device is not ready
1071 * to receive scan abort command or it does not perform
1072 * scheduled scan currently */
David Spinadelfb98be52014-05-04 12:51:10 +03001073 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1074 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1075 mvm->scan_status != IWL_MVM_SCAN_OS))
David Spinadel35a000b2013-08-28 09:29:43 +03001076 return -EIO;
1077
1078 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1079 if (ret)
1080 return ret;
1081
1082 if (status != CAN_ABORT_STATUS) {
1083 /*
1084 * The scan abort will return 1 for success or
1085 * 2 for "failure". A failure condition can be
1086 * due to simply not being in an active scan which
1087 * can occur if we send the scan abort before the
1088 * microcode has notified us that a scan is completed.
1089 */
1090 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001091 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +03001092 }
1093
1094 return ret;
1095}
1096
David Spinadelfb98be52014-05-04 12:51:10 +03001097int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +03001098{
1099 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001100 struct iwl_notification_wait wait_scan_done;
1101 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadelfb98be52014-05-04 12:51:10 +03001102 bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
David Spinadel35a000b2013-08-28 09:29:43 +03001103
1104 lockdep_assert_held(&mvm->mutex);
1105
David Spinadeld2496222014-05-20 12:46:37 +03001106 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1107 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
1108 notify);
1109
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +02001110 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1111 return 0;
1112
1113 if (iwl_mvm_is_radio_killed(mvm))
1114 goto out;
1115
David Spinadelfb98be52014-05-04 12:51:10 +03001116 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1117 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1118 mvm->scan_status != IWL_MVM_SCAN_OS)) {
1119 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001120 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001121 }
1122
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001123 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1124 scan_done_notif,
1125 ARRAY_SIZE(scan_done_notif),
1126 NULL, NULL);
1127
David Spinadelfb98be52014-05-04 12:51:10 +03001128 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001129 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +03001130 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1131 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001132 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1133 return ret;
1134 }
1135
David Spinadelfb98be52014-05-04 12:51:10 +03001136 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1137 sched ? "offloaded " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001138
1139 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1140 if (ret)
1141 return ret;
1142
1143 /*
1144 * Clear the scan status so the next scan requests will succeed. This
1145 * also ensures the Rx handler doesn't do anything, as the scan was
Eliad Peller4ff78182014-06-22 14:44:44 +03001146 * stopped from above. Since the rx handler won't do anything now,
1147 * we have to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001148 */
Eliad Peller4ff78182014-06-22 14:44:44 +03001149 if (mvm->scan_status == IWL_MVM_SCAN_OS)
1150 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1151
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +02001152out:
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001153 mvm->scan_status = IWL_MVM_SCAN_NONE;
1154
David Spinadelfb98be52014-05-04 12:51:10 +03001155 if (notify) {
1156 if (sched)
1157 ieee80211_sched_scan_stopped(mvm->hw);
1158 else
1159 ieee80211_scan_completed(mvm->hw, true);
1160 }
David Spinadel636a2cd2014-05-01 15:57:22 +03001161
Arik Nemtsov33ea27f2014-02-10 15:34:29 +02001162 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +03001163}
David Spinadelfb98be52014-05-04 12:51:10 +03001164
1165static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1166 struct iwl_scan_req_tx_cmd *tx_cmd,
1167 bool no_cck)
1168{
1169 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1170 TX_CMD_FLG_BT_DIS);
1171 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1172 IEEE80211_BAND_2GHZ,
1173 no_cck);
1174 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1175
1176 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1177 TX_CMD_FLG_BT_DIS);
1178 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1179 IEEE80211_BAND_5GHZ,
1180 no_cck);
1181 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1182}
1183
1184static void
1185iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1186 struct ieee80211_channel **channels,
1187 int n_channels, u32 ssid_bitmap,
1188 struct iwl_scan_req_unified_lmac *cmd)
1189{
1190 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1191 int i;
1192
1193 for (i = 0; i < n_channels; i++) {
1194 channel_cfg[i].channel_num =
1195 cpu_to_le16(channels[i]->hw_value);
1196 channel_cfg[i].iter_count = cpu_to_le16(1);
1197 channel_cfg[i].iter_interval = 0;
1198 channel_cfg[i].flags =
1199 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1200 ssid_bitmap);
1201 }
1202}
1203
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001204static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
1205 size_t len, u8 *const pos)
1206{
1207 static const u8 before_ds_params[] = {
1208 WLAN_EID_SSID,
1209 WLAN_EID_SUPP_RATES,
1210 WLAN_EID_REQUEST,
1211 WLAN_EID_EXT_SUPP_RATES,
1212 };
1213 size_t offs;
1214 u8 *newpos = pos;
1215
1216 if (!iwl_mvm_rrm_scan_needed(mvm)) {
1217 memcpy(newpos, ies, len);
1218 return newpos + len;
1219 }
1220
1221 offs = ieee80211_ie_split(ies, len,
1222 before_ds_params,
1223 ARRAY_SIZE(before_ds_params),
1224 0);
1225
1226 memcpy(newpos, ies, offs);
1227 newpos += offs;
1228
1229 /* Add a placeholder for DS Parameter Set element */
1230 *newpos++ = WLAN_EID_DS_PARAMS;
1231 *newpos++ = 1;
1232 *newpos++ = 0;
1233
1234 memcpy(newpos, ies + offs, len - offs);
1235 newpos += len - offs;
1236
1237 return newpos;
1238}
1239
David Spinadelfb98be52014-05-04 12:51:10 +03001240static void
1241iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1242 struct ieee80211_scan_ies *ies,
Johannes Bergeffd05a2014-11-18 17:21:19 +01001243 struct iwl_scan_probe_req *preq,
1244 const u8 *mac_addr, const u8 *mac_addr_mask)
David Spinadelfb98be52014-05-04 12:51:10 +03001245{
David Spinadelfb98be52014-05-04 12:51:10 +03001246 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001247 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001248
Johannes Bergeffd05a2014-11-18 17:21:19 +01001249 /*
1250 * Unfortunately, right now the offload scan doesn't support randomising
1251 * within the firmware, so until the firmware API is ready we implement
1252 * it in the driver. This means that the scan iterations won't really be
1253 * random, only when it's restarted, but at least that helps a bit.
1254 */
1255 if (mac_addr)
1256 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
1257 else
1258 memcpy(frame->sa, vif->addr, ETH_ALEN);
1259
David Spinadelfb98be52014-05-04 12:51:10 +03001260 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1261 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +03001262 eth_broadcast_addr(frame->bssid);
1263 frame->seq_ctrl = 0;
1264
1265 pos = frame->u.probe_req.variable;
1266 *pos++ = WLAN_EID_SSID;
1267 *pos++ = 0;
1268
1269 preq->mac_header.offset = 0;
1270 preq->mac_header.len = cpu_to_le16(24 + 2);
1271
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001272 /* Insert ds parameter set element on 2.4 GHz band */
1273 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
1274 ies->ies[IEEE80211_BAND_2GHZ],
1275 ies->len[IEEE80211_BAND_2GHZ],
1276 pos);
David Spinadelfb98be52014-05-04 12:51:10 +03001277 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +03001278 preq->band_data[0].len = cpu_to_le16(newpos - pos);
1279 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +03001280
1281 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1282 ies->len[IEEE80211_BAND_5GHZ]);
1283 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1284 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1285 pos += ies->len[IEEE80211_BAND_5GHZ];
1286
1287 memcpy(pos, ies->common_ies, ies->common_ie_len);
1288 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1289 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1290}
1291
1292static void
1293iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1294 struct iwl_scan_req_unified_lmac *cmd,
1295 struct iwl_mvm_scan_params *params)
1296{
David Spinadelaf913442014-06-12 19:29:40 +03001297 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +03001298 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1299 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1300 if (params->passive_fragmented)
1301 cmd->fragmented_dwell =
1302 params->dwell[IEEE80211_BAND_2GHZ].passive;
David Spinadelfb98be52014-05-04 12:51:10 +03001303 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1304 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1305 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1306 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +03001307 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +03001308
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001309 if (iwl_mvm_rrm_scan_needed(mvm))
1310 cmd->scan_flags |=
1311 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +03001312}
1313
1314int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1315 struct ieee80211_vif *vif,
1316 struct ieee80211_scan_request *req)
1317{
1318 struct iwl_host_cmd hcmd = {
1319 .id = SCAN_OFFLOAD_REQUEST_CMD,
1320 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1321 sizeof(struct iwl_scan_channel_cfg_lmac) *
1322 mvm->fw->ucode_capa.n_scan_channels +
1323 sizeof(struct iwl_scan_probe_req), },
1324 .data = { mvm->scan_cmd, },
1325 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1326 };
1327 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001328 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001329 struct iwl_mvm_scan_params params = {};
1330 u32 flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001331 u32 ssid_bitmap = 0;
David Spinadelfb98be52014-05-04 12:51:10 +03001332 int ret, i;
1333
1334 lockdep_assert_held(&mvm->mutex);
1335
1336 /* we should have failed registration if scan_cmd was NULL */
1337 if (WARN_ON(mvm->scan_cmd == NULL))
1338 return -ENOMEM;
1339
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001340 if (req->req.n_ssids > PROBE_OPTION_MAX ||
1341 req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1342 req->ies.len[NL80211_BAND_5GHZ] >
1343 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1344 req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1345 return -ENOBUFS;
David Spinadelfb98be52014-05-04 12:51:10 +03001346
1347 mvm->scan_status = IWL_MVM_SCAN_OS;
1348
1349 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1350 &params);
1351
1352 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1353
1354 cmd->n_channels = (u8)req->req.n_channels;
1355
1356 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1357
1358 if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1359 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1360
1361 if (params.passive_fragmented)
1362 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1363
1364 if (req->req.n_ssids == 0)
1365 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1366
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001367 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001368
1369 cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1370 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1371 MAC_FILTER_IN_BEACON);
1372 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1373 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1374 req->req.n_ssids, 0);
1375
1376 cmd->schedule[0].delay = 0;
1377 cmd->schedule[0].iterations = 1;
1378 cmd->schedule[0].full_scan_mul = 0;
1379 cmd->schedule[1].delay = 0;
1380 cmd->schedule[1].iterations = 0;
1381 cmd->schedule[1].full_scan_mul = 0;
1382
David Spinadel1f9c4182014-12-01 23:30:07 +02001383 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
1384 mvm->last_ebs_successful) {
1385 cmd->channel_opt[0].flags =
1386 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1387 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1388 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1389 cmd->channel_opt[0].non_ebs_ratio =
1390 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1391 cmd->channel_opt[1].flags =
1392 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1393 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1394 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1395 cmd->channel_opt[1].non_ebs_ratio =
1396 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1397 }
1398
David Spinadelfb98be52014-05-04 12:51:10 +03001399 for (i = 1; i <= req->req.n_ssids; i++)
1400 ssid_bitmap |= BIT(i);
1401
1402 iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1403 req->req.n_channels, ssid_bitmap,
1404 cmd);
1405
David Spinadeld2496222014-05-20 12:46:37 +03001406 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1407 mvm->fw->ucode_capa.n_scan_channels);
1408
Johannes Bergeffd05a2014-11-18 17:21:19 +01001409 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
1410 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1411 req->req.mac_addr : NULL,
1412 req->req.mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001413
1414 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1415 if (!ret) {
1416 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1417 } else {
1418 /*
1419 * If the scan failed, it usually means that the FW was unable
1420 * to allocate the time events. Warn on it, but maybe we
1421 * should try to send the command again with different params.
1422 */
1423 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1424 mvm->scan_status = IWL_MVM_SCAN_NONE;
1425 ret = -EIO;
1426 }
1427 return ret;
1428}
1429
1430int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1431 struct ieee80211_vif *vif,
1432 struct cfg80211_sched_scan_request *req,
1433 struct ieee80211_scan_ies *ies)
1434{
1435 struct iwl_host_cmd hcmd = {
1436 .id = SCAN_OFFLOAD_REQUEST_CMD,
1437 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1438 sizeof(struct iwl_scan_channel_cfg_lmac) *
1439 mvm->fw->ucode_capa.n_scan_channels +
1440 sizeof(struct iwl_scan_probe_req), },
1441 .data = { mvm->scan_cmd, },
1442 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1443 };
1444 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +03001445 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +03001446 struct iwl_mvm_scan_params params = {};
1447 int ret;
1448 u32 flags = 0, ssid_bitmap = 0;
1449
1450 lockdep_assert_held(&mvm->mutex);
1451
1452 /* we should have failed registration if scan_cmd was NULL */
1453 if (WARN_ON(mvm->scan_cmd == NULL))
1454 return -ENOMEM;
1455
Andrei Otcheretianski48849a42014-09-09 10:58:49 +03001456 if (req->n_ssids > PROBE_OPTION_MAX ||
1457 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1458 ies->len[NL80211_BAND_5GHZ] >
1459 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1460 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
David Spinadelfb98be52014-05-04 12:51:10 +03001461 return -ENOBUFS;
1462
1463 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1464
1465 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1466
1467 cmd->n_channels = (u8)req->n_channels;
1468
David Spinadeld2496222014-05-20 12:46:37 +03001469 if (iwl_mvm_scan_pass_all(mvm, req))
David Spinadelfb98be52014-05-04 12:51:10 +03001470 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
David Spinadel7e2a3882014-12-23 14:38:09 +02001471 else
1472 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
David Spinadelfb98be52014-05-04 12:51:10 +03001473
1474 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1475 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1476
1477 if (params.passive_fragmented)
1478 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1479
1480 if (req->n_ssids == 0)
1481 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1482
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +03001483 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +03001484
1485 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1486 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1487 MAC_FILTER_IN_BEACON);
1488 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1489 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1490
David Spinadelb14fc2b2014-06-25 13:17:53 +03001491 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001492 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1493 cmd->schedule[0].full_scan_mul = 1;
1494
David Spinadelb14fc2b2014-06-25 13:17:53 +03001495 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +03001496 cmd->schedule[1].iterations = 0xff;
1497 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1498
David Spinadel1f9c4182014-12-01 23:30:07 +02001499 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1500 mvm->last_ebs_successful) {
1501 cmd->channel_opt[0].flags =
1502 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1503 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1504 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1505 cmd->channel_opt[0].non_ebs_ratio =
1506 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1507 cmd->channel_opt[1].flags =
1508 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1509 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1510 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1511 cmd->channel_opt[1].non_ebs_ratio =
1512 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1513 }
1514
David Spinadelfb98be52014-05-04 12:51:10 +03001515 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1516 ssid_bitmap, cmd);
1517
David Spinadeld2496222014-05-20 12:46:37 +03001518 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1519 mvm->fw->ucode_capa.n_scan_channels);
1520
Johannes Bergeffd05a2014-11-18 17:21:19 +01001521 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
1522 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1523 req->mac_addr : NULL,
1524 req->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001525
1526 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1527 if (!ret) {
1528 IWL_DEBUG_SCAN(mvm,
1529 "Sched scan request was sent successfully\n");
1530 } else {
1531 /*
1532 * If the scan failed, it usually means that the FW was unable
1533 * to allocate the time events. Warn on it, but maybe we
1534 * should try to send the command again with different params.
1535 */
1536 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1537 mvm->scan_status = IWL_MVM_SCAN_NONE;
1538 ret = -EIO;
1539 }
1540 return ret;
1541}
1542
1543
1544int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1545{
David Spinadeld2496222014-05-20 12:46:37 +03001546 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1547 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1548 true);
1549
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001550 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1551 return 0;
1552
1553 if (iwl_mvm_is_radio_killed(mvm)) {
1554 ieee80211_scan_completed(mvm->hw, true);
1555 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1556 mvm->scan_status = IWL_MVM_SCAN_NONE;
1557 return 0;
1558 }
1559
David Spinadelfb98be52014-05-04 12:51:10 +03001560 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1561 return iwl_mvm_scan_offload_stop(mvm, true);
1562 return iwl_mvm_cancel_regular_scan(mvm);
1563}
David Spinadeld2496222014-05-20 12:46:37 +03001564
1565/* UMAC scan API */
1566
1567struct iwl_umac_scan_done {
1568 struct iwl_mvm *mvm;
1569 enum iwl_umac_scan_uid_type type;
1570};
1571
1572static int rate_to_scan_rate_flag(unsigned int rate)
1573{
1574 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1575 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1576 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1577 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1578 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1579 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1580 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1581 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1582 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1583 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1584 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1585 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1586 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1587 };
1588
1589 return rate_to_scan_rate[rate];
1590}
1591
1592static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1593{
1594 struct ieee80211_supported_band *band;
1595 unsigned int rates = 0;
1596 int i;
1597
1598 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1599 for (i = 0; i < band->n_bitrates; i++)
1600 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1601 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1602 for (i = 0; i < band->n_bitrates; i++)
1603 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1604
1605 /* Set both basic rates and supported rates */
1606 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1607
1608 return cpu_to_le32(rates);
1609}
1610
1611int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1612{
1613
1614 struct iwl_scan_config *scan_config;
1615 struct ieee80211_supported_band *band;
1616 int num_channels =
1617 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1618 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1619 int ret, i, j = 0, cmd_size, data_size;
1620 struct iwl_host_cmd cmd = {
1621 .id = SCAN_CFG_CMD,
1622 };
1623
1624 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1625 return -ENOBUFS;
1626
1627 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1628
1629 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1630 if (!scan_config)
1631 return -ENOMEM;
1632
1633 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1634 scan_config->hdr.size = cpu_to_le16(data_size);
1635 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1636 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1637 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1638 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1639 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1640 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1641 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1642 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1643 SCAN_CONFIG_N_CHANNELS(num_channels));
1644 scan_config->tx_chains = cpu_to_le32(mvm->fw->valid_tx_ant);
1645 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1646 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1647 scan_config->out_of_channel_time = cpu_to_le32(170);
1648 scan_config->suspend_time = cpu_to_le32(30);
1649 scan_config->dwell_active = 20;
1650 scan_config->dwell_passive = 110;
1651 scan_config->dwell_fragmented = 20;
1652
1653 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1654
1655 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1656 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1657 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1658 IWL_CHANNEL_FLAG_EBS_ADD |
1659 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1660
1661 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1662 for (i = 0; i < band->n_channels; i++, j++)
1663 scan_config->channel_array[j] = band->channels[i].center_freq;
1664 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1665 for (i = 0; i < band->n_channels; i++, j++)
1666 scan_config->channel_array[j] = band->channels[i].center_freq;
1667
1668 cmd.data[0] = scan_config;
1669 cmd.len[0] = cmd_size;
1670 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1671
1672 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1673
1674 ret = iwl_mvm_send_cmd(mvm, &cmd);
1675
1676 kfree(scan_config);
1677 return ret;
1678}
1679
1680static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1681{
1682 int i;
1683
1684 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1685 if (mvm->scan_uid[i] == uid)
1686 return i;
1687
1688 return i;
1689}
1690
1691static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1692{
1693 return iwl_mvm_find_scan_uid(mvm, 0);
1694}
1695
1696static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1697 enum iwl_umac_scan_uid_type type)
1698{
1699 int i;
1700
1701 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1702 if (mvm->scan_uid[i] & type)
1703 return true;
1704
1705 return false;
1706}
1707
1708static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1709 enum iwl_umac_scan_uid_type type)
1710{
1711 u32 uid;
1712
1713 /* make sure exactly one bit is on in scan type */
1714 WARN_ON(hweight8(type) != 1);
1715
1716 /*
1717 * Make sure scan uids are unique. If one scan lasts long time while
1718 * others are completing frequently, the seq number will wrap up and
1719 * we may have more than one scan with the same uid.
1720 */
1721 do {
1722 uid = type | (mvm->scan_seq_num <<
1723 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1724 mvm->scan_seq_num++;
1725 } while (iwl_mvm_find_scan_uid(mvm, uid) <
1726 IWL_MVM_MAX_SIMULTANEOUS_SCANS);
1727
1728 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1729
1730 return uid;
1731}
1732
1733static void
1734iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1735 struct iwl_scan_req_umac *cmd,
1736 struct iwl_mvm_scan_params *params)
1737{
1738 memset(cmd, 0, ksize(cmd));
1739 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1740 sizeof(struct iwl_mvm_umac_cmd_hdr));
1741 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1742 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1743 if (params->passive_fragmented)
1744 cmd->fragmented_dwell =
1745 params->dwell[IEEE80211_BAND_2GHZ].passive;
1746 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1747 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1748 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1749}
1750
1751static void
1752iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1753 struct ieee80211_channel **channels,
1754 int n_channels, u32 ssid_bitmap,
1755 struct iwl_scan_req_umac *cmd)
1756{
1757 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1758 int i;
1759
1760 for (i = 0; i < n_channels; i++) {
1761 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1762 channel_cfg[i].channel_num = channels[i]->hw_value;
1763 channel_cfg[i].iter_count = 1;
1764 channel_cfg[i].iter_interval = 0;
1765 }
1766}
1767
1768static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1769 struct cfg80211_ssid *ssids,
1770 int fragmented)
1771{
1772 int flags = 0;
1773
1774 if (n_ssids == 0)
1775 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1776
1777 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1778 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1779
1780 if (fragmented)
1781 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1782
1783 if (iwl_mvm_rrm_scan_needed(mvm))
1784 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1785
1786 return flags;
1787}
1788
1789int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1790 struct ieee80211_scan_request *req)
1791{
1792 struct iwl_host_cmd hcmd = {
1793 .id = SCAN_REQ_UMAC,
1794 .len = { iwl_mvm_scan_size(mvm), },
1795 .data = { mvm->scan_cmd, },
1796 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1797 };
1798 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1799 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1800 sizeof(struct iwl_scan_channel_cfg_umac) *
1801 mvm->fw->ucode_capa.n_scan_channels;
1802 struct iwl_mvm_scan_params params = {};
1803 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001804 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001805 int ret, i, uid_idx;
1806
1807 lockdep_assert_held(&mvm->mutex);
1808
1809 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1810 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1811 return -EBUSY;
1812
1813 /* we should have failed registration if scan_cmd was NULL */
1814 if (WARN_ON(mvm->scan_cmd == NULL))
1815 return -ENOMEM;
1816
1817 if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
1818 req->ies.common_ie_len +
1819 req->ies.len[NL80211_BAND_2GHZ] +
1820 req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
1821 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
1822 mvm->fw->ucode_capa.n_scan_channels))
1823 return -ENOBUFS;
1824
1825 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1826 &params);
1827
1828 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1829
1830 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1831 mvm->scan_uid[uid_idx] = uid;
1832 cmd->uid = cpu_to_le32(uid);
1833
1834 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1835
1836 flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
1837 req->req.ssids,
1838 params.passive_fragmented);
1839
1840 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1841
1842 cmd->general_flags = cpu_to_le32(flags);
1843 cmd->n_channels = req->req.n_channels;
1844
1845 for (i = 0; i < req->req.n_ssids; i++)
1846 ssid_bitmap |= BIT(i);
1847
1848 iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
1849 req->req.n_channels, ssid_bitmap, cmd);
1850
1851 sec_part->schedule[0].iter_count = 1;
1852 sec_part->delay = 0;
1853
Johannes Bergeffd05a2014-11-18 17:21:19 +01001854 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
1855 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1856 req->req.mac_addr : NULL,
1857 req->req.mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001858
1859 iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
1860 req->req.n_ssids, 0);
1861
1862 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1863 if (!ret) {
1864 IWL_DEBUG_SCAN(mvm,
1865 "Scan request was sent successfully\n");
1866 } else {
1867 /*
1868 * If the scan failed, it usually means that the FW was unable
1869 * to allocate the time events. Warn on it, but maybe we
1870 * should try to send the command again with different params.
1871 */
1872 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1873 }
1874 return ret;
1875}
1876
1877int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1878 struct cfg80211_sched_scan_request *req,
1879 struct ieee80211_scan_ies *ies)
1880{
1881
1882 struct iwl_host_cmd hcmd = {
1883 .id = SCAN_REQ_UMAC,
1884 .len = { iwl_mvm_scan_size(mvm), },
1885 .data = { mvm->scan_cmd, },
1886 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1887 };
1888 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1889 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1890 sizeof(struct iwl_scan_channel_cfg_umac) *
1891 mvm->fw->ucode_capa.n_scan_channels;
1892 struct iwl_mvm_scan_params params = {};
1893 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001894 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001895 int ret, uid_idx;
1896
1897 lockdep_assert_held(&mvm->mutex);
1898
1899 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1900 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1901 return -EBUSY;
1902
1903 /* we should have failed registration if scan_cmd was NULL */
1904 if (WARN_ON(mvm->scan_cmd == NULL))
1905 return -ENOMEM;
1906
1907 if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1908 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1909 ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1910 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
1911 mvm->fw->ucode_capa.n_scan_channels))
1912 return -ENOBUFS;
1913
1914 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
1915 &params);
1916
1917 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1918
1919 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1920
1921 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1922 mvm->scan_uid[uid_idx] = uid;
1923 cmd->uid = cpu_to_le32(uid);
1924
1925 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1926
1927 flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
1928 params.passive_fragmented);
1929
1930 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1931
1932 if (iwl_mvm_scan_pass_all(mvm, req))
1933 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1934 else
1935 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1936
1937 cmd->general_flags = cpu_to_le32(flags);
1938
1939 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1940 mvm->last_ebs_successful)
1941 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1942 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1943 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1944
1945 cmd->n_channels = req->n_channels;
1946
1947 iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
1948 false);
1949
1950 /* This API uses bits 0-19 instead of 1-20. */
1951 ssid_bitmap = ssid_bitmap >> 1;
1952
1953 iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1954 ssid_bitmap, cmd);
1955
1956 sec_part->schedule[0].interval =
1957 cpu_to_le16(req->interval / MSEC_PER_SEC);
1958 sec_part->schedule[0].iter_count = 0xff;
1959
1960 sec_part->delay = 0;
1961
Johannes Bergeffd05a2014-11-18 17:21:19 +01001962 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
1963 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1964 req->mac_addr : NULL,
1965 req->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001966
1967 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1968 if (!ret) {
1969 IWL_DEBUG_SCAN(mvm,
1970 "Sched scan request was sent successfully\n");
1971 } else {
1972 /*
1973 * If the scan failed, it usually means that the FW was unable
1974 * to allocate the time events. Warn on it, but maybe we
1975 * should try to send the command again with different params.
1976 */
1977 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1978 }
1979 return ret;
1980}
1981
1982int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1983 struct iwl_rx_cmd_buffer *rxb,
1984 struct iwl_device_cmd *cmd)
1985{
1986 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1987 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1988 u32 uid = __le32_to_cpu(notif->uid);
1989 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1990 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1991
David Spinadel2992a322014-11-10 11:16:53 +02001992 /*
1993 * Scan uid may be set to zero in case of scan abort request from above.
1994 */
1995 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
David Spinadeld2496222014-05-20 12:46:37 +03001996 return 0;
1997
1998 IWL_DEBUG_SCAN(mvm,
1999 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
2000 uid, sched ? "sched" : "regular",
2001 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
2002 "completed" : "aborted",
2003 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
2004 "success" : "failed");
2005
2006 mvm->last_ebs_successful = !notif->ebs_status;
2007 mvm->scan_uid[uid_idx] = 0;
2008
2009 if (!sched) {
2010 ieee80211_scan_completed(mvm->hw,
2011 notif->status ==
2012 IWL_SCAN_OFFLOAD_ABORTED);
2013 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2014 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
2015 ieee80211_sched_scan_stopped(mvm->hw);
2016 } else {
2017 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
2018 }
2019
2020 return 0;
2021}
2022
2023static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
2024 struct iwl_rx_packet *pkt, void *data)
2025{
2026 struct iwl_umac_scan_done *scan_done = data;
2027 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2028 u32 uid = __le32_to_cpu(notif->uid);
2029 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
2030
2031 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
2032 return false;
2033
2034 if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
2035 return false;
2036
2037 /*
2038 * Clear scan uid of scans that was aborted from above and completed
2039 * in FW so the RX handler does nothing.
2040 */
2041 scan_done->mvm->scan_uid[uid_idx] = 0;
2042
2043 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
2044}
2045
2046static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
2047{
2048 struct iwl_umac_scan_abort cmd = {
2049 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
2050 sizeof(struct iwl_mvm_umac_cmd_hdr)),
2051 .uid = cpu_to_le32(uid),
2052 };
2053
2054 lockdep_assert_held(&mvm->mutex);
2055
2056 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2057
2058 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
2059}
2060
2061static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
2062 enum iwl_umac_scan_uid_type type, bool notify)
2063{
2064 struct iwl_notification_wait wait_scan_done;
2065 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
2066 struct iwl_umac_scan_done scan_done = {
2067 .mvm = mvm,
2068 .type = type,
2069 };
2070 int i, ret = -EIO;
2071
2072 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2073 scan_done_notif,
2074 ARRAY_SIZE(scan_done_notif),
2075 iwl_scan_umac_done_check, &scan_done);
2076
2077 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2078
2079 for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
2080 if (mvm->scan_uid[i] & type) {
2081 int err;
2082
2083 if (iwl_mvm_is_radio_killed(mvm) &&
2084 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
2085 ieee80211_scan_completed(mvm->hw, true);
2086 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2087 break;
2088 }
2089
2090 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
2091 if (!err)
2092 ret = 0;
2093 }
2094 }
2095
2096 if (ret) {
2097 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
2098 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2099 return ret;
2100 }
2101
2102 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
2103 if (ret)
2104 return ret;
2105
2106 if (notify) {
2107 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
2108 ieee80211_sched_scan_stopped(mvm->hw);
2109 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
2110 ieee80211_scan_completed(mvm->hw, true);
2111 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2112 }
2113 }
2114
2115 return ret;
2116}
2117
2118int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2119{
2120 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
2121 return sizeof(struct iwl_scan_req_umac) +
2122 sizeof(struct iwl_scan_channel_cfg_umac) *
2123 mvm->fw->ucode_capa.n_scan_channels +
2124 sizeof(struct iwl_scan_req_umac_tail);
2125
2126 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
2127 return sizeof(struct iwl_scan_req_unified_lmac) +
2128 sizeof(struct iwl_scan_channel_cfg_lmac) *
2129 mvm->fw->ucode_capa.n_scan_channels +
2130 sizeof(struct iwl_scan_probe_req);
2131
2132 return sizeof(struct iwl_scan_cmd) +
2133 mvm->fw->ucode_capa.max_probe_length +
2134 mvm->fw->ucode_capa.n_scan_channels *
2135 sizeof(struct iwl_scan_channel);
2136}