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 enum iwl_mvm_scan_type {
76 IWL_SCAN_TYPE_UNASSOC,
79 IWL_SCAN_TYPE_FRAGMENTED,
82 enum iwl_mvm_traffic_load {
84 IWL_MVM_TRAFFIC_MEDIUM,
88 struct iwl_mvm_scan_timing_params {
96 static struct iwl_mvm_scan_timing_params scan_timing[] = {
97 [IWL_SCAN_TYPE_UNASSOC] = {
100 .dwell_fragmented = 44,
104 [IWL_SCAN_TYPE_WILD] = {
106 .dwell_passive = 110,
107 .dwell_fragmented = 44,
111 [IWL_SCAN_TYPE_MILD] = {
113 .dwell_passive = 110,
114 .dwell_fragmented = 44,
118 [IWL_SCAN_TYPE_FRAGMENTED] = {
120 .dwell_passive = 110,
121 .dwell_fragmented = 44,
127 struct iwl_mvm_scan_params {
128 enum iwl_mvm_scan_type type;
132 struct cfg80211_ssid *ssids;
133 struct ieee80211_channel **channels;
134 u16 interval; /* interval between scans (in secs) */
141 struct iwl_scan_probe_req preq;
142 struct cfg80211_match_set *match_sets;
146 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
148 if (mvm->scan_rx_ant != ANT_NONE)
149 return mvm->scan_rx_ant;
150 return iwl_mvm_get_valid_rx_ant(mvm);
153 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
158 rx_ant = iwl_mvm_scan_rx_ant(mvm);
159 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
160 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
161 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
162 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
163 return cpu_to_le16(rx_chain);
166 static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
168 if (band == IEEE80211_BAND_2GHZ)
169 return cpu_to_le32(PHY_BAND_24);
171 return cpu_to_le32(PHY_BAND_5);
175 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
180 mvm->scan_last_antenna_idx =
181 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
182 mvm->scan_last_antenna_idx);
183 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
185 if (band == IEEE80211_BAND_2GHZ && !no_cck)
186 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
189 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
192 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
193 struct ieee80211_vif *vif)
195 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
196 int *global_cnt = data;
198 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
199 mvmvif->phy_ctxt->id < MAX_PHYS)
203 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
205 return IWL_MVM_TRAFFIC_LOW;
209 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
210 struct ieee80211_vif *vif,
211 struct iwl_mvm_scan_params *params)
214 enum iwl_mvm_traffic_load load;
217 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
218 IEEE80211_IFACE_ITER_NORMAL,
219 iwl_mvm_scan_condition_iterator,
222 return IWL_SCAN_TYPE_UNASSOC;
224 load = iwl_mvm_get_traffic_load(mvm);
225 low_latency = iwl_mvm_low_latency(mvm);
227 if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
228 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
229 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_FRAGMENTED_SCAN))
230 return IWL_SCAN_TYPE_FRAGMENTED;
232 if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
233 return IWL_SCAN_TYPE_MILD;
235 return IWL_SCAN_TYPE_WILD;
238 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
240 /* require rrm scan whenever the fw supports it */
241 return fw_has_capa(&mvm->fw->ucode_capa,
242 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
245 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
249 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
251 /* we create the 802.11 header and SSID element */
252 max_probe_len -= 24 + 2;
254 /* DS parameter set element is added on 2.4GHZ band if required */
255 if (iwl_mvm_rrm_scan_needed(mvm))
258 return max_probe_len;
261 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
263 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
265 /* TODO: [BUG] This function should return the maximum allowed size of
266 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
267 * in the same command. So the correct implementation of this function
268 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
269 * command has only 512 bytes and it would leave us with about 240
270 * bytes for scan IEs, which is clearly not enough. So meanwhile
271 * we will report an incorrect value. This may result in a failure to
272 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
273 * functions with -ENOBUFS, if a large enough probe will be provided.
278 static u8 *iwl_mvm_dump_channel_list(struct iwl_scan_results_notif *res,
279 int num_res, u8 *buf, size_t buf_size)
282 u8 *pos = buf, *end = buf + buf_size;
284 for (i = 0; pos < end && i < num_res; i++)
285 pos += snprintf(pos, end - pos, " %u", res[i].channel);
287 /* terminate the string in case the buffer was too short */
288 *(buf + buf_size - 1) = '\0';
293 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
294 struct iwl_rx_cmd_buffer *rxb)
296 struct iwl_rx_packet *pkt = rxb_addr(rxb);
297 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
301 "Scan offload iteration complete: status=0x%x scanned channels=%d channels list: %s\n",
302 notif->status, notif->scanned_channels,
303 iwl_mvm_dump_channel_list(notif->results,
304 notif->scanned_channels, buf,
308 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
309 struct iwl_rx_cmd_buffer *rxb)
311 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
312 ieee80211_sched_scan_results(mvm->hw);
315 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
318 case IWL_SCAN_EBS_SUCCESS:
320 case IWL_SCAN_EBS_INACTIVE:
322 case IWL_SCAN_EBS_FAILED:
323 case IWL_SCAN_EBS_CHAN_NOT_FOUND:
329 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
330 struct iwl_rx_cmd_buffer *rxb)
332 struct iwl_rx_packet *pkt = rxb_addr(rxb);
333 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
334 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
336 /* scan status must be locked for proper checking */
337 lockdep_assert_held(&mvm->mutex);
339 /* We first check if we were stopping a scan, in which case we
340 * just clear the stopping flag. Then we check if it was a
341 * firmware initiated stop, in which case we need to inform
343 * Note that we can have a stopping and a running scan
344 * simultaneously, but we can't have two different types of
345 * scans stopping or running at the same time (since LMAC
346 * doesn't support it).
349 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
350 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
352 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
353 aborted ? "aborted" : "completed",
354 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
356 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
357 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
358 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
359 aborted ? "aborted" : "completed",
360 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
362 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
363 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
364 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
366 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s (FW)\n",
367 aborted ? "aborted" : "completed",
368 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
370 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
371 ieee80211_sched_scan_stopped(mvm->hw);
372 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
373 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
374 aborted ? "aborted" : "completed",
375 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
377 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
378 ieee80211_scan_completed(mvm->hw,
379 scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
380 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
383 mvm->last_ebs_successful =
384 scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
385 scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
388 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
392 for (i = 0; i < PROBE_OPTION_MAX; i++) {
393 if (!ssid_list[i].len)
395 if (ssid_list[i].len == ssid_len &&
396 !memcmp(ssid_list->ssid, ssid, ssid_len))
402 /* We insert the SSIDs in an inverted order, because the FW will
405 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
406 struct iwl_ssid_ie *ssids,
413 * copy SSIDs from match list.
414 * iwl_config_sched_scan_profiles() uses the order of these ssids to
417 for (i = 0, j = params->n_match_sets - 1;
418 j >= 0 && i < PROBE_OPTION_MAX;
420 /* skip empty SSID matchsets */
421 if (!params->match_sets[j].ssid.ssid_len)
423 ssids[i].id = WLAN_EID_SSID;
424 ssids[i].len = params->match_sets[j].ssid.ssid_len;
425 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
429 /* add SSIDs from scan SSID list */
431 for (j = params->n_ssids - 1;
432 j >= 0 && i < PROBE_OPTION_MAX;
434 index = iwl_ssid_exist(params->ssids[j].ssid,
435 params->ssids[j].ssid_len,
438 ssids[i].id = WLAN_EID_SSID;
439 ssids[i].len = params->ssids[j].ssid_len;
440 memcpy(ssids[i].ssid, params->ssids[j].ssid,
442 *ssid_bitmap |= BIT(i);
444 *ssid_bitmap |= BIT(index);
450 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
451 struct cfg80211_sched_scan_request *req)
453 struct iwl_scan_offload_profile *profile;
454 struct iwl_scan_offload_profile_cfg *profile_cfg;
455 struct iwl_scan_offload_blacklist *blacklist;
456 struct iwl_host_cmd cmd = {
457 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
458 .len[1] = sizeof(*profile_cfg),
459 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
460 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
466 if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
469 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
470 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
472 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
474 blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
478 profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
484 cmd.data[0] = blacklist;
485 cmd.len[0] = sizeof(*blacklist) * blacklist_len;
486 cmd.data[1] = profile_cfg;
488 /* No blacklist configuration */
490 profile_cfg->num_profiles = req->n_match_sets;
491 profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
492 profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
493 profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
494 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
495 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
497 for (i = 0; i < req->n_match_sets; i++) {
498 profile = &profile_cfg->profiles[i];
499 profile->ssid_index = i;
500 /* Support any cipher and auth algorithm */
501 profile->unicast_cipher = 0xff;
502 profile->auth_alg = 0xff;
503 profile->network_type = IWL_NETWORK_TYPE_ANY;
504 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
505 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
508 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
510 ret = iwl_mvm_send_cmd(mvm, &cmd);
518 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
519 struct cfg80211_sched_scan_request *req)
521 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
523 "Sending scheduled scan with filtering, n_match_sets %d\n",
528 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
532 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
535 struct iwl_host_cmd cmd = {
536 .id = SCAN_OFFLOAD_ABORT_CMD,
540 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
544 if (status != CAN_ABORT_STATUS) {
546 * The scan abort will return 1 for success or
547 * 2 for "failure". A failure condition can be
548 * due to simply not being in an active scan which
549 * can occur if we send the scan abort before the
550 * microcode has notified us that a scan is completed.
552 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
559 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
560 struct iwl_scan_req_tx_cmd *tx_cmd,
563 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
565 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
568 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
570 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
572 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
575 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
579 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
580 struct ieee80211_channel **channels,
581 int n_channels, u32 ssid_bitmap,
582 struct iwl_scan_req_lmac *cmd)
584 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
587 for (i = 0; i < n_channels; i++) {
588 channel_cfg[i].channel_num =
589 cpu_to_le16(channels[i]->hw_value);
590 channel_cfg[i].iter_count = cpu_to_le16(1);
591 channel_cfg[i].iter_interval = 0;
592 channel_cfg[i].flags =
593 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
598 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
599 size_t len, u8 *const pos)
601 static const u8 before_ds_params[] = {
605 WLAN_EID_EXT_SUPP_RATES,
610 if (!iwl_mvm_rrm_scan_needed(mvm)) {
611 memcpy(newpos, ies, len);
615 offs = ieee80211_ie_split(ies, len,
617 ARRAY_SIZE(before_ds_params),
620 memcpy(newpos, ies, offs);
623 /* Add a placeholder for DS Parameter Set element */
624 *newpos++ = WLAN_EID_DS_PARAMS;
628 memcpy(newpos, ies + offs, len - offs);
629 newpos += len - offs;
635 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
636 struct ieee80211_scan_ies *ies,
637 struct iwl_mvm_scan_params *params)
639 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
641 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
642 params->mac_addr : NULL;
645 * Unfortunately, right now the offload scan doesn't support randomising
646 * within the firmware, so until the firmware API is ready we implement
647 * it in the driver. This means that the scan iterations won't really be
648 * random, only when it's restarted, but at least that helps a bit.
651 get_random_mask_addr(frame->sa, mac_addr,
652 params->mac_addr_mask);
654 memcpy(frame->sa, vif->addr, ETH_ALEN);
656 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
657 eth_broadcast_addr(frame->da);
658 eth_broadcast_addr(frame->bssid);
661 pos = frame->u.probe_req.variable;
662 *pos++ = WLAN_EID_SSID;
665 params->preq.mac_header.offset = 0;
666 params->preq.mac_header.len = cpu_to_le16(24 + 2);
668 /* Insert ds parameter set element on 2.4 GHz band */
669 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
670 ies->ies[IEEE80211_BAND_2GHZ],
671 ies->len[IEEE80211_BAND_2GHZ],
673 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
674 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
677 memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
678 ies->len[IEEE80211_BAND_5GHZ]);
679 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
680 params->preq.band_data[1].len =
681 cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
682 pos += ies->len[IEEE80211_BAND_5GHZ];
684 memcpy(pos, ies->common_ies, ies->common_ie_len);
685 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
686 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
689 static __le32 iwl_mvm_scan_priority(struct iwl_mvm *mvm,
690 enum iwl_scan_priority_ext prio)
692 if (fw_has_api(&mvm->fw->ucode_capa,
693 IWL_UCODE_TLV_API_EXT_SCAN_PRIORITY))
694 return cpu_to_le32(prio);
696 if (prio <= IWL_SCAN_PRIORITY_EXT_2)
697 return cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
699 if (prio <= IWL_SCAN_PRIORITY_EXT_4)
700 return cpu_to_le32(IWL_SCAN_PRIORITY_MEDIUM);
702 return cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
705 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
706 struct iwl_scan_req_lmac *cmd,
707 struct iwl_mvm_scan_params *params)
709 cmd->active_dwell = scan_timing[params->type].dwell_active;
710 cmd->passive_dwell = scan_timing[params->type].dwell_passive;
711 cmd->fragmented_dwell = scan_timing[params->type].dwell_fragmented;
712 cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
713 cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
714 cmd->scan_prio = iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
717 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
718 struct ieee80211_scan_ies *ies,
721 return ((n_ssids <= PROBE_OPTION_MAX) &&
722 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
723 (ies->common_ie_len +
724 ies->len[NL80211_BAND_2GHZ] +
725 ies->len[NL80211_BAND_5GHZ] <=
726 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
729 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
730 struct ieee80211_vif *vif,
733 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
735 /* We can only use EBS if:
736 * 1. the feature is supported;
737 * 2. the last EBS was successful;
738 * 3. if only single scan, the single scan EBS API is supported;
739 * 4. it's not a p2p find operation.
741 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
742 mvm->last_ebs_successful &&
744 fw_has_api(capa, IWL_UCODE_TLV_API_SINGLE_SCAN_EBS)) &&
745 vif->type != NL80211_IFTYPE_P2P_DEVICE);
748 static int iwl_mvm_scan_total_iterations(struct iwl_mvm_scan_params *params)
750 return params->iterations[0] + params->iterations[1];
753 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
754 struct iwl_mvm_scan_params *params)
758 if (params->n_ssids == 0)
759 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
761 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
762 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
764 if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
765 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
767 if (iwl_mvm_rrm_scan_needed(mvm))
768 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
770 if (params->pass_all)
771 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
773 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
775 #ifdef CONFIG_IWLWIFI_DEBUGFS
776 if (mvm->scan_iter_notif_enabled)
777 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
783 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
784 struct iwl_mvm_scan_params *params)
786 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
787 struct iwl_scan_probe_req *preq =
788 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
789 mvm->fw->ucode_capa.n_scan_channels);
791 int n_iterations = iwl_mvm_scan_total_iterations(params);
793 lockdep_assert_held(&mvm->mutex);
795 memset(cmd, 0, ksize(cmd));
797 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
799 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
800 cmd->iter_num = cpu_to_le32(1);
801 cmd->n_channels = (u8)params->n_channels;
803 cmd->delay = cpu_to_le32(params->delay);
805 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params));
807 cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
808 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
809 MAC_FILTER_IN_BEACON);
810 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
811 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
813 /* this API uses bits 1-20 instead of 0-19 */
816 cmd->schedule[0].delay = cpu_to_le16(params->interval);
817 cmd->schedule[0].iterations = params->iterations[0];
818 cmd->schedule[0].full_scan_mul = 1;
819 cmd->schedule[1].delay = cpu_to_le16(params->interval);
820 cmd->schedule[1].iterations = params->iterations[1];
821 cmd->schedule[1].full_scan_mul = 1;
823 if (iwl_mvm_scan_use_ebs(mvm, vif, n_iterations)) {
824 cmd->channel_opt[0].flags =
825 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
826 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
827 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
828 cmd->channel_opt[0].non_ebs_ratio =
829 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
830 cmd->channel_opt[1].flags =
831 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
832 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
833 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
834 cmd->channel_opt[1].non_ebs_ratio =
835 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
838 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
839 params->n_channels, ssid_bitmap, cmd);
841 *preq = params->preq;
846 static int rate_to_scan_rate_flag(unsigned int rate)
848 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
849 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
850 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
851 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
852 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
853 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
854 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
855 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
856 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
857 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
858 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
859 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
860 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
863 return rate_to_scan_rate[rate];
866 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
868 struct ieee80211_supported_band *band;
869 unsigned int rates = 0;
872 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
873 for (i = 0; i < band->n_bitrates; i++)
874 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
875 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
876 for (i = 0; i < band->n_bitrates; i++)
877 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
879 /* Set both basic rates and supported rates */
880 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
882 return cpu_to_le32(rates);
885 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
888 struct iwl_scan_config *scan_config;
889 struct ieee80211_supported_band *band;
891 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
892 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
893 int ret, i, j = 0, cmd_size;
894 struct iwl_host_cmd cmd = {
895 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
898 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
901 cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
903 scan_config = kzalloc(cmd_size, GFP_KERNEL);
907 scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
908 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
909 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
910 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
911 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
912 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
913 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
914 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
915 SCAN_CONFIG_N_CHANNELS(num_channels));
916 scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
917 scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
918 scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
919 scan_config->out_of_channel_time = cpu_to_le32(170);
920 scan_config->suspend_time = cpu_to_le32(30);
921 scan_config->dwell_active = 20;
922 scan_config->dwell_passive = 110;
923 scan_config->dwell_fragmented = 20;
925 memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
927 scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
928 scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
929 IWL_CHANNEL_FLAG_ACCURATE_EBS |
930 IWL_CHANNEL_FLAG_EBS_ADD |
931 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
933 band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
934 for (i = 0; i < band->n_channels; i++, j++)
935 scan_config->channel_array[j] = band->channels[i].hw_value;
936 band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
937 for (i = 0; i < band->n_channels; i++, j++)
938 scan_config->channel_array[j] = band->channels[i].hw_value;
940 cmd.data[0] = scan_config;
941 cmd.len[0] = cmd_size;
942 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
944 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
946 ret = iwl_mvm_send_cmd(mvm, &cmd);
952 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
956 for (i = 0; i < mvm->max_scans; i++)
957 if (mvm->scan_uid_status[i] == status)
963 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
964 struct iwl_scan_req_umac *cmd,
965 struct iwl_mvm_scan_params *params)
967 cmd->active_dwell = scan_timing[params->type].dwell_active;
968 cmd->passive_dwell = scan_timing[params->type].dwell_passive;
969 cmd->fragmented_dwell = scan_timing[params->type].dwell_fragmented;
970 cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
971 cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
973 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
975 if (iwl_mvm_scan_total_iterations(params) == 1)
977 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_6);
980 iwl_mvm_scan_priority(mvm, IWL_SCAN_PRIORITY_EXT_2);
984 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
985 struct ieee80211_channel **channels,
986 int n_channels, u32 ssid_bitmap,
987 struct iwl_scan_req_umac *cmd)
989 struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
992 for (i = 0; i < n_channels; i++) {
993 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
994 channel_cfg[i].channel_num = channels[i]->hw_value;
995 channel_cfg[i].iter_count = 1;
996 channel_cfg[i].iter_interval = 0;
1000 static u32 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1001 struct iwl_mvm_scan_params *params)
1005 if (params->n_ssids == 0)
1006 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1008 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1009 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1011 if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
1012 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1014 if (iwl_mvm_rrm_scan_needed(mvm))
1015 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1017 if (params->pass_all)
1018 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1020 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1022 if (iwl_mvm_scan_total_iterations(params) > 1)
1023 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1025 #ifdef CONFIG_IWLWIFI_DEBUGFS
1026 if (mvm->scan_iter_notif_enabled)
1027 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1032 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1033 struct iwl_mvm_scan_params *params,
1036 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1037 struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1038 sizeof(struct iwl_scan_channel_cfg_umac) *
1039 mvm->fw->ucode_capa.n_scan_channels;
1041 u32 ssid_bitmap = 0;
1042 int n_iterations = iwl_mvm_scan_total_iterations(params);
1044 lockdep_assert_held(&mvm->mutex);
1046 uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1050 memset(cmd, 0, ksize(cmd));
1052 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1054 mvm->scan_uid_status[uid] = type;
1056 cmd->uid = cpu_to_le32(uid);
1057 cmd->general_flags = cpu_to_le32(iwl_mvm_scan_umac_flags(mvm, params));
1059 if (type == IWL_MVM_SCAN_SCHED)
1060 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1062 if (iwl_mvm_scan_use_ebs(mvm, vif, n_iterations))
1063 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1064 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1065 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1067 cmd->n_channels = params->n_channels;
1069 iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1071 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1072 params->n_channels, ssid_bitmap, cmd);
1074 /* With UMAC we use only one schedule for now, so use the sum
1075 * of the iterations (with a a maximum of 255).
1077 sec_part->schedule[0].iter_count =
1078 (n_iterations > 255) ? 255 : n_iterations;
1079 sec_part->schedule[0].interval = cpu_to_le16(params->interval);
1081 sec_part->delay = cpu_to_le16(params->delay);
1082 sec_part->preq = params->preq;
1087 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1089 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1092 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1094 /* This looks a bit arbitrary, but the idea is that if we run
1095 * out of possible simultaneous scans and the userspace is
1096 * trying to run a scan type that is already running, we
1097 * return -EBUSY. But if the userspace wants to start a
1098 * different type of scan, we stop the opposite type to make
1099 * space for the new request. The reason is backwards
1100 * compatibility with old wpa_supplicant that wouldn't stop a
1101 * scheduled scan before starting a normal scan.
1104 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1107 /* Use a switch, even though this is a bitmask, so that more
1108 * than one bits set will fall in default and we will warn.
1111 case IWL_MVM_SCAN_REGULAR:
1112 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1114 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1115 case IWL_MVM_SCAN_SCHED:
1116 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1118 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1119 case IWL_MVM_SCAN_NETDETECT:
1120 /* No need to stop anything for net-detect since the
1121 * firmware is restarted anyway. This way, any sched
1122 * scans that were running will be restarted when we
1134 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1135 struct cfg80211_scan_request *req,
1136 struct ieee80211_scan_ies *ies)
1138 struct iwl_host_cmd hcmd = {
1139 .len = { iwl_mvm_scan_size(mvm), },
1140 .data = { mvm->scan_cmd, },
1141 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1143 struct iwl_mvm_scan_params params = {};
1146 lockdep_assert_held(&mvm->mutex);
1148 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1149 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1153 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1157 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1159 /* we should have failed registration if scan_cmd was NULL */
1160 if (WARN_ON(!mvm->scan_cmd))
1163 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1166 params.n_ssids = req->n_ssids;
1167 params.flags = req->flags;
1168 params.n_channels = req->n_channels;
1170 params.interval = 0;
1171 params.ssids = req->ssids;
1172 params.channels = req->channels;
1173 params.mac_addr = req->mac_addr;
1174 params.mac_addr_mask = req->mac_addr_mask;
1175 params.no_cck = req->no_cck;
1176 params.pass_all = true;
1177 params.n_match_sets = 0;
1178 params.match_sets = NULL;
1180 params.iterations[0] = 1;
1181 params.iterations[1] = 0;
1183 params.type = iwl_mvm_get_scan_type(mvm, vif, ¶ms);
1185 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1187 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1188 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1189 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms,
1190 IWL_MVM_SCAN_REGULAR);
1192 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1193 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1199 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1201 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1202 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1204 /* If the scan failed, it usually means that the FW was unable
1205 * to allocate the time events. Warn on it, but maybe we
1206 * should try to send the command again with different params.
1208 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1212 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1217 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1218 struct ieee80211_vif *vif,
1219 struct cfg80211_sched_scan_request *req,
1220 struct ieee80211_scan_ies *ies,
1223 struct iwl_host_cmd hcmd = {
1224 .len = { iwl_mvm_scan_size(mvm), },
1225 .data = { mvm->scan_cmd, },
1226 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1228 struct iwl_mvm_scan_params params = {};
1231 lockdep_assert_held(&mvm->mutex);
1233 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1234 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1238 ret = iwl_mvm_check_running_scans(mvm, type);
1242 /* we should have failed registration if scan_cmd was NULL */
1243 if (WARN_ON(!mvm->scan_cmd))
1246 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1249 params.n_ssids = req->n_ssids;
1250 params.flags = req->flags;
1251 params.n_channels = req->n_channels;
1252 params.ssids = req->ssids;
1253 params.channels = req->channels;
1254 params.mac_addr = req->mac_addr;
1255 params.mac_addr_mask = req->mac_addr_mask;
1256 params.no_cck = false;
1257 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
1258 params.n_match_sets = req->n_match_sets;
1259 params.match_sets = req->match_sets;
1261 params.iterations[0] = 0;
1262 params.iterations[1] = 0xff;
1264 params.type = iwl_mvm_get_scan_type(mvm, vif, ¶ms);
1266 if (req->interval > U16_MAX) {
1268 "interval value is > 16-bits, set to max possible\n");
1269 params.interval = U16_MAX;
1271 params.interval = req->interval / MSEC_PER_SEC;
1274 /* In theory, LMAC scans can handle a 32-bit delay, but since
1275 * waiting for over 18 hours to start the scan is a bit silly
1276 * and to keep it aligned with UMAC scans (which only support
1277 * 16-bit delays), trim it down to 16-bits.
1279 if (req->delay > U16_MAX) {
1281 "delay value is > 16-bits, set to max possible\n");
1282 params.delay = U16_MAX;
1284 params.delay = req->delay;
1287 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1291 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
1293 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1294 hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1295 ret = iwl_mvm_scan_umac(mvm, vif, ¶ms, IWL_MVM_SCAN_SCHED);
1297 hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1298 ret = iwl_mvm_scan_lmac(mvm, vif, ¶ms);
1304 ret = iwl_mvm_send_cmd(mvm, &hcmd);
1307 "Sched scan request was sent successfully\n");
1308 mvm->scan_status |= type;
1310 /* If the scan failed, it usually means that the FW was unable
1311 * to allocate the time events. Warn on it, but maybe we
1312 * should try to send the command again with different params.
1314 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1320 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1321 struct iwl_rx_cmd_buffer *rxb)
1323 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1324 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1325 u32 uid = __le32_to_cpu(notif->uid);
1326 bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1328 if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1331 /* if the scan is already stopping, we don't need to notify mac80211 */
1332 if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1333 ieee80211_scan_completed(mvm->hw, aborted);
1334 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1335 } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1336 ieee80211_sched_scan_stopped(mvm->hw);
1339 mvm->scan_status &= ~mvm->scan_uid_status[uid];
1342 "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1343 uid, mvm->scan_uid_status[uid],
1344 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1345 "completed" : "aborted",
1346 iwl_mvm_ebs_status_str(notif->ebs_status));
1348 if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1349 notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1350 mvm->last_ebs_successful = false;
1352 mvm->scan_uid_status[uid] = 0;
1355 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1356 struct iwl_rx_cmd_buffer *rxb)
1358 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1359 struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1363 "UMAC Scan iteration complete: status=0x%x scanned_channels=%d channels list: %s\n",
1364 notif->status, notif->scanned_channels,
1365 iwl_mvm_dump_channel_list(notif->results,
1366 notif->scanned_channels, buf,
1370 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1372 struct iwl_umac_scan_abort cmd = {};
1375 lockdep_assert_held(&mvm->mutex);
1377 /* We should always get a valid index here, because we already
1378 * checked that this type of scan was running in the generic
1381 uid = iwl_mvm_scan_uid_by_status(mvm, type);
1382 if (WARN_ON_ONCE(uid < 0))
1385 cmd.uid = cpu_to_le32(uid);
1387 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1389 ret = iwl_mvm_send_cmd_pdu(mvm,
1390 iwl_cmd_id(SCAN_ABORT_UMAC,
1391 IWL_ALWAYS_LONG_GROUP, 0),
1392 0, sizeof(cmd), &cmd);
1394 mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1399 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1401 struct iwl_notification_wait wait_scan_done;
1402 static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1403 SCAN_OFFLOAD_COMPLETE, };
1406 lockdep_assert_held(&mvm->mutex);
1408 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1410 ARRAY_SIZE(scan_done_notif),
1413 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1415 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1416 ret = iwl_mvm_umac_scan_abort(mvm, type);
1418 ret = iwl_mvm_lmac_scan_abort(mvm);
1421 IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1422 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1426 ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1431 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1433 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1434 return sizeof(struct iwl_scan_req_umac) +
1435 sizeof(struct iwl_scan_channel_cfg_umac) *
1436 mvm->fw->ucode_capa.n_scan_channels +
1437 sizeof(struct iwl_scan_req_umac_tail);
1439 return sizeof(struct iwl_scan_req_lmac) +
1440 sizeof(struct iwl_scan_channel_cfg_lmac) *
1441 mvm->fw->ucode_capa.n_scan_channels +
1442 sizeof(struct iwl_scan_probe_req);
1446 * This function is used in nic restart flow, to inform mac80211 about scans
1447 * that was aborted by restart flow or by an assert.
1449 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
1451 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1454 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
1456 ieee80211_scan_completed(mvm->hw, true);
1457 mvm->scan_uid_status[uid] = 0;
1459 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
1460 if (uid >= 0 && !mvm->restart_fw) {
1461 ieee80211_sched_scan_stopped(mvm->hw);
1462 mvm->scan_uid_status[uid] = 0;
1465 /* We shouldn't have any UIDs still set. Loop over all the
1466 * UIDs to make sure there's nothing left there and warn if
1469 for (i = 0; i < mvm->max_scans; i++) {
1470 if (WARN_ONCE(mvm->scan_uid_status[i],
1471 "UMAC scan UID %d status was not cleaned\n",
1473 mvm->scan_uid_status[i] = 0;
1476 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
1477 ieee80211_scan_completed(mvm->hw, true);
1479 /* Sched scan will be restarted by mac80211 in
1480 * restart_hw, so do not report if FW is about to be
1483 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) && !mvm->restart_fw)
1484 ieee80211_sched_scan_stopped(mvm->hw);
1488 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
1492 if (!(mvm->scan_status & type))
1495 if (iwl_mvm_is_radio_killed(mvm)) {
1500 ret = iwl_mvm_scan_stop_wait(mvm, type);
1502 mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
1504 /* Clear the scan status so the next scan requests will
1505 * succeed and mark the scan as stopping, so that the Rx
1506 * handler doesn't do anything, as the scan was stopped from
1509 mvm->scan_status &= ~type;
1511 if (type == IWL_MVM_SCAN_REGULAR) {
1512 /* Since the rx handler won't do anything now, we have
1513 * to release the scan reference here.
1515 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1517 ieee80211_scan_completed(mvm->hw, true);
1518 } else if (notify) {
1519 ieee80211_sched_scan_stopped(mvm->hw);