blob: 713efd71efe26b6eb4f545c4a7f306813f77c1dc [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
Haim Dreyfuss61f63252013-11-03 23:02:59 +020073#define LONG_OUT_TIME_PERIOD 600
74#define SHORT_OUT_TIME_PERIOD 200
75#define SUSPEND_TIME_PERIOD 100
Johannes Berg8ca151b2013-01-24 14:25:36 +010076
77static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
78{
79 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +030080 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010081
Oren Givon91b05d12013-08-19 08:36:48 +030082 if (mvm->scan_rx_ant != ANT_NONE)
83 rx_ant = mvm->scan_rx_ant;
84 else
Johannes Berg4ed735e2014-02-12 21:47:44 +010085 rx_ant = mvm->fw->valid_rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +010086 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
87 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
88 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
89 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
90 return cpu_to_le16(rx_chain);
91}
92
Haim Dreyfuss61f63252013-11-03 23:02:59 +020093static inline __le32 iwl_mvm_scan_max_out_time(struct ieee80211_vif *vif,
94 u32 flags, bool is_assoc)
Johannes Berg8ca151b2013-01-24 14:25:36 +010095{
Haim Dreyfuss61f63252013-11-03 23:02:59 +020096 if (!is_assoc)
Johannes Berg8ca151b2013-01-24 14:25:36 +010097 return 0;
Haim Dreyfuss61f63252013-11-03 23:02:59 +020098 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
99 return cpu_to_le32(ieee80211_tu_to_usec(SHORT_OUT_TIME_PERIOD));
100 return cpu_to_le32(ieee80211_tu_to_usec(LONG_OUT_TIME_PERIOD));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101}
102
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200103static inline __le32 iwl_mvm_scan_suspend_time(struct ieee80211_vif *vif,
104 bool is_assoc)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105{
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200106 if (!is_assoc)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107 return 0;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200108 return cpu_to_le32(ieee80211_tu_to_usec(SUSPEND_TIME_PERIOD));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109}
110
111static inline __le32
112iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
113{
114 if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
115 return cpu_to_le32(PHY_BAND_24);
116 else
117 return cpu_to_le32(PHY_BAND_5);
118}
119
120static inline __le32
121iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
122 bool no_cck)
123{
124 u32 tx_ant;
125
126 mvm->scan_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100127 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100128 mvm->scan_last_antenna_idx);
129 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
130
131 if (band == IEEE80211_BAND_2GHZ && !no_cck)
132 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
133 tx_ant);
134 else
135 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
136}
137
138/*
139 * We insert the SSIDs in an inverted order, because the FW will
140 * invert it back. The most prioritized SSID, which is first in the
141 * request list, is not copied here, but inserted directly to the probe
142 * request.
143 */
144static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300145 struct cfg80211_scan_request *req,
146 int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100147{
148 int fw_idx, req_idx;
149
David Spinadel20f1a5d2013-08-21 09:14:27 +0300150 for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300151 req_idx--, fw_idx++) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100152 cmd->direct_scan[fw_idx].id = WLAN_EID_SSID;
153 cmd->direct_scan[fw_idx].len = req->ssids[req_idx].ssid_len;
154 memcpy(cmd->direct_scan[fw_idx].ssid,
155 req->ssids[req_idx].ssid,
156 req->ssids[req_idx].ssid_len);
157 }
158}
159
160/*
161 * If req->n_ssids > 0, it means we should do an active scan.
162 * In case of active scan w/o directed scan, we receive a zero-length SSID
163 * just to notify that this scan is active and not passive.
164 * In order to notify the FW of the number of SSIDs we wish to scan (including
165 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300166 * one for each SSID, and set the active bit (first). If the first SSID is
167 * already included in the probe template, so we need to set only
168 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100169 */
170static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
171{
172 if (band == IEEE80211_BAND_2GHZ)
173 return 30 + 3 * (n_ssids + 1);
174 return 20 + 2 * (n_ssids + 1);
175}
176
177static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
178{
179 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
180}
181
182static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300183 struct cfg80211_scan_request *req,
184 bool basic_ssid)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185{
186 u16 passive_dwell = iwl_mvm_get_passive_dwell(req->channels[0]->band);
187 u16 active_dwell = iwl_mvm_get_active_dwell(req->channels[0]->band,
188 req->n_ssids);
189 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
190 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
191 int i;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300192 int type = BIT(req->n_ssids) - 1;
193
194 if (!basic_ssid)
195 type |= BIT(req->n_ssids);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100196
197 for (i = 0; i < cmd->channel_count; i++) {
198 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
David Spinadel20f1a5d2013-08-21 09:14:27 +0300199 chan->type = cpu_to_le32(type);
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200200 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
David Spinadelbb963c42013-07-23 14:13:32 +0300201 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100202 chan->active_dwell = cpu_to_le16(active_dwell);
203 chan->passive_dwell = cpu_to_le16(passive_dwell);
204 chan->iteration_count = cpu_to_le16(1);
205 chan++;
206 }
207}
208
209/*
210 * Fill in probe request with the following parameters:
211 * TA is our vif HW address, which mac80211 ensures we have.
212 * Packet is broadcasted, so this is both SA and DA.
213 * The probe request IE is made out of two: first comes the most prioritized
214 * SSID if a directed scan is requested. Second comes whatever extra
215 * information was given to us as the scan request IE.
216 */
217static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
218 int n_ssids, const u8 *ssid, int ssid_len,
219 const u8 *ie, int ie_len,
220 int left)
221{
222 int len = 0;
223 u8 *pos = NULL;
224
225 /* Make sure there is enough space for the probe request,
226 * two mandatory IEs and the data */
227 left -= 24;
228 if (left < 0)
229 return 0;
230
231 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
232 eth_broadcast_addr(frame->da);
233 memcpy(frame->sa, ta, ETH_ALEN);
234 eth_broadcast_addr(frame->bssid);
235 frame->seq_ctrl = 0;
236
237 len += 24;
238
239 /* for passive scans, no need to fill anything */
240 if (n_ssids == 0)
241 return (u16)len;
242
243 /* points to the payload of the request */
244 pos = &frame->u.probe_req.variable[0];
245
246 /* fill in our SSID IE */
247 left -= ssid_len + 2;
248 if (left < 0)
249 return 0;
250 *pos++ = WLAN_EID_SSID;
251 *pos++ = ssid_len;
252 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
253 memcpy(pos, ssid, ssid_len);
254 pos += ssid_len;
255 }
256
257 len += ssid_len + 2;
258
259 if (WARN_ON(left < ie_len))
260 return len;
261
262 if (ie && ie_len) {
263 memcpy(pos, ie, ie_len);
264 len += ie_len;
265 }
266
267 return (u16)len;
268}
269
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200270static void iwl_mvm_vif_assoc_iterator(void *data, u8 *mac,
271 struct ieee80211_vif *vif)
272{
273 bool *is_assoc = data;
274
275 if (vif->bss_conf.assoc)
276 *is_assoc = true;
277}
278
Johannes Berg8ca151b2013-01-24 14:25:36 +0100279int iwl_mvm_scan_request(struct iwl_mvm *mvm,
280 struct ieee80211_vif *vif,
281 struct cfg80211_scan_request *req)
282{
283 struct iwl_host_cmd hcmd = {
284 .id = SCAN_REQUEST_CMD,
285 .len = { 0, },
286 .data = { mvm->scan_cmd, },
287 .flags = CMD_SYNC,
288 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
289 };
290 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200291 bool is_assoc = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100292 int ret;
293 u32 status;
294 int ssid_len = 0;
295 u8 *ssid = NULL;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300296 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
297 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100298
299 lockdep_assert_held(&mvm->mutex);
300 BUG_ON(mvm->scan_cmd == NULL);
301
302 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
303 mvm->scan_status = IWL_MVM_SCAN_OS;
304 memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
305 mvm->fw->ucode_capa.max_probe_length +
306 (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200307 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
308 IEEE80211_IFACE_ITER_NORMAL,
309 iwl_mvm_vif_assoc_iterator,
310 &is_assoc);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100311 cmd->channel_count = (u8)req->n_channels;
312 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
313 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
314 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200315 cmd->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
316 is_assoc);
317 cmd->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100318 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
319 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
320 MAC_FILTER_IN_BEACON);
Ilan Peerd91b06d2013-02-11 08:50:45 +0200321
322 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
323 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
324 else
325 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
326
Johannes Berg8ca151b2013-01-24 14:25:36 +0100327 cmd->repeats = cpu_to_le32(1);
328
329 /*
330 * If the user asked for passive scan, don't change to active scan if
331 * you see any activity on the channel - remain passive.
332 */
333 if (req->n_ssids > 0) {
334 cmd->passive2active = cpu_to_le16(1);
David Spinadel26e05cc2013-07-08 13:12:29 +0300335 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
David Spinadel20f1a5d2013-08-21 09:14:27 +0300336 if (basic_ssid) {
337 ssid = req->ssids[0].ssid;
338 ssid_len = req->ssids[0].ssid_len;
339 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100340 } else {
341 cmd->passive2active = 0;
David Spinadel26e05cc2013-07-08 13:12:29 +0300342 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100343 }
344
David Spinadel20f1a5d2013-08-21 09:14:27 +0300345 iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100346
Emmanuel Grumbach8e2a8662014-01-28 12:27:31 +0200347 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
348 TX_CMD_FLG_BT_DIS);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100349 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
350 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
351 cmd->tx_cmd.rate_n_flags =
352 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
353 req->no_cck);
354
355 cmd->tx_cmd.len =
356 cpu_to_le16(iwl_mvm_fill_probe_req(
357 (struct ieee80211_mgmt *)cmd->data,
358 vif->addr,
359 req->n_ssids, ssid, ssid_len,
360 req->ie, req->ie_len,
361 mvm->fw->ucode_capa.max_probe_length));
362
David Spinadel20f1a5d2013-08-21 09:14:27 +0300363 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100364
365 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
366 le16_to_cpu(cmd->tx_cmd.len) +
367 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
368 hcmd.len[0] = le16_to_cpu(cmd->len);
369
370 status = SCAN_RESPONSE_OK;
371 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
372 if (!ret && status == SCAN_RESPONSE_OK) {
373 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
374 } else {
375 /*
376 * If the scan failed, it usually means that the FW was unable
377 * to allocate the time events. Warn on it, but maybe we
378 * should try to send the command again with different params.
379 */
380 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
381 status, ret);
382 mvm->scan_status = IWL_MVM_SCAN_NONE;
383 ret = -EIO;
384 }
385 return ret;
386}
387
388int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
389 struct iwl_device_cmd *cmd)
390{
391 struct iwl_rx_packet *pkt = rxb_addr(rxb);
392 struct iwl_cmd_response *resp = (void *)pkt->data;
393
394 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
395 le32_to_cpu(resp->status));
396 return 0;
397}
398
399int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
400 struct iwl_device_cmd *cmd)
401{
402 struct iwl_rx_packet *pkt = rxb_addr(rxb);
403 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
404
405 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
406 notif->status, notif->scanned_channels);
407
408 mvm->scan_status = IWL_MVM_SCAN_NONE;
409 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
410
Arik Nemtsov519e2022013-10-17 17:51:35 +0300411 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
412
Johannes Berg8ca151b2013-01-24 14:25:36 +0100413 return 0;
414}
415
David Spinadel35a000b2013-08-28 09:29:43 +0300416int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
417 struct iwl_rx_cmd_buffer *rxb,
418 struct iwl_device_cmd *cmd)
419{
420 struct iwl_rx_packet *pkt = rxb_addr(rxb);
421 struct iwl_sched_scan_results *notif = (void *)pkt->data;
422
423 if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
424 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
425 ieee80211_sched_scan_results(mvm->hw);
426 }
427
428 return 0;
429}
430
Johannes Berg8ca151b2013-01-24 14:25:36 +0100431static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
432 struct iwl_rx_packet *pkt, void *data)
433{
434 struct iwl_mvm *mvm =
435 container_of(notif_wait, struct iwl_mvm, notif_wait);
436 struct iwl_scan_complete_notif *notif;
437 u32 *resp;
438
439 switch (pkt->hdr.cmd) {
440 case SCAN_ABORT_CMD:
441 resp = (void *)pkt->data;
442 if (*resp == CAN_ABORT_STATUS) {
443 IWL_DEBUG_SCAN(mvm,
444 "Scan can be aborted, wait until completion\n");
445 return false;
446 }
447
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300448 /*
449 * If scan cannot be aborted, it means that we had a
450 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
451 * ieee80211_scan_completed already.
452 */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100453 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
454 *resp);
455 return true;
456
457 case SCAN_COMPLETE_NOTIFICATION:
458 notif = (void *)pkt->data;
459 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
460 notif->status);
461 return true;
462
463 default:
464 WARN_ON(1);
465 return false;
466 };
467}
468
469void iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
470{
471 struct iwl_notification_wait wait_scan_abort;
472 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
473 SCAN_COMPLETE_NOTIFICATION };
474 int ret;
475
Emmanuel Grumbach5a3e9f72013-09-15 14:39:02 +0300476 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
477 return;
478
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200479 if (iwl_mvm_is_radio_killed(mvm)) {
480 ieee80211_scan_completed(mvm->hw, true);
Arik Nemtsov519e2022013-10-17 17:51:35 +0300481 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200482 mvm->scan_status = IWL_MVM_SCAN_NONE;
483 return;
484 }
485
Johannes Berg8ca151b2013-01-24 14:25:36 +0100486 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
487 scan_abort_notif,
488 ARRAY_SIZE(scan_abort_notif),
489 iwl_mvm_scan_abort_notif, NULL);
490
Emmanuel Grumbachaaa4e742013-12-19 22:20:58 +0200491 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100492 if (ret) {
493 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
David Spinadel992f81f2014-01-09 14:22:55 +0200494 /* mac80211's state will be cleaned in the nic_restart flow */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100495 goto out_remove_notif;
496 }
497
498 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, 1 * HZ);
499 if (ret)
500 IWL_ERR(mvm, "%s - failed on timeout\n", __func__);
501
502 return;
503
504out_remove_notif:
505 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
506}
David Spinadel35a000b2013-08-28 09:29:43 +0300507
508int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
509 struct iwl_rx_cmd_buffer *rxb,
510 struct iwl_device_cmd *cmd)
511{
512 struct iwl_rx_packet *pkt = rxb_addr(rxb);
513 struct iwl_scan_offload_complete *scan_notif = (void *)pkt->data;
514
Johannes Berga6623e82014-01-27 15:40:53 +0100515 /* scan status must be locked for proper checking */
516 lockdep_assert_held(&mvm->mutex);
517
David Spinadel35a000b2013-08-28 09:29:43 +0300518 IWL_DEBUG_SCAN(mvm, "Scheduled scan completed, status %s\n",
519 scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
520 "completed" : "aborted");
521
Johannes Berga6623e82014-01-27 15:40:53 +0100522 /* might already be something else again, don't reset if so */
523 if (mvm->scan_status == IWL_MVM_SCAN_SCHED)
524 mvm->scan_status = IWL_MVM_SCAN_NONE;
David Spinadel35a000b2013-08-28 09:29:43 +0300525 ieee80211_sched_scan_stopped(mvm->hw);
526
527 return 0;
528}
529
530static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
531 struct ieee80211_vif *vif,
532 struct ieee80211_sched_scan_ies *ies,
533 enum ieee80211_band band,
534 struct iwl_tx_cmd *cmd,
535 u8 *data)
536{
537 u16 cmd_len;
538
539 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
540 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
541 cmd->sta_id = mvm->aux_sta.sta_id;
542
543 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
544
545 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
546 vif->addr,
547 1, NULL, 0,
548 ies->ie[band], ies->len[band],
549 SCAN_OFFLOAD_PROBE_REQ_SIZE);
550 cmd->len = cpu_to_le16(cmd_len);
551}
552
553static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
554 struct ieee80211_vif *vif,
555 struct cfg80211_sched_scan_request *req,
556 struct iwl_scan_offload_cmd *scan)
557{
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200558 bool is_assoc = false;
559
560 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
561 IEEE80211_IFACE_ITER_NORMAL,
562 iwl_mvm_vif_assoc_iterator,
563 &is_assoc);
David Spinadel35a000b2013-08-28 09:29:43 +0300564 scan->channel_count =
565 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
566 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
567 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
568 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
569 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
570 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200571 scan->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
572 is_assoc);
573 scan->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
David Spinadel35a000b2013-08-28 09:29:43 +0300574 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
575 MAC_FILTER_IN_BEACON);
576 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
577 scan->rep_count = cpu_to_le32(1);
578}
579
580static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
581{
582 int i;
583
584 for (i = 0; i < PROBE_OPTION_MAX; i++) {
585 if (!ssid_list[i].len)
586 break;
587 if (ssid_list[i].len == ssid_len &&
588 !memcmp(ssid_list->ssid, ssid, ssid_len))
589 return i;
590 }
591 return -1;
592}
593
594static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
595 struct iwl_scan_offload_cmd *scan,
596 u32 *ssid_bitmap)
597{
598 int i, j;
599 int index;
600
601 /*
602 * copy SSIDs from match list.
603 * iwl_config_sched_scan_profiles() uses the order of these ssids to
604 * config match list.
605 */
606 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100607 /* skip empty SSID matchsets */
608 if (!req->match_sets[i].ssid.ssid_len)
609 continue;
David Spinadel35a000b2013-08-28 09:29:43 +0300610 scan->direct_scan[i].id = WLAN_EID_SSID;
611 scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
612 memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
613 scan->direct_scan[i].len);
614 }
615
616 /* add SSIDs from scan SSID list */
617 *ssid_bitmap = 0;
618 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
619 index = iwl_ssid_exist(req->ssids[j].ssid,
620 req->ssids[j].ssid_len,
621 scan->direct_scan);
622 if (index < 0) {
623 if (!req->ssids[j].ssid_len)
624 continue;
625 scan->direct_scan[i].id = WLAN_EID_SSID;
626 scan->direct_scan[i].len = req->ssids[j].ssid_len;
627 memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
628 scan->direct_scan[i].len);
629 *ssid_bitmap |= BIT(i + 1);
630 i++;
631 } else {
632 *ssid_bitmap |= BIT(index + 1);
633 }
634 }
635}
636
637static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
638 struct cfg80211_sched_scan_request *req,
639 struct iwl_scan_channel_cfg *channels,
640 enum ieee80211_band band,
641 int *head, int *tail,
642 u32 ssid_bitmap)
643{
644 struct ieee80211_supported_band *s_band;
645 int n_probes = req->n_ssids;
646 int n_channels = req->n_channels;
647 u8 active_dwell, passive_dwell;
648 int i, j, index = 0;
649 bool partial;
650
651 /*
652 * We have to configure all supported channels, even if we don't want to
653 * scan on them, but we have to send channels in the order that we want
654 * to scan. So add requested channels to head of the list and others to
655 * the end.
656 */
657 active_dwell = iwl_mvm_get_active_dwell(band, n_probes);
658 passive_dwell = iwl_mvm_get_passive_dwell(band);
659 s_band = &mvm->nvm_data->bands[band];
660
661 for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
662 partial = false;
663 for (j = 0; j < n_channels; j++)
664 if (s_band->channels[i].center_freq ==
665 req->channels[j]->center_freq) {
666 index = *head;
667 (*head)++;
668 /*
669 * Channels that came with the request will be
670 * in partial scan .
671 */
672 partial = true;
673 break;
674 }
675 if (!partial) {
676 index = *tail;
677 (*tail)--;
678 }
679 channels->channel_number[index] =
680 cpu_to_le16(ieee80211_frequency_to_channel(
681 s_band->channels[i].center_freq));
682 channels->dwell_time[index][0] = active_dwell;
683 channels->dwell_time[index][1] = passive_dwell;
684
685 channels->iter_count[index] = cpu_to_le16(1);
686 channels->iter_interval[index] = 0;
687
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200688 if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
David Spinadel35a000b2013-08-28 09:29:43 +0300689 channels->type[index] |=
690 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
691
692 channels->type[index] |=
693 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
694 if (partial)
695 channels->type[index] |=
696 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
697
698 if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
699 channels->type[index] |=
700 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
701
702 /* scan for all SSIDs from req->ssids */
703 channels->type[index] |= cpu_to_le32(ssid_bitmap);
704 }
705}
706
707int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
708 struct ieee80211_vif *vif,
709 struct cfg80211_sched_scan_request *req,
710 struct ieee80211_sched_scan_ies *ies)
711{
712 int supported_bands = 0;
713 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
714 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
715 int head = 0;
716 int tail = band_2ghz + band_5ghz;
717 u32 ssid_bitmap;
718 int cmd_len;
719 int ret;
720
721 struct iwl_scan_offload_cfg *scan_cfg;
722 struct iwl_host_cmd cmd = {
723 .id = SCAN_OFFLOAD_CONFIG_CMD,
724 .flags = CMD_SYNC,
725 };
726
727 lockdep_assert_held(&mvm->mutex);
728
729 if (band_2ghz)
730 supported_bands++;
731 if (band_5ghz)
732 supported_bands++;
733
734 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
735 supported_bands * SCAN_OFFLOAD_PROBE_REQ_SIZE;
736
737 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
738 if (!scan_cfg)
739 return -ENOMEM;
740
741 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd);
742 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
743
744 iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
745 /* build tx frames for supported bands */
746 if (band_2ghz) {
747 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
748 IEEE80211_BAND_2GHZ,
749 &scan_cfg->scan_cmd.tx_cmd[0],
750 scan_cfg->data);
751 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
752 IEEE80211_BAND_2GHZ, &head, &tail,
753 ssid_bitmap);
754 }
755 if (band_5ghz) {
756 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
757 IEEE80211_BAND_5GHZ,
758 &scan_cfg->scan_cmd.tx_cmd[1],
759 scan_cfg->data +
760 SCAN_OFFLOAD_PROBE_REQ_SIZE);
761 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
762 IEEE80211_BAND_5GHZ, &head, &tail,
763 ssid_bitmap);
764 }
765
766 cmd.data[0] = scan_cfg;
767 cmd.len[0] = cmd_len;
768 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
769
770 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
771
772 ret = iwl_mvm_send_cmd(mvm, &cmd);
773 kfree(scan_cfg);
774 return ret;
775}
776
777int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
778 struct cfg80211_sched_scan_request *req)
779{
780 struct iwl_scan_offload_profile *profile;
781 struct iwl_scan_offload_profile_cfg *profile_cfg;
782 struct iwl_scan_offload_blacklist *blacklist;
783 struct iwl_host_cmd cmd = {
784 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
785 .flags = CMD_SYNC,
786 .len[1] = sizeof(*profile_cfg),
787 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
788 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
789 };
790 int blacklist_len;
791 int i;
792 int ret;
793
794 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
795 return -EIO;
796
797 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
798 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
799 else
800 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
801
802 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
803 if (!blacklist)
804 return -ENOMEM;
805
806 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
807 if (!profile_cfg) {
808 ret = -ENOMEM;
809 goto free_blacklist;
810 }
811
812 cmd.data[0] = blacklist;
813 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
814 cmd.data[1] = profile_cfg;
815
816 /* No blacklist configuration */
817
818 profile_cfg->num_profiles = req->n_match_sets;
819 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
820 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
821 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200822 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
823 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300824
825 for (i = 0; i < req->n_match_sets; i++) {
826 profile = &profile_cfg->profiles[i];
827 profile->ssid_index = i;
828 /* Support any cipher and auth algorithm */
829 profile->unicast_cipher = 0xff;
830 profile->auth_alg = 0xff;
831 profile->network_type = IWL_NETWORK_TYPE_ANY;
832 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
833 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
834 }
835
836 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
837
838 ret = iwl_mvm_send_cmd(mvm, &cmd);
839 kfree(profile_cfg);
840free_blacklist:
841 kfree(blacklist);
842
843 return ret;
844}
845
846int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
847 struct cfg80211_sched_scan_request *req)
848{
849 struct iwl_scan_offload_req scan_req = {
850 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
851
852 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
853 .schedule_line[0].delay = req->interval / 1000,
854 .schedule_line[0].full_scan_mul = 1,
855
856 .schedule_line[1].iterations = 0xff,
857 .schedule_line[1].delay = req->interval / 1000,
858 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
859 };
860
861 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
862 IWL_DEBUG_SCAN(mvm,
863 "Sending scheduled scan with filtering, filter len %d\n",
864 req->n_match_sets);
David Spinadel35a000b2013-08-28 09:29:43 +0300865 } else {
866 IWL_DEBUG_SCAN(mvm,
867 "Sending Scheduled scan without filtering\n");
Eliad Pellerde33fb52013-11-10 12:59:46 +0200868 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
David Spinadel35a000b2013-08-28 09:29:43 +0300869 }
870
871 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, CMD_SYNC,
872 sizeof(scan_req), &scan_req);
873}
874
875static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
876{
877 int ret;
878 struct iwl_host_cmd cmd = {
879 .id = SCAN_OFFLOAD_ABORT_CMD,
880 .flags = CMD_SYNC,
881 };
882 u32 status;
883
884 /* Exit instantly with error when device is not ready
885 * to receive scan abort command or it does not perform
886 * scheduled scan currently */
887 if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
888 return -EIO;
889
890 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
891 if (ret)
892 return ret;
893
894 if (status != CAN_ABORT_STATUS) {
895 /*
896 * The scan abort will return 1 for success or
897 * 2 for "failure". A failure condition can be
898 * due to simply not being in an active scan which
899 * can occur if we send the scan abort before the
900 * microcode has notified us that a scan is completed.
901 */
902 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
903 ret = -EIO;
904 }
905
906 return ret;
907}
908
909void iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm)
910{
911 int ret;
912
913 lockdep_assert_held(&mvm->mutex);
914
915 if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
916 IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
917 return;
918 }
919
920 ret = iwl_mvm_send_sched_scan_abort(mvm);
921 if (ret)
922 IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
923 else
924 IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
925}