1 /******************************************************************************
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.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
68 #include <linux/etherdevice.h>
69 #include <net/mac80211.h>
72 #include "fw/api/scan.h"
75 #define IWL_DENSE_EBS_SCAN_RATIO 5
76 #define IWL_SPARSE_EBS_SCAN_RATIO 1
78 enum iwl_mvm_traffic_load {
80 IWL_MVM_TRAFFIC_MEDIUM,
84 #define IWL_SCAN_DWELL_ACTIVE 10
85 #define IWL_SCAN_DWELL_PASSIVE 110
86 #define IWL_SCAN_DWELL_FRAGMENTED 44
87 #define IWL_SCAN_DWELL_EXTENDED 90
89 struct iwl_mvm_scan_timing_params {
94 static struct iwl_mvm_scan_timing_params scan_timing[] = {
95 [IWL_SCAN_TYPE_UNASSOC] = {
99 [IWL_SCAN_TYPE_WILD] = {
103 [IWL_SCAN_TYPE_MILD] = {
107 [IWL_SCAN_TYPE_FRAGMENTED] = {
113 struct iwl_mvm_scan_params {
114 enum iwl_mvm_scan_type type;
118 struct cfg80211_ssid *ssids;
119 struct ieee80211_channel **channels;
126 struct iwl_scan_probe_req preq;
127 struct cfg80211_match_set *match_sets;
129 struct cfg80211_sched_scan_plan *scan_plans;
130 u32 measurement_dwell;
133 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
135 if (mvm->scan_rx_ant != ANT_NONE)
136 return mvm->scan_rx_ant;
137 return iwl_mvm_get_valid_rx_ant(mvm);
140 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
145 rx_ant = iwl_mvm_scan_rx_ant(mvm);
146 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
147 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
148 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
149 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
150 return cpu_to_le16(rx_chain);
153 static __le32 iwl_mvm_scan_rxon_flags(enum nl80211_band band)
155 if (band == NL80211_BAND_2GHZ)
156 return cpu_to_le32(PHY_BAND_24);
158 return cpu_to_le32(PHY_BAND_5);
162 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
167 mvm->scan_last_antenna_idx =
168 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
169 mvm->scan_last_antenna_idx);
170 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
172 if (band == NL80211_BAND_2GHZ && !no_cck)
173 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
176 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
179 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
180 struct ieee80211_vif *vif)
182 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
183 int *global_cnt = data;
185 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
186 mvmvif->phy_ctxt->id < NUM_PHY_CTX)
190 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
192 return IWL_MVM_TRAFFIC_LOW;
196 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm, bool p2p_device)
199 enum iwl_mvm_traffic_load load;
202 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
203 IEEE80211_IFACE_ITER_NORMAL,
204 iwl_mvm_scan_condition_iterator,
207 return IWL_SCAN_TYPE_UNASSOC;
209 load = iwl_mvm_get_traffic_load(mvm);
210 low_latency = iwl_mvm_low_latency(mvm);
212 if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) && !p2p_device &&
213 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_FRAGMENTED_SCAN))
214 return IWL_SCAN_TYPE_FRAGMENTED;
216 if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
217 return IWL_SCAN_TYPE_MILD;
219 return IWL_SCAN_TYPE_WILD;
223 iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
224 struct cfg80211_scan_request *req,
225 struct iwl_mvm_scan_params *params)
230 if (req->duration_mandatory &&
231 req->duration > scan_timing[params->type].max_out_time) {
233 "Measurement scan - too long dwell %hu (max out time %u)\n",
235 scan_timing[params->type].max_out_time);
239 return min_t(u32, (u32)req->duration,
240 scan_timing[params->type].max_out_time);
243 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
245 /* require rrm scan whenever the fw supports it */
246 return fw_has_capa(&mvm->fw->ucode_capa,
247 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
250 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
254 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
256 /* we create the 802.11 header and SSID element */
257 max_probe_len -= 24 + 2;
259 /* DS parameter set element is added on 2.4GHZ band if required */
260 if (iwl_mvm_rrm_scan_needed(mvm))
263 return max_probe_len;
266 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
268 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
270 /* TODO: [BUG] This function should return the maximum allowed size of
271 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
272 * in the same command. So the correct implementation of this function
273 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
274 * command has only 512 bytes and it would leave us with about 240
275 * bytes for scan IEs, which is clearly not enough. So meanwhile
276 * we will report an incorrect value. This may result in a failure to
277 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
278 * functions with -ENOBUFS, if a large enough probe will be provided.
283 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
284 struct iwl_rx_cmd_buffer *rxb)
286 struct iwl_rx_packet *pkt = rxb_addr(rxb);
287 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
290 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
291 notif->status, notif->scanned_channels);
293 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
294 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
295 ieee80211_sched_scan_results(mvm->hw);
296 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
300 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
301 struct iwl_rx_cmd_buffer *rxb)
303 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
304 ieee80211_sched_scan_results(mvm->hw);
307 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
310 case IWL_SCAN_EBS_SUCCESS:
312 case IWL_SCAN_EBS_INACTIVE:
314 case IWL_SCAN_EBS_FAILED:
315 case IWL_SCAN_EBS_CHAN_NOT_FOUND:
321 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
322 struct iwl_rx_cmd_buffer *rxb)
324 struct iwl_rx_packet *pkt = rxb_addr(rxb);
325 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
326 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
328 /* If this happens, the firmware has mistakenly sent an LMAC
329 * notification during UMAC scans -- warn and ignore it.
331 if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
332 IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
335 /* scan status must be locked for proper checking */
336 lockdep_assert_held(&mvm->mutex);
338 /* We first check if we were stopping a scan, in which case we
339 * just clear the stopping flag. Then we check if it was a
340 * firmware initiated stop, in which case we need to inform
342 * Note that we can have a stopping and a running scan
343 * simultaneously, but we can't have two different types of
344 * scans stopping or running at the same time (since LMAC
345 * doesn't support it).
348 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
349 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
351 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
352 aborted ? "aborted" : "completed",
353 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
355 "Last line %d, Last iteration %d, Time after last iteration %d\n",
356 scan_notif->last_schedule_line,
357 scan_notif->last_schedule_iteration,
358 __le32_to_cpu(scan_notif->time_after_last_iter));
360 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
361 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
362 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
363 aborted ? "aborted" : "completed",
364 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
366 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
367 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
368 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
370 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
371 aborted ? "aborted" : "completed",
372 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
374 "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
375 scan_notif->last_schedule_line,
376 scan_notif->last_schedule_iteration,
377 __le32_to_cpu(scan_notif->time_after_last_iter));
379 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
380 ieee80211_sched_scan_stopped(mvm->hw);
381 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
382 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
383 struct cfg80211_scan_info info = {
387 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
388 aborted ? "aborted" : "completed",
389 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
391 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
392 ieee80211_scan_completed(mvm->hw, &info);
393 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
394 cancel_delayed_work(&mvm->scan_timeout_dwork);
397 "got scan complete notification but no scan is running\n");
400 mvm->last_ebs_successful =
401 scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
402 scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
405 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
409 for (i = 0; i < PROBE_OPTION_MAX; i++) {
410 if (!ssid_list[i].len)
412 if (ssid_list[i].len == ssid_len &&
413 !memcmp(ssid_list->ssid, ssid, ssid_len))
419 /* We insert the SSIDs in an inverted order, because the FW will
422 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
423 struct iwl_ssid_ie *ssids,
430 * copy SSIDs from match list.
431 * iwl_config_sched_scan_profiles() uses the order of these ssids to
434 for (i = 0, j = params->n_match_sets - 1;
435 j >= 0 && i < PROBE_OPTION_MAX;
437 /* skip empty SSID matchsets */
438 if (!params->match_sets[j].ssid.ssid_len)
440 ssids[i].id = WLAN_EID_SSID;
441 ssids[i].len = params->match_sets[j].ssid.ssid_len;
442 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
446 /* add SSIDs from scan SSID list */
448 for (j = params->n_ssids - 1;
449 j >= 0 && i < PROBE_OPTION_MAX;
451 index = iwl_ssid_exist(params->ssids[j].ssid,
452 params->ssids[j].ssid_len,
455 ssids[i].id = WLAN_EID_SSID;
456 ssids[i].len = params->ssids[j].ssid_len;
457 memcpy(ssids[i].ssid, params->ssids[j].ssid,
459 *ssid_bitmap |= BIT(i);
461 *ssid_bitmap |= BIT(index);
467 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
468 struct cfg80211_sched_scan_request *req)
470 struct iwl_scan_offload_profile *profile;
471 struct iwl_scan_offload_profile_cfg *profile_cfg;
472 struct iwl_scan_offload_blacklist *blacklist;
473 struct iwl_host_cmd cmd = {
474 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
475 .len[1] = sizeof(*profile_cfg),
476 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
477 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
483 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
486 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
487 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
489 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
491 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
495 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
501 cmd.data[0] = blacklist;
502 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
503 cmd.data[1] = profile_cfg;
505 /* No blacklist configuration */
507 profile_cfg->num_profiles = req->n_match_sets;
508 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
509 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
510 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
511 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
512 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
514 for (i = 0; i < req->n_match_sets; i++) {
515 profile = &profile_cfg->profiles[i];
516 profile->ssid_index = i;
517 /* Support any cipher and auth algorithm */
518 profile->unicast_cipher = 0xff;
519 profile->auth_alg = 0xff;
520 profile->network_type = IWL_NETWORK_TYPE_ANY;
521 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
522 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
525 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
527 ret = iwl_mvm_send_cmd(mvm, &cmd);
535 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
536 struct cfg80211_sched_scan_request *req)
538 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
540 "Sending scheduled scan with filtering, n_match_sets %d\n",
542 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
546 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
548 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
552 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
555 struct iwl_host_cmd cmd = {
556 .id = SCAN_OFFLOAD_ABORT_CMD,
560 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
564 if (status != CAN_ABORT_STATUS) {
566 * The scan abort will return 1 for success or
567 * 2 for "failure". A failure condition can be
568 * due to simply not being in an active scan which
569 * can occur if we send the scan abort before the
570 * microcode has notified us that a scan is completed.
572 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
579 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
580 struct iwl_scan_req_tx_cmd *tx_cmd,
583 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
585 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
588 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
590 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
592 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
595 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
599 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
600 struct ieee80211_channel **channels,
601 int n_channels, u32 ssid_bitmap,
602 struct iwl_scan_req_lmac *cmd)
604 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
607 for (i = 0; i < n_channels; i++) {
608 channel_cfg[i].channel_num =
609 cpu_to_le16(channels[i]->hw_value);
610 channel_cfg[i].iter_count = cpu_to_le16(1);
611 channel_cfg[i].iter_interval = 0;
612 channel_cfg[i].flags =
613 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
618 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
619 size_t len, u8 *const pos)
621 static const u8 before_ds_params[] = {
625 WLAN_EID_EXT_SUPP_RATES,
630 if (!iwl_mvm_rrm_scan_needed(mvm)) {
631 memcpy(newpos, ies, len);
635 offs = ieee80211_ie_split(ies, len,
637 ARRAY_SIZE(before_ds_params),
640 memcpy(newpos, ies, offs);
643 /* Add a placeholder for DS Parameter Set element */
644 *newpos++ = WLAN_EID_DS_PARAMS;
648 memcpy(newpos, ies + offs, len - offs);
649 newpos += len - offs;
655 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
656 struct ieee80211_scan_ies *ies,
657 struct iwl_mvm_scan_params *params)
659 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
661 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
662 params->mac_addr : NULL;
665 * Unfortunately, right now the offload scan doesn't support randomising
666 * within the firmware, so until the firmware API is ready we implement
667 * it in the driver. This means that the scan iterations won't really be
668 * random, only when it's restarted, but at least that helps a bit.
671 get_random_mask_addr(frame->sa, mac_addr,
672 params->mac_addr_mask);
674 memcpy(frame->sa, vif->addr, ETH_ALEN);
676 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
677 eth_broadcast_addr(frame->da);
678 eth_broadcast_addr(frame->bssid);
681 pos = frame->u.probe_req.variable;
682 *pos++ = WLAN_EID_SSID;
685 params->preq.mac_header.offset = 0;
686 params->preq.mac_header.len = cpu_to_le16(24 + 2);
688 /* Insert ds parameter set element on 2.4 GHz band */
689 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
690 ies->ies[NL80211_BAND_2GHZ],
691 ies->len[NL80211_BAND_2GHZ],
693 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
694 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
697 memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
698 ies->len[NL80211_BAND_5GHZ]);
699 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
700 params->preq.band_data[1].len =
701 cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
702 pos += ies->len[NL80211_BAND_5GHZ];
704 memcpy(pos, ies->common_ies, ies->common_ie_len);
705 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
706 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
709 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
710 struct iwl_scan_req_lmac *cmd,
711 struct iwl_mvm_scan_params *params)
713 cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
714 cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
715 cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
716 cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
717 cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
718 cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
719 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
722 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
723 struct ieee80211_scan_ies *ies,
726 return ((n_ssids <= PROBE_OPTION_MAX) &&
727 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
728 (ies->common_ie_len +
729 ies->len[NL80211_BAND_2GHZ] +
730 ies->len[NL80211_BAND_5GHZ] <=
731 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
734 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
735 struct ieee80211_vif *vif)
737 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
739 /* We can only use EBS if:
740 * 1. the feature is supported;
741 * 2. the last EBS was successful;
742 * 3. if only single scan, the single scan EBS API is supported;
743 * 4. it's not a p2p find operation.
745 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
746 mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
747 vif->type != NL80211_IFTYPE_P2P_DEVICE);
750 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
752 return params->n_scan_plans == 1 &&
753 params->scan_plans[0].iterations == 1;
756 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
757 struct iwl_mvm_scan_params *params,
758 struct ieee80211_vif *vif)
762 if (params->n_ssids == 0)
763 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
765 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
766 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
768 if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
769 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
771 if (iwl_mvm_rrm_scan_needed(mvm))
772 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
774 if (params->pass_all)
775 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
777 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
779 #ifdef CONFIG_IWLWIFI_DEBUGFS
780 if (mvm->scan_iter_notif_enabled)
781 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
784 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
785 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
787 if (iwl_mvm_is_regular_scan(params) &&
788 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
789 params->type != IWL_SCAN_TYPE_FRAGMENTED)
790 flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
795 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
796 struct iwl_mvm_scan_params *params)
798 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
799 struct iwl_scan_probe_req *preq =
800 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
801 mvm->fw->ucode_capa.n_scan_channels);
805 lockdep_assert_held(&mvm->mutex);
807 memset(cmd, 0, ksize(cmd));
809 if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
812 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
814 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
815 cmd->iter_num = cpu_to_le32(1);
816 cmd->n_channels = (u8)params->n_channels;
818 cmd->delay = cpu_to_le32(params->delay);
820 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
823 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
824 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
825 MAC_FILTER_IN_BEACON);
826 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
827 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
829 /* this API uses bits 1-20 instead of 0-19 */
832 for (i = 0; i < params->n_scan_plans; i++) {
833 struct cfg80211_sched_scan_plan *scan_plan =
834 ¶ms->scan_plans[i];
836 cmd->schedule[i].delay =
837 cpu_to_le16(scan_plan->interval);
838 cmd->schedule[i].iterations = scan_plan->iterations;
839 cmd->schedule[i].full_scan_mul = 1;
843 * If the number of iterations of the last scan plan is set to
844 * zero, it should run infinitely. However, this is not always the case.
845 * For example, when regular scan is requested the driver sets one scan
846 * plan with one iteration.
848 if (!cmd->schedule[i - 1].iterations)
849 cmd->schedule[i - 1].iterations = 0xff;
851 if (iwl_mvm_scan_use_ebs(mvm, vif)) {
852 cmd->channel_opt[0].flags =
853 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
854 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
855 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
856 cmd->channel_opt[0].non_ebs_ratio =
857 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
858 cmd->channel_opt[1].flags =
859 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
860 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
861 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
862 cmd->channel_opt[1].non_ebs_ratio =
863 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
866 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
867 params->n_channels, ssid_bitmap, cmd);
869 *preq = params->preq;
874 static int rate_to_scan_rate_flag(unsigned int rate)
876 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
877 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
878 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
879 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
880 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
881 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
882 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
883 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
884 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
885 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
886 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
887 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
888 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
891 return rate_to_scan_rate[rate];
894 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
896 struct ieee80211_supported_band *band;
897 unsigned int rates = 0;
900 band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
901 for (i = 0; i < band->n_bitrates; i++)
902 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
903 band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
904 for (i = 0; i < band->n_bitrates; i++)
905 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
907 /* Set both basic rates and supported rates */
908 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
910 return cpu_to_le32(rates);
913 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
914 struct iwl_scan_dwell *dwell)
916 dwell->active = IWL_SCAN_DWELL_ACTIVE;
917 dwell->passive = IWL_SCAN_DWELL_PASSIVE;
918 dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
919 dwell->extended = IWL_SCAN_DWELL_EXTENDED;
922 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels)
924 struct ieee80211_supported_band *band;
927 band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
928 for (i = 0; i < band->n_channels; i++, j++)
929 channels[j] = band->channels[i].hw_value;
930 band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
931 for (i = 0; i < band->n_channels; i++, j++)
932 channels[j] = band->channels[i].hw_value;
935 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
936 u32 flags, u8 channel_flags)
938 enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
939 struct iwl_scan_config_v1 *cfg = config;
941 cfg->flags = cpu_to_le32(flags);
942 cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
943 cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
944 cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
945 cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
946 cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
948 iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
950 memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
952 cfg->bcast_sta_id = mvm->aux_sta.sta_id;
953 cfg->channel_flags = channel_flags;
955 iwl_mvm_fill_channels(mvm, cfg->channel_array);
958 static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
959 u32 flags, u8 channel_flags)
961 enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
962 struct iwl_scan_config *cfg = config;
964 cfg->flags = cpu_to_le32(flags);
965 cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
966 cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
967 cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
968 cfg->out_of_channel_time[0] =
969 cpu_to_le32(scan_timing[type].max_out_time);
970 cfg->suspend_time[0] = cpu_to_le32(scan_timing[type].suspend_time);
972 if (iwl_mvm_is_cdb_supported(mvm)) {
973 cfg->suspend_time[1] =
974 cpu_to_le32(scan_timing[type].suspend_time);
975 cfg->out_of_channel_time[1] =
976 cpu_to_le32(scan_timing[type].max_out_time);
979 iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
981 memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
983 cfg->bcast_sta_id = mvm->aux_sta.sta_id;
984 cfg->channel_flags = channel_flags;
986 iwl_mvm_fill_channels(mvm, cfg->channel_array);
989 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
993 struct iwl_host_cmd cmd = {
994 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
996 enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
998 mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
999 mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1003 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1006 if (type == mvm->scan_type)
1009 if (iwl_mvm_has_new_tx_api(mvm))
1010 cmd_size = sizeof(struct iwl_scan_config);
1012 cmd_size = sizeof(struct iwl_scan_config_v1);
1013 cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1015 cfg = kzalloc(cmd_size, GFP_KERNEL);
1019 flags = SCAN_CONFIG_FLAG_ACTIVATE |
1020 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1021 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1022 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1023 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1024 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1025 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1026 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1027 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1028 SCAN_CONFIG_N_CHANNELS(num_channels) |
1029 (type == IWL_SCAN_TYPE_FRAGMENTED ?
1030 SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1031 SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1033 channel_flags = IWL_CHANNEL_FLAG_EBS |
1034 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1035 IWL_CHANNEL_FLAG_EBS_ADD |
1036 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1038 if (iwl_mvm_has_new_tx_api(mvm)) {
1039 flags |= (type == IWL_SCAN_TYPE_FRAGMENTED) ?
1040 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1041 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1042 iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags);
1044 iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags);
1048 cmd.len[0] = cmd_size;
1049 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1051 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1053 ret = iwl_mvm_send_cmd(mvm, &cmd);
1055 mvm->scan_type = type;
1061 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1065 for (i = 0; i < mvm->max_scans; i++)
1066 if (mvm->scan_uid_status[i] == status)
1072 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1073 struct iwl_scan_req_umac *cmd,
1074 struct iwl_mvm_scan_params *params)
1076 struct iwl_mvm_scan_timing_params *timing = &scan_timing[params->type];
1078 if (params->measurement_dwell) {
1079 cmd->active_dwell = params->measurement_dwell;
1080 cmd->passive_dwell = params->measurement_dwell;
1081 cmd->extended_dwell = params->measurement_dwell;
1083 cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
1084 cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1085 cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
1087 cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1089 if (iwl_mvm_has_new_tx_api(mvm)) {
1090 cmd->v6.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1091 cmd->v6.max_out_time[0] = cpu_to_le32(timing->max_out_time);
1092 cmd->v6.suspend_time[0] = cpu_to_le32(timing->suspend_time);
1093 if (iwl_mvm_is_cdb_supported(mvm)) {
1094 cmd->v6.max_out_time[1] =
1095 cpu_to_le32(timing->max_out_time);
1096 cmd->v6.suspend_time[1] =
1097 cpu_to_le32(timing->suspend_time);
1100 cmd->v1.max_out_time = cpu_to_le32(timing->max_out_time);
1101 cmd->v1.suspend_time = cpu_to_le32(timing->suspend_time);
1102 cmd->v1.scan_priority =
1103 cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1106 if (iwl_mvm_is_regular_scan(params))
1107 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1109 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1113 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1114 struct ieee80211_channel **channels,
1115 int n_channels, u32 ssid_bitmap,
1116 struct iwl_scan_channel_cfg_umac *channel_cfg)
1120 for (i = 0; i < n_channels; i++) {
1121 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1122 channel_cfg[i].channel_num = channels[i]->hw_value;
1123 channel_cfg[i].iter_count = 1;
1124 channel_cfg[i].iter_interval = 0;
1128 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1129 struct iwl_mvm_scan_params *params,
1130 struct ieee80211_vif *vif)
1134 if (params->n_ssids == 0)
1135 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1137 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1138 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1140 if (params->type == IWL_SCAN_TYPE_FRAGMENTED) {
1141 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1142 if (iwl_mvm_is_cdb_supported(mvm))
1143 flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1146 if (iwl_mvm_rrm_scan_needed(mvm))
1147 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1149 if (params->pass_all)
1150 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1152 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1154 if (!iwl_mvm_is_regular_scan(params))
1155 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1157 if (params->measurement_dwell)
1158 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1160 #ifdef CONFIG_IWLWIFI_DEBUGFS
1161 if (mvm->scan_iter_notif_enabled)
1162 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1165 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1166 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1168 if (iwl_mvm_is_regular_scan(params) &&
1169 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1170 params->type != IWL_SCAN_TYPE_FRAGMENTED)
1171 flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1176 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1177 struct iwl_mvm_scan_params *params,
1180 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1181 void *cmd_data = iwl_mvm_has_new_tx_api(mvm) ?
1182 (void *)&cmd->v6.data : (void *)&cmd->v1.data;
1183 struct iwl_scan_req_umac_tail *sec_part = cmd_data +
1184 sizeof(struct iwl_scan_channel_cfg_umac) *
1185 mvm->fw->ucode_capa.n_scan_channels;
1187 u32 ssid_bitmap = 0;
1188 u8 channel_flags = 0;
1189 struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1191 lockdep_assert_held(&mvm->mutex);
1193 if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1196 uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1200 memset(cmd, 0, ksize(cmd));
1202 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1204 mvm->scan_uid_status[uid] = type;
1206 cmd->uid = cpu_to_le32(uid);
1207 cmd->general_flags = cpu_to_le16(iwl_mvm_scan_umac_flags(mvm, params,
1209 cmd->scan_start_mac_id = scan_vif->id;
1211 if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1212 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1214 if (iwl_mvm_scan_use_ebs(mvm, vif))
1215 channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1216 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1217 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1219 if (iwl_mvm_has_new_tx_api(mvm)) {
1220 cmd->v6.channel_flags = channel_flags;
1221 cmd->v6.n_channels = params->n_channels;
1223 cmd->v1.channel_flags = channel_flags;
1224 cmd->v1.n_channels = params->n_channels;
1227 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1229 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1230 params->n_channels, ssid_bitmap,
1233 for (i = 0; i < params->n_scan_plans; i++) {
1234 struct cfg80211_sched_scan_plan *scan_plan =
1235 ¶ms->scan_plans[i];
1237 sec_part->schedule[i].iter_count = scan_plan->iterations;
1238 sec_part->schedule[i].interval =
1239 cpu_to_le16(scan_plan->interval);
1243 * If the number of iterations of the last scan plan is set to
1244 * zero, it should run infinitely. However, this is not always the case.
1245 * For example, when regular scan is requested the driver sets one scan
1246 * plan with one iteration.
1248 if (!sec_part->schedule[i - 1].iter_count)
1249 sec_part->schedule[i - 1].iter_count = 0xff;
1251 sec_part->delay = cpu_to_le16(params->delay);
1252 sec_part->preq = params->preq;
1257 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1259 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1262 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1264 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1265 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1267 /* This looks a bit arbitrary, but the idea is that if we run
1268 * out of possible simultaneous scans and the userspace is
1269 * trying to run a scan type that is already running, we
1270 * return -EBUSY. But if the userspace wants to start a
1271 * different type of scan, we stop the opposite type to make
1272 * space for the new request. The reason is backwards
1273 * compatibility with old wpa_supplicant that wouldn't stop a
1274 * scheduled scan before starting a normal scan.
1277 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1280 /* Use a switch, even though this is a bitmask, so that more
1281 * than one bits set will fall in default and we will warn.
1284 case IWL_MVM_SCAN_REGULAR:
1285 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1287 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1288 case IWL_MVM_SCAN_SCHED:
1289 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1291 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1292 case IWL_MVM_SCAN_NETDETECT:
1293 /* For non-unified images, there's no need to stop
1294 * anything for net-detect since the firmware is
1295 * restarted anyway. This way, any sched scans that
1296 * were running will be restarted when we resume.
1301 /* If this is a unified image and we ran out of scans,
1302 * we need to stop something. Prefer stopping regular
1303 * scans, because the results are useless at this
1304 * point, and we should be able to keep running
1305 * another scheduled scan while suspended.
1307 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1308 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
1310 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1311 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
1314 /* fall through, something is wrong if no scan was
1315 * running but we ran out of scans.
1325 #define SCAN_TIMEOUT 20000
1327 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
1329 struct delayed_work *delayed_work = to_delayed_work(work);
1330 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1331 scan_timeout_dwork);
1333 IWL_ERR(mvm, "regular scan timed out\n");
1335 iwl_force_nmi(mvm->trans);
1338 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1339 struct cfg80211_scan_request *req,
1340 struct ieee80211_scan_ies *ies)
1342 struct iwl_host_cmd hcmd = {
1343 .len = { iwl_mvm_scan_size(mvm), },
1344 .data = { mvm->scan_cmd, },
1345 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1347 struct iwl_mvm_scan_params params = {};
1349 struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
1351 lockdep_assert_held(&mvm->mutex);
1353 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1354 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1358 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1362 /* we should have failed registration if scan_cmd was NULL */
1363 if (WARN_ON(!mvm->scan_cmd))
1366 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1369 params.n_ssids = req->n_ssids;
1370 params.flags = req->flags;
1371 params.n_channels = req->n_channels;
1373 params.ssids = req->ssids;
1374 params.channels = req->channels;
1375 params.mac_addr = req->mac_addr;
1376 params.mac_addr_mask = req->mac_addr_mask;
1377 params.no_cck = req->no_cck;
1378 params.pass_all = true;
1379 params.n_match_sets = 0;
1380 params.match_sets = NULL;
1382 params.scan_plans = &scan_plan;
1383 params.n_scan_plans = 1;
1386 iwl_mvm_get_scan_type(mvm,
1387 vif->type == NL80211_IFTYPE_P2P_DEVICE);
1389 ret = iwl_mvm_get_measurement_dwell(mvm, req, ¶ms);
1393 params.measurement_dwell = ret;
1395 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1397 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1398 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1399 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms,
1400 IWL_MVM_SCAN_REGULAR);
1402 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1403 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1409 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1411 /* If the scan failed, it usually means that the FW was unable
1412 * to allocate the time events. Warn on it, but maybe we
1413 * should try to send the command again with different params.
1415 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1419 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1420 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1421 mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
1422 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1424 schedule_delayed_work(&mvm->scan_timeout_dwork,
1425 msecs_to_jiffies(SCAN_TIMEOUT));
1430 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1431 struct ieee80211_vif *vif,
1432 struct cfg80211_sched_scan_request *req,
1433 struct ieee80211_scan_ies *ies,
1436 struct iwl_host_cmd hcmd = {
1437 .len = { iwl_mvm_scan_size(mvm), },
1438 .data = { mvm->scan_cmd, },
1439 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1441 struct iwl_mvm_scan_params params = {};
1444 lockdep_assert_held(&mvm->mutex);
1446 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1447 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1451 ret = iwl_mvm_check_running_scans(mvm, type);
1455 /* we should have failed registration if scan_cmd was NULL */
1456 if (WARN_ON(!mvm->scan_cmd))
1459 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1462 params.n_ssids = req->n_ssids;
1463 params.flags = req->flags;
1464 params.n_channels = req->n_channels;
1465 params.ssids = req->ssids;
1466 params.channels = req->channels;
1467 params.mac_addr = req->mac_addr;
1468 params.mac_addr_mask = req->mac_addr_mask;
1469 params.no_cck = false;
1470 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1471 params.n_match_sets = req->n_match_sets;
1472 params.match_sets = req->match_sets;
1473 if (!req->n_scan_plans)
1476 params.n_scan_plans = req->n_scan_plans;
1477 params.scan_plans = req->scan_plans;
1480 iwl_mvm_get_scan_type(mvm,
1481 vif->type == NL80211_IFTYPE_P2P_DEVICE);
1483 /* In theory, LMAC scans can handle a 32-bit delay, but since
1484 * waiting for over 18 hours to start the scan is a bit silly
1485 * and to keep it aligned with UMAC scans (which only support
1486 * 16-bit delays), trim it down to 16-bits.
1488 if (req->delay > U16_MAX) {
1490 "delay value is > 16-bits, set to max possible\n");
1491 params.delay = U16_MAX;
1493 params.delay = req->delay;
1496 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1500 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1502 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1503 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1504 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms, type);
1506 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1507 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1513 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1516 "Sched scan request was sent successfully\n");
1517 mvm->scan_status |= type;
1519 /* If the scan failed, it usually means that the FW was unable
1520 * to allocate the time events. Warn on it, but maybe we
1521 * should try to send the command again with different params.
1523 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1529 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1530 struct iwl_rx_cmd_buffer *rxb)
1532 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1533 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1534 u32 uid = __le32_to_cpu(notif->uid);
1535 bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1537 if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1540 /* if the scan is already stopping, we don't need to notify mac80211 */
1541 if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1542 struct cfg80211_scan_info info = {
1544 .scan_start_tsf = mvm->scan_start,
1547 memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
1548 ieee80211_scan_completed(mvm->hw, &info);
1549 mvm->scan_vif = NULL;
1550 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1551 cancel_delayed_work(&mvm->scan_timeout_dwork);
1552 } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1553 ieee80211_sched_scan_stopped(mvm->hw);
1554 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1557 mvm->scan_status &= ~mvm->scan_uid_status[uid];
1559 "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1560 uid, mvm->scan_uid_status[uid],
1561 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1562 "completed" : "aborted",
1563 iwl_mvm_ebs_status_str(notif->ebs_status));
1565 "Last line %d, Last iteration %d, Time from last iteration %d\n",
1566 notif->last_schedule, notif->last_iter,
1567 __le32_to_cpu(notif->time_from_last_iter));
1569 if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1570 notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1571 mvm->last_ebs_successful = false;
1573 mvm->scan_uid_status[uid] = 0;
1576 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1577 struct iwl_rx_cmd_buffer *rxb)
1579 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1580 struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1582 mvm->scan_start = le64_to_cpu(notif->start_tsf);
1585 "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
1586 notif->status, notif->scanned_channels);
1588 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
1589 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
1590 ieee80211_sched_scan_results(mvm->hw);
1591 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
1595 "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
1599 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1601 struct iwl_umac_scan_abort cmd = {};
1604 lockdep_assert_held(&mvm->mutex);
1606 /* We should always get a valid index here, because we already
1607 * checked that this type of scan was running in the generic
1610 uid = iwl_mvm_scan_uid_by_status(mvm, type);
1611 if (WARN_ON_ONCE(uid < 0))
1614 cmd.uid = cpu_to_le32(uid);
1616 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1618 ret = iwl_mvm_send_cmd_pdu(mvm,
1619 iwl_cmd_id(SCAN_ABORT_UMAC,
1620 IWL_ALWAYS_LONG_GROUP, 0),
1621 0, sizeof(cmd), &cmd);
1623 mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1628 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1630 struct iwl_notification_wait wait_scan_done;
1631 static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1632 SCAN_OFFLOAD_COMPLETE, };
1635 lockdep_assert_held(&mvm->mutex);
1637 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1639 ARRAY_SIZE(scan_done_notif),
1642 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1644 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1645 ret = iwl_mvm_umac_scan_abort(mvm, type);
1647 ret = iwl_mvm_lmac_scan_abort(mvm);
1650 IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1651 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1655 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1660 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1662 int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
1664 if (iwl_mvm_has_new_tx_api(mvm))
1665 base_size = IWL_SCAN_REQ_UMAC_SIZE;
1667 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1669 sizeof(struct iwl_scan_channel_cfg_umac) *
1670 mvm->fw->ucode_capa.n_scan_channels +
1671 sizeof(struct iwl_scan_req_umac_tail);
1673 return sizeof(struct iwl_scan_req_lmac) +
1674 sizeof(struct iwl_scan_channel_cfg_lmac) *
1675 mvm->fw->ucode_capa.n_scan_channels +
1676 sizeof(struct iwl_scan_probe_req);
1680 * This function is used in nic restart flow, to inform mac80211 about scans
1681 * that was aborted by restart flow or by an assert.
1683 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1685 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1688 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
1690 struct cfg80211_scan_info info = {
1694 ieee80211_scan_completed(mvm->hw, &info);
1695 mvm->scan_uid_status[uid] = 0;
1697 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
1698 if (uid >= 0 && !mvm->fw_restart) {
1699 ieee80211_sched_scan_stopped(mvm->hw);
1700 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1701 mvm->scan_uid_status[uid] = 0;
1704 /* We shouldn't have any UIDs still set. Loop over all the
1705 * UIDs to make sure there's nothing left there and warn if
1708 for (i = 0; i < mvm->max_scans; i++) {
1709 if (WARN_ONCE(mvm->scan_uid_status[i],
1710 "UMAC scan UID %d status was not cleaned\n",
1712 mvm->scan_uid_status[i] = 0;
1715 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
1716 struct cfg80211_scan_info info = {
1720 ieee80211_scan_completed(mvm->hw, &info);
1723 /* Sched scan will be restarted by mac80211 in
1724 * restart_hw, so do not report if FW is about to be
1727 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
1729 ieee80211_sched_scan_stopped(mvm->hw);
1730 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1735 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
1739 if (!(mvm->scan_status & type))
1742 if (iwl_mvm_is_radio_killed(mvm)) {
1747 ret = iwl_mvm_scan_stop_wait(mvm, type);
1749 mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
1751 /* Clear the scan status so the next scan requests will
1752 * succeed and mark the scan as stopping, so that the Rx
1753 * handler doesn't do anything, as the scan was stopped from
1756 mvm->scan_status &= ~type;
1758 if (type == IWL_MVM_SCAN_REGULAR) {
1759 /* Since the rx handler won't do anything now, we have
1760 * to release the scan reference here.
1762 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1763 cancel_delayed_work(&mvm->scan_timeout_dwork);
1765 struct cfg80211_scan_info info = {
1769 ieee80211_scan_completed(mvm->hw, &info);
1771 } else if (notify) {
1772 ieee80211_sched_scan_stopped(mvm->hw);
1773 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;