]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/iwlwifi/mvm/scan.c
iwlwifi: mvm: disable BT Co-running by default
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / iwlwifi / mvm / scan.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
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 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
410dc5aa 26 * in the file called COPYING.
8ca151b5
JB
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
51368bf7 34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/etherdevice.h>
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "iwl-eeprom-parse.h"
71#include "fw-api-scan.h"
72
73#define IWL_PLCP_QUIET_THRESH 1
74#define IWL_ACTIVE_QUIET_TIME 10
8a110d9b
AB
75
76struct iwl_mvm_scan_params {
77 u32 max_out_time;
78 u32 suspend_time;
50df8a30
AB
79 bool passive_fragmented;
80 struct _dwell {
81 u16 passive;
82 u16 active;
83 } dwell[IEEE80211_NUM_BANDS];
8a110d9b 84};
8ca151b5
JB
85
86static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
87{
88 u16 rx_chain;
91b05d10 89 u8 rx_ant;
8ca151b5 90
91b05d10
OG
91 if (mvm->scan_rx_ant != ANT_NONE)
92 rx_ant = mvm->scan_rx_ant;
93 else
4ed735e7 94 rx_ant = mvm->fw->valid_rx_ant;
8ca151b5
JB
95 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
96 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
97 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
98 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
99 return cpu_to_le16(rx_chain);
100}
101
fb98be5e 102static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
8ca151b5 103{
fb98be5e 104 if (band == IEEE80211_BAND_2GHZ)
8ca151b5
JB
105 return cpu_to_le32(PHY_BAND_24);
106 else
107 return cpu_to_le32(PHY_BAND_5);
108}
109
110static inline __le32
111iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
112 bool no_cck)
113{
114 u32 tx_ant;
115
116 mvm->scan_last_antenna_idx =
4ed735e7 117 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
8ca151b5
JB
118 mvm->scan_last_antenna_idx);
119 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
120
121 if (band == IEEE80211_BAND_2GHZ && !no_cck)
122 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
123 tx_ant);
124 else
125 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
126}
127
128/*
129 * We insert the SSIDs in an inverted order, because the FW will
130 * invert it back. The most prioritized SSID, which is first in the
131 * request list, is not copied here, but inserted directly to the probe
132 * request.
133 */
fb98be5e
DS
134static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
135 struct cfg80211_ssid *ssids,
136 int n_ssids, int first)
8ca151b5
JB
137{
138 int fw_idx, req_idx;
139
fb98be5e 140 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
fe04e837 141 req_idx--, fw_idx++) {
fb98be5e
DS
142 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
143 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
144 memcpy(cmd_ssid[fw_idx].ssid,
145 ssids[req_idx].ssid,
146 ssids[req_idx].ssid_len);
8ca151b5
JB
147 }
148}
149
150/*
151 * If req->n_ssids > 0, it means we should do an active scan.
152 * In case of active scan w/o directed scan, we receive a zero-length SSID
153 * just to notify that this scan is active and not passive.
154 * In order to notify the FW of the number of SSIDs we wish to scan (including
155 * the zero-length one), we need to set the corresponding bits in chan->type,
20f1a5de
DS
156 * one for each SSID, and set the active bit (first). If the first SSID is
157 * already included in the probe template, so we need to set only
158 * req->n_ssids - 1 bits in addition to the first bit.
8ca151b5
JB
159 */
160static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
161{
162 if (band == IEEE80211_BAND_2GHZ)
39745337
DS
163 return 20 + 3 * (n_ssids + 1);
164 return 10 + 2 * (n_ssids + 1);
8ca151b5
JB
165}
166
167static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
168{
169 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
170}
171
172static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
20f1a5de 173 struct cfg80211_scan_request *req,
50df8a30
AB
174 bool basic_ssid,
175 struct iwl_mvm_scan_params *params)
8ca151b5 176{
8ca151b5
JB
177 struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
178 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
179 int i;
20f1a5de 180 int type = BIT(req->n_ssids) - 1;
50df8a30 181 enum ieee80211_band band = req->channels[0]->band;
20f1a5de
DS
182
183 if (!basic_ssid)
184 type |= BIT(req->n_ssids);
8ca151b5
JB
185
186 for (i = 0; i < cmd->channel_count; i++) {
187 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
20f1a5de 188 chan->type = cpu_to_le32(type);
8fe02e16 189 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
bb963c4a 190 chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
50df8a30
AB
191 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
192 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
8ca151b5
JB
193 chan->iteration_count = cpu_to_le16(1);
194 chan++;
195 }
196}
197
198/*
199 * Fill in probe request with the following parameters:
200 * TA is our vif HW address, which mac80211 ensures we have.
201 * Packet is broadcasted, so this is both SA and DA.
202 * The probe request IE is made out of two: first comes the most prioritized
203 * SSID if a directed scan is requested. Second comes whatever extra
204 * information was given to us as the scan request IE.
205 */
206static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
207 int n_ssids, const u8 *ssid, int ssid_len,
633e2713
DS
208 const u8 *band_ie, int band_ie_len,
209 const u8 *common_ie, int common_ie_len,
8ca151b5
JB
210 int left)
211{
212 int len = 0;
213 u8 *pos = NULL;
214
215 /* Make sure there is enough space for the probe request,
216 * two mandatory IEs and the data */
217 left -= 24;
218 if (left < 0)
219 return 0;
220
221 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
222 eth_broadcast_addr(frame->da);
223 memcpy(frame->sa, ta, ETH_ALEN);
224 eth_broadcast_addr(frame->bssid);
225 frame->seq_ctrl = 0;
226
227 len += 24;
228
229 /* for passive scans, no need to fill anything */
230 if (n_ssids == 0)
231 return (u16)len;
232
233 /* points to the payload of the request */
234 pos = &frame->u.probe_req.variable[0];
235
236 /* fill in our SSID IE */
237 left -= ssid_len + 2;
238 if (left < 0)
239 return 0;
240 *pos++ = WLAN_EID_SSID;
241 *pos++ = ssid_len;
242 if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
243 memcpy(pos, ssid, ssid_len);
244 pos += ssid_len;
245 }
246
247 len += ssid_len + 2;
248
633e2713 249 if (WARN_ON(left < band_ie_len + common_ie_len))
8ca151b5
JB
250 return len;
251
633e2713
DS
252 if (band_ie && band_ie_len) {
253 memcpy(pos, band_ie, band_ie_len);
254 pos += band_ie_len;
255 len += band_ie_len;
256 }
257
258 if (common_ie && common_ie_len) {
259 memcpy(pos, common_ie, common_ie_len);
260 pos += common_ie_len;
261 len += common_ie_len;
8ca151b5
JB
262 }
263
264 return (u16)len;
265}
266
8a110d9b
AB
267static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
268 struct ieee80211_vif *vif)
61f6325d 269{
8a110d9b
AB
270 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
271 bool *global_bound = data;
61f6325d 272
8a110d9b
AB
273 if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
274 *global_bound = true;
275}
276
277static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
50df8a30 278 struct ieee80211_vif *vif,
ab480030 279 int n_ssids, u32 flags,
8a110d9b
AB
280 struct iwl_mvm_scan_params *params)
281{
282 bool global_bound = false;
50df8a30 283 enum ieee80211_band band;
2ce89cd6 284 u8 frag_passive_dwell = 0;
8a110d9b
AB
285
286 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
287 IEEE80211_IFACE_ITER_NORMAL,
288 iwl_mvm_scan_condition_iterator,
289 &global_bound);
ef67f18d
AB
290
291 if (!global_bound)
292 goto not_bound;
293
2ce89cd6
DS
294 params->suspend_time = 30;
295 params->max_out_time = 170;
ef67f18d
AB
296
297 if (iwl_mvm_low_latency(mvm)) {
2ce89cd6
DS
298 if (mvm->fw->ucode_capa.api[0] &
299 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
300 params->suspend_time = 105;
301 params->max_out_time = 70;
302 frag_passive_dwell = 20;
303 } else {
304 params->suspend_time = 120;
305 params->max_out_time = 120;
306 }
307 }
308
309 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
310 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
311 /*
312 * P2P device scan should not be fragmented to avoid negative
313 * impact on P2P device discovery. Configure max_out_time to be
314 * equal to dwell time on passive channel. Take a longest
315 * possible value, one that corresponds to 2GHz band
316 */
317 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
318 u32 passive_dwell =
319 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
320 params->max_out_time = passive_dwell;
321 } else {
322 params->passive_fragmented = true;
323 }
50df8a30 324 }
8a110d9b 325
ab480030
JB
326 if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
327 params->max_out_time = 200;
328
ef67f18d
AB
329not_bound:
330
50df8a30 331 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
2ce89cd6
DS
332 if (params->passive_fragmented)
333 params->dwell[band].passive = frag_passive_dwell;
334 else
335 params->dwell[band].passive =
336 iwl_mvm_get_passive_dwell(band);
50df8a30
AB
337 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
338 n_ssids);
339 }
61f6325d
HD
340}
341
73897bd1
AO
342static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
343{
344 /* require rrm scan whenever the fw supports it */
345 return mvm->fw->ucode_capa.capa[0] &
346 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
347}
348
8ca151b5
JB
349int iwl_mvm_scan_request(struct iwl_mvm *mvm,
350 struct ieee80211_vif *vif,
351 struct cfg80211_scan_request *req)
352{
353 struct iwl_host_cmd hcmd = {
354 .id = SCAN_REQUEST_CMD,
355 .len = { 0, },
356 .data = { mvm->scan_cmd, },
8ca151b5
JB
357 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
358 };
359 struct iwl_scan_cmd *cmd = mvm->scan_cmd;
360 int ret;
361 u32 status;
362 int ssid_len = 0;
363 u8 *ssid = NULL;
20f1a5de
DS
364 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
365 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
8a110d9b 366 struct iwl_mvm_scan_params params = {};
8ca151b5
JB
367
368 lockdep_assert_held(&mvm->mutex);
748fa67c
EG
369
370 /* we should have failed registration if scan_cmd was NULL */
371 if (WARN_ON(mvm->scan_cmd == NULL))
372 return -ENOMEM;
8ca151b5
JB
373
374 IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
375 mvm->scan_status = IWL_MVM_SCAN_OS;
0564679b 376 memset(cmd, 0, ksize(cmd));
8a110d9b 377
8ca151b5
JB
378 cmd->channel_count = (u8)req->n_channels;
379 cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
380 cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
381 cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
8a110d9b 382
ab480030 383 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
8a110d9b
AB
384 cmd->max_out_time = cpu_to_le32(params.max_out_time);
385 cmd->suspend_time = cpu_to_le32(params.suspend_time);
50df8a30
AB
386 if (params.passive_fragmented)
387 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
8a110d9b 388
fb98be5e 389 cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
8ca151b5
JB
390 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
391 MAC_FILTER_IN_BEACON);
d91b06d2
IP
392
393 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
394 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
395 else
396 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
397
8ca151b5
JB
398 cmd->repeats = cpu_to_le32(1);
399
400 /*
401 * If the user asked for passive scan, don't change to active scan if
402 * you see any activity on the channel - remain passive.
403 */
404 if (req->n_ssids > 0) {
405 cmd->passive2active = cpu_to_le16(1);
26e05cc3 406 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
20f1a5de
DS
407 if (basic_ssid) {
408 ssid = req->ssids[0].ssid;
409 ssid_len = req->ssids[0].ssid_len;
410 }
8ca151b5
JB
411 } else {
412 cmd->passive2active = 0;
26e05cc3 413 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
8ca151b5
JB
414 }
415
fb98be5e
DS
416 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
417 basic_ssid ? 1 : 0);
8ca151b5 418
8e2a866e
EG
419 cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
420 TX_CMD_FLG_BT_DIS);
8ca151b5
JB
421 cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
422 cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
423 cmd->tx_cmd.rate_n_flags =
424 iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
425 req->no_cck);
426
427 cmd->tx_cmd.len =
428 cpu_to_le16(iwl_mvm_fill_probe_req(
429 (struct ieee80211_mgmt *)cmd->data,
430 vif->addr,
431 req->n_ssids, ssid, ssid_len,
633e2713 432 req->ie, req->ie_len, NULL, 0,
8ca151b5
JB
433 mvm->fw->ucode_capa.max_probe_length));
434
50df8a30 435 iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
8ca151b5
JB
436
437 cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
438 le16_to_cpu(cmd->tx_cmd.len) +
439 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
440 hcmd.len[0] = le16_to_cpu(cmd->len);
441
442 status = SCAN_RESPONSE_OK;
443 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
444 if (!ret && status == SCAN_RESPONSE_OK) {
445 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
446 } else {
447 /*
448 * If the scan failed, it usually means that the FW was unable
449 * to allocate the time events. Warn on it, but maybe we
450 * should try to send the command again with different params.
451 */
452 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
453 status, ret);
454 mvm->scan_status = IWL_MVM_SCAN_NONE;
455 ret = -EIO;
456 }
457 return ret;
458}
459
460int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
461 struct iwl_device_cmd *cmd)
462{
463 struct iwl_rx_packet *pkt = rxb_addr(rxb);
464 struct iwl_cmd_response *resp = (void *)pkt->data;
465
466 IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
467 le32_to_cpu(resp->status));
468 return 0;
469}
470
471int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
472 struct iwl_device_cmd *cmd)
473{
474 struct iwl_rx_packet *pkt = rxb_addr(rxb);
475 struct iwl_scan_complete_notif *notif = (void *)pkt->data;
476
91b80256
AN
477 lockdep_assert_held(&mvm->mutex);
478
8ca151b5
JB
479 IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
480 notif->status, notif->scanned_channels);
481
91b80256
AN
482 if (mvm->scan_status == IWL_MVM_SCAN_OS)
483 mvm->scan_status = IWL_MVM_SCAN_NONE;
8ca151b5
JB
484 ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
485
519e2026
AN
486 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
487
8ca151b5
JB
488 return 0;
489}
490
fb98be5e
DS
491int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
492 struct iwl_rx_cmd_buffer *rxb,
493 struct iwl_device_cmd *cmd)
35a000b7
DS
494{
495 struct iwl_rx_packet *pkt = rxb_addr(rxb);
fb98be5e
DS
496 u8 client_bitmap = 0;
497
498 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
499 struct iwl_sched_scan_results *notif = (void *)pkt->data;
35a000b7 500
fb98be5e
DS
501 client_bitmap = notif->client_bitmap;
502 }
503
504 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
505 client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
506 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
507 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
508 ieee80211_sched_scan_results(mvm->hw);
509 } else {
510 IWL_DEBUG_SCAN(mvm, "Scan results\n");
511 }
35a000b7
DS
512 }
513
514 return 0;
515}
516
8ca151b5
JB
517static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
518 struct iwl_rx_packet *pkt, void *data)
519{
520 struct iwl_mvm *mvm =
521 container_of(notif_wait, struct iwl_mvm, notif_wait);
522 struct iwl_scan_complete_notif *notif;
523 u32 *resp;
524
525 switch (pkt->hdr.cmd) {
526 case SCAN_ABORT_CMD:
527 resp = (void *)pkt->data;
528 if (*resp == CAN_ABORT_STATUS) {
529 IWL_DEBUG_SCAN(mvm,
530 "Scan can be aborted, wait until completion\n");
531 return false;
532 }
533
5a3e9f7f
EG
534 /*
535 * If scan cannot be aborted, it means that we had a
536 * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
537 * ieee80211_scan_completed already.
538 */
8ca151b5
JB
539 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
540 *resp);
541 return true;
542
543 case SCAN_COMPLETE_NOTIFICATION:
544 notif = (void *)pkt->data;
545 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
546 notif->status);
547 return true;
548
549 default:
550 WARN_ON(1);
551 return false;
552 };
553}
554
fb98be5e 555static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
8ca151b5
JB
556{
557 struct iwl_notification_wait wait_scan_abort;
558 static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
559 SCAN_COMPLETE_NOTIFICATION };
560 int ret;
561
5a3e9f7f 562 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
91b80256 563 return 0;
5a3e9f7f 564
aaa4e741
EG
565 if (iwl_mvm_is_radio_killed(mvm)) {
566 ieee80211_scan_completed(mvm->hw, true);
519e2026 567 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
aaa4e741 568 mvm->scan_status = IWL_MVM_SCAN_NONE;
91b80256 569 return 0;
aaa4e741
EG
570 }
571
8ca151b5
JB
572 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
573 scan_abort_notif,
574 ARRAY_SIZE(scan_abort_notif),
575 iwl_mvm_scan_abort_notif, NULL);
576
a1022927 577 ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
8ca151b5
JB
578 if (ret) {
579 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
992f81fc 580 /* mac80211's state will be cleaned in the nic_restart flow */
8ca151b5
JB
581 goto out_remove_notif;
582 }
583
91b80256 584 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
8ca151b5
JB
585
586out_remove_notif:
587 iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
91b80256 588 return ret;
8ca151b5 589}
35a000b7
DS
590
591int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
592 struct iwl_rx_cmd_buffer *rxb,
593 struct iwl_device_cmd *cmd)
594{
595 struct iwl_rx_packet *pkt = rxb_addr(rxb);
fb98be5e
DS
596 u8 status, ebs_status;
597
598 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
599 struct iwl_periodic_scan_complete *scan_notif;
35a000b7 600
fb98be5e
DS
601 scan_notif = (void *)pkt->data;
602 status = scan_notif->status;
603 ebs_status = scan_notif->ebs_status;
604 } else {
605 struct iwl_scan_offload_complete *scan_notif;
606
607 scan_notif = (void *)pkt->data;
608 status = scan_notif->status;
609 ebs_status = scan_notif->ebs_status;
610 }
a6623e84
JB
611 /* scan status must be locked for proper checking */
612 lockdep_assert_held(&mvm->mutex);
613
e820c2da 614 IWL_DEBUG_SCAN(mvm,
fb98be5e
DS
615 "%s completed, status %s, EBS status %s\n",
616 mvm->scan_status == IWL_MVM_SCAN_SCHED ?
617 "Scheduled scan" : "Scan",
618 status == IWL_SCAN_OFFLOAD_COMPLETED ?
619 "completed" : "aborted",
620 ebs_status == IWL_SCAN_EBS_SUCCESS ?
621 "success" : "failed");
e820c2da 622
35a000b7 623
33ea27f6
AN
624 /* only call mac80211 completion if the stop was initiated by FW */
625 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
a6623e84 626 mvm->scan_status = IWL_MVM_SCAN_NONE;
33ea27f6 627 ieee80211_sched_scan_stopped(mvm->hw);
fb98be5e
DS
628 } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
629 mvm->scan_status = IWL_MVM_SCAN_NONE;
630 ieee80211_scan_completed(mvm->hw,
631 status == IWL_SCAN_OFFLOAD_ABORTED);
33ea27f6 632 }
35a000b7 633
fb98be5e 634 mvm->last_ebs_successful = !ebs_status;
e820c2da 635
35a000b7
DS
636 return 0;
637}
638
639static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
640 struct ieee80211_vif *vif,
633e2713 641 struct ieee80211_scan_ies *ies,
35a000b7
DS
642 enum ieee80211_band band,
643 struct iwl_tx_cmd *cmd,
644 u8 *data)
645{
646 u16 cmd_len;
647
648 cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
649 cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
650 cmd->sta_id = mvm->aux_sta.sta_id;
651
652 cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
653
654 cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
655 vif->addr,
656 1, NULL, 0,
633e2713
DS
657 ies->ies[band], ies->len[band],
658 ies->common_ies, ies->common_ie_len,
35a000b7
DS
659 SCAN_OFFLOAD_PROBE_REQ_SIZE);
660 cmd->len = cpu_to_le16(cmd_len);
661}
662
663static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
664 struct ieee80211_vif *vif,
665 struct cfg80211_sched_scan_request *req,
50df8a30
AB
666 struct iwl_scan_offload_cmd *scan,
667 struct iwl_mvm_scan_params *params)
35a000b7 668{
89879413 669 scan->channel_count = req->n_channels;
35a000b7
DS
670 scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
671 scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
672 scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
673 scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
8a110d9b 674
50df8a30
AB
675 scan->max_out_time = cpu_to_le32(params->max_out_time);
676 scan->suspend_time = cpu_to_le32(params->suspend_time);
8a110d9b 677
35a000b7
DS
678 scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
679 MAC_FILTER_IN_BEACON);
680 scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
681 scan->rep_count = cpu_to_le32(1);
50df8a30
AB
682
683 if (params->passive_fragmented)
684 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
35a000b7
DS
685}
686
687static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
688{
689 int i;
690
691 for (i = 0; i < PROBE_OPTION_MAX; i++) {
692 if (!ssid_list[i].len)
693 break;
694 if (ssid_list[i].len == ssid_len &&
695 !memcmp(ssid_list->ssid, ssid, ssid_len))
696 return i;
697 }
698 return -1;
699}
700
701static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
fb98be5e
DS
702 struct iwl_ssid_ie *direct_scan,
703 u32 *ssid_bitmap, bool basic_ssid)
35a000b7
DS
704{
705 int i, j;
706 int index;
707
708 /*
709 * copy SSIDs from match list.
710 * iwl_config_sched_scan_profiles() uses the order of these ssids to
711 * config match list.
712 */
713 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
ea73cbce
JB
714 /* skip empty SSID matchsets */
715 if (!req->match_sets[i].ssid.ssid_len)
716 continue;
fb98be5e
DS
717 direct_scan[i].id = WLAN_EID_SSID;
718 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
719 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
720 direct_scan[i].len);
35a000b7
DS
721 }
722
723 /* add SSIDs from scan SSID list */
724 *ssid_bitmap = 0;
725 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
726 index = iwl_ssid_exist(req->ssids[j].ssid,
727 req->ssids[j].ssid_len,
fb98be5e 728 direct_scan);
35a000b7 729 if (index < 0) {
fb98be5e 730 if (!req->ssids[j].ssid_len && basic_ssid)
35a000b7 731 continue;
fb98be5e
DS
732 direct_scan[i].id = WLAN_EID_SSID;
733 direct_scan[i].len = req->ssids[j].ssid_len;
734 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
735 direct_scan[i].len);
35a000b7
DS
736 *ssid_bitmap |= BIT(i + 1);
737 i++;
738 } else {
739 *ssid_bitmap |= BIT(index + 1);
740 }
741 }
742}
743
744static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
745 struct cfg80211_sched_scan_request *req,
762533ba 746 u8 *channels_buffer,
35a000b7 747 enum ieee80211_band band,
89879413 748 int *head,
50df8a30
AB
749 u32 ssid_bitmap,
750 struct iwl_mvm_scan_params *params)
35a000b7 751{
762533ba
DS
752 u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
753 __le32 *type = (__le32 *)channels_buffer;
754 __le16 *channel_number = (__le16 *)(type + n_channels);
755 __le16 *iter_count = channel_number + n_channels;
756 __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
757 u8 *active_dwell = (u8 *)(iter_interval + n_channels);
758 u8 *passive_dwell = active_dwell + n_channels;
89879413 759 int i, index = 0;
35a000b7 760
89879413
EP
761 for (i = 0; i < req->n_channels; i++) {
762 struct ieee80211_channel *chan = req->channels[i];
763
764 if (chan->band != band)
765 continue;
35a000b7 766
89879413
EP
767 index = *head;
768 (*head)++;
769
762533ba
DS
770 channel_number[index] = cpu_to_le16(chan->hw_value);
771 active_dwell[index] = params->dwell[band].active;
772 passive_dwell[index] = params->dwell[band].passive;
35a000b7 773
762533ba
DS
774 iter_count[index] = cpu_to_le16(1);
775 iter_interval[index] = 0;
35a000b7 776
89879413 777 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
762533ba 778 type[index] |=
35a000b7
DS
779 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
780
762533ba
DS
781 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
782 IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
35a000b7 783
89879413 784 if (chan->flags & IEEE80211_CHAN_NO_HT40)
762533ba 785 type[index] |=
35a000b7
DS
786 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
787
788 /* scan for all SSIDs from req->ssids */
762533ba 789 type[index] |= cpu_to_le32(ssid_bitmap);
35a000b7
DS
790 }
791}
792
793int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
794 struct ieee80211_vif *vif,
795 struct cfg80211_sched_scan_request *req,
633e2713 796 struct ieee80211_scan_ies *ies)
35a000b7 797{
35a000b7
DS
798 int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
799 int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
800 int head = 0;
35a000b7
DS
801 u32 ssid_bitmap;
802 int cmd_len;
803 int ret;
762533ba 804 u8 *probes;
fb98be5e
DS
805 bool basic_ssid = !(mvm->fw->ucode_capa.flags &
806 IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
35a000b7
DS
807
808 struct iwl_scan_offload_cfg *scan_cfg;
809 struct iwl_host_cmd cmd = {
810 .id = SCAN_OFFLOAD_CONFIG_CMD,
35a000b7 811 };
50df8a30 812 struct iwl_mvm_scan_params params = {};
35a000b7
DS
813
814 lockdep_assert_held(&mvm->mutex);
815
35a000b7 816 cmd_len = sizeof(struct iwl_scan_offload_cfg) +
762533ba 817 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
66092537 818 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
35a000b7
DS
819
820 scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
821 if (!scan_cfg)
822 return -ENOMEM;
823
762533ba
DS
824 probes = scan_cfg->data +
825 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
826
ab480030 827 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
50df8a30 828 iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
35a000b7
DS
829 scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
830
fb98be5e
DS
831 iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
832 &ssid_bitmap, basic_ssid);
35a000b7
DS
833 /* build tx frames for supported bands */
834 if (band_2ghz) {
835 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
836 IEEE80211_BAND_2GHZ,
837 &scan_cfg->scan_cmd.tx_cmd[0],
762533ba
DS
838 probes);
839 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
89879413 840 IEEE80211_BAND_2GHZ, &head,
50df8a30 841 ssid_bitmap, &params);
35a000b7
DS
842 }
843 if (band_5ghz) {
844 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
845 IEEE80211_BAND_5GHZ,
846 &scan_cfg->scan_cmd.tx_cmd[1],
762533ba 847 probes +
35a000b7 848 SCAN_OFFLOAD_PROBE_REQ_SIZE);
762533ba 849 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
89879413 850 IEEE80211_BAND_5GHZ, &head,
50df8a30 851 ssid_bitmap, &params);
35a000b7
DS
852 }
853
854 cmd.data[0] = scan_cfg;
855 cmd.len[0] = cmd_len;
856 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
857
858 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
859
860 ret = iwl_mvm_send_cmd(mvm, &cmd);
861 kfree(scan_cfg);
862 return ret;
863}
864
865int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
866 struct cfg80211_sched_scan_request *req)
867{
868 struct iwl_scan_offload_profile *profile;
869 struct iwl_scan_offload_profile_cfg *profile_cfg;
870 struct iwl_scan_offload_blacklist *blacklist;
871 struct iwl_host_cmd cmd = {
872 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
35a000b7
DS
873 .len[1] = sizeof(*profile_cfg),
874 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
875 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
876 };
877 int blacklist_len;
878 int i;
879 int ret;
880
881 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
882 return -EIO;
883
884 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
885 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
886 else
887 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
888
889 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
890 if (!blacklist)
891 return -ENOMEM;
892
893 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
894 if (!profile_cfg) {
895 ret = -ENOMEM;
896 goto free_blacklist;
897 }
898
899 cmd.data[0] = blacklist;
900 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
901 cmd.data[1] = profile_cfg;
902
903 /* No blacklist configuration */
904
905 profile_cfg->num_profiles = req->n_match_sets;
906 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
907 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
908 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
6e0bbe5e
DS
909 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
910 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
35a000b7
DS
911
912 for (i = 0; i < req->n_match_sets; i++) {
913 profile = &profile_cfg->profiles[i];
914 profile->ssid_index = i;
915 /* Support any cipher and auth algorithm */
916 profile->unicast_cipher = 0xff;
917 profile->auth_alg = 0xff;
918 profile->network_type = IWL_NETWORK_TYPE_ANY;
919 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
920 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
921 }
922
923 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
924
925 ret = iwl_mvm_send_cmd(mvm, &cmd);
926 kfree(profile_cfg);
927free_blacklist:
928 kfree(blacklist);
929
930 return ret;
931}
932
933int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
934 struct cfg80211_sched_scan_request *req)
935{
936 struct iwl_scan_offload_req scan_req = {
937 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
938
939 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
b14fc2be 940 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
35a000b7
DS
941 .schedule_line[0].full_scan_mul = 1,
942
943 .schedule_line[1].iterations = 0xff,
b14fc2be 944 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
35a000b7
DS
945 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
946 };
947
948 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
949 IWL_DEBUG_SCAN(mvm,
950 "Sending scheduled scan with filtering, filter len %d\n",
951 req->n_match_sets);
35a000b7
DS
952 } else {
953 IWL_DEBUG_SCAN(mvm,
954 "Sending Scheduled scan without filtering\n");
de33fb5e 955 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
35a000b7
DS
956 }
957
e820c2da
HD
958 if (mvm->last_ebs_successful &&
959 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
960 scan_req.flags |=
961 cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
962
a1022927 963 return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
35a000b7
DS
964 sizeof(scan_req), &scan_req);
965}
966
fb98be5e 967static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
35a000b7
DS
968{
969 int ret;
970 struct iwl_host_cmd cmd = {
971 .id = SCAN_OFFLOAD_ABORT_CMD,
35a000b7
DS
972 };
973 u32 status;
974
975 /* Exit instantly with error when device is not ready
976 * to receive scan abort command or it does not perform
977 * scheduled scan currently */
fb98be5e
DS
978 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
979 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
980 mvm->scan_status != IWL_MVM_SCAN_OS))
35a000b7
DS
981 return -EIO;
982
983 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
984 if (ret)
985 return ret;
986
987 if (status != CAN_ABORT_STATUS) {
988 /*
989 * The scan abort will return 1 for success or
990 * 2 for "failure". A failure condition can be
991 * due to simply not being in an active scan which
992 * can occur if we send the scan abort before the
993 * microcode has notified us that a scan is completed.
994 */
995 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
33ea27f6 996 ret = -ENOENT;
35a000b7
DS
997 }
998
999 return ret;
1000}
1001
fb98be5e 1002int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
35a000b7
DS
1003{
1004 int ret;
33ea27f6
AN
1005 struct iwl_notification_wait wait_scan_done;
1006 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
fb98be5e 1007 bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
35a000b7
DS
1008
1009 lockdep_assert_held(&mvm->mutex);
1010
fb98be5e
DS
1011 if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1012 (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1013 mvm->scan_status != IWL_MVM_SCAN_OS)) {
1014 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
33ea27f6 1015 return 0;
35a000b7
DS
1016 }
1017
33ea27f6
AN
1018 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1019 scan_done_notif,
1020 ARRAY_SIZE(scan_done_notif),
1021 NULL, NULL);
1022
fb98be5e 1023 ret = iwl_mvm_send_scan_offload_abort(mvm);
33ea27f6 1024 if (ret) {
fb98be5e
DS
1025 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1026 sched ? "offloaded " : "", ret);
33ea27f6
AN
1027 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1028 return ret;
1029 }
1030
fb98be5e
DS
1031 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1032 sched ? "offloaded " : "");
33ea27f6
AN
1033
1034 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1035 if (ret)
1036 return ret;
1037
1038 /*
1039 * Clear the scan status so the next scan requests will succeed. This
1040 * also ensures the Rx handler doesn't do anything, as the scan was
1041 * stopped from above.
1042 */
1043 mvm->scan_status = IWL_MVM_SCAN_NONE;
1044
fb98be5e
DS
1045 if (notify) {
1046 if (sched)
1047 ieee80211_sched_scan_stopped(mvm->hw);
1048 else
1049 ieee80211_scan_completed(mvm->hw, true);
1050 }
636a2cdc 1051
33ea27f6 1052 return 0;
35a000b7 1053}
fb98be5e
DS
1054
1055static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1056 struct iwl_scan_req_tx_cmd *tx_cmd,
1057 bool no_cck)
1058{
1059 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1060 TX_CMD_FLG_BT_DIS);
1061 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1062 IEEE80211_BAND_2GHZ,
1063 no_cck);
1064 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1065
1066 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1067 TX_CMD_FLG_BT_DIS);
1068 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1069 IEEE80211_BAND_5GHZ,
1070 no_cck);
1071 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1072}
1073
1074static void
1075iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1076 struct ieee80211_channel **channels,
1077 int n_channels, u32 ssid_bitmap,
1078 struct iwl_scan_req_unified_lmac *cmd)
1079{
1080 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1081 int i;
1082
1083 for (i = 0; i < n_channels; i++) {
1084 channel_cfg[i].channel_num =
1085 cpu_to_le16(channels[i]->hw_value);
1086 channel_cfg[i].iter_count = cpu_to_le16(1);
1087 channel_cfg[i].iter_interval = 0;
1088 channel_cfg[i].flags =
1089 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1090 ssid_bitmap);
1091 }
1092}
1093
1094static void
1095iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1096 struct ieee80211_scan_ies *ies,
1097 struct iwl_scan_req_unified_lmac *cmd)
1098{
1099 struct iwl_scan_probe_req *preq = (void *)(cmd->data +
1100 sizeof(struct iwl_scan_channel_cfg_lmac) *
1101 mvm->fw->ucode_capa.n_scan_channels);
1102 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
1103 u8 *pos;
1104
1105 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1106 eth_broadcast_addr(frame->da);
1107 memcpy(frame->sa, vif->addr, ETH_ALEN);
1108 eth_broadcast_addr(frame->bssid);
1109 frame->seq_ctrl = 0;
1110
1111 pos = frame->u.probe_req.variable;
1112 *pos++ = WLAN_EID_SSID;
1113 *pos++ = 0;
1114
1115 preq->mac_header.offset = 0;
1116 preq->mac_header.len = cpu_to_le16(24 + 2);
1117
1118 memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
1119 ies->len[IEEE80211_BAND_2GHZ]);
1120 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
1121 preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
1122 pos += ies->len[IEEE80211_BAND_2GHZ];
1123
1124 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1125 ies->len[IEEE80211_BAND_5GHZ]);
1126 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1127 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1128 pos += ies->len[IEEE80211_BAND_5GHZ];
1129
1130 memcpy(pos, ies->common_ies, ies->common_ie_len);
1131 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1132 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1133}
1134
1135static void
1136iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1137 struct iwl_scan_req_unified_lmac *cmd,
1138 struct iwl_mvm_scan_params *params)
1139{
af91344c 1140 memset(cmd, 0, ksize(cmd));
2ce89cd6
DS
1141 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1142 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1143 if (params->passive_fragmented)
1144 cmd->fragmented_dwell =
1145 params->dwell[IEEE80211_BAND_2GHZ].passive;
fb98be5e
DS
1146 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1147 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1148 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1149 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
fb98be5e 1150 cmd->iter_num = cpu_to_le32(1);
af91344c
DS
1151
1152 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1153 mvm->last_ebs_successful) {
1154 cmd->channel_opt[0].flags =
1155 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1156 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1157 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1158 cmd->channel_opt[1].flags =
1159 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1160 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1161 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1162 }
73897bd1
AO
1163
1164 if (iwl_mvm_rrm_scan_needed(mvm))
1165 cmd->scan_flags |=
1166 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
fb98be5e
DS
1167}
1168
1169int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1170 struct ieee80211_vif *vif,
1171 struct ieee80211_scan_request *req)
1172{
1173 struct iwl_host_cmd hcmd = {
1174 .id = SCAN_OFFLOAD_REQUEST_CMD,
1175 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1176 sizeof(struct iwl_scan_channel_cfg_lmac) *
1177 mvm->fw->ucode_capa.n_scan_channels +
1178 sizeof(struct iwl_scan_probe_req), },
1179 .data = { mvm->scan_cmd, },
1180 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1181 };
1182 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1183 struct iwl_mvm_scan_params params = {};
1184 u32 flags;
1185 int ssid_bitmap = 0;
1186 int ret, i;
1187
1188 lockdep_assert_held(&mvm->mutex);
1189
1190 /* we should have failed registration if scan_cmd was NULL */
1191 if (WARN_ON(mvm->scan_cmd == NULL))
1192 return -ENOMEM;
1193
1194 if (WARN_ON_ONCE(req->req.n_ssids > PROBE_OPTION_MAX ||
1195 req->ies.common_ie_len + req->ies.len[0] +
1196 req->ies.len[1] + 24 + 2 >
1197 SCAN_OFFLOAD_PROBE_REQ_SIZE ||
1198 req->req.n_channels >
1199 mvm->fw->ucode_capa.n_scan_channels))
1200 return -1;
1201
1202 mvm->scan_status = IWL_MVM_SCAN_OS;
1203
1204 iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1205 &params);
1206
1207 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1208
1209 cmd->n_channels = (u8)req->req.n_channels;
1210
1211 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1212
1213 if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1214 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1215
1216 if (params.passive_fragmented)
1217 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1218
1219 if (req->req.n_ssids == 0)
1220 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1221
73897bd1 1222 cmd->scan_flags |= cpu_to_le32(flags);
fb98be5e
DS
1223
1224 cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1225 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1226 MAC_FILTER_IN_BEACON);
1227 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1228 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1229 req->req.n_ssids, 0);
1230
1231 cmd->schedule[0].delay = 0;
1232 cmd->schedule[0].iterations = 1;
1233 cmd->schedule[0].full_scan_mul = 0;
1234 cmd->schedule[1].delay = 0;
1235 cmd->schedule[1].iterations = 0;
1236 cmd->schedule[1].full_scan_mul = 0;
1237
1238 for (i = 1; i <= req->req.n_ssids; i++)
1239 ssid_bitmap |= BIT(i);
1240
1241 iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1242 req->req.n_channels, ssid_bitmap,
1243 cmd);
1244
1245 iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
1246
1247 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1248 if (!ret) {
1249 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1250 } else {
1251 /*
1252 * If the scan failed, it usually means that the FW was unable
1253 * to allocate the time events. Warn on it, but maybe we
1254 * should try to send the command again with different params.
1255 */
1256 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1257 mvm->scan_status = IWL_MVM_SCAN_NONE;
1258 ret = -EIO;
1259 }
1260 return ret;
1261}
1262
1263int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1264 struct ieee80211_vif *vif,
1265 struct cfg80211_sched_scan_request *req,
1266 struct ieee80211_scan_ies *ies)
1267{
1268 struct iwl_host_cmd hcmd = {
1269 .id = SCAN_OFFLOAD_REQUEST_CMD,
1270 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1271 sizeof(struct iwl_scan_channel_cfg_lmac) *
1272 mvm->fw->ucode_capa.n_scan_channels +
1273 sizeof(struct iwl_scan_probe_req), },
1274 .data = { mvm->scan_cmd, },
1275 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1276 };
1277 struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1278 struct iwl_mvm_scan_params params = {};
1279 int ret;
1280 u32 flags = 0, ssid_bitmap = 0;
1281
1282 lockdep_assert_held(&mvm->mutex);
1283
1284 /* we should have failed registration if scan_cmd was NULL */
1285 if (WARN_ON(mvm->scan_cmd == NULL))
1286 return -ENOMEM;
1287
1288 if (WARN_ON_ONCE(req->n_ssids > PROBE_OPTION_MAX ||
1289 ies->common_ie_len + ies->len[0] + ies->len[1] + 24 + 2
1290 > SCAN_OFFLOAD_PROBE_REQ_SIZE ||
1291 req->n_channels > mvm->fw->ucode_capa.n_scan_channels))
1292 return -ENOBUFS;
1293
1294 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1295
1296 iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1297
1298 cmd->n_channels = (u8)req->n_channels;
1299
1300 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
1301 IWL_DEBUG_SCAN(mvm,
1302 "Sending scheduled scan with filtering, n_match_sets %d\n",
1303 req->n_match_sets);
1304 } else {
1305 IWL_DEBUG_SCAN(mvm,
1306 "Sending Scheduled scan without filtering\n");
1307 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1308 }
1309
1310 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1311 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1312
1313 if (params.passive_fragmented)
1314 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1315
1316 if (req->n_ssids == 0)
1317 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1318
73897bd1 1319 cmd->scan_flags |= cpu_to_le32(flags);
fb98be5e
DS
1320
1321 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1322 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1323 MAC_FILTER_IN_BEACON);
1324 iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1325 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1326
b14fc2be 1327 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
fb98be5e
DS
1328 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1329 cmd->schedule[0].full_scan_mul = 1;
1330
b14fc2be 1331 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
fb98be5e
DS
1332 cmd->schedule[1].iterations = 0xff;
1333 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1334
1335 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1336 ssid_bitmap, cmd);
1337
1338 iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
1339
1340 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1341 if (!ret) {
1342 IWL_DEBUG_SCAN(mvm,
1343 "Sched scan request was sent successfully\n");
1344 } else {
1345 /*
1346 * If the scan failed, it usually means that the FW was unable
1347 * to allocate the time events. Warn on it, but maybe we
1348 * should try to send the command again with different params.
1349 */
1350 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1351 mvm->scan_status = IWL_MVM_SCAN_NONE;
1352 ret = -EIO;
1353 }
1354 return ret;
1355}
1356
1357
1358int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1359{
1360 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1361 return iwl_mvm_scan_offload_stop(mvm, true);
1362 return iwl_mvm_cancel_regular_scan(mvm);
1363}