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
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.
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.
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,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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
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.
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.
64 *****************************************************************************/
66 #include <linux/etherdevice.h>
67 #include <net/mac80211.h>
70 #include "fw-api-scan.h"
72 #define IWL_DENSE_EBS_SCAN_RATIO 5
73 #define IWL_SPARSE_EBS_SCAN_RATIO 1
75 struct iwl_mvm_scan_params {
78 bool passive_fragmented;
82 struct cfg80211_ssid *ssids;
83 struct ieee80211_channel **channels;
84 u16 interval; /* interval between scans (in secs) */
91 struct iwl_scan_probe_req preq;
92 struct cfg80211_match_set *match_sets;
97 } dwell[IEEE80211_NUM_BANDS];
100 u8 full_scan_mul; /* not used for UMAC */
104 static int iwl_umac_scan_stop(struct iwl_mvm *mvm, int type, bool notify);
106 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
108 if (mvm->scan_rx_ant != ANT_NONE)
109 return mvm->scan_rx_ant;
110 return iwl_mvm_get_valid_rx_ant(mvm);
113 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
118 rx_ant = iwl_mvm_scan_rx_ant(mvm);
119 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
120 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
121 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
122 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
123 return cpu_to_le16(rx_chain);
126 static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
128 if (band == IEEE80211_BAND_2GHZ)
129 return cpu_to_le32(PHY_BAND_24);
131 return cpu_to_le32(PHY_BAND_5);
135 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
140 mvm->scan_last_antenna_idx =
141 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
142 mvm->scan_last_antenna_idx);
143 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
145 if (band == IEEE80211_BAND_2GHZ && !no_cck)
146 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
149 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
153 * If req->n_ssids > 0, it means we should do an active scan.
154 * In case of active scan w/o directed scan, we receive a zero-length SSID
155 * just to notify that this scan is active and not passive.
156 * In order to notify the FW of the number of SSIDs we wish to scan (including
157 * the zero-length one), we need to set the corresponding bits in chan->type,
158 * one for each SSID, and set the active bit (first). If the first SSID is
159 * already included in the probe template, so we need to set only
160 * req->n_ssids - 1 bits in addition to the first bit.
162 static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
163 enum ieee80211_band band, int n_ssids)
165 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
167 if (band == IEEE80211_BAND_2GHZ)
168 return 20 + 3 * (n_ssids + 1);
169 return 10 + 2 * (n_ssids + 1);
172 static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
173 enum ieee80211_band band)
175 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
177 return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
180 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
181 struct ieee80211_vif *vif)
183 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
184 int *global_cnt = data;
186 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
187 mvmvif->phy_ctxt->id < MAX_PHYS)
191 static void iwl_mvm_scan_calc_dwell(struct iwl_mvm *mvm,
192 struct ieee80211_vif *vif,
193 struct iwl_mvm_scan_params *params)
196 enum ieee80211_band band;
197 u8 frag_passive_dwell = 0;
199 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
200 IEEE80211_IFACE_ITER_NORMAL,
201 iwl_mvm_scan_condition_iterator,
206 params->suspend_time = 30;
207 params->max_out_time = 120;
209 if (iwl_mvm_low_latency(mvm)) {
210 if (mvm->fw->ucode_capa.api[0] &
211 IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
212 params->suspend_time = 105;
214 * If there is more than one active interface make
215 * passive scan more fragmented.
217 frag_passive_dwell = 40;
218 params->max_out_time = frag_passive_dwell;
220 params->suspend_time = 120;
221 params->max_out_time = 120;
225 if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
226 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
228 * P2P device scan should not be fragmented to avoid negative
229 * impact on P2P device discovery. Configure max_out_time to be
230 * equal to dwell time on passive channel. Take a longest
231 * possible value, one that corresponds to 2GHz band
233 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
235 iwl_mvm_get_passive_dwell(mvm,
236 IEEE80211_BAND_2GHZ);
237 params->max_out_time = passive_dwell;
239 params->passive_fragmented = true;
243 if ((params->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
244 (params->max_out_time > 200))
245 params->max_out_time = 200;
249 for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
250 if (params->passive_fragmented)
251 params->dwell[band].fragmented = frag_passive_dwell;
253 params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
255 params->dwell[band].active =
256 iwl_mvm_get_active_dwell(mvm, band, params->n_ssids);
260 "scan parameters: max_out_time %d, suspend_time %d, passive_fragmented %d\n",
261 params->max_out_time, params->suspend_time,
262 params->passive_fragmented);
264 "dwell[IEEE80211_BAND_2GHZ]: passive %d, active %d, fragmented %d\n",
265 params->dwell[IEEE80211_BAND_2GHZ].passive,
266 params->dwell[IEEE80211_BAND_2GHZ].active,
267 params->dwell[IEEE80211_BAND_2GHZ].fragmented);
269 "dwell[IEEE80211_BAND_5GHZ]: passive %d, active %d, fragmented %d\n",
270 params->dwell[IEEE80211_BAND_5GHZ].passive,
271 params->dwell[IEEE80211_BAND_5GHZ].active,
272 params->dwell[IEEE80211_BAND_5GHZ].fragmented);
275 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
277 /* require rrm scan whenever the fw supports it */
278 return mvm->fw->ucode_capa.capa[0] &
279 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
282 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
286 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
288 /* we create the 802.11 header and SSID element */
289 max_probe_len -= 24 + 2;
291 /* DS parameter set element is added on 2.4GHZ band if required */
292 if (iwl_mvm_rrm_scan_needed(mvm))
295 return max_probe_len;
298 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
300 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
302 /* TODO: [BUG] This function should return the maximum allowed size of
303 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
304 * in the same command. So the correct implementation of this function
305 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
306 * command has only 512 bytes and it would leave us with about 240
307 * bytes for scan IEs, which is clearly not enough. So meanwhile
308 * we will report an incorrect value. This may result in a failure to
309 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
310 * functions with -ENOBUFS, if a large enough probe will be provided.
315 static u8 *iwl_mvm_dump_channel_list(struct iwl_scan_results_notif *res,
316 int num_res, u8 *buf, size_t buf_size)
319 u8 *pos = buf, *end = buf + buf_size;
321 for (i = 0; pos < end && i < num_res; i++)
322 pos += snprintf(pos, end - pos, " %u", res[i].channel);
324 /* terminate the string in case the buffer was too short */
325 *(buf + buf_size - 1) = '\0';
330 int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
331 struct iwl_rx_cmd_buffer *rxb,
332 struct iwl_device_cmd *cmd)
334 struct iwl_rx_packet *pkt = rxb_addr(rxb);
335 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
339 "Scan offload iteration complete: status=0x%x scanned channels=%d channels list: %s\n",
340 notif->status, notif->scanned_channels,
341 iwl_mvm_dump_channel_list(notif->results,
342 notif->scanned_channels, buf,
347 int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
348 struct iwl_rx_cmd_buffer *rxb,
349 struct iwl_device_cmd *cmd)
351 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
352 ieee80211_sched_scan_results(mvm->hw);
357 int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
358 struct iwl_rx_cmd_buffer *rxb,
359 struct iwl_device_cmd *cmd)
361 struct iwl_rx_packet *pkt = rxb_addr(rxb);
362 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
363 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
364 bool ebs_successful = (scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS);
366 /* scan status must be locked for proper checking */
367 lockdep_assert_held(&mvm->mutex);
369 /* We first check if we were stopping a scan, in which case we
370 * just clear the stopping flag. Then we check if it was a
371 * firmware initiated stop, in which case we need to inform
373 * Note that we can have a stopping and a running scan
374 * simultaneously, but we can't have two different types of
375 * scans stopping or running at the same time (since LMAC
376 * doesn't support it).
379 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
380 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
382 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
383 aborted ? "aborted" : "completed",
384 ebs_successful ? "successful" : "failed");
386 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
387 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
388 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
389 aborted ? "aborted" : "completed",
390 ebs_successful ? "successful" : "failed");
392 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
393 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
394 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
396 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
397 aborted ? "aborted" : "completed",
398 ebs_successful ? "successful" : "failed");
400 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
401 ieee80211_sched_scan_stopped(mvm->hw);
402 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
403 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
404 aborted ? "aborted" : "completed",
405 ebs_successful ? "successful" : "failed");
407 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
408 ieee80211_scan_completed(mvm->hw,
409 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
410 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
413 mvm->last_ebs_successful = ebs_successful;
418 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
422 for (i = 0; i < PROBE_OPTION_MAX; i++) {
423 if (!ssid_list[i].len)
425 if (ssid_list[i].len == ssid_len &&
426 !memcmp(ssid_list->ssid, ssid, ssid_len))
432 /* We insert the SSIDs in an inverted order, because the FW will
435 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
436 struct iwl_ssid_ie *ssids,
443 * copy SSIDs from match list.
444 * iwl_config_sched_scan_profiles() uses the order of these ssids to
447 for (i = 0, j = params->n_match_sets - 1;
448 j >= 0 && i < PROBE_OPTION_MAX;
450 /* skip empty SSID matchsets */
451 if (!params->match_sets[j].ssid.ssid_len)
453 ssids[i].id = WLAN_EID_SSID;
454 ssids[i].len = params->match_sets[j].ssid.ssid_len;
455 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
459 /* add SSIDs from scan SSID list */
461 for (j = params->n_ssids - 1;
462 j >= 0 && i < PROBE_OPTION_MAX;
464 index = iwl_ssid_exist(params->ssids[j].ssid,
465 params->ssids[j].ssid_len,
468 ssids[i].id = WLAN_EID_SSID;
469 ssids[i].len = params->ssids[j].ssid_len;
470 memcpy(ssids[i].ssid, params->ssids[j].ssid,
472 *ssid_bitmap |= BIT(i);
474 *ssid_bitmap |= BIT(index);
479 int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
480 struct cfg80211_sched_scan_request *req)
482 struct iwl_scan_offload_profile *profile;
483 struct iwl_scan_offload_profile_cfg *profile_cfg;
484 struct iwl_scan_offload_blacklist *blacklist;
485 struct iwl_host_cmd cmd = {
486 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
487 .len[1] = sizeof(*profile_cfg),
488 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
489 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
495 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
498 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
499 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
501 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
503 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
507 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
513 cmd.data[0] = blacklist;
514 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
515 cmd.data[1] = profile_cfg;
517 /* No blacklist configuration */
519 profile_cfg->num_profiles = req->n_match_sets;
520 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
521 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
522 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
523 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
524 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
526 for (i = 0; i < req->n_match_sets; i++) {
527 profile = &profile_cfg->profiles[i];
528 profile->ssid_index = i;
529 /* Support any cipher and auth algorithm */
530 profile->unicast_cipher = 0xff;
531 profile->auth_alg = 0xff;
532 profile->network_type = IWL_NETWORK_TYPE_ANY;
533 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
534 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
537 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
539 ret = iwl_mvm_send_cmd(mvm, &cmd);
547 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
548 struct cfg80211_sched_scan_request *req)
550 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
552 "Sending scheduled scan with filtering, n_match_sets %d\n",
557 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
561 static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
564 struct iwl_host_cmd cmd = {
565 .id = SCAN_OFFLOAD_ABORT_CMD,
569 /* Exit instantly with error when device is not ready
570 * to receive scan abort command or it does not perform
571 * scheduled scan currently */
572 if (!mvm->scan_status)
575 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
579 if (status != CAN_ABORT_STATUS) {
581 * The scan abort will return 1 for success or
582 * 2 for "failure". A failure condition can be
583 * due to simply not being in an active scan which
584 * can occur if we send the scan abort before the
585 * microcode has notified us that a scan is completed.
587 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
594 int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
597 struct iwl_notification_wait wait_scan_done;
598 static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
599 bool sched = !!(mvm->scan_status & IWL_MVM_SCAN_SCHED);
601 lockdep_assert_held(&mvm->mutex);
603 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
604 return iwl_umac_scan_stop(mvm, IWL_MVM_SCAN_SCHED, notify);
606 /* FIXME: For now we only check if no scan is set here, since
607 * we only support LMAC in this flow and it doesn't support
610 if (!mvm->scan_status)
613 if (iwl_mvm_is_radio_killed(mvm)) {
618 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
620 ARRAY_SIZE(scan_done_notif),
623 ret = iwl_mvm_send_scan_offload_abort(mvm);
625 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
626 sched ? "offloaded " : "", ret);
627 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
631 IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
632 sched ? "scheduled " : "");
634 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
636 /* Clear the scan status so the next scan requests will
637 * succeed and mark the scan as stopping, so that the Rx
638 * handler doesn't do anything, as the scan was stopped from
639 * above. Since the rx handler won't do anything now, we have
640 * to release the scan reference here.
642 if (mvm->scan_status == IWL_MVM_SCAN_REGULAR)
643 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
646 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
647 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_SCHED;
649 ieee80211_sched_scan_stopped(mvm->hw);
651 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
652 mvm->scan_status |= IWL_MVM_SCAN_STOPPING_REGULAR;
654 ieee80211_scan_completed(mvm->hw, true);
660 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
661 struct iwl_scan_req_tx_cmd *tx_cmd,
664 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
666 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
669 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
671 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
673 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
676 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
680 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
681 struct ieee80211_channel **channels,
682 int n_channels, u32 ssid_bitmap,
683 struct iwl_scan_req_lmac *cmd)
685 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
688 for (i = 0; i < n_channels; i++) {
689 channel_cfg[i].channel_num =
690 cpu_to_le16(channels[i]->hw_value);
691 channel_cfg[i].iter_count = cpu_to_le16(1);
692 channel_cfg[i].iter_interval = 0;
693 channel_cfg[i].flags =
694 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
699 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
700 size_t len, u8 *const pos)
702 static const u8 before_ds_params[] = {
706 WLAN_EID_EXT_SUPP_RATES,
711 if (!iwl_mvm_rrm_scan_needed(mvm)) {
712 memcpy(newpos, ies, len);
716 offs = ieee80211_ie_split(ies, len,
718 ARRAY_SIZE(before_ds_params),
721 memcpy(newpos, ies, offs);
724 /* Add a placeholder for DS Parameter Set element */
725 *newpos++ = WLAN_EID_DS_PARAMS;
729 memcpy(newpos, ies + offs, len - offs);
730 newpos += len - offs;
736 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
737 struct ieee80211_scan_ies *ies,
738 struct iwl_mvm_scan_params *params)
740 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
742 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
743 params->mac_addr : NULL;
746 * Unfortunately, right now the offload scan doesn't support randomising
747 * within the firmware, so until the firmware API is ready we implement
748 * it in the driver. This means that the scan iterations won't really be
749 * random, only when it's restarted, but at least that helps a bit.
752 get_random_mask_addr(frame->sa, mac_addr,
753 params->mac_addr_mask);
755 memcpy(frame->sa, vif->addr, ETH_ALEN);
757 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
758 eth_broadcast_addr(frame->da);
759 eth_broadcast_addr(frame->bssid);
762 pos = frame->u.probe_req.variable;
763 *pos++ = WLAN_EID_SSID;
766 params->preq.mac_header.offset = 0;
767 params->preq.mac_header.len = cpu_to_le16(24 + 2);
769 /* Insert ds parameter set element on 2.4 GHz band */
770 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
771 ies->ies[IEEE80211_BAND_2GHZ],
772 ies->len[IEEE80211_BAND_2GHZ],
774 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
775 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
778 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
779 ies->len[IEEE80211_BAND_5GHZ]);
780 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
781 params->preq.band_data[1].len =
782 cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
783 pos += ies->len[IEEE80211_BAND_5GHZ];
785 memcpy(pos, ies->common_ies, ies->common_ie_len);
786 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
787 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
790 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
791 struct iwl_scan_req_lmac *cmd,
792 struct iwl_mvm_scan_params *params)
794 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
795 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
796 if (params->passive_fragmented)
797 cmd->fragmented_dwell =
798 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
799 cmd->max_out_time = cpu_to_le32(params->max_out_time);
800 cmd->suspend_time = cpu_to_le32(params->suspend_time);
801 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
804 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
805 struct ieee80211_scan_ies *ies,
808 return ((n_ssids <= PROBE_OPTION_MAX) &&
809 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
810 (ies->common_ie_len +
811 ies->len[NL80211_BAND_2GHZ] +
812 ies->len[NL80211_BAND_5GHZ] <=
813 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
816 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm, int n_iterations)
818 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
820 /* We can only use EBS if:
821 * 1. the feature is supported;
822 * 2. the last EBS was successful;
823 * 3. if only single scan, the single scan EBS API is supported.
825 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
826 mvm->last_ebs_successful &&
828 (capa->api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS)));
831 static int iwl_mvm_scan_total_iterations(struct iwl_mvm_scan_params *params)
833 return params->schedule[0].iterations + params->schedule[1].iterations;
836 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
837 struct iwl_mvm_scan_params *params)
841 if (params->n_ssids == 0)
842 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
844 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
845 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
847 if (params->passive_fragmented)
848 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
850 if (iwl_mvm_rrm_scan_needed(mvm))
851 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
853 if (params->pass_all)
854 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
856 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
858 #ifdef CONFIG_IWLWIFI_DEBUGFS
859 if (mvm->scan_iter_notif_enabled)
860 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
866 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
867 struct iwl_mvm_scan_params *params)
869 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
870 struct iwl_scan_probe_req *preq =
871 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
872 mvm->fw->ucode_capa.n_scan_channels);
874 int n_iterations = iwl_mvm_scan_total_iterations(params);
876 lockdep_assert_held(&mvm->mutex);
878 memset(cmd, 0, ksize(cmd));
880 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
882 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
883 cmd->iter_num = cpu_to_le32(1);
884 cmd->n_channels = (u8)params->n_channels;
886 cmd->delay = cpu_to_le32(params->delay);
888 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params));
890 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
891 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
892 MAC_FILTER_IN_BEACON);
893 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
894 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
896 /* this API uses bits 1-20 instead of 0-19 */
899 cmd->schedule[0].delay = cpu_to_le16(params->interval);
900 cmd->schedule[0].iterations = params->schedule[0].iterations;
901 cmd->schedule[0].full_scan_mul = params->schedule[0].full_scan_mul;
902 cmd->schedule[1].delay = cpu_to_le16(params->interval);
903 cmd->schedule[1].iterations = params->schedule[1].iterations;
904 cmd->schedule[1].full_scan_mul = params->schedule[1].iterations;
906 if (iwl_mvm_scan_use_ebs(mvm, n_iterations)) {
907 cmd->channel_opt[0].flags =
908 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
909 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
910 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
911 cmd->channel_opt[0].non_ebs_ratio =
912 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
913 cmd->channel_opt[1].flags =
914 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
915 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
916 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
917 cmd->channel_opt[1].non_ebs_ratio =
918 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
921 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
922 params->n_channels, ssid_bitmap, cmd);
924 *preq = params->preq;
929 int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
931 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
932 return iwl_umac_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
934 if (!(mvm->scan_status & IWL_MVM_SCAN_REGULAR))
937 if (iwl_mvm_is_radio_killed(mvm)) {
938 ieee80211_scan_completed(mvm->hw, true);
939 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
940 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
944 return iwl_mvm_scan_offload_stop(mvm, true);
949 struct iwl_umac_scan_done {
954 static int rate_to_scan_rate_flag(unsigned int rate)
956 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
957 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
958 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
959 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
960 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
961 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
962 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
963 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
964 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
965 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
966 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
967 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
968 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
971 return rate_to_scan_rate[rate];
974 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
976 struct ieee80211_supported_band *band;
977 unsigned int rates = 0;
980 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
981 for (i = 0; i < band->n_bitrates; i++)
982 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
983 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
984 for (i = 0; i < band->n_bitrates; i++)
985 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
987 /* Set both basic rates and supported rates */
988 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
990 return cpu_to_le32(rates);
993 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
996 struct iwl_scan_config *scan_config;
997 struct ieee80211_supported_band *band;
999 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1000 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1001 int ret, i, j = 0, cmd_size, data_size;
1002 struct iwl_host_cmd cmd = {
1006 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1009 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1011 scan_config = kzalloc(cmd_size, GFP_KERNEL);
1015 data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1016 scan_config->hdr.size = cpu_to_le16(data_size);
1017 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1018 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1019 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1020 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1021 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1022 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1023 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1024 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1025 SCAN_CONFIG_N_CHANNELS(num_channels));
1026 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1027 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1028 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1029 scan_config->out_of_channel_time = cpu_to_le32(170);
1030 scan_config->suspend_time = cpu_to_le32(30);
1031 scan_config->dwell_active = 20;
1032 scan_config->dwell_passive = 110;
1033 scan_config->dwell_fragmented = 20;
1035 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1037 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1038 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1039 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1040 IWL_CHANNEL_FLAG_EBS_ADD |
1041 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1043 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1044 for (i = 0; i < band->n_channels; i++, j++)
1045 scan_config->channel_array[j] = band->channels[i].hw_value;
1046 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1047 for (i = 0; i < band->n_channels; i++, j++)
1048 scan_config->channel_array[j] = band->channels[i].hw_value;
1050 cmd.data[0] = scan_config;
1051 cmd.len[0] = cmd_size;
1052 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1054 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1056 ret = iwl_mvm_send_cmd(mvm, &cmd);
1062 static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1066 for (i = 0; i < mvm->max_scans; i++)
1067 if (mvm->scan_uid[i] == uid)
1073 static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1075 return iwl_mvm_find_scan_uid(mvm, 0);
1078 static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm, int type)
1082 for (i = 0; i < mvm->max_scans; i++)
1083 if (mvm->scan_uid[i] & type)
1089 static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm, int type)
1093 for (i = 0; i < mvm->max_scans; i++)
1094 if (mvm->scan_uid[i] & type)
1100 static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm, int type)
1104 /* make sure exactly one bit is on in scan type */
1105 WARN_ON(hweight8(type) != 1);
1108 * Make sure scan uids are unique. If one scan lasts long time while
1109 * others are completing frequently, the seq number will wrap up and
1110 * we may have more than one scan with the same uid.
1113 uid = type | (mvm->scan_seq_num <<
1114 IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1115 mvm->scan_seq_num++;
1116 } while (iwl_mvm_find_scan_uid(mvm, uid) < mvm->max_scans);
1118 IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1123 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1124 struct iwl_scan_req_umac *cmd,
1125 struct iwl_mvm_scan_params *params)
1127 cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1128 cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1129 if (params->passive_fragmented)
1130 cmd->fragmented_dwell =
1131 params->dwell[IEEE80211_BAND_2GHZ].fragmented;
1132 cmd->max_out_time = cpu_to_le32(params->max_out_time);
1133 cmd->suspend_time = cpu_to_le32(params->suspend_time);
1134 cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1136 if (iwl_mvm_scan_total_iterations(params) == 0)
1137 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1139 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1143 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1144 struct ieee80211_channel **channels,
1145 int n_channels, u32 ssid_bitmap,
1146 struct iwl_scan_req_umac *cmd)
1148 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1151 for (i = 0; i < n_channels; i++) {
1152 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1153 channel_cfg[i].channel_num = channels[i]->hw_value;
1154 channel_cfg[i].iter_count = 1;
1155 channel_cfg[i].iter_interval = 0;
1159 static u32 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1160 struct iwl_mvm_scan_params *params)
1164 if (params->n_ssids == 0)
1165 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1167 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1168 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1170 if (params->passive_fragmented)
1171 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1173 if (iwl_mvm_rrm_scan_needed(mvm))
1174 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1176 if (params->pass_all)
1177 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1179 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1181 if (iwl_mvm_scan_total_iterations(params) > 1)
1182 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1187 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1188 struct iwl_mvm_scan_params *params)
1190 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1191 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1192 sizeof(struct iwl_scan_channel_cfg_umac) *
1193 mvm->fw->ucode_capa.n_scan_channels;
1195 u32 ssid_bitmap = 0;
1196 int n_iterations = iwl_mvm_scan_total_iterations(params);
1199 lockdep_assert_held(&mvm->mutex);
1201 uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1202 if (uid_idx >= mvm->max_scans)
1205 memset(cmd, 0, ksize(cmd));
1206 cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1207 sizeof(struct iwl_mvm_umac_cmd_hdr));
1209 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1211 if (n_iterations == 1)
1212 uid = iwl_generate_scan_uid(mvm, IWL_MVM_SCAN_REGULAR);
1214 uid = iwl_generate_scan_uid(mvm, IWL_MVM_SCAN_SCHED);
1216 mvm->scan_uid[uid_idx] = uid;
1217 cmd->uid = cpu_to_le32(uid);
1219 cmd->general_flags = cpu_to_le32(iwl_mvm_scan_umac_flags(mvm, params));
1221 if (iwl_mvm_scan_use_ebs(mvm, n_iterations))
1222 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1223 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1224 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1226 cmd->n_channels = params->n_channels;
1228 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1230 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1231 params->n_channels, ssid_bitmap, cmd);
1233 /* With UMAC we use only one schedule for now, so use the sum
1234 * of the iterations (with a a maximum of 255).
1236 sec_part->schedule[0].iter_count =
1237 (n_iterations > 255) ? 255 : n_iterations;
1238 sec_part->schedule[0].interval = cpu_to_le16(params->interval);
1240 sec_part->delay = cpu_to_le16(params->delay);
1241 sec_part->preq = params->preq;
1246 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1248 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1251 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1253 /* This looks a bit arbitrary, but the idea is that if we run
1254 * out of possible simultaneous scans and the userspace is
1255 * trying to run a scan type that is already running, we
1256 * return -EBUSY. But if the userspace wants to start a
1257 * different type of scan, we stop the opposite type to make
1258 * space for the new request. The reason is backwards
1259 * compatibility with old wpa_supplicant that wouldn't stop a
1260 * scheduled scan before starting a normal scan.
1263 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1266 /* Use a switch, even though this is a bitmask, so that more
1267 * than one bits set will fall in default and we will warn.
1270 case IWL_MVM_SCAN_REGULAR:
1271 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1273 return iwl_mvm_scan_offload_stop(mvm, true);
1274 case IWL_MVM_SCAN_SCHED:
1275 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1277 return iwl_mvm_cancel_scan(mvm);
1278 case IWL_MVM_SCAN_NETDETECT:
1279 /* No need to stop anything for net-detect since the
1280 * firmware is restarted anyway. This way, any sched
1281 * scans that were running will be restarted when we
1293 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1294 struct cfg80211_scan_request *req,
1295 struct ieee80211_scan_ies *ies)
1297 struct iwl_host_cmd hcmd = {
1298 .len = { iwl_mvm_scan_size(mvm), },
1299 .data = { mvm->scan_cmd, },
1300 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1302 struct iwl_mvm_scan_params params = {};
1305 lockdep_assert_held(&mvm->mutex);
1307 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1308 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1312 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1316 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1318 /* we should have failed registration if scan_cmd was NULL */
1319 if (WARN_ON(!mvm->scan_cmd))
1322 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1325 params.n_ssids = req->n_ssids;
1326 params.flags = req->flags;
1327 params.n_channels = req->n_channels;
1329 params.interval = 0;
1330 params.ssids = req->ssids;
1331 params.channels = req->channels;
1332 params.mac_addr = req->mac_addr;
1333 params.mac_addr_mask = req->mac_addr_mask;
1334 params.no_cck = req->no_cck;
1335 params.pass_all = true;
1336 params.n_match_sets = 0;
1337 params.match_sets = NULL;
1339 params.schedule[0].iterations = 1;
1340 params.schedule[0].full_scan_mul = 0;
1341 params.schedule[1].iterations = 0;
1342 params.schedule[1].full_scan_mul = 0;
1344 iwl_mvm_scan_calc_dwell(mvm, vif, ¶ms);
1346 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1348 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1349 hcmd.id = SCAN_REQ_UMAC;
1350 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms);
1352 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1353 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1359 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1361 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1362 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1364 /* If the scan failed, it usually means that the FW was unable
1365 * to allocate the time events. Warn on it, but maybe we
1366 * should try to send the command again with different params.
1368 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1372 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1377 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1378 struct ieee80211_vif *vif,
1379 struct cfg80211_sched_scan_request *req,
1380 struct ieee80211_scan_ies *ies,
1383 struct iwl_host_cmd hcmd = {
1384 .len = { iwl_mvm_scan_size(mvm), },
1385 .data = { mvm->scan_cmd, },
1386 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1388 struct iwl_mvm_scan_params params = {};
1391 lockdep_assert_held(&mvm->mutex);
1393 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1394 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1398 ret = iwl_mvm_check_running_scans(mvm, type);
1402 /* we should have failed registration if scan_cmd was NULL */
1403 if (WARN_ON(!mvm->scan_cmd))
1406 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1409 params.n_ssids = req->n_ssids;
1410 params.flags = req->flags;
1411 params.n_channels = req->n_channels;
1412 params.ssids = req->ssids;
1413 params.channels = req->channels;
1414 params.mac_addr = req->mac_addr;
1415 params.mac_addr_mask = req->mac_addr_mask;
1416 params.no_cck = false;
1417 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1418 params.n_match_sets = req->n_match_sets;
1419 params.match_sets = req->match_sets;
1421 params.schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1422 params.schedule[0].full_scan_mul = 1;
1423 params.schedule[1].iterations = 0xff;
1424 params.schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1426 if (req->interval > U16_MAX) {
1428 "interval value is > 16-bits, set to max possible\n");
1429 params.interval = U16_MAX;
1431 params.interval = req->interval / MSEC_PER_SEC;
1434 /* In theory, LMAC scans can handle a 32-bit delay, but since
1435 * waiting for over 18 hours to start the scan is a bit silly
1436 * and to keep it aligned with UMAC scans (which only support
1437 * 16-bit delays), trim it down to 16-bits.
1439 if (req->delay > U16_MAX) {
1441 "delay value is > 16-bits, set to max possible\n");
1442 params.delay = U16_MAX;
1444 params.delay = req->delay;
1447 iwl_mvm_scan_calc_dwell(mvm, vif, ¶ms);
1449 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1453 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1455 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1456 hcmd.id = SCAN_REQ_UMAC;
1457 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms);
1459 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1460 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1466 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1469 "Sched scan request was sent successfully\n");
1470 mvm->scan_status |= type;
1472 /* If the scan failed, it usually means that the FW was unable
1473 * to allocate the time events. Warn on it, but maybe we
1474 * should try to send the command again with different params.
1476 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1482 int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1483 struct iwl_rx_cmd_buffer *rxb,
1484 struct iwl_device_cmd *cmd)
1486 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1487 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1488 u32 uid = __le32_to_cpu(notif->uid);
1489 bool sched = !!(uid & IWL_MVM_SCAN_SCHED);
1490 int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1493 * Scan uid may be set to zero in case of scan abort request from above.
1495 if (uid_idx >= mvm->max_scans)
1499 "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1500 uid, sched ? "sched" : "regular",
1501 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1502 "completed" : "aborted",
1503 notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1504 "success" : "failed");
1506 if (notif->ebs_status)
1507 mvm->last_ebs_successful = false;
1509 mvm->scan_uid[uid_idx] = 0;
1512 ieee80211_scan_completed(mvm->hw,
1514 IWL_SCAN_OFFLOAD_ABORTED);
1515 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1516 } else if (!iwl_mvm_find_scan_type(mvm, IWL_MVM_SCAN_SCHED)) {
1517 ieee80211_sched_scan_stopped(mvm->hw);
1519 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1525 static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1526 struct iwl_rx_packet *pkt, void *data)
1528 struct iwl_umac_scan_done *scan_done = data;
1529 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1530 u32 uid = __le32_to_cpu(notif->uid);
1531 int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
1533 if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
1536 if (uid_idx >= scan_done->mvm->max_scans)
1540 * Clear scan uid of scans that was aborted from above and completed
1541 * in FW so the RX handler does nothing. Set last_ebs_successful here if
1544 scan_done->mvm->scan_uid[uid_idx] = 0;
1546 if (notif->ebs_status)
1547 scan_done->mvm->last_ebs_successful = false;
1549 return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
1552 static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
1554 struct iwl_umac_scan_abort cmd = {
1555 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
1556 sizeof(struct iwl_mvm_umac_cmd_hdr)),
1557 .uid = cpu_to_le32(uid),
1560 lockdep_assert_held(&mvm->mutex);
1562 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1564 return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
1567 static int iwl_umac_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
1569 struct iwl_notification_wait wait_scan_done;
1570 static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
1571 struct iwl_umac_scan_done scan_done = {
1577 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1579 ARRAY_SIZE(scan_done_notif),
1580 iwl_scan_umac_done_check, &scan_done);
1582 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1584 for (i = 0; i < mvm->max_scans; i++) {
1585 if (mvm->scan_uid[i] & type) {
1588 if (iwl_mvm_is_radio_killed(mvm) &&
1589 (type & IWL_MVM_SCAN_REGULAR)) {
1590 ieee80211_scan_completed(mvm->hw, true);
1591 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1595 err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
1602 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
1603 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1607 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1612 if (type & IWL_MVM_SCAN_SCHED)
1613 ieee80211_sched_scan_stopped(mvm->hw);
1614 if (type & IWL_MVM_SCAN_REGULAR) {
1615 ieee80211_scan_completed(mvm->hw, true);
1616 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1623 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1625 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1626 return sizeof(struct iwl_scan_req_umac) +
1627 sizeof(struct iwl_scan_channel_cfg_umac) *
1628 mvm->fw->ucode_capa.n_scan_channels +
1629 sizeof(struct iwl_scan_req_umac_tail);
1631 return sizeof(struct iwl_scan_req_lmac) +
1632 sizeof(struct iwl_scan_channel_cfg_lmac) *
1633 mvm->fw->ucode_capa.n_scan_channels +
1634 sizeof(struct iwl_scan_probe_req);
1638 * This function is used in nic restart flow, to inform mac80211 about scans
1639 * that was aborted by restart flow or by an assert.
1641 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1643 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1646 uid = iwl_mvm_find_first_scan(mvm, IWL_MVM_SCAN_REGULAR);
1647 if (uid < mvm->max_scans) {
1648 ieee80211_scan_completed(mvm->hw, true);
1649 mvm->scan_uid[uid] = 0;
1651 uid = iwl_mvm_find_first_scan(mvm, IWL_MVM_SCAN_SCHED);
1652 if (uid < mvm->max_scans && !mvm->restart_fw) {
1653 ieee80211_sched_scan_stopped(mvm->hw);
1654 mvm->scan_uid[uid] = 0;
1657 /* We shouldn't have any UIDs still set. Loop over all the
1658 * UIDs to make sure there's nothing left there and warn if
1661 for (i = 0; i < mvm->max_scans; i++) {
1662 if (WARN_ONCE(mvm->scan_uid[i],
1663 "UMAC scan UID %d was not cleaned\n",
1665 mvm->scan_uid[i] = 0;
1668 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
1669 ieee80211_scan_completed(mvm->hw, true);
1671 /* Sched scan will be restarted by mac80211 in
1672 * restart_hw, so do not report if FW is about to be
1675 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1676 ieee80211_sched_scan_stopped(mvm->hw);