]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/net/wireless/iwlwifi/mvm/scan.c
Merge tag 'iwlwifi-next-for-kalle-2015-08-04' of https://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-zesty-kernel.git] / drivers / net / wireless / iwlwifi / mvm / scan.c
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 *
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
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
26 * in the file called COPYING.
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 *
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
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 "fw-api-scan.h"
71
72 #define IWL_DENSE_EBS_SCAN_RATIO 5
73 #define IWL_SPARSE_EBS_SCAN_RATIO 1
74
75 struct iwl_mvm_scan_params {
76 u32 max_out_time;
77 u32 suspend_time;
78 bool passive_fragmented;
79 u32 n_channels;
80 u16 delay;
81 int n_ssids;
82 struct cfg80211_ssid *ssids;
83 struct ieee80211_channel **channels;
84 u16 interval; /* interval between scans (in secs) */
85 u32 flags;
86 u8 *mac_addr;
87 u8 *mac_addr_mask;
88 bool no_cck;
89 bool pass_all;
90 int n_match_sets;
91 struct iwl_scan_probe_req preq;
92 struct cfg80211_match_set *match_sets;
93 u16 passive_dwell;
94 u16 active_dwell;
95 u16 fragmented_dwell;
96 struct {
97 u8 iterations;
98 u8 full_scan_mul; /* not used for UMAC */
99 } schedule[2];
100 };
101
102 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
103 {
104 if (mvm->scan_rx_ant != ANT_NONE)
105 return mvm->scan_rx_ant;
106 return iwl_mvm_get_valid_rx_ant(mvm);
107 }
108
109 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
110 {
111 u16 rx_chain;
112 u8 rx_ant;
113
114 rx_ant = iwl_mvm_scan_rx_ant(mvm);
115 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
116 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
117 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
118 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
119 return cpu_to_le16(rx_chain);
120 }
121
122 static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
123 {
124 if (band == IEEE80211_BAND_2GHZ)
125 return cpu_to_le32(PHY_BAND_24);
126 else
127 return cpu_to_le32(PHY_BAND_5);
128 }
129
130 static inline __le32
131 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
132 bool no_cck)
133 {
134 u32 tx_ant;
135
136 mvm->scan_last_antenna_idx =
137 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
138 mvm->scan_last_antenna_idx);
139 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
140
141 if (band == IEEE80211_BAND_2GHZ && !no_cck)
142 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
143 tx_ant);
144 else
145 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
146 }
147
148 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
149 struct ieee80211_vif *vif)
150 {
151 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
152 int *global_cnt = data;
153
154 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
155 mvmvif->phy_ctxt->id < MAX_PHYS)
156 *global_cnt += 1;
157 }
158
159 static void iwl_mvm_scan_calc_dwell(struct iwl_mvm *mvm,
160 struct ieee80211_vif *vif,
161 struct iwl_mvm_scan_params *params)
162 {
163 int global_cnt = 0;
164 u8 frag_passive_dwell = 0;
165
166 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
167 IEEE80211_IFACE_ITER_NORMAL,
168 iwl_mvm_scan_condition_iterator,
169 &global_cnt);
170 if (!global_cnt)
171 goto not_bound;
172
173 params->suspend_time = 30;
174 params->max_out_time = 120;
175
176 if (iwl_mvm_low_latency(mvm)) {
177 if (fw_has_api(&mvm->fw->ucode_capa,
178 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
179
180 params->suspend_time = 105;
181 /*
182 * If there is more than one active interface make
183 * passive scan more fragmented.
184 */
185 frag_passive_dwell = 40;
186 params->max_out_time = frag_passive_dwell;
187 } else {
188 params->suspend_time = 120;
189 params->max_out_time = 120;
190 }
191 }
192
193 if (frag_passive_dwell &&
194 fw_has_api(&mvm->fw->ucode_capa,
195 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
196 /*
197 * P2P device scan should not be fragmented to avoid negative
198 * impact on P2P device discovery. Configure max_out_time to be
199 * equal to dwell time on passive channel.
200 */
201 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
202 params->max_out_time = 120;
203 } else {
204 params->passive_fragmented = true;
205 }
206 }
207
208 if ((params->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
209 (params->max_out_time > 200))
210 params->max_out_time = 200;
211
212 not_bound:
213
214 if (params->passive_fragmented)
215 params->fragmented_dwell = frag_passive_dwell;
216
217 /*
218 * use only basic dwell time in scan command, regardless of the band or
219 * the number of the probes. FW will calculate the actual dwell time.
220 */
221 params->passive_dwell = 110;
222 params->active_dwell = 10;
223
224
225 IWL_DEBUG_SCAN(mvm,
226 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
227 params->max_out_time, params->suspend_time,
228 params->passive_fragmented);
229 }
230
231 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
232 {
233 /* require rrm scan whenever the fw supports it */
234 return fw_has_capa(&mvm->fw->ucode_capa,
235 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
236 }
237
238 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
239 {
240 int max_probe_len;
241
242 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
243
244 /* we create the 802.11 header and SSID element */
245 max_probe_len -= 24 + 2;
246
247 /* DS parameter set element is added on 2.4GHZ band if required */
248 if (iwl_mvm_rrm_scan_needed(mvm))
249 max_probe_len -= 3;
250
251 return max_probe_len;
252 }
253
254 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
255 {
256 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
257
258 /* TODO: [BUG] This function should return the maximum allowed size of
259 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
260 * in the same command. So the correct implementation of this function
261 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
262 * command has only 512 bytes and it would leave us with about 240
263 * bytes for scan IEs, which is clearly not enough. So meanwhile
264 * we will report an incorrect value. This may result in a failure to
265 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
266 * functions with -ENOBUFS, if a large enough probe will be provided.
267 */
268 return max_ie_len;
269 }
270
271 static u8 *iwl_mvm_dump_channel_list(struct iwl_scan_results_notif *res,
272 int num_res, u8 *buf, size_t buf_size)
273 {
274 int i;
275 u8 *pos = buf, *end = buf + buf_size;
276
277 for (i = 0; pos < end && i < num_res; i++)
278 pos += snprintf(pos, end - pos, " %u", res[i].channel);
279
280 /* terminate the string in case the buffer was too short */
281 *(buf + buf_size - 1) = '\0';
282
283 return buf;
284 }
285
286 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
287 struct iwl_rx_cmd_buffer *rxb)
288 {
289 struct iwl_rx_packet *pkt = rxb_addr(rxb);
290 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
291 u8 buf[256];
292
293 IWL_DEBUG_SCAN(mvm,
294 "Scan offload iteration complete: status=0x%x scanned channels=%d channels list: %s\n",
295 notif->status, notif->scanned_channels,
296 iwl_mvm_dump_channel_list(notif->results,
297 notif->scanned_channels, buf,
298 sizeof(buf)));
299 }
300
301 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
302 struct iwl_rx_cmd_buffer *rxb)
303 {
304 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
305 ieee80211_sched_scan_results(mvm->hw);
306 }
307
308 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
309 {
310 switch (status) {
311 case IWL_SCAN_EBS_SUCCESS:
312 return "successful";
313 case IWL_SCAN_EBS_INACTIVE:
314 return "inactive";
315 case IWL_SCAN_EBS_FAILED:
316 case IWL_SCAN_EBS_CHAN_NOT_FOUND:
317 default:
318 return "failed";
319 }
320 }
321
322 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
323 struct iwl_rx_cmd_buffer *rxb)
324 {
325 struct iwl_rx_packet *pkt = rxb_addr(rxb);
326 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
327 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
328
329 /* scan status must be locked for proper checking */
330 lockdep_assert_held(&mvm->mutex);
331
332 /* We first check if we were stopping a scan, in which case we
333 * just clear the stopping flag. Then we check if it was a
334 * firmware initiated stop, in which case we need to inform
335 * mac80211.
336 * Note that we can have a stopping and a running scan
337 * simultaneously, but we can't have two different types of
338 * scans stopping or running at the same time (since LMAC
339 * doesn't support it).
340 */
341
342 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
343 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
344
345 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
346 aborted ? "aborted" : "completed",
347 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
348
349 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
350 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
351 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
352 aborted ? "aborted" : "completed",
353 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
354
355 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
356 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
357 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
358
359 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
360 aborted ? "aborted" : "completed",
361 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
362
363 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
364 ieee80211_sched_scan_stopped(mvm->hw);
365 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
366 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
367 aborted ? "aborted" : "completed",
368 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
369
370 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
371 ieee80211_scan_completed(mvm->hw,
372 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
373 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
374 }
375
376 mvm->last_ebs_successful =
377 scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
378 scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
379 }
380
381 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
382 {
383 int i;
384
385 for (i = 0; i < PROBE_OPTION_MAX; i++) {
386 if (!ssid_list[i].len)
387 break;
388 if (ssid_list[i].len == ssid_len &&
389 !memcmp(ssid_list->ssid, ssid, ssid_len))
390 return i;
391 }
392 return -1;
393 }
394
395 /* We insert the SSIDs in an inverted order, because the FW will
396 * invert it back.
397 */
398 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
399 struct iwl_ssid_ie *ssids,
400 u32 *ssid_bitmap)
401 {
402 int i, j;
403 int index;
404
405 /*
406 * copy SSIDs from match list.
407 * iwl_config_sched_scan_profiles() uses the order of these ssids to
408 * config match list.
409 */
410 for (i = 0, j = params->n_match_sets - 1;
411 j >= 0 && i < PROBE_OPTION_MAX;
412 i++, j--) {
413 /* skip empty SSID matchsets */
414 if (!params->match_sets[j].ssid.ssid_len)
415 continue;
416 ssids[i].id = WLAN_EID_SSID;
417 ssids[i].len = params->match_sets[j].ssid.ssid_len;
418 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
419 ssids[i].len);
420 }
421
422 /* add SSIDs from scan SSID list */
423 *ssid_bitmap = 0;
424 for (j = params->n_ssids - 1;
425 j >= 0 && i < PROBE_OPTION_MAX;
426 i++, j--) {
427 index = iwl_ssid_exist(params->ssids[j].ssid,
428 params->ssids[j].ssid_len,
429 ssids);
430 if (index < 0) {
431 ssids[i].id = WLAN_EID_SSID;
432 ssids[i].len = params->ssids[j].ssid_len;
433 memcpy(ssids[i].ssid, params->ssids[j].ssid,
434 ssids[i].len);
435 *ssid_bitmap |= BIT(i);
436 } else {
437 *ssid_bitmap |= BIT(index);
438 }
439 }
440 }
441
442 static int
443 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
444 struct cfg80211_sched_scan_request *req)
445 {
446 struct iwl_scan_offload_profile *profile;
447 struct iwl_scan_offload_profile_cfg *profile_cfg;
448 struct iwl_scan_offload_blacklist *blacklist;
449 struct iwl_host_cmd cmd = {
450 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
451 .len[1] = sizeof(*profile_cfg),
452 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
453 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
454 };
455 int blacklist_len;
456 int i;
457 int ret;
458
459 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
460 return -EIO;
461
462 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
463 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
464 else
465 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
466
467 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
468 if (!blacklist)
469 return -ENOMEM;
470
471 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
472 if (!profile_cfg) {
473 ret = -ENOMEM;
474 goto free_blacklist;
475 }
476
477 cmd.data[0] = blacklist;
478 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
479 cmd.data[1] = profile_cfg;
480
481 /* No blacklist configuration */
482
483 profile_cfg->num_profiles = req->n_match_sets;
484 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
485 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
486 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
487 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
488 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
489
490 for (i = 0; i < req->n_match_sets; i++) {
491 profile = &profile_cfg->profiles[i];
492 profile->ssid_index = i;
493 /* Support any cipher and auth algorithm */
494 profile->unicast_cipher = 0xff;
495 profile->auth_alg = 0xff;
496 profile->network_type = IWL_NETWORK_TYPE_ANY;
497 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
498 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
499 }
500
501 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
502
503 ret = iwl_mvm_send_cmd(mvm, &cmd);
504 kfree(profile_cfg);
505 free_blacklist:
506 kfree(blacklist);
507
508 return ret;
509 }
510
511 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
512 struct cfg80211_sched_scan_request *req)
513 {
514 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
515 IWL_DEBUG_SCAN(mvm,
516 "Sending scheduled scan with filtering, n_match_sets %d\n",
517 req->n_match_sets);
518 return false;
519 }
520
521 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
522 return true;
523 }
524
525 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
526 {
527 int ret;
528 struct iwl_host_cmd cmd = {
529 .id = SCAN_OFFLOAD_ABORT_CMD,
530 };
531 u32 status;
532
533 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
534 if (ret)
535 return ret;
536
537 if (status != CAN_ABORT_STATUS) {
538 /*
539 * The scan abort will return 1 for success or
540 * 2 for "failure". A failure condition can be
541 * due to simply not being in an active scan which
542 * can occur if we send the scan abort before the
543 * microcode has notified us that a scan is completed.
544 */
545 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
546 ret = -ENOENT;
547 }
548
549 return ret;
550 }
551
552 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
553 struct iwl_scan_req_tx_cmd *tx_cmd,
554 bool no_cck)
555 {
556 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
557 TX_CMD_FLG_BT_DIS);
558 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
559 IEEE80211_BAND_2GHZ,
560 no_cck);
561 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
562
563 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
564 TX_CMD_FLG_BT_DIS);
565 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
566 IEEE80211_BAND_5GHZ,
567 no_cck);
568 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
569 }
570
571 static void
572 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
573 struct ieee80211_channel **channels,
574 int n_channels, u32 ssid_bitmap,
575 struct iwl_scan_req_lmac *cmd)
576 {
577 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
578 int i;
579
580 for (i = 0; i < n_channels; i++) {
581 channel_cfg[i].channel_num =
582 cpu_to_le16(channels[i]->hw_value);
583 channel_cfg[i].iter_count = cpu_to_le16(1);
584 channel_cfg[i].iter_interval = 0;
585 channel_cfg[i].flags =
586 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
587 ssid_bitmap);
588 }
589 }
590
591 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
592 size_t len, u8 *const pos)
593 {
594 static const u8 before_ds_params[] = {
595 WLAN_EID_SSID,
596 WLAN_EID_SUPP_RATES,
597 WLAN_EID_REQUEST,
598 WLAN_EID_EXT_SUPP_RATES,
599 };
600 size_t offs;
601 u8 *newpos = pos;
602
603 if (!iwl_mvm_rrm_scan_needed(mvm)) {
604 memcpy(newpos, ies, len);
605 return newpos + len;
606 }
607
608 offs = ieee80211_ie_split(ies, len,
609 before_ds_params,
610 ARRAY_SIZE(before_ds_params),
611 0);
612
613 memcpy(newpos, ies, offs);
614 newpos += offs;
615
616 /* Add a placeholder for DS Parameter Set element */
617 *newpos++ = WLAN_EID_DS_PARAMS;
618 *newpos++ = 1;
619 *newpos++ = 0;
620
621 memcpy(newpos, ies + offs, len - offs);
622 newpos += len - offs;
623
624 return newpos;
625 }
626
627 static void
628 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
629 struct ieee80211_scan_ies *ies,
630 struct iwl_mvm_scan_params *params)
631 {
632 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
633 u8 *pos, *newpos;
634 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
635 params->mac_addr : NULL;
636
637 /*
638 * Unfortunately, right now the offload scan doesn't support randomising
639 * within the firmware, so until the firmware API is ready we implement
640 * it in the driver. This means that the scan iterations won't really be
641 * random, only when it's restarted, but at least that helps a bit.
642 */
643 if (mac_addr)
644 get_random_mask_addr(frame->sa, mac_addr,
645 params->mac_addr_mask);
646 else
647 memcpy(frame->sa, vif->addr, ETH_ALEN);
648
649 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
650 eth_broadcast_addr(frame->da);
651 eth_broadcast_addr(frame->bssid);
652 frame->seq_ctrl = 0;
653
654 pos = frame->u.probe_req.variable;
655 *pos++ = WLAN_EID_SSID;
656 *pos++ = 0;
657
658 params->preq.mac_header.offset = 0;
659 params->preq.mac_header.len = cpu_to_le16(24 + 2);
660
661 /* Insert ds parameter set element on 2.4 GHz band */
662 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
663 ies->ies[IEEE80211_BAND_2GHZ],
664 ies->len[IEEE80211_BAND_2GHZ],
665 pos);
666 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
667 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
668 pos = newpos;
669
670 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
671 ies->len[IEEE80211_BAND_5GHZ]);
672 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
673 params->preq.band_data[1].len =
674 cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
675 pos += ies->len[IEEE80211_BAND_5GHZ];
676
677 memcpy(pos, ies->common_ies, ies->common_ie_len);
678 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
679 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
680 }
681
682 static __le32 iwl_mvm_scan_priority(struct iwl_mvm *mvm,
683 enum iwl_scan_priority_ext prio)
684 {
685 if (fw_has_api(&mvm->fw->ucode_capa,
686 IWL_UCODE_TLV_API_EXT_SCAN_PRIORITY))
687 return cpu_to_le32(prio);
688
689 if (prio <= IWL_SCAN_PRIORITY_EXT_2)
690 return cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
691
692 if (prio <= IWL_SCAN_PRIORITY_EXT_4)
693 return cpu_to_le32(IWL_SCAN_PRIORITY_MEDIUM);
694
695 return cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
696 }
697
698 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
699 struct iwl_scan_req_lmac *cmd,
700 struct iwl_mvm_scan_params *params)
701 {
702 cmd->active_dwell = params->active_dwell;
703 cmd->passive_dwell = params->passive_dwell;
704 if (params->passive_fragmented)
705 cmd->fragmented_dwell = params->fragmented_dwell;
706 cmd->max_out_time = cpu_to_le32(params->max_out_time);
707 cmd->suspend_time = cpu_to_le32(params->suspend_time);
708 cmd->scan_prio = iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
709 }
710
711 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
712 struct ieee80211_scan_ies *ies,
713 int n_channels)
714 {
715 return ((n_ssids <= PROBE_OPTION_MAX) &&
716 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
717 (ies->common_ie_len +
718 ies->len[NL80211_BAND_2GHZ] +
719 ies->len[NL80211_BAND_5GHZ] <=
720 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
721 }
722
723 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
724 struct ieee80211_vif *vif,
725 int n_iterations)
726 {
727 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
728
729 /* We can only use EBS if:
730 * 1. the feature is supported;
731 * 2. the last EBS was successful;
732 * 3. if only single scan, the single scan EBS API is supported;
733 * 4. it's not a p2p find operation.
734 */
735 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
736 mvm->last_ebs_successful &&
737 (n_iterations > 1 ||
738 fw_has_api(capa, IWL_UCODE_TLV_API_SINGLE_SCAN_EBS)) &&
739 vif->type != NL80211_IFTYPE_P2P_DEVICE);
740 }
741
742 static int iwl_mvm_scan_total_iterations(struct iwl_mvm_scan_params *params)
743 {
744 return params->schedule[0].iterations + params->schedule[1].iterations;
745 }
746
747 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
748 struct iwl_mvm_scan_params *params)
749 {
750 int flags = 0;
751
752 if (params->n_ssids == 0)
753 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
754
755 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
756 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
757
758 if (params->passive_fragmented)
759 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
760
761 if (iwl_mvm_rrm_scan_needed(mvm))
762 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
763
764 if (params->pass_all)
765 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
766 else
767 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
768
769 #ifdef CONFIG_IWLWIFI_DEBUGFS
770 if (mvm->scan_iter_notif_enabled)
771 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
772 #endif
773
774 return flags;
775 }
776
777 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
778 struct iwl_mvm_scan_params *params)
779 {
780 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
781 struct iwl_scan_probe_req *preq =
782 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
783 mvm->fw->ucode_capa.n_scan_channels);
784 u32 ssid_bitmap = 0;
785 int n_iterations = iwl_mvm_scan_total_iterations(params);
786
787 lockdep_assert_held(&mvm->mutex);
788
789 memset(cmd, 0, ksize(cmd));
790
791 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
792
793 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
794 cmd->iter_num = cpu_to_le32(1);
795 cmd->n_channels = (u8)params->n_channels;
796
797 cmd->delay = cpu_to_le32(params->delay);
798
799 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params));
800
801 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
802 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
803 MAC_FILTER_IN_BEACON);
804 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
805 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
806
807 /* this API uses bits 1-20 instead of 0-19 */
808 ssid_bitmap <<= 1;
809
810 cmd->schedule[0].delay = cpu_to_le16(params->interval);
811 cmd->schedule[0].iterations = params->schedule[0].iterations;
812 cmd->schedule[0].full_scan_mul = params->schedule[0].full_scan_mul;
813 cmd->schedule[1].delay = cpu_to_le16(params->interval);
814 cmd->schedule[1].iterations = params->schedule[1].iterations;
815 cmd->schedule[1].full_scan_mul = params->schedule[1].iterations;
816
817 if (iwl_mvm_scan_use_ebs(mvm, vif, n_iterations)) {
818 cmd->channel_opt[0].flags =
819 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
820 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
821 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
822 cmd->channel_opt[0].non_ebs_ratio =
823 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
824 cmd->channel_opt[1].flags =
825 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
826 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
827 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
828 cmd->channel_opt[1].non_ebs_ratio =
829 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
830 }
831
832 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
833 params->n_channels, ssid_bitmap, cmd);
834
835 *preq = params->preq;
836
837 return 0;
838 }
839
840 static int rate_to_scan_rate_flag(unsigned int rate)
841 {
842 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
843 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
844 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
845 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
846 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
847 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
848 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
849 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
850 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
851 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
852 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
853 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
854 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
855 };
856
857 return rate_to_scan_rate[rate];
858 }
859
860 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
861 {
862 struct ieee80211_supported_band *band;
863 unsigned int rates = 0;
864 int i;
865
866 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
867 for (i = 0; i < band->n_bitrates; i++)
868 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
869 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
870 for (i = 0; i < band->n_bitrates; i++)
871 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
872
873 /* Set both basic rates and supported rates */
874 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
875
876 return cpu_to_le32(rates);
877 }
878
879 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
880 {
881
882 struct iwl_scan_config *scan_config;
883 struct ieee80211_supported_band *band;
884 int num_channels =
885 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
886 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
887 int ret, i, j = 0, cmd_size;
888 struct iwl_host_cmd cmd = {
889 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
890 };
891
892 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
893 return -ENOBUFS;
894
895 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
896
897 scan_config = kzalloc(cmd_size, GFP_KERNEL);
898 if (!scan_config)
899 return -ENOMEM;
900
901 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
902 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
903 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
904 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
905 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
906 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
907 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
908 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
909 SCAN_CONFIG_N_CHANNELS(num_channels));
910 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
911 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
912 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
913 scan_config->out_of_channel_time = cpu_to_le32(170);
914 scan_config->suspend_time = cpu_to_le32(30);
915 scan_config->dwell_active = 20;
916 scan_config->dwell_passive = 110;
917 scan_config->dwell_fragmented = 20;
918
919 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
920
921 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
922 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
923 IWL_CHANNEL_FLAG_ACCURATE_EBS |
924 IWL_CHANNEL_FLAG_EBS_ADD |
925 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
926
927 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
928 for (i = 0; i < band->n_channels; i++, j++)
929 scan_config->channel_array[j] = band->channels[i].hw_value;
930 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
931 for (i = 0; i < band->n_channels; i++, j++)
932 scan_config->channel_array[j] = band->channels[i].hw_value;
933
934 cmd.data[0] = scan_config;
935 cmd.len[0] = cmd_size;
936 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
937
938 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
939
940 ret = iwl_mvm_send_cmd(mvm, &cmd);
941
942 kfree(scan_config);
943 return ret;
944 }
945
946 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
947 {
948 int i;
949
950 for (i = 0; i < mvm->max_scans; i++)
951 if (mvm->scan_uid_status[i] == status)
952 return i;
953
954 return -ENOENT;
955 }
956
957 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
958 struct iwl_scan_req_umac *cmd,
959 struct iwl_mvm_scan_params *params)
960 {
961 cmd->active_dwell = params->active_dwell;
962 cmd->passive_dwell = params->passive_dwell;
963 if (params->passive_fragmented)
964 cmd->fragmented_dwell = params->fragmented_dwell;
965 cmd->max_out_time = cpu_to_le32(params->max_out_time);
966 cmd->suspend_time = cpu_to_le32(params->suspend_time);
967 cmd->scan_priority =
968 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
969
970 if (iwl_mvm_scan_total_iterations(params) == 0)
971 cmd->ooc_priority =
972 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
973 else
974 cmd->ooc_priority =
975 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_2);
976 }
977
978 static void
979 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
980 struct ieee80211_channel **channels,
981 int n_channels, u32 ssid_bitmap,
982 struct iwl_scan_req_umac *cmd)
983 {
984 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
985 int i;
986
987 for (i = 0; i < n_channels; i++) {
988 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
989 channel_cfg[i].channel_num = channels[i]->hw_value;
990 channel_cfg[i].iter_count = 1;
991 channel_cfg[i].iter_interval = 0;
992 }
993 }
994
995 static u32 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
996 struct iwl_mvm_scan_params *params)
997 {
998 int flags = 0;
999
1000 if (params->n_ssids == 0)
1001 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1002
1003 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1004 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1005
1006 if (params->passive_fragmented)
1007 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1008
1009 if (iwl_mvm_rrm_scan_needed(mvm))
1010 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1011
1012 if (params->pass_all)
1013 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1014 else
1015 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1016
1017 if (iwl_mvm_scan_total_iterations(params) > 1)
1018 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1019
1020 #ifdef CONFIG_IWLWIFI_DEBUGFS
1021 if (mvm->scan_iter_notif_enabled)
1022 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1023 #endif
1024 return flags;
1025 }
1026
1027 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1028 struct iwl_mvm_scan_params *params,
1029 int type)
1030 {
1031 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1032 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1033 sizeof(struct iwl_scan_channel_cfg_umac) *
1034 mvm->fw->ucode_capa.n_scan_channels;
1035 int uid;
1036 u32 ssid_bitmap = 0;
1037 int n_iterations = iwl_mvm_scan_total_iterations(params);
1038
1039 lockdep_assert_held(&mvm->mutex);
1040
1041 uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1042 if (uid < 0)
1043 return uid;
1044
1045 memset(cmd, 0, ksize(cmd));
1046
1047 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1048
1049 mvm->scan_uid_status[uid] = type;
1050
1051 cmd->uid = cpu_to_le32(uid);
1052 cmd->general_flags = cpu_to_le32(iwl_mvm_scan_umac_flags(mvm, params));
1053
1054 if (type == IWL_MVM_SCAN_SCHED)
1055 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1056
1057 if (iwl_mvm_scan_use_ebs(mvm, vif, n_iterations))
1058 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1059 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1060 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1061
1062 cmd->n_channels = params->n_channels;
1063
1064 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1065
1066 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1067 params->n_channels, ssid_bitmap, cmd);
1068
1069 /* With UMAC we use only one schedule for now, so use the sum
1070 * of the iterations (with a a maximum of 255).
1071 */
1072 sec_part->schedule[0].iter_count =
1073 (n_iterations > 255) ? 255 : n_iterations;
1074 sec_part->schedule[0].interval = cpu_to_le16(params->interval);
1075
1076 sec_part->delay = cpu_to_le16(params->delay);
1077 sec_part->preq = params->preq;
1078
1079 return 0;
1080 }
1081
1082 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1083 {
1084 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1085 }
1086
1087 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1088 {
1089 /* This looks a bit arbitrary, but the idea is that if we run
1090 * out of possible simultaneous scans and the userspace is
1091 * trying to run a scan type that is already running, we
1092 * return -EBUSY. But if the userspace wants to start a
1093 * different type of scan, we stop the opposite type to make
1094 * space for the new request. The reason is backwards
1095 * compatibility with old wpa_supplicant that wouldn't stop a
1096 * scheduled scan before starting a normal scan.
1097 */
1098
1099 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1100 return 0;
1101
1102 /* Use a switch, even though this is a bitmask, so that more
1103 * than one bits set will fall in default and we will warn.
1104 */
1105 switch (type) {
1106 case IWL_MVM_SCAN_REGULAR:
1107 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1108 return -EBUSY;
1109 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1110 case IWL_MVM_SCAN_SCHED:
1111 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1112 return -EBUSY;
1113 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1114 case IWL_MVM_SCAN_NETDETECT:
1115 /* No need to stop anything for net-detect since the
1116 * firmware is restarted anyway. This way, any sched
1117 * scans that were running will be restarted when we
1118 * resume.
1119 */
1120 return 0;
1121 default:
1122 WARN_ON(1);
1123 break;
1124 }
1125
1126 return -EIO;
1127 }
1128
1129 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1130 struct cfg80211_scan_request *req,
1131 struct ieee80211_scan_ies *ies)
1132 {
1133 struct iwl_host_cmd hcmd = {
1134 .len = { iwl_mvm_scan_size(mvm), },
1135 .data = { mvm->scan_cmd, },
1136 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1137 };
1138 struct iwl_mvm_scan_params params = {};
1139 int ret;
1140
1141 lockdep_assert_held(&mvm->mutex);
1142
1143 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1144 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1145 return -EBUSY;
1146 }
1147
1148 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1149 if (ret)
1150 return ret;
1151
1152 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1153
1154 /* we should have failed registration if scan_cmd was NULL */
1155 if (WARN_ON(!mvm->scan_cmd))
1156 return -ENOMEM;
1157
1158 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1159 return -ENOBUFS;
1160
1161 params.n_ssids = req->n_ssids;
1162 params.flags = req->flags;
1163 params.n_channels = req->n_channels;
1164 params.delay = 0;
1165 params.interval = 0;
1166 params.ssids = req->ssids;
1167 params.channels = req->channels;
1168 params.mac_addr = req->mac_addr;
1169 params.mac_addr_mask = req->mac_addr_mask;
1170 params.no_cck = req->no_cck;
1171 params.pass_all = true;
1172 params.n_match_sets = 0;
1173 params.match_sets = NULL;
1174
1175 params.schedule[0].iterations = 1;
1176 params.schedule[0].full_scan_mul = 0;
1177 params.schedule[1].iterations = 0;
1178 params.schedule[1].full_scan_mul = 0;
1179
1180 iwl_mvm_scan_calc_dwell(mvm, vif, &params);
1181
1182 iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1183
1184 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1185 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1186 ret = iwl_mvm_scan_umac(mvm, vif, &params,
1187 IWL_MVM_SCAN_REGULAR);
1188 } else {
1189 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1190 ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1191 }
1192
1193 if (ret)
1194 return ret;
1195
1196 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1197 if (!ret) {
1198 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1199 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1200 } else {
1201 /* If the scan failed, it usually means that the FW was unable
1202 * to allocate the time events. Warn on it, but maybe we
1203 * should try to send the command again with different params.
1204 */
1205 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1206 }
1207
1208 if (ret)
1209 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1210
1211 return ret;
1212 }
1213
1214 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1215 struct ieee80211_vif *vif,
1216 struct cfg80211_sched_scan_request *req,
1217 struct ieee80211_scan_ies *ies,
1218 int type)
1219 {
1220 struct iwl_host_cmd hcmd = {
1221 .len = { iwl_mvm_scan_size(mvm), },
1222 .data = { mvm->scan_cmd, },
1223 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1224 };
1225 struct iwl_mvm_scan_params params = {};
1226 int ret;
1227
1228 lockdep_assert_held(&mvm->mutex);
1229
1230 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1231 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1232 return -EBUSY;
1233 }
1234
1235 ret = iwl_mvm_check_running_scans(mvm, type);
1236 if (ret)
1237 return ret;
1238
1239 /* we should have failed registration if scan_cmd was NULL */
1240 if (WARN_ON(!mvm->scan_cmd))
1241 return -ENOMEM;
1242
1243 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1244 return -ENOBUFS;
1245
1246 params.n_ssids = req->n_ssids;
1247 params.flags = req->flags;
1248 params.n_channels = req->n_channels;
1249 params.ssids = req->ssids;
1250 params.channels = req->channels;
1251 params.mac_addr = req->mac_addr;
1252 params.mac_addr_mask = req->mac_addr_mask;
1253 params.no_cck = false;
1254 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1255 params.n_match_sets = req->n_match_sets;
1256 params.match_sets = req->match_sets;
1257
1258 params.schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1259 params.schedule[0].full_scan_mul = 1;
1260 params.schedule[1].iterations = 0xff;
1261 params.schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1262
1263 if (req->interval > U16_MAX) {
1264 IWL_DEBUG_SCAN(mvm,
1265 "interval value is > 16-bits, set to max possible\n");
1266 params.interval = U16_MAX;
1267 } else {
1268 params.interval = req->interval / MSEC_PER_SEC;
1269 }
1270
1271 /* In theory, LMAC scans can handle a 32-bit delay, but since
1272 * waiting for over 18 hours to start the scan is a bit silly
1273 * and to keep it aligned with UMAC scans (which only support
1274 * 16-bit delays), trim it down to 16-bits.
1275 */
1276 if (req->delay > U16_MAX) {
1277 IWL_DEBUG_SCAN(mvm,
1278 "delay value is > 16-bits, set to max possible\n");
1279 params.delay = U16_MAX;
1280 } else {
1281 params.delay = req->delay;
1282 }
1283
1284 iwl_mvm_scan_calc_dwell(mvm, vif, &params);
1285
1286 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1287 if (ret)
1288 return ret;
1289
1290 iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1291
1292 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1293 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1294 ret = iwl_mvm_scan_umac(mvm, vif, &params, IWL_MVM_SCAN_SCHED);
1295 } else {
1296 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1297 ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1298 }
1299
1300 if (ret)
1301 return ret;
1302
1303 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1304 if (!ret) {
1305 IWL_DEBUG_SCAN(mvm,
1306 "Sched scan request was sent successfully\n");
1307 mvm->scan_status |= type;
1308 } else {
1309 /* If the scan failed, it usually means that the FW was unable
1310 * to allocate the time events. Warn on it, but maybe we
1311 * should try to send the command again with different params.
1312 */
1313 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1314 }
1315
1316 return ret;
1317 }
1318
1319 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1320 struct iwl_rx_cmd_buffer *rxb)
1321 {
1322 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1323 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1324 u32 uid = __le32_to_cpu(notif->uid);
1325 bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1326
1327 if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1328 return;
1329
1330 /* if the scan is already stopping, we don't need to notify mac80211 */
1331 if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1332 ieee80211_scan_completed(mvm->hw, aborted);
1333 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1334 } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1335 ieee80211_sched_scan_stopped(mvm->hw);
1336 }
1337
1338 mvm->scan_status &= ~mvm->scan_uid_status[uid];
1339
1340 IWL_DEBUG_SCAN(mvm,
1341 "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1342 uid, mvm->scan_uid_status[uid],
1343 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1344 "completed" : "aborted",
1345 iwl_mvm_ebs_status_str(notif->ebs_status));
1346
1347 if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1348 notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1349 mvm->last_ebs_successful = false;
1350
1351 mvm->scan_uid_status[uid] = 0;
1352 }
1353
1354 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1355 struct iwl_rx_cmd_buffer *rxb)
1356 {
1357 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1358 struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1359 u8 buf[256];
1360
1361 IWL_DEBUG_SCAN(mvm,
1362 "UMAC Scan iteration complete: status=0x%x scanned_channels=%d channels list: %s\n",
1363 notif->status, notif->scanned_channels,
1364 iwl_mvm_dump_channel_list(notif->results,
1365 notif->scanned_channels, buf,
1366 sizeof(buf)));
1367 }
1368
1369 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1370 {
1371 struct iwl_umac_scan_abort cmd = {};
1372 int uid, ret;
1373
1374 lockdep_assert_held(&mvm->mutex);
1375
1376 /* We should always get a valid index here, because we already
1377 * checked that this type of scan was running in the generic
1378 * code.
1379 */
1380 uid = iwl_mvm_scan_uid_by_status(mvm, type);
1381 if (WARN_ON_ONCE(uid < 0))
1382 return uid;
1383
1384 cmd.uid = cpu_to_le32(uid);
1385
1386 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1387
1388 ret = iwl_mvm_send_cmd_pdu(mvm,
1389 iwl_cmd_id(SCAN_ABORT_UMAC,
1390 IWL_ALWAYS_LONG_GROUP, 0),
1391 0, sizeof(cmd), &cmd);
1392 if (!ret)
1393 mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1394
1395 return ret;
1396 }
1397
1398 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1399 {
1400 struct iwl_notification_wait wait_scan_done;
1401 static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1402 SCAN_OFFLOAD_COMPLETE, };
1403 int ret;
1404
1405 lockdep_assert_held(&mvm->mutex);
1406
1407 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1408 scan_done_notif,
1409 ARRAY_SIZE(scan_done_notif),
1410 NULL, NULL);
1411
1412 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1413
1414 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1415 ret = iwl_mvm_umac_scan_abort(mvm, type);
1416 else
1417 ret = iwl_mvm_lmac_scan_abort(mvm);
1418
1419 if (ret) {
1420 IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1421 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1422 return ret;
1423 }
1424
1425 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1426
1427 return ret;
1428 }
1429
1430 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1431 {
1432 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1433 return sizeof(struct iwl_scan_req_umac) +
1434 sizeof(struct iwl_scan_channel_cfg_umac) *
1435 mvm->fw->ucode_capa.n_scan_channels +
1436 sizeof(struct iwl_scan_req_umac_tail);
1437
1438 return sizeof(struct iwl_scan_req_lmac) +
1439 sizeof(struct iwl_scan_channel_cfg_lmac) *
1440 mvm->fw->ucode_capa.n_scan_channels +
1441 sizeof(struct iwl_scan_probe_req);
1442 }
1443
1444 /*
1445 * This function is used in nic restart flow, to inform mac80211 about scans
1446 * that was aborted by restart flow or by an assert.
1447 */
1448 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1449 {
1450 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1451 int uid, i;
1452
1453 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
1454 if (uid >= 0) {
1455 ieee80211_scan_completed(mvm->hw, true);
1456 mvm->scan_uid_status[uid] = 0;
1457 }
1458 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
1459 if (uid >= 0 && !mvm->restart_fw) {
1460 ieee80211_sched_scan_stopped(mvm->hw);
1461 mvm->scan_uid_status[uid] = 0;
1462 }
1463
1464 /* We shouldn't have any UIDs still set. Loop over all the
1465 * UIDs to make sure there's nothing left there and warn if
1466 * any is found.
1467 */
1468 for (i = 0; i < mvm->max_scans; i++) {
1469 if (WARN_ONCE(mvm->scan_uid_status[i],
1470 "UMAC scan UID %d status was not cleaned\n",
1471 i))
1472 mvm->scan_uid_status[i] = 0;
1473 }
1474 } else {
1475 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
1476 ieee80211_scan_completed(mvm->hw, true);
1477
1478 /* Sched scan will be restarted by mac80211 in
1479 * restart_hw, so do not report if FW is about to be
1480 * restarted.
1481 */
1482 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1483 ieee80211_sched_scan_stopped(mvm->hw);
1484 }
1485 }
1486
1487 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
1488 {
1489 int ret;
1490
1491 if (!(mvm->scan_status & type))
1492 return 0;
1493
1494 if (iwl_mvm_is_radio_killed(mvm)) {
1495 ret = 0;
1496 goto out;
1497 }
1498
1499 ret = iwl_mvm_scan_stop_wait(mvm, type);
1500 if (!ret)
1501 mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
1502 out:
1503 /* Clear the scan status so the next scan requests will
1504 * succeed and mark the scan as stopping, so that the Rx
1505 * handler doesn't do anything, as the scan was stopped from
1506 * above.
1507 */
1508 mvm->scan_status &= ~type;
1509
1510 if (type == IWL_MVM_SCAN_REGULAR) {
1511 /* Since the rx handler won't do anything now, we have
1512 * to release the scan reference here.
1513 */
1514 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1515 if (notify)
1516 ieee80211_scan_completed(mvm->hw, true);
1517 } else if (notify) {
1518 ieee80211_sched_scan_stopped(mvm->hw);
1519 }
1520
1521 return ret;
1522 }