linux/drivers/net/wireless/intel/iwlwifi/mvm/scan.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * This file is provided under a dual BSD/GPLv2 license.  When using or
   4 * redistributing this file, you may do so under either license.
   5 *
   6 * GPL LICENSE SUMMARY
   7 *
   8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11 * Copyright(c) 2018 - 2019 Intel Corporation
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of version 2 of the GNU General Public License as
  15 * published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful, but
  18 * WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20 * General Public License for more details.
  21 *
  22 * The full GNU General Public License is included in this distribution
  23 * in the file called COPYING.
  24 *
  25 * Contact Information:
  26 *  Intel Linux Wireless <linuxwifi@intel.com>
  27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28 *
  29 * BSD LICENSE
  30 *
  31 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  34 * Copyright(c) 2018 - 2019 Intel Corporation
  35 * All rights reserved.
  36 *
  37 * Redistribution and use in source and binary forms, with or without
  38 * modification, are permitted provided that the following conditions
  39 * are met:
  40 *
  41 *  * Redistributions of source code must retain the above copyright
  42 *    notice, this list of conditions and the following disclaimer.
  43 *  * Redistributions in binary form must reproduce the above copyright
  44 *    notice, this list of conditions and the following disclaimer in
  45 *    the documentation and/or other materials provided with the
  46 *    distribution.
  47 *  * Neither the name Intel Corporation nor the names of its
  48 *    contributors may be used to endorse or promote products derived
  49 *    from this software without specific prior written permission.
  50 *
  51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62 *
  63 *****************************************************************************/
  64
  65#include <linux/etherdevice.h>
  66#include <net/mac80211.h>
  67
  68#include "mvm.h"
  69#include "fw/api/scan.h"
  70#include "iwl-io.h"
  71
  72#define IWL_DENSE_EBS_SCAN_RATIO 5
  73#define IWL_SPARSE_EBS_SCAN_RATIO 1
  74
  75#define IWL_SCAN_DWELL_ACTIVE           10
  76#define IWL_SCAN_DWELL_PASSIVE          110
  77#define IWL_SCAN_DWELL_FRAGMENTED       44
  78#define IWL_SCAN_DWELL_EXTENDED         90
  79#define IWL_SCAN_NUM_OF_FRAGS           3
  80
  81
  82/* adaptive dwell max budget time [TU] for full scan */
  83#define IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN 300
  84/* adaptive dwell max budget time [TU] for directed scan */
  85#define IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN 100
  86/* adaptive dwell default APs number */
  87#define IWL_SCAN_ADWELL_DEFAULT_N_APS 2
  88/* adaptive dwell default APs number in social channels (1, 6, 11) */
  89#define IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL 10
  90
  91struct iwl_mvm_scan_timing_params {
  92        u32 suspend_time;
  93        u32 max_out_time;
  94};
  95
  96static struct iwl_mvm_scan_timing_params scan_timing[] = {
  97        [IWL_SCAN_TYPE_UNASSOC] = {
  98                .suspend_time = 0,
  99                .max_out_time = 0,
 100        },
 101        [IWL_SCAN_TYPE_WILD] = {
 102                .suspend_time = 30,
 103                .max_out_time = 120,
 104        },
 105        [IWL_SCAN_TYPE_MILD] = {
 106                .suspend_time = 120,
 107                .max_out_time = 120,
 108        },
 109        [IWL_SCAN_TYPE_FRAGMENTED] = {
 110                .suspend_time = 95,
 111                .max_out_time = 44,
 112        },
 113        [IWL_SCAN_TYPE_FAST_BALANCE] = {
 114                .suspend_time = 30,
 115                .max_out_time = 37,
 116        },
 117};
 118
 119struct iwl_mvm_scan_params {
 120        /* For CDB this is low band scan type, for non-CDB - type. */
 121        enum iwl_mvm_scan_type type;
 122        enum iwl_mvm_scan_type hb_type;
 123        u32 n_channels;
 124        u16 delay;
 125        int n_ssids;
 126        struct cfg80211_ssid *ssids;
 127        struct ieee80211_channel **channels;
 128        u32 flags;
 129        u8 *mac_addr;
 130        u8 *mac_addr_mask;
 131        bool no_cck;
 132        bool pass_all;
 133        int n_match_sets;
 134        struct iwl_scan_probe_req preq;
 135        struct cfg80211_match_set *match_sets;
 136        int n_scan_plans;
 137        struct cfg80211_sched_scan_plan *scan_plans;
 138        u32 measurement_dwell;
 139};
 140
 141static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
 142{
 143        struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
 144
 145        if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
 146                return (void *)&cmd->v8.data;
 147
 148        if (iwl_mvm_is_adaptive_dwell_supported(mvm))
 149                return (void *)&cmd->v7.data;
 150
 151        if (iwl_mvm_cdb_scan_api(mvm))
 152                return (void *)&cmd->v6.data;
 153
 154        return (void *)&cmd->v1.data;
 155}
 156
 157static inline struct iwl_scan_umac_chan_param *
 158iwl_mvm_get_scan_req_umac_channel(struct iwl_mvm *mvm)
 159{
 160        struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
 161
 162        if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
 163                return &cmd->v8.channel;
 164
 165        if (iwl_mvm_is_adaptive_dwell_supported(mvm))
 166                return &cmd->v7.channel;
 167
 168        if (iwl_mvm_cdb_scan_api(mvm))
 169                return &cmd->v6.channel;
 170
 171        return &cmd->v1.channel;
 172}
 173
 174static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
 175{
 176        if (mvm->scan_rx_ant != ANT_NONE)
 177                return mvm->scan_rx_ant;
 178        return iwl_mvm_get_valid_rx_ant(mvm);
 179}
 180
 181static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
 182{
 183        u16 rx_chain;
 184        u8 rx_ant;
 185
 186        rx_ant = iwl_mvm_scan_rx_ant(mvm);
 187        rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
 188        rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
 189        rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
 190        rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
 191        return cpu_to_le16(rx_chain);
 192}
 193
 194static __le32 iwl_mvm_scan_rxon_flags(enum nl80211_band band)
 195{
 196        if (band == NL80211_BAND_2GHZ)
 197                return cpu_to_le32(PHY_BAND_24);
 198        else
 199                return cpu_to_le32(PHY_BAND_5);
 200}
 201
 202static inline __le32
 203iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
 204                          bool no_cck)
 205{
 206        u32 tx_ant;
 207
 208        iwl_mvm_toggle_tx_ant(mvm, &mvm->scan_last_antenna_idx);
 209        tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
 210
 211        if (band == NL80211_BAND_2GHZ && !no_cck)
 212                return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
 213                                   tx_ant);
 214        else
 215                return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
 216}
 217
 218static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
 219                                            struct ieee80211_vif *vif)
 220{
 221        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 222        int *global_cnt = data;
 223
 224        if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
 225            mvmvif->phy_ctxt->id < NUM_PHY_CTX)
 226                *global_cnt += 1;
 227}
 228
 229static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
 230{
 231        return mvm->tcm.result.global_load;
 232}
 233
 234static enum iwl_mvm_traffic_load
 235iwl_mvm_get_traffic_load_band(struct iwl_mvm *mvm, enum nl80211_band band)
 236{
 237        return mvm->tcm.result.band_load[band];
 238}
 239
 240struct iwl_is_dcm_with_go_iterator_data {
 241        struct ieee80211_vif *current_vif;
 242        bool is_dcm_with_p2p_go;
 243};
 244
 245static void iwl_mvm_is_dcm_with_go_iterator(void *_data, u8 *mac,
 246                                            struct ieee80211_vif *vif)
 247{
 248        struct iwl_is_dcm_with_go_iterator_data *data = _data;
 249        struct iwl_mvm_vif *other_mvmvif = iwl_mvm_vif_from_mac80211(vif);
 250        struct iwl_mvm_vif *curr_mvmvif =
 251                iwl_mvm_vif_from_mac80211(data->current_vif);
 252
 253        /* exclude the given vif */
 254        if (vif == data->current_vif)
 255                return;
 256
 257        if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
 258            other_mvmvif->phy_ctxt && curr_mvmvif->phy_ctxt &&
 259            other_mvmvif->phy_ctxt->id != curr_mvmvif->phy_ctxt->id)
 260                data->is_dcm_with_p2p_go = true;
 261}
 262
 263static enum
 264iwl_mvm_scan_type _iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
 265                                         struct ieee80211_vif *vif,
 266                                         enum iwl_mvm_traffic_load load,
 267                                         bool low_latency)
 268{
 269        int global_cnt = 0;
 270
 271        ieee80211_iterate_active_interfaces_atomic(mvm->hw,
 272                                            IEEE80211_IFACE_ITER_NORMAL,
 273                                            iwl_mvm_scan_condition_iterator,
 274                                            &global_cnt);
 275        if (!global_cnt)
 276                return IWL_SCAN_TYPE_UNASSOC;
 277
 278        if (fw_has_api(&mvm->fw->ucode_capa,
 279                       IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
 280                if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) &&
 281                    (!vif || vif->type != NL80211_IFTYPE_P2P_DEVICE))
 282                        return IWL_SCAN_TYPE_FRAGMENTED;
 283
 284                /* in case of DCM with GO where BSS DTIM interval < 220msec
 285                 * set all scan requests as fast-balance scan
 286                 * */
 287                if (vif && vif->type == NL80211_IFTYPE_STATION &&
 288                    vif->bss_conf.dtim_period < 220) {
 289                        struct iwl_is_dcm_with_go_iterator_data data = {
 290                                .current_vif = vif,
 291                                .is_dcm_with_p2p_go = false,
 292                        };
 293
 294                        ieee80211_iterate_active_interfaces_atomic(mvm->hw,
 295                                                IEEE80211_IFACE_ITER_NORMAL,
 296                                                iwl_mvm_is_dcm_with_go_iterator,
 297                                                &data);
 298                        if (data.is_dcm_with_p2p_go)
 299                                return IWL_SCAN_TYPE_FAST_BALANCE;
 300                }
 301        }
 302
 303        if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
 304                return IWL_SCAN_TYPE_MILD;
 305
 306        return IWL_SCAN_TYPE_WILD;
 307}
 308
 309static enum
 310iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm,
 311                                        struct ieee80211_vif *vif)
 312{
 313        enum iwl_mvm_traffic_load load;
 314        bool low_latency;
 315
 316        load = iwl_mvm_get_traffic_load(mvm);
 317        low_latency = iwl_mvm_low_latency(mvm);
 318
 319        return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
 320}
 321
 322static enum
 323iwl_mvm_scan_type iwl_mvm_get_scan_type_band(struct iwl_mvm *mvm,
 324                                             struct ieee80211_vif *vif,
 325                                             enum nl80211_band band)
 326{
 327        enum iwl_mvm_traffic_load load;
 328        bool low_latency;
 329
 330        load = iwl_mvm_get_traffic_load_band(mvm, band);
 331        low_latency = iwl_mvm_low_latency_band(mvm, band);
 332
 333        return _iwl_mvm_get_scan_type(mvm, vif, load, low_latency);
 334}
 335
 336static int
 337iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
 338                              struct cfg80211_scan_request *req,
 339                              struct iwl_mvm_scan_params *params)
 340{
 341        u32 duration = scan_timing[params->type].max_out_time;
 342
 343        if (!req->duration)
 344                return 0;
 345
 346        if (iwl_mvm_is_cdb_supported(mvm)) {
 347                u32 hb_time = scan_timing[params->hb_type].max_out_time;
 348
 349                duration = min_t(u32, duration, hb_time);
 350        }
 351
 352        if (req->duration_mandatory && req->duration > duration) {
 353                IWL_DEBUG_SCAN(mvm,
 354                               "Measurement scan - too long dwell %hu (max out time %u)\n",
 355                               req->duration,
 356                               duration);
 357                return -EOPNOTSUPP;
 358        }
 359
 360        return min_t(u32, (u32)req->duration, duration);
 361}
 362
 363static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
 364{
 365        /* require rrm scan whenever the fw supports it */
 366        return fw_has_capa(&mvm->fw->ucode_capa,
 367                           IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
 368}
 369
 370static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
 371{
 372        int max_probe_len;
 373
 374        max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
 375
 376        /* we create the 802.11 header and SSID element */
 377        max_probe_len -= 24 + 2;
 378
 379        /* DS parameter set element is added on 2.4GHZ band if required */
 380        if (iwl_mvm_rrm_scan_needed(mvm))
 381                max_probe_len -= 3;
 382
 383        return max_probe_len;
 384}
 385
 386int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
 387{
 388        int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
 389
 390        /* TODO: [BUG] This function should return the maximum allowed size of
 391         * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
 392         * in the same command. So the correct implementation of this function
 393         * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
 394         * command has only 512 bytes and it would leave us with about 240
 395         * bytes for scan IEs, which is clearly not enough. So meanwhile
 396         * we will report an incorrect value. This may result in a failure to
 397         * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
 398         * functions with -ENOBUFS, if a large enough probe will be provided.
 399         */
 400        return max_ie_len;
 401}
 402
 403void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
 404                                              struct iwl_rx_cmd_buffer *rxb)
 405{
 406        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 407        struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
 408
 409        IWL_DEBUG_SCAN(mvm,
 410                       "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
 411                       notif->status, notif->scanned_channels);
 412
 413        if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
 414                IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
 415                ieee80211_sched_scan_results(mvm->hw);
 416                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
 417        }
 418}
 419
 420void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
 421                                 struct iwl_rx_cmd_buffer *rxb)
 422{
 423        IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
 424        ieee80211_sched_scan_results(mvm->hw);
 425}
 426
 427static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
 428{
 429        switch (status) {
 430        case IWL_SCAN_EBS_SUCCESS:
 431                return "successful";
 432        case IWL_SCAN_EBS_INACTIVE:
 433                return "inactive";
 434        case IWL_SCAN_EBS_FAILED:
 435        case IWL_SCAN_EBS_CHAN_NOT_FOUND:
 436        default:
 437                return "failed";
 438        }
 439}
 440
 441void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
 442                                         struct iwl_rx_cmd_buffer *rxb)
 443{
 444        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 445        struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
 446        bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
 447
 448        /* If this happens, the firmware has mistakenly sent an LMAC
 449         * notification during UMAC scans -- warn and ignore it.
 450         */
 451        if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
 452                                     IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
 453                return;
 454
 455        /* scan status must be locked for proper checking */
 456        lockdep_assert_held(&mvm->mutex);
 457
 458        /* We first check if we were stopping a scan, in which case we
 459         * just clear the stopping flag.  Then we check if it was a
 460         * firmware initiated stop, in which case we need to inform
 461         * mac80211.
 462         * Note that we can have a stopping and a running scan
 463         * simultaneously, but we can't have two different types of
 464         * scans stopping or running at the same time (since LMAC
 465         * doesn't support it).
 466         */
 467
 468        if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
 469                WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
 470
 471                IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
 472                               aborted ? "aborted" : "completed",
 473                               iwl_mvm_ebs_status_str(scan_notif->ebs_status));
 474                IWL_DEBUG_SCAN(mvm,
 475                               "Last line %d, Last iteration %d, Time after last iteration %d\n",
 476                               scan_notif->last_schedule_line,
 477                               scan_notif->last_schedule_iteration,
 478                               __le32_to_cpu(scan_notif->time_after_last_iter));
 479
 480                mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
 481        } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
 482                IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
 483                               aborted ? "aborted" : "completed",
 484                               iwl_mvm_ebs_status_str(scan_notif->ebs_status));
 485
 486                mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
 487        } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
 488                WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
 489
 490                IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
 491                               aborted ? "aborted" : "completed",
 492                               iwl_mvm_ebs_status_str(scan_notif->ebs_status));
 493                IWL_DEBUG_SCAN(mvm,
 494                               "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
 495                               scan_notif->last_schedule_line,
 496                               scan_notif->last_schedule_iteration,
 497                               __le32_to_cpu(scan_notif->time_after_last_iter));
 498
 499                mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
 500                ieee80211_sched_scan_stopped(mvm->hw);
 501                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
 502        } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
 503                struct cfg80211_scan_info info = {
 504                        .aborted = aborted,
 505                };
 506
 507                IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
 508                               aborted ? "aborted" : "completed",
 509                               iwl_mvm_ebs_status_str(scan_notif->ebs_status));
 510
 511                mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
 512                ieee80211_scan_completed(mvm->hw, &info);
 513                iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
 514                cancel_delayed_work(&mvm->scan_timeout_dwork);
 515                iwl_mvm_resume_tcm(mvm);
 516        } else {
 517                IWL_ERR(mvm,
 518                        "got scan complete notification but no scan is running\n");
 519        }
 520
 521        mvm->last_ebs_successful =
 522                        scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
 523                        scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
 524}
 525
 526static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
 527{
 528        int i;
 529
 530        for (i = 0; i < PROBE_OPTION_MAX; i++) {
 531                if (!ssid_list[i].len)
 532                        break;
 533                if (ssid_list[i].len == ssid_len &&
 534                    !memcmp(ssid_list->ssid, ssid, ssid_len))
 535                        return i;
 536        }
 537        return -1;
 538}
 539
 540/* We insert the SSIDs in an inverted order, because the FW will
 541 * invert it back.
 542 */
 543static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
 544                                 struct iwl_ssid_ie *ssids,
 545                                 u32 *ssid_bitmap)
 546{
 547        int i, j;
 548        int index;
 549
 550        /*
 551         * copy SSIDs from match list.
 552         * iwl_config_sched_scan_profiles() uses the order of these ssids to
 553         * config match list.
 554         */
 555        for (i = 0, j = params->n_match_sets - 1;
 556             j >= 0 && i < PROBE_OPTION_MAX;
 557             i++, j--) {
 558                /* skip empty SSID matchsets */
 559                if (!params->match_sets[j].ssid.ssid_len)
 560                        continue;
 561                ssids[i].id = WLAN_EID_SSID;
 562                ssids[i].len = params->match_sets[j].ssid.ssid_len;
 563                memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
 564                       ssids[i].len);
 565        }
 566
 567        /* add SSIDs from scan SSID list */
 568        *ssid_bitmap = 0;
 569        for (j = params->n_ssids - 1;
 570             j >= 0 && i < PROBE_OPTION_MAX;
 571             i++, j--) {
 572                index = iwl_ssid_exist(params->ssids[j].ssid,
 573                                       params->ssids[j].ssid_len,
 574                                       ssids);
 575                if (index < 0) {
 576                        ssids[i].id = WLAN_EID_SSID;
 577                        ssids[i].len = params->ssids[j].ssid_len;
 578                        memcpy(ssids[i].ssid, params->ssids[j].ssid,
 579                               ssids[i].len);
 580                        *ssid_bitmap |= BIT(i);
 581                } else {
 582                        *ssid_bitmap |= BIT(index);
 583                }
 584        }
 585}
 586
 587static int
 588iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
 589                                   struct cfg80211_sched_scan_request *req)
 590{
 591        struct iwl_scan_offload_profile *profile;
 592        struct iwl_scan_offload_profile_cfg *profile_cfg;
 593        struct iwl_scan_offload_blacklist *blacklist;
 594        struct iwl_host_cmd cmd = {
 595                .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
 596                .len[1] = sizeof(*profile_cfg),
 597                .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
 598                .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
 599        };
 600        int blacklist_len;
 601        int i;
 602        int ret;
 603
 604        if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
 605                return -EIO;
 606
 607        if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
 608                blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
 609        else
 610                blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
 611
 612        blacklist = kcalloc(blacklist_len, sizeof(*blacklist), GFP_KERNEL);
 613        if (!blacklist)
 614                return -ENOMEM;
 615
 616        profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
 617        if (!profile_cfg) {
 618                ret = -ENOMEM;
 619                goto free_blacklist;
 620        }
 621
 622        cmd.data[0] = blacklist;
 623        cmd.len[0] = sizeof(*blacklist) * blacklist_len;
 624        cmd.data[1] = profile_cfg;
 625
 626        /* No blacklist configuration */
 627
 628        profile_cfg->num_profiles = req->n_match_sets;
 629        profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
 630        profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
 631        profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
 632        if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
 633                profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
 634
 635        for (i = 0; i < req->n_match_sets; i++) {
 636                profile = &profile_cfg->profiles[i];
 637                profile->ssid_index = i;
 638                /* Support any cipher and auth algorithm */
 639                profile->unicast_cipher = 0xff;
 640                profile->auth_alg = 0xff;
 641                profile->network_type = IWL_NETWORK_TYPE_ANY;
 642                profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
 643                profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
 644        }
 645
 646        IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
 647
 648        ret = iwl_mvm_send_cmd(mvm, &cmd);
 649        kfree(profile_cfg);
 650free_blacklist:
 651        kfree(blacklist);
 652
 653        return ret;
 654}
 655
 656static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
 657                                  struct cfg80211_sched_scan_request *req)
 658{
 659        if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
 660                IWL_DEBUG_SCAN(mvm,
 661                               "Sending scheduled scan with filtering, n_match_sets %d\n",
 662                               req->n_match_sets);
 663                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
 664                return false;
 665        }
 666
 667        IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
 668
 669        mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
 670        return true;
 671}
 672
 673static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
 674{
 675        int ret;
 676        struct iwl_host_cmd cmd = {
 677                .id = SCAN_OFFLOAD_ABORT_CMD,
 678        };
 679        u32 status = CAN_ABORT_STATUS;
 680
 681        ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
 682        if (ret)
 683                return ret;
 684
 685        if (status != CAN_ABORT_STATUS) {
 686                /*
 687                 * The scan abort will return 1 for success or
 688                 * 2 for "failure".  A failure condition can be
 689                 * due to simply not being in an active scan which
 690                 * can occur if we send the scan abort before the
 691                 * microcode has notified us that a scan is completed.
 692                 */
 693                IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
 694                ret = -ENOENT;
 695        }
 696
 697        return ret;
 698}
 699
 700static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
 701                                     struct iwl_scan_req_tx_cmd *tx_cmd,
 702                                     bool no_cck)
 703{
 704        tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
 705                                         TX_CMD_FLG_BT_DIS);
 706        tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
 707                                                           NL80211_BAND_2GHZ,
 708                                                           no_cck);
 709        tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
 710
 711        tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
 712                                         TX_CMD_FLG_BT_DIS);
 713        tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
 714                                                           NL80211_BAND_5GHZ,
 715                                                           no_cck);
 716        tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
 717}
 718
 719static void
 720iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
 721                               struct ieee80211_channel **channels,
 722                               int n_channels, u32 ssid_bitmap,
 723                               struct iwl_scan_req_lmac *cmd)
 724{
 725        struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
 726        int i;
 727
 728        for (i = 0; i < n_channels; i++) {
 729                channel_cfg[i].channel_num =
 730                        cpu_to_le16(channels[i]->hw_value);
 731                channel_cfg[i].iter_count = cpu_to_le16(1);
 732                channel_cfg[i].iter_interval = 0;
 733                channel_cfg[i].flags =
 734                        cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
 735                                    ssid_bitmap);
 736        }
 737}
 738
 739static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
 740                                           size_t len, u8 *const pos)
 741{
 742        static const u8 before_ds_params[] = {
 743                        WLAN_EID_SSID,
 744                        WLAN_EID_SUPP_RATES,
 745                        WLAN_EID_REQUEST,
 746                        WLAN_EID_EXT_SUPP_RATES,
 747        };
 748        size_t offs;
 749        u8 *newpos = pos;
 750
 751        if (!iwl_mvm_rrm_scan_needed(mvm)) {
 752                memcpy(newpos, ies, len);
 753                return newpos + len;
 754        }
 755
 756        offs = ieee80211_ie_split(ies, len,
 757                                  before_ds_params,
 758                                  ARRAY_SIZE(before_ds_params),
 759                                  0);
 760
 761        memcpy(newpos, ies, offs);
 762        newpos += offs;
 763
 764        /* Add a placeholder for DS Parameter Set element */
 765        *newpos++ = WLAN_EID_DS_PARAMS;
 766        *newpos++ = 1;
 767        *newpos++ = 0;
 768
 769        memcpy(newpos, ies + offs, len - offs);
 770        newpos += len - offs;
 771
 772        return newpos;
 773}
 774
 775#define WFA_TPC_IE_LEN  9
 776
 777static void iwl_mvm_add_tpc_report_ie(u8 *pos)
 778{
 779        pos[0] = WLAN_EID_VENDOR_SPECIFIC;
 780        pos[1] = WFA_TPC_IE_LEN - 2;
 781        pos[2] = (WLAN_OUI_MICROSOFT >> 16) & 0xff;
 782        pos[3] = (WLAN_OUI_MICROSOFT >> 8) & 0xff;
 783        pos[4] = WLAN_OUI_MICROSOFT & 0xff;
 784        pos[5] = WLAN_OUI_TYPE_MICROSOFT_TPC;
 785        pos[6] = 0;
 786        /* pos[7] - tx power will be inserted by the FW */
 787        pos[7] = 0;
 788        pos[8] = 0;
 789}
 790
 791static void
 792iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 793                         struct ieee80211_scan_ies *ies,
 794                         struct iwl_mvm_scan_params *params)
 795{
 796        struct ieee80211_mgmt *frame = (void *)params->preq.buf;
 797        u8 *pos, *newpos;
 798        const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
 799                params->mac_addr : NULL;
 800
 801        /*
 802         * Unfortunately, right now the offload scan doesn't support randomising
 803         * within the firmware, so until the firmware API is ready we implement
 804         * it in the driver. This means that the scan iterations won't really be
 805         * random, only when it's restarted, but at least that helps a bit.
 806         */
 807        if (mac_addr)
 808                get_random_mask_addr(frame->sa, mac_addr,
 809                                     params->mac_addr_mask);
 810        else
 811                memcpy(frame->sa, vif->addr, ETH_ALEN);
 812
 813        frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
 814        eth_broadcast_addr(frame->da);
 815        eth_broadcast_addr(frame->bssid);
 816        frame->seq_ctrl = 0;
 817
 818        pos = frame->u.probe_req.variable;
 819        *pos++ = WLAN_EID_SSID;
 820        *pos++ = 0;
 821
 822        params->preq.mac_header.offset = 0;
 823        params->preq.mac_header.len = cpu_to_le16(24 + 2);
 824
 825        /* Insert ds parameter set element on 2.4 GHz band */
 826        newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
 827                                                 ies->ies[NL80211_BAND_2GHZ],
 828                                                 ies->len[NL80211_BAND_2GHZ],
 829                                                 pos);
 830        params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
 831        params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
 832        pos = newpos;
 833
 834        memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
 835               ies->len[NL80211_BAND_5GHZ]);
 836        params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
 837        params->preq.band_data[1].len =
 838                cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
 839        pos += ies->len[NL80211_BAND_5GHZ];
 840
 841        memcpy(pos, ies->common_ies, ies->common_ie_len);
 842        params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
 843
 844        if (iwl_mvm_rrm_scan_needed(mvm) &&
 845            !fw_has_capa(&mvm->fw->ucode_capa,
 846                         IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)) {
 847                iwl_mvm_add_tpc_report_ie(pos + ies->common_ie_len);
 848                params->preq.common_data.len = cpu_to_le16(ies->common_ie_len +
 849                                                           WFA_TPC_IE_LEN);
 850        } else {
 851                params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
 852        }
 853}
 854
 855static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
 856                                    struct iwl_scan_req_lmac *cmd,
 857                                    struct iwl_mvm_scan_params *params)
 858{
 859        cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
 860        cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
 861        cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
 862        cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
 863        cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
 864        cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
 865        cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
 866}
 867
 868static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
 869                                     struct ieee80211_scan_ies *ies,
 870                                     int n_channels)
 871{
 872        return ((n_ssids <= PROBE_OPTION_MAX) &&
 873                (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
 874                (ies->common_ie_len +
 875                 ies->len[NL80211_BAND_2GHZ] +
 876                 ies->len[NL80211_BAND_5GHZ] <=
 877                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
 878}
 879
 880static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
 881                                        struct ieee80211_vif *vif)
 882{
 883        const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
 884        bool low_latency;
 885
 886        if (iwl_mvm_is_cdb_supported(mvm))
 887                low_latency = iwl_mvm_low_latency_band(mvm, NL80211_BAND_5GHZ);
 888        else
 889                low_latency = iwl_mvm_low_latency(mvm);
 890
 891        /* We can only use EBS if:
 892         *      1. the feature is supported;
 893         *      2. the last EBS was successful;
 894         *      3. if only single scan, the single scan EBS API is supported;
 895         *      4. it's not a p2p find operation.
 896         *      5. we are not in low latency mode,
 897         *         or if fragmented ebs is supported by the FW
 898         */
 899        return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
 900                mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
 901                vif->type != NL80211_IFTYPE_P2P_DEVICE &&
 902                (!low_latency || iwl_mvm_is_frag_ebs_supported(mvm)));
 903}
 904
 905static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
 906{
 907        return params->n_scan_plans == 1 &&
 908                params->scan_plans[0].iterations == 1;
 909}
 910
 911static bool iwl_mvm_is_scan_fragmented(enum iwl_mvm_scan_type type)
 912{
 913        return (type == IWL_SCAN_TYPE_FRAGMENTED ||
 914                type == IWL_SCAN_TYPE_FAST_BALANCE);
 915}
 916
 917static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
 918                                   struct iwl_mvm_scan_params *params,
 919                                   struct ieee80211_vif *vif)
 920{
 921        int flags = 0;
 922
 923        if (params->n_ssids == 0)
 924                flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
 925
 926        if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
 927                flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
 928
 929        if (iwl_mvm_is_scan_fragmented(params->type))
 930                flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
 931
 932        if (iwl_mvm_rrm_scan_needed(mvm) &&
 933            fw_has_capa(&mvm->fw->ucode_capa,
 934                        IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
 935                flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
 936
 937        if (params->pass_all)
 938                flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
 939        else
 940                flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
 941
 942#ifdef CONFIG_IWLWIFI_DEBUGFS
 943        if (mvm->scan_iter_notif_enabled)
 944                flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
 945#endif
 946
 947        if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
 948                flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
 949
 950        if (iwl_mvm_is_regular_scan(params) &&
 951            vif->type != NL80211_IFTYPE_P2P_DEVICE &&
 952            !iwl_mvm_is_scan_fragmented(params->type))
 953                flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
 954
 955        return flags;
 956}
 957
 958static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 959                             struct iwl_mvm_scan_params *params)
 960{
 961        struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
 962        struct iwl_scan_probe_req *preq =
 963                (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
 964                         mvm->fw->ucode_capa.n_scan_channels);
 965        u32 ssid_bitmap = 0;
 966        int i;
 967
 968        lockdep_assert_held(&mvm->mutex);
 969
 970        memset(cmd, 0, ksize(cmd));
 971
 972        if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
 973                return -EINVAL;
 974
 975        iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
 976
 977        cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
 978        cmd->iter_num = cpu_to_le32(1);
 979        cmd->n_channels = (u8)params->n_channels;
 980
 981        cmd->delay = cpu_to_le32(params->delay);
 982
 983        cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
 984                                                              vif));
 985
 986        cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
 987        cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
 988                                        MAC_FILTER_IN_BEACON);
 989        iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
 990        iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
 991
 992        /* this API uses bits 1-20 instead of 0-19 */
 993        ssid_bitmap <<= 1;
 994
 995        for (i = 0; i < params->n_scan_plans; i++) {
 996                struct cfg80211_sched_scan_plan *scan_plan =
 997                        &params->scan_plans[i];
 998
 999                cmd->schedule[i].delay =
1000                        cpu_to_le16(scan_plan->interval);
1001                cmd->schedule[i].iterations = scan_plan->iterations;
1002                cmd->schedule[i].full_scan_mul = 1;
1003        }
1004
1005        /*
1006         * If the number of iterations of the last scan plan is set to
1007         * zero, it should run infinitely. However, this is not always the case.
1008         * For example, when regular scan is requested the driver sets one scan
1009         * plan with one iteration.
1010         */
1011        if (!cmd->schedule[i - 1].iterations)
1012                cmd->schedule[i - 1].iterations = 0xff;
1013
1014        if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1015                cmd->channel_opt[0].flags =
1016                        cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1017                                    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1018                                    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1019                cmd->channel_opt[0].non_ebs_ratio =
1020                        cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1021                cmd->channel_opt[1].flags =
1022                        cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1023                                    IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1024                                    IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1025                cmd->channel_opt[1].non_ebs_ratio =
1026                        cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1027        }
1028
1029        iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
1030                                       params->n_channels, ssid_bitmap, cmd);
1031
1032        *preq = params->preq;
1033
1034        return 0;
1035}
1036
1037static int rate_to_scan_rate_flag(unsigned int rate)
1038{
1039        static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1040                [IWL_RATE_1M_INDEX]     = SCAN_CONFIG_RATE_1M,
1041                [IWL_RATE_2M_INDEX]     = SCAN_CONFIG_RATE_2M,
1042                [IWL_RATE_5M_INDEX]     = SCAN_CONFIG_RATE_5M,
1043                [IWL_RATE_11M_INDEX]    = SCAN_CONFIG_RATE_11M,
1044                [IWL_RATE_6M_INDEX]     = SCAN_CONFIG_RATE_6M,
1045                [IWL_RATE_9M_INDEX]     = SCAN_CONFIG_RATE_9M,
1046                [IWL_RATE_12M_INDEX]    = SCAN_CONFIG_RATE_12M,
1047                [IWL_RATE_18M_INDEX]    = SCAN_CONFIG_RATE_18M,
1048                [IWL_RATE_24M_INDEX]    = SCAN_CONFIG_RATE_24M,
1049                [IWL_RATE_36M_INDEX]    = SCAN_CONFIG_RATE_36M,
1050                [IWL_RATE_48M_INDEX]    = SCAN_CONFIG_RATE_48M,
1051                [IWL_RATE_54M_INDEX]    = SCAN_CONFIG_RATE_54M,
1052        };
1053
1054        return rate_to_scan_rate[rate];
1055}
1056
1057static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1058{
1059        struct ieee80211_supported_band *band;
1060        unsigned int rates = 0;
1061        int i;
1062
1063        band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1064        for (i = 0; i < band->n_bitrates; i++)
1065                rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1066        band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1067        for (i = 0; i < band->n_bitrates; i++)
1068                rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1069
1070        /* Set both basic rates and supported rates */
1071        rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1072
1073        return cpu_to_le32(rates);
1074}
1075
1076static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
1077                                    struct iwl_scan_dwell *dwell)
1078{
1079        dwell->active = IWL_SCAN_DWELL_ACTIVE;
1080        dwell->passive = IWL_SCAN_DWELL_PASSIVE;
1081        dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
1082        dwell->extended = IWL_SCAN_DWELL_EXTENDED;
1083}
1084
1085static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels,
1086                                  u32 max_channels)
1087{
1088        struct ieee80211_supported_band *band;
1089        int i, j = 0;
1090
1091        band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
1092        for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1093                channels[j] = band->channels[i].hw_value;
1094        band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
1095        for (i = 0; i < band->n_channels && j < max_channels; i++, j++)
1096                channels[j] = band->channels[i].hw_value;
1097}
1098
1099static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
1100                                        u32 flags, u8 channel_flags,
1101                                        u32 max_channels)
1102{
1103        enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, NULL);
1104        struct iwl_scan_config_v1 *cfg = config;
1105
1106        cfg->flags = cpu_to_le32(flags);
1107        cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1108        cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1109        cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1110        cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
1111        cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
1112
1113        iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1114
1115        memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1116
1117        cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1118        cfg->channel_flags = channel_flags;
1119
1120        iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1121}
1122
1123static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
1124                                     u32 flags, u8 channel_flags,
1125                                     u32 max_channels)
1126{
1127        struct iwl_scan_config *cfg = config;
1128
1129        cfg->flags = cpu_to_le32(flags);
1130        cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1131        cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1132        cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1133
1134        if (iwl_mvm_is_cdb_supported(mvm)) {
1135                enum iwl_mvm_scan_type lb_type, hb_type;
1136
1137                lb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1138                                                     NL80211_BAND_2GHZ);
1139                hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1140                                                     NL80211_BAND_5GHZ);
1141
1142                cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1143                        cpu_to_le32(scan_timing[lb_type].max_out_time);
1144                cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1145                        cpu_to_le32(scan_timing[lb_type].suspend_time);
1146
1147                cfg->out_of_channel_time[SCAN_HB_LMAC_IDX] =
1148                        cpu_to_le32(scan_timing[hb_type].max_out_time);
1149                cfg->suspend_time[SCAN_HB_LMAC_IDX] =
1150                        cpu_to_le32(scan_timing[hb_type].suspend_time);
1151        } else {
1152                enum iwl_mvm_scan_type type =
1153                        iwl_mvm_get_scan_type(mvm, NULL);
1154
1155                cfg->out_of_channel_time[SCAN_LB_LMAC_IDX] =
1156                        cpu_to_le32(scan_timing[type].max_out_time);
1157                cfg->suspend_time[SCAN_LB_LMAC_IDX] =
1158                        cpu_to_le32(scan_timing[type].suspend_time);
1159        }
1160
1161        iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
1162
1163        memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1164
1165        cfg->bcast_sta_id = mvm->aux_sta.sta_id;
1166        cfg->channel_flags = channel_flags;
1167
1168        iwl_mvm_fill_channels(mvm, cfg->channel_array, max_channels);
1169}
1170
1171int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1172{
1173        void *cfg;
1174        int ret, cmd_size;
1175        struct iwl_host_cmd cmd = {
1176                .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
1177        };
1178        enum iwl_mvm_scan_type type;
1179        enum iwl_mvm_scan_type hb_type = IWL_SCAN_TYPE_NOT_SET;
1180        int num_channels =
1181                mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
1182                mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
1183        u32 flags;
1184        u8 channel_flags;
1185
1186        if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1187                num_channels = mvm->fw->ucode_capa.n_scan_channels;
1188
1189        if (iwl_mvm_is_cdb_supported(mvm)) {
1190                type = iwl_mvm_get_scan_type_band(mvm, NULL,
1191                                                  NL80211_BAND_2GHZ);
1192                hb_type = iwl_mvm_get_scan_type_band(mvm, NULL,
1193                                                     NL80211_BAND_5GHZ);
1194                if (type == mvm->scan_type && hb_type == mvm->hb_scan_type)
1195                        return 0;
1196        } else {
1197                type = iwl_mvm_get_scan_type(mvm, NULL);
1198                if (type == mvm->scan_type)
1199                        return 0;
1200        }
1201
1202        if (iwl_mvm_cdb_scan_api(mvm))
1203                cmd_size = sizeof(struct iwl_scan_config);
1204        else
1205                cmd_size = sizeof(struct iwl_scan_config_v1);
1206        cmd_size += mvm->fw->ucode_capa.n_scan_channels;
1207
1208        cfg = kzalloc(cmd_size, GFP_KERNEL);
1209        if (!cfg)
1210                return -ENOMEM;
1211
1212        flags = SCAN_CONFIG_FLAG_ACTIVATE |
1213                 SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1214                 SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1215                 SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1216                 SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
1217                 SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1218                 SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1219                 SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1220                 SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
1221                 SCAN_CONFIG_N_CHANNELS(num_channels) |
1222                 (iwl_mvm_is_scan_fragmented(type) ?
1223                  SCAN_CONFIG_FLAG_SET_FRAGMENTED :
1224                  SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
1225
1226        channel_flags = IWL_CHANNEL_FLAG_EBS |
1227                        IWL_CHANNEL_FLAG_ACCURATE_EBS |
1228                        IWL_CHANNEL_FLAG_EBS_ADD |
1229                        IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1230
1231        /*
1232         * Check for fragmented scan on LMAC2 - high band.
1233         * LMAC1 - low band is checked above.
1234         */
1235        if (iwl_mvm_cdb_scan_api(mvm)) {
1236                if (iwl_mvm_is_cdb_supported(mvm))
1237                        flags |= (iwl_mvm_is_scan_fragmented(hb_type)) ?
1238                                 SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
1239                                 SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
1240                iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags,
1241                                         num_channels);
1242        } else {
1243                iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags,
1244                                            num_channels);
1245        }
1246
1247        cmd.data[0] = cfg;
1248        cmd.len[0] = cmd_size;
1249        cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1250
1251        IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1252
1253        ret = iwl_mvm_send_cmd(mvm, &cmd);
1254        if (!ret) {
1255                mvm->scan_type = type;
1256                mvm->hb_scan_type = hb_type;
1257        }
1258
1259        kfree(cfg);
1260        return ret;
1261}
1262
1263static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
1264{
1265        int i;
1266
1267        for (i = 0; i < mvm->max_scans; i++)
1268                if (mvm->scan_uid_status[i] == status)
1269                        return i;
1270
1271        return -ENOENT;
1272}
1273
1274static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
1275                                    struct iwl_scan_req_umac *cmd,
1276                                    struct iwl_mvm_scan_params *params)
1277{
1278        struct iwl_mvm_scan_timing_params *timing, *hb_timing;
1279        u8 active_dwell, passive_dwell;
1280
1281        timing = &scan_timing[params->type];
1282        active_dwell = params->measurement_dwell ?
1283                params->measurement_dwell : IWL_SCAN_DWELL_ACTIVE;
1284        passive_dwell = params->measurement_dwell ?
1285                params->measurement_dwell : IWL_SCAN_DWELL_PASSIVE;
1286
1287        if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
1288                cmd->v7.adwell_default_n_aps_social =
1289                        IWL_SCAN_ADWELL_DEFAULT_N_APS_SOCIAL;
1290                cmd->v7.adwell_default_n_aps =
1291                        IWL_SCAN_ADWELL_DEFAULT_N_APS;
1292
1293                /* if custom max budget was configured with debugfs */
1294                if (IWL_MVM_ADWELL_MAX_BUDGET)
1295                        cmd->v7.adwell_max_budget =
1296                                cpu_to_le16(IWL_MVM_ADWELL_MAX_BUDGET);
1297                else if (params->ssids && params->ssids[0].ssid_len)
1298                        cmd->v7.adwell_max_budget =
1299                                cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_DIRECTED_SCAN);
1300                else
1301                        cmd->v7.adwell_max_budget =
1302                                cpu_to_le16(IWL_SCAN_ADWELL_MAX_BUDGET_FULL_SCAN);
1303
1304                cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1305                cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
1306                        cpu_to_le32(timing->max_out_time);
1307                cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
1308                        cpu_to_le32(timing->suspend_time);
1309
1310                if (iwl_mvm_is_cdb_supported(mvm)) {
1311                        hb_timing = &scan_timing[params->hb_type];
1312
1313                        cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
1314                                cpu_to_le32(hb_timing->max_out_time);
1315                        cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
1316                                cpu_to_le32(hb_timing->suspend_time);
1317                }
1318
1319                if (!iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1320                        cmd->v7.active_dwell = active_dwell;
1321                        cmd->v7.passive_dwell = passive_dwell;
1322                        cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1323                } else {
1324                        cmd->v8.active_dwell[SCAN_LB_LMAC_IDX] = active_dwell;
1325                        cmd->v8.passive_dwell[SCAN_LB_LMAC_IDX] = passive_dwell;
1326                        if (iwl_mvm_is_cdb_supported(mvm)) {
1327                                cmd->v8.active_dwell[SCAN_HB_LMAC_IDX] =
1328                                        active_dwell;
1329                                cmd->v8.passive_dwell[SCAN_HB_LMAC_IDX] =
1330                                        passive_dwell;
1331                        }
1332                }
1333        } else {
1334                cmd->v1.extended_dwell = params->measurement_dwell ?
1335                        params->measurement_dwell : IWL_SCAN_DWELL_EXTENDED;
1336                cmd->v1.active_dwell = active_dwell;
1337                cmd->v1.passive_dwell = passive_dwell;
1338                cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
1339
1340                if (iwl_mvm_is_cdb_supported(mvm)) {
1341                        hb_timing = &scan_timing[params->hb_type];
1342
1343                        cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
1344                                        cpu_to_le32(hb_timing->max_out_time);
1345                        cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
1346                                        cpu_to_le32(hb_timing->suspend_time);
1347                }
1348
1349                if (iwl_mvm_cdb_scan_api(mvm)) {
1350                        cmd->v6.scan_priority =
1351                                cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1352                        cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
1353                                cpu_to_le32(timing->max_out_time);
1354                        cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
1355                                cpu_to_le32(timing->suspend_time);
1356                } else {
1357                        cmd->v1.scan_priority =
1358                                cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1359                        cmd->v1.max_out_time =
1360                                cpu_to_le32(timing->max_out_time);
1361                        cmd->v1.suspend_time =
1362                                cpu_to_le32(timing->suspend_time);
1363                }
1364        }
1365
1366        if (iwl_mvm_is_regular_scan(params))
1367                cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
1368        else
1369                cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
1370}
1371
1372static void
1373iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1374                               struct ieee80211_channel **channels,
1375                               int n_channels, u32 ssid_bitmap,
1376                               struct iwl_scan_channel_cfg_umac *channel_cfg)
1377{
1378        int i;
1379
1380        for (i = 0; i < n_channels; i++) {
1381                channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1382                channel_cfg[i].channel_num = channels[i]->hw_value;
1383                channel_cfg[i].iter_count = 1;
1384                channel_cfg[i].iter_interval = 0;
1385        }
1386}
1387
1388static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
1389                                   struct iwl_mvm_scan_params *params,
1390                                   struct ieee80211_vif *vif)
1391{
1392        u16 flags = 0;
1393
1394        if (params->n_ssids == 0)
1395                flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1396
1397        if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
1398                flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1399
1400        if (iwl_mvm_is_scan_fragmented(params->type))
1401                flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1402
1403        if (iwl_mvm_is_cdb_supported(mvm) &&
1404            iwl_mvm_is_scan_fragmented(params->hb_type))
1405                flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
1406
1407        if (iwl_mvm_rrm_scan_needed(mvm) &&
1408            fw_has_capa(&mvm->fw->ucode_capa,
1409                        IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
1410                flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1411
1412        if (params->pass_all)
1413                flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1414        else
1415                flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1416
1417        if (!iwl_mvm_is_regular_scan(params))
1418                flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1419
1420        if (params->measurement_dwell)
1421                flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1422
1423#ifdef CONFIG_IWLWIFI_DEBUGFS
1424        if (mvm->scan_iter_notif_enabled)
1425                flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1426#endif
1427
1428        if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
1429                flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
1430
1431        if (iwl_mvm_is_adaptive_dwell_supported(mvm) && IWL_MVM_ADWELL_ENABLE &&
1432            vif->type != NL80211_IFTYPE_P2P_DEVICE)
1433                flags |= IWL_UMAC_SCAN_GEN_FLAGS_ADAPTIVE_DWELL;
1434
1435        /*
1436         * Extended dwell is relevant only for low band to start with, as it is
1437         * being used for social channles only (1, 6, 11), so we can check
1438         * only scan type on low band also for CDB.
1439         */
1440        if (iwl_mvm_is_regular_scan(params) &&
1441            vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1442            !iwl_mvm_is_scan_fragmented(params->type) &&
1443            !iwl_mvm_is_adaptive_dwell_supported(mvm) &&
1444            !iwl_mvm_is_oce_supported(mvm))
1445                flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
1446
1447        if (iwl_mvm_is_oce_supported(mvm)) {
1448                if ((params->flags &
1449                     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE))
1450                        flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_HIGH_TX_RATE;
1451                /* Since IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL and
1452                 * NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION shares
1453                 * the same bit, we need to make sure that we use this bit here
1454                 * only when IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL cannot be
1455                 * used. */
1456                if ((params->flags &
1457                     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
1458                     !WARN_ON_ONCE(!iwl_mvm_is_adaptive_dwell_supported(mvm)))
1459                        flags |= IWL_UMAC_SCAN_GEN_FLAGS_PROB_REQ_DEFER_SUPP;
1460                if ((params->flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME))
1461                        flags |= IWL_UMAC_SCAN_GEN_FLAGS_MAX_CHNL_TIME;
1462        }
1463
1464        return flags;
1465}
1466
1467static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1468                             struct iwl_mvm_scan_params *params,
1469                             int type)
1470{
1471        struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1472        struct iwl_scan_umac_chan_param *chan_param;
1473        void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
1474        struct iwl_scan_req_umac_tail *sec_part = cmd_data +
1475                sizeof(struct iwl_scan_channel_cfg_umac) *
1476                        mvm->fw->ucode_capa.n_scan_channels;
1477        int uid, i;
1478        u32 ssid_bitmap = 0;
1479        u8 channel_flags = 0;
1480        u16 gen_flags;
1481        struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
1482
1483        chan_param = iwl_mvm_get_scan_req_umac_channel(mvm);
1484
1485        lockdep_assert_held(&mvm->mutex);
1486
1487        if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
1488                return -EINVAL;
1489
1490        uid = iwl_mvm_scan_uid_by_status(mvm, 0);
1491        if (uid < 0)
1492                return uid;
1493
1494        memset(cmd, 0, ksize(cmd));
1495
1496        iwl_mvm_scan_umac_dwell(mvm, cmd, params);
1497
1498        mvm->scan_uid_status[uid] = type;
1499
1500        cmd->uid = cpu_to_le32(uid);
1501        gen_flags = iwl_mvm_scan_umac_flags(mvm, params, vif);
1502        cmd->general_flags = cpu_to_le16(gen_flags);
1503        if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm)) {
1504                if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED)
1505                        cmd->v8.num_of_fragments[SCAN_LB_LMAC_IDX] =
1506                                                        IWL_SCAN_NUM_OF_FRAGS;
1507                if (gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED)
1508                        cmd->v8.num_of_fragments[SCAN_HB_LMAC_IDX] =
1509                                                        IWL_SCAN_NUM_OF_FRAGS;
1510
1511                cmd->v8.general_flags2 =
1512                        IWL_UMAC_SCAN_GEN_FLAGS2_ALLOW_CHNL_REORDER;
1513        }
1514
1515        cmd->scan_start_mac_id = scan_vif->id;
1516
1517        if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
1518                cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1519
1520        if (iwl_mvm_scan_use_ebs(mvm, vif)) {
1521                channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1522                                IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1523                                IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1524
1525                /* set fragmented ebs for fragmented scan on HB channels */
1526                if (iwl_mvm_is_frag_ebs_supported(mvm)) {
1527                        if (gen_flags &
1528                            IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED ||
1529                            (!iwl_mvm_is_cdb_supported(mvm) &&
1530                             gen_flags & IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED))
1531                                channel_flags |= IWL_SCAN_CHANNEL_FLAG_EBS_FRAG;
1532                }
1533        }
1534
1535        chan_param->flags = channel_flags;
1536        chan_param->count = params->n_channels;
1537
1538        iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
1539
1540        iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
1541                                       params->n_channels, ssid_bitmap,
1542                                       cmd_data);
1543
1544        for (i = 0; i < params->n_scan_plans; i++) {
1545                struct cfg80211_sched_scan_plan *scan_plan =
1546                        &params->scan_plans[i];
1547
1548                sec_part->schedule[i].iter_count = scan_plan->iterations;
1549                sec_part->schedule[i].interval =
1550                        cpu_to_le16(scan_plan->interval);
1551        }
1552
1553        /*
1554         * If the number of iterations of the last scan plan is set to
1555         * zero, it should run infinitely. However, this is not always the case.
1556         * For example, when regular scan is requested the driver sets one scan
1557         * plan with one iteration.
1558         */
1559        if (!sec_part->schedule[i - 1].iter_count)
1560                sec_part->schedule[i - 1].iter_count = 0xff;
1561
1562        sec_part->delay = cpu_to_le16(params->delay);
1563        sec_part->preq = params->preq;
1564
1565        return 0;
1566}
1567
1568static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
1569{
1570        return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
1571}
1572
1573static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
1574{
1575        bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1576                                         IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1577
1578        /* This looks a bit arbitrary, but the idea is that if we run
1579         * out of possible simultaneous scans and the userspace is
1580         * trying to run a scan type that is already running, we
1581         * return -EBUSY.  But if the userspace wants to start a
1582         * different type of scan, we stop the opposite type to make
1583         * space for the new request.  The reason is backwards
1584         * compatibility with old wpa_supplicant that wouldn't stop a
1585         * scheduled scan before starting a normal scan.
1586         */
1587
1588        /* FW supports only a single periodic scan */
1589        if ((type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT) &&
1590            mvm->scan_status & (IWL_MVM_SCAN_SCHED | IWL_MVM_SCAN_NETDETECT))
1591                return -EBUSY;
1592
1593        if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
1594                return 0;
1595
1596        /* Use a switch, even though this is a bitmask, so that more
1597         * than one bits set will fall in default and we will warn.
1598         */
1599        switch (type) {
1600        case IWL_MVM_SCAN_REGULAR:
1601                if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1602                        return -EBUSY;
1603                return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1604        case IWL_MVM_SCAN_SCHED:
1605                if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1606                        return -EBUSY;
1607                return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
1608        case IWL_MVM_SCAN_NETDETECT:
1609                /* For non-unified images, there's no need to stop
1610                 * anything for net-detect since the firmware is
1611                 * restarted anyway.  This way, any sched scans that
1612                 * were running will be restarted when we resume.
1613                 */
1614                if (!unified_image)
1615                        return 0;
1616
1617                /* If this is a unified image and we ran out of scans,
1618                 * we need to stop something.  Prefer stopping regular
1619                 * scans, because the results are useless at this
1620                 * point, and we should be able to keep running
1621                 * another scheduled scan while suspended.
1622                 */
1623                if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
1624                        return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
1625                                                 true);
1626                if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
1627                        return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
1628                                                 true);
1629                /* Something is wrong if no scan was running but we
1630                 * ran out of scans.
1631                 */
1632                /* fall through */
1633        default:
1634                WARN_ON(1);
1635                break;
1636        }
1637
1638        return -EIO;
1639}
1640
1641#define SCAN_TIMEOUT 20000
1642
1643void iwl_mvm_scan_timeout_wk(struct work_struct *work)
1644{
1645        struct delayed_work *delayed_work = to_delayed_work(work);
1646        struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1647                                           scan_timeout_dwork);
1648
1649        IWL_ERR(mvm, "regular scan timed out\n");
1650
1651        iwl_force_nmi(mvm->trans);
1652}
1653
1654static void iwl_mvm_fill_scan_type(struct iwl_mvm *mvm,
1655                                   struct iwl_mvm_scan_params *params,
1656                                   struct ieee80211_vif *vif)
1657{
1658        if (iwl_mvm_is_cdb_supported(mvm)) {
1659                params->type =
1660                        iwl_mvm_get_scan_type_band(mvm, vif,
1661                                                   NL80211_BAND_2GHZ);
1662                params->hb_type =
1663                        iwl_mvm_get_scan_type_band(mvm, vif,
1664                                                   NL80211_BAND_5GHZ);
1665        } else {
1666                params->type = iwl_mvm_get_scan_type(mvm, vif);
1667        }
1668}
1669
1670int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1671                           struct cfg80211_scan_request *req,
1672                           struct ieee80211_scan_ies *ies)
1673{
1674        struct iwl_host_cmd hcmd = {
1675                .len = { iwl_mvm_scan_size(mvm), },
1676                .data = { mvm->scan_cmd, },
1677                .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1678        };
1679        struct iwl_mvm_scan_params params = {};
1680        int ret;
1681        struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
1682
1683        lockdep_assert_held(&mvm->mutex);
1684
1685        if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1686                IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
1687                return -EBUSY;
1688        }
1689
1690        ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
1691        if (ret)
1692                return ret;
1693
1694        /* we should have failed registration if scan_cmd was NULL */
1695        if (WARN_ON(!mvm->scan_cmd))
1696                return -ENOMEM;
1697
1698        if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1699                return -ENOBUFS;
1700
1701        params.n_ssids = req->n_ssids;
1702        params.flags = req->flags;
1703        params.n_channels = req->n_channels;
1704        params.delay = 0;
1705        params.ssids = req->ssids;
1706        params.channels = req->channels;
1707        params.mac_addr = req->mac_addr;
1708        params.mac_addr_mask = req->mac_addr_mask;
1709        params.no_cck = req->no_cck;
1710        params.pass_all = true;
1711        params.n_match_sets = 0;
1712        params.match_sets = NULL;
1713
1714        params.scan_plans = &scan_plan;
1715        params.n_scan_plans = 1;
1716
1717        iwl_mvm_fill_scan_type(mvm, &params, vif);
1718
1719        ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
1720        if (ret < 0)
1721                return ret;
1722
1723        params.measurement_dwell = ret;
1724
1725        iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1726
1727        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1728                hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1729                ret = iwl_mvm_scan_umac(mvm, vif, &params,
1730                                        IWL_MVM_SCAN_REGULAR);
1731        } else {
1732                hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1733                ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1734        }
1735
1736        if (ret)
1737                return ret;
1738
1739        iwl_mvm_pause_tcm(mvm, false);
1740
1741        ret = iwl_mvm_send_cmd(mvm, &hcmd);
1742        if (ret) {
1743                /* If the scan failed, it usually means that the FW was unable
1744                 * to allocate the time events. Warn on it, but maybe we
1745                 * should try to send the command again with different params.
1746                 */
1747                IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1748                iwl_mvm_resume_tcm(mvm);
1749                return ret;
1750        }
1751
1752        IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1753        mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
1754        mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
1755        iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1756
1757        schedule_delayed_work(&mvm->scan_timeout_dwork,
1758                              msecs_to_jiffies(SCAN_TIMEOUT));
1759
1760        return 0;
1761}
1762
1763int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1764                             struct ieee80211_vif *vif,
1765                             struct cfg80211_sched_scan_request *req,
1766                             struct ieee80211_scan_ies *ies,
1767                             int type)
1768{
1769        struct iwl_host_cmd hcmd = {
1770                .len = { iwl_mvm_scan_size(mvm), },
1771                .data = { mvm->scan_cmd, },
1772                .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1773        };
1774        struct iwl_mvm_scan_params params = {};
1775        int ret;
1776
1777        lockdep_assert_held(&mvm->mutex);
1778
1779        if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
1780                IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
1781                return -EBUSY;
1782        }
1783
1784        ret = iwl_mvm_check_running_scans(mvm, type);
1785        if (ret)
1786                return ret;
1787
1788        /* we should have failed registration if scan_cmd was NULL */
1789        if (WARN_ON(!mvm->scan_cmd))
1790                return -ENOMEM;
1791
1792        if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
1793                return -ENOBUFS;
1794
1795        params.n_ssids = req->n_ssids;
1796        params.flags = req->flags;
1797        params.n_channels = req->n_channels;
1798        params.ssids = req->ssids;
1799        params.channels = req->channels;
1800        params.mac_addr = req->mac_addr;
1801        params.mac_addr_mask = req->mac_addr_mask;
1802        params.no_cck = false;
1803        params.pass_all =  iwl_mvm_scan_pass_all(mvm, req);
1804        params.n_match_sets = req->n_match_sets;
1805        params.match_sets = req->match_sets;
1806        if (!req->n_scan_plans)
1807                return -EINVAL;
1808
1809        params.n_scan_plans = req->n_scan_plans;
1810        params.scan_plans = req->scan_plans;
1811
1812        iwl_mvm_fill_scan_type(mvm, &params, vif);
1813
1814        /* In theory, LMAC scans can handle a 32-bit delay, but since
1815         * waiting for over 18 hours to start the scan is a bit silly
1816         * and to keep it aligned with UMAC scans (which only support
1817         * 16-bit delays), trim it down to 16-bits.
1818         */
1819        if (req->delay > U16_MAX) {
1820                IWL_DEBUG_SCAN(mvm,
1821                               "delay value is > 16-bits, set to max possible\n");
1822                params.delay = U16_MAX;
1823        } else {
1824                params.delay = req->delay;
1825        }
1826
1827        ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1828        if (ret)
1829                return ret;
1830
1831        iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
1832
1833        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1834                hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
1835                ret = iwl_mvm_scan_umac(mvm, vif, &params, type);
1836        } else {
1837                hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
1838                ret = iwl_mvm_scan_lmac(mvm, vif, &params);
1839        }
1840
1841        if (ret)
1842                return ret;
1843
1844        ret = iwl_mvm_send_cmd(mvm, &hcmd);
1845        if (!ret) {
1846                IWL_DEBUG_SCAN(mvm,
1847                               "Sched scan request was sent successfully\n");
1848                mvm->scan_status |= type;
1849        } else {
1850                /* If the scan failed, it usually means that the FW was unable
1851                 * to allocate the time events. Warn on it, but maybe we
1852                 * should try to send the command again with different params.
1853                 */
1854                IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1855        }
1856
1857        return ret;
1858}
1859
1860void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1861                                         struct iwl_rx_cmd_buffer *rxb)
1862{
1863        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1864        struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1865        u32 uid = __le32_to_cpu(notif->uid);
1866        bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
1867
1868        if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
1869                return;
1870
1871        /* if the scan is already stopping, we don't need to notify mac80211 */
1872        if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
1873                struct cfg80211_scan_info info = {
1874                        .aborted = aborted,
1875                        .scan_start_tsf = mvm->scan_start,
1876                };
1877
1878                memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
1879                ieee80211_scan_completed(mvm->hw, &info);
1880                mvm->scan_vif = NULL;
1881                iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1882                cancel_delayed_work(&mvm->scan_timeout_dwork);
1883                iwl_mvm_resume_tcm(mvm);
1884        } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
1885                ieee80211_sched_scan_stopped(mvm->hw);
1886                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
1887        }
1888
1889        mvm->scan_status &= ~mvm->scan_uid_status[uid];
1890        IWL_DEBUG_SCAN(mvm,
1891                       "Scan completed, uid %u type %u, status %s, EBS status %s\n",
1892                       uid, mvm->scan_uid_status[uid],
1893                       notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1894                                "completed" : "aborted",
1895                       iwl_mvm_ebs_status_str(notif->ebs_status));
1896        IWL_DEBUG_SCAN(mvm,
1897                       "Last line %d, Last iteration %d, Time from last iteration %d\n",
1898                       notif->last_schedule, notif->last_iter,
1899                       __le32_to_cpu(notif->time_from_last_iter));
1900
1901        if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
1902            notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
1903                mvm->last_ebs_successful = false;
1904
1905        mvm->scan_uid_status[uid] = 0;
1906
1907        iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_SCAN_COMPLETE);
1908}
1909
1910void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
1911                                              struct iwl_rx_cmd_buffer *rxb)
1912{
1913        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1914        struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
1915
1916        mvm->scan_start = le64_to_cpu(notif->start_tsf);
1917
1918        IWL_DEBUG_SCAN(mvm,
1919                       "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
1920                       notif->status, notif->scanned_channels);
1921
1922        if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
1923                IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
1924                ieee80211_sched_scan_results(mvm->hw);
1925                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
1926        }
1927
1928        IWL_DEBUG_SCAN(mvm,
1929                       "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
1930                       mvm->scan_start);
1931}
1932
1933static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
1934{
1935        struct iwl_umac_scan_abort cmd = {};
1936        int uid, ret;
1937
1938        lockdep_assert_held(&mvm->mutex);
1939
1940        /* We should always get a valid index here, because we already
1941         * checked that this type of scan was running in the generic
1942         * code.
1943         */
1944        uid = iwl_mvm_scan_uid_by_status(mvm, type);
1945        if (WARN_ON_ONCE(uid < 0))
1946                return uid;
1947
1948        cmd.uid = cpu_to_le32(uid);
1949
1950        IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
1951
1952        ret = iwl_mvm_send_cmd_pdu(mvm,
1953                                   iwl_cmd_id(SCAN_ABORT_UMAC,
1954                                              IWL_ALWAYS_LONG_GROUP, 0),
1955                                   0, sizeof(cmd), &cmd);
1956        if (!ret)
1957                mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
1958
1959        return ret;
1960}
1961
1962static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
1963{
1964        struct iwl_notification_wait wait_scan_done;
1965        static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
1966                                              SCAN_OFFLOAD_COMPLETE, };
1967        int ret;
1968
1969        lockdep_assert_held(&mvm->mutex);
1970
1971        iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1972                                   scan_done_notif,
1973                                   ARRAY_SIZE(scan_done_notif),
1974                                   NULL, NULL);
1975
1976        IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
1977
1978        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1979                ret = iwl_mvm_umac_scan_abort(mvm, type);
1980        else
1981                ret = iwl_mvm_lmac_scan_abort(mvm);
1982
1983        if (ret) {
1984                IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
1985                iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1986                return ret;
1987        }
1988
1989        return iwl_wait_notification(&mvm->notif_wait, &wait_scan_done,
1990                                     1 * HZ);
1991}
1992
1993int iwl_mvm_scan_size(struct iwl_mvm *mvm)
1994{
1995        int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
1996
1997        if (iwl_mvm_is_adaptive_dwell_v2_supported(mvm))
1998                base_size = IWL_SCAN_REQ_UMAC_SIZE_V8;
1999        else if (iwl_mvm_is_adaptive_dwell_supported(mvm))
2000                base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
2001        else if (iwl_mvm_cdb_scan_api(mvm))
2002                base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
2003
2004        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2005                return base_size +
2006                        sizeof(struct iwl_scan_channel_cfg_umac) *
2007                                mvm->fw->ucode_capa.n_scan_channels +
2008                        sizeof(struct iwl_scan_req_umac_tail);
2009
2010        return sizeof(struct iwl_scan_req_lmac) +
2011                sizeof(struct iwl_scan_channel_cfg_lmac) *
2012                mvm->fw->ucode_capa.n_scan_channels +
2013                sizeof(struct iwl_scan_probe_req);
2014}
2015
2016/*
2017 * This function is used in nic restart flow, to inform mac80211 about scans
2018 * that was aborted by restart flow or by an assert.
2019 */
2020void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
2021{
2022        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2023                int uid, i;
2024
2025                uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
2026                if (uid >= 0) {
2027                        struct cfg80211_scan_info info = {
2028                                .aborted = true,
2029                        };
2030
2031                        ieee80211_scan_completed(mvm->hw, &info);
2032                        mvm->scan_uid_status[uid] = 0;
2033                }
2034                uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
2035                if (uid >= 0 && !mvm->fw_restart) {
2036                        ieee80211_sched_scan_stopped(mvm->hw);
2037                        mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2038                        mvm->scan_uid_status[uid] = 0;
2039                }
2040
2041                /* We shouldn't have any UIDs still set.  Loop over all the
2042                 * UIDs to make sure there's nothing left there and warn if
2043                 * any is found.
2044                 */
2045                for (i = 0; i < mvm->max_scans; i++) {
2046                        if (WARN_ONCE(mvm->scan_uid_status[i],
2047                                      "UMAC scan UID %d status was not cleaned\n",
2048                                      i))
2049                                mvm->scan_uid_status[i] = 0;
2050                }
2051        } else {
2052                if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
2053                        struct cfg80211_scan_info info = {
2054                                .aborted = true,
2055                        };
2056
2057                        ieee80211_scan_completed(mvm->hw, &info);
2058                }
2059
2060                /* Sched scan will be restarted by mac80211 in
2061                 * restart_hw, so do not report if FW is about to be
2062                 * restarted.
2063                 */
2064                if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
2065                    !mvm->fw_restart) {
2066                        ieee80211_sched_scan_stopped(mvm->hw);
2067                        mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2068                }
2069        }
2070}
2071
2072int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
2073{
2074        int ret;
2075
2076        if (!(mvm->scan_status & type))
2077                return 0;
2078
2079        if (iwl_mvm_is_radio_killed(mvm)) {
2080                ret = 0;
2081                goto out;
2082        }
2083
2084        ret = iwl_mvm_scan_stop_wait(mvm, type);
2085        if (!ret)
2086                mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
2087out:
2088        /* Clear the scan status so the next scan requests will
2089         * succeed and mark the scan as stopping, so that the Rx
2090         * handler doesn't do anything, as the scan was stopped from
2091         * above.
2092         */
2093        mvm->scan_status &= ~type;
2094
2095        if (type == IWL_MVM_SCAN_REGULAR) {
2096                /* Since the rx handler won't do anything now, we have
2097                 * to release the scan reference here.
2098                 */
2099                iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2100                cancel_delayed_work(&mvm->scan_timeout_dwork);
2101                if (notify) {
2102                        struct cfg80211_scan_info info = {
2103                                .aborted = true,
2104                        };
2105
2106                        ieee80211_scan_completed(mvm->hw, &info);
2107                }
2108        } else if (notify) {
2109                ieee80211_sched_scan_stopped(mvm->hw);
2110                mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
2111        }
2112
2113        return ret;
2114}
2115