Commit | Line | Data |
---|---|---|
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 | |
a1ed4025 HD |
75 | #define IWL_DENSE_EBS_SCAN_RATIO 5 |
76 | #define IWL_SPARSE_EBS_SCAN_RATIO 1 | |
8a110d9b AB |
77 | |
78 | struct iwl_mvm_scan_params { | |
79 | u32 max_out_time; | |
80 | u32 suspend_time; | |
50df8a30 AB |
81 | bool passive_fragmented; |
82 | struct _dwell { | |
83 | u16 passive; | |
84 | u16 active; | |
85 | } dwell[IEEE80211_NUM_BANDS]; | |
8a110d9b | 86 | }; |
8ca151b5 | 87 | |
d2496221 DS |
88 | enum iwl_umac_scan_uid_type { |
89 | IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0), | |
90 | IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1), | |
91 | IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN | | |
92 | IWL_UMAC_SCAN_UID_SCHED_SCAN, | |
93 | }; | |
94 | ||
95 | static int iwl_umac_scan_stop(struct iwl_mvm *mvm, | |
96 | enum iwl_umac_scan_uid_type type, bool notify); | |
97 | ||
98 | static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm) | |
99 | { | |
100 | if (mvm->scan_rx_ant != ANT_NONE) | |
101 | return mvm->scan_rx_ant; | |
a0544272 | 102 | return iwl_mvm_get_valid_rx_ant(mvm); |
d2496221 DS |
103 | } |
104 | ||
8ca151b5 JB |
105 | static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm) |
106 | { | |
107 | u16 rx_chain; | |
91b05d10 | 108 | u8 rx_ant; |
8ca151b5 | 109 | |
d2496221 | 110 | rx_ant = iwl_mvm_scan_rx_ant(mvm); |
8ca151b5 JB |
111 | rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS; |
112 | rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS; | |
113 | rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS; | |
114 | rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS; | |
115 | return cpu_to_le16(rx_chain); | |
116 | } | |
117 | ||
fb98be5e | 118 | static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band) |
8ca151b5 | 119 | { |
fb98be5e | 120 | if (band == IEEE80211_BAND_2GHZ) |
8ca151b5 JB |
121 | return cpu_to_le32(PHY_BAND_24); |
122 | else | |
123 | return cpu_to_le32(PHY_BAND_5); | |
124 | } | |
125 | ||
126 | static inline __le32 | |
127 | iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band, | |
128 | bool no_cck) | |
129 | { | |
130 | u32 tx_ant; | |
131 | ||
132 | mvm->scan_last_antenna_idx = | |
a0544272 | 133 | iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm), |
8ca151b5 JB |
134 | mvm->scan_last_antenna_idx); |
135 | tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS; | |
136 | ||
137 | if (band == IEEE80211_BAND_2GHZ && !no_cck) | |
138 | return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK | | |
139 | tx_ant); | |
140 | else | |
141 | return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant); | |
142 | } | |
143 | ||
144 | /* | |
145 | * We insert the SSIDs in an inverted order, because the FW will | |
146 | * invert it back. The most prioritized SSID, which is first in the | |
147 | * request list, is not copied here, but inserted directly to the probe | |
148 | * request. | |
149 | */ | |
fb98be5e DS |
150 | static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid, |
151 | struct cfg80211_ssid *ssids, | |
152 | int n_ssids, int first) | |
8ca151b5 JB |
153 | { |
154 | int fw_idx, req_idx; | |
155 | ||
fb98be5e | 156 | for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first; |
fe04e837 | 157 | req_idx--, fw_idx++) { |
fb98be5e DS |
158 | cmd_ssid[fw_idx].id = WLAN_EID_SSID; |
159 | cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len; | |
160 | memcpy(cmd_ssid[fw_idx].ssid, | |
161 | ssids[req_idx].ssid, | |
162 | ssids[req_idx].ssid_len); | |
8ca151b5 JB |
163 | } |
164 | } | |
165 | ||
166 | /* | |
167 | * If req->n_ssids > 0, it means we should do an active scan. | |
168 | * In case of active scan w/o directed scan, we receive a zero-length SSID | |
169 | * just to notify that this scan is active and not passive. | |
170 | * In order to notify the FW of the number of SSIDs we wish to scan (including | |
171 | * the zero-length one), we need to set the corresponding bits in chan->type, | |
20f1a5de DS |
172 | * one for each SSID, and set the active bit (first). If the first SSID is |
173 | * already included in the probe template, so we need to set only | |
174 | * req->n_ssids - 1 bits in addition to the first bit. | |
8ca151b5 | 175 | */ |
3cae0734 DS |
176 | static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm, |
177 | enum ieee80211_band band, int n_ssids) | |
8ca151b5 | 178 | { |
3cae0734 DS |
179 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL) |
180 | return 10; | |
8ca151b5 | 181 | if (band == IEEE80211_BAND_2GHZ) |
39745337 DS |
182 | return 20 + 3 * (n_ssids + 1); |
183 | return 10 + 2 * (n_ssids + 1); | |
8ca151b5 JB |
184 | } |
185 | ||
3cae0734 DS |
186 | static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm, |
187 | enum ieee80211_band band) | |
8ca151b5 | 188 | { |
3cae0734 DS |
189 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL) |
190 | return 110; | |
8ca151b5 JB |
191 | return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10; |
192 | } | |
193 | ||
194 | static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd, | |
20f1a5de | 195 | struct cfg80211_scan_request *req, |
50df8a30 AB |
196 | bool basic_ssid, |
197 | struct iwl_mvm_scan_params *params) | |
8ca151b5 | 198 | { |
8ca151b5 JB |
199 | struct iwl_scan_channel *chan = (struct iwl_scan_channel *) |
200 | (cmd->data + le16_to_cpu(cmd->tx_cmd.len)); | |
201 | int i; | |
20f1a5de | 202 | int type = BIT(req->n_ssids) - 1; |
50df8a30 | 203 | enum ieee80211_band band = req->channels[0]->band; |
20f1a5de DS |
204 | |
205 | if (!basic_ssid) | |
206 | type |= BIT(req->n_ssids); | |
8ca151b5 JB |
207 | |
208 | for (i = 0; i < cmd->channel_count; i++) { | |
209 | chan->channel = cpu_to_le16(req->channels[i]->hw_value); | |
20f1a5de | 210 | chan->type = cpu_to_le32(type); |
8fe02e16 | 211 | if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR) |
bb963c4a | 212 | chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE); |
50df8a30 AB |
213 | chan->active_dwell = cpu_to_le16(params->dwell[band].active); |
214 | chan->passive_dwell = cpu_to_le16(params->dwell[band].passive); | |
8ca151b5 JB |
215 | chan->iteration_count = cpu_to_le16(1); |
216 | chan++; | |
217 | } | |
218 | } | |
219 | ||
220 | /* | |
221 | * Fill in probe request with the following parameters: | |
222 | * TA is our vif HW address, which mac80211 ensures we have. | |
223 | * Packet is broadcasted, so this is both SA and DA. | |
224 | * The probe request IE is made out of two: first comes the most prioritized | |
225 | * SSID if a directed scan is requested. Second comes whatever extra | |
226 | * information was given to us as the scan request IE. | |
227 | */ | |
228 | static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta, | |
229 | int n_ssids, const u8 *ssid, int ssid_len, | |
633e2713 DS |
230 | const u8 *band_ie, int band_ie_len, |
231 | const u8 *common_ie, int common_ie_len, | |
8ca151b5 JB |
232 | int left) |
233 | { | |
234 | int len = 0; | |
235 | u8 *pos = NULL; | |
236 | ||
237 | /* Make sure there is enough space for the probe request, | |
238 | * two mandatory IEs and the data */ | |
239 | left -= 24; | |
240 | if (left < 0) | |
241 | return 0; | |
242 | ||
243 | frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ); | |
244 | eth_broadcast_addr(frame->da); | |
245 | memcpy(frame->sa, ta, ETH_ALEN); | |
246 | eth_broadcast_addr(frame->bssid); | |
247 | frame->seq_ctrl = 0; | |
248 | ||
249 | len += 24; | |
250 | ||
251 | /* for passive scans, no need to fill anything */ | |
252 | if (n_ssids == 0) | |
253 | return (u16)len; | |
254 | ||
255 | /* points to the payload of the request */ | |
256 | pos = &frame->u.probe_req.variable[0]; | |
257 | ||
258 | /* fill in our SSID IE */ | |
259 | left -= ssid_len + 2; | |
260 | if (left < 0) | |
261 | return 0; | |
262 | *pos++ = WLAN_EID_SSID; | |
263 | *pos++ = ssid_len; | |
264 | if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */ | |
265 | memcpy(pos, ssid, ssid_len); | |
266 | pos += ssid_len; | |
267 | } | |
268 | ||
269 | len += ssid_len + 2; | |
270 | ||
633e2713 | 271 | if (WARN_ON(left < band_ie_len + common_ie_len)) |
8ca151b5 JB |
272 | return len; |
273 | ||
633e2713 DS |
274 | if (band_ie && band_ie_len) { |
275 | memcpy(pos, band_ie, band_ie_len); | |
276 | pos += band_ie_len; | |
277 | len += band_ie_len; | |
278 | } | |
279 | ||
280 | if (common_ie && common_ie_len) { | |
281 | memcpy(pos, common_ie, common_ie_len); | |
282 | pos += common_ie_len; | |
283 | len += common_ie_len; | |
8ca151b5 JB |
284 | } |
285 | ||
286 | return (u16)len; | |
287 | } | |
288 | ||
8a110d9b AB |
289 | static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac, |
290 | struct ieee80211_vif *vif) | |
61f6325d | 291 | { |
8a110d9b | 292 | struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); |
c3f8d0a3 | 293 | int *global_cnt = data; |
61f6325d | 294 | |
1e2ebe0e DS |
295 | if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt && |
296 | mvmvif->phy_ctxt->id < MAX_PHYS) | |
c3f8d0a3 | 297 | *global_cnt += 1; |
8a110d9b AB |
298 | } |
299 | ||
300 | static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm, | |
50df8a30 | 301 | struct ieee80211_vif *vif, |
ab480030 | 302 | int n_ssids, u32 flags, |
8a110d9b AB |
303 | struct iwl_mvm_scan_params *params) |
304 | { | |
c3f8d0a3 | 305 | int global_cnt = 0; |
50df8a30 | 306 | enum ieee80211_band band; |
2ce89cd6 | 307 | u8 frag_passive_dwell = 0; |
8a110d9b AB |
308 | |
309 | ieee80211_iterate_active_interfaces_atomic(mvm->hw, | |
310 | IEEE80211_IFACE_ITER_NORMAL, | |
311 | iwl_mvm_scan_condition_iterator, | |
c3f8d0a3 | 312 | &global_cnt); |
ef67f18d | 313 | |
c3f8d0a3 | 314 | if (!global_cnt) |
ef67f18d AB |
315 | goto not_bound; |
316 | ||
2ce89cd6 | 317 | params->suspend_time = 30; |
44e9cd7e | 318 | params->max_out_time = 120; |
ef67f18d AB |
319 | |
320 | if (iwl_mvm_low_latency(mvm)) { | |
2ce89cd6 DS |
321 | if (mvm->fw->ucode_capa.api[0] & |
322 | IWL_UCODE_TLV_API_FRAGMENTED_SCAN) { | |
323 | params->suspend_time = 105; | |
c3f8d0a3 HD |
324 | /* |
325 | * If there is more than one active interface make | |
326 | * passive scan more fragmented. | |
327 | */ | |
328 | frag_passive_dwell = (global_cnt < 2) ? 40 : 20; | |
44e9cd7e | 329 | params->max_out_time = frag_passive_dwell; |
2ce89cd6 DS |
330 | } else { |
331 | params->suspend_time = 120; | |
332 | params->max_out_time = 120; | |
333 | } | |
334 | } | |
335 | ||
336 | if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] & | |
337 | IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) { | |
338 | /* | |
339 | * P2P device scan should not be fragmented to avoid negative | |
340 | * impact on P2P device discovery. Configure max_out_time to be | |
341 | * equal to dwell time on passive channel. Take a longest | |
342 | * possible value, one that corresponds to 2GHz band | |
343 | */ | |
344 | if (vif->type == NL80211_IFTYPE_P2P_DEVICE) { | |
345 | u32 passive_dwell = | |
3cae0734 DS |
346 | iwl_mvm_get_passive_dwell(mvm, |
347 | IEEE80211_BAND_2GHZ); | |
2ce89cd6 DS |
348 | params->max_out_time = passive_dwell; |
349 | } else { | |
350 | params->passive_fragmented = true; | |
351 | } | |
50df8a30 | 352 | } |
8a110d9b | 353 | |
ab480030 JB |
354 | if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY) |
355 | params->max_out_time = 200; | |
356 | ||
ef67f18d AB |
357 | not_bound: |
358 | ||
50df8a30 | 359 | for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) { |
2ce89cd6 DS |
360 | if (params->passive_fragmented) |
361 | params->dwell[band].passive = frag_passive_dwell; | |
362 | else | |
363 | params->dwell[band].passive = | |
3cae0734 DS |
364 | iwl_mvm_get_passive_dwell(mvm, band); |
365 | params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band, | |
50df8a30 AB |
366 | n_ssids); |
367 | } | |
61f6325d HD |
368 | } |
369 | ||
73897bd1 AO |
370 | static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm) |
371 | { | |
372 | /* require rrm scan whenever the fw supports it */ | |
373 | return mvm->fw->ucode_capa.capa[0] & | |
374 | IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT; | |
375 | } | |
376 | ||
48849a41 AO |
377 | static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm, |
378 | bool is_sched_scan) | |
379 | { | |
380 | int max_probe_len; | |
381 | ||
382 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) | |
383 | max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE; | |
384 | else | |
385 | max_probe_len = mvm->fw->ucode_capa.max_probe_length; | |
386 | ||
387 | /* we create the 802.11 header and SSID element */ | |
388 | max_probe_len -= 24 + 2; | |
389 | ||
390 | /* basic ssid is added only for hw_scan with and old api */ | |
391 | if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) && | |
392 | !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) && | |
393 | !is_sched_scan) | |
394 | max_probe_len -= 32; | |
395 | ||
66dc5272 AO |
396 | /* DS parameter set element is added on 2.4GHZ band if required */ |
397 | if (iwl_mvm_rrm_scan_needed(mvm)) | |
398 | max_probe_len -= 3; | |
399 | ||
48849a41 AO |
400 | return max_probe_len; |
401 | } | |
402 | ||
403 | int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan) | |
404 | { | |
405 | int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan); | |
406 | ||
407 | if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) | |
408 | return max_ie_len; | |
409 | ||
410 | /* TODO: [BUG] This function should return the maximum allowed size of | |
411 | * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs | |
412 | * in the same command. So the correct implementation of this function | |
413 | * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan | |
414 | * command has only 512 bytes and it would leave us with about 240 | |
415 | * bytes for scan IEs, which is clearly not enough. So meanwhile | |
416 | * we will report an incorrect value. This may result in a failure to | |
417 | * issue a scan in unified_scan_lmac and unified_sched_scan_lmac | |
418 | * functions with -ENOBUFS, if a large enough probe will be provided. | |
419 | */ | |
420 | return max_ie_len; | |
421 | } | |
422 | ||
8ca151b5 JB |
423 | int iwl_mvm_scan_request(struct iwl_mvm *mvm, |
424 | struct ieee80211_vif *vif, | |
425 | struct cfg80211_scan_request *req) | |
426 | { | |
427 | struct iwl_host_cmd hcmd = { | |
428 | .id = SCAN_REQUEST_CMD, | |
429 | .len = { 0, }, | |
430 | .data = { mvm->scan_cmd, }, | |
8ca151b5 JB |
431 | .dataflags = { IWL_HCMD_DFL_NOCOPY, }, |
432 | }; | |
433 | struct iwl_scan_cmd *cmd = mvm->scan_cmd; | |
434 | int ret; | |
435 | u32 status; | |
436 | int ssid_len = 0; | |
437 | u8 *ssid = NULL; | |
20f1a5de DS |
438 | bool basic_ssid = !(mvm->fw->ucode_capa.flags & |
439 | IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID); | |
8a110d9b | 440 | struct iwl_mvm_scan_params params = {}; |
8ca151b5 JB |
441 | |
442 | lockdep_assert_held(&mvm->mutex); | |
748fa67c EG |
443 | |
444 | /* we should have failed registration if scan_cmd was NULL */ | |
445 | if (WARN_ON(mvm->scan_cmd == NULL)) | |
446 | return -ENOMEM; | |
8ca151b5 JB |
447 | |
448 | IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n"); | |
449 | mvm->scan_status = IWL_MVM_SCAN_OS; | |
0564679b | 450 | memset(cmd, 0, ksize(cmd)); |
8a110d9b | 451 | |
8ca151b5 JB |
452 | cmd->channel_count = (u8)req->n_channels; |
453 | cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME); | |
454 | cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH); | |
455 | cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm); | |
8a110d9b | 456 | |
ab480030 | 457 | iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, ¶ms); |
8a110d9b AB |
458 | cmd->max_out_time = cpu_to_le32(params.max_out_time); |
459 | cmd->suspend_time = cpu_to_le32(params.suspend_time); | |
50df8a30 AB |
460 | if (params.passive_fragmented) |
461 | cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN; | |
8a110d9b | 462 | |
fb98be5e | 463 | cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band); |
8ca151b5 JB |
464 | cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP | |
465 | MAC_FILTER_IN_BEACON); | |
d91b06d2 IP |
466 | |
467 | if (vif->type == NL80211_IFTYPE_P2P_DEVICE) | |
468 | cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED); | |
469 | else | |
470 | cmd->type = cpu_to_le32(SCAN_TYPE_FORCED); | |
471 | ||
8ca151b5 JB |
472 | cmd->repeats = cpu_to_le32(1); |
473 | ||
474 | /* | |
475 | * If the user asked for passive scan, don't change to active scan if | |
476 | * you see any activity on the channel - remain passive. | |
477 | */ | |
478 | if (req->n_ssids > 0) { | |
479 | cmd->passive2active = cpu_to_le16(1); | |
26e05cc3 | 480 | cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE; |
20f1a5de DS |
481 | if (basic_ssid) { |
482 | ssid = req->ssids[0].ssid; | |
483 | ssid_len = req->ssids[0].ssid_len; | |
484 | } | |
8ca151b5 JB |
485 | } else { |
486 | cmd->passive2active = 0; | |
26e05cc3 | 487 | cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE; |
8ca151b5 JB |
488 | } |
489 | ||
fb98be5e DS |
490 | iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids, |
491 | basic_ssid ? 1 : 0); | |
8ca151b5 | 492 | |
8e2a866e | 493 | cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL | |
3856b78c EG |
494 | 3 << TX_CMD_FLG_BT_PRIO_POS); |
495 | ||
8ca151b5 JB |
496 | cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id; |
497 | cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE); | |
498 | cmd->tx_cmd.rate_n_flags = | |
499 | iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band, | |
500 | req->no_cck); | |
501 | ||
502 | cmd->tx_cmd.len = | |
503 | cpu_to_le16(iwl_mvm_fill_probe_req( | |
504 | (struct ieee80211_mgmt *)cmd->data, | |
505 | vif->addr, | |
506 | req->n_ssids, ssid, ssid_len, | |
633e2713 | 507 | req->ie, req->ie_len, NULL, 0, |
8ca151b5 JB |
508 | mvm->fw->ucode_capa.max_probe_length)); |
509 | ||
50df8a30 | 510 | iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, ¶ms); |
8ca151b5 JB |
511 | |
512 | cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) + | |
513 | le16_to_cpu(cmd->tx_cmd.len) + | |
514 | (cmd->channel_count * sizeof(struct iwl_scan_channel))); | |
515 | hcmd.len[0] = le16_to_cpu(cmd->len); | |
516 | ||
517 | status = SCAN_RESPONSE_OK; | |
518 | ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status); | |
519 | if (!ret && status == SCAN_RESPONSE_OK) { | |
520 | IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n"); | |
521 | } else { | |
522 | /* | |
523 | * If the scan failed, it usually means that the FW was unable | |
524 | * to allocate the time events. Warn on it, but maybe we | |
525 | * should try to send the command again with different params. | |
526 | */ | |
527 | IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n", | |
528 | status, ret); | |
529 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
530 | ret = -EIO; | |
531 | } | |
532 | return ret; | |
533 | } | |
534 | ||
535 | int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb, | |
536 | struct iwl_device_cmd *cmd) | |
537 | { | |
538 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
539 | struct iwl_cmd_response *resp = (void *)pkt->data; | |
540 | ||
541 | IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n", | |
542 | le32_to_cpu(resp->status)); | |
543 | return 0; | |
544 | } | |
545 | ||
e5d74646 AB |
546 | int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm, |
547 | struct iwl_rx_cmd_buffer *rxb, | |
548 | struct iwl_device_cmd *cmd) | |
549 | { | |
550 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
551 | struct iwl_scan_complete_notif *notif = (void *)pkt->data; | |
552 | ||
553 | IWL_DEBUG_SCAN(mvm, | |
554 | "Scan offload iteration complete: status=0x%x scanned channels=%d\n", | |
555 | notif->status, notif->scanned_channels); | |
556 | return 0; | |
557 | } | |
558 | ||
8ca151b5 JB |
559 | int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb, |
560 | struct iwl_device_cmd *cmd) | |
561 | { | |
562 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
563 | struct iwl_scan_complete_notif *notif = (void *)pkt->data; | |
564 | ||
91b80256 AN |
565 | lockdep_assert_held(&mvm->mutex); |
566 | ||
8ca151b5 JB |
567 | IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n", |
568 | notif->status, notif->scanned_channels); | |
569 | ||
91b80256 AN |
570 | if (mvm->scan_status == IWL_MVM_SCAN_OS) |
571 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
8ca151b5 JB |
572 | ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK); |
573 | ||
519e2026 AN |
574 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); |
575 | ||
8ca151b5 JB |
576 | return 0; |
577 | } | |
578 | ||
fb98be5e DS |
579 | int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm, |
580 | struct iwl_rx_cmd_buffer *rxb, | |
581 | struct iwl_device_cmd *cmd) | |
35a000b7 DS |
582 | { |
583 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
fb98be5e | 584 | |
d2496221 DS |
585 | if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) && |
586 | !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) { | |
fb98be5e | 587 | struct iwl_sched_scan_results *notif = (void *)pkt->data; |
35a000b7 | 588 | |
d2496221 DS |
589 | if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN)) |
590 | return 0; | |
fb98be5e DS |
591 | } |
592 | ||
d2496221 DS |
593 | IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n"); |
594 | ieee80211_sched_scan_results(mvm->hw); | |
35a000b7 DS |
595 | |
596 | return 0; | |
597 | } | |
598 | ||
8ca151b5 JB |
599 | static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait, |
600 | struct iwl_rx_packet *pkt, void *data) | |
601 | { | |
602 | struct iwl_mvm *mvm = | |
603 | container_of(notif_wait, struct iwl_mvm, notif_wait); | |
604 | struct iwl_scan_complete_notif *notif; | |
605 | u32 *resp; | |
606 | ||
607 | switch (pkt->hdr.cmd) { | |
608 | case SCAN_ABORT_CMD: | |
609 | resp = (void *)pkt->data; | |
610 | if (*resp == CAN_ABORT_STATUS) { | |
611 | IWL_DEBUG_SCAN(mvm, | |
612 | "Scan can be aborted, wait until completion\n"); | |
613 | return false; | |
614 | } | |
615 | ||
5a3e9f7f EG |
616 | /* |
617 | * If scan cannot be aborted, it means that we had a | |
618 | * SCAN_COMPLETE_NOTIFICATION in the pipe and it called | |
619 | * ieee80211_scan_completed already. | |
620 | */ | |
8ca151b5 JB |
621 | IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n", |
622 | *resp); | |
623 | return true; | |
624 | ||
625 | case SCAN_COMPLETE_NOTIFICATION: | |
626 | notif = (void *)pkt->data; | |
627 | IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n", | |
628 | notif->status); | |
629 | return true; | |
630 | ||
631 | default: | |
632 | WARN_ON(1); | |
633 | return false; | |
634 | }; | |
635 | } | |
636 | ||
fb98be5e | 637 | static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm) |
8ca151b5 JB |
638 | { |
639 | struct iwl_notification_wait wait_scan_abort; | |
640 | static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD, | |
641 | SCAN_COMPLETE_NOTIFICATION }; | |
642 | int ret; | |
643 | ||
644 | iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort, | |
645 | scan_abort_notif, | |
646 | ARRAY_SIZE(scan_abort_notif), | |
647 | iwl_mvm_scan_abort_notif, NULL); | |
648 | ||
a1022927 | 649 | ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL); |
8ca151b5 JB |
650 | if (ret) { |
651 | IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret); | |
992f81fc | 652 | /* mac80211's state will be cleaned in the nic_restart flow */ |
8ca151b5 JB |
653 | goto out_remove_notif; |
654 | } | |
655 | ||
91b80256 | 656 | return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ); |
8ca151b5 JB |
657 | |
658 | out_remove_notif: | |
659 | iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort); | |
91b80256 | 660 | return ret; |
8ca151b5 | 661 | } |
35a000b7 DS |
662 | |
663 | int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm, | |
664 | struct iwl_rx_cmd_buffer *rxb, | |
665 | struct iwl_device_cmd *cmd) | |
666 | { | |
667 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
fb98be5e DS |
668 | u8 status, ebs_status; |
669 | ||
670 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) { | |
671 | struct iwl_periodic_scan_complete *scan_notif; | |
35a000b7 | 672 | |
fb98be5e DS |
673 | scan_notif = (void *)pkt->data; |
674 | status = scan_notif->status; | |
675 | ebs_status = scan_notif->ebs_status; | |
676 | } else { | |
677 | struct iwl_scan_offload_complete *scan_notif; | |
678 | ||
679 | scan_notif = (void *)pkt->data; | |
680 | status = scan_notif->status; | |
681 | ebs_status = scan_notif->ebs_status; | |
682 | } | |
a6623e84 JB |
683 | /* scan status must be locked for proper checking */ |
684 | lockdep_assert_held(&mvm->mutex); | |
685 | ||
e820c2da | 686 | IWL_DEBUG_SCAN(mvm, |
fb98be5e DS |
687 | "%s completed, status %s, EBS status %s\n", |
688 | mvm->scan_status == IWL_MVM_SCAN_SCHED ? | |
689 | "Scheduled scan" : "Scan", | |
690 | status == IWL_SCAN_OFFLOAD_COMPLETED ? | |
691 | "completed" : "aborted", | |
692 | ebs_status == IWL_SCAN_EBS_SUCCESS ? | |
693 | "success" : "failed"); | |
e820c2da | 694 | |
35a000b7 | 695 | |
33ea27f6 AN |
696 | /* only call mac80211 completion if the stop was initiated by FW */ |
697 | if (mvm->scan_status == IWL_MVM_SCAN_SCHED) { | |
a6623e84 | 698 | mvm->scan_status = IWL_MVM_SCAN_NONE; |
33ea27f6 | 699 | ieee80211_sched_scan_stopped(mvm->hw); |
fb98be5e DS |
700 | } else if (mvm->scan_status == IWL_MVM_SCAN_OS) { |
701 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
702 | ieee80211_scan_completed(mvm->hw, | |
703 | status == IWL_SCAN_OFFLOAD_ABORTED); | |
4ff78181 | 704 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); |
33ea27f6 | 705 | } |
35a000b7 | 706 | |
fb98be5e | 707 | mvm->last_ebs_successful = !ebs_status; |
e820c2da | 708 | |
35a000b7 DS |
709 | return 0; |
710 | } | |
711 | ||
712 | static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm, | |
713 | struct ieee80211_vif *vif, | |
633e2713 | 714 | struct ieee80211_scan_ies *ies, |
35a000b7 DS |
715 | enum ieee80211_band band, |
716 | struct iwl_tx_cmd *cmd, | |
717 | u8 *data) | |
718 | { | |
719 | u16 cmd_len; | |
720 | ||
721 | cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL); | |
722 | cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE); | |
723 | cmd->sta_id = mvm->aux_sta.sta_id; | |
724 | ||
725 | cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false); | |
726 | ||
727 | cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data, | |
728 | vif->addr, | |
729 | 1, NULL, 0, | |
633e2713 DS |
730 | ies->ies[band], ies->len[band], |
731 | ies->common_ies, ies->common_ie_len, | |
35a000b7 DS |
732 | SCAN_OFFLOAD_PROBE_REQ_SIZE); |
733 | cmd->len = cpu_to_le16(cmd_len); | |
734 | } | |
735 | ||
736 | static void iwl_build_scan_cmd(struct iwl_mvm *mvm, | |
737 | struct ieee80211_vif *vif, | |
738 | struct cfg80211_sched_scan_request *req, | |
50df8a30 AB |
739 | struct iwl_scan_offload_cmd *scan, |
740 | struct iwl_mvm_scan_params *params) | |
35a000b7 | 741 | { |
89879413 | 742 | scan->channel_count = req->n_channels; |
35a000b7 DS |
743 | scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME); |
744 | scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH); | |
745 | scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT; | |
746 | scan->rx_chain = iwl_mvm_scan_rx_chain(mvm); | |
8a110d9b | 747 | |
50df8a30 AB |
748 | scan->max_out_time = cpu_to_le32(params->max_out_time); |
749 | scan->suspend_time = cpu_to_le32(params->suspend_time); | |
8a110d9b | 750 | |
35a000b7 DS |
751 | scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP | |
752 | MAC_FILTER_IN_BEACON); | |
753 | scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND); | |
754 | scan->rep_count = cpu_to_le32(1); | |
50df8a30 AB |
755 | |
756 | if (params->passive_fragmented) | |
757 | scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN; | |
35a000b7 DS |
758 | } |
759 | ||
760 | static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list) | |
761 | { | |
762 | int i; | |
763 | ||
764 | for (i = 0; i < PROBE_OPTION_MAX; i++) { | |
765 | if (!ssid_list[i].len) | |
766 | break; | |
767 | if (ssid_list[i].len == ssid_len && | |
768 | !memcmp(ssid_list->ssid, ssid, ssid_len)) | |
769 | return i; | |
770 | } | |
771 | return -1; | |
772 | } | |
773 | ||
774 | static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req, | |
fb98be5e DS |
775 | struct iwl_ssid_ie *direct_scan, |
776 | u32 *ssid_bitmap, bool basic_ssid) | |
35a000b7 DS |
777 | { |
778 | int i, j; | |
779 | int index; | |
780 | ||
781 | /* | |
782 | * copy SSIDs from match list. | |
783 | * iwl_config_sched_scan_profiles() uses the order of these ssids to | |
784 | * config match list. | |
785 | */ | |
786 | for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) { | |
ea73cbce JB |
787 | /* skip empty SSID matchsets */ |
788 | if (!req->match_sets[i].ssid.ssid_len) | |
789 | continue; | |
fb98be5e DS |
790 | direct_scan[i].id = WLAN_EID_SSID; |
791 | direct_scan[i].len = req->match_sets[i].ssid.ssid_len; | |
792 | memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid, | |
793 | direct_scan[i].len); | |
35a000b7 DS |
794 | } |
795 | ||
796 | /* add SSIDs from scan SSID list */ | |
797 | *ssid_bitmap = 0; | |
798 | for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) { | |
799 | index = iwl_ssid_exist(req->ssids[j].ssid, | |
800 | req->ssids[j].ssid_len, | |
fb98be5e | 801 | direct_scan); |
35a000b7 | 802 | if (index < 0) { |
fb98be5e | 803 | if (!req->ssids[j].ssid_len && basic_ssid) |
35a000b7 | 804 | continue; |
fb98be5e DS |
805 | direct_scan[i].id = WLAN_EID_SSID; |
806 | direct_scan[i].len = req->ssids[j].ssid_len; | |
807 | memcpy(direct_scan[i].ssid, req->ssids[j].ssid, | |
808 | direct_scan[i].len); | |
35a000b7 DS |
809 | *ssid_bitmap |= BIT(i + 1); |
810 | i++; | |
811 | } else { | |
812 | *ssid_bitmap |= BIT(index + 1); | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
817 | static void iwl_build_channel_cfg(struct iwl_mvm *mvm, | |
818 | struct cfg80211_sched_scan_request *req, | |
762533ba | 819 | u8 *channels_buffer, |
35a000b7 | 820 | enum ieee80211_band band, |
89879413 | 821 | int *head, |
50df8a30 AB |
822 | u32 ssid_bitmap, |
823 | struct iwl_mvm_scan_params *params) | |
35a000b7 | 824 | { |
762533ba DS |
825 | u32 n_channels = mvm->fw->ucode_capa.n_scan_channels; |
826 | __le32 *type = (__le32 *)channels_buffer; | |
827 | __le16 *channel_number = (__le16 *)(type + n_channels); | |
828 | __le16 *iter_count = channel_number + n_channels; | |
829 | __le32 *iter_interval = (__le32 *)(iter_count + n_channels); | |
830 | u8 *active_dwell = (u8 *)(iter_interval + n_channels); | |
831 | u8 *passive_dwell = active_dwell + n_channels; | |
89879413 | 832 | int i, index = 0; |
35a000b7 | 833 | |
89879413 EP |
834 | for (i = 0; i < req->n_channels; i++) { |
835 | struct ieee80211_channel *chan = req->channels[i]; | |
836 | ||
837 | if (chan->band != band) | |
838 | continue; | |
35a000b7 | 839 | |
89879413 EP |
840 | index = *head; |
841 | (*head)++; | |
842 | ||
762533ba DS |
843 | channel_number[index] = cpu_to_le16(chan->hw_value); |
844 | active_dwell[index] = params->dwell[band].active; | |
845 | passive_dwell[index] = params->dwell[band].passive; | |
35a000b7 | 846 | |
762533ba DS |
847 | iter_count[index] = cpu_to_le16(1); |
848 | iter_interval[index] = 0; | |
35a000b7 | 849 | |
89879413 | 850 | if (!(chan->flags & IEEE80211_CHAN_NO_IR)) |
762533ba | 851 | type[index] |= |
35a000b7 DS |
852 | cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE); |
853 | ||
762533ba DS |
854 | type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL | |
855 | IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL); | |
35a000b7 | 856 | |
89879413 | 857 | if (chan->flags & IEEE80211_CHAN_NO_HT40) |
762533ba | 858 | type[index] |= |
35a000b7 DS |
859 | cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW); |
860 | ||
861 | /* scan for all SSIDs from req->ssids */ | |
762533ba | 862 | type[index] |= cpu_to_le32(ssid_bitmap); |
35a000b7 DS |
863 | } |
864 | } | |
865 | ||
866 | int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm, | |
867 | struct ieee80211_vif *vif, | |
868 | struct cfg80211_sched_scan_request *req, | |
633e2713 | 869 | struct ieee80211_scan_ies *ies) |
35a000b7 | 870 | { |
35a000b7 DS |
871 | int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels; |
872 | int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels; | |
873 | int head = 0; | |
35a000b7 DS |
874 | u32 ssid_bitmap; |
875 | int cmd_len; | |
876 | int ret; | |
762533ba | 877 | u8 *probes; |
fb98be5e DS |
878 | bool basic_ssid = !(mvm->fw->ucode_capa.flags & |
879 | IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID); | |
35a000b7 DS |
880 | |
881 | struct iwl_scan_offload_cfg *scan_cfg; | |
882 | struct iwl_host_cmd cmd = { | |
883 | .id = SCAN_OFFLOAD_CONFIG_CMD, | |
35a000b7 | 884 | }; |
50df8a30 | 885 | struct iwl_mvm_scan_params params = {}; |
35a000b7 DS |
886 | |
887 | lockdep_assert_held(&mvm->mutex); | |
888 | ||
35a000b7 | 889 | cmd_len = sizeof(struct iwl_scan_offload_cfg) + |
762533ba | 890 | mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE + |
66092537 | 891 | 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE; |
35a000b7 DS |
892 | |
893 | scan_cfg = kzalloc(cmd_len, GFP_KERNEL); | |
894 | if (!scan_cfg) | |
895 | return -ENOMEM; | |
896 | ||
762533ba DS |
897 | probes = scan_cfg->data + |
898 | mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE; | |
899 | ||
ab480030 | 900 | iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, ¶ms); |
50df8a30 | 901 | iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, ¶ms); |
35a000b7 DS |
902 | scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len); |
903 | ||
fb98be5e DS |
904 | iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan, |
905 | &ssid_bitmap, basic_ssid); | |
35a000b7 DS |
906 | /* build tx frames for supported bands */ |
907 | if (band_2ghz) { | |
908 | iwl_scan_offload_build_tx_cmd(mvm, vif, ies, | |
909 | IEEE80211_BAND_2GHZ, | |
910 | &scan_cfg->scan_cmd.tx_cmd[0], | |
762533ba DS |
911 | probes); |
912 | iwl_build_channel_cfg(mvm, req, scan_cfg->data, | |
89879413 | 913 | IEEE80211_BAND_2GHZ, &head, |
50df8a30 | 914 | ssid_bitmap, ¶ms); |
35a000b7 DS |
915 | } |
916 | if (band_5ghz) { | |
917 | iwl_scan_offload_build_tx_cmd(mvm, vif, ies, | |
918 | IEEE80211_BAND_5GHZ, | |
919 | &scan_cfg->scan_cmd.tx_cmd[1], | |
762533ba | 920 | probes + |
35a000b7 | 921 | SCAN_OFFLOAD_PROBE_REQ_SIZE); |
762533ba | 922 | iwl_build_channel_cfg(mvm, req, scan_cfg->data, |
89879413 | 923 | IEEE80211_BAND_5GHZ, &head, |
50df8a30 | 924 | ssid_bitmap, ¶ms); |
35a000b7 DS |
925 | } |
926 | ||
927 | cmd.data[0] = scan_cfg; | |
928 | cmd.len[0] = cmd_len; | |
929 | cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY; | |
930 | ||
931 | IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n"); | |
932 | ||
933 | ret = iwl_mvm_send_cmd(mvm, &cmd); | |
934 | kfree(scan_cfg); | |
935 | return ret; | |
936 | } | |
937 | ||
938 | int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm, | |
939 | struct cfg80211_sched_scan_request *req) | |
940 | { | |
941 | struct iwl_scan_offload_profile *profile; | |
942 | struct iwl_scan_offload_profile_cfg *profile_cfg; | |
943 | struct iwl_scan_offload_blacklist *blacklist; | |
944 | struct iwl_host_cmd cmd = { | |
945 | .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD, | |
35a000b7 DS |
946 | .len[1] = sizeof(*profile_cfg), |
947 | .dataflags[0] = IWL_HCMD_DFL_NOCOPY, | |
948 | .dataflags[1] = IWL_HCMD_DFL_NOCOPY, | |
949 | }; | |
950 | int blacklist_len; | |
951 | int i; | |
952 | int ret; | |
953 | ||
954 | if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES)) | |
955 | return -EIO; | |
956 | ||
957 | if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL) | |
958 | blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN; | |
959 | else | |
960 | blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN; | |
961 | ||
962 | blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL); | |
963 | if (!blacklist) | |
964 | return -ENOMEM; | |
965 | ||
966 | profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL); | |
967 | if (!profile_cfg) { | |
968 | ret = -ENOMEM; | |
969 | goto free_blacklist; | |
970 | } | |
971 | ||
972 | cmd.data[0] = blacklist; | |
973 | cmd.len[0] = sizeof(*blacklist) * blacklist_len; | |
974 | cmd.data[1] = profile_cfg; | |
975 | ||
976 | /* No blacklist configuration */ | |
977 | ||
978 | profile_cfg->num_profiles = req->n_match_sets; | |
979 | profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN; | |
980 | profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN; | |
981 | profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN; | |
6e0bbe5e DS |
982 | if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len) |
983 | profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN; | |
35a000b7 DS |
984 | |
985 | for (i = 0; i < req->n_match_sets; i++) { | |
986 | profile = &profile_cfg->profiles[i]; | |
987 | profile->ssid_index = i; | |
988 | /* Support any cipher and auth algorithm */ | |
989 | profile->unicast_cipher = 0xff; | |
990 | profile->auth_alg = 0xff; | |
991 | profile->network_type = IWL_NETWORK_TYPE_ANY; | |
992 | profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY; | |
993 | profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN; | |
994 | } | |
995 | ||
996 | IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n"); | |
997 | ||
998 | ret = iwl_mvm_send_cmd(mvm, &cmd); | |
999 | kfree(profile_cfg); | |
1000 | free_blacklist: | |
1001 | kfree(blacklist); | |
1002 | ||
1003 | return ret; | |
1004 | } | |
1005 | ||
d2496221 DS |
1006 | static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm, |
1007 | struct cfg80211_sched_scan_request *req) | |
1008 | { | |
1009 | if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) { | |
1010 | IWL_DEBUG_SCAN(mvm, | |
1011 | "Sending scheduled scan with filtering, n_match_sets %d\n", | |
1012 | req->n_match_sets); | |
1013 | return false; | |
1014 | } | |
1015 | ||
1016 | IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n"); | |
1017 | return true; | |
1018 | } | |
1019 | ||
35a000b7 DS |
1020 | int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm, |
1021 | struct cfg80211_sched_scan_request *req) | |
1022 | { | |
1023 | struct iwl_scan_offload_req scan_req = { | |
1024 | .watchdog = IWL_SCHED_SCAN_WATCHDOG, | |
1025 | ||
1026 | .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS, | |
b14fc2be | 1027 | .schedule_line[0].delay = cpu_to_le16(req->interval / 1000), |
35a000b7 DS |
1028 | .schedule_line[0].full_scan_mul = 1, |
1029 | ||
1030 | .schedule_line[1].iterations = 0xff, | |
b14fc2be | 1031 | .schedule_line[1].delay = cpu_to_le16(req->interval / 1000), |
35a000b7 DS |
1032 | .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER, |
1033 | }; | |
1034 | ||
d2496221 | 1035 | if (iwl_mvm_scan_pass_all(mvm, req)) |
de33fb5e | 1036 | scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL); |
35a000b7 | 1037 | |
e820c2da HD |
1038 | if (mvm->last_ebs_successful && |
1039 | mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) | |
1040 | scan_req.flags |= | |
1041 | cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE); | |
1042 | ||
a1022927 | 1043 | return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0, |
35a000b7 DS |
1044 | sizeof(scan_req), &scan_req); |
1045 | } | |
1046 | ||
b141c235 LC |
1047 | int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm, |
1048 | struct ieee80211_vif *vif, | |
1049 | struct cfg80211_sched_scan_request *req, | |
1050 | struct ieee80211_scan_ies *ies) | |
1051 | { | |
1052 | int ret; | |
1053 | ||
b975e55a | 1054 | if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) { |
b141c235 LC |
1055 | ret = iwl_mvm_config_sched_scan_profiles(mvm, req); |
1056 | if (ret) | |
1057 | return ret; | |
b975e55a DS |
1058 | ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies); |
1059 | } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) { | |
1060 | mvm->scan_status = IWL_MVM_SCAN_SCHED; | |
d2496221 DS |
1061 | ret = iwl_mvm_config_sched_scan_profiles(mvm, req); |
1062 | if (ret) | |
1063 | return ret; | |
b975e55a | 1064 | ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies); |
b141c235 | 1065 | } else { |
d2496221 | 1066 | mvm->scan_status = IWL_MVM_SCAN_SCHED; |
b141c235 LC |
1067 | ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies); |
1068 | if (ret) | |
1069 | return ret; | |
1070 | ret = iwl_mvm_config_sched_scan_profiles(mvm, req); | |
1071 | if (ret) | |
1072 | return ret; | |
1073 | ret = iwl_mvm_sched_scan_start(mvm, req); | |
1074 | } | |
1075 | ||
1076 | return ret; | |
1077 | } | |
1078 | ||
fb98be5e | 1079 | static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm) |
35a000b7 DS |
1080 | { |
1081 | int ret; | |
1082 | struct iwl_host_cmd cmd = { | |
1083 | .id = SCAN_OFFLOAD_ABORT_CMD, | |
35a000b7 DS |
1084 | }; |
1085 | u32 status; | |
1086 | ||
1087 | /* Exit instantly with error when device is not ready | |
1088 | * to receive scan abort command or it does not perform | |
1089 | * scheduled scan currently */ | |
fb98be5e DS |
1090 | if (mvm->scan_status != IWL_MVM_SCAN_SCHED && |
1091 | (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) || | |
1092 | mvm->scan_status != IWL_MVM_SCAN_OS)) | |
35a000b7 DS |
1093 | return -EIO; |
1094 | ||
1095 | ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status); | |
1096 | if (ret) | |
1097 | return ret; | |
1098 | ||
1099 | if (status != CAN_ABORT_STATUS) { | |
1100 | /* | |
1101 | * The scan abort will return 1 for success or | |
1102 | * 2 for "failure". A failure condition can be | |
1103 | * due to simply not being in an active scan which | |
1104 | * can occur if we send the scan abort before the | |
1105 | * microcode has notified us that a scan is completed. | |
1106 | */ | |
1107 | IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status); | |
33ea27f6 | 1108 | ret = -ENOENT; |
35a000b7 DS |
1109 | } |
1110 | ||
1111 | return ret; | |
1112 | } | |
1113 | ||
fb98be5e | 1114 | int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify) |
35a000b7 DS |
1115 | { |
1116 | int ret; | |
33ea27f6 AN |
1117 | struct iwl_notification_wait wait_scan_done; |
1118 | static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, }; | |
fb98be5e | 1119 | bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED; |
35a000b7 DS |
1120 | |
1121 | lockdep_assert_held(&mvm->mutex); | |
1122 | ||
d2496221 DS |
1123 | if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) |
1124 | return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN, | |
1125 | notify); | |
1126 | ||
fb98be5e DS |
1127 | if (mvm->scan_status != IWL_MVM_SCAN_SCHED && |
1128 | (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) || | |
1129 | mvm->scan_status != IWL_MVM_SCAN_OS)) { | |
1130 | IWL_DEBUG_SCAN(mvm, "No scan to stop\n"); | |
33ea27f6 | 1131 | return 0; |
35a000b7 DS |
1132 | } |
1133 | ||
33ea27f6 AN |
1134 | iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done, |
1135 | scan_done_notif, | |
1136 | ARRAY_SIZE(scan_done_notif), | |
1137 | NULL, NULL); | |
1138 | ||
fb98be5e | 1139 | ret = iwl_mvm_send_scan_offload_abort(mvm); |
33ea27f6 | 1140 | if (ret) { |
fb98be5e DS |
1141 | IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n", |
1142 | sched ? "offloaded " : "", ret); | |
33ea27f6 AN |
1143 | iwl_remove_notification(&mvm->notif_wait, &wait_scan_done); |
1144 | return ret; | |
1145 | } | |
1146 | ||
fb98be5e DS |
1147 | IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n", |
1148 | sched ? "offloaded " : ""); | |
33ea27f6 AN |
1149 | |
1150 | ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ); | |
1151 | if (ret) | |
1152 | return ret; | |
1153 | ||
1154 | /* | |
1155 | * Clear the scan status so the next scan requests will succeed. This | |
1156 | * also ensures the Rx handler doesn't do anything, as the scan was | |
4ff78181 EP |
1157 | * stopped from above. Since the rx handler won't do anything now, |
1158 | * we have to release the scan reference here. | |
33ea27f6 | 1159 | */ |
4ff78181 EP |
1160 | if (mvm->scan_status == IWL_MVM_SCAN_OS) |
1161 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); | |
1162 | ||
33ea27f6 AN |
1163 | mvm->scan_status = IWL_MVM_SCAN_NONE; |
1164 | ||
fb98be5e DS |
1165 | if (notify) { |
1166 | if (sched) | |
1167 | ieee80211_sched_scan_stopped(mvm->hw); | |
1168 | else | |
1169 | ieee80211_scan_completed(mvm->hw, true); | |
1170 | } | |
636a2cdc | 1171 | |
33ea27f6 | 1172 | return 0; |
35a000b7 | 1173 | } |
fb98be5e DS |
1174 | |
1175 | static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm, | |
1176 | struct iwl_scan_req_tx_cmd *tx_cmd, | |
1177 | bool no_cck) | |
1178 | { | |
1179 | tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL | | |
1180 | TX_CMD_FLG_BT_DIS); | |
1181 | tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, | |
1182 | IEEE80211_BAND_2GHZ, | |
1183 | no_cck); | |
1184 | tx_cmd[0].sta_id = mvm->aux_sta.sta_id; | |
1185 | ||
1186 | tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL | | |
1187 | TX_CMD_FLG_BT_DIS); | |
1188 | tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, | |
1189 | IEEE80211_BAND_5GHZ, | |
1190 | no_cck); | |
1191 | tx_cmd[1].sta_id = mvm->aux_sta.sta_id; | |
1192 | } | |
1193 | ||
1194 | static void | |
1195 | iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm, | |
1196 | struct ieee80211_channel **channels, | |
1197 | int n_channels, u32 ssid_bitmap, | |
1198 | struct iwl_scan_req_unified_lmac *cmd) | |
1199 | { | |
1200 | struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data; | |
1201 | int i; | |
1202 | ||
1203 | for (i = 0; i < n_channels; i++) { | |
1204 | channel_cfg[i].channel_num = | |
1205 | cpu_to_le16(channels[i]->hw_value); | |
1206 | channel_cfg[i].iter_count = cpu_to_le16(1); | |
1207 | channel_cfg[i].iter_interval = 0; | |
1208 | channel_cfg[i].flags = | |
1209 | cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL | | |
1210 | ssid_bitmap); | |
1211 | } | |
1212 | } | |
1213 | ||
66dc5272 AO |
1214 | static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies, |
1215 | size_t len, u8 *const pos) | |
1216 | { | |
1217 | static const u8 before_ds_params[] = { | |
1218 | WLAN_EID_SSID, | |
1219 | WLAN_EID_SUPP_RATES, | |
1220 | WLAN_EID_REQUEST, | |
1221 | WLAN_EID_EXT_SUPP_RATES, | |
1222 | }; | |
1223 | size_t offs; | |
1224 | u8 *newpos = pos; | |
1225 | ||
1226 | if (!iwl_mvm_rrm_scan_needed(mvm)) { | |
1227 | memcpy(newpos, ies, len); | |
1228 | return newpos + len; | |
1229 | } | |
1230 | ||
1231 | offs = ieee80211_ie_split(ies, len, | |
1232 | before_ds_params, | |
1233 | ARRAY_SIZE(before_ds_params), | |
1234 | 0); | |
1235 | ||
1236 | memcpy(newpos, ies, offs); | |
1237 | newpos += offs; | |
1238 | ||
1239 | /* Add a placeholder for DS Parameter Set element */ | |
1240 | *newpos++ = WLAN_EID_DS_PARAMS; | |
1241 | *newpos++ = 1; | |
1242 | *newpos++ = 0; | |
1243 | ||
1244 | memcpy(newpos, ies + offs, len - offs); | |
1245 | newpos += len - offs; | |
1246 | ||
1247 | return newpos; | |
1248 | } | |
1249 | ||
fb98be5e DS |
1250 | static void |
1251 | iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif, | |
1252 | struct ieee80211_scan_ies *ies, | |
effd05ac JB |
1253 | struct iwl_scan_probe_req *preq, |
1254 | const u8 *mac_addr, const u8 *mac_addr_mask) | |
fb98be5e | 1255 | { |
fb98be5e | 1256 | struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf; |
66dc5272 | 1257 | u8 *pos, *newpos; |
fb98be5e | 1258 | |
effd05ac JB |
1259 | /* |
1260 | * Unfortunately, right now the offload scan doesn't support randomising | |
1261 | * within the firmware, so until the firmware API is ready we implement | |
1262 | * it in the driver. This means that the scan iterations won't really be | |
1263 | * random, only when it's restarted, but at least that helps a bit. | |
1264 | */ | |
1265 | if (mac_addr) | |
1266 | get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask); | |
1267 | else | |
1268 | memcpy(frame->sa, vif->addr, ETH_ALEN); | |
1269 | ||
fb98be5e DS |
1270 | frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ); |
1271 | eth_broadcast_addr(frame->da); | |
fb98be5e DS |
1272 | eth_broadcast_addr(frame->bssid); |
1273 | frame->seq_ctrl = 0; | |
1274 | ||
1275 | pos = frame->u.probe_req.variable; | |
1276 | *pos++ = WLAN_EID_SSID; | |
1277 | *pos++ = 0; | |
1278 | ||
1279 | preq->mac_header.offset = 0; | |
1280 | preq->mac_header.len = cpu_to_le16(24 + 2); | |
1281 | ||
66dc5272 AO |
1282 | /* Insert ds parameter set element on 2.4 GHz band */ |
1283 | newpos = iwl_mvm_copy_and_insert_ds_elem(mvm, | |
1284 | ies->ies[IEEE80211_BAND_2GHZ], | |
1285 | ies->len[IEEE80211_BAND_2GHZ], | |
1286 | pos); | |
fb98be5e | 1287 | preq->band_data[0].offset = cpu_to_le16(pos - preq->buf); |
66dc5272 AO |
1288 | preq->band_data[0].len = cpu_to_le16(newpos - pos); |
1289 | pos = newpos; | |
fb98be5e DS |
1290 | |
1291 | memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ], | |
1292 | ies->len[IEEE80211_BAND_5GHZ]); | |
1293 | preq->band_data[1].offset = cpu_to_le16(pos - preq->buf); | |
1294 | preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]); | |
1295 | pos += ies->len[IEEE80211_BAND_5GHZ]; | |
1296 | ||
1297 | memcpy(pos, ies->common_ies, ies->common_ie_len); | |
1298 | preq->common_data.offset = cpu_to_le16(pos - preq->buf); | |
1299 | preq->common_data.len = cpu_to_le16(ies->common_ie_len); | |
1300 | } | |
1301 | ||
1302 | static void | |
1303 | iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm, | |
1304 | struct iwl_scan_req_unified_lmac *cmd, | |
1305 | struct iwl_mvm_scan_params *params) | |
1306 | { | |
af91344c | 1307 | memset(cmd, 0, ksize(cmd)); |
2ce89cd6 DS |
1308 | cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active; |
1309 | cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive; | |
1310 | if (params->passive_fragmented) | |
1311 | cmd->fragmented_dwell = | |
1312 | params->dwell[IEEE80211_BAND_2GHZ].passive; | |
fb98be5e DS |
1313 | cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm); |
1314 | cmd->max_out_time = cpu_to_le32(params->max_out_time); | |
1315 | cmd->suspend_time = cpu_to_le32(params->suspend_time); | |
1316 | cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH); | |
fb98be5e | 1317 | cmd->iter_num = cpu_to_le32(1); |
af91344c DS |
1318 | |
1319 | if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT && | |
1320 | mvm->last_ebs_successful) { | |
1321 | cmd->channel_opt[0].flags = | |
1322 | cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS | | |
1323 | IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE | | |
1324 | IWL_SCAN_CHANNEL_FLAG_CACHE_ADD); | |
a1ed4025 HD |
1325 | cmd->channel_opt[0].non_ebs_ratio = |
1326 | cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO); | |
af91344c DS |
1327 | cmd->channel_opt[1].flags = |
1328 | cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS | | |
1329 | IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE | | |
1330 | IWL_SCAN_CHANNEL_FLAG_CACHE_ADD); | |
a1ed4025 HD |
1331 | cmd->channel_opt[1].non_ebs_ratio = |
1332 | cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO); | |
af91344c | 1333 | } |
73897bd1 AO |
1334 | |
1335 | if (iwl_mvm_rrm_scan_needed(mvm)) | |
1336 | cmd->scan_flags |= | |
1337 | cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED); | |
fb98be5e DS |
1338 | } |
1339 | ||
1340 | int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm, | |
1341 | struct ieee80211_vif *vif, | |
1342 | struct ieee80211_scan_request *req) | |
1343 | { | |
1344 | struct iwl_host_cmd hcmd = { | |
1345 | .id = SCAN_OFFLOAD_REQUEST_CMD, | |
1346 | .len = { sizeof(struct iwl_scan_req_unified_lmac) + | |
1347 | sizeof(struct iwl_scan_channel_cfg_lmac) * | |
1348 | mvm->fw->ucode_capa.n_scan_channels + | |
1349 | sizeof(struct iwl_scan_probe_req), }, | |
1350 | .data = { mvm->scan_cmd, }, | |
1351 | .dataflags = { IWL_HCMD_DFL_NOCOPY, }, | |
1352 | }; | |
1353 | struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd; | |
d2496221 | 1354 | struct iwl_scan_probe_req *preq; |
fb98be5e DS |
1355 | struct iwl_mvm_scan_params params = {}; |
1356 | u32 flags; | |
c8660dd5 | 1357 | u32 ssid_bitmap = 0; |
fb98be5e DS |
1358 | int ret, i; |
1359 | ||
1360 | lockdep_assert_held(&mvm->mutex); | |
1361 | ||
1362 | /* we should have failed registration if scan_cmd was NULL */ | |
1363 | if (WARN_ON(mvm->scan_cmd == NULL)) | |
1364 | return -ENOMEM; | |
1365 | ||
48849a41 AO |
1366 | if (req->req.n_ssids > PROBE_OPTION_MAX || |
1367 | req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] + | |
1368 | req->ies.len[NL80211_BAND_5GHZ] > | |
1369 | iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) || | |
1370 | req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels) | |
1371 | return -ENOBUFS; | |
fb98be5e DS |
1372 | |
1373 | mvm->scan_status = IWL_MVM_SCAN_OS; | |
1374 | ||
1375 | iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags, | |
1376 | ¶ms); | |
1377 | ||
1378 | iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, ¶ms); | |
1379 | ||
1380 | cmd->n_channels = (u8)req->req.n_channels; | |
1381 | ||
1382 | flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL; | |
1383 | ||
1384 | if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0) | |
1385 | flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION; | |
1386 | ||
1387 | if (params.passive_fragmented) | |
1388 | flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED; | |
1389 | ||
1390 | if (req->req.n_ssids == 0) | |
1391 | flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE; | |
1392 | ||
73897bd1 | 1393 | cmd->scan_flags |= cpu_to_le32(flags); |
fb98be5e DS |
1394 | |
1395 | cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band); | |
1396 | cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP | | |
1397 | MAC_FILTER_IN_BEACON); | |
1398 | iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck); | |
1399 | iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids, | |
1400 | req->req.n_ssids, 0); | |
1401 | ||
1402 | cmd->schedule[0].delay = 0; | |
1403 | cmd->schedule[0].iterations = 1; | |
1404 | cmd->schedule[0].full_scan_mul = 0; | |
1405 | cmd->schedule[1].delay = 0; | |
1406 | cmd->schedule[1].iterations = 0; | |
1407 | cmd->schedule[1].full_scan_mul = 0; | |
1408 | ||
1409 | for (i = 1; i <= req->req.n_ssids; i++) | |
1410 | ssid_bitmap |= BIT(i); | |
1411 | ||
1412 | iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels, | |
1413 | req->req.n_channels, ssid_bitmap, | |
1414 | cmd); | |
1415 | ||
d2496221 DS |
1416 | preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) * |
1417 | mvm->fw->ucode_capa.n_scan_channels); | |
1418 | ||
effd05ac JB |
1419 | iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq, |
1420 | req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ? | |
1421 | req->req.mac_addr : NULL, | |
1422 | req->req.mac_addr_mask); | |
fb98be5e DS |
1423 | |
1424 | ret = iwl_mvm_send_cmd(mvm, &hcmd); | |
1425 | if (!ret) { | |
1426 | IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n"); | |
1427 | } else { | |
1428 | /* | |
1429 | * If the scan failed, it usually means that the FW was unable | |
1430 | * to allocate the time events. Warn on it, but maybe we | |
1431 | * should try to send the command again with different params. | |
1432 | */ | |
1433 | IWL_ERR(mvm, "Scan failed! ret %d\n", ret); | |
1434 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
1435 | ret = -EIO; | |
1436 | } | |
1437 | return ret; | |
1438 | } | |
1439 | ||
1440 | int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm, | |
1441 | struct ieee80211_vif *vif, | |
1442 | struct cfg80211_sched_scan_request *req, | |
1443 | struct ieee80211_scan_ies *ies) | |
1444 | { | |
1445 | struct iwl_host_cmd hcmd = { | |
1446 | .id = SCAN_OFFLOAD_REQUEST_CMD, | |
1447 | .len = { sizeof(struct iwl_scan_req_unified_lmac) + | |
1448 | sizeof(struct iwl_scan_channel_cfg_lmac) * | |
1449 | mvm->fw->ucode_capa.n_scan_channels + | |
1450 | sizeof(struct iwl_scan_probe_req), }, | |
1451 | .data = { mvm->scan_cmd, }, | |
1452 | .dataflags = { IWL_HCMD_DFL_NOCOPY, }, | |
1453 | }; | |
1454 | struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd; | |
d2496221 | 1455 | struct iwl_scan_probe_req *preq; |
fb98be5e DS |
1456 | struct iwl_mvm_scan_params params = {}; |
1457 | int ret; | |
1458 | u32 flags = 0, ssid_bitmap = 0; | |
1459 | ||
1460 | lockdep_assert_held(&mvm->mutex); | |
1461 | ||
1462 | /* we should have failed registration if scan_cmd was NULL */ | |
1463 | if (WARN_ON(mvm->scan_cmd == NULL)) | |
1464 | return -ENOMEM; | |
1465 | ||
48849a41 AO |
1466 | if (req->n_ssids > PROBE_OPTION_MAX || |
1467 | ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] + | |
1468 | ies->len[NL80211_BAND_5GHZ] > | |
1469 | iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) || | |
1470 | req->n_channels > mvm->fw->ucode_capa.n_scan_channels) | |
fb98be5e DS |
1471 | return -ENOBUFS; |
1472 | ||
1473 | iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, ¶ms); | |
1474 | ||
1475 | iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, ¶ms); | |
1476 | ||
1477 | cmd->n_channels = (u8)req->n_channels; | |
1478 | ||
d2496221 | 1479 | if (iwl_mvm_scan_pass_all(mvm, req)) |
fb98be5e | 1480 | flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL; |
fb98be5e DS |
1481 | |
1482 | if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0) | |
1483 | flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION; | |
1484 | ||
1485 | if (params.passive_fragmented) | |
1486 | flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED; | |
1487 | ||
1488 | if (req->n_ssids == 0) | |
1489 | flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE; | |
1490 | ||
e5d74646 AB |
1491 | #ifdef CONFIG_IWLWIFI_DEBUGFS |
1492 | if (mvm->scan_iter_notif_enabled) | |
1493 | flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE; | |
1494 | #endif | |
1495 | ||
73897bd1 | 1496 | cmd->scan_flags |= cpu_to_le32(flags); |
fb98be5e DS |
1497 | |
1498 | cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band); | |
1499 | cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP | | |
1500 | MAC_FILTER_IN_BEACON); | |
1501 | iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false); | |
1502 | iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false); | |
1503 | ||
b14fc2be | 1504 | cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC); |
fb98be5e DS |
1505 | cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS; |
1506 | cmd->schedule[0].full_scan_mul = 1; | |
1507 | ||
b14fc2be | 1508 | cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC); |
fb98be5e DS |
1509 | cmd->schedule[1].iterations = 0xff; |
1510 | cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER; | |
1511 | ||
1512 | iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels, | |
1513 | ssid_bitmap, cmd); | |
1514 | ||
d2496221 DS |
1515 | preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) * |
1516 | mvm->fw->ucode_capa.n_scan_channels); | |
1517 | ||
effd05ac JB |
1518 | iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq, |
1519 | req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ? | |
1520 | req->mac_addr : NULL, | |
1521 | req->mac_addr_mask); | |
fb98be5e DS |
1522 | |
1523 | ret = iwl_mvm_send_cmd(mvm, &hcmd); | |
1524 | if (!ret) { | |
1525 | IWL_DEBUG_SCAN(mvm, | |
1526 | "Sched scan request was sent successfully\n"); | |
1527 | } else { | |
1528 | /* | |
1529 | * If the scan failed, it usually means that the FW was unable | |
1530 | * to allocate the time events. Warn on it, but maybe we | |
1531 | * should try to send the command again with different params. | |
1532 | */ | |
1533 | IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret); | |
1534 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
1535 | ret = -EIO; | |
1536 | } | |
1537 | return ret; | |
1538 | } | |
1539 | ||
1540 | ||
1541 | int iwl_mvm_cancel_scan(struct iwl_mvm *mvm) | |
1542 | { | |
d2496221 DS |
1543 | if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) |
1544 | return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN, | |
1545 | true); | |
1546 | ||
9b520d84 EG |
1547 | if (mvm->scan_status == IWL_MVM_SCAN_NONE) |
1548 | return 0; | |
1549 | ||
1550 | if (iwl_mvm_is_radio_killed(mvm)) { | |
1551 | ieee80211_scan_completed(mvm->hw, true); | |
1552 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); | |
1553 | mvm->scan_status = IWL_MVM_SCAN_NONE; | |
1554 | return 0; | |
1555 | } | |
1556 | ||
fb98be5e DS |
1557 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) |
1558 | return iwl_mvm_scan_offload_stop(mvm, true); | |
1559 | return iwl_mvm_cancel_regular_scan(mvm); | |
1560 | } | |
d2496221 DS |
1561 | |
1562 | /* UMAC scan API */ | |
1563 | ||
1564 | struct iwl_umac_scan_done { | |
1565 | struct iwl_mvm *mvm; | |
1566 | enum iwl_umac_scan_uid_type type; | |
1567 | }; | |
1568 | ||
1569 | static int rate_to_scan_rate_flag(unsigned int rate) | |
1570 | { | |
1571 | static const int rate_to_scan_rate[IWL_RATE_COUNT] = { | |
1572 | [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M, | |
1573 | [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M, | |
1574 | [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M, | |
1575 | [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M, | |
1576 | [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M, | |
1577 | [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M, | |
1578 | [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M, | |
1579 | [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M, | |
1580 | [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M, | |
1581 | [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M, | |
1582 | [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M, | |
1583 | [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M, | |
1584 | }; | |
1585 | ||
1586 | return rate_to_scan_rate[rate]; | |
1587 | } | |
1588 | ||
1589 | static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm) | |
1590 | { | |
1591 | struct ieee80211_supported_band *band; | |
1592 | unsigned int rates = 0; | |
1593 | int i; | |
1594 | ||
1595 | band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ]; | |
1596 | for (i = 0; i < band->n_bitrates; i++) | |
1597 | rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value); | |
1598 | band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ]; | |
1599 | for (i = 0; i < band->n_bitrates; i++) | |
1600 | rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value); | |
1601 | ||
1602 | /* Set both basic rates and supported rates */ | |
1603 | rates |= SCAN_CONFIG_SUPPORTED_RATE(rates); | |
1604 | ||
1605 | return cpu_to_le32(rates); | |
1606 | } | |
1607 | ||
1608 | int iwl_mvm_config_scan(struct iwl_mvm *mvm) | |
1609 | { | |
1610 | ||
1611 | struct iwl_scan_config *scan_config; | |
1612 | struct ieee80211_supported_band *band; | |
1613 | int num_channels = | |
1614 | mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels + | |
1615 | mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels; | |
1616 | int ret, i, j = 0, cmd_size, data_size; | |
1617 | struct iwl_host_cmd cmd = { | |
1618 | .id = SCAN_CFG_CMD, | |
1619 | }; | |
1620 | ||
1621 | if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels)) | |
1622 | return -ENOBUFS; | |
1623 | ||
1624 | cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels; | |
1625 | ||
1626 | scan_config = kzalloc(cmd_size, GFP_KERNEL); | |
1627 | if (!scan_config) | |
1628 | return -ENOMEM; | |
1629 | ||
1630 | data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr); | |
1631 | scan_config->hdr.size = cpu_to_le16(data_size); | |
1632 | scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE | | |
1633 | SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS | | |
1634 | SCAN_CONFIG_FLAG_SET_TX_CHAINS | | |
1635 | SCAN_CONFIG_FLAG_SET_RX_CHAINS | | |
1636 | SCAN_CONFIG_FLAG_SET_ALL_TIMES | | |
1637 | SCAN_CONFIG_FLAG_SET_LEGACY_RATES | | |
1638 | SCAN_CONFIG_FLAG_SET_MAC_ADDR | | |
1639 | SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS| | |
1640 | SCAN_CONFIG_N_CHANNELS(num_channels)); | |
a0544272 | 1641 | scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm)); |
d2496221 DS |
1642 | scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm)); |
1643 | scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm); | |
1644 | scan_config->out_of_channel_time = cpu_to_le32(170); | |
1645 | scan_config->suspend_time = cpu_to_le32(30); | |
1646 | scan_config->dwell_active = 20; | |
1647 | scan_config->dwell_passive = 110; | |
1648 | scan_config->dwell_fragmented = 20; | |
1649 | ||
1650 | memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN); | |
1651 | ||
1652 | scan_config->bcast_sta_id = mvm->aux_sta.sta_id; | |
1653 | scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS | | |
1654 | IWL_CHANNEL_FLAG_ACCURATE_EBS | | |
1655 | IWL_CHANNEL_FLAG_EBS_ADD | | |
1656 | IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE; | |
1657 | ||
1658 | band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ]; | |
1659 | for (i = 0; i < band->n_channels; i++, j++) | |
1660 | scan_config->channel_array[j] = band->channels[i].center_freq; | |
1661 | band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ]; | |
1662 | for (i = 0; i < band->n_channels; i++, j++) | |
1663 | scan_config->channel_array[j] = band->channels[i].center_freq; | |
1664 | ||
1665 | cmd.data[0] = scan_config; | |
1666 | cmd.len[0] = cmd_size; | |
1667 | cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY; | |
1668 | ||
1669 | IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n"); | |
1670 | ||
1671 | ret = iwl_mvm_send_cmd(mvm, &cmd); | |
1672 | ||
1673 | kfree(scan_config); | |
1674 | return ret; | |
1675 | } | |
1676 | ||
1677 | static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid) | |
1678 | { | |
1679 | int i; | |
1680 | ||
1681 | for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) | |
1682 | if (mvm->scan_uid[i] == uid) | |
1683 | return i; | |
1684 | ||
1685 | return i; | |
1686 | } | |
1687 | ||
1688 | static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm) | |
1689 | { | |
1690 | return iwl_mvm_find_scan_uid(mvm, 0); | |
1691 | } | |
1692 | ||
1693 | static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm, | |
1694 | enum iwl_umac_scan_uid_type type) | |
1695 | { | |
1696 | int i; | |
1697 | ||
1698 | for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) | |
1699 | if (mvm->scan_uid[i] & type) | |
1700 | return true; | |
1701 | ||
1702 | return false; | |
1703 | } | |
1704 | ||
1705 | static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm, | |
1706 | enum iwl_umac_scan_uid_type type) | |
1707 | { | |
1708 | u32 uid; | |
1709 | ||
1710 | /* make sure exactly one bit is on in scan type */ | |
1711 | WARN_ON(hweight8(type) != 1); | |
1712 | ||
1713 | /* | |
1714 | * Make sure scan uids are unique. If one scan lasts long time while | |
1715 | * others are completing frequently, the seq number will wrap up and | |
1716 | * we may have more than one scan with the same uid. | |
1717 | */ | |
1718 | do { | |
1719 | uid = type | (mvm->scan_seq_num << | |
1720 | IWL_UMAC_SCAN_UID_SEQ_OFFSET); | |
1721 | mvm->scan_seq_num++; | |
1722 | } while (iwl_mvm_find_scan_uid(mvm, uid) < | |
1723 | IWL_MVM_MAX_SIMULTANEOUS_SCANS); | |
1724 | ||
1725 | IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid); | |
1726 | ||
1727 | return uid; | |
1728 | } | |
1729 | ||
1730 | static void | |
1731 | iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm, | |
1732 | struct iwl_scan_req_umac *cmd, | |
1733 | struct iwl_mvm_scan_params *params) | |
1734 | { | |
1735 | memset(cmd, 0, ksize(cmd)); | |
1736 | cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) - | |
1737 | sizeof(struct iwl_mvm_umac_cmd_hdr)); | |
1738 | cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active; | |
1739 | cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive; | |
1740 | if (params->passive_fragmented) | |
1741 | cmd->fragmented_dwell = | |
1742 | params->dwell[IEEE80211_BAND_2GHZ].passive; | |
1743 | cmd->max_out_time = cpu_to_le32(params->max_out_time); | |
1744 | cmd->suspend_time = cpu_to_le32(params->suspend_time); | |
1745 | cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH); | |
1746 | } | |
1747 | ||
1748 | static void | |
1749 | iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm, | |
1750 | struct ieee80211_channel **channels, | |
1751 | int n_channels, u32 ssid_bitmap, | |
1752 | struct iwl_scan_req_umac *cmd) | |
1753 | { | |
1754 | struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data; | |
1755 | int i; | |
1756 | ||
1757 | for (i = 0; i < n_channels; i++) { | |
1758 | channel_cfg[i].flags = cpu_to_le32(ssid_bitmap); | |
1759 | channel_cfg[i].channel_num = channels[i]->hw_value; | |
1760 | channel_cfg[i].iter_count = 1; | |
1761 | channel_cfg[i].iter_interval = 0; | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids, | |
1766 | struct cfg80211_ssid *ssids, | |
1767 | int fragmented) | |
1768 | { | |
1769 | int flags = 0; | |
1770 | ||
1771 | if (n_ssids == 0) | |
1772 | flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE; | |
1773 | ||
1774 | if (n_ssids == 1 && ssids[0].ssid_len != 0) | |
1775 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT; | |
1776 | ||
1777 | if (fragmented) | |
1778 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED; | |
1779 | ||
1780 | if (iwl_mvm_rrm_scan_needed(mvm)) | |
1781 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED; | |
1782 | ||
1783 | return flags; | |
1784 | } | |
1785 | ||
1786 | int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif, | |
1787 | struct ieee80211_scan_request *req) | |
1788 | { | |
1789 | struct iwl_host_cmd hcmd = { | |
1790 | .id = SCAN_REQ_UMAC, | |
1791 | .len = { iwl_mvm_scan_size(mvm), }, | |
1792 | .data = { mvm->scan_cmd, }, | |
1793 | .dataflags = { IWL_HCMD_DFL_NOCOPY, }, | |
1794 | }; | |
1795 | struct iwl_scan_req_umac *cmd = mvm->scan_cmd; | |
1796 | struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data + | |
1797 | sizeof(struct iwl_scan_channel_cfg_umac) * | |
1798 | mvm->fw->ucode_capa.n_scan_channels; | |
1799 | struct iwl_mvm_scan_params params = {}; | |
1800 | u32 uid, flags; | |
c8660dd5 | 1801 | u32 ssid_bitmap = 0; |
d2496221 DS |
1802 | int ret, i, uid_idx; |
1803 | ||
1804 | lockdep_assert_held(&mvm->mutex); | |
1805 | ||
1806 | uid_idx = iwl_mvm_find_free_scan_uid(mvm); | |
1807 | if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS) | |
1808 | return -EBUSY; | |
1809 | ||
1810 | /* we should have failed registration if scan_cmd was NULL */ | |
1811 | if (WARN_ON(mvm->scan_cmd == NULL)) | |
1812 | return -ENOMEM; | |
1813 | ||
1814 | if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX || | |
1815 | req->ies.common_ie_len + | |
1816 | req->ies.len[NL80211_BAND_2GHZ] + | |
1817 | req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 > | |
1818 | SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels > | |
1819 | mvm->fw->ucode_capa.n_scan_channels)) | |
1820 | return -ENOBUFS; | |
1821 | ||
1822 | iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags, | |
1823 | ¶ms); | |
1824 | ||
1825 | iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, ¶ms); | |
1826 | ||
1827 | uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN); | |
1828 | mvm->scan_uid[uid_idx] = uid; | |
1829 | cmd->uid = cpu_to_le32(uid); | |
1830 | ||
1831 | cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH); | |
1832 | ||
1833 | flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids, | |
1834 | req->req.ssids, | |
1835 | params.passive_fragmented); | |
1836 | ||
1837 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL; | |
1838 | ||
1839 | cmd->general_flags = cpu_to_le32(flags); | |
1840 | cmd->n_channels = req->req.n_channels; | |
1841 | ||
1842 | for (i = 0; i < req->req.n_ssids; i++) | |
1843 | ssid_bitmap |= BIT(i); | |
1844 | ||
1845 | iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels, | |
1846 | req->req.n_channels, ssid_bitmap, cmd); | |
1847 | ||
1848 | sec_part->schedule[0].iter_count = 1; | |
1849 | sec_part->delay = 0; | |
1850 | ||
effd05ac JB |
1851 | iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq, |
1852 | req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ? | |
1853 | req->req.mac_addr : NULL, | |
1854 | req->req.mac_addr_mask); | |
d2496221 DS |
1855 | |
1856 | iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids, | |
1857 | req->req.n_ssids, 0); | |
1858 | ||
1859 | ret = iwl_mvm_send_cmd(mvm, &hcmd); | |
1860 | if (!ret) { | |
1861 | IWL_DEBUG_SCAN(mvm, | |
1862 | "Scan request was sent successfully\n"); | |
1863 | } else { | |
1864 | /* | |
1865 | * If the scan failed, it usually means that the FW was unable | |
1866 | * to allocate the time events. Warn on it, but maybe we | |
1867 | * should try to send the command again with different params. | |
1868 | */ | |
1869 | IWL_ERR(mvm, "Scan failed! ret %d\n", ret); | |
1870 | } | |
1871 | return ret; | |
1872 | } | |
1873 | ||
1874 | int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif, | |
1875 | struct cfg80211_sched_scan_request *req, | |
1876 | struct ieee80211_scan_ies *ies) | |
1877 | { | |
1878 | ||
1879 | struct iwl_host_cmd hcmd = { | |
1880 | .id = SCAN_REQ_UMAC, | |
1881 | .len = { iwl_mvm_scan_size(mvm), }, | |
1882 | .data = { mvm->scan_cmd, }, | |
1883 | .dataflags = { IWL_HCMD_DFL_NOCOPY, }, | |
1884 | }; | |
1885 | struct iwl_scan_req_umac *cmd = mvm->scan_cmd; | |
1886 | struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data + | |
1887 | sizeof(struct iwl_scan_channel_cfg_umac) * | |
1888 | mvm->fw->ucode_capa.n_scan_channels; | |
1889 | struct iwl_mvm_scan_params params = {}; | |
1890 | u32 uid, flags; | |
c8660dd5 | 1891 | u32 ssid_bitmap = 0; |
d2496221 DS |
1892 | int ret, uid_idx; |
1893 | ||
1894 | lockdep_assert_held(&mvm->mutex); | |
1895 | ||
1896 | uid_idx = iwl_mvm_find_free_scan_uid(mvm); | |
1897 | if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS) | |
1898 | return -EBUSY; | |
1899 | ||
1900 | /* we should have failed registration if scan_cmd was NULL */ | |
1901 | if (WARN_ON(mvm->scan_cmd == NULL)) | |
1902 | return -ENOMEM; | |
1903 | ||
1904 | if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX || | |
1905 | ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] + | |
1906 | ies->len[NL80211_BAND_5GHZ] + 24 + 2 > | |
1907 | SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels > | |
1908 | mvm->fw->ucode_capa.n_scan_channels)) | |
1909 | return -ENOBUFS; | |
1910 | ||
1911 | iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, | |
1912 | ¶ms); | |
1913 | ||
1914 | iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, ¶ms); | |
1915 | ||
1916 | cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE); | |
1917 | ||
1918 | uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN); | |
1919 | mvm->scan_uid[uid_idx] = uid; | |
1920 | cmd->uid = cpu_to_le32(uid); | |
1921 | ||
1922 | cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW); | |
1923 | ||
1924 | flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids, | |
1925 | params.passive_fragmented); | |
1926 | ||
1927 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC; | |
1928 | ||
1929 | if (iwl_mvm_scan_pass_all(mvm, req)) | |
1930 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL; | |
1931 | else | |
1932 | flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH; | |
1933 | ||
1934 | cmd->general_flags = cpu_to_le32(flags); | |
1935 | ||
1936 | if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT && | |
1937 | mvm->last_ebs_successful) | |
1938 | cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS | | |
1939 | IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE | | |
1940 | IWL_SCAN_CHANNEL_FLAG_CACHE_ADD; | |
1941 | ||
1942 | cmd->n_channels = req->n_channels; | |
1943 | ||
1944 | iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap, | |
1945 | false); | |
1946 | ||
1947 | /* This API uses bits 0-19 instead of 1-20. */ | |
1948 | ssid_bitmap = ssid_bitmap >> 1; | |
1949 | ||
1950 | iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels, | |
1951 | ssid_bitmap, cmd); | |
1952 | ||
1953 | sec_part->schedule[0].interval = | |
1954 | cpu_to_le16(req->interval / MSEC_PER_SEC); | |
1955 | sec_part->schedule[0].iter_count = 0xff; | |
1956 | ||
1957 | sec_part->delay = 0; | |
1958 | ||
effd05ac JB |
1959 | iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq, |
1960 | req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ? | |
1961 | req->mac_addr : NULL, | |
1962 | req->mac_addr_mask); | |
d2496221 DS |
1963 | |
1964 | ret = iwl_mvm_send_cmd(mvm, &hcmd); | |
1965 | if (!ret) { | |
1966 | IWL_DEBUG_SCAN(mvm, | |
1967 | "Sched scan request was sent successfully\n"); | |
1968 | } else { | |
1969 | /* | |
1970 | * If the scan failed, it usually means that the FW was unable | |
1971 | * to allocate the time events. Warn on it, but maybe we | |
1972 | * should try to send the command again with different params. | |
1973 | */ | |
1974 | IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret); | |
1975 | } | |
1976 | return ret; | |
1977 | } | |
1978 | ||
1979 | int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm, | |
1980 | struct iwl_rx_cmd_buffer *rxb, | |
1981 | struct iwl_device_cmd *cmd) | |
1982 | { | |
1983 | struct iwl_rx_packet *pkt = rxb_addr(rxb); | |
1984 | struct iwl_umac_scan_complete *notif = (void *)pkt->data; | |
1985 | u32 uid = __le32_to_cpu(notif->uid); | |
1986 | bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN); | |
1987 | int uid_idx = iwl_mvm_find_scan_uid(mvm, uid); | |
1988 | ||
2992a324 DS |
1989 | /* |
1990 | * Scan uid may be set to zero in case of scan abort request from above. | |
1991 | */ | |
1992 | if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS) | |
d2496221 DS |
1993 | return 0; |
1994 | ||
1995 | IWL_DEBUG_SCAN(mvm, | |
1996 | "Scan completed, uid %u type %s, status %s, EBS status %s\n", | |
1997 | uid, sched ? "sched" : "regular", | |
1998 | notif->status == IWL_SCAN_OFFLOAD_COMPLETED ? | |
1999 | "completed" : "aborted", | |
2000 | notif->ebs_status == IWL_SCAN_EBS_SUCCESS ? | |
2001 | "success" : "failed"); | |
2002 | ||
2003 | mvm->last_ebs_successful = !notif->ebs_status; | |
2004 | mvm->scan_uid[uid_idx] = 0; | |
2005 | ||
2006 | if (!sched) { | |
2007 | ieee80211_scan_completed(mvm->hw, | |
2008 | notif->status == | |
2009 | IWL_SCAN_OFFLOAD_ABORTED); | |
2010 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); | |
2011 | } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) { | |
2012 | ieee80211_sched_scan_stopped(mvm->hw); | |
2013 | } else { | |
2014 | IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n"); | |
2015 | } | |
2016 | ||
2017 | return 0; | |
2018 | } | |
2019 | ||
2020 | static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait, | |
2021 | struct iwl_rx_packet *pkt, void *data) | |
2022 | { | |
2023 | struct iwl_umac_scan_done *scan_done = data; | |
2024 | struct iwl_umac_scan_complete *notif = (void *)pkt->data; | |
2025 | u32 uid = __le32_to_cpu(notif->uid); | |
2026 | int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid); | |
2027 | ||
2028 | if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC)) | |
2029 | return false; | |
2030 | ||
2031 | if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS) | |
2032 | return false; | |
2033 | ||
2034 | /* | |
2035 | * Clear scan uid of scans that was aborted from above and completed | |
2036 | * in FW so the RX handler does nothing. | |
2037 | */ | |
2038 | scan_done->mvm->scan_uid[uid_idx] = 0; | |
2039 | ||
2040 | return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type); | |
2041 | } | |
2042 | ||
2043 | static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid) | |
2044 | { | |
2045 | struct iwl_umac_scan_abort cmd = { | |
2046 | .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) - | |
2047 | sizeof(struct iwl_mvm_umac_cmd_hdr)), | |
2048 | .uid = cpu_to_le32(uid), | |
2049 | }; | |
2050 | ||
2051 | lockdep_assert_held(&mvm->mutex); | |
2052 | ||
2053 | IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid); | |
2054 | ||
2055 | return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd); | |
2056 | } | |
2057 | ||
2058 | static int iwl_umac_scan_stop(struct iwl_mvm *mvm, | |
2059 | enum iwl_umac_scan_uid_type type, bool notify) | |
2060 | { | |
2061 | struct iwl_notification_wait wait_scan_done; | |
2062 | static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, }; | |
2063 | struct iwl_umac_scan_done scan_done = { | |
2064 | .mvm = mvm, | |
2065 | .type = type, | |
2066 | }; | |
2067 | int i, ret = -EIO; | |
2068 | ||
2069 | iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done, | |
2070 | scan_done_notif, | |
2071 | ARRAY_SIZE(scan_done_notif), | |
2072 | iwl_scan_umac_done_check, &scan_done); | |
2073 | ||
2074 | IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type); | |
2075 | ||
2076 | for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) { | |
2077 | if (mvm->scan_uid[i] & type) { | |
2078 | int err; | |
2079 | ||
2080 | if (iwl_mvm_is_radio_killed(mvm) && | |
2081 | (type & IWL_UMAC_SCAN_UID_REG_SCAN)) { | |
2082 | ieee80211_scan_completed(mvm->hw, true); | |
2083 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); | |
2084 | break; | |
2085 | } | |
2086 | ||
2087 | err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]); | |
2088 | if (!err) | |
2089 | ret = 0; | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | if (ret) { | |
2094 | IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n"); | |
2095 | iwl_remove_notification(&mvm->notif_wait, &wait_scan_done); | |
2096 | return ret; | |
2097 | } | |
2098 | ||
2099 | ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ); | |
2100 | if (ret) | |
2101 | return ret; | |
2102 | ||
2103 | if (notify) { | |
2104 | if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN) | |
2105 | ieee80211_sched_scan_stopped(mvm->hw); | |
2106 | if (type & IWL_UMAC_SCAN_UID_REG_SCAN) { | |
2107 | ieee80211_scan_completed(mvm->hw, true); | |
2108 | iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN); | |
2109 | } | |
2110 | } | |
2111 | ||
2112 | return ret; | |
2113 | } | |
2114 | ||
2115 | int iwl_mvm_scan_size(struct iwl_mvm *mvm) | |
2116 | { | |
2117 | if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) | |
2118 | return sizeof(struct iwl_scan_req_umac) + | |
2119 | sizeof(struct iwl_scan_channel_cfg_umac) * | |
2120 | mvm->fw->ucode_capa.n_scan_channels + | |
2121 | sizeof(struct iwl_scan_req_umac_tail); | |
2122 | ||
2123 | if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) | |
2124 | return sizeof(struct iwl_scan_req_unified_lmac) + | |
2125 | sizeof(struct iwl_scan_channel_cfg_lmac) * | |
2126 | mvm->fw->ucode_capa.n_scan_channels + | |
2127 | sizeof(struct iwl_scan_probe_req); | |
2128 | ||
2129 | return sizeof(struct iwl_scan_cmd) + | |
2130 | mvm->fw->ucode_capa.max_probe_length + | |
2131 | mvm->fw->ucode_capa.n_scan_channels * | |
2132 | sizeof(struct iwl_scan_channel); | |
2133 | } |