1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
7 #include <linux/etherdevice.h>
8 #include <net/mac80211.h>
9 #include <linux/crc32.h>
12 #include "fw/api/scan.h"
15 #define IWL_DENSE_EBS_SCAN_RATIO 5
16 #define IWL_SPARSE_EBS_SCAN_RATIO 1
18 #define IWL_SCAN_DWELL_ACTIVE 10
19 #define IWL_SCAN_DWELL_PASSIVE 110
20 #define IWL_SCAN_DWELL_FRAGMENTED 44
21 #define IWL_SCAN_DWELL_EXTENDED 90
22 #define IWL_SCAN_NUM_OF_FRAGS 3
23 #define IWL_SCAN_LAST_2_4_CHN 14
25 /* adaptive dwell max budget time [TU] for full scan */
26 #define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
27 /* adaptive dwell max budget time [TU] for directed scan */
28 #define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
29 /* adaptive dwell default high band APs number */
30 #define IWL_SCAN_ADWELL_DEFAULT_HB_N_APS 8
31 /* adaptive dwell default low band APs number */
32 #define IWL_SCAN_ADWELL_DEFAULT_LB_N_APS 2
33 /* adaptive dwell default APs number in social channels (1, 6, 11) */
34 #define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
35 /* number of scan channels */
36 #define IWL_SCAN_NUM_CHANNELS 112
37 /* adaptive dwell number of APs override mask for p2p friendly GO */
38 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT BIT(20)
39 /* adaptive dwell number of APs override mask for social channels */
40 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT BIT(21)
41 /* adaptive dwell number of APs override for p2p friendly GO channels */
42 #define IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY 10
43 /* adaptive dwell number of APs override for social channels */
44 #define IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS 2
46 struct iwl_mvm_scan_timing_params {
51 static struct iwl_mvm_scan_timing_params scan_timing[] = {
52 [IWL_SCAN_TYPE_UNASSOC] = {
56 [IWL_SCAN_TYPE_WILD] = {
60 [IWL_SCAN_TYPE_MILD] = {
64 [IWL_SCAN_TYPE_FRAGMENTED] = {
68 [IWL_SCAN_TYPE_FAST_BALANCE] = {
74 struct iwl_mvm_scan_params {
75 /* For CDB this is low band scan type, for non-CDB - type. */
76 enum iwl_mvm_scan_type type;
77 enum iwl_mvm_scan_type hb_type;
81 struct cfg80211_ssid *ssids;
82 struct ieee80211_channel **channels;
89 struct iwl_scan_probe_req preq;
90 struct cfg80211_match_set *match_sets;
92 struct cfg80211_sched_scan_plan *scan_plans;
94 struct cfg80211_scan_6ghz_params *scan_6ghz_params;
99 static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
101 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
103 if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
104 return (void *)&cmd->v8.data;
106 if (iwl_mvm_is_adaptive_dwell_supported(mvm))
107 return (void *)&cmd->v7.data;
109 if (iwl_mvm_cdb_scan_api(mvm))
110 return (void *)&cmd->v6.data;
112 return (void *)&cmd->v1.data;
115 static inline struct iwl_scan_umac_chan_param *
116 iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
118 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
120 if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
121 return &cmd->v8.channel;
123 if (iwl_mvm_is_adaptive_dwell_supported(mvm))
124 return &cmd->v7.channel;
126 if (iwl_mvm_cdb_scan_api(mvm))
127 return &cmd->v6.channel;
129 return &cmd->v1.channel;
132 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
134 if (mvm->scan_rx_ant != ANT_NONE)
135 return mvm->scan_rx_ant;
136 return iwl_mvm_get_valid_rx_ant(mvm);
139 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
144 rx_ant = iwl_mvm_scan_rx_ant(mvm);
145 rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
146 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
147 rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
148 rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
149 return cpu_to_le16(rx_chain);
153 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
158 iwl_mvm_toggle_tx_ant(mvm, &mvm->scan_last_antenna_idx);
159 tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
161 if (band == NL80211_BAND_2GHZ && !no_cck)
162 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
165 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
168 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
169 struct ieee80211_vif *vif)
171 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
172 int *global_cnt = data;
174 if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
175 mvmvif->phy_ctxt->id < NUM_PHY_CTX)
179 static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
181 return mvm->tcm.result.global_load;
184 static enum iwl_mvm_traffic_load
185 iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
187 return mvm->tcm.result.band_load[band];
190 struct iwl_is_dcm_with_go_iterator_data {
191 struct ieee80211_vif *current_vif;
192 bool is_dcm_with_p2p_go;
195 static void iwl_mvm_is_dcm_with_go_iterator(void *_data, u8 *mac,
196 struct ieee80211_vif *vif)
198 struct iwl_is_dcm_with_go_iterator_data *data = _data;
199 struct iwl_mvm_vif *other_mvmvif = iwl_mvm_vif_from_mac80211(vif);
200 struct iwl_mvm_vif *curr_mvmvif =
201 iwl_mvm_vif_from_mac80211(data->current_vif);
203 /* exclude the given vif */
204 if (vif == data->current_vif)
207 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
208 other_mvmvif->phy_ctxt && curr_mvmvif->phy_ctxt &&
209 other_mvmvif->phy_ctxt->id != curr_mvmvif->phy_ctxt->id)
210 data->is_dcm_with_p2p_go = true;
214 iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
215 struct ieee80211_vif *vif,
216 enum iwl_mvm_traffic_load load,
221 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
222 IEEE80211_IFACE_ITER_NORMAL,
223 iwl_mvm_scan_condition_iterator,
226 return IWL_SCAN_TYPE_UNASSOC;
228 if (fw_has_api(&mvm->fw->ucode_capa,
229 IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
230 if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
231 (!vif || vif->type != NL80211_IFTYPE_P2P_DEVICE))
232 return IWL_SCAN_TYPE_FRAGMENTED;
234 /* in case of DCM with GO where BSS DTIM interval < 220msec
235 * set all scan requests as fast-balance scan
237 if (vif && vif->type == NL80211_IFTYPE_STATION &&
238 vif->bss_conf.dtim_period < 220) {
239 struct iwl_is_dcm_with_go_iterator_data data = {
241 .is_dcm_with_p2p_go = false,
244 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
245 IEEE80211_IFACE_ITER_NORMAL,
246 iwl_mvm_is_dcm_with_go_iterator,
248 if (data.is_dcm_with_p2p_go)
249 return IWL_SCAN_TYPE_FAST_BALANCE;
253 if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
254 return IWL_SCAN_TYPE_MILD;
256 return IWL_SCAN_TYPE_WILD;
260 iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
261 struct ieee80211_vif *vif)
263 enum iwl_mvm_traffic_load load;
266 load = iwl_mvm_get_traffic_load(mvm);
267 low_latency = iwl_mvm_low_latency(mvm);
269 return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
273 iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
274 struct ieee80211_vif *vif,
275 enum nl80211_band band)
277 enum iwl_mvm_traffic_load load;
280 load = iwl_mvm_get_traffic_load_band(mvm, band);
281 low_latency = iwl_mvm_low_latency_band(mvm, band);
283 return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
286 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
288 /* require rrm scan whenever the fw supports it */
289 return fw_has_capa(&mvm->fw->ucode_capa,
290 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
293 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
297 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
299 /* we create the 802.11 header and SSID element */
300 max_probe_len -= 24 + 2;
302 /* DS parameter set element is added on 2.4GHZ band if required */
303 if (iwl_mvm_rrm_scan_needed(mvm))
306 return max_probe_len;
309 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
311 int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
313 /* TODO: [BUG] This function should return the maximum allowed size of
314 * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
315 * in the same command. So the correct implementation of this function
316 * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
317 * command has only 512 bytes and it would leave us with about 240
318 * bytes for scan IEs, which is clearly not enough. So meanwhile
319 * we will report an incorrect value. This may result in a failure to
320 * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
321 * functions with -ENOBUFS, if a large enough probe will be provided.
326 void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
327 struct iwl_rx_cmd_buffer *rxb)
329 struct iwl_rx_packet *pkt = rxb_addr(rxb);
330 struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
333 "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
334 notif->status, notif->scanned_channels);
336 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
337 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
338 ieee80211_sched_scan_results(mvm->hw);
339 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
343 void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
344 struct iwl_rx_cmd_buffer *rxb)
346 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
347 ieee80211_sched_scan_results(mvm->hw);
350 static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
353 case IWL_SCAN_EBS_SUCCESS:
355 case IWL_SCAN_EBS_INACTIVE:
357 case IWL_SCAN_EBS_FAILED:
358 case IWL_SCAN_EBS_CHAN_NOT_FOUND:
364 void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
365 struct iwl_rx_cmd_buffer *rxb)
367 struct iwl_rx_packet *pkt = rxb_addr(rxb);
368 struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
369 bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
371 /* If this happens, the firmware has mistakenly sent an LMAC
372 * notification during UMAC scans -- warn and ignore it.
374 if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
375 IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
378 /* scan status must be locked for proper checking */
379 lockdep_assert_held(&mvm->mutex);
381 /* We first check if we were stopping a scan, in which case we
382 * just clear the stopping flag. Then we check if it was a
383 * firmware initiated stop, in which case we need to inform
385 * Note that we can have a stopping and a running scan
386 * simultaneously, but we can't have two different types of
387 * scans stopping or running at the same time (since LMAC
388 * doesn't support it).
391 if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
392 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
394 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
395 aborted ? "aborted" : "completed",
396 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
398 "Last line %d, Last iteration %d, Time after last iteration %d\n",
399 scan_notif->last_schedule_line,
400 scan_notif->last_schedule_iteration,
401 __le32_to_cpu(scan_notif->time_after_last_iter));
403 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
404 } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
405 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
406 aborted ? "aborted" : "completed",
407 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
409 mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
410 } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
411 WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
413 IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
414 aborted ? "aborted" : "completed",
415 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
417 "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
418 scan_notif->last_schedule_line,
419 scan_notif->last_schedule_iteration,
420 __le32_to_cpu(scan_notif->time_after_last_iter));
422 mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
423 ieee80211_sched_scan_stopped(mvm->hw);
424 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
425 } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
426 struct cfg80211_scan_info info = {
430 IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
431 aborted ? "aborted" : "completed",
432 iwl_mvm_ebs_status_str(scan_notif->ebs_status));
434 mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
435 ieee80211_scan_completed(mvm->hw, &info);
436 cancel_delayed_work(&mvm->scan_timeout_dwork);
437 iwl_mvm_resume_tcm(mvm);
440 "got scan complete notification but no scan is running\n");
443 mvm->last_ebs_successful =
444 scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
445 scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
448 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
452 for (i = 0; i < PROBE_OPTION_MAX; i++) {
453 if (!ssid_list[i].len)
455 if (ssid_list[i].len == ssid_len &&
456 !memcmp(ssid_list->ssid, ssid, ssid_len))
462 /* We insert the SSIDs in an inverted order, because the FW will
465 static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
466 struct iwl_ssid_ie *ssids,
474 * copy SSIDs from match list.
475 * iwl_config_sched_scan_profiles() uses the order of these ssids to
478 for (i = 0, j = params->n_match_sets - 1;
479 j >= 0 && i < PROBE_OPTION_MAX;
481 /* skip empty SSID matchsets */
482 if (!params->match_sets[j].ssid.ssid_len)
484 ssids[i].id = WLAN_EID_SSID;
485 ssids[i].len = params->match_sets[j].ssid.ssid_len;
486 memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
490 /* add SSIDs from scan SSID list */
491 for (j = params->n_ssids - 1;
492 j >= 0 && i < PROBE_OPTION_MAX;
494 index = iwl_ssid_exist(params->ssids[j].ssid,
495 params->ssids[j].ssid_len,
498 ssids[i].id = WLAN_EID_SSID;
499 ssids[i].len = params->ssids[j].ssid_len;
500 memcpy(ssids[i].ssid, params->ssids[j].ssid,
502 tmp_bitmap |= BIT(i);
504 tmp_bitmap |= BIT(index);
508 *ssid_bitmap = tmp_bitmap;
512 iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
513 struct cfg80211_sched_scan_request *req)
515 struct iwl_scan_offload_profile *profile;
516 struct iwl_scan_offload_profile_cfg_v1 *profile_cfg_v1;
517 struct iwl_scan_offload_blocklist *blocklist;
518 struct iwl_scan_offload_profile_cfg_data *data;
519 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
520 int profile_cfg_size = sizeof(*data) +
521 sizeof(*profile) * max_profiles;
522 struct iwl_host_cmd cmd = {
523 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
524 .len[1] = profile_cfg_size,
525 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
526 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
532 if (WARN_ON(req->n_match_sets > max_profiles))
535 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
536 blocklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
538 blocklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
540 blocklist = kcalloc(blocklist_len, sizeof(*blocklist), GFP_KERNEL);
544 profile_cfg_v1 = kzalloc(profile_cfg_size, GFP_KERNEL);
545 if (!profile_cfg_v1) {
550 cmd.data[0] = blocklist;
551 cmd.len[0] = sizeof(*blocklist) * blocklist_len;
552 cmd.data[1] = profile_cfg_v1;
554 /* if max_profile is MAX_PROFILES_V2, we have the new API */
555 if (max_profiles == IWL_SCAN_MAX_PROFILES_V2) {
556 struct iwl_scan_offload_profile_cfg *profile_cfg =
557 (struct iwl_scan_offload_profile_cfg *)profile_cfg_v1;
559 data = &profile_cfg->data;
561 data = &profile_cfg_v1->data;
564 /* No blocklist configuration */
565 data->num_profiles = req->n_match_sets;
566 data->active_clients = SCAN_CLIENT_SCHED_SCAN;
567 data->pass_match = SCAN_CLIENT_SCHED_SCAN;
568 data->match_notify = SCAN_CLIENT_SCHED_SCAN;
570 if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
571 data->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
573 for (i = 0; i < req->n_match_sets; i++) {
574 profile = &profile_cfg_v1->profiles[i];
575 profile->ssid_index = i;
576 /* Support any cipher and auth algorithm */
577 profile->unicast_cipher = 0xff;
578 profile->auth_alg = 0xff;
579 profile->network_type = IWL_NETWORK_TYPE_ANY;
580 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
581 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
584 IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
586 ret = iwl_mvm_send_cmd(mvm, &cmd);
587 kfree(profile_cfg_v1);
594 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
595 struct cfg80211_sched_scan_request *req)
597 if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
599 "Sending scheduled scan with filtering, n_match_sets %d\n",
601 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
605 IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
607 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
611 static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
614 struct iwl_host_cmd cmd = {
615 .id = SCAN_OFFLOAD_ABORT_CMD,
617 u32 status = CAN_ABORT_STATUS;
619 ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
623 if (status != CAN_ABORT_STATUS) {
625 * The scan abort will return 1 for success or
626 * 2 for "failure". A failure condition can be
627 * due to simply not being in an active scan which
628 * can occur if we send the scan abort before the
629 * microcode has notified us that a scan is completed.
631 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
638 static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
639 struct iwl_scan_req_tx_cmd *tx_cmd,
642 tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
644 tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
648 if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
651 tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
652 tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
655 * Fw doesn't use this sta anymore, pending deprecation via HOST API
659 tx_cmd[0].sta_id = 0xff;
660 tx_cmd[1].sta_id = 0xff;
663 tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
666 tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
672 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
673 struct ieee80211_channel **channels,
674 int n_channels, u32 ssid_bitmap,
675 struct iwl_scan_req_lmac *cmd)
677 struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
680 for (i = 0; i < n_channels; i++) {
681 channel_cfg[i].channel_num =
682 cpu_to_le16(channels[i]->hw_value);
683 channel_cfg[i].iter_count = cpu_to_le16(1);
684 channel_cfg[i].iter_interval = 0;
685 channel_cfg[i].flags =
686 cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
691 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
692 size_t len, u8 *const pos)
694 static const u8 before_ds_params[] = {
698 WLAN_EID_EXT_SUPP_RATES,
703 if (!iwl_mvm_rrm_scan_needed(mvm)) {
704 memcpy(newpos, ies, len);
708 offs = ieee80211_ie_split(ies, len,
710 ARRAY_SIZE(before_ds_params),
713 memcpy(newpos, ies, offs);
716 /* Add a placeholder for DS Parameter Set element */
717 *newpos++ = WLAN_EID_DS_PARAMS;
721 memcpy(newpos, ies + offs, len - offs);
722 newpos += len - offs;
727 #define WFA_TPC_IE_LEN 9
729 static void iwl_mvm_add_tpc_report_ie(u8 *pos)
731 pos[0] = WLAN_EID_VENDOR_SPECIFIC;
732 pos[1] = WFA_TPC_IE_LEN - 2;
733 pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
734 pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
735 pos[4] = WLAN_OUI_MICROSOFT & 0xff;
736 pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
738 /* pos[7] - tx power will be inserted by the FW */
744 iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
745 struct ieee80211_scan_ies *ies,
746 struct iwl_mvm_scan_params *params)
748 struct ieee80211_mgmt *frame = (void *)params->preq.buf;
750 const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
751 params->mac_addr : NULL;
754 * Unfortunately, right now the offload scan doesn't support randomising
755 * within the firmware, so until the firmware API is ready we implement
756 * it in the driver. This means that the scan iterations won't really be
757 * random, only when it's restarted, but at least that helps a bit.
760 get_random_mask_addr(frame->sa, mac_addr,
761 params->mac_addr_mask);
763 memcpy(frame->sa, vif->addr, ETH_ALEN);
765 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
766 eth_broadcast_addr(frame->da);
767 eth_broadcast_addr(frame->bssid);
770 pos = frame->u.probe_req.variable;
771 *pos++ = WLAN_EID_SSID;
774 params->preq.mac_header.offset = 0;
775 params->preq.mac_header.len = cpu_to_le16(24 + 2);
777 /* Insert ds parameter set element on 2.4 GHz band */
778 newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
779 ies->ies[NL80211_BAND_2GHZ],
780 ies->len[NL80211_BAND_2GHZ],
782 params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
783 params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
786 memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
787 ies->len[NL80211_BAND_5GHZ]);
788 params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
789 params->preq.band_data[1].len =
790 cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
791 pos += ies->len[NL80211_BAND_5GHZ];
793 memcpy(pos, ies->ies[NL80211_BAND_6GHZ],
794 ies->len[NL80211_BAND_6GHZ]);
795 params->preq.band_data[2].offset = cpu_to_le16(pos - params->preq.buf);
796 params->preq.band_data[2].len =
797 cpu_to_le16(ies->len[NL80211_BAND_6GHZ]);
798 pos += ies->len[NL80211_BAND_6GHZ];
799 memcpy(pos, ies->common_ies, ies->common_ie_len);
800 params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
802 if (iwl_mvm_rrm_scan_needed(mvm) &&
803 !fw_has_capa(&mvm->fw->ucode_capa,
804 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
805 iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
806 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
809 params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
813 static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
814 struct iwl_scan_req_lmac *cmd,
815 struct iwl_mvm_scan_params *params)
817 cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
818 cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
819 cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
820 cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
821 cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
822 cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
823 cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
826 static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
827 struct ieee80211_scan_ies *ies,
830 return ((n_ssids <= PROBE_OPTION_MAX) &&
831 (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
832 (ies->common_ie_len +
833 ies->len[NL80211_BAND_2GHZ] +
834 ies->len[NL80211_BAND_5GHZ] <=
835 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
838 static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
839 struct ieee80211_vif *vif)
841 const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
844 if (iwl_mvm_is_cdb_supported(mvm))
845 low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
847 low_latency = iwl_mvm_low_latency(mvm);
849 /* We can only use EBS if:
850 * 1. the feature is supported;
851 * 2. the last EBS was successful;
852 * 3. if only single scan, the single scan EBS API is supported;
853 * 4. it's not a p2p find operation.
854 * 5. we are not in low latency mode,
855 * or if fragmented ebs is supported by the FW
857 return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
858 mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
859 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
860 (!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
863 static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
865 return params->n_scan_plans == 1 &&
866 params->scan_plans[0].iterations == 1;
869 static bool iwl_mvm_is_scan_fragmented(enum iwl_mvm_scan_type type)
871 return (type == IWL_SCAN_TYPE_FRAGMENTED ||
872 type == IWL_SCAN_TYPE_FAST_BALANCE);
875 static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
876 struct iwl_mvm_scan_params *params,
877 struct ieee80211_vif *vif)
881 if (params->n_ssids == 0)
882 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
884 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
885 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
887 if (iwl_mvm_is_scan_fragmented(params->type))
888 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
890 if (iwl_mvm_rrm_scan_needed(mvm) &&
891 fw_has_capa(&mvm->fw->ucode_capa,
892 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
893 flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
895 if (params->pass_all)
896 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
898 flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
900 #ifdef CONFIG_IWLWIFI_DEBUGFS
901 if (mvm->scan_iter_notif_enabled)
902 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
905 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
906 flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
908 if (iwl_mvm_is_regular_scan(params) &&
909 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
910 !iwl_mvm_is_scan_fragmented(params->type))
911 flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
917 iwl_mvm_scan_set_legacy_probe_req(struct iwl_scan_probe_req_v1 *p_req,
918 struct iwl_scan_probe_req *src_p_req)
922 p_req->mac_header = src_p_req->mac_header;
923 for (i = 0; i < SCAN_NUM_BAND_PROBE_DATA_V_1; i++)
924 p_req->band_data[i] = src_p_req->band_data[i];
925 p_req->common_data = src_p_req->common_data;
926 memcpy(p_req->buf, src_p_req->buf, sizeof(p_req->buf));
929 static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
930 struct iwl_mvm_scan_params *params)
932 struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
933 struct iwl_scan_probe_req_v1 *preq =
934 (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
935 mvm->fw->ucode_capa.n_scan_channels);
940 if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
943 iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
945 cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
946 cmd->iter_num = cpu_to_le32(1);
947 cmd->n_channels = (u8)params->n_channels;
949 cmd->delay = cpu_to_le32(params->delay);
951 cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
954 band = iwl_mvm_phy_band_from_nl80211(params->channels[0]->band);
955 cmd->flags = cpu_to_le32(band);
956 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
957 MAC_FILTER_IN_BEACON);
958 iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
959 iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
961 /* this API uses bits 1-20 instead of 0-19 */
964 for (i = 0; i < params->n_scan_plans; i++) {
965 struct cfg80211_sched_scan_plan *scan_plan =
966 ¶ms->scan_plans[i];
968 cmd->schedule[i].delay =
969 cpu_to_le16(scan_plan->interval);
970 cmd->schedule[i].iterations = scan_plan->iterations;
971 cmd->schedule[i].full_scan_mul = 1;
975 * If the number of iterations of the last scan plan is set to
976 * zero, it should run infinitely. However, this is not always the case.
977 * For example, when regular scan is requested the driver sets one scan
978 * plan with one iteration.
980 if (!cmd->schedule[i - 1].iterations)
981 cmd->schedule[i - 1].iterations = 0xff;
983 if (iwl_mvm_scan_use_ebs(mvm, vif)) {
984 cmd->channel_opt[0].flags =
985 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
986 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
987 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
988 cmd->channel_opt[0].non_ebs_ratio =
989 cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
990 cmd->channel_opt[1].flags =
991 cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
992 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
993 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
994 cmd->channel_opt[1].non_ebs_ratio =
995 cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
998 iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
999 params->n_channels, ssid_bitmap, cmd);
1001 iwl_mvm_scan_set_legacy_probe_req(preq, ¶ms->preq);
1006 static int rate_to_scan_rate_flag(unsigned int rate)
1008 static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1009 [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
1010 [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
1011 [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
1012 [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
1013 [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
1014 [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
1015 [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
1016 [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
1017 [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
1018 [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
1019 [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
1020 [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
1023 return rate_to_scan_rate[rate];
1026 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1028 struct ieee80211_supported_band *band;
1029 unsigned int rates = 0;
1032 band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1033 for (i = 0; i < band->n_bitrates; i++)
1034 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1035 band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1036 for (i = 0; i < band->n_bitrates; i++)
1037 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1039 /* Set both basic rates and supported rates */
1040 rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1042 return cpu_to_le32(rates);
1045 static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1046 struct iwl_scan_dwell *dwell)
1048 dwell->active = IWL_SCAN_DWELL_ACTIVE;
1049 dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1050 dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1051 dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1054 static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels,
1057 struct ieee80211_supported_band *band;
1060 band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1061 for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1062 channels[j] = band->channels[i].hw_value;
1063 band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1064 for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1065 channels[j] = band->channels[i].hw_value;
1068 static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1069 u32 flags, u8 channel_flags,
1072 enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, NULL);
1073 struct iwl_scan_config_v1 *cfg = config;
1075 cfg->flags = cpu_to_le32(flags);
1076 cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1077 cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1078 cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1079 cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1080 cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1082 iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1084 memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1086 /* This function should not be called when using ADD_STA ver >=12 */
1087 WARN_ON_ONCE(iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1090 cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1091 cfg->channel_flags = channel_flags;
1093 iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1096 static void iwl_mvm_fill_scan_config_v2(struct iwl_mvm *mvm, void *config,
1097 u32 flags, u8 channel_flags,
1100 struct iwl_scan_config_v2 *cfg = config;
1102 cfg->flags = cpu_to_le32(flags);
1103 cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1104 cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1105 cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1107 if (iwl_mvm_is_cdb_supported(mvm)) {
1108 enum iwl_mvm_scan_type lb_type, hb_type;
1110 lb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1112 hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1115 cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1116 cpu_to_le32(scan_timing[lb_type].max_out_time);
1117 cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1118 cpu_to_le32(scan_timing[lb_type].suspend_time);
1120 cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1121 cpu_to_le32(scan_timing[hb_type].max_out_time);
1122 cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1123 cpu_to_le32(scan_timing[hb_type].suspend_time);
1125 enum iwl_mvm_scan_type type =
1126 iwl_mvm_get_scan_type(mvm, NULL);
1128 cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1129 cpu_to_le32(scan_timing[type].max_out_time);
1130 cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1131 cpu_to_le32(scan_timing[type].suspend_time);
1134 iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1136 memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1138 /* This function should not be called when using ADD_STA ver >=12 */
1139 WARN_ON_ONCE(iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1142 cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1143 cfg->channel_flags = channel_flags;
1145 iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1148 static int iwl_mvm_legacy_config_scan(struct iwl_mvm *mvm)
1152 struct iwl_host_cmd cmd = {
1153 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1155 enum iwl_mvm_scan_type type;
1156 enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1158 mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1159 mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1163 if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1164 num_channels = mvm->fw->ucode_capa.n_scan_channels;
1166 if (iwl_mvm_is_cdb_supported(mvm)) {
1167 type = iwl_mvm_get_scan_type_band(mvm, NULL,
1169 hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1171 if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1174 type = iwl_mvm_get_scan_type(mvm, NULL);
1175 if (type == mvm->scan_type)
1179 if (iwl_mvm_cdb_scan_api(mvm))
1180 cmd_size = sizeof(struct iwl_scan_config_v2);
1182 cmd_size = sizeof(struct iwl_scan_config_v1);
1183 cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1185 cfg = kzalloc(cmd_size, GFP_KERNEL);
1189 flags = SCAN_CONFIG_FLAG_ACTIVATE |
1190 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1191 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1192 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1193 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1194 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1195 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1196 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1197 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1198 SCAN_CONFIG_N_CHANNELS(num_channels) |
1199 (iwl_mvm_is_scan_fragmented(type) ?
1200 SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1201 SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1203 channel_flags = IWL_CHANNEL_FLAG_EBS |
1204 IWL_CHANNEL_FLAG_ACCURATE_EBS |
1205 IWL_CHANNEL_FLAG_EBS_ADD |
1206 IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1209 * Check for fragmented scan on LMAC2 - high band.
1210 * LMAC1 - low band is checked above.
1212 if (iwl_mvm_cdb_scan_api(mvm)) {
1213 if (iwl_mvm_is_cdb_supported(mvm))
1214 flags |= (iwl_mvm_is_scan_fragmented(hb_type)) ?
1215 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1216 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1217 iwl_mvm_fill_scan_config_v2(mvm, cfg, flags, channel_flags,
1220 iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags,
1225 cmd.len[0] = cmd_size;
1226 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1228 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1230 ret = iwl_mvm_send_cmd(mvm, &cmd);
1232 mvm->scan_type = type;
1233 mvm->hb_scan_type = hb_type;
1240 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1242 struct iwl_scan_config cfg;
1243 struct iwl_host_cmd cmd = {
1244 .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1245 .len[0] = sizeof(cfg),
1247 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1250 if (!iwl_mvm_is_reduced_config_scan_supported(mvm))
1251 return iwl_mvm_legacy_config_scan(mvm);
1253 memset(&cfg, 0, sizeof(cfg));
1255 if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1257 cfg.bcast_sta_id = mvm->aux_sta.sta_id;
1259 * Fw doesn't use this sta anymore, pending deprecation via HOST API
1263 cfg.bcast_sta_id = 0xff;
1265 cfg.tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1266 cfg.rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1268 IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1270 return iwl_mvm_send_cmd(mvm, &cmd);
1273 static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1277 for (i = 0; i < mvm->max_scans; i++)
1278 if (mvm->scan_uid_status[i] == status)
1284 static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1285 struct iwl_scan_req_umac *cmd,
1286 struct iwl_mvm_scan_params *params)
1288 struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1289 u8 active_dwell, passive_dwell;
1291 timing = &scan_timing[params->type];
1292 active_dwell = IWL_SCAN_DWELL_ACTIVE;
1293 passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1295 if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1296 cmd->v7.adwell_default_n_aps_social =
1297 IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1298 cmd->v7.adwell_default_n_aps =
1299 IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1301 if (iwl_mvm_is_adwell_hb_ap_num_supported(mvm))
1302 cmd->v9.adwell_default_hb_n_aps =
1303 IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1305 /* if custom max budget was configured with debugfs */
1306 if (IWL_MVM_ADWELL_MAX_BUDGET)
1307 cmd->v7.adwell_max_budget =
1308 cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1309 else if (params->ssids && params->ssids[0].ssid_len)
1310 cmd->v7.adwell_max_budget =
1311 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1313 cmd->v7.adwell_max_budget =
1314 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1316 cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1317 cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1318 cpu_to_le32(timing->max_out_time);
1319 cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1320 cpu_to_le32(timing->suspend_time);
1322 if (iwl_mvm_is_cdb_supported(mvm)) {
1323 hb_timing = &scan_timing[params->hb_type];
1325 cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1326 cpu_to_le32(hb_timing->max_out_time);
1327 cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1328 cpu_to_le32(hb_timing->suspend_time);
1331 if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1332 cmd->v7.active_dwell = active_dwell;
1333 cmd->v7.passive_dwell = passive_dwell;
1334 cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1336 cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1337 cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1338 if (iwl_mvm_is_cdb_supported(mvm)) {
1339 cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1341 cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1346 cmd->v1.extended_dwell = IWL_SCAN_DWELL_EXTENDED;
1347 cmd->v1.active_dwell = active_dwell;
1348 cmd->v1.passive_dwell = passive_dwell;
1349 cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1351 if (iwl_mvm_is_cdb_supported(mvm)) {
1352 hb_timing = &scan_timing[params->hb_type];
1354 cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1355 cpu_to_le32(hb_timing->max_out_time);
1356 cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1357 cpu_to_le32(hb_timing->suspend_time);
1360 if (iwl_mvm_cdb_scan_api(mvm)) {
1361 cmd->v6.scan_priority =
1362 cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1363 cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1364 cpu_to_le32(timing->max_out_time);
1365 cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1366 cpu_to_le32(timing->suspend_time);
1368 cmd->v1.scan_priority =
1369 cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1370 cmd->v1.max_out_time =
1371 cpu_to_le32(timing->max_out_time);
1372 cmd->v1.suspend_time =
1373 cpu_to_le32(timing->suspend_time);
1377 if (iwl_mvm_is_regular_scan(params))
1378 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1380 cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1383 static u32 iwl_mvm_scan_umac_ooc_priority(struct iwl_mvm_scan_params *params)
1385 return iwl_mvm_is_regular_scan(params) ?
1386 IWL_SCAN_PRIORITY_EXT_6 :
1387 IWL_SCAN_PRIORITY_EXT_2;
1391 iwl_mvm_scan_umac_dwell_v10(struct iwl_mvm *mvm,
1392 struct iwl_scan_general_params_v10 *general_params,
1393 struct iwl_mvm_scan_params *params)
1395 struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1396 u8 active_dwell, passive_dwell;
1398 timing = &scan_timing[params->type];
1399 active_dwell = IWL_SCAN_DWELL_ACTIVE;
1400 passive_dwell = IWL_SCAN_DWELL_PASSIVE;
1402 general_params->adwell_default_social_chn =
1403 IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1404 general_params->adwell_default_2g = IWL_SCAN_ADWELL_DEFAULT_LB_N_APS;
1405 general_params->adwell_default_5g = IWL_SCAN_ADWELL_DEFAULT_HB_N_APS;
1407 /* if custom max budget was configured with debugfs */
1408 if (IWL_MVM_ADWELL_MAX_BUDGET)
1409 general_params->adwell_max_budget =
1410 cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1411 else if (params->ssids && params->ssids[0].ssid_len)
1412 general_params->adwell_max_budget =
1413 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1415 general_params->adwell_max_budget =
1416 cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1418 general_params->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1419 general_params->max_out_of_time[SCAN_LB_LMAC_IDX] =
1420 cpu_to_le32(timing->max_out_time);
1421 general_params->suspend_time[SCAN_LB_LMAC_IDX] =
1422 cpu_to_le32(timing->suspend_time);
1424 hb_timing = &scan_timing[params->hb_type];
1426 general_params->max_out_of_time[SCAN_HB_LMAC_IDX] =
1427 cpu_to_le32(hb_timing->max_out_time);
1428 general_params->suspend_time[SCAN_HB_LMAC_IDX] =
1429 cpu_to_le32(hb_timing->suspend_time);
1431 general_params->active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1432 general_params->passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1433 general_params->active_dwell[SCAN_HB_LMAC_IDX] = active_dwell;
1434 general_params->passive_dwell[SCAN_HB_LMAC_IDX] = passive_dwell;
1437 struct iwl_mvm_scan_channel_segment {
1440 u8 first_channel_id;
1442 u8 channel_spacing_shift;
1446 static const struct iwl_mvm_scan_channel_segment scan_channel_segments[] = {
1450 .first_channel_id = 1,
1451 .last_channel_id = 14,
1452 .channel_spacing_shift = 0,
1458 .first_channel_id = 36,
1459 .last_channel_id = 144,
1460 .channel_spacing_shift = 2,
1466 .first_channel_id = 149,
1467 .last_channel_id = 181,
1468 .channel_spacing_shift = 2,
1474 .first_channel_id = 1,
1475 .last_channel_id = 241,
1476 .channel_spacing_shift = 2,
1481 static int iwl_mvm_scan_ch_and_band_to_idx(u8 channel_id, u8 band)
1488 for (i = 0; i < ARRAY_SIZE(scan_channel_segments); i++) {
1489 const struct iwl_mvm_scan_channel_segment *ch_segment =
1490 &scan_channel_segments[i];
1493 if (ch_segment->band != band ||
1494 ch_segment->first_channel_id > channel_id ||
1495 ch_segment->last_channel_id < channel_id)
1498 ch_offset = (channel_id - ch_segment->first_channel_id) >>
1499 ch_segment->channel_spacing_shift;
1501 index = scan_channel_segments[i].start_idx + ch_offset;
1502 if (index < IWL_SCAN_NUM_CHANNELS)
1511 static const u8 p2p_go_friendly_chs[] = {
1512 36, 40, 44, 48, 149, 153, 157, 161, 165,
1515 static const u8 social_chs[] = {
1519 static void iwl_mvm_scan_ch_add_n_aps_override(enum nl80211_iftype vif_type,
1520 u8 ch_id, u8 band, u8 *ch_bitmap,
1521 size_t bitmap_n_entries)
1525 if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
1528 for (i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
1529 if (p2p_go_friendly_chs[i] == ch_id) {
1530 int ch_idx, bitmap_idx;
1532 ch_idx = iwl_mvm_scan_ch_and_band_to_idx(ch_id, band);
1536 bitmap_idx = ch_idx / 8;
1537 if (bitmap_idx >= bitmap_n_entries)
1540 ch_idx = ch_idx % 8;
1541 ch_bitmap[bitmap_idx] |= BIT(ch_idx);
1548 static u32 iwl_mvm_scan_ch_n_aps_flag(enum nl80211_iftype vif_type, u8 ch_id)
1553 if (vif_type != NL80211_IFTYPE_P2P_DEVICE)
1556 for (i = 0; i < ARRAY_SIZE(p2p_go_friendly_chs); i++) {
1557 if (p2p_go_friendly_chs[i] == ch_id) {
1558 flags |= IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY_BIT;
1566 for (i = 0; i < ARRAY_SIZE(social_chs); i++) {
1567 if (social_chs[i] == ch_id) {
1568 flags |= IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS_BIT;
1578 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1579 struct ieee80211_channel **channels,
1580 int n_channels, u32 flags,
1581 struct iwl_scan_channel_cfg_umac *channel_cfg)
1585 for (i = 0; i < n_channels; i++) {
1586 channel_cfg[i].flags = cpu_to_le32(flags);
1587 channel_cfg[i].v1.channel_num = channels[i]->hw_value;
1588 if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
1589 enum nl80211_band band = channels[i]->band;
1591 channel_cfg[i].v2.band =
1592 iwl_mvm_phy_band_from_nl80211(band);
1593 channel_cfg[i].v2.iter_count = 1;
1594 channel_cfg[i].v2.iter_interval = 0;
1596 channel_cfg[i].v1.iter_count = 1;
1597 channel_cfg[i].v1.iter_interval = 0;
1603 iwl_mvm_umac_scan_cfg_channels_v4(struct iwl_mvm *mvm,
1604 struct ieee80211_channel **channels,
1605 struct iwl_scan_channel_params_v4 *cp,
1606 int n_channels, u32 flags,
1607 enum nl80211_iftype vif_type)
1609 u8 *bitmap = cp->adwell_ch_override_bitmap;
1610 size_t bitmap_n_entries = ARRAY_SIZE(cp->adwell_ch_override_bitmap);
1613 for (i = 0; i < n_channels; i++) {
1614 enum nl80211_band band = channels[i]->band;
1615 struct iwl_scan_channel_cfg_umac *cfg =
1616 &cp->channel_config[i];
1618 cfg->flags = cpu_to_le32(flags);
1619 cfg->v2.channel_num = channels[i]->hw_value;
1620 cfg->v2.band = iwl_mvm_phy_band_from_nl80211(band);
1621 cfg->v2.iter_count = 1;
1622 cfg->v2.iter_interval = 0;
1624 iwl_mvm_scan_ch_add_n_aps_override(vif_type,
1625 cfg->v2.channel_num,
1626 cfg->v2.band, bitmap,
1632 iwl_mvm_umac_scan_cfg_channels_v6(struct iwl_mvm *mvm,
1633 struct ieee80211_channel **channels,
1634 struct iwl_scan_channel_params_v6 *cp,
1635 int n_channels, u32 flags,
1636 enum nl80211_iftype vif_type)
1640 for (i = 0; i < n_channels; i++) {
1641 enum nl80211_band band = channels[i]->band;
1642 struct iwl_scan_channel_cfg_umac *cfg = &cp->channel_config[i];
1644 iwl_mvm_scan_ch_n_aps_flag(vif_type,
1645 cfg->v2.channel_num);
1647 cfg->flags = cpu_to_le32(flags | n_aps_flag);
1648 cfg->v2.channel_num = channels[i]->hw_value;
1649 cfg->v2.band = iwl_mvm_phy_band_from_nl80211(band);
1650 if (cfg80211_channel_is_psc(channels[i]))
1652 cfg->v2.iter_count = 1;
1653 cfg->v2.iter_interval = 0;
1658 iwl_mvm_umac_scan_fill_6g_chan_list(struct iwl_mvm_scan_params *params,
1659 __le32 *cmd_short_ssid, u8 *cmd_bssid,
1660 u8 *scan_ssid_num, u8 *bssid_num)
1662 int j, idex_s = 0, idex_b = 0;
1663 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
1664 params->scan_6ghz_params;
1666 if (!params->n_6ghz_params) {
1667 for (j = 0; j < params->n_ssids; j++) {
1668 cmd_short_ssid[idex_s++] =
1669 cpu_to_le32(~crc32_le(~0, params->ssids[j].ssid,
1670 params->ssids[j].ssid_len));
1677 * Populate the arrays of the short SSIDs and the BSSIDs using the 6GHz
1678 * collocated parameters. This might not be optimal, as this processing
1679 * does not (yet) correspond to the actual channels, so it is possible
1680 * that some entries would be left out.
1682 * TODO: improve this logic.
1684 for (j = 0; j < params->n_6ghz_params; j++) {
1687 /* First, try to place the short SSID */
1688 if (scan_6ghz_params[j].short_ssid_valid) {
1689 for (k = 0; k < idex_s; k++) {
1690 if (cmd_short_ssid[k] ==
1691 cpu_to_le32(scan_6ghz_params[j].short_ssid))
1695 if (k == idex_s && idex_s < SCAN_SHORT_SSID_MAX_SIZE) {
1696 cmd_short_ssid[idex_s++] =
1697 cpu_to_le32(scan_6ghz_params[j].short_ssid);
1702 /* try to place BSSID for the same entry */
1703 for (k = 0; k < idex_b; k++) {
1704 if (!memcmp(&cmd_bssid[ETH_ALEN * k],
1705 scan_6ghz_params[j].bssid, ETH_ALEN))
1709 if (k == idex_b && idex_b < SCAN_BSSID_MAX_SIZE) {
1710 memcpy(&cmd_bssid[ETH_ALEN * idex_b++],
1711 scan_6ghz_params[j].bssid, ETH_ALEN);
1718 /* TODO: this function can be merged with iwl_mvm_scan_umac_fill_ch_p_v6 */
1720 iwl_mvm_umac_scan_cfg_channels_v6_6g(struct iwl_mvm_scan_params *params,
1721 u32 n_channels, __le32 *cmd_short_ssid,
1722 u8 *cmd_bssid, u8 scan_ssid_num,
1724 struct iwl_scan_channel_params_v6 *cp,
1725 enum nl80211_iftype vif_type)
1727 struct iwl_scan_channel_cfg_umac *channel_cfg = cp->channel_config;
1729 struct cfg80211_scan_6ghz_params *scan_6ghz_params =
1730 params->scan_6ghz_params;
1732 for (i = 0; i < params->n_channels; i++) {
1733 struct iwl_scan_channel_cfg_umac *cfg =
1734 &cp->channel_config[i];
1736 u32 s_ssid_bitmap = 0, bssid_bitmap = 0, flags = 0;
1737 u8 j, k, s_max = 0, b_max = 0, n_used_bssid_entries;
1738 bool force_passive, found = false,
1739 unsolicited_probe_on_chan = false, psc_no_listen = false;
1741 cfg->v1.channel_num = params->channels[i]->hw_value;
1743 cfg->v2.iter_count = 1;
1744 cfg->v2.iter_interval = 0;
1747 * The optimize the scan time, i.e., reduce the scan dwell time
1748 * on each channel, the below logic tries to set 3 direct BSSID
1749 * probe requests for each broadcast probe request with a short
1751 * TODO: improve this logic
1753 n_used_bssid_entries = 3;
1754 for (j = 0; j < params->n_6ghz_params; j++) {
1755 if (!(scan_6ghz_params[j].channel_idx == i))
1759 unsolicited_probe_on_chan |=
1760 scan_6ghz_params[j].unsolicited_probe;
1761 psc_no_listen |= scan_6ghz_params[j].psc_no_listen;
1763 for (k = 0; k < scan_ssid_num; k++) {
1764 if (!scan_6ghz_params[j].unsolicited_probe &&
1765 le32_to_cpu(cmd_short_ssid[k]) ==
1766 scan_6ghz_params[j].short_ssid) {
1767 /* Relevant short SSID bit set */
1768 if (s_ssid_bitmap & BIT(k)) {
1774 * Use short SSID only to create a new
1775 * iteration during channel dwell.
1777 if (n_used_bssid_entries >= 3) {
1778 s_ssid_bitmap |= BIT(k);
1780 n_used_bssid_entries -= 3;
1790 for (k = 0; k < bssid_num; k++) {
1791 if (!memcmp(&cmd_bssid[ETH_ALEN * k],
1792 scan_6ghz_params[j].bssid,
1794 if (!(bssid_bitmap & BIT(k))) {
1795 bssid_bitmap |= BIT(k);
1797 n_used_bssid_entries++;
1804 flags = bssid_bitmap | (s_ssid_bitmap << 16);
1806 if (cfg80211_channel_is_psc(params->channels[i]) &&
1808 flags |= IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN;
1810 if (unsolicited_probe_on_chan)
1811 flags |= IWL_UHB_CHAN_CFG_FLAG_UNSOLICITED_PROBE_RES;
1814 * In the following cases apply passive scan:
1815 * 1. Non fragmented scan:
1816 * - PSC channel with NO_LISTEN_FLAG on should be treated
1817 * like non PSC channel
1818 * - Non PSC channel with more than 3 short SSIDs or more
1820 * - Non PSC Channel with unsolicited probe response and
1821 * more than 2 short SSIDs or more than 6 BSSIDs.
1822 * - PSC channel with more than 2 short SSIDs or more than
1824 * 3. Fragmented scan:
1825 * - PSC channel with more than 1 SSID or 3 BSSIDs.
1826 * - Non PSC channel with more than 2 SSIDs or 6 BSSIDs.
1827 * - Non PSC channel with unsolicited probe response and
1828 * more than 1 SSID or more than 3 BSSIDs.
1830 if (!iwl_mvm_is_scan_fragmented(params->type)) {
1831 if (!cfg80211_channel_is_psc(params->channels[i]) ||
1832 flags & IWL_UHB_CHAN_CFG_FLAG_PSC_CHAN_NO_LISTEN) {
1833 force_passive = (s_max > 3 || b_max > 9);
1834 force_passive |= (unsolicited_probe_on_chan &&
1835 (s_max > 2 || b_max > 6));
1837 force_passive = (s_max > 2 || b_max > 6);
1839 } else if (cfg80211_channel_is_psc(params->channels[i])) {
1840 force_passive = (s_max > 1 || b_max > 3);
1842 force_passive = (s_max > 2 || b_max > 6);
1843 force_passive |= (unsolicited_probe_on_chan &&
1844 (s_max > 1 || b_max > 3));
1846 if (force_passive ||
1847 (!flags && !cfg80211_channel_is_psc(params->channels[i])))
1848 flags |= IWL_UHB_CHAN_CFG_FLAG_FORCE_PASSIVE;
1850 channel_cfg[i].flags |= cpu_to_le32(flags);
1854 static u8 iwl_mvm_scan_umac_chan_flags_v2(struct iwl_mvm *mvm,
1855 struct iwl_mvm_scan_params *params,
1856 struct ieee80211_vif *vif)
1860 flags |= IWL_SCAN_CHANNEL_FLAG_ENABLE_CHAN_ORDER;
1862 if (iwl_mvm_scan_use_ebs(mvm, vif))
1863 flags |= IWL_SCAN_CHANNEL_FLAG_EBS |
1864 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1865 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1867 /* set fragmented ebs for fragmented scan on HB channels */
1868 if (iwl_mvm_is_scan_fragmented(params->hb_type))
1869 flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1874 static u16 iwl_mvm_scan_umac_flags_v2(struct iwl_mvm *mvm,
1875 struct iwl_mvm_scan_params *params,
1876 struct ieee80211_vif *vif,
1881 if (params->n_ssids == 0)
1882 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FORCE_PASSIVE;
1884 if (iwl_mvm_is_scan_fragmented(params->type))
1885 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1;
1887 if (iwl_mvm_is_scan_fragmented(params->hb_type))
1888 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2;
1890 if (params->pass_all)
1891 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PASS_ALL;
1893 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_MATCH;
1895 if (!iwl_mvm_is_regular_scan(params))
1896 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PERIODIC;
1898 if (params->iter_notif ||
1899 mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1900 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_NTFY_ITER_COMPLETE;
1902 if (IWL_MVM_ADWELL_ENABLE)
1903 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_ADAPTIVE_DWELL;
1905 if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1906 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_PREEMPTIVE;
1908 if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
1909 params->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ)
1910 flags |= IWL_UMAC_SCAN_GEN_FLAGS_V2_TRIGGER_UHB_SCAN;
1915 static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1916 struct iwl_mvm_scan_params *params,
1917 struct ieee80211_vif *vif)
1921 if (params->n_ssids == 0)
1922 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1924 if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1925 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1927 if (iwl_mvm_is_scan_fragmented(params->type))
1928 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1930 if (iwl_mvm_is_cdb_supported(mvm) &&
1931 iwl_mvm_is_scan_fragmented(params->hb_type))
1932 flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1934 if (iwl_mvm_rrm_scan_needed(mvm) &&
1935 fw_has_capa(&mvm->fw->ucode_capa,
1936 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1937 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1939 if (params->pass_all)
1940 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1942 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1944 if (!iwl_mvm_is_regular_scan(params))
1945 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1947 if (params->iter_notif)
1948 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1950 #ifdef CONFIG_IWLWIFI_DEBUGFS
1951 if (mvm->scan_iter_notif_enabled)
1952 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1955 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1956 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1958 if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE)
1959 flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1962 * Extended dwell is relevant only for low band to start with, as it is
1963 * being used for social channles only (1, 6, 11), so we can check
1964 * only scan type on low band also for CDB.
1966 if (iwl_mvm_is_regular_scan(params) &&
1967 vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1968 !iwl_mvm_is_scan_fragmented(params->type) &&
1969 !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1970 !iwl_mvm_is_oce_supported(mvm))
1971 flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1973 if (iwl_mvm_is_oce_supported(mvm)) {
1974 if ((params->flags &
1975 NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1976 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1977 /* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1978 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1979 * the same bit, we need to make sure that we use this bit here
1980 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1982 if ((params->flags &
1983 NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1984 !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1985 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1986 if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1987 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1994 iwl_mvm_fill_scan_sched_params(struct iwl_mvm_scan_params *params,
1995 struct iwl_scan_umac_schedule *schedule,
1999 if (WARN_ON(!params->n_scan_plans ||
2000 params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
2003 for (i = 0; i < params->n_scan_plans; i++) {
2004 struct cfg80211_sched_scan_plan *scan_plan =
2005 ¶ms->scan_plans[i];
2007 schedule[i].iter_count = scan_plan->iterations;
2008 schedule[i].interval =
2009 cpu_to_le16(scan_plan->interval);
2013 * If the number of iterations of the last scan plan is set to
2014 * zero, it should run infinitely. However, this is not always the case.
2015 * For example, when regular scan is requested the driver sets one scan
2016 * plan with one iteration.
2018 if (!schedule[params->n_scan_plans - 1].iter_count)
2019 schedule[params->n_scan_plans - 1].iter_count = 0xff;
2021 *delay = cpu_to_le16(params->delay);
2026 static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2027 struct iwl_mvm_scan_params *params,
2030 struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
2031 struct iwl_scan_umac_chan_param *chan_param;
2032 void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
2033 void *sec_part = cmd_data + sizeof(struct iwl_scan_channel_cfg_umac) *
2034 mvm->fw->ucode_capa.n_scan_channels;
2035 struct iwl_scan_req_umac_tail_v2 *tail_v2 =
2036 (struct iwl_scan_req_umac_tail_v2 *)sec_part;
2037 struct iwl_scan_req_umac_tail_v1 *tail_v1;
2038 struct iwl_ssid_ie *direct_scan;
2040 u32 ssid_bitmap = 0;
2041 u8 channel_flags = 0;
2043 struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2045 chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
2047 iwl_mvm_scan_umac_dwell(mvm, cmd, params);
2049 mvm->scan_uid_status[uid] = type;
2051 cmd->uid = cpu_to_le32(uid);
2052 gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
2053 cmd->general_flags = cpu_to_le16(gen_flags);
2054 if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
2055 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
2056 cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
2057 IWL_SCAN_NUM_OF_FRAGS;
2058 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
2059 cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
2060 IWL_SCAN_NUM_OF_FRAGS;
2062 cmd->v8.general_flags2 =
2063 IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
2066 cmd->scan_start_mac_id = scan_vif->id;
2068 if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
2069 cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
2071 if (iwl_mvm_scan_use_ebs(mvm, vif)) {
2072 channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
2073 IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
2074 IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
2076 /* set fragmented ebs for fragmented scan on HB channels */
2077 if (iwl_mvm_is_frag_ebs_supported(mvm)) {
2079 IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
2080 (!iwl_mvm_is_cdb_supported(mvm) &&
2081 gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
2082 channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
2086 chan_param->flags = channel_flags;
2087 chan_param->count = params->n_channels;
2089 ret = iwl_mvm_fill_scan_sched_params(params, tail_v2->schedule,
2092 mvm->scan_uid_status[uid] = 0;
2096 if (iwl_mvm_is_scan_ext_chan_supported(mvm)) {
2097 tail_v2->preq = params->preq;
2098 direct_scan = tail_v2->direct_scan;
2100 tail_v1 = (struct iwl_scan_req_umac_tail_v1 *)sec_part;
2101 iwl_mvm_scan_set_legacy_probe_req(&tail_v1->preq,
2103 direct_scan = tail_v1->direct_scan;
2105 iwl_scan_build_ssids(params, direct_scan, &ssid_bitmap);
2106 iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
2107 params->n_channels, ssid_bitmap,
2113 iwl_mvm_scan_umac_fill_general_p_v10(struct iwl_mvm *mvm,
2114 struct iwl_mvm_scan_params *params,
2115 struct ieee80211_vif *vif,
2116 struct iwl_scan_general_params_v10 *gp,
2119 struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
2121 iwl_mvm_scan_umac_dwell_v10(mvm, gp, params);
2123 gp->flags = cpu_to_le16(gen_flags);
2125 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC1)
2126 gp->num_of_fragments[SCAN_LB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2127 if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_V2_FRAGMENTED_LMAC2)
2128 gp->num_of_fragments[SCAN_HB_LMAC_IDX] = IWL_SCAN_NUM_OF_FRAGS;
2130 gp->scan_start_mac_id = scan_vif->id;
2134 iwl_mvm_scan_umac_fill_probe_p_v3(struct iwl_mvm_scan_params *params,
2135 struct iwl_scan_probe_params_v3 *pp)
2137 pp->preq = params->preq;
2138 pp->ssid_num = params->n_ssids;
2139 iwl_scan_build_ssids(params, pp->direct_scan, NULL);
2143 iwl_mvm_scan_umac_fill_probe_p_v4(struct iwl_mvm_scan_params *params,
2144 struct iwl_scan_probe_params_v4 *pp,
2147 pp->preq = params->preq;
2148 iwl_scan_build_ssids(params, pp->direct_scan, bitmap_ssid);
2152 iwl_mvm_scan_umac_fill_ch_p_v4(struct iwl_mvm *mvm,
2153 struct iwl_mvm_scan_params *params,
2154 struct ieee80211_vif *vif,
2155 struct iwl_scan_channel_params_v4 *cp,
2156 u32 channel_cfg_flags)
2158 cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2159 cp->count = params->n_channels;
2160 cp->num_of_aps_override = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2162 iwl_mvm_umac_scan_cfg_channels_v4(mvm, params->channels, cp,
2169 iwl_mvm_scan_umac_fill_ch_p_v6(struct iwl_mvm *mvm,
2170 struct iwl_mvm_scan_params *params,
2171 struct ieee80211_vif *vif,
2172 struct iwl_scan_channel_params_v6 *cp,
2173 u32 channel_cfg_flags)
2175 cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2176 cp->count = params->n_channels;
2177 cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2178 cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2180 iwl_mvm_umac_scan_cfg_channels_v6(mvm, params->channels, cp,
2186 static int iwl_mvm_scan_umac_v12(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2187 struct iwl_mvm_scan_params *params, int type,
2190 struct iwl_scan_req_umac_v12 *cmd = mvm->scan_cmd;
2191 struct iwl_scan_req_params_v12 *scan_p = &cmd->scan_params;
2195 mvm->scan_uid_status[uid] = type;
2197 cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
2198 cmd->uid = cpu_to_le32(uid);
2200 gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2201 iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
2202 &scan_p->general_params,
2205 ret = iwl_mvm_fill_scan_sched_params(params,
2206 scan_p->periodic_params.schedule,
2207 &scan_p->periodic_params.delay);
2211 iwl_mvm_scan_umac_fill_probe_p_v3(params, &scan_p->probe_params);
2212 iwl_mvm_scan_umac_fill_ch_p_v4(mvm, params, vif,
2213 &scan_p->channel_params, 0);
2218 static int iwl_mvm_scan_umac_v14(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2219 struct iwl_mvm_scan_params *params, int type,
2222 struct iwl_scan_req_umac_v14 *cmd = mvm->scan_cmd;
2223 struct iwl_scan_req_params_v14 *scan_p = &cmd->scan_params;
2224 struct iwl_scan_channel_params_v6 *cp = &scan_p->channel_params;
2225 struct iwl_scan_probe_params_v4 *pb = &scan_p->probe_params;
2228 u32 bitmap_ssid = 0;
2230 mvm->scan_uid_status[uid] = type;
2232 cmd->ooc_priority = cpu_to_le32(iwl_mvm_scan_umac_ooc_priority(params));
2233 cmd->uid = cpu_to_le32(uid);
2235 gen_flags = iwl_mvm_scan_umac_flags_v2(mvm, params, vif, type);
2236 iwl_mvm_scan_umac_fill_general_p_v10(mvm, params, vif,
2237 &scan_p->general_params,
2240 ret = iwl_mvm_fill_scan_sched_params(params,
2241 scan_p->periodic_params.schedule,
2242 &scan_p->periodic_params.delay);
2246 iwl_mvm_scan_umac_fill_probe_p_v4(params, &scan_p->probe_params,
2248 if (!params->scan_6ghz) {
2249 iwl_mvm_scan_umac_fill_ch_p_v6(mvm, params, vif,
2250 &scan_p->channel_params, bitmap_ssid);
2254 cp->flags = iwl_mvm_scan_umac_chan_flags_v2(mvm, params, vif);
2255 cp->n_aps_override[0] = IWL_SCAN_ADWELL_N_APS_GO_FRIENDLY;
2256 cp->n_aps_override[1] = IWL_SCAN_ADWELL_N_APS_SOCIAL_CHS;
2258 ret = iwl_mvm_umac_scan_fill_6g_chan_list(params, pb->short_ssid,
2260 &pb->short_ssid_num,
2265 iwl_mvm_umac_scan_cfg_channels_v6_6g(params,
2272 cp->count = params->n_channels;
2273 if (!params->n_ssids ||
2274 (params->n_ssids == 1 && !params->ssids[0].ssid_len))
2275 cp->flags |= IWL_SCAN_CHANNEL_FLAG_6G_PSC_NO_FILTER;
2280 static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
2282 return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
2285 static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
2287 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2288 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2290 /* This looks a bit arbitrary, but the idea is that if we run
2291 * out of possible simultaneous scans and the userspace is
2292 * trying to run a scan type that is already running, we
2293 * return -EBUSY. But if the userspace wants to start a
2294 * different type of scan, we stop the opposite type to make
2295 * space for the new request. The reason is backwards
2296 * compatibility with old wpa_supplicant that wouldn't stop a
2297 * scheduled scan before starting a normal scan.
2300 /* FW supports only a single periodic scan */
2301 if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
2302 mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
2305 if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
2308 /* Use a switch, even though this is a bitmask, so that more
2309 * than one bits set will fall in default and we will warn.
2312 case IWL_MVM_SCAN_REGULAR:
2313 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2315 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2316 case IWL_MVM_SCAN_SCHED:
2317 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2319 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2320 case IWL_MVM_SCAN_NETDETECT:
2321 /* For non-unified images, there's no need to stop
2322 * anything for net-detect since the firmware is
2323 * restarted anyway. This way, any sched scans that
2324 * were running will be restarted when we resume.
2329 /* If this is a unified image and we ran out of scans,
2330 * we need to stop something. Prefer stopping regular
2331 * scans, because the results are useless at this
2332 * point, and we should be able to keep running
2333 * another scheduled scan while suspended.
2335 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
2336 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
2338 if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
2339 return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
2341 /* Something is wrong if no scan was running but we
2353 #define SCAN_TIMEOUT 20000
2355 void iwl_mvm_scan_timeout_wk(struct work_struct *work)
2357 struct delayed_work *delayed_work = to_delayed_work(work);
2358 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
2359 scan_timeout_dwork);
2361 IWL_ERR(mvm, "regular scan timed out\n");
2363 iwl_force_nmi(mvm->trans);
2366 static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
2367 struct iwl_mvm_scan_params *params,
2368 struct ieee80211_vif *vif)
2370 if (iwl_mvm_is_cdb_supported(mvm)) {
2372 iwl_mvm_get_scan_type_band(mvm, vif,
2375 iwl_mvm_get_scan_type_band(mvm, vif,
2378 params->type = iwl_mvm_get_scan_type(mvm, vif);
2382 struct iwl_scan_umac_handler {
2384 int (*handler)(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2385 struct iwl_mvm_scan_params *params, int type, int uid);
2388 #define IWL_SCAN_UMAC_HANDLER(_ver) { \
2390 .handler = iwl_mvm_scan_umac_v##_ver, \
2393 static const struct iwl_scan_umac_handler iwl_scan_umac_handlers[] = {
2394 /* set the newest version first to shorten the list traverse time */
2395 IWL_SCAN_UMAC_HANDLER(14),
2396 IWL_SCAN_UMAC_HANDLER(12),
2399 static int iwl_mvm_build_scan_cmd(struct iwl_mvm *mvm,
2400 struct ieee80211_vif *vif,
2401 struct iwl_host_cmd *hcmd,
2402 struct iwl_mvm_scan_params *params,
2408 lockdep_assert_held(&mvm->mutex);
2409 memset(mvm->scan_cmd, 0, ksize(mvm->scan_cmd));
2411 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2412 hcmd->id = SCAN_OFFLOAD_REQUEST_CMD;
2414 return iwl_mvm_scan_lmac(mvm, vif, params);
2417 uid = iwl_mvm_scan_uid_by_status(mvm, 0);
2421 hcmd->id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
2423 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2425 IWL_FW_CMD_VER_UNKNOWN);
2427 for (i = 0; i < ARRAY_SIZE(iwl_scan_umac_handlers); i++) {
2428 const struct iwl_scan_umac_handler *ver_handler =
2429 &iwl_scan_umac_handlers[i];
2431 if (ver_handler->version != scan_ver)
2434 return ver_handler->handler(mvm, vif, params, type, uid);
2437 err = iwl_mvm_scan_umac(mvm, vif, params, type, uid);
2444 int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2445 struct cfg80211_scan_request *req,
2446 struct ieee80211_scan_ies *ies)
2448 struct iwl_host_cmd hcmd = {
2449 .len = { iwl_mvm_scan_size(mvm), },
2450 .data = { mvm->scan_cmd, },
2451 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
2453 struct iwl_mvm_scan_params params = {};
2455 struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
2457 lockdep_assert_held(&mvm->mutex);
2459 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2460 IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
2464 ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
2468 /* we should have failed registration if scan_cmd was NULL */
2469 if (WARN_ON(!mvm->scan_cmd))
2472 if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
2475 params.n_ssids = req->n_ssids;
2476 params.flags = req->flags;
2477 params.n_channels = req->n_channels;
2479 params.ssids = req->ssids;
2480 params.channels = req->channels;
2481 params.mac_addr = req->mac_addr;
2482 params.mac_addr_mask = req->mac_addr_mask;
2483 params.no_cck = req->no_cck;
2484 params.pass_all = true;
2485 params.n_match_sets = 0;
2486 params.match_sets = NULL;
2488 params.scan_plans = &scan_plan;
2489 params.n_scan_plans = 1;
2491 params.n_6ghz_params = req->n_6ghz_params;
2492 params.scan_6ghz_params = req->scan_6ghz_params;
2493 params.scan_6ghz = req->scan_6ghz;
2494 iwl_mvm_fill_scan_type(mvm, ¶ms, vif);
2497 params.iter_notif = true;
2499 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
2501 uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, ¶ms,
2502 IWL_MVM_SCAN_REGULAR);
2507 iwl_mvm_pause_tcm(mvm, false);
2509 ret = iwl_mvm_send_cmd(mvm, &hcmd);
2511 /* If the scan failed, it usually means that the FW was unable
2512 * to allocate the time events. Warn on it, but maybe we
2513 * should try to send the command again with different params.
2515 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
2516 iwl_mvm_resume_tcm(mvm);
2517 mvm->scan_uid_status[uid] = 0;
2521 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
2522 mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
2523 mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
2525 schedule_delayed_work(&mvm->scan_timeout_dwork,
2526 msecs_to_jiffies(SCAN_TIMEOUT));
2531 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
2532 struct ieee80211_vif *vif,
2533 struct cfg80211_sched_scan_request *req,
2534 struct ieee80211_scan_ies *ies,
2537 struct iwl_host_cmd hcmd = {
2538 .len = { iwl_mvm_scan_size(mvm), },
2539 .data = { mvm->scan_cmd, },
2540 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
2542 struct iwl_mvm_scan_params params = {};
2545 bool non_psc_included = false;
2547 lockdep_assert_held(&mvm->mutex);
2549 if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
2550 IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
2554 ret = iwl_mvm_check_running_scans(mvm, type);
2558 /* we should have failed registration if scan_cmd was NULL */
2559 if (WARN_ON(!mvm->scan_cmd))
2563 params.n_ssids = req->n_ssids;
2564 params.flags = req->flags;
2565 params.n_channels = req->n_channels;
2566 params.ssids = req->ssids;
2567 params.channels = req->channels;
2568 params.mac_addr = req->mac_addr;
2569 params.mac_addr_mask = req->mac_addr_mask;
2570 params.no_cck = false;
2571 params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
2572 params.n_match_sets = req->n_match_sets;
2573 params.match_sets = req->match_sets;
2574 if (!req->n_scan_plans)
2577 params.n_scan_plans = req->n_scan_plans;
2578 params.scan_plans = req->scan_plans;
2580 iwl_mvm_fill_scan_type(mvm, ¶ms, vif);
2582 /* In theory, LMAC scans can handle a 32-bit delay, but since
2583 * waiting for over 18 hours to start the scan is a bit silly
2584 * and to keep it aligned with UMAC scans (which only support
2585 * 16-bit delays), trim it down to 16-bits.
2587 if (req->delay > U16_MAX) {
2589 "delay value is > 16-bits, set to max possible\n");
2590 params.delay = U16_MAX;
2592 params.delay = req->delay;
2595 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
2599 iwl_mvm_build_scan_probe(mvm, vif, ies, ¶ms);
2601 /* for 6 GHZ band only PSC channels need to be added */
2602 for (i = 0; i < params.n_channels; i++) {
2603 struct ieee80211_channel *channel = params.channels[i];
2605 if (channel->band == NL80211_BAND_6GHZ &&
2606 !cfg80211_channel_is_psc(channel)) {
2607 non_psc_included = true;
2612 if (non_psc_included) {
2613 params.channels = kmemdup(params.channels,
2614 sizeof(params.channels[0]) *
2617 if (!params.channels)
2620 for (i = j = 0; i < params.n_channels; i++) {
2621 if (params.channels[i]->band == NL80211_BAND_6GHZ &&
2622 !cfg80211_channel_is_psc(params.channels[i]))
2624 params.channels[j++] = params.channels[i];
2626 params.n_channels = j;
2629 if (non_psc_included &&
2630 !iwl_mvm_scan_fits(mvm, req->n_ssids, ies, params.n_channels)) {
2631 kfree(params.channels);
2635 uid = iwl_mvm_build_scan_cmd(mvm, vif, &hcmd, ¶ms, type);
2637 if (non_psc_included)
2638 kfree(params.channels);
2642 ret = iwl_mvm_send_cmd(mvm, &hcmd);
2645 "Sched scan request was sent successfully\n");
2646 mvm->scan_status |= type;
2648 /* If the scan failed, it usually means that the FW was unable
2649 * to allocate the time events. Warn on it, but maybe we
2650 * should try to send the command again with different params.
2652 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
2653 mvm->scan_uid_status[uid] = 0;
2654 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2660 void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
2661 struct iwl_rx_cmd_buffer *rxb)
2663 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2664 struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2665 u32 uid = __le32_to_cpu(notif->uid);
2666 bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
2668 if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
2671 /* if the scan is already stopping, we don't need to notify mac80211 */
2672 if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
2673 struct cfg80211_scan_info info = {
2675 .scan_start_tsf = mvm->scan_start,
2678 memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
2679 ieee80211_scan_completed(mvm->hw, &info);
2680 mvm->scan_vif = NULL;
2681 cancel_delayed_work(&mvm->scan_timeout_dwork);
2682 iwl_mvm_resume_tcm(mvm);
2683 } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
2684 ieee80211_sched_scan_stopped(mvm->hw);
2685 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2688 mvm->scan_status &= ~mvm->scan_uid_status[uid];
2690 "Scan completed, uid %u type %u, status %s, EBS status %s\n",
2691 uid, mvm->scan_uid_status[uid],
2692 notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
2693 "completed" : "aborted",
2694 iwl_mvm_ebs_status_str(notif->ebs_status));
2696 "Last line %d, Last iteration %d, Time from last iteration %d\n",
2697 notif->last_schedule, notif->last_iter,
2698 __le32_to_cpu(notif->time_from_last_iter));
2700 if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
2701 notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
2702 mvm->last_ebs_successful = false;
2704 mvm->scan_uid_status[uid] = 0;
2707 void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
2708 struct iwl_rx_cmd_buffer *rxb)
2710 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2711 struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
2713 mvm->scan_start = le64_to_cpu(notif->start_tsf);
2716 "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
2717 notif->status, notif->scanned_channels);
2719 if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
2720 IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
2721 ieee80211_sched_scan_results(mvm->hw);
2722 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
2726 "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
2730 static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
2732 struct iwl_umac_scan_abort cmd = {};
2735 lockdep_assert_held(&mvm->mutex);
2737 /* We should always get a valid index here, because we already
2738 * checked that this type of scan was running in the generic
2741 uid = iwl_mvm_scan_uid_by_status(mvm, type);
2742 if (WARN_ON_ONCE(uid < 0))
2745 cmd.uid = cpu_to_le32(uid);
2747 IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2749 ret = iwl_mvm_send_cmd_pdu(mvm,
2750 iwl_cmd_id(SCAN_ABORT_UMAC,
2751 IWL_ALWAYS_LONG_GROUP, 0),
2752 0, sizeof(cmd), &cmd);
2754 mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
2759 static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
2761 struct iwl_notification_wait wait_scan_done;
2762 static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
2763 SCAN_OFFLOAD_COMPLETE, };
2766 lockdep_assert_held(&mvm->mutex);
2768 iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2770 ARRAY_SIZE(scan_done_notif),
2773 IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2775 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2776 ret = iwl_mvm_umac_scan_abort(mvm, type);
2778 ret = iwl_mvm_lmac_scan_abort(mvm);
2781 IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
2782 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2786 return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
2790 #define IWL_SCAN_REQ_UMAC_HANDLE_SIZE(_ver) { \
2791 case (_ver): return sizeof(struct iwl_scan_req_umac_v##_ver); \
2794 static int iwl_scan_req_umac_get_size(u8 scan_ver)
2797 IWL_SCAN_REQ_UMAC_HANDLE_SIZE(14);
2798 IWL_SCAN_REQ_UMAC_HANDLE_SIZE(12);
2804 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2806 int base_size, tail_size;
2807 u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
2809 IWL_FW_CMD_VER_UNKNOWN);
2811 base_size = iwl_scan_req_umac_get_size(scan_ver);
2816 if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
2817 base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
2818 else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
2819 base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
2820 else if (iwl_mvm_cdb_scan_api(mvm))
2821 base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
2823 base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
2825 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2826 if (iwl_mvm_is_scan_ext_chan_supported(mvm))
2827 tail_size = sizeof(struct iwl_scan_req_umac_tail_v2);
2829 tail_size = sizeof(struct iwl_scan_req_umac_tail_v1);
2832 sizeof(struct iwl_scan_channel_cfg_umac) *
2833 mvm->fw->ucode_capa.n_scan_channels +
2836 return sizeof(struct iwl_scan_req_lmac) +
2837 sizeof(struct iwl_scan_channel_cfg_lmac) *
2838 mvm->fw->ucode_capa.n_scan_channels +
2839 sizeof(struct iwl_scan_probe_req_v1);
2843 * This function is used in nic restart flow, to inform mac80211 about scans
2844 * that was aborted by restart flow or by an assert.
2846 void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
2848 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2851 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
2853 struct cfg80211_scan_info info = {
2857 ieee80211_scan_completed(mvm->hw, &info);
2858 mvm->scan_uid_status[uid] = 0;
2860 uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
2861 if (uid >= 0 && !mvm->fw_restart) {
2862 ieee80211_sched_scan_stopped(mvm->hw);
2863 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2864 mvm->scan_uid_status[uid] = 0;
2866 uid = iwl_mvm_scan_uid_by_status(mvm,
2867 IWL_MVM_SCAN_STOPPING_REGULAR);
2869 mvm->scan_uid_status[uid] = 0;
2871 uid = iwl_mvm_scan_uid_by_status(mvm,
2872 IWL_MVM_SCAN_STOPPING_SCHED);
2874 mvm->scan_uid_status[uid] = 0;
2876 /* We shouldn't have any UIDs still set. Loop over all the
2877 * UIDs to make sure there's nothing left there and warn if
2880 for (i = 0; i < mvm->max_scans; i++) {
2881 if (WARN_ONCE(mvm->scan_uid_status[i],
2882 "UMAC scan UID %d status was not cleaned\n",
2884 mvm->scan_uid_status[i] = 0;
2887 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
2888 struct cfg80211_scan_info info = {
2892 ieee80211_scan_completed(mvm->hw, &info);
2895 /* Sched scan will be restarted by mac80211 in
2896 * restart_hw, so do not report if FW is about to be
2899 if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
2901 ieee80211_sched_scan_stopped(mvm->hw);
2902 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2907 int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2911 if (!(mvm->scan_status & type))
2914 if (iwl_mvm_is_radio_killed(mvm)) {
2919 ret = iwl_mvm_scan_stop_wait(mvm, type);
2921 mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2923 /* Clear the scan status so the next scan requests will
2924 * succeed and mark the scan as stopping, so that the Rx
2925 * handler doesn't do anything, as the scan was stopped from
2928 mvm->scan_status &= ~type;
2930 if (type == IWL_MVM_SCAN_REGULAR) {
2931 cancel_delayed_work(&mvm->scan_timeout_dwork);
2933 struct cfg80211_scan_info info = {
2937 ieee80211_scan_completed(mvm->hw, &info);
2939 } else if (notify) {
2940 ieee80211_sched_scan_stopped(mvm->hw);
2941 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;