blob: 3206fa09725516689dc1999bd2f01325f077b9f4 [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 Berg8ca151b2013-01-24 14:25:36 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
64#include <linux/etherdevice.h>
65#include <net/mac80211.h>
66
67#include "mvm.h"
68#include "iwl-eeprom-parse.h"
69#include "fw-api-scan.h"
70
71#define IWL_PLCP_QUIET_THRESH 1
72#define IWL_ACTIVE_QUIET_TIME 10
Alexander Bondar8a110d92014-03-12 17:31:19 +020073
74struct iwl_mvm_scan_params {
75 u32 max_out_time;
76 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020077 bool passive_fragmented;
78 struct _dwell {
79 u16 passive;
80 u16 active;
81 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +020082};
Johannes Berg8ca151b2013-01-24 14:25:36 +010083
84static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
85{
86 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +030087 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010088
Oren Givon91b05d12013-08-19 08:36:48 +030089 if (mvm->scan_rx_ant != ANT_NONE)
90 rx_ant = mvm->scan_rx_ant;
91 else
Johannes Berg4ed735e2014-02-12 21:47:44 +010092 rx_ant = mvm->fw->valid_rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010093 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
94 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
95 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
96 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
97 return cpu_to_le16(rx_chain);
98}
99
Johannes Berg8ca151b2013-01-24 14:25:36 +0100100static inline __le32
101iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
102{
103 if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
104 return cpu_to_le32(PHY_BAND_24);
105 else
106 return cpu_to_le32(PHY_BAND_5);
107}
108
109static inline __le32
110iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
111 bool no_cck)
112{
113 u32 tx_ant;
114
115 mvm->scan_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100116 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100117 mvm->scan_last_antenna_idx);
118 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
119
120 if (band == IEEE80211_BAND_2GHZ && !no_cck)
121 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
122 tx_ant);
123 else
124 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
125}
126
127/*
128 * We insert the SSIDs in an inverted order, because the FW will
129 * invert it back. The most prioritized SSID, which is first in the
130 * request list, is not copied here, but inserted directly to the probe
131 * request.
132 */
133static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300134 struct cfg80211_scan_request *req,
135 int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100136{
137 int fw_idx, req_idx;
138
David Spinadel20f1a5d2013-08-21 09:14:27 +0300139 for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300140 req_idx--, fw_idx++) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100141 cmd->direct_scan[fw_idx].id = WLAN_EID_SSID;
142 cmd->direct_scan[fw_idx].len = req->ssids[req_idx].ssid_len;
143 memcpy(cmd->direct_scan[fw_idx].ssid,
144 req->ssids[req_idx].ssid,
145 req->ssids[req_idx].ssid_len);
146 }
147}
148
149/*
150 * If req->n_ssids > 0, it means we should do an active scan.
151 * In case of active scan w/o directed scan, we receive a zero-length SSID
152 * just to notify that this scan is active and not passive.
153 * In order to notify the FW of the number of SSIDs we wish to scan (including
154 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300155 * one for each SSID, and set the active bit (first). If the first SSID is
156 * already included in the probe template, so we need to set only
157 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100158 */
159static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
160{
161 if (band == IEEE80211_BAND_2GHZ)
162 return 30 + 3 * (n_ssids + 1);
163 return 20 + 2 * (n_ssids + 1);
164}
165
166static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
167{
168 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
169}
170
171static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300172 struct cfg80211_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200173 bool basic_ssid,
174 struct iwl_mvm_scan_params *params)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100175{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100176 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
177 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
178 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300179 int type = BIT(req->n_ssids) - 1;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200180 enum ieee80211_band band = req->channels[0]->band;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300181
182 if (!basic_ssid)
183 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100184
185 for (i = 0; i < cmd->channel_count; i++) {
186 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300187 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200188 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300189 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200190 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
191 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100192 chan->iteration_count = cpu_to_le16(1);
193 chan++;
194 }
195}
196
197/*
198 * Fill in probe request with the following parameters:
199 * TA is our vif HW address, which mac80211 ensures we have.
200 * Packet is broadcasted, so this is both SA and DA.
201 * The probe request IE is made out of two: first comes the most prioritized
202 * SSID if a directed scan is requested. Second comes whatever extra
203 * information was given to us as the scan request IE.
204 */
205static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
206 int n_ssids, const u8 *ssid, int ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200207 const u8 *band_ie, int band_ie_len,
208 const u8 *common_ie, int common_ie_len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100209 int left)
210{
211 int len = 0;
212 u8 *pos = NULL;
213
214 /* Make sure there is enough space for the probe request,
215 * two mandatory IEs and the data */
216 left -= 24;
217 if (left < 0)
218 return 0;
219
220 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
221 eth_broadcast_addr(frame->da);
222 memcpy(frame->sa, ta, ETH_ALEN);
223 eth_broadcast_addr(frame->bssid);
224 frame->seq_ctrl = 0;
225
226 len += 24;
227
228 /* for passive scans, no need to fill anything */
229 if (n_ssids == 0)
230 return (u16)len;
231
232 /* points to the payload of the request */
233 pos = &frame->u.probe_req.variable[0];
234
235 /* fill in our SSID IE */
236 left -= ssid_len + 2;
237 if (left < 0)
238 return 0;
239 *pos++ = WLAN_EID_SSID;
240 *pos++ = ssid_len;
241 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
242 memcpy(pos, ssid, ssid_len);
243 pos += ssid_len;
244 }
245
246 len += ssid_len + 2;
247
David Spinadel633e2712014-02-06 16:15:23 +0200248 if (WARN_ON(left < band_ie_len + common_ie_len))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100249 return len;
250
David Spinadel633e2712014-02-06 16:15:23 +0200251 if (band_ie && band_ie_len) {
252 memcpy(pos, band_ie, band_ie_len);
253 pos += band_ie_len;
254 len += band_ie_len;
255 }
256
257 if (common_ie && common_ie_len) {
258 memcpy(pos, common_ie, common_ie_len);
259 pos += common_ie_len;
260 len += common_ie_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100261 }
262
263 return (u16)len;
264}
265
Alexander Bondar8a110d92014-03-12 17:31:19 +0200266static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
267 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200268{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200269 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
270 bool *global_bound = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200271
Alexander Bondar8a110d92014-03-12 17:31:19 +0200272 if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
273 *global_bound = true;
274}
275
276static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200277 struct ieee80211_vif *vif,
278 int n_ssids,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200279 struct iwl_mvm_scan_params *params)
280{
281 bool global_bound = false;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200282 enum ieee80211_band band;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200283
284 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
285 IEEE80211_IFACE_ITER_NORMAL,
286 iwl_mvm_scan_condition_iterator,
287 &global_bound);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200288
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300289 if (!global_bound)
290 goto not_bound;
291
292 params->suspend_time = 100;
293 params->max_out_time = 600;
294
295 if (iwl_mvm_low_latency(mvm)) {
296 params->suspend_time = 250;
297 params->max_out_time = 250;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200298 }
299
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300300not_bound:
301
Alexander Bondar50df8a32014-03-12 20:30:51 +0200302 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300303 params->dwell[band].passive = iwl_mvm_get_passive_dwell(band);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200304 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
305 n_ssids);
306 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200307}
308
Johannes Berg8ca151b2013-01-24 14:25:36 +0100309int iwl_mvm_scan_request(struct iwl_mvm *mvm,
310 struct ieee80211_vif *vif,
311 struct cfg80211_scan_request *req)
312{
313 struct iwl_host_cmd hcmd = {
314 .id = SCAN_REQUEST_CMD,
315 .len = { 0, },
316 .data = { mvm->scan_cmd, },
Johannes Berg8ca151b2013-01-24 14:25:36 +0100317 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
318 };
319 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
320 int ret;
321 u32 status;
322 int ssid_len = 0;
323 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300324 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
325 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200326 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100327
328 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200329
330 /* we should have failed registration if scan_cmd was NULL */
331 if (WARN_ON(mvm->scan_cmd == NULL))
332 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100333
334 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
335 mvm->scan_status = IWL_MVM_SCAN_OS;
336 memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
337 mvm->fw->ucode_capa.max_probe_length +
338 (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200339
Johannes Berg8ca151b2013-01-24 14:25:36 +0100340 cmd->channel_count = (u8)req->n_channels;
341 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
342 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
343 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200344
Alexander Bondar50df8a32014-03-12 20:30:51 +0200345 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200346 cmd->max_out_time = cpu_to_le32(params.max_out_time);
347 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200348 if (params.passive_fragmented)
349 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200350
Johannes Berg8ca151b2013-01-24 14:25:36 +0100351 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
352 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
353 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200354
355 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
356 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
357 else
358 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
359
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360 cmd->repeats = cpu_to_le32(1);
361
362 /*
363 * If the user asked for passive scan, don't change to active scan if
364 * you see any activity on the channel - remain passive.
365 */
366 if (req->n_ssids > 0) {
367 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300368 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300369 if (basic_ssid) {
370 ssid = req->ssids[0].ssid;
371 ssid_len = req->ssids[0].ssid_len;
372 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100373 } else {
374 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300375 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100376 }
377
David Spinadel20f1a5d2013-08-21 09:14:27 +0300378 iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100379
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200380 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
381 TX_CMD_FLG_BT_DIS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100382 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
383 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
384 cmd->tx_cmd.rate_n_flags =
385 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
386 req->no_cck);
387
388 cmd->tx_cmd.len =
389 cpu_to_le16(iwl_mvm_fill_probe_req(
390 (struct ieee80211_mgmt *)cmd->data,
391 vif->addr,
392 req->n_ssids, ssid, ssid_len,
David Spinadel633e2712014-02-06 16:15:23 +0200393 req->ie, req->ie_len, NULL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100394 mvm->fw->ucode_capa.max_probe_length));
395
Alexander Bondar50df8a32014-03-12 20:30:51 +0200396 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100397
398 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
399 le16_to_cpu(cmd->tx_cmd.len) +
400 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
401 hcmd.len[0] = le16_to_cpu(cmd->len);
402
403 status = SCAN_RESPONSE_OK;
404 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
405 if (!ret && status == SCAN_RESPONSE_OK) {
406 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
407 } else {
408 /*
409 * If the scan failed, it usually means that the FW was unable
410 * to allocate the time events. Warn on it, but maybe we
411 * should try to send the command again with different params.
412 */
413 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
414 status, ret);
415 mvm->scan_status = IWL_MVM_SCAN_NONE;
416 ret = -EIO;
417 }
418 return ret;
419}
420
421int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
422 struct iwl_device_cmd *cmd)
423{
424 struct iwl_rx_packet *pkt = rxb_addr(rxb);
425 struct iwl_cmd_response *resp = (void *)pkt->data;
426
427 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
428 le32_to_cpu(resp->status));
429 return 0;
430}
431
432int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
433 struct iwl_device_cmd *cmd)
434{
435 struct iwl_rx_packet *pkt = rxb_addr(rxb);
436 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
437
Arik Nemtsov91b80252014-02-10 12:49:39 +0200438 lockdep_assert_held(&mvm->mutex);
439
Johannes Berg8ca151b2013-01-24 14:25:36 +0100440 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
441 notif->status, notif->scanned_channels);
442
Arik Nemtsov91b80252014-02-10 12:49:39 +0200443 if (mvm->scan_status == IWL_MVM_SCAN_OS)
444 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100445 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
446
Arik Nemtsov519e2022013-10-17 17:51:35 +0300447 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
448
Johannes Berg8ca151b2013-01-24 14:25:36 +0100449 return 0;
450}
451
David Spinadel35a000b2013-08-28 09:29:43 +0300452int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
453 struct iwl_rx_cmd_buffer *rxb,
454 struct iwl_device_cmd *cmd)
455{
456 struct iwl_rx_packet *pkt = rxb_addr(rxb);
457 struct iwl_sched_scan_results *notif = (void *)pkt->data;
458
459 if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
460 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
461 ieee80211_sched_scan_results(mvm->hw);
462 }
463
464 return 0;
465}
466
Johannes Berg8ca151b2013-01-24 14:25:36 +0100467static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
468 struct iwl_rx_packet *pkt, void *data)
469{
470 struct iwl_mvm *mvm =
471 container_of(notif_wait, struct iwl_mvm, notif_wait);
472 struct iwl_scan_complete_notif *notif;
473 u32 *resp;
474
475 switch (pkt->hdr.cmd) {
476 case SCAN_ABORT_CMD:
477 resp = (void *)pkt->data;
478 if (*resp == CAN_ABORT_STATUS) {
479 IWL_DEBUG_SCAN(mvm,
480 "Scan can be aborted, wait until completion\n");
481 return false;
482 }
483
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300484 /*
485 * If scan cannot be aborted, it means that we had a
486 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
487 * ieee80211_scan_completed already.
488 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100489 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
490 *resp);
491 return true;
492
493 case SCAN_COMPLETE_NOTIFICATION:
494 notif = (void *)pkt->data;
495 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
496 notif->status);
497 return true;
498
499 default:
500 WARN_ON(1);
501 return false;
502 };
503}
504
Arik Nemtsov91b80252014-02-10 12:49:39 +0200505int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100506{
507 struct iwl_notification_wait wait_scan_abort;
508 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
509 SCAN_COMPLETE_NOTIFICATION };
510 int ret;
511
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300512 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
Arik Nemtsov91b80252014-02-10 12:49:39 +0200513 return 0;
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300514
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200515 if (iwl_mvm_is_radio_killed(mvm)) {
516 ieee80211_scan_completed(mvm->hw, true);
Arik Nemtsov519e2022013-10-17 17:51:35 +0300517 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200518 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov91b80252014-02-10 12:49:39 +0200519 return 0;
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200520 }
521
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
523 scan_abort_notif,
524 ARRAY_SIZE(scan_abort_notif),
525 iwl_mvm_scan_abort_notif, NULL);
526
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300527 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100528 if (ret) {
529 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200530 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100531 goto out_remove_notif;
532 }
533
Arik Nemtsov91b80252014-02-10 12:49:39 +0200534 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100535
536out_remove_notif:
537 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200538 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539}
David Spinadel35a000b2013-08-28 09:29:43 +0300540
541int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
542 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_offload_complete *scan_notif = (void *)pkt->data;
547
Johannes Berga6623e82014-01-27 15:40:53 +0100548 /* scan status must be locked for proper checking */
549 lockdep_assert_held(&mvm->mutex);
550
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300551 IWL_DEBUG_SCAN(mvm,
552 "Scheduled scan completed, status %s EBS status %s:%d\n",
David Spinadel35a000b2013-08-28 09:29:43 +0300553 scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300554 "completed" : "aborted", scan_notif->ebs_status ==
555 IWL_SCAN_EBS_SUCCESS ? "success" : "failed",
556 scan_notif->ebs_status);
557
David Spinadel35a000b2013-08-28 09:29:43 +0300558
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200559 /* only call mac80211 completion if the stop was initiated by FW */
560 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100561 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200562 ieee80211_sched_scan_stopped(mvm->hw);
563 }
David Spinadel35a000b2013-08-28 09:29:43 +0300564
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300565 mvm->last_ebs_successful = !scan_notif->ebs_status;
566
David Spinadel35a000b2013-08-28 09:29:43 +0300567 return 0;
568}
569
570static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
571 struct ieee80211_vif *vif,
David Spinadel633e2712014-02-06 16:15:23 +0200572 struct ieee80211_scan_ies *ies,
David Spinadel35a000b2013-08-28 09:29:43 +0300573 enum ieee80211_band band,
574 struct iwl_tx_cmd *cmd,
575 u8 *data)
576{
577 u16 cmd_len;
578
579 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
580 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
581 cmd->sta_id = mvm->aux_sta.sta_id;
582
583 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
584
585 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
586 vif->addr,
587 1, NULL, 0,
David Spinadel633e2712014-02-06 16:15:23 +0200588 ies->ies[band], ies->len[band],
589 ies->common_ies, ies->common_ie_len,
David Spinadel35a000b2013-08-28 09:29:43 +0300590 SCAN_OFFLOAD_PROBE_REQ_SIZE);
591 cmd->len = cpu_to_le16(cmd_len);
592}
593
594static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
595 struct ieee80211_vif *vif,
596 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200597 struct iwl_scan_offload_cmd *scan,
598 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300599{
600 scan->channel_count =
601 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
602 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
603 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
604 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
605 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
606 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200607
Alexander Bondar50df8a32014-03-12 20:30:51 +0200608 scan->max_out_time = cpu_to_le32(params->max_out_time);
609 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200610
David Spinadel35a000b2013-08-28 09:29:43 +0300611 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
612 MAC_FILTER_IN_BEACON);
613 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
614 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200615
616 if (params->passive_fragmented)
617 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300618}
619
620static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
621{
622 int i;
623
624 for (i = 0; i < PROBE_OPTION_MAX; i++) {
625 if (!ssid_list[i].len)
626 break;
627 if (ssid_list[i].len == ssid_len &&
628 !memcmp(ssid_list->ssid, ssid, ssid_len))
629 return i;
630 }
631 return -1;
632}
633
634static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
635 struct iwl_scan_offload_cmd *scan,
636 u32 *ssid_bitmap)
637{
638 int i, j;
639 int index;
640
641 /*
642 * copy SSIDs from match list.
643 * iwl_config_sched_scan_profiles() uses the order of these ssids to
644 * config match list.
645 */
646 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100647 /* skip empty SSID matchsets */
648 if (!req->match_sets[i].ssid.ssid_len)
649 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300650 scan->direct_scan[i].id = WLAN_EID_SSID;
651 scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
652 memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
653 scan->direct_scan[i].len);
654 }
655
656 /* add SSIDs from scan SSID list */
657 *ssid_bitmap = 0;
658 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
659 index = iwl_ssid_exist(req->ssids[j].ssid,
660 req->ssids[j].ssid_len,
661 scan->direct_scan);
662 if (index < 0) {
663 if (!req->ssids[j].ssid_len)
664 continue;
665 scan->direct_scan[i].id = WLAN_EID_SSID;
666 scan->direct_scan[i].len = req->ssids[j].ssid_len;
667 memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
668 scan->direct_scan[i].len);
669 *ssid_bitmap |= BIT(i + 1);
670 i++;
671 } else {
672 *ssid_bitmap |= BIT(index + 1);
673 }
674 }
675}
676
677static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
678 struct cfg80211_sched_scan_request *req,
679 struct iwl_scan_channel_cfg *channels,
680 enum ieee80211_band band,
681 int *head, int *tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200682 u32 ssid_bitmap,
683 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300684{
685 struct ieee80211_supported_band *s_band;
David Spinadel35a000b2013-08-28 09:29:43 +0300686 int n_channels = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300687 int i, j, index = 0;
688 bool partial;
689
690 /*
691 * We have to configure all supported channels, even if we don't want to
692 * scan on them, but we have to send channels in the order that we want
693 * to scan. So add requested channels to head of the list and others to
694 * the end.
695 */
David Spinadel35a000b2013-08-28 09:29:43 +0300696 s_band = &mvm->nvm_data->bands[band];
697
698 for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
699 partial = false;
700 for (j = 0; j < n_channels; j++)
701 if (s_band->channels[i].center_freq ==
702 req->channels[j]->center_freq) {
703 index = *head;
704 (*head)++;
705 /*
706 * Channels that came with the request will be
707 * in partial scan .
708 */
709 partial = true;
710 break;
711 }
712 if (!partial) {
713 index = *tail;
714 (*tail)--;
715 }
716 channels->channel_number[index] =
717 cpu_to_le16(ieee80211_frequency_to_channel(
718 s_band->channels[i].center_freq));
Alexander Bondar50df8a32014-03-12 20:30:51 +0200719 channels->dwell_time[index][0] = params->dwell[band].active;
720 channels->dwell_time[index][1] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300721
722 channels->iter_count[index] = cpu_to_le16(1);
723 channels->iter_interval[index] = 0;
724
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200725 if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
David Spinadel35a000b2013-08-28 09:29:43 +0300726 channels->type[index] |=
727 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
728
729 channels->type[index] |=
730 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
731 if (partial)
732 channels->type[index] |=
733 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
734
735 if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
736 channels->type[index] |=
737 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
738
739 /* scan for all SSIDs from req->ssids */
740 channels->type[index] |= cpu_to_le32(ssid_bitmap);
741 }
742}
743
744int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
745 struct ieee80211_vif *vif,
746 struct cfg80211_sched_scan_request *req,
David Spinadel633e2712014-02-06 16:15:23 +0200747 struct ieee80211_scan_ies *ies)
David Spinadel35a000b2013-08-28 09:29:43 +0300748{
David Spinadel35a000b2013-08-28 09:29:43 +0300749 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
750 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
751 int head = 0;
Eliad Peller64793112014-05-11 19:59:43 +0300752 int tail = band_2ghz + band_5ghz - 1;
David Spinadel35a000b2013-08-28 09:29:43 +0300753 u32 ssid_bitmap;
754 int cmd_len;
755 int ret;
756
757 struct iwl_scan_offload_cfg *scan_cfg;
758 struct iwl_host_cmd cmd = {
759 .id = SCAN_OFFLOAD_CONFIG_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300760 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200761 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300762
763 lockdep_assert_held(&mvm->mutex);
764
David Spinadel35a000b2013-08-28 09:29:43 +0300765 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200766 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300767
768 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
769 if (!scan_cfg)
770 return -ENOMEM;
771
Alexander Bondar50df8a32014-03-12 20:30:51 +0200772 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
773 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300774 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
775
776 iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
777 /* build tx frames for supported bands */
778 if (band_2ghz) {
779 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
780 IEEE80211_BAND_2GHZ,
781 &scan_cfg->scan_cmd.tx_cmd[0],
782 scan_cfg->data);
783 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
784 IEEE80211_BAND_2GHZ, &head, &tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200785 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300786 }
787 if (band_5ghz) {
788 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
789 IEEE80211_BAND_5GHZ,
790 &scan_cfg->scan_cmd.tx_cmd[1],
791 scan_cfg->data +
792 SCAN_OFFLOAD_PROBE_REQ_SIZE);
793 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
794 IEEE80211_BAND_5GHZ, &head, &tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200795 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300796 }
797
798 cmd.data[0] = scan_cfg;
799 cmd.len[0] = cmd_len;
800 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
801
802 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
803
804 ret = iwl_mvm_send_cmd(mvm, &cmd);
805 kfree(scan_cfg);
806 return ret;
807}
808
809int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
810 struct cfg80211_sched_scan_request *req)
811{
812 struct iwl_scan_offload_profile *profile;
813 struct iwl_scan_offload_profile_cfg *profile_cfg;
814 struct iwl_scan_offload_blacklist *blacklist;
815 struct iwl_host_cmd cmd = {
816 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300817 .len[1] = sizeof(*profile_cfg),
818 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
819 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
820 };
821 int blacklist_len;
822 int i;
823 int ret;
824
825 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
826 return -EIO;
827
828 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
829 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
830 else
831 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
832
833 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
834 if (!blacklist)
835 return -ENOMEM;
836
837 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
838 if (!profile_cfg) {
839 ret = -ENOMEM;
840 goto free_blacklist;
841 }
842
843 cmd.data[0] = blacklist;
844 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
845 cmd.data[1] = profile_cfg;
846
847 /* No blacklist configuration */
848
849 profile_cfg->num_profiles = req->n_match_sets;
850 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
851 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
852 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200853 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
854 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300855
856 for (i = 0; i < req->n_match_sets; i++) {
857 profile = &profile_cfg->profiles[i];
858 profile->ssid_index = i;
859 /* Support any cipher and auth algorithm */
860 profile->unicast_cipher = 0xff;
861 profile->auth_alg = 0xff;
862 profile->network_type = IWL_NETWORK_TYPE_ANY;
863 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
864 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
865 }
866
867 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
868
869 ret = iwl_mvm_send_cmd(mvm, &cmd);
870 kfree(profile_cfg);
871free_blacklist:
872 kfree(blacklist);
873
874 return ret;
875}
876
877int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
878 struct cfg80211_sched_scan_request *req)
879{
880 struct iwl_scan_offload_req scan_req = {
881 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
882
883 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
884 .schedule_line[0].delay = req->interval / 1000,
885 .schedule_line[0].full_scan_mul = 1,
886
887 .schedule_line[1].iterations = 0xff,
888 .schedule_line[1].delay = req->interval / 1000,
889 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
890 };
891
892 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
893 IWL_DEBUG_SCAN(mvm,
894 "Sending scheduled scan with filtering, filter len %d\n",
895 req->n_match_sets);
David Spinadel35a000b2013-08-28 09:29:43 +0300896 } else {
897 IWL_DEBUG_SCAN(mvm,
898 "Sending Scheduled scan without filtering\n");
Eliad Pellerde33fb52013-11-10 12:59:46 +0200899 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +0300900 }
901
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300902 if (mvm->last_ebs_successful &&
903 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
904 scan_req.flags |=
905 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
906
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300907 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
David Spinadel35a000b2013-08-28 09:29:43 +0300908 sizeof(scan_req), &scan_req);
909}
910
911static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
912{
913 int ret;
914 struct iwl_host_cmd cmd = {
915 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300916 };
917 u32 status;
918
919 /* Exit instantly with error when device is not ready
920 * to receive scan abort command or it does not perform
921 * scheduled scan currently */
922 if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
923 return -EIO;
924
925 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
926 if (ret)
927 return ret;
928
929 if (status != CAN_ABORT_STATUS) {
930 /*
931 * The scan abort will return 1 for success or
932 * 2 for "failure". A failure condition can be
933 * due to simply not being in an active scan which
934 * can occur if we send the scan abort before the
935 * microcode has notified us that a scan is completed.
936 */
937 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200938 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +0300939 }
940
941 return ret;
942}
943
David Spinadel636a2cd2014-05-01 15:57:22 +0300944int iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +0300945{
946 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200947 struct iwl_notification_wait wait_scan_done;
948 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadel35a000b2013-08-28 09:29:43 +0300949
950 lockdep_assert_held(&mvm->mutex);
951
952 if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
953 IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200954 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300955 }
956
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200957 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
958 scan_done_notif,
959 ARRAY_SIZE(scan_done_notif),
960 NULL, NULL);
961
David Spinadel35a000b2013-08-28 09:29:43 +0300962 ret = iwl_mvm_send_sched_scan_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200963 if (ret) {
David Spinadel35a000b2013-08-28 09:29:43 +0300964 IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200965 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
966 return ret;
967 }
968
969 IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
970
971 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
972 if (ret)
973 return ret;
974
975 /*
976 * Clear the scan status so the next scan requests will succeed. This
977 * also ensures the Rx handler doesn't do anything, as the scan was
978 * stopped from above.
979 */
980 mvm->scan_status = IWL_MVM_SCAN_NONE;
981
David Spinadel636a2cd2014-05-01 15:57:22 +0300982 if (notify)
983 ieee80211_sched_scan_stopped(mvm->hw);
984
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200985 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300986}