blob: 9e760d0c5985c820550db3ad3a7deabe53dc8de7 [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,
207 const u8 *ie, int ie_len,
208 int left)
209{
210 int len = 0;
211 u8 *pos = NULL;
212
213 /* Make sure there is enough space for the probe request,
214 * two mandatory IEs and the data */
215 left -= 24;
216 if (left < 0)
217 return 0;
218
219 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
220 eth_broadcast_addr(frame->da);
221 memcpy(frame->sa, ta, ETH_ALEN);
222 eth_broadcast_addr(frame->bssid);
223 frame->seq_ctrl = 0;
224
225 len += 24;
226
227 /* for passive scans, no need to fill anything */
228 if (n_ssids == 0)
229 return (u16)len;
230
231 /* points to the payload of the request */
232 pos = &frame->u.probe_req.variable[0];
233
234 /* fill in our SSID IE */
235 left -= ssid_len + 2;
236 if (left < 0)
237 return 0;
238 *pos++ = WLAN_EID_SSID;
239 *pos++ = ssid_len;
240 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
241 memcpy(pos, ssid, ssid_len);
242 pos += ssid_len;
243 }
244
245 len += ssid_len + 2;
246
247 if (WARN_ON(left < ie_len))
248 return len;
249
250 if (ie && ie_len) {
251 memcpy(pos, ie, ie_len);
252 len += ie_len;
253 }
254
255 return (u16)len;
256}
257
Alexander Bondar8a110d92014-03-12 17:31:19 +0200258static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
259 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200260{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200261 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
262 bool *global_bound = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200263
Alexander Bondar8a110d92014-03-12 17:31:19 +0200264 if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
265 *global_bound = true;
266}
267
268static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200269 struct ieee80211_vif *vif,
270 int n_ssids,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200271 struct iwl_mvm_scan_params *params)
272{
273 bool global_bound = false;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200274 enum ieee80211_band band;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200275
276 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
277 IEEE80211_IFACE_ITER_NORMAL,
278 iwl_mvm_scan_condition_iterator,
279 &global_bound);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200280 /*
281 * Under low latency traffic passive scan is fragmented meaning
282 * that dwell on a particular channel will be fragmented. Each fragment
283 * dwell time is 20ms and fragments period is 105ms. Skipping to next
284 * channel will be delayed by the same period - 105ms. So suspend_time
285 * parameter describing both fragments and channels skipping periods is
286 * set to 105ms. This value is chosen so that overall passive scan
287 * duration will not be too long. Max_out_time in this case is set to
288 * 70ms, so for active scanning operating channel will be left for 70ms
289 * while for passive still for 20ms (fragment dwell).
290 */
291 if (global_bound) {
292 if (!iwl_mvm_low_latency(mvm)) {
293 params->suspend_time = ieee80211_tu_to_usec(100);
294 params->max_out_time = ieee80211_tu_to_usec(600);
295 } else {
296 params->suspend_time = ieee80211_tu_to_usec(105);
297 /* P2P doesn't support fragmented passive scan, so
298 * configure max_out_time to be at least longest dwell
299 * time for passive scan.
300 */
301 if (vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
302 params->max_out_time = ieee80211_tu_to_usec(70);
303 params->passive_fragmented = true;
304 } else {
305 u32 passive_dwell;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200306
Alexander Bondar50df8a32014-03-12 20:30:51 +0200307 /*
308 * Use band G so that passive channel dwell time
309 * will be assigned with maximum value.
310 */
311 band = IEEE80211_BAND_2GHZ;
312 passive_dwell = iwl_mvm_get_passive_dwell(band);
313 params->max_out_time =
314 ieee80211_tu_to_usec(passive_dwell);
315 }
316 }
317 }
318
319 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
320 if (params->passive_fragmented)
321 params->dwell[band].passive = 20;
322 else
323 params->dwell[band].passive =
324 iwl_mvm_get_passive_dwell(band);
325 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
326 n_ssids);
327 }
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200328}
329
Johannes Berg8ca151b2013-01-24 14:25:36 +0100330int iwl_mvm_scan_request(struct iwl_mvm *mvm,
331 struct ieee80211_vif *vif,
332 struct cfg80211_scan_request *req)
333{
334 struct iwl_host_cmd hcmd = {
335 .id = SCAN_REQUEST_CMD,
336 .len = { 0, },
337 .data = { mvm->scan_cmd, },
338 .flags = CMD_SYNC,
339 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
340 };
341 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
342 int ret;
343 u32 status;
344 int ssid_len = 0;
345 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300346 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
347 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200348 struct iwl_mvm_scan_params params = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100349
350 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach748fa67c2014-03-27 10:06:29 +0200351
352 /* we should have failed registration if scan_cmd was NULL */
353 if (WARN_ON(mvm->scan_cmd == NULL))
354 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100355
356 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
357 mvm->scan_status = IWL_MVM_SCAN_OS;
358 memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
359 mvm->fw->ucode_capa.max_probe_length +
360 (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
Alexander Bondar8a110d92014-03-12 17:31:19 +0200361
Johannes Berg8ca151b2013-01-24 14:25:36 +0100362 cmd->channel_count = (u8)req->n_channels;
363 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
364 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
365 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200366
Alexander Bondar50df8a32014-03-12 20:30:51 +0200367 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200368 cmd->max_out_time = cpu_to_le32(params.max_out_time);
369 cmd->suspend_time = cpu_to_le32(params.suspend_time);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200370 if (params.passive_fragmented)
371 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200372
Johannes Berg8ca151b2013-01-24 14:25:36 +0100373 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
374 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
375 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200376
377 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
378 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
379 else
380 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
381
Johannes Berg8ca151b2013-01-24 14:25:36 +0100382 cmd->repeats = cpu_to_le32(1);
383
384 /*
385 * If the user asked for passive scan, don't change to active scan if
386 * you see any activity on the channel - remain passive.
387 */
388 if (req->n_ssids > 0) {
389 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300390 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300391 if (basic_ssid) {
392 ssid = req->ssids[0].ssid;
393 ssid_len = req->ssids[0].ssid_len;
394 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100395 } else {
396 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300397 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100398 }
399
David Spinadel20f1a5d2013-08-21 09:14:27 +0300400 iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100401
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200402 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
403 TX_CMD_FLG_BT_DIS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100404 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
405 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
406 cmd->tx_cmd.rate_n_flags =
407 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
408 req->no_cck);
409
410 cmd->tx_cmd.len =
411 cpu_to_le16(iwl_mvm_fill_probe_req(
412 (struct ieee80211_mgmt *)cmd->data,
413 vif->addr,
414 req->n_ssids, ssid, ssid_len,
415 req->ie, req->ie_len,
416 mvm->fw->ucode_capa.max_probe_length));
417
Alexander Bondar50df8a32014-03-12 20:30:51 +0200418 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100419
420 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
421 le16_to_cpu(cmd->tx_cmd.len) +
422 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
423 hcmd.len[0] = le16_to_cpu(cmd->len);
424
425 status = SCAN_RESPONSE_OK;
426 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
427 if (!ret && status == SCAN_RESPONSE_OK) {
428 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
429 } else {
430 /*
431 * If the scan failed, it usually means that the FW was unable
432 * to allocate the time events. Warn on it, but maybe we
433 * should try to send the command again with different params.
434 */
435 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
436 status, ret);
437 mvm->scan_status = IWL_MVM_SCAN_NONE;
438 ret = -EIO;
439 }
440 return ret;
441}
442
443int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
444 struct iwl_device_cmd *cmd)
445{
446 struct iwl_rx_packet *pkt = rxb_addr(rxb);
447 struct iwl_cmd_response *resp = (void *)pkt->data;
448
449 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
450 le32_to_cpu(resp->status));
451 return 0;
452}
453
454int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
455 struct iwl_device_cmd *cmd)
456{
457 struct iwl_rx_packet *pkt = rxb_addr(rxb);
458 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
459
Arik Nemtsov91b80252014-02-10 12:49:39 +0200460 lockdep_assert_held(&mvm->mutex);
461
Johannes Berg8ca151b2013-01-24 14:25:36 +0100462 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
463 notif->status, notif->scanned_channels);
464
Arik Nemtsov91b80252014-02-10 12:49:39 +0200465 if (mvm->scan_status == IWL_MVM_SCAN_OS)
466 mvm->scan_status = IWL_MVM_SCAN_NONE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100467 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
468
Arik Nemtsov519e2022013-10-17 17:51:35 +0300469 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
470
Johannes Berg8ca151b2013-01-24 14:25:36 +0100471 return 0;
472}
473
David Spinadel35a000b2013-08-28 09:29:43 +0300474int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
475 struct iwl_rx_cmd_buffer *rxb,
476 struct iwl_device_cmd *cmd)
477{
478 struct iwl_rx_packet *pkt = rxb_addr(rxb);
479 struct iwl_sched_scan_results *notif = (void *)pkt->data;
480
481 if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
482 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
483 ieee80211_sched_scan_results(mvm->hw);
484 }
485
486 return 0;
487}
488
Johannes Berg8ca151b2013-01-24 14:25:36 +0100489static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
490 struct iwl_rx_packet *pkt, void *data)
491{
492 struct iwl_mvm *mvm =
493 container_of(notif_wait, struct iwl_mvm, notif_wait);
494 struct iwl_scan_complete_notif *notif;
495 u32 *resp;
496
497 switch (pkt->hdr.cmd) {
498 case SCAN_ABORT_CMD:
499 resp = (void *)pkt->data;
500 if (*resp == CAN_ABORT_STATUS) {
501 IWL_DEBUG_SCAN(mvm,
502 "Scan can be aborted, wait until completion\n");
503 return false;
504 }
505
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300506 /*
507 * If scan cannot be aborted, it means that we had a
508 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
509 * ieee80211_scan_completed already.
510 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100511 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
512 *resp);
513 return true;
514
515 case SCAN_COMPLETE_NOTIFICATION:
516 notif = (void *)pkt->data;
517 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
518 notif->status);
519 return true;
520
521 default:
522 WARN_ON(1);
523 return false;
524 };
525}
526
Arik Nemtsov91b80252014-02-10 12:49:39 +0200527int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100528{
529 struct iwl_notification_wait wait_scan_abort;
530 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
531 SCAN_COMPLETE_NOTIFICATION };
532 int ret;
533
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300534 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
Arik Nemtsov91b80252014-02-10 12:49:39 +0200535 return 0;
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300536
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200537 if (iwl_mvm_is_radio_killed(mvm)) {
538 ieee80211_scan_completed(mvm->hw, true);
Arik Nemtsov519e2022013-10-17 17:51:35 +0300539 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200540 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov91b80252014-02-10 12:49:39 +0200541 return 0;
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200542 }
543
Johannes Berg8ca151b2013-01-24 14:25:36 +0100544 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
545 scan_abort_notif,
546 ARRAY_SIZE(scan_abort_notif),
547 iwl_mvm_scan_abort_notif, NULL);
548
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200549 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 if (ret) {
551 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200552 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100553 goto out_remove_notif;
554 }
555
Arik Nemtsov91b80252014-02-10 12:49:39 +0200556 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100557
558out_remove_notif:
559 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
Arik Nemtsov91b80252014-02-10 12:49:39 +0200560 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100561}
David Spinadel35a000b2013-08-28 09:29:43 +0300562
563int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
564 struct iwl_rx_cmd_buffer *rxb,
565 struct iwl_device_cmd *cmd)
566{
567 struct iwl_rx_packet *pkt = rxb_addr(rxb);
568 struct iwl_scan_offload_complete *scan_notif = (void *)pkt->data;
569
Johannes Berga6623e82014-01-27 15:40:53 +0100570 /* scan status must be locked for proper checking */
571 lockdep_assert_held(&mvm->mutex);
572
David Spinadel35a000b2013-08-28 09:29:43 +0300573 IWL_DEBUG_SCAN(mvm, "Scheduled scan completed, status %s\n",
574 scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
575 "completed" : "aborted");
576
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200577 /* only call mac80211 completion if the stop was initiated by FW */
578 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
Johannes Berga6623e82014-01-27 15:40:53 +0100579 mvm->scan_status = IWL_MVM_SCAN_NONE;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200580 ieee80211_sched_scan_stopped(mvm->hw);
581 }
David Spinadel35a000b2013-08-28 09:29:43 +0300582
583 return 0;
584}
585
586static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
587 struct ieee80211_vif *vif,
588 struct ieee80211_sched_scan_ies *ies,
589 enum ieee80211_band band,
590 struct iwl_tx_cmd *cmd,
591 u8 *data)
592{
593 u16 cmd_len;
594
595 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
596 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
597 cmd->sta_id = mvm->aux_sta.sta_id;
598
599 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
600
601 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
602 vif->addr,
603 1, NULL, 0,
604 ies->ie[band], ies->len[band],
605 SCAN_OFFLOAD_PROBE_REQ_SIZE);
606 cmd->len = cpu_to_le16(cmd_len);
607}
608
609static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
610 struct ieee80211_vif *vif,
611 struct cfg80211_sched_scan_request *req,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200612 struct iwl_scan_offload_cmd *scan,
613 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300614{
615 scan->channel_count =
616 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
617 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
618 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
619 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
620 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
621 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200622
Alexander Bondar50df8a32014-03-12 20:30:51 +0200623 scan->max_out_time = cpu_to_le32(params->max_out_time);
624 scan->suspend_time = cpu_to_le32(params->suspend_time);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200625
David Spinadel35a000b2013-08-28 09:29:43 +0300626 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
627 MAC_FILTER_IN_BEACON);
628 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
629 scan->rep_count = cpu_to_le32(1);
Alexander Bondar50df8a32014-03-12 20:30:51 +0200630
631 if (params->passive_fragmented)
632 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300633}
634
635static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
636{
637 int i;
638
639 for (i = 0; i < PROBE_OPTION_MAX; i++) {
640 if (!ssid_list[i].len)
641 break;
642 if (ssid_list[i].len == ssid_len &&
643 !memcmp(ssid_list->ssid, ssid, ssid_len))
644 return i;
645 }
646 return -1;
647}
648
649static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
650 struct iwl_scan_offload_cmd *scan,
651 u32 *ssid_bitmap)
652{
653 int i, j;
654 int index;
655
656 /*
657 * copy SSIDs from match list.
658 * iwl_config_sched_scan_profiles() uses the order of these ssids to
659 * config match list.
660 */
661 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100662 /* skip empty SSID matchsets */
663 if (!req->match_sets[i].ssid.ssid_len)
664 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300665 scan->direct_scan[i].id = WLAN_EID_SSID;
666 scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
667 memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
668 scan->direct_scan[i].len);
669 }
670
671 /* add SSIDs from scan SSID list */
672 *ssid_bitmap = 0;
673 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
674 index = iwl_ssid_exist(req->ssids[j].ssid,
675 req->ssids[j].ssid_len,
676 scan->direct_scan);
677 if (index < 0) {
678 if (!req->ssids[j].ssid_len)
679 continue;
680 scan->direct_scan[i].id = WLAN_EID_SSID;
681 scan->direct_scan[i].len = req->ssids[j].ssid_len;
682 memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
683 scan->direct_scan[i].len);
684 *ssid_bitmap |= BIT(i + 1);
685 i++;
686 } else {
687 *ssid_bitmap |= BIT(index + 1);
688 }
689 }
690}
691
692static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
693 struct cfg80211_sched_scan_request *req,
694 struct iwl_scan_channel_cfg *channels,
695 enum ieee80211_band band,
696 int *head, int *tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200697 u32 ssid_bitmap,
698 struct iwl_mvm_scan_params *params)
David Spinadel35a000b2013-08-28 09:29:43 +0300699{
700 struct ieee80211_supported_band *s_band;
David Spinadel35a000b2013-08-28 09:29:43 +0300701 int n_channels = req->n_channels;
David Spinadel35a000b2013-08-28 09:29:43 +0300702 int i, j, index = 0;
703 bool partial;
704
705 /*
706 * We have to configure all supported channels, even if we don't want to
707 * scan on them, but we have to send channels in the order that we want
708 * to scan. So add requested channels to head of the list and others to
709 * the end.
710 */
David Spinadel35a000b2013-08-28 09:29:43 +0300711 s_band = &mvm->nvm_data->bands[band];
712
713 for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
714 partial = false;
715 for (j = 0; j < n_channels; j++)
716 if (s_band->channels[i].center_freq ==
717 req->channels[j]->center_freq) {
718 index = *head;
719 (*head)++;
720 /*
721 * Channels that came with the request will be
722 * in partial scan .
723 */
724 partial = true;
725 break;
726 }
727 if (!partial) {
728 index = *tail;
729 (*tail)--;
730 }
731 channels->channel_number[index] =
732 cpu_to_le16(ieee80211_frequency_to_channel(
733 s_band->channels[i].center_freq));
Alexander Bondar50df8a32014-03-12 20:30:51 +0200734 channels->dwell_time[index][0] = params->dwell[band].active;
735 channels->dwell_time[index][1] = params->dwell[band].passive;
David Spinadel35a000b2013-08-28 09:29:43 +0300736
737 channels->iter_count[index] = cpu_to_le16(1);
738 channels->iter_interval[index] = 0;
739
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200740 if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
David Spinadel35a000b2013-08-28 09:29:43 +0300741 channels->type[index] |=
742 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
743
744 channels->type[index] |=
745 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
746 if (partial)
747 channels->type[index] |=
748 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
749
750 if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
751 channels->type[index] |=
752 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
753
754 /* scan for all SSIDs from req->ssids */
755 channels->type[index] |= cpu_to_le32(ssid_bitmap);
756 }
757}
758
759int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
760 struct ieee80211_vif *vif,
761 struct cfg80211_sched_scan_request *req,
762 struct ieee80211_sched_scan_ies *ies)
763{
David Spinadel35a000b2013-08-28 09:29:43 +0300764 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
765 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
766 int head = 0;
767 int tail = band_2ghz + band_5ghz;
768 u32 ssid_bitmap;
769 int cmd_len;
770 int ret;
771
772 struct iwl_scan_offload_cfg *scan_cfg;
773 struct iwl_host_cmd cmd = {
774 .id = SCAN_OFFLOAD_CONFIG_CMD,
775 .flags = CMD_SYNC,
776 };
Alexander Bondar50df8a32014-03-12 20:30:51 +0200777 struct iwl_mvm_scan_params params = {};
David Spinadel35a000b2013-08-28 09:29:43 +0300778
779 lockdep_assert_held(&mvm->mutex);
780
David Spinadel35a000b2013-08-28 09:29:43 +0300781 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
Emmanuel Grumbach66092532014-02-20 14:58:30 +0200782 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
David Spinadel35a000b2013-08-28 09:29:43 +0300783
784 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
785 if (!scan_cfg)
786 return -ENOMEM;
787
Alexander Bondar50df8a32014-03-12 20:30:51 +0200788 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
789 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300790 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
791
792 iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
793 /* build tx frames for supported bands */
794 if (band_2ghz) {
795 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
796 IEEE80211_BAND_2GHZ,
797 &scan_cfg->scan_cmd.tx_cmd[0],
798 scan_cfg->data);
799 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
800 IEEE80211_BAND_2GHZ, &head, &tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200801 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300802 }
803 if (band_5ghz) {
804 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
805 IEEE80211_BAND_5GHZ,
806 &scan_cfg->scan_cmd.tx_cmd[1],
807 scan_cfg->data +
808 SCAN_OFFLOAD_PROBE_REQ_SIZE);
809 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
810 IEEE80211_BAND_5GHZ, &head, &tail,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200811 ssid_bitmap, &params);
David Spinadel35a000b2013-08-28 09:29:43 +0300812 }
813
814 cmd.data[0] = scan_cfg;
815 cmd.len[0] = cmd_len;
816 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
817
818 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
819
820 ret = iwl_mvm_send_cmd(mvm, &cmd);
821 kfree(scan_cfg);
822 return ret;
823}
824
825int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
826 struct cfg80211_sched_scan_request *req)
827{
828 struct iwl_scan_offload_profile *profile;
829 struct iwl_scan_offload_profile_cfg *profile_cfg;
830 struct iwl_scan_offload_blacklist *blacklist;
831 struct iwl_host_cmd cmd = {
832 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
833 .flags = CMD_SYNC,
834 .len[1] = sizeof(*profile_cfg),
835 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
836 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
837 };
838 int blacklist_len;
839 int i;
840 int ret;
841
842 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
843 return -EIO;
844
845 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
846 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
847 else
848 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
849
850 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
851 if (!blacklist)
852 return -ENOMEM;
853
854 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
855 if (!profile_cfg) {
856 ret = -ENOMEM;
857 goto free_blacklist;
858 }
859
860 cmd.data[0] = blacklist;
861 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
862 cmd.data[1] = profile_cfg;
863
864 /* No blacklist configuration */
865
866 profile_cfg->num_profiles = req->n_match_sets;
867 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
868 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
869 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200870 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
871 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300872
873 for (i = 0; i < req->n_match_sets; i++) {
874 profile = &profile_cfg->profiles[i];
875 profile->ssid_index = i;
876 /* Support any cipher and auth algorithm */
877 profile->unicast_cipher = 0xff;
878 profile->auth_alg = 0xff;
879 profile->network_type = IWL_NETWORK_TYPE_ANY;
880 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
881 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
882 }
883
884 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
885
886 ret = iwl_mvm_send_cmd(mvm, &cmd);
887 kfree(profile_cfg);
888free_blacklist:
889 kfree(blacklist);
890
891 return ret;
892}
893
894int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
895 struct cfg80211_sched_scan_request *req)
896{
897 struct iwl_scan_offload_req scan_req = {
898 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
899
900 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
901 .schedule_line[0].delay = req->interval / 1000,
902 .schedule_line[0].full_scan_mul = 1,
903
904 .schedule_line[1].iterations = 0xff,
905 .schedule_line[1].delay = req->interval / 1000,
906 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
907 };
908
909 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
910 IWL_DEBUG_SCAN(mvm,
911 "Sending scheduled scan with filtering, filter len %d\n",
912 req->n_match_sets);
David Spinadel35a000b2013-08-28 09:29:43 +0300913 } else {
914 IWL_DEBUG_SCAN(mvm,
915 "Sending Scheduled scan without filtering\n");
Eliad Pellerde33fb52013-11-10 12:59:46 +0200916 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +0300917 }
918
919 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, CMD_SYNC,
920 sizeof(scan_req), &scan_req);
921}
922
923static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
924{
925 int ret;
926 struct iwl_host_cmd cmd = {
927 .id = SCAN_OFFLOAD_ABORT_CMD,
928 .flags = CMD_SYNC,
929 };
930 u32 status;
931
932 /* Exit instantly with error when device is not ready
933 * to receive scan abort command or it does not perform
934 * scheduled scan currently */
935 if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
936 return -EIO;
937
938 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
939 if (ret)
940 return ret;
941
942 if (status != CAN_ABORT_STATUS) {
943 /*
944 * The scan abort will return 1 for success or
945 * 2 for "failure". A failure condition can be
946 * due to simply not being in an active scan which
947 * can occur if we send the scan abort before the
948 * microcode has notified us that a scan is completed.
949 */
950 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200951 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +0300952 }
953
954 return ret;
955}
956
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200957int iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +0300958{
959 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200960 struct iwl_notification_wait wait_scan_done;
961 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
David Spinadel35a000b2013-08-28 09:29:43 +0300962
963 lockdep_assert_held(&mvm->mutex);
964
965 if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
966 IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200967 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300968 }
969
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200970 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
971 scan_done_notif,
972 ARRAY_SIZE(scan_done_notif),
973 NULL, NULL);
974
David Spinadel35a000b2013-08-28 09:29:43 +0300975 ret = iwl_mvm_send_sched_scan_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200976 if (ret) {
David Spinadel35a000b2013-08-28 09:29:43 +0300977 IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200978 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
979 return ret;
980 }
981
982 IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
983
984 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
985 if (ret)
986 return ret;
987
988 /*
989 * Clear the scan status so the next scan requests will succeed. This
990 * also ensures the Rx handler doesn't do anything, as the scan was
991 * stopped from above.
992 */
993 mvm->scan_status = IWL_MVM_SCAN_NONE;
994
995 return 0;
David Spinadel35a000b2013-08-28 09:29:43 +0300996}