blob: cbed9f31758c2f789af5b957e5def172211a3b69 [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.
Luciano Coelho9af91f42015-02-10 10:42:26 +02009 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Luciano Coelho9af91f42015-02-10 10:42:26 +020035 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/etherdevice.h>
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "iwl-eeprom-parse.h"
71#include "fw-api-scan.h"
72
73#define IWL_PLCP_QUIET_THRESH 1
74#define IWL_ACTIVE_QUIET_TIME 10
Haim Dreyfussa1ed4022014-12-01 23:30:07 +020075#define IWL_DENSE_EBS_SCAN_RATIO 5
76#define IWL_SPARSE_EBS_SCAN_RATIO 1
Alexander Bondar8a110d92014-03-12 17:31:19 +020077
78struct iwl_mvm_scan_params {
79 u32 max_out_time;
80 u32 suspend_time;
Alexander Bondar50df8a32014-03-12 20:30:51 +020081 bool passive_fragmented;
82 struct _dwell {
83 u16 passive;
84 u16 active;
David Spinadel190f1022015-02-17 12:45:21 +020085 u16 fragmented;
Alexander Bondar50df8a32014-03-12 20:30:51 +020086 } dwell[IEEE80211_NUM_BANDS];
Alexander Bondar8a110d92014-03-12 17:31:19 +020087};
Johannes Berg8ca151b2013-01-24 14:25:36 +010088
David Spinadeld2496222014-05-20 12:46:37 +030089enum iwl_umac_scan_uid_type {
90 IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
91 IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
92 IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
93 IWL_UMAC_SCAN_UID_SCHED_SCAN,
94};
95
96static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
97 enum iwl_umac_scan_uid_type type, bool notify);
98
99static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
100{
101 if (mvm->scan_rx_ant != ANT_NONE)
102 return mvm->scan_rx_ant;
Moshe Harela0544272014-12-08 21:13:14 +0200103 return iwl_mvm_get_valid_rx_ant(mvm);
David Spinadeld2496222014-05-20 12:46:37 +0300104}
105
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
107{
108 u16 rx_chain;
Oren Givon91b05d12013-08-19 08:36:48 +0300109 u8 rx_ant;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110
David Spinadeld2496222014-05-20 12:46:37 +0300111 rx_ant = iwl_mvm_scan_rx_ant(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
113 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
114 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
115 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
116 return cpu_to_le16(rx_chain);
117}
118
David Spinadelfb98be52014-05-04 12:51:10 +0300119static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100120{
David Spinadelfb98be52014-05-04 12:51:10 +0300121 if (band == IEEE80211_BAND_2GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100122 return cpu_to_le32(PHY_BAND_24);
123 else
124 return cpu_to_le32(PHY_BAND_5);
125}
126
127static inline __le32
128iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
129 bool no_cck)
130{
131 u32 tx_ant;
132
133 mvm->scan_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200134 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100135 mvm->scan_last_antenna_idx);
136 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
137
138 if (band == IEEE80211_BAND_2GHZ && !no_cck)
139 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
140 tx_ant);
141 else
142 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
143}
144
145/*
146 * We insert the SSIDs in an inverted order, because the FW will
147 * invert it back. The most prioritized SSID, which is first in the
148 * request list, is not copied here, but inserted directly to the probe
149 * request.
150 */
David Spinadelfb98be52014-05-04 12:51:10 +0300151static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
152 struct cfg80211_ssid *ssids,
153 int n_ssids, int first)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100154{
155 int fw_idx, req_idx;
156
David Spinadelfb98be52014-05-04 12:51:10 +0300157 for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
David Spinadelfe04e832013-07-04 15:17:48 +0300158 req_idx--, fw_idx++) {
David Spinadelfb98be52014-05-04 12:51:10 +0300159 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
160 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
161 memcpy(cmd_ssid[fw_idx].ssid,
162 ssids[req_idx].ssid,
163 ssids[req_idx].ssid_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100164 }
165}
166
167/*
168 * If req->n_ssids > 0, it means we should do an active scan.
169 * In case of active scan w/o directed scan, we receive a zero-length SSID
170 * just to notify that this scan is active and not passive.
171 * In order to notify the FW of the number of SSIDs we wish to scan (including
172 * the zero-length one), we need to set the corresponding bits in chan->type,
David Spinadel20f1a5d2013-08-21 09:14:27 +0300173 * one for each SSID, and set the active bit (first). If the first SSID is
174 * already included in the probe template, so we need to set only
175 * req->n_ssids - 1 bits in addition to the first bit.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100176 */
David Spinadel3cae0732014-12-29 15:43:48 +0200177static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
178 enum ieee80211_band band, int n_ssids)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100179{
David Spinadel3cae0732014-12-29 15:43:48 +0200180 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
181 return 10;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100182 if (band == IEEE80211_BAND_2GHZ)
David Spinadel39745332014-09-10 16:40:41 +0300183 return 20 + 3 * (n_ssids + 1);
184 return 10 + 2 * (n_ssids + 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185}
186
David Spinadel3cae0732014-12-29 15:43:48 +0200187static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
188 enum ieee80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100189{
David Spinadel3cae0732014-12-29 15:43:48 +0200190 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
191 return 110;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100192 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
193}
194
Alexander Bondar8a110d92014-03-12 17:31:19 +0200195static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
196 struct ieee80211_vif *vif)
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200197{
Alexander Bondar8a110d92014-03-12 17:31:19 +0200198 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200199 int *global_cnt = data;
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200200
David Spinadel1e2ebe02014-10-26 15:53:27 +0200201 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
202 mvmvif->phy_ctxt->id < MAX_PHYS)
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200203 *global_cnt += 1;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200204}
205
206static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200207 struct ieee80211_vif *vif,
Johannes Bergab480032014-06-04 10:13:50 +0200208 int n_ssids, u32 flags,
Alexander Bondar8a110d92014-03-12 17:31:19 +0200209 struct iwl_mvm_scan_params *params)
210{
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200211 int global_cnt = 0;
Alexander Bondar50df8a32014-03-12 20:30:51 +0200212 enum ieee80211_band band;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300213 u8 frag_passive_dwell = 0;
Alexander Bondar8a110d92014-03-12 17:31:19 +0200214
215 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
216 IEEE80211_IFACE_ITER_NORMAL,
217 iwl_mvm_scan_condition_iterator,
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200218 &global_cnt);
Alexander Bondar8a110d92014-03-12 17:31:19 +0200219
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200220 if (!global_cnt)
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300221 goto not_bound;
222
David Spinadel2ce89cd2014-07-22 13:11:18 +0300223 params->suspend_time = 30;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200224 params->max_out_time = 120;
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300225
226 if (iwl_mvm_low_latency(mvm)) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300227 if (mvm->fw->ucode_capa.api[0] &
228 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
229 params->suspend_time = 105;
Haim Dreyfussc3f8d0a2014-12-02 11:03:16 +0200230 /*
231 * If there is more than one active interface make
232 * passive scan more fragmented.
233 */
David Spinadelef177082015-02-15 14:45:33 +0200234 frag_passive_dwell = 40;
Haim Dreyfuss44e9cd72014-12-18 12:34:01 +0200235 params->max_out_time = frag_passive_dwell;
David Spinadel2ce89cd2014-07-22 13:11:18 +0300236 } else {
237 params->suspend_time = 120;
238 params->max_out_time = 120;
239 }
240 }
241
242 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
243 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
244 /*
245 * P2P device scan should not be fragmented to avoid negative
246 * impact on P2P device discovery. Configure max_out_time to be
247 * equal to dwell time on passive channel. Take a longest
248 * possible value, one that corresponds to 2GHz band
249 */
250 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
251 u32 passive_dwell =
David Spinadel3cae0732014-12-29 15:43:48 +0200252 iwl_mvm_get_passive_dwell(mvm,
253 IEEE80211_BAND_2GHZ);
David Spinadel2ce89cd2014-07-22 13:11:18 +0300254 params->max_out_time = passive_dwell;
255 } else {
256 params->passive_fragmented = true;
257 }
Alexander Bondar50df8a32014-03-12 20:30:51 +0200258 }
259
Luciano Coelhoc1537662015-04-09 14:48:11 +0300260 if ((flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
261 (params->max_out_time > 200))
Johannes Bergab480032014-06-04 10:13:50 +0200262 params->max_out_time = 200;
263
Alexander Bondaref67f18d2014-03-30 10:47:08 +0300264not_bound:
265
Alexander Bondar50df8a32014-03-12 20:30:51 +0200266 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
David Spinadel2ce89cd2014-07-22 13:11:18 +0300267 if (params->passive_fragmented)
David Spinadel190f1022015-02-17 12:45:21 +0200268 params->dwell[band].fragmented = frag_passive_dwell;
269
270 params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
271 band);
David Spinadel3cae0732014-12-29 15:43:48 +0200272 params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
Alexander Bondar50df8a32014-03-12 20:30:51 +0200273 n_ssids);
274 }
Luciano Coelho0616c622015-04-09 12:18:56 +0300275
276 IWL_DEBUG_SCAN(mvm,
277 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
278 params->max_out_time, params->suspend_time,
279 params->passive_fragmented);
280 IWL_DEBUG_SCAN(mvm,
281 "dwell[IEEE80211_BAND_2GHZ]: passive %d, active %d, fragmented %d\n",
282 params->dwell[IEEE80211_BAND_2GHZ].passive,
283 params->dwell[IEEE80211_BAND_2GHZ].active,
284 params->dwell[IEEE80211_BAND_2GHZ].fragmented);
285 IWL_DEBUG_SCAN(mvm,
286 "dwell[IEEE80211_BAND_5GHZ]: passive %d, active %d, fragmented %d\n",
287 params->dwell[IEEE80211_BAND_5GHZ].passive,
288 params->dwell[IEEE80211_BAND_5GHZ].active,
289 params->dwell[IEEE80211_BAND_5GHZ].fragmented);
Haim Dreyfuss61f63252013-11-03 23:02:59 +0200290}
291
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300292static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
293{
294 /* require rrm scan whenever the fw supports it */
295 return mvm->fw->ucode_capa.capa[0] &
296 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
297}
298
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300299static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
300 bool is_sched_scan)
301{
302 int max_probe_len;
303
Luciano Coelho1f940382015-02-10 13:03:38 +0200304 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300305
306 /* we create the 802.11 header and SSID element */
307 max_probe_len -= 24 + 2;
308
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300309 /* DS parameter set element is added on 2.4GHZ band if required */
310 if (iwl_mvm_rrm_scan_needed(mvm))
311 max_probe_len -= 3;
312
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300313 return max_probe_len;
314}
315
316int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
317{
318 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
319
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300320 /* TODO: [BUG] This function should return the maximum allowed size of
321 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
322 * in the same command. So the correct implementation of this function
323 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
324 * command has only 512 bytes and it would leave us with about 240
325 * bytes for scan IEs, which is clearly not enough. So meanwhile
326 * we will report an incorrect value. This may result in a failure to
327 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
328 * functions with -ENOBUFS, if a large enough probe will be provided.
329 */
330 return max_ie_len;
331}
332
Alexander Bondare5d74642014-12-09 19:15:49 +0200333int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
334 struct iwl_rx_cmd_buffer *rxb,
335 struct iwl_device_cmd *cmd)
336{
337 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Avraham Stern1083fd72015-03-31 16:14:41 +0300338 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
Alexander Bondare5d74642014-12-09 19:15:49 +0200339
340 IWL_DEBUG_SCAN(mvm,
341 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
342 notif->status, notif->scanned_channels);
343 return 0;
344}
345
David Spinadelfb98be52014-05-04 12:51:10 +0300346int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
347 struct iwl_rx_cmd_buffer *rxb,
348 struct iwl_device_cmd *cmd)
David Spinadel35a000b2013-08-28 09:29:43 +0300349{
David Spinadeld2496222014-05-20 12:46:37 +0300350 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
351 ieee80211_sched_scan_results(mvm->hw);
David Spinadel35a000b2013-08-28 09:29:43 +0300352
353 return 0;
354}
355
David Spinadel35a000b2013-08-28 09:29:43 +0300356int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
357 struct iwl_rx_cmd_buffer *rxb,
358 struct iwl_device_cmd *cmd)
359{
360 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200361 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
362 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
363 bool ebs_successful = (scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS);
David Spinadelfb98be52014-05-04 12:51:10 +0300364
Johannes Berga6623e82014-01-27 15:40:53 +0100365 /* scan status must be locked for proper checking */
366 lockdep_assert_held(&mvm->mutex);
367
Luciano Coelho9af91f42015-02-10 10:42:26 +0200368 /* We first check if we were stopping a scan, in which case we
369 * just clear the stopping flag. Then we check if it was a
370 * firmware initiated stop, in which case we need to inform
371 * mac80211.
372 * Note that we can have a stopping and a running scan
373 * simultaneously, but we can't have two different types of
374 * scans stopping or running at the same time (since LMAC
375 * doesn't support it).
376 */
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300377
Luciano Coelho9af91f42015-02-10 10:42:26 +0200378 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
379 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
David Spinadel35a000b2013-08-28 09:29:43 +0300380
Luciano Coelho9af91f42015-02-10 10:42:26 +0200381 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
382 aborted ? "aborted" : "completed",
383 ebs_successful ? "successful" : "failed");
384
385 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
386 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
387 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
388 aborted ? "aborted" : "completed",
389 ebs_successful ? "successful" : "failed");
390
391 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
392 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
393 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
394
395 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
396 aborted ? "aborted" : "completed",
397 ebs_successful ? "successful" : "failed");
398
399 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200400 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200401 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
402 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
403 aborted ? "aborted" : "completed",
404 ebs_successful ? "successful" : "failed");
405
406 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
David Spinadelfb98be52014-05-04 12:51:10 +0300407 ieee80211_scan_completed(mvm->hw,
Luciano Coelho1f940382015-02-10 13:03:38 +0200408 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
Eliad Peller4ff78182014-06-22 14:44:44 +0300409 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200410 }
David Spinadel35a000b2013-08-28 09:29:43 +0300411
Luciano Coelho9af91f42015-02-10 10:42:26 +0200412 mvm->last_ebs_successful = ebs_successful;
Haim Dreyfusse820c2d2014-04-06 11:19:09 +0300413
David Spinadel35a000b2013-08-28 09:29:43 +0300414 return 0;
415}
416
David Spinadel35a000b2013-08-28 09:29:43 +0300417static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
418{
419 int i;
420
421 for (i = 0; i < PROBE_OPTION_MAX; i++) {
422 if (!ssid_list[i].len)
423 break;
424 if (ssid_list[i].len == ssid_len &&
425 !memcmp(ssid_list->ssid, ssid, ssid_len))
426 return i;
427 }
428 return -1;
429}
430
431static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
David Spinadelfb98be52014-05-04 12:51:10 +0300432 struct iwl_ssid_ie *direct_scan,
433 u32 *ssid_bitmap, bool basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300434{
435 int i, j;
436 int index;
437
438 /*
439 * copy SSIDs from match list.
440 * iwl_config_sched_scan_profiles() uses the order of these ssids to
441 * config match list.
442 */
443 for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
Johannes Bergea73cbc2014-01-24 10:53:53 +0100444 /* skip empty SSID matchsets */
445 if (!req->match_sets[i].ssid.ssid_len)
446 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300447 direct_scan[i].id = WLAN_EID_SSID;
448 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
449 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
450 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300451 }
452
453 /* add SSIDs from scan SSID list */
454 *ssid_bitmap = 0;
455 for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
456 index = iwl_ssid_exist(req->ssids[j].ssid,
457 req->ssids[j].ssid_len,
David Spinadelfb98be52014-05-04 12:51:10 +0300458 direct_scan);
David Spinadel35a000b2013-08-28 09:29:43 +0300459 if (index < 0) {
David Spinadelfb98be52014-05-04 12:51:10 +0300460 if (!req->ssids[j].ssid_len && basic_ssid)
David Spinadel35a000b2013-08-28 09:29:43 +0300461 continue;
David Spinadelfb98be52014-05-04 12:51:10 +0300462 direct_scan[i].id = WLAN_EID_SSID;
463 direct_scan[i].len = req->ssids[j].ssid_len;
464 memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
465 direct_scan[i].len);
David Spinadel35a000b2013-08-28 09:29:43 +0300466 *ssid_bitmap |= BIT(i + 1);
467 i++;
468 } else {
469 *ssid_bitmap |= BIT(index + 1);
470 }
471 }
472}
473
David Spinadel35a000b2013-08-28 09:29:43 +0300474int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
475 struct cfg80211_sched_scan_request *req)
476{
477 struct iwl_scan_offload_profile *profile;
478 struct iwl_scan_offload_profile_cfg *profile_cfg;
479 struct iwl_scan_offload_blacklist *blacklist;
480 struct iwl_host_cmd cmd = {
481 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300482 .len[1] = sizeof(*profile_cfg),
483 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
484 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
485 };
486 int blacklist_len;
487 int i;
488 int ret;
489
490 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
491 return -EIO;
492
493 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
494 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
495 else
496 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
497
498 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
499 if (!blacklist)
500 return -ENOMEM;
501
502 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
503 if (!profile_cfg) {
504 ret = -ENOMEM;
505 goto free_blacklist;
506 }
507
508 cmd.data[0] = blacklist;
509 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
510 cmd.data[1] = profile_cfg;
511
512 /* No blacklist configuration */
513
514 profile_cfg->num_profiles = req->n_match_sets;
515 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
516 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
517 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel6e0bbe52013-12-30 09:59:45 +0200518 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
519 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
David Spinadel35a000b2013-08-28 09:29:43 +0300520
521 for (i = 0; i < req->n_match_sets; i++) {
522 profile = &profile_cfg->profiles[i];
523 profile->ssid_index = i;
524 /* Support any cipher and auth algorithm */
525 profile->unicast_cipher = 0xff;
526 profile->auth_alg = 0xff;
527 profile->network_type = IWL_NETWORK_TYPE_ANY;
528 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
529 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
530 }
531
532 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
533
534 ret = iwl_mvm_send_cmd(mvm, &cmd);
535 kfree(profile_cfg);
536free_blacklist:
537 kfree(blacklist);
538
539 return ret;
540}
541
David Spinadeld2496222014-05-20 12:46:37 +0300542static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
543 struct cfg80211_sched_scan_request *req)
544{
545 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
546 IWL_DEBUG_SCAN(mvm,
547 "Sending scheduled scan with filtering, n_match_sets %d\n",
548 req->n_match_sets);
549 return false;
550 }
551
552 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
553 return true;
554}
555
David Spinadelfb98be52014-05-04 12:51:10 +0300556static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
David Spinadel35a000b2013-08-28 09:29:43 +0300557{
558 int ret;
559 struct iwl_host_cmd cmd = {
560 .id = SCAN_OFFLOAD_ABORT_CMD,
David Spinadel35a000b2013-08-28 09:29:43 +0300561 };
562 u32 status;
563
564 /* Exit instantly with error when device is not ready
565 * to receive scan abort command or it does not perform
566 * scheduled scan currently */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200567 if (!mvm->scan_status)
David Spinadel35a000b2013-08-28 09:29:43 +0300568 return -EIO;
569
570 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
571 if (ret)
572 return ret;
573
574 if (status != CAN_ABORT_STATUS) {
575 /*
576 * The scan abort will return 1 for success or
577 * 2 for "failure". A failure condition can be
578 * due to simply not being in an active scan which
579 * can occur if we send the scan abort before the
580 * microcode has notified us that a scan is completed.
581 */
582 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200583 ret = -ENOENT;
David Spinadel35a000b2013-08-28 09:29:43 +0300584 }
585
586 return ret;
587}
588
David Spinadelfb98be52014-05-04 12:51:10 +0300589int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
David Spinadel35a000b2013-08-28 09:29:43 +0300590{
591 int ret;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200592 struct iwl_notification_wait wait_scan_done;
593 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
Luciano Coelho9af91f42015-02-10 10:42:26 +0200594 bool sched = !!(mvm->scan_status & IWL_MVM_SCAN_SCHED);
David Spinadel35a000b2013-08-28 09:29:43 +0300595
596 lockdep_assert_held(&mvm->mutex);
597
David Spinadeld2496222014-05-20 12:46:37 +0300598 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
599 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
600 notify);
601
Luciano Coelho9af91f42015-02-10 10:42:26 +0200602 /* FIXME: For now we only check if no scan is set here, since
603 * we only support LMAC in this flow and it doesn't support
604 * multiple scans.
605 */
606 if (!mvm->scan_status)
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200607 return 0;
608
Luciano Coelho54331db2015-02-05 00:39:22 +0200609 if (iwl_mvm_is_radio_killed(mvm)) {
610 ret = 0;
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200611 goto out;
Luciano Coelho54331db2015-02-05 00:39:22 +0200612 }
Emmanuel Grumbach90ea15c2015-01-18 21:39:30 +0200613
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200614 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
615 scan_done_notif,
616 ARRAY_SIZE(scan_done_notif),
617 NULL, NULL);
618
David Spinadelfb98be52014-05-04 12:51:10 +0300619 ret = iwl_mvm_send_scan_offload_abort(mvm);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200620 if (ret) {
David Spinadelfb98be52014-05-04 12:51:10 +0300621 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
622 sched ? "offloaded " : "", ret);
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200623 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
Luciano Coelho54331db2015-02-05 00:39:22 +0200624 goto out;
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200625 }
626
David Spinadelfb98be52014-05-04 12:51:10 +0300627 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
Luciano Coelho9af91f42015-02-10 10:42:26 +0200628 sched ? "scheduled " : "");
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200629
630 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
Luciano Coelho54331db2015-02-05 00:39:22 +0200631out:
Luciano Coelho9af91f42015-02-10 10:42:26 +0200632 /* Clear the scan status so the next scan requests will
633 * succeed and mark the scan as stopping, so that the Rx
634 * handler doesn't do anything, as the scan was stopped from
635 * above. Since the rx handler won't do anything now, we have
636 * to release the scan reference here.
Arik Nemtsov33ea27f2014-02-10 15:34:29 +0200637 */
Luciano Coelho9af91f42015-02-10 10:42:26 +0200638 if (mvm->scan_status == IWL_MVM_SCAN_REGULAR)
Eliad Peller4ff78182014-06-22 14:44:44 +0300639 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
640
Luciano Coelho9af91f42015-02-10 10:42:26 +0200641 if (sched) {
642 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
643 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_SCHED;
644 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300645 ieee80211_sched_scan_stopped(mvm->hw);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200646 } else {
647 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
648 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_REGULAR;
649 if (notify)
David Spinadelfb98be52014-05-04 12:51:10 +0300650 ieee80211_scan_completed(mvm->hw, true);
651 }
David Spinadel636a2cd2014-05-01 15:57:22 +0300652
Luciano Coelho54331db2015-02-05 00:39:22 +0200653 return ret;
David Spinadel35a000b2013-08-28 09:29:43 +0300654}
David Spinadelfb98be52014-05-04 12:51:10 +0300655
Luciano Coelho65ff5562015-03-20 13:35:47 +0200656static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
657 struct iwl_scan_req_tx_cmd *tx_cmd,
658 bool no_cck)
David Spinadelfb98be52014-05-04 12:51:10 +0300659{
660 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
661 TX_CMD_FLG_BT_DIS);
662 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
663 IEEE80211_BAND_2GHZ,
664 no_cck);
665 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
666
667 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
668 TX_CMD_FLG_BT_DIS);
669 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
670 IEEE80211_BAND_5GHZ,
671 no_cck);
672 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
673}
674
675static void
676iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
677 struct ieee80211_channel **channels,
678 int n_channels, u32 ssid_bitmap,
Luciano Coelho65ff5562015-03-20 13:35:47 +0200679 struct iwl_scan_req_lmac *cmd)
David Spinadelfb98be52014-05-04 12:51:10 +0300680{
681 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
682 int i;
683
684 for (i = 0; i < n_channels; i++) {
685 channel_cfg[i].channel_num =
686 cpu_to_le16(channels[i]->hw_value);
687 channel_cfg[i].iter_count = cpu_to_le16(1);
688 channel_cfg[i].iter_interval = 0;
689 channel_cfg[i].flags =
690 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
691 ssid_bitmap);
692 }
693}
694
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300695static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
696 size_t len, u8 *const pos)
697{
698 static const u8 before_ds_params[] = {
699 WLAN_EID_SSID,
700 WLAN_EID_SUPP_RATES,
701 WLAN_EID_REQUEST,
702 WLAN_EID_EXT_SUPP_RATES,
703 };
704 size_t offs;
705 u8 *newpos = pos;
706
707 if (!iwl_mvm_rrm_scan_needed(mvm)) {
708 memcpy(newpos, ies, len);
709 return newpos + len;
710 }
711
712 offs = ieee80211_ie_split(ies, len,
713 before_ds_params,
714 ARRAY_SIZE(before_ds_params),
715 0);
716
717 memcpy(newpos, ies, offs);
718 newpos += offs;
719
720 /* Add a placeholder for DS Parameter Set element */
721 *newpos++ = WLAN_EID_DS_PARAMS;
722 *newpos++ = 1;
723 *newpos++ = 0;
724
725 memcpy(newpos, ies + offs, len - offs);
726 newpos += len - offs;
727
728 return newpos;
729}
730
David Spinadelfb98be52014-05-04 12:51:10 +0300731static void
Luciano Coelho65ff5562015-03-20 13:35:47 +0200732iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
733 struct ieee80211_scan_ies *ies,
734 struct iwl_scan_probe_req *preq,
735 const u8 *mac_addr, const u8 *mac_addr_mask)
David Spinadelfb98be52014-05-04 12:51:10 +0300736{
David Spinadelfb98be52014-05-04 12:51:10 +0300737 struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300738 u8 *pos, *newpos;
David Spinadelfb98be52014-05-04 12:51:10 +0300739
Johannes Bergeffd05a2014-11-18 17:21:19 +0100740 /*
741 * Unfortunately, right now the offload scan doesn't support randomising
742 * within the firmware, so until the firmware API is ready we implement
743 * it in the driver. This means that the scan iterations won't really be
744 * random, only when it's restarted, but at least that helps a bit.
745 */
746 if (mac_addr)
747 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
748 else
749 memcpy(frame->sa, vif->addr, ETH_ALEN);
750
David Spinadelfb98be52014-05-04 12:51:10 +0300751 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
752 eth_broadcast_addr(frame->da);
David Spinadelfb98be52014-05-04 12:51:10 +0300753 eth_broadcast_addr(frame->bssid);
754 frame->seq_ctrl = 0;
755
756 pos = frame->u.probe_req.variable;
757 *pos++ = WLAN_EID_SSID;
758 *pos++ = 0;
759
760 preq->mac_header.offset = 0;
761 preq->mac_header.len = cpu_to_le16(24 + 2);
762
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300763 /* Insert ds parameter set element on 2.4 GHz band */
764 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
765 ies->ies[IEEE80211_BAND_2GHZ],
766 ies->len[IEEE80211_BAND_2GHZ],
767 pos);
David Spinadelfb98be52014-05-04 12:51:10 +0300768 preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
Andrei Otcheretianski66dc5272014-09-02 17:55:40 +0300769 preq->band_data[0].len = cpu_to_le16(newpos - pos);
770 pos = newpos;
David Spinadelfb98be52014-05-04 12:51:10 +0300771
772 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
773 ies->len[IEEE80211_BAND_5GHZ]);
774 preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
775 preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
776 pos += ies->len[IEEE80211_BAND_5GHZ];
777
778 memcpy(pos, ies->common_ies, ies->common_ie_len);
779 preq->common_data.offset = cpu_to_le16(pos - preq->buf);
780 preq->common_data.len = cpu_to_le16(ies->common_ie_len);
781}
782
783static void
Luciano Coelho65ff5562015-03-20 13:35:47 +0200784iwl_mvm_build_generic_scan_cmd(struct iwl_mvm *mvm,
785 struct iwl_scan_req_lmac *cmd,
786 struct iwl_mvm_scan_params *params)
David Spinadelfb98be52014-05-04 12:51:10 +0300787{
David Spinadelaf913442014-06-12 19:29:40 +0300788 memset(cmd, 0, ksize(cmd));
David Spinadel2ce89cd2014-07-22 13:11:18 +0300789 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
790 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
791 if (params->passive_fragmented)
792 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +0200793 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadelfb98be52014-05-04 12:51:10 +0300794 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
795 cmd->max_out_time = cpu_to_le32(params->max_out_time);
796 cmd->suspend_time = cpu_to_le32(params->suspend_time);
797 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
David Spinadelfb98be52014-05-04 12:51:10 +0300798 cmd->iter_num = cpu_to_le32(1);
David Spinadelaf913442014-06-12 19:29:40 +0300799
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300800 if (iwl_mvm_rrm_scan_needed(mvm))
801 cmd->scan_flags |=
802 cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
David Spinadelfb98be52014-05-04 12:51:10 +0300803}
804
Luciano Coelho6749dd82015-03-20 15:51:36 +0200805static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
806 struct cfg80211_scan_request *req,
807 struct ieee80211_scan_ies *ies)
David Spinadelfb98be52014-05-04 12:51:10 +0300808{
809 struct iwl_host_cmd hcmd = {
810 .id = SCAN_OFFLOAD_REQUEST_CMD,
Luciano Coelho83c415f2015-03-30 15:09:24 +0300811 .len = { iwl_mvm_scan_size(mvm), },
David Spinadelfb98be52014-05-04 12:51:10 +0300812 .data = { mvm->scan_cmd, },
813 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
814 };
Luciano Coelho65ff5562015-03-20 13:35:47 +0200815 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +0300816 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +0300817 struct iwl_mvm_scan_params params = {};
818 u32 flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +0100819 u32 ssid_bitmap = 0;
David Spinadelfb98be52014-05-04 12:51:10 +0300820 int ret, i;
821
822 lockdep_assert_held(&mvm->mutex);
823
824 /* we should have failed registration if scan_cmd was NULL */
825 if (WARN_ON(mvm->scan_cmd == NULL))
826 return -ENOMEM;
827
Luciano Coelho6749dd82015-03-20 15:51:36 +0200828 if (req->n_ssids > PROBE_OPTION_MAX ||
829 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
830 ies->len[NL80211_BAND_5GHZ] >
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300831 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
Luciano Coelho6749dd82015-03-20 15:51:36 +0200832 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300833 return -ENOBUFS;
David Spinadelfb98be52014-05-04 12:51:10 +0300834
Luciano Coelho9af91f42015-02-10 10:42:26 +0200835 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
David Spinadelfb98be52014-05-04 12:51:10 +0300836
Luciano Coelho6749dd82015-03-20 15:51:36 +0200837 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
David Spinadelfb98be52014-05-04 12:51:10 +0300838 &params);
839
Luciano Coelho65ff5562015-03-20 13:35:47 +0200840 iwl_mvm_build_generic_scan_cmd(mvm, cmd, &params);
David Spinadelfb98be52014-05-04 12:51:10 +0300841
Luciano Coelho6749dd82015-03-20 15:51:36 +0200842 cmd->n_channels = (u8)req->n_channels;
David Spinadelfb98be52014-05-04 12:51:10 +0300843
844 flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
845
Luciano Coelho6749dd82015-03-20 15:51:36 +0200846 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300847 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
848
849 if (params.passive_fragmented)
850 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
851
Luciano Coelho6749dd82015-03-20 15:51:36 +0200852 if (req->n_ssids == 0)
David Spinadelfb98be52014-05-04 12:51:10 +0300853 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
854
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300855 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +0300856
Luciano Coelho6749dd82015-03-20 15:51:36 +0200857 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
David Spinadelfb98be52014-05-04 12:51:10 +0300858 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
859 MAC_FILTER_IN_BEACON);
Luciano Coelho6749dd82015-03-20 15:51:36 +0200860 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->no_cck);
861 iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids,
862 req->n_ssids, 0);
David Spinadelfb98be52014-05-04 12:51:10 +0300863
864 cmd->schedule[0].delay = 0;
865 cmd->schedule[0].iterations = 1;
866 cmd->schedule[0].full_scan_mul = 0;
867 cmd->schedule[1].delay = 0;
868 cmd->schedule[1].iterations = 0;
869 cmd->schedule[1].full_scan_mul = 0;
870
David Spinadel1f9c4182014-12-01 23:30:07 +0200871 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
872 mvm->last_ebs_successful) {
873 cmd->channel_opt[0].flags =
874 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
875 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
876 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
877 cmd->channel_opt[0].non_ebs_ratio =
878 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
879 cmd->channel_opt[1].flags =
880 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
881 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
882 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
883 cmd->channel_opt[1].non_ebs_ratio =
884 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
885 }
886
Luciano Coelho6749dd82015-03-20 15:51:36 +0200887 for (i = 1; i <= req->n_ssids; i++)
David Spinadelfb98be52014-05-04 12:51:10 +0300888 ssid_bitmap |= BIT(i);
889
Luciano Coelho6749dd82015-03-20 15:51:36 +0200890 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels,
891 req->n_channels, ssid_bitmap,
David Spinadelfb98be52014-05-04 12:51:10 +0300892 cmd);
893
David Spinadeld2496222014-05-20 12:46:37 +0300894 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
895 mvm->fw->ucode_capa.n_scan_channels);
896
Luciano Coelho6749dd82015-03-20 15:51:36 +0200897 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
898 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
899 req->mac_addr : NULL,
900 req->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +0300901
902 ret = iwl_mvm_send_cmd(mvm, &hcmd);
903 if (!ret) {
904 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
905 } else {
906 /*
907 * If the scan failed, it usually means that the FW was unable
908 * to allocate the time events. Warn on it, but maybe we
909 * should try to send the command again with different params.
910 */
911 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
Luciano Coelho9af91f42015-02-10 10:42:26 +0200912 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
David Spinadelfb98be52014-05-04 12:51:10 +0300913 ret = -EIO;
914 }
915 return ret;
916}
917
Luciano Coelho65ff5562015-03-20 13:35:47 +0200918static int iwl_mvm_sched_scan_lmac(struct iwl_mvm *mvm,
919 struct ieee80211_vif *vif,
920 struct cfg80211_sched_scan_request *req,
921 struct ieee80211_scan_ies *ies)
David Spinadelfb98be52014-05-04 12:51:10 +0300922{
923 struct iwl_host_cmd hcmd = {
924 .id = SCAN_OFFLOAD_REQUEST_CMD,
Luciano Coelho83c415f2015-03-30 15:09:24 +0300925 .len = { iwl_mvm_scan_size(mvm), },
David Spinadelfb98be52014-05-04 12:51:10 +0300926 .data = { mvm->scan_cmd, },
927 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
928 };
Luciano Coelho65ff5562015-03-20 13:35:47 +0200929 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
David Spinadeld2496222014-05-20 12:46:37 +0300930 struct iwl_scan_probe_req *preq;
David Spinadelfb98be52014-05-04 12:51:10 +0300931 struct iwl_mvm_scan_params params = {};
932 int ret;
933 u32 flags = 0, ssid_bitmap = 0;
934
935 lockdep_assert_held(&mvm->mutex);
936
937 /* we should have failed registration if scan_cmd was NULL */
938 if (WARN_ON(mvm->scan_cmd == NULL))
939 return -ENOMEM;
940
Andrei Otcheretianski48849a42014-09-09 10:58:49 +0300941 if (req->n_ssids > PROBE_OPTION_MAX ||
942 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
943 ies->len[NL80211_BAND_5GHZ] >
944 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
945 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
David Spinadelfb98be52014-05-04 12:51:10 +0300946 return -ENOBUFS;
947
948 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
949
Luciano Coelho65ff5562015-03-20 13:35:47 +0200950 iwl_mvm_build_generic_scan_cmd(mvm, cmd, &params);
David Spinadelfb98be52014-05-04 12:51:10 +0300951
952 cmd->n_channels = (u8)req->n_channels;
953
Luciano Coelho2250fd92014-12-03 10:52:26 +0200954 cmd->delay = cpu_to_le32(req->delay);
955
David Spinadeld2496222014-05-20 12:46:37 +0300956 if (iwl_mvm_scan_pass_all(mvm, req))
David Spinadelfb98be52014-05-04 12:51:10 +0300957 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
David Spinadel7e2a3882014-12-23 14:38:09 +0200958 else
959 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
David Spinadelfb98be52014-05-04 12:51:10 +0300960
961 if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
962 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
963
964 if (params.passive_fragmented)
965 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
966
967 if (req->n_ssids == 0)
968 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
969
Alexander Bondare5d74642014-12-09 19:15:49 +0200970#ifdef CONFIG_IWLWIFI_DEBUGFS
971 if (mvm->scan_iter_notif_enabled)
972 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
973#endif
974
Andrei Otcheretianski73897bd2014-07-09 18:59:14 +0300975 cmd->scan_flags |= cpu_to_le32(flags);
David Spinadelfb98be52014-05-04 12:51:10 +0300976
977 cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
978 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
979 MAC_FILTER_IN_BEACON);
Luciano Coelho65ff5562015-03-20 13:35:47 +0200980 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
David Spinadelfb98be52014-05-04 12:51:10 +0300981 iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
982
David Spinadelb14fc2b2014-06-25 13:17:53 +0300983 cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +0300984 cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
985 cmd->schedule[0].full_scan_mul = 1;
986
David Spinadelb14fc2b2014-06-25 13:17:53 +0300987 cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
David Spinadelfb98be52014-05-04 12:51:10 +0300988 cmd->schedule[1].iterations = 0xff;
989 cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
990
David Spinadel1f9c4182014-12-01 23:30:07 +0200991 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
992 mvm->last_ebs_successful) {
993 cmd->channel_opt[0].flags =
994 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
995 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
996 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
997 cmd->channel_opt[0].non_ebs_ratio =
998 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
999 cmd->channel_opt[1].flags =
1000 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1001 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1002 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1003 cmd->channel_opt[1].non_ebs_ratio =
1004 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1005 }
1006
David Spinadelfb98be52014-05-04 12:51:10 +03001007 iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1008 ssid_bitmap, cmd);
1009
David Spinadeld2496222014-05-20 12:46:37 +03001010 preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1011 mvm->fw->ucode_capa.n_scan_channels);
1012
Luciano Coelho65ff5562015-03-20 13:35:47 +02001013 iwl_mvm_build_scan_probe(mvm, vif, ies, preq,
Johannes Bergeffd05a2014-11-18 17:21:19 +01001014 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1015 req->mac_addr : NULL,
1016 req->mac_addr_mask);
David Spinadelfb98be52014-05-04 12:51:10 +03001017
1018 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1019 if (!ret) {
1020 IWL_DEBUG_SCAN(mvm,
1021 "Sched scan request was sent successfully\n");
1022 } else {
1023 /*
1024 * If the scan failed, it usually means that the FW was unable
1025 * to allocate the time events. Warn on it, but maybe we
1026 * should try to send the command again with different params.
1027 */
1028 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001029 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
David Spinadelfb98be52014-05-04 12:51:10 +03001030 ret = -EIO;
1031 }
1032 return ret;
1033}
1034
1035
1036int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1037{
David Spinadeld2496222014-05-20 12:46:37 +03001038 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1039 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1040 true);
1041
Luciano Coelho9af91f42015-02-10 10:42:26 +02001042 if (!(mvm->scan_status & IWL_MVM_SCAN_REGULAR))
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001043 return 0;
1044
1045 if (iwl_mvm_is_radio_killed(mvm)) {
1046 ieee80211_scan_completed(mvm->hw, true);
1047 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001048 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
Emmanuel Grumbach9b520d82014-11-04 15:54:11 +02001049 return 0;
1050 }
1051
Luciano Coelho1f940382015-02-10 13:03:38 +02001052 return iwl_mvm_scan_offload_stop(mvm, true);
David Spinadelfb98be52014-05-04 12:51:10 +03001053}
David Spinadeld2496222014-05-20 12:46:37 +03001054
1055/* UMAC scan API */
1056
1057struct iwl_umac_scan_done {
1058 struct iwl_mvm *mvm;
1059 enum iwl_umac_scan_uid_type type;
1060};
1061
1062static int rate_to_scan_rate_flag(unsigned int rate)
1063{
1064 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1065 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1066 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1067 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1068 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1069 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1070 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1071 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1072 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1073 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1074 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1075 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1076 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1077 };
1078
1079 return rate_to_scan_rate[rate];
1080}
1081
1082static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1083{
1084 struct ieee80211_supported_band *band;
1085 unsigned int rates = 0;
1086 int i;
1087
1088 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1089 for (i = 0; i < band->n_bitrates; i++)
1090 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1091 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1092 for (i = 0; i < band->n_bitrates; i++)
1093 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1094
1095 /* Set both basic rates and supported rates */
1096 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1097
1098 return cpu_to_le32(rates);
1099}
1100
1101int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1102{
1103
1104 struct iwl_scan_config *scan_config;
1105 struct ieee80211_supported_band *band;
1106 int num_channels =
1107 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1108 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1109 int ret, i, j = 0, cmd_size, data_size;
1110 struct iwl_host_cmd cmd = {
1111 .id = SCAN_CFG_CMD,
1112 };
1113
1114 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1115 return -ENOBUFS;
1116
1117 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1118
1119 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1120 if (!scan_config)
1121 return -ENOMEM;
1122
1123 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1124 scan_config->hdr.size = cpu_to_le16(data_size);
1125 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1126 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1127 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1128 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1129 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1130 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1131 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1132 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1133 SCAN_CONFIG_N_CHANNELS(num_channels));
Moshe Harela0544272014-12-08 21:13:14 +02001134 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
David Spinadeld2496222014-05-20 12:46:37 +03001135 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1136 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1137 scan_config->out_of_channel_time = cpu_to_le32(170);
1138 scan_config->suspend_time = cpu_to_le32(30);
1139 scan_config->dwell_active = 20;
1140 scan_config->dwell_passive = 110;
1141 scan_config->dwell_fragmented = 20;
1142
1143 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1144
1145 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1146 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1147 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1148 IWL_CHANNEL_FLAG_EBS_ADD |
1149 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1150
1151 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1152 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001153 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001154 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1155 for (i = 0; i < band->n_channels; i++, j++)
Ilan Peera25d40e2015-01-23 21:13:01 +02001156 scan_config->channel_array[j] = band->channels[i].hw_value;
David Spinadeld2496222014-05-20 12:46:37 +03001157
1158 cmd.data[0] = scan_config;
1159 cmd.len[0] = cmd_size;
1160 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1161
1162 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1163
1164 ret = iwl_mvm_send_cmd(mvm, &cmd);
1165
1166 kfree(scan_config);
1167 return ret;
1168}
1169
1170static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1171{
1172 int i;
1173
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001174 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001175 if (mvm->scan_uid[i] == uid)
1176 return i;
1177
1178 return i;
1179}
1180
1181static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1182{
1183 return iwl_mvm_find_scan_uid(mvm, 0);
1184}
1185
1186static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1187 enum iwl_umac_scan_uid_type type)
1188{
1189 int i;
1190
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001191 for (i = 0; i < mvm->max_scans; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001192 if (mvm->scan_uid[i] & type)
1193 return true;
1194
1195 return false;
1196}
1197
Alexander Bondar963221b2015-03-26 11:07:35 +02001198static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm,
1199 enum iwl_umac_scan_uid_type type)
1200{
1201 int i;
1202
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001203 for (i = 0; i < mvm->max_scans; i++)
Alexander Bondar963221b2015-03-26 11:07:35 +02001204 if (mvm->scan_uid[i] & type)
1205 return i;
1206
1207 return i;
1208}
1209
David Spinadeld2496222014-05-20 12:46:37 +03001210static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1211 enum iwl_umac_scan_uid_type type)
1212{
1213 u32 uid;
1214
1215 /* make sure exactly one bit is on in scan type */
1216 WARN_ON(hweight8(type) != 1);
1217
1218 /*
1219 * Make sure scan uids are unique. If one scan lasts long time while
1220 * others are completing frequently, the seq number will wrap up and
1221 * we may have more than one scan with the same uid.
1222 */
1223 do {
1224 uid = type | (mvm->scan_seq_num <<
1225 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1226 mvm->scan_seq_num++;
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001227 } while (iwl_mvm_find_scan_uid(mvm, uid) < mvm->max_scans);
David Spinadeld2496222014-05-20 12:46:37 +03001228
1229 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1230
1231 return uid;
1232}
1233
1234static void
1235iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1236 struct iwl_scan_req_umac *cmd,
1237 struct iwl_mvm_scan_params *params)
1238{
1239 memset(cmd, 0, ksize(cmd));
1240 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1241 sizeof(struct iwl_mvm_umac_cmd_hdr));
1242 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1243 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1244 if (params->passive_fragmented)
1245 cmd->fragmented_dwell =
David Spinadel190f1022015-02-17 12:45:21 +02001246 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
David Spinadeld2496222014-05-20 12:46:37 +03001247 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1248 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1249 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1250}
1251
1252static void
1253iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1254 struct ieee80211_channel **channels,
1255 int n_channels, u32 ssid_bitmap,
1256 struct iwl_scan_req_umac *cmd)
1257{
1258 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1259 int i;
1260
1261 for (i = 0; i < n_channels; i++) {
1262 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1263 channel_cfg[i].channel_num = channels[i]->hw_value;
1264 channel_cfg[i].iter_count = 1;
1265 channel_cfg[i].iter_interval = 0;
1266 }
1267}
1268
1269static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1270 struct cfg80211_ssid *ssids,
1271 int fragmented)
1272{
1273 int flags = 0;
1274
1275 if (n_ssids == 0)
1276 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1277
1278 if (n_ssids == 1 && ssids[0].ssid_len != 0)
1279 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1280
1281 if (fragmented)
1282 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1283
1284 if (iwl_mvm_rrm_scan_needed(mvm))
1285 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1286
1287 return flags;
1288}
1289
Luciano Coelho6749dd82015-03-20 15:51:36 +02001290static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1291 struct cfg80211_scan_request *req,
1292 struct ieee80211_scan_ies *ies)
David Spinadeld2496222014-05-20 12:46:37 +03001293{
1294 struct iwl_host_cmd hcmd = {
1295 .id = SCAN_REQ_UMAC,
1296 .len = { iwl_mvm_scan_size(mvm), },
1297 .data = { mvm->scan_cmd, },
1298 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1299 };
1300 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1301 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1302 sizeof(struct iwl_scan_channel_cfg_umac) *
1303 mvm->fw->ucode_capa.n_scan_channels;
1304 struct iwl_mvm_scan_params params = {};
1305 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001306 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001307 int ret, i, uid_idx;
1308
1309 lockdep_assert_held(&mvm->mutex);
1310
1311 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001312 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001313 return -EBUSY;
1314
1315 /* we should have failed registration if scan_cmd was NULL */
1316 if (WARN_ON(mvm->scan_cmd == NULL))
1317 return -ENOMEM;
1318
Luciano Coelho6749dd82015-03-20 15:51:36 +02001319 if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1320 ies->common_ie_len +
1321 ies->len[NL80211_BAND_2GHZ] +
1322 ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1323 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
David Spinadeld2496222014-05-20 12:46:37 +03001324 mvm->fw->ucode_capa.n_scan_channels))
1325 return -ENOBUFS;
1326
Luciano Coelho6749dd82015-03-20 15:51:36 +02001327 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
David Spinadeld2496222014-05-20 12:46:37 +03001328 &params);
1329
1330 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1331
1332 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1333 mvm->scan_uid[uid_idx] = uid;
1334 cmd->uid = cpu_to_le32(uid);
1335
1336 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1337
Luciano Coelho6749dd82015-03-20 15:51:36 +02001338 flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids,
1339 req->ssids,
David Spinadeld2496222014-05-20 12:46:37 +03001340 params.passive_fragmented);
1341
1342 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1343
1344 cmd->general_flags = cpu_to_le32(flags);
Haim Dreyfuss4db65582015-01-05 15:00:35 +02001345
1346 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
1347 mvm->last_ebs_successful)
1348 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1349 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1350 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1351
Luciano Coelho6749dd82015-03-20 15:51:36 +02001352 cmd->n_channels = req->n_channels;
David Spinadeld2496222014-05-20 12:46:37 +03001353
Luciano Coelho6749dd82015-03-20 15:51:36 +02001354 for (i = 0; i < req->n_ssids; i++)
David Spinadeld2496222014-05-20 12:46:37 +03001355 ssid_bitmap |= BIT(i);
1356
Luciano Coelho6749dd82015-03-20 15:51:36 +02001357 iwl_mvm_umac_scan_cfg_channels(mvm, req->channels,
1358 req->n_channels, ssid_bitmap, cmd);
David Spinadeld2496222014-05-20 12:46:37 +03001359
1360 sec_part->schedule[0].iter_count = 1;
1361 sec_part->delay = 0;
1362
Luciano Coelho6749dd82015-03-20 15:51:36 +02001363 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
1364 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1365 req->mac_addr : NULL,
1366 req->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001367
Luciano Coelho6749dd82015-03-20 15:51:36 +02001368 iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->ssids,
1369 req->n_ssids, 0);
David Spinadeld2496222014-05-20 12:46:37 +03001370
1371 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1372 if (!ret) {
1373 IWL_DEBUG_SCAN(mvm,
1374 "Scan request was sent successfully\n");
1375 } else {
1376 /*
1377 * If the scan failed, it usually means that the FW was unable
1378 * to allocate the time events. Warn on it, but maybe we
1379 * should try to send the command again with different params.
1380 */
1381 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1382 }
1383 return ret;
1384}
1385
Luciano Coelho6749dd82015-03-20 15:51:36 +02001386static int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm,
1387 struct ieee80211_vif *vif,
1388 struct cfg80211_sched_scan_request *req,
1389 struct ieee80211_scan_ies *ies)
David Spinadeld2496222014-05-20 12:46:37 +03001390{
1391
1392 struct iwl_host_cmd hcmd = {
1393 .id = SCAN_REQ_UMAC,
1394 .len = { iwl_mvm_scan_size(mvm), },
1395 .data = { mvm->scan_cmd, },
1396 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1397 };
1398 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1399 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1400 sizeof(struct iwl_scan_channel_cfg_umac) *
1401 mvm->fw->ucode_capa.n_scan_channels;
1402 struct iwl_mvm_scan_params params = {};
1403 u32 uid, flags;
Johannes Bergc8660dd2014-11-17 15:06:52 +01001404 u32 ssid_bitmap = 0;
David Spinadeld2496222014-05-20 12:46:37 +03001405 int ret, uid_idx;
1406
1407 lockdep_assert_held(&mvm->mutex);
1408
1409 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001410 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001411 return -EBUSY;
1412
1413 /* we should have failed registration if scan_cmd was NULL */
1414 if (WARN_ON(mvm->scan_cmd == NULL))
1415 return -ENOMEM;
1416
1417 if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1418 ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1419 ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1420 SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
1421 mvm->fw->ucode_capa.n_scan_channels))
1422 return -ENOBUFS;
1423
1424 iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
1425 &params);
1426
1427 iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1428
1429 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1430
1431 uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1432 mvm->scan_uid[uid_idx] = uid;
1433 cmd->uid = cpu_to_le32(uid);
1434
1435 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1436
1437 flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
1438 params.passive_fragmented);
1439
1440 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1441
1442 if (iwl_mvm_scan_pass_all(mvm, req))
1443 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1444 else
1445 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1446
1447 cmd->general_flags = cpu_to_le32(flags);
1448
1449 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1450 mvm->last_ebs_successful)
1451 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1452 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1453 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1454
1455 cmd->n_channels = req->n_channels;
1456
1457 iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
1458 false);
1459
1460 /* This API uses bits 0-19 instead of 1-20. */
1461 ssid_bitmap = ssid_bitmap >> 1;
1462
1463 iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1464 ssid_bitmap, cmd);
1465
1466 sec_part->schedule[0].interval =
1467 cpu_to_le16(req->interval / MSEC_PER_SEC);
1468 sec_part->schedule[0].iter_count = 0xff;
1469
Luciano Coelho2250fd92014-12-03 10:52:26 +02001470 if (req->delay > U16_MAX) {
1471 IWL_DEBUG_SCAN(mvm,
1472 "delay value is > 16-bits, set to max possible\n");
1473 sec_part->delay = cpu_to_le16(U16_MAX);
1474 } else {
1475 sec_part->delay = cpu_to_le16(req->delay);
1476 }
David Spinadeld2496222014-05-20 12:46:37 +03001477
Luciano Coelho65ff5562015-03-20 13:35:47 +02001478 iwl_mvm_build_scan_probe(mvm, vif, ies, &sec_part->preq,
Johannes Bergeffd05a2014-11-18 17:21:19 +01001479 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1480 req->mac_addr : NULL,
1481 req->mac_addr_mask);
David Spinadeld2496222014-05-20 12:46:37 +03001482
1483 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1484 if (!ret) {
1485 IWL_DEBUG_SCAN(mvm,
1486 "Sched scan request was sent successfully\n");
1487 } else {
1488 /*
1489 * If the scan failed, it usually means that the FW was unable
1490 * to allocate the time events. Warn on it, but maybe we
1491 * should try to send the command again with different params.
1492 */
1493 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1494 }
1495 return ret;
1496}
1497
Luciano Coelho6749dd82015-03-20 15:51:36 +02001498static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1499{
1500 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1501}
1502
1503static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1504{
1505 /* This looks a bit arbitrary, but the idea is that if we run
1506 * out of possible simultaneous scans and the userspace is
1507 * trying to run a scan type that is already running, we
1508 * return -EBUSY. But if the userspace wants to start a
1509 * different type of scan, we stop the opposite type to make
1510 * space for the new request. The reason is backwards
1511 * compatibility with old wpa_supplicant that wouldn't stop a
1512 * scheduled scan before starting a normal scan.
1513 */
1514
1515 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1516 return 0;
1517
1518 /* Use a switch, even though this is a bitmask, so that more
1519 * than one bits set will fall in default and we will warn.
1520 */
1521 switch (type) {
1522 case IWL_MVM_SCAN_REGULAR:
1523 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1524 return -EBUSY;
1525 return iwl_mvm_scan_offload_stop(mvm, true);
1526 case IWL_MVM_SCAN_SCHED:
1527 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1528 return -EBUSY;
1529 return iwl_mvm_cancel_scan(mvm);
Luciano Coelho19945df2015-03-20 16:11:28 +02001530 case IWL_MVM_SCAN_NETDETECT:
1531 /* No need to stop anything for net-detect since the
1532 * firmware is restarted anyway. This way, any sched
1533 * scans that were running will be restarted when we
1534 * resume.
1535 */
1536 return 0;
Luciano Coelho6749dd82015-03-20 15:51:36 +02001537 default:
1538 WARN_ON(1);
1539 break;
1540 }
1541
1542 return -EIO;
1543}
1544
1545int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1546 struct cfg80211_scan_request *req,
1547 struct ieee80211_scan_ies *ies)
1548{
1549 int ret;
1550
1551 lockdep_assert_held(&mvm->mutex);
1552
1553 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1554 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1555 return -EBUSY;
1556 }
1557
1558 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1559 if (ret)
1560 return ret;
1561
1562 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1563
1564 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1565 ret = iwl_mvm_scan_umac(mvm, vif, req, ies);
1566 else
1567 ret = iwl_mvm_scan_lmac(mvm, vif, req, ies);
1568
1569 if (ret)
1570 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1571
1572 return ret;
1573}
1574
Luciano Coelho65ff5562015-03-20 13:35:47 +02001575int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1576 struct ieee80211_vif *vif,
1577 struct cfg80211_sched_scan_request *req,
Luciano Coelho19945df2015-03-20 16:11:28 +02001578 struct ieee80211_scan_ies *ies,
1579 int type)
Luciano Coelho65ff5562015-03-20 13:35:47 +02001580{
1581 int ret;
1582
Luciano Coelho6749dd82015-03-20 15:51:36 +02001583 lockdep_assert_held(&mvm->mutex);
1584
1585 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1586 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1587 return -EBUSY;
1588 }
1589
Luciano Coelho19945df2015-03-20 16:11:28 +02001590 ret = iwl_mvm_check_running_scans(mvm, type);
Luciano Coelho6749dd82015-03-20 15:51:36 +02001591 if (ret)
1592 return ret;
1593
Luciano Coelho65ff5562015-03-20 13:35:47 +02001594 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1595 if (ret)
1596 return ret;
1597
1598 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1599 ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
1600 } else {
1601 ret = iwl_mvm_sched_scan_lmac(mvm, vif, req, ies);
1602 if (!ret)
1603 mvm->scan_status |= IWL_MVM_SCAN_SCHED;
1604 }
1605
1606 return ret;
1607}
1608
David Spinadeld2496222014-05-20 12:46:37 +03001609int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1610 struct iwl_rx_cmd_buffer *rxb,
1611 struct iwl_device_cmd *cmd)
1612{
1613 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1614 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1615 u32 uid = __le32_to_cpu(notif->uid);
1616 bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1617 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1618
David Spinadel2992a322014-11-10 11:16:53 +02001619 /*
1620 * Scan uid may be set to zero in case of scan abort request from above.
1621 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001622 if (uid_idx >= mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001623 return 0;
1624
1625 IWL_DEBUG_SCAN(mvm,
1626 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1627 uid, sched ? "sched" : "regular",
1628 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1629 "completed" : "aborted",
1630 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1631 "success" : "failed");
1632
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001633 if (notif->ebs_status)
1634 mvm->last_ebs_successful = false;
1635
David Spinadeld2496222014-05-20 12:46:37 +03001636 mvm->scan_uid[uid_idx] = 0;
1637
1638 if (!sched) {
1639 ieee80211_scan_completed(mvm->hw,
1640 notif->status ==
1641 IWL_SCAN_OFFLOAD_ABORTED);
1642 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1643 } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
1644 ieee80211_sched_scan_stopped(mvm->hw);
1645 } else {
1646 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1647 }
1648
1649 return 0;
1650}
1651
1652static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1653 struct iwl_rx_packet *pkt, void *data)
1654{
1655 struct iwl_umac_scan_done *scan_done = data;
1656 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1657 u32 uid = __le32_to_cpu(notif->uid);
1658 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1659
1660 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1661 return false;
1662
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001663 if (uid_idx >= scan_done->mvm->max_scans)
David Spinadeld2496222014-05-20 12:46:37 +03001664 return false;
1665
1666 /*
1667 * Clear scan uid of scans that was aborted from above and completed
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001668 * in FW so the RX handler does nothing. Set last_ebs_successful here if
1669 * needed.
David Spinadeld2496222014-05-20 12:46:37 +03001670 */
1671 scan_done->mvm->scan_uid[uid_idx] = 0;
1672
Haim Dreyfuss5a4b2af2015-01-13 11:54:51 +02001673 if (notif->ebs_status)
1674 scan_done->mvm->last_ebs_successful = false;
1675
David Spinadeld2496222014-05-20 12:46:37 +03001676 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
1677}
1678
1679static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
1680{
1681 struct iwl_umac_scan_abort cmd = {
1682 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
1683 sizeof(struct iwl_mvm_umac_cmd_hdr)),
1684 .uid = cpu_to_le32(uid),
1685 };
1686
1687 lockdep_assert_held(&mvm->mutex);
1688
1689 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1690
1691 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
1692}
1693
1694static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
1695 enum iwl_umac_scan_uid_type type, bool notify)
1696{
1697 struct iwl_notification_wait wait_scan_done;
1698 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
1699 struct iwl_umac_scan_done scan_done = {
1700 .mvm = mvm,
1701 .type = type,
1702 };
1703 int i, ret = -EIO;
1704
1705 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1706 scan_done_notif,
1707 ARRAY_SIZE(scan_done_notif),
1708 iwl_scan_umac_done_check, &scan_done);
1709
1710 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1711
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001712 for (i = 0; i < mvm->max_scans; i++) {
David Spinadeld2496222014-05-20 12:46:37 +03001713 if (mvm->scan_uid[i] & type) {
1714 int err;
1715
1716 if (iwl_mvm_is_radio_killed(mvm) &&
1717 (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
1718 ieee80211_scan_completed(mvm->hw, true);
1719 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1720 break;
1721 }
1722
1723 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
1724 if (!err)
1725 ret = 0;
1726 }
1727 }
1728
1729 if (ret) {
1730 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
1731 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1732 return ret;
1733 }
1734
1735 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1736 if (ret)
1737 return ret;
1738
1739 if (notify) {
1740 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
1741 ieee80211_sched_scan_stopped(mvm->hw);
1742 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
1743 ieee80211_scan_completed(mvm->hw, true);
1744 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1745 }
1746 }
1747
1748 return ret;
1749}
1750
1751int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1752{
1753 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1754 return sizeof(struct iwl_scan_req_umac) +
1755 sizeof(struct iwl_scan_channel_cfg_umac) *
1756 mvm->fw->ucode_capa.n_scan_channels +
1757 sizeof(struct iwl_scan_req_umac_tail);
1758
Luciano Coelho65ff5562015-03-20 13:35:47 +02001759 return sizeof(struct iwl_scan_req_lmac) +
Luciano Coelho1f940382015-02-10 13:03:38 +02001760 sizeof(struct iwl_scan_channel_cfg_lmac) *
1761 mvm->fw->ucode_capa.n_scan_channels +
1762 sizeof(struct iwl_scan_probe_req);
David Spinadeld2496222014-05-20 12:46:37 +03001763}
David Spinadel4ffb3652015-03-10 10:06:02 +02001764
1765/*
1766 * This function is used in nic restart flow, to inform mac80211 about scans
1767 * that was aborted by restart flow or by an assert.
1768 */
1769void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1770{
1771 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001772 u32 uid, i;
1773
1774 uid = iwl_mvm_find_first_scan(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001775 if (uid < mvm->max_scans) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001776 ieee80211_scan_completed(mvm->hw, true);
Alexander Bondar963221b2015-03-26 11:07:35 +02001777 mvm->scan_uid[uid] = 0;
1778 }
1779 uid = iwl_mvm_find_first_scan(mvm,
1780 IWL_UMAC_SCAN_UID_SCHED_SCAN);
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001781 if (uid < mvm->max_scans && !mvm->restart_fw) {
David Spinadel4ffb3652015-03-10 10:06:02 +02001782 ieee80211_sched_scan_stopped(mvm->hw);
Alexander Bondar963221b2015-03-26 11:07:35 +02001783 mvm->scan_uid[uid] = 0;
1784 }
1785
1786 /* We shouldn't have any UIDs still set. Loop over all the
1787 * UIDs to make sure there's nothing left there and warn if
1788 * any is found.
1789 */
Luciano Coelho507e4cd2015-03-19 22:58:33 +02001790 for (i = 0; i < mvm->max_scans; i++) {
Alexander Bondar963221b2015-03-26 11:07:35 +02001791 if (WARN_ONCE(mvm->scan_uid[i],
1792 "UMAC scan UID %d was not cleaned\n",
1793 mvm->scan_uid[i]))
1794 mvm->scan_uid[i] = 0;
1795 }
David Spinadel4ffb3652015-03-10 10:06:02 +02001796 } else {
Luciano Coelho9af91f42015-02-10 10:42:26 +02001797 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
David Spinadel4ffb3652015-03-10 10:06:02 +02001798 ieee80211_scan_completed(mvm->hw, true);
Luciano Coelho9af91f42015-02-10 10:42:26 +02001799
1800 /* Sched scan will be restarted by mac80211 in
1801 * restart_hw, so do not report if FW is about to be
1802 * restarted.
1803 */
1804 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1805 ieee80211_sched_scan_stopped(mvm->hw);
David Spinadel4ffb3652015-03-10 10:06:02 +02001806 }
1807}