linux/drivers/net/wireless/intel/iwlwifi/mvm/ops.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   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
   6 */
   7#include <linux/module.h>
   8#include <linux/vmalloc.h>
   9#include <net/mac80211.h>
  10
  11#include "fw/notif-wait.h"
  12#include "iwl-trans.h"
  13#include "iwl-op-mode.h"
  14#include "fw/img.h"
  15#include "iwl-debug.h"
  16#include "iwl-drv.h"
  17#include "iwl-modparams.h"
  18#include "mvm.h"
  19#include "iwl-phy-db.h"
  20#include "iwl-eeprom-parse.h"
  21#include "iwl-csr.h"
  22#include "iwl-io.h"
  23#include "iwl-prph.h"
  24#include "rs.h"
  25#include "fw/api/scan.h"
  26#include "time-event.h"
  27#include "fw-api.h"
  28#include "fw/acpi.h"
  29
  30#define DRV_DESCRIPTION "The new Intel(R) wireless AGN driver for Linux"
  31MODULE_DESCRIPTION(DRV_DESCRIPTION);
  32MODULE_LICENSE("GPL");
  33
  34static const struct iwl_op_mode_ops iwl_mvm_ops;
  35static const struct iwl_op_mode_ops iwl_mvm_ops_mq;
  36
  37struct iwl_mvm_mod_params iwlmvm_mod_params = {
  38        .power_scheme = IWL_POWER_SCHEME_BPS,
  39        /* rest of fields are 0 by default */
  40};
  41
  42module_param_named(init_dbg, iwlmvm_mod_params.init_dbg, bool, 0444);
  43MODULE_PARM_DESC(init_dbg,
  44                 "set to true to debug an ASSERT in INIT fw (default: false");
  45module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, 0444);
  46MODULE_PARM_DESC(power_scheme,
  47                 "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
  48
  49/*
  50 * module init and exit functions
  51 */
  52static int __init iwl_mvm_init(void)
  53{
  54        int ret;
  55
  56        ret = iwl_mvm_rate_control_register();
  57        if (ret) {
  58                pr_err("Unable to register rate control algorithm: %d\n", ret);
  59                return ret;
  60        }
  61
  62        ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
  63        if (ret)
  64                pr_err("Unable to register MVM op_mode: %d\n", ret);
  65
  66        return ret;
  67}
  68module_init(iwl_mvm_init);
  69
  70static void __exit iwl_mvm_exit(void)
  71{
  72        iwl_opmode_deregister("iwlmvm");
  73        iwl_mvm_rate_control_unregister();
  74}
  75module_exit(iwl_mvm_exit);
  76
  77static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
  78{
  79        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  80        u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
  81        u32 reg_val;
  82        u32 phy_config = iwl_mvm_get_phy_config(mvm);
  83
  84        radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
  85                         FW_PHY_CFG_RADIO_TYPE_POS;
  86        radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
  87                         FW_PHY_CFG_RADIO_STEP_POS;
  88        radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
  89                         FW_PHY_CFG_RADIO_DASH_POS;
  90
  91        /* SKU control */
  92        reg_val = CSR_HW_REV_STEP_DASH(mvm->trans->hw_rev);
  93
  94        /* radio configuration */
  95        reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
  96        reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
  97        reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
  98
  99        WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
 100                 ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
 101
 102        /*
 103         * TODO: Bits 7-8 of CSR in 8000 HW family and higher set the ADC
 104         * sampling, and shouldn't be set to any non-zero value.
 105         * The same is supposed to be true of the other HW, but unsetting
 106         * them (such as the 7260) causes automatic tests to fail on seemingly
 107         * unrelated errors. Need to further investigate this, but for now
 108         * we'll separate cases.
 109         */
 110        if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000)
 111                reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
 112
 113        if (iwl_fw_dbg_is_d3_debug_enabled(&mvm->fwrt))
 114                reg_val |= CSR_HW_IF_CONFIG_REG_D3_DEBUG;
 115
 116        iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
 117                                CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP_DASH |
 118                                CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
 119                                CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
 120                                CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
 121                                CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
 122                                CSR_HW_IF_CONFIG_REG_BIT_MAC_SI   |
 123                                CSR_HW_IF_CONFIG_REG_D3_DEBUG,
 124                                reg_val);
 125
 126        IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
 127                       radio_cfg_step, radio_cfg_dash);
 128
 129        /*
 130         * W/A : NIC is stuck in a reset state after Early PCIe power off
 131         * (PCIe power is lost before PERST# is asserted), causing ME FW
 132         * to lose ownership and not being able to obtain it back.
 133         */
 134        if (!mvm->trans->cfg->apmg_not_supported)
 135                iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
 136                                       APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
 137                                       ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
 138}
 139
 140static void iwl_mvm_rx_monitor_notif(struct iwl_mvm *mvm,
 141                                     struct iwl_rx_cmd_buffer *rxb)
 142{
 143        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 144        struct iwl_datapath_monitor_notif *notif = (void *)pkt->data;
 145        struct ieee80211_supported_band *sband;
 146        const struct ieee80211_sta_he_cap *he_cap;
 147        struct ieee80211_vif *vif;
 148
 149        if (notif->type != cpu_to_le32(IWL_DP_MON_NOTIF_TYPE_EXT_CCA))
 150                return;
 151
 152        vif = iwl_mvm_get_vif_by_macid(mvm, notif->mac_id);
 153        if (!vif || vif->type != NL80211_IFTYPE_STATION)
 154                return;
 155
 156        if (!vif->bss_conf.chandef.chan ||
 157            vif->bss_conf.chandef.chan->band != NL80211_BAND_2GHZ ||
 158            vif->bss_conf.chandef.width < NL80211_CHAN_WIDTH_40)
 159                return;
 160
 161        if (!vif->bss_conf.assoc)
 162                return;
 163
 164        /* this shouldn't happen *again*, ignore it */
 165        if (mvm->cca_40mhz_workaround)
 166                return;
 167
 168        /*
 169         * We'll decrement this on disconnect - so set to 2 since we'll
 170         * still have to disconnect from the current AP first.
 171         */
 172        mvm->cca_40mhz_workaround = 2;
 173
 174        /*
 175         * This capability manipulation isn't really ideal, but it's the
 176         * easiest choice - otherwise we'd have to do some major changes
 177         * in mac80211 to support this, which isn't worth it. This does
 178         * mean that userspace may have outdated information, but that's
 179         * actually not an issue at all.
 180         */
 181        sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
 182
 183        WARN_ON(!sband->ht_cap.ht_supported);
 184        WARN_ON(!(sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40));
 185        sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 186
 187        he_cap = ieee80211_get_he_iftype_cap(sband,
 188                                             ieee80211_vif_type_p2p(vif));
 189
 190        if (he_cap) {
 191                /* we know that ours is writable */
 192                struct ieee80211_sta_he_cap *he = (void *)he_cap;
 193
 194                WARN_ON(!he->has_he);
 195                WARN_ON(!(he->he_cap_elem.phy_cap_info[0] &
 196                                IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G));
 197                he->he_cap_elem.phy_cap_info[0] &=
 198                        ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
 199        }
 200
 201        ieee80211_disconnect(vif, true);
 202}
 203
 204void iwl_mvm_apply_fw_smps_request(struct ieee80211_vif *vif)
 205{
 206        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 207        struct iwl_mvm *mvm = mvmvif->mvm;
 208        enum ieee80211_smps_mode mode = IEEE80211_SMPS_AUTOMATIC;
 209
 210        if (mvm->fw_static_smps_request &&
 211            vif->bss_conf.chandef.width == NL80211_CHAN_WIDTH_160 &&
 212            vif->bss_conf.he_support)
 213                mode = IEEE80211_SMPS_STATIC;
 214
 215        iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_FW, mode);
 216}
 217
 218static void iwl_mvm_intf_dual_chain_req(void *data, u8 *mac,
 219                                        struct ieee80211_vif *vif)
 220{
 221        iwl_mvm_apply_fw_smps_request(vif);
 222}
 223
 224static void iwl_mvm_rx_thermal_dual_chain_req(struct iwl_mvm *mvm,
 225                                              struct iwl_rx_cmd_buffer *rxb)
 226{
 227        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 228        struct iwl_thermal_dual_chain_request *req = (void *)pkt->data;
 229
 230        /*
 231         * We could pass it to the iterator data, but also need to remember
 232         * it for new interfaces that are added while in this state.
 233         */
 234        mvm->fw_static_smps_request =
 235                req->event == cpu_to_le32(THERMAL_DUAL_CHAIN_REQ_DISABLE);
 236        ieee80211_iterate_interfaces(mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
 237                                     iwl_mvm_intf_dual_chain_req, NULL);
 238}
 239
 240/**
 241 * enum iwl_rx_handler_context context for Rx handler
 242 * @RX_HANDLER_SYNC : this means that it will be called in the Rx path
 243 *      which can't acquire mvm->mutex.
 244 * @RX_HANDLER_ASYNC_LOCKED : If the handler needs to hold mvm->mutex
 245 *      (and only in this case!), it should be set as ASYNC. In that case,
 246 *      it will be called from a worker with mvm->mutex held.
 247 * @RX_HANDLER_ASYNC_UNLOCKED : in case the handler needs to lock the
 248 *      mutex itself, it will be called from a worker without mvm->mutex held.
 249 */
 250enum iwl_rx_handler_context {
 251        RX_HANDLER_SYNC,
 252        RX_HANDLER_ASYNC_LOCKED,
 253        RX_HANDLER_ASYNC_UNLOCKED,
 254};
 255
 256/**
 257 * struct iwl_rx_handlers handler for FW notification
 258 * @cmd_id: command id
 259 * @context: see &iwl_rx_handler_context
 260 * @fn: the function is called when notification is received
 261 */
 262struct iwl_rx_handlers {
 263        u16 cmd_id, min_size;
 264        enum iwl_rx_handler_context context;
 265        void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
 266};
 267
 268#define RX_HANDLER_NO_SIZE(_cmd_id, _fn, _context)              \
 269        { .cmd_id = _cmd_id, .fn = _fn, .context = _context, }
 270#define RX_HANDLER_GRP_NO_SIZE(_grp, _cmd, _fn, _context)       \
 271        { .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn, .context = _context, }
 272#define RX_HANDLER(_cmd_id, _fn, _context, _struct)             \
 273        { .cmd_id = _cmd_id, .fn = _fn,                         \
 274          .context = _context, .min_size = sizeof(_struct), }
 275#define RX_HANDLER_GRP(_grp, _cmd, _fn, _context, _struct)      \
 276        { .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn,             \
 277          .context = _context, .min_size = sizeof(_struct), }
 278
 279/*
 280 * Handlers for fw notifications
 281 * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
 282 * This list should be in order of frequency for performance purposes.
 283 *
 284 * The handler can be one from three contexts, see &iwl_rx_handler_context
 285 */
 286static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
 287        RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, RX_HANDLER_SYNC,
 288                   struct iwl_mvm_tx_resp),
 289        RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, RX_HANDLER_SYNC,
 290                   struct iwl_mvm_ba_notif),
 291
 292        RX_HANDLER_GRP(DATA_PATH_GROUP, TLC_MNG_UPDATE_NOTIF,
 293                       iwl_mvm_tlc_update_notif, RX_HANDLER_SYNC,
 294                       struct iwl_tlc_update_notif),
 295
 296        RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_notif,
 297                   RX_HANDLER_ASYNC_LOCKED, struct iwl_bt_coex_profile_notif),
 298        RX_HANDLER_NO_SIZE(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif,
 299                           RX_HANDLER_ASYNC_LOCKED),
 300        RX_HANDLER_NO_SIZE(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics,
 301                           RX_HANDLER_ASYNC_LOCKED),
 302
 303        RX_HANDLER(BA_WINDOW_STATUS_NOTIFICATION_ID,
 304                   iwl_mvm_window_status_notif, RX_HANDLER_SYNC,
 305                   struct iwl_ba_window_status_notif),
 306
 307        RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif,
 308                   RX_HANDLER_SYNC, struct iwl_time_event_notif),
 309        RX_HANDLER_GRP(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF,
 310                       iwl_mvm_rx_session_protect_notif, RX_HANDLER_SYNC,
 311                       struct iwl_mvm_session_prot_notif),
 312        RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc,
 313                   RX_HANDLER_ASYNC_LOCKED, struct iwl_mcc_chub_notif),
 314
 315        RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, RX_HANDLER_SYNC,
 316                   struct iwl_mvm_eosp_notification),
 317
 318        RX_HANDLER(SCAN_ITERATION_COMPLETE,
 319                   iwl_mvm_rx_lmac_scan_iter_complete_notif, RX_HANDLER_SYNC,
 320                   struct iwl_lmac_scan_complete_notif),
 321        RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
 322                   iwl_mvm_rx_lmac_scan_complete_notif,
 323                   RX_HANDLER_ASYNC_LOCKED, struct iwl_periodic_scan_complete),
 324        RX_HANDLER_NO_SIZE(MATCH_FOUND_NOTIFICATION,
 325                           iwl_mvm_rx_scan_match_found,
 326                           RX_HANDLER_SYNC),
 327        RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
 328                   RX_HANDLER_ASYNC_LOCKED, struct iwl_umac_scan_complete),
 329        RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
 330                   iwl_mvm_rx_umac_scan_iter_complete_notif, RX_HANDLER_SYNC,
 331                   struct iwl_umac_scan_iter_complete_notif),
 332
 333        RX_HANDLER(CARD_STATE_NOTIFICATION, iwl_mvm_rx_card_state_notif,
 334                   RX_HANDLER_SYNC, struct iwl_card_state_notif),
 335
 336        RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
 337                   RX_HANDLER_SYNC, struct iwl_missed_beacons_notif),
 338
 339        RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, RX_HANDLER_SYNC,
 340                   struct iwl_error_resp),
 341        RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
 342                   iwl_mvm_power_uapsd_misbehaving_ap_notif, RX_HANDLER_SYNC,
 343                   struct iwl_uapsd_misbehaving_ap_notif),
 344        RX_HANDLER_NO_SIZE(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif,
 345                           RX_HANDLER_ASYNC_LOCKED),
 346        RX_HANDLER_GRP_NO_SIZE(PHY_OPS_GROUP, DTS_MEASUREMENT_NOTIF_WIDE,
 347                               iwl_mvm_temp_notif, RX_HANDLER_ASYNC_UNLOCKED),
 348        RX_HANDLER_GRP(PHY_OPS_GROUP, CT_KILL_NOTIFICATION,
 349                       iwl_mvm_ct_kill_notif, RX_HANDLER_SYNC,
 350                       struct ct_kill_notif),
 351
 352        RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
 353                   RX_HANDLER_ASYNC_LOCKED,
 354                   struct iwl_tdls_channel_switch_notif),
 355        RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif,
 356                   RX_HANDLER_SYNC, struct iwl_mfuart_load_notif_v1),
 357        RX_HANDLER_GRP(LOCATION_GROUP, TOF_RESPONDER_STATS,
 358                       iwl_mvm_ftm_responder_stats, RX_HANDLER_ASYNC_LOCKED,
 359                       struct iwl_ftm_responder_stats),
 360
 361        RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_RANGE_RESPONSE_NOTIF,
 362                               iwl_mvm_ftm_range_resp, RX_HANDLER_ASYNC_LOCKED),
 363        RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_LC_NOTIF,
 364                               iwl_mvm_ftm_lc_notif, RX_HANDLER_ASYNC_LOCKED),
 365
 366        RX_HANDLER_GRP(DEBUG_GROUP, MFU_ASSERT_DUMP_NTF,
 367                       iwl_mvm_mfu_assert_dump_notif, RX_HANDLER_SYNC,
 368                       struct iwl_mfu_assert_dump_notif),
 369        RX_HANDLER_GRP(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF,
 370                       iwl_mvm_rx_stored_beacon_notif, RX_HANDLER_SYNC,
 371                       struct iwl_stored_beacon_notif_v2),
 372        RX_HANDLER_GRP(DATA_PATH_GROUP, MU_GROUP_MGMT_NOTIF,
 373                       iwl_mvm_mu_mimo_grp_notif, RX_HANDLER_SYNC,
 374                       struct iwl_mu_group_mgmt_notif),
 375        RX_HANDLER_GRP(DATA_PATH_GROUP, STA_PM_NOTIF,
 376                       iwl_mvm_sta_pm_notif, RX_HANDLER_SYNC,
 377                       struct iwl_mvm_pm_state_notification),
 378        RX_HANDLER_GRP(MAC_CONF_GROUP, PROBE_RESPONSE_DATA_NOTIF,
 379                       iwl_mvm_probe_resp_data_notif,
 380                       RX_HANDLER_ASYNC_LOCKED,
 381                       struct iwl_probe_resp_data_notif),
 382        RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_START_NOTIF,
 383                       iwl_mvm_channel_switch_start_notif,
 384                       RX_HANDLER_SYNC, struct iwl_channel_switch_start_notif),
 385        RX_HANDLER_GRP(DATA_PATH_GROUP, MONITOR_NOTIF,
 386                       iwl_mvm_rx_monitor_notif, RX_HANDLER_ASYNC_LOCKED,
 387                       struct iwl_datapath_monitor_notif),
 388
 389        RX_HANDLER_GRP(DATA_PATH_GROUP, THERMAL_DUAL_CHAIN_REQUEST,
 390                       iwl_mvm_rx_thermal_dual_chain_req,
 391                       RX_HANDLER_ASYNC_LOCKED,
 392                       struct iwl_thermal_dual_chain_request),
 393};
 394#undef RX_HANDLER
 395#undef RX_HANDLER_GRP
 396
 397/* Please keep this array *SORTED* by hex value.
 398 * Access is done through binary search
 399 */
 400static const struct iwl_hcmd_names iwl_mvm_legacy_names[] = {
 401        HCMD_NAME(UCODE_ALIVE_NTFY),
 402        HCMD_NAME(REPLY_ERROR),
 403        HCMD_NAME(ECHO_CMD),
 404        HCMD_NAME(INIT_COMPLETE_NOTIF),
 405        HCMD_NAME(PHY_CONTEXT_CMD),
 406        HCMD_NAME(DBG_CFG),
 407        HCMD_NAME(SCAN_CFG_CMD),
 408        HCMD_NAME(SCAN_REQ_UMAC),
 409        HCMD_NAME(SCAN_ABORT_UMAC),
 410        HCMD_NAME(SCAN_COMPLETE_UMAC),
 411        HCMD_NAME(BA_WINDOW_STATUS_NOTIFICATION_ID),
 412        HCMD_NAME(ADD_STA_KEY),
 413        HCMD_NAME(ADD_STA),
 414        HCMD_NAME(REMOVE_STA),
 415        HCMD_NAME(FW_GET_ITEM_CMD),
 416        HCMD_NAME(TX_CMD),
 417        HCMD_NAME(SCD_QUEUE_CFG),
 418        HCMD_NAME(TXPATH_FLUSH),
 419        HCMD_NAME(MGMT_MCAST_KEY),
 420        HCMD_NAME(WEP_KEY),
 421        HCMD_NAME(SHARED_MEM_CFG),
 422        HCMD_NAME(TDLS_CHANNEL_SWITCH_CMD),
 423        HCMD_NAME(MAC_CONTEXT_CMD),
 424        HCMD_NAME(TIME_EVENT_CMD),
 425        HCMD_NAME(TIME_EVENT_NOTIFICATION),
 426        HCMD_NAME(BINDING_CONTEXT_CMD),
 427        HCMD_NAME(TIME_QUOTA_CMD),
 428        HCMD_NAME(NON_QOS_TX_COUNTER_CMD),
 429        HCMD_NAME(LEDS_CMD),
 430        HCMD_NAME(LQ_CMD),
 431        HCMD_NAME(FW_PAGING_BLOCK_CMD),
 432        HCMD_NAME(SCAN_OFFLOAD_REQUEST_CMD),
 433        HCMD_NAME(SCAN_OFFLOAD_ABORT_CMD),
 434        HCMD_NAME(HOT_SPOT_CMD),
 435        HCMD_NAME(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
 436        HCMD_NAME(BT_COEX_UPDATE_REDUCED_TXP),
 437        HCMD_NAME(BT_COEX_CI),
 438        HCMD_NAME(PHY_CONFIGURATION_CMD),
 439        HCMD_NAME(CALIB_RES_NOTIF_PHY_DB),
 440        HCMD_NAME(PHY_DB_CMD),
 441        HCMD_NAME(SCAN_OFFLOAD_COMPLETE),
 442        HCMD_NAME(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
 443        HCMD_NAME(POWER_TABLE_CMD),
 444        HCMD_NAME(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
 445        HCMD_NAME(REPLY_THERMAL_MNG_BACKOFF),
 446        HCMD_NAME(DC2DC_CONFIG_CMD),
 447        HCMD_NAME(NVM_ACCESS_CMD),
 448        HCMD_NAME(BEACON_NOTIFICATION),
 449        HCMD_NAME(BEACON_TEMPLATE_CMD),
 450        HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
 451        HCMD_NAME(BT_CONFIG),
 452        HCMD_NAME(STATISTICS_CMD),
 453        HCMD_NAME(STATISTICS_NOTIFICATION),
 454        HCMD_NAME(EOSP_NOTIFICATION),
 455        HCMD_NAME(REDUCE_TX_POWER_CMD),
 456        HCMD_NAME(CARD_STATE_NOTIFICATION),
 457        HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
 458        HCMD_NAME(TDLS_CONFIG_CMD),
 459        HCMD_NAME(MAC_PM_POWER_TABLE),
 460        HCMD_NAME(TDLS_CHANNEL_SWITCH_NOTIFICATION),
 461        HCMD_NAME(MFUART_LOAD_NOTIFICATION),
 462        HCMD_NAME(RSS_CONFIG_CMD),
 463        HCMD_NAME(SCAN_ITERATION_COMPLETE_UMAC),
 464        HCMD_NAME(REPLY_RX_PHY_CMD),
 465        HCMD_NAME(REPLY_RX_MPDU_CMD),
 466        HCMD_NAME(BAR_FRAME_RELEASE),
 467        HCMD_NAME(FRAME_RELEASE),
 468        HCMD_NAME(BA_NOTIF),
 469        HCMD_NAME(MCC_UPDATE_CMD),
 470        HCMD_NAME(MCC_CHUB_UPDATE_CMD),
 471        HCMD_NAME(MARKER_CMD),
 472        HCMD_NAME(BT_PROFILE_NOTIFICATION),
 473        HCMD_NAME(BCAST_FILTER_CMD),
 474        HCMD_NAME(MCAST_FILTER_CMD),
 475        HCMD_NAME(REPLY_SF_CFG_CMD),
 476        HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
 477        HCMD_NAME(D3_CONFIG_CMD),
 478        HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
 479        HCMD_NAME(OFFLOADS_QUERY_CMD),
 480        HCMD_NAME(MATCH_FOUND_NOTIFICATION),
 481        HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
 482        HCMD_NAME(WOWLAN_PATTERNS),
 483        HCMD_NAME(WOWLAN_CONFIGURATION),
 484        HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
 485        HCMD_NAME(WOWLAN_TKIP_PARAM),
 486        HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
 487        HCMD_NAME(WOWLAN_GET_STATUSES),
 488        HCMD_NAME(SCAN_ITERATION_COMPLETE),
 489        HCMD_NAME(D0I3_END_CMD),
 490        HCMD_NAME(LTR_CONFIG),
 491        HCMD_NAME(LDBG_CONFIG_CMD),
 492};
 493
 494/* Please keep this array *SORTED* by hex value.
 495 * Access is done through binary search
 496 */
 497static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
 498        HCMD_NAME(SHARED_MEM_CFG_CMD),
 499        HCMD_NAME(INIT_EXTENDED_CFG_CMD),
 500        HCMD_NAME(FW_ERROR_RECOVERY_CMD),
 501};
 502
 503/* Please keep this array *SORTED* by hex value.
 504 * Access is done through binary search
 505 */
 506static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
 507        HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
 508        HCMD_NAME(SESSION_PROTECTION_CMD),
 509        HCMD_NAME(SESSION_PROTECTION_NOTIF),
 510        HCMD_NAME(CHANNEL_SWITCH_START_NOTIF),
 511};
 512
 513/* Please keep this array *SORTED* by hex value.
 514 * Access is done through binary search
 515 */
 516static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
 517        HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
 518        HCMD_NAME(CTDP_CONFIG_CMD),
 519        HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
 520        HCMD_NAME(PER_CHAIN_LIMIT_OFFSET_CMD),
 521        HCMD_NAME(CT_KILL_NOTIFICATION),
 522        HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
 523};
 524
 525/* Please keep this array *SORTED* by hex value.
 526 * Access is done through binary search
 527 */
 528static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
 529        HCMD_NAME(DQA_ENABLE_CMD),
 530        HCMD_NAME(UPDATE_MU_GROUPS_CMD),
 531        HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
 532        HCMD_NAME(STA_HE_CTXT_CMD),
 533        HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
 534        HCMD_NAME(TLC_MNG_CONFIG_CMD),
 535        HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
 536        HCMD_NAME(MONITOR_NOTIF),
 537        HCMD_NAME(THERMAL_DUAL_CHAIN_REQUEST),
 538        HCMD_NAME(STA_PM_NOTIF),
 539        HCMD_NAME(MU_GROUP_MGMT_NOTIF),
 540        HCMD_NAME(RX_QUEUES_NOTIFICATION),
 541};
 542
 543/* Please keep this array *SORTED* by hex value.
 544 * Access is done through binary search
 545 */
 546static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
 547        HCMD_NAME(TOF_RANGE_REQ_CMD),
 548        HCMD_NAME(TOF_CONFIG_CMD),
 549        HCMD_NAME(TOF_RANGE_ABORT_CMD),
 550        HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
 551        HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
 552        HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
 553        HCMD_NAME(TOF_LC_NOTIF),
 554        HCMD_NAME(TOF_RESPONDER_STATS),
 555        HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
 556        HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
 557};
 558
 559/* Please keep this array *SORTED* by hex value.
 560 * Access is done through binary search
 561 */
 562static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
 563        HCMD_NAME(STORED_BEACON_NTF),
 564};
 565
 566/* Please keep this array *SORTED* by hex value.
 567 * Access is done through binary search
 568 */
 569static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
 570        HCMD_NAME(NVM_ACCESS_COMPLETE),
 571        HCMD_NAME(NVM_GET_INFO),
 572        HCMD_NAME(TAS_CONFIG),
 573};
 574
 575static const struct iwl_hcmd_arr iwl_mvm_groups[] = {
 576        [LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
 577        [LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
 578        [SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
 579        [MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
 580        [PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
 581        [DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
 582        [LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
 583        [PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
 584        [REGULATORY_AND_NVM_GROUP] =
 585                HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
 586};
 587
 588/* this forward declaration can avoid to export the function */
 589static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
 590
 591static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
 592{
 593        const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
 594        u64 dflt_pwr_limit;
 595
 596        if (!backoff)
 597                return 0;
 598
 599        dflt_pwr_limit = iwl_acpi_get_pwr_limit(mvm->dev);
 600
 601        while (backoff->pwr) {
 602                if (dflt_pwr_limit >= backoff->pwr)
 603                        return backoff->backoff;
 604
 605                backoff++;
 606        }
 607
 608        return 0;
 609}
 610
 611static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
 612{
 613        struct iwl_mvm *mvm =
 614                container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
 615        struct ieee80211_vif *tx_blocked_vif;
 616        struct iwl_mvm_vif *mvmvif;
 617
 618        mutex_lock(&mvm->mutex);
 619
 620        tx_blocked_vif =
 621                rcu_dereference_protected(mvm->csa_tx_blocked_vif,
 622                                          lockdep_is_held(&mvm->mutex));
 623
 624        if (!tx_blocked_vif)
 625                goto unlock;
 626
 627        mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
 628        iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
 629        RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
 630unlock:
 631        mutex_unlock(&mvm->mutex);
 632}
 633
 634static int iwl_mvm_fwrt_dump_start(void *ctx)
 635{
 636        struct iwl_mvm *mvm = ctx;
 637
 638        mutex_lock(&mvm->mutex);
 639
 640        return 0;
 641}
 642
 643static void iwl_mvm_fwrt_dump_end(void *ctx)
 644{
 645        struct iwl_mvm *mvm = ctx;
 646
 647        mutex_unlock(&mvm->mutex);
 648}
 649
 650static bool iwl_mvm_fwrt_fw_running(void *ctx)
 651{
 652        return iwl_mvm_firmware_running(ctx);
 653}
 654
 655static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
 656{
 657        struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
 658        int ret;
 659
 660        mutex_lock(&mvm->mutex);
 661        ret = iwl_mvm_send_cmd(mvm, host_cmd);
 662        mutex_unlock(&mvm->mutex);
 663
 664        return ret;
 665}
 666
 667static bool iwl_mvm_d3_debug_enable(void *ctx)
 668{
 669        return IWL_MVM_D3_DEBUG;
 670}
 671
 672static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
 673        .dump_start = iwl_mvm_fwrt_dump_start,
 674        .dump_end = iwl_mvm_fwrt_dump_end,
 675        .fw_running = iwl_mvm_fwrt_fw_running,
 676        .send_hcmd = iwl_mvm_fwrt_send_hcmd,
 677        .d3_debug_enable = iwl_mvm_d3_debug_enable,
 678};
 679
 680static int iwl_mvm_start_get_nvm(struct iwl_mvm *mvm)
 681{
 682        int ret;
 683
 684        rtnl_lock();
 685        wiphy_lock(mvm->hw->wiphy);
 686        mutex_lock(&mvm->mutex);
 687
 688        ret = iwl_run_init_mvm_ucode(mvm);
 689
 690        if (ret && ret != -ERFKILL)
 691                iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
 692        if (!ret && iwl_mvm_is_lar_supported(mvm)) {
 693                mvm->hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
 694                ret = iwl_mvm_init_mcc(mvm);
 695        }
 696
 697        if (!iwlmvm_mod_params.init_dbg || !ret)
 698                iwl_mvm_stop_device(mvm);
 699
 700        mutex_unlock(&mvm->mutex);
 701        wiphy_unlock(mvm->hw->wiphy);
 702        rtnl_unlock();
 703
 704        if (ret < 0)
 705                IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
 706
 707        return ret;
 708}
 709
 710static int iwl_mvm_start_post_nvm(struct iwl_mvm *mvm)
 711{
 712        int ret;
 713
 714        iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
 715
 716        ret = iwl_mvm_mac_setup_register(mvm);
 717        if (ret)
 718                return ret;
 719        mvm->hw_registered = true;
 720
 721        iwl_mvm_dbgfs_register(mvm);
 722
 723        return 0;
 724}
 725
 726struct iwl_mvm_frob_txf_data {
 727        u8 *buf;
 728        size_t buflen;
 729};
 730
 731static void iwl_mvm_frob_txf_key_iter(struct ieee80211_hw *hw,
 732                                      struct ieee80211_vif *vif,
 733                                      struct ieee80211_sta *sta,
 734                                      struct ieee80211_key_conf *key,
 735                                      void *data)
 736{
 737        struct iwl_mvm_frob_txf_data *txf = data;
 738        u8 keylen, match, matchend;
 739        u8 *keydata;
 740        size_t i;
 741
 742        switch (key->cipher) {
 743        case WLAN_CIPHER_SUITE_CCMP:
 744                keydata = key->key;
 745                keylen = key->keylen;
 746                break;
 747        case WLAN_CIPHER_SUITE_WEP40:
 748        case WLAN_CIPHER_SUITE_WEP104:
 749        case WLAN_CIPHER_SUITE_TKIP:
 750                /*
 751                 * WEP has short keys which might show up in the payload,
 752                 * and then you can deduce the key, so in this case just
 753                 * remove all FIFO data.
 754                 * For TKIP, we don't know the phase 2 keys here, so same.
 755                 */
 756                memset(txf->buf, 0xBB, txf->buflen);
 757                return;
 758        default:
 759                return;
 760        }
 761
 762        /* scan for key material and clear it out */
 763        match = 0;
 764        for (i = 0; i < txf->buflen; i++) {
 765                if (txf->buf[i] != keydata[match]) {
 766                        match = 0;
 767                        continue;
 768                }
 769                match++;
 770                if (match == keylen) {
 771                        memset(txf->buf + i - keylen, 0xAA, keylen);
 772                        match = 0;
 773                }
 774        }
 775
 776        /* we're dealing with a FIFO, so check wrapped around data */
 777        matchend = match;
 778        for (i = 0; match && i < keylen - match; i++) {
 779                if (txf->buf[i] != keydata[match])
 780                        break;
 781                match++;
 782                if (match == keylen) {
 783                        memset(txf->buf, 0xAA, i + 1);
 784                        memset(txf->buf + txf->buflen - matchend, 0xAA,
 785                               matchend);
 786                        break;
 787                }
 788        }
 789}
 790
 791static void iwl_mvm_frob_txf(void *ctx, void *buf, size_t buflen)
 792{
 793        struct iwl_mvm_frob_txf_data txf = {
 794                .buf = buf,
 795                .buflen = buflen,
 796        };
 797        struct iwl_mvm *mvm = ctx;
 798
 799        /* embedded key material exists only on old API */
 800        if (iwl_mvm_has_new_tx_api(mvm))
 801                return;
 802
 803        rcu_read_lock();
 804        ieee80211_iter_keys_rcu(mvm->hw, NULL, iwl_mvm_frob_txf_key_iter, &txf);
 805        rcu_read_unlock();
 806}
 807
 808static void iwl_mvm_frob_hcmd(void *ctx, void *hcmd, size_t len)
 809{
 810        /* we only use wide headers for commands */
 811        struct iwl_cmd_header_wide *hdr = hcmd;
 812        unsigned int frob_start = sizeof(*hdr), frob_end = 0;
 813
 814        if (len < sizeof(hdr))
 815                return;
 816
 817        /* all the commands we care about are in LONG_GROUP */
 818        if (hdr->group_id != LONG_GROUP)
 819                return;
 820
 821        switch (hdr->cmd) {
 822        case WEP_KEY:
 823        case WOWLAN_TKIP_PARAM:
 824        case WOWLAN_KEK_KCK_MATERIAL:
 825        case ADD_STA_KEY:
 826                /*
 827                 * blank out everything here, easier than dealing
 828                 * with the various versions of the command
 829                 */
 830                frob_end = INT_MAX;
 831                break;
 832        case MGMT_MCAST_KEY:
 833                frob_start = offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
 834                BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) !=
 835                             offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
 836
 837                frob_end = offsetofend(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
 838                BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) <
 839                             offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
 840                break;
 841        }
 842
 843        if (frob_start >= frob_end)
 844                return;
 845
 846        if (frob_end > len)
 847                frob_end = len;
 848
 849        memset((u8 *)hcmd + frob_start, 0xAA, frob_end - frob_start);
 850}
 851
 852static void iwl_mvm_frob_mem(void *ctx, u32 mem_addr, void *mem, size_t buflen)
 853{
 854        const struct iwl_dump_exclude *excl;
 855        struct iwl_mvm *mvm = ctx;
 856        int i;
 857
 858        switch (mvm->fwrt.cur_fw_img) {
 859        case IWL_UCODE_INIT:
 860        default:
 861                /* not relevant */
 862                return;
 863        case IWL_UCODE_REGULAR:
 864        case IWL_UCODE_REGULAR_USNIFFER:
 865                excl = mvm->fw->dump_excl;
 866                break;
 867        case IWL_UCODE_WOWLAN:
 868                excl = mvm->fw->dump_excl_wowlan;
 869                break;
 870        }
 871
 872        BUILD_BUG_ON(sizeof(mvm->fw->dump_excl) !=
 873                     sizeof(mvm->fw->dump_excl_wowlan));
 874
 875        for (i = 0; i < ARRAY_SIZE(mvm->fw->dump_excl); i++) {
 876                u32 start, end;
 877
 878                if (!excl[i].addr || !excl[i].size)
 879                        continue;
 880
 881                start = excl[i].addr;
 882                end = start + excl[i].size;
 883
 884                if (end <= mem_addr || start >= mem_addr + buflen)
 885                        continue;
 886
 887                if (start < mem_addr)
 888                        start = mem_addr;
 889
 890                if (end > mem_addr + buflen)
 891                        end = mem_addr + buflen;
 892
 893                memset((u8 *)mem + start - mem_addr, 0xAA, end - start);
 894        }
 895}
 896
 897static const struct iwl_dump_sanitize_ops iwl_mvm_sanitize_ops = {
 898        .frob_txf = iwl_mvm_frob_txf,
 899        .frob_hcmd = iwl_mvm_frob_hcmd,
 900        .frob_mem = iwl_mvm_frob_mem,
 901};
 902
 903static struct iwl_op_mode *
 904iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
 905                      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
 906{
 907        struct ieee80211_hw *hw;
 908        struct iwl_op_mode *op_mode;
 909        struct iwl_mvm *mvm;
 910        struct iwl_trans_config trans_cfg = {};
 911        static const u8 no_reclaim_cmds[] = {
 912                TX_CMD,
 913        };
 914        int err, scan_size;
 915        u32 min_backoff;
 916        enum iwl_amsdu_size rb_size_default;
 917
 918        /*
 919         * We use IWL_MVM_STATION_COUNT_MAX to check the validity of the station
 920         * index all over the driver - check that its value corresponds to the
 921         * array size.
 922         */
 923        BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
 924                     IWL_MVM_STATION_COUNT_MAX);
 925
 926        /********************************
 927         * 1. Allocating and configuring HW data
 928         ********************************/
 929        hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
 930                                sizeof(struct iwl_mvm),
 931                                &iwl_mvm_hw_ops);
 932        if (!hw)
 933                return NULL;
 934
 935        hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
 936
 937        if (cfg->max_tx_agg_size)
 938                hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
 939        else
 940                hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
 941
 942        op_mode = hw->priv;
 943
 944        mvm = IWL_OP_MODE_GET_MVM(op_mode);
 945        mvm->dev = trans->dev;
 946        mvm->trans = trans;
 947        mvm->cfg = cfg;
 948        mvm->fw = fw;
 949        mvm->hw = hw;
 950
 951        iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
 952                            &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
 953
 954        iwl_mvm_get_acpi_tables(mvm);
 955
 956        mvm->init_status = 0;
 957
 958        if (iwl_mvm_has_new_rx_api(mvm)) {
 959                op_mode->ops = &iwl_mvm_ops_mq;
 960                trans->rx_mpdu_cmd_hdr_size =
 961                        (trans->trans_cfg->device_family >=
 962                         IWL_DEVICE_FAMILY_AX210) ?
 963                        sizeof(struct iwl_rx_mpdu_desc) :
 964                        IWL_RX_DESC_SIZE_V1;
 965        } else {
 966                op_mode->ops = &iwl_mvm_ops;
 967                trans->rx_mpdu_cmd_hdr_size =
 968                        sizeof(struct iwl_rx_mpdu_res_start);
 969
 970                if (WARN_ON(trans->num_rx_queues > 1))
 971                        goto out_free;
 972        }
 973
 974        mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
 975
 976        if (iwl_mvm_has_new_tx_api(mvm)) {
 977                /*
 978                 * If we have the new TX/queue allocation API initialize them
 979                 * all to invalid numbers. We'll rewrite the ones that we need
 980                 * later, but that doesn't happen for all of them all of the
 981                 * time (e.g. P2P Device is optional), and if a dynamic queue
 982                 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
 983                 * iwl_mvm_is_static_queue() erroneously returns true, and we
 984                 * might have things getting stuck.
 985                 */
 986                mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
 987                mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
 988                mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
 989                mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
 990        } else {
 991                mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
 992                mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
 993                mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
 994                mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
 995        }
 996
 997        mvm->sf_state = SF_UNINIT;
 998        if (iwl_mvm_has_unified_ucode(mvm))
 999                iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1000        else
1001                iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1002        mvm->drop_bcn_ap_mode = true;
1003
1004        mutex_init(&mvm->mutex);
1005        spin_lock_init(&mvm->async_handlers_lock);
1006        INIT_LIST_HEAD(&mvm->time_event_list);
1007        INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1008        INIT_LIST_HEAD(&mvm->async_handlers_list);
1009        spin_lock_init(&mvm->time_event_lock);
1010        INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1011        INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1012        INIT_LIST_HEAD(&mvm->resp_pasn_list);
1013
1014        INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1015        INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1016        INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1017        INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1018        INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1019        INIT_LIST_HEAD(&mvm->add_stream_txqs);
1020
1021        init_waitqueue_head(&mvm->rx_sync_waitq);
1022
1023        mvm->queue_sync_state = 0;
1024
1025        SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1026
1027        spin_lock_init(&mvm->tcm.lock);
1028        INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1029        mvm->tcm.ts = jiffies;
1030        mvm->tcm.ll_ts = jiffies;
1031        mvm->tcm.uapsd_nonagg_ts = jiffies;
1032
1033        INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1034
1035        mvm->cmd_ver.d0i3_resp =
1036                iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, D0I3_END_CMD,
1037                                        0);
1038        /* we only support version 1 */
1039        if (WARN_ON_ONCE(mvm->cmd_ver.d0i3_resp > 1))
1040                goto out_free;
1041
1042        mvm->cmd_ver.range_resp =
1043                iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1044                                        TOF_RANGE_RESPONSE_NOTIF, 5);
1045        /* we only support up to version 9 */
1046        if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9))
1047                goto out_free;
1048
1049        /*
1050         * Populate the state variables that the transport layer needs
1051         * to know about.
1052         */
1053        trans_cfg.op_mode = op_mode;
1054        trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1055        trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1056
1057        if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1058                rb_size_default = IWL_AMSDU_2K;
1059        else
1060                rb_size_default = IWL_AMSDU_4K;
1061
1062        switch (iwlwifi_mod_params.amsdu_size) {
1063        case IWL_AMSDU_DEF:
1064                trans_cfg.rx_buf_size = rb_size_default;
1065                break;
1066        case IWL_AMSDU_4K:
1067                trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1068                break;
1069        case IWL_AMSDU_8K:
1070                trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1071                break;
1072        case IWL_AMSDU_12K:
1073                trans_cfg.rx_buf_size = IWL_AMSDU_12K;
1074                break;
1075        default:
1076                pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
1077                       iwlwifi_mod_params.amsdu_size);
1078                trans_cfg.rx_buf_size = rb_size_default;
1079        }
1080
1081        trans->wide_cmd_header = true;
1082        trans_cfg.bc_table_dword =
1083                mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210;
1084
1085        trans_cfg.command_groups = iwl_mvm_groups;
1086        trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1087
1088        trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1089        trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1090        trans_cfg.scd_set_active = true;
1091
1092        trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1093                                          driver_data[2]);
1094
1095        /* Set a short watchdog for the command queue */
1096        trans_cfg.cmd_q_wdg_timeout =
1097                iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
1098
1099        snprintf(mvm->hw->wiphy->fw_version,
1100                 sizeof(mvm->hw->wiphy->fw_version),
1101                 "%s", fw->fw_version);
1102
1103        trans_cfg.fw_reset_handshake = fw_has_capa(&mvm->fw->ucode_capa,
1104                                                   IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1105
1106        /* Configure transport layer */
1107        iwl_trans_configure(mvm->trans, &trans_cfg);
1108
1109        trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1110        trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1111        trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1112        memcpy(trans->dbg.conf_tlv, mvm->fw->dbg.conf_tlv,
1113               sizeof(trans->dbg.conf_tlv));
1114        trans->dbg.trigger_tlv = mvm->fw->dbg.trigger_tlv;
1115
1116        trans->iml = mvm->fw->iml;
1117        trans->iml_len = mvm->fw->iml_len;
1118
1119        /* set up notification wait support */
1120        iwl_notification_wait_init(&mvm->notif_wait);
1121
1122        /* Init phy db */
1123        mvm->phy_db = iwl_phy_db_init(trans);
1124        if (!mvm->phy_db) {
1125                IWL_ERR(mvm, "Cannot init phy_db\n");
1126                goto out_free;
1127        }
1128
1129        IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
1130                 mvm->trans->name, mvm->trans->hw_rev);
1131
1132        if (iwlwifi_mod_params.nvm_file)
1133                mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1134        else
1135                IWL_DEBUG_EEPROM(mvm->trans->dev,
1136                                 "working without external nvm file\n");
1137
1138        err = iwl_trans_start_hw(mvm->trans);
1139        if (err)
1140                goto out_free;
1141
1142        scan_size = iwl_mvm_scan_size(mvm);
1143
1144        mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1145        if (!mvm->scan_cmd)
1146                goto out_free;
1147
1148        /* invalidate ids to prevent accidental removal of sta_id 0 */
1149        mvm->aux_sta.sta_id = IWL_MVM_INVALID_STA;
1150        mvm->snif_sta.sta_id = IWL_MVM_INVALID_STA;
1151
1152        /* Set EBS as successful as long as not stated otherwise by the FW. */
1153        mvm->last_ebs_successful = true;
1154
1155        min_backoff = iwl_mvm_min_backoff(mvm);
1156        iwl_mvm_thermal_initialize(mvm, min_backoff);
1157
1158        if (!iwl_mvm_has_new_rx_stats_api(mvm))
1159                memset(&mvm->rx_stats_v3, 0,
1160                       sizeof(struct mvm_statistics_rx_v3));
1161        else
1162                memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1163
1164        mvm->debugfs_dir = dbgfs_dir;
1165
1166        if (iwl_mvm_start_get_nvm(mvm))
1167                goto out_thermal_exit;
1168
1169        if (iwl_mvm_start_post_nvm(mvm))
1170                goto out_thermal_exit;
1171
1172        return op_mode;
1173
1174 out_thermal_exit:
1175        iwl_mvm_thermal_exit(mvm);
1176 out_free:
1177        iwl_fw_flush_dumps(&mvm->fwrt);
1178        iwl_fw_runtime_free(&mvm->fwrt);
1179
1180        if (iwlmvm_mod_params.init_dbg)
1181                return op_mode;
1182        iwl_phy_db_free(mvm->phy_db);
1183        kfree(mvm->scan_cmd);
1184        iwl_trans_op_mode_leave(trans);
1185
1186        ieee80211_free_hw(mvm->hw);
1187        return NULL;
1188}
1189
1190void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1191{
1192        lockdep_assert_held(&mvm->mutex);
1193
1194        iwl_fw_cancel_timestamp(&mvm->fwrt);
1195
1196        clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1197
1198        iwl_fw_dbg_stop_sync(&mvm->fwrt);
1199        iwl_trans_stop_device(mvm->trans);
1200        iwl_free_fw_paging(&mvm->fwrt);
1201        iwl_fw_dump_conf_clear(&mvm->fwrt);
1202}
1203
1204static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1205{
1206        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1207        int i;
1208
1209        iwl_mvm_leds_exit(mvm);
1210
1211        iwl_mvm_thermal_exit(mvm);
1212
1213        ieee80211_unregister_hw(mvm->hw);
1214
1215        kfree(mvm->scan_cmd);
1216        kfree(mvm->mcast_filter_cmd);
1217        mvm->mcast_filter_cmd = NULL;
1218
1219        kfree(mvm->error_recovery_buf);
1220        mvm->error_recovery_buf = NULL;
1221
1222        iwl_trans_op_mode_leave(mvm->trans);
1223
1224        iwl_phy_db_free(mvm->phy_db);
1225        mvm->phy_db = NULL;
1226
1227        kfree(mvm->nvm_data);
1228        for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1229                kfree(mvm->nvm_sections[i].data);
1230
1231        cancel_delayed_work_sync(&mvm->tcm.work);
1232
1233        iwl_fw_runtime_free(&mvm->fwrt);
1234        mutex_destroy(&mvm->mutex);
1235
1236        ieee80211_free_hw(mvm->hw);
1237}
1238
1239struct iwl_async_handler_entry {
1240        struct list_head list;
1241        struct iwl_rx_cmd_buffer rxb;
1242        enum iwl_rx_handler_context context;
1243        void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1244};
1245
1246void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1247{
1248        struct iwl_async_handler_entry *entry, *tmp;
1249
1250        spin_lock_bh(&mvm->async_handlers_lock);
1251        list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1252                iwl_free_rxb(&entry->rxb);
1253                list_del(&entry->list);
1254                kfree(entry);
1255        }
1256        spin_unlock_bh(&mvm->async_handlers_lock);
1257}
1258
1259static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1260{
1261        struct iwl_mvm *mvm =
1262                container_of(wk, struct iwl_mvm, async_handlers_wk);
1263        struct iwl_async_handler_entry *entry, *tmp;
1264        LIST_HEAD(local_list);
1265
1266        /* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
1267
1268        /*
1269         * Sync with Rx path with a lock. Remove all the entries from this list,
1270         * add them to a local one (lock free), and then handle them.
1271         */
1272        spin_lock_bh(&mvm->async_handlers_lock);
1273        list_splice_init(&mvm->async_handlers_list, &local_list);
1274        spin_unlock_bh(&mvm->async_handlers_lock);
1275
1276        list_for_each_entry_safe(entry, tmp, &local_list, list) {
1277                if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1278                        mutex_lock(&mvm->mutex);
1279                entry->fn(mvm, &entry->rxb);
1280                iwl_free_rxb(&entry->rxb);
1281                list_del(&entry->list);
1282                if (entry->context == RX_HANDLER_ASYNC_LOCKED)
1283                        mutex_unlock(&mvm->mutex);
1284                kfree(entry);
1285        }
1286}
1287
1288static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1289                                            struct iwl_rx_packet *pkt)
1290{
1291        struct iwl_fw_dbg_trigger_tlv *trig;
1292        struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1293        int i;
1294
1295        trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1296                                     FW_DBG_TRIGGER_FW_NOTIF);
1297        if (!trig)
1298                return;
1299
1300        cmds_trig = (void *)trig->data;
1301
1302        for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1303                /* don't collect on CMD 0 */
1304                if (!cmds_trig->cmds[i].cmd_id)
1305                        break;
1306
1307                if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1308                    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1309                        continue;
1310
1311                iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1312                                        "CMD 0x%02x.%02x received",
1313                                        pkt->hdr.group_id, pkt->hdr.cmd);
1314                break;
1315        }
1316}
1317
1318static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1319                              struct iwl_rx_cmd_buffer *rxb,
1320                              struct iwl_rx_packet *pkt)
1321{
1322        unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1323        int i;
1324        union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1325
1326        iwl_dbg_tlv_time_point(&mvm->fwrt,
1327                               IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1328        iwl_mvm_rx_check_trigger(mvm, pkt);
1329
1330        /*
1331         * Do the notification wait before RX handlers so
1332         * even if the RX handler consumes the RXB we have
1333         * access to it in the notification wait entry.
1334         */
1335        iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1336
1337        for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1338                const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1339                struct iwl_async_handler_entry *entry;
1340
1341                if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1342                        continue;
1343
1344                if (unlikely(pkt_len < rx_h->min_size))
1345                        return;
1346
1347                if (rx_h->context == RX_HANDLER_SYNC) {
1348                        rx_h->fn(mvm, rxb);
1349                        return;
1350                }
1351
1352                entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1353                /* we can't do much... */
1354                if (!entry)
1355                        return;
1356
1357                entry->rxb._page = rxb_steal_page(rxb);
1358                entry->rxb._offset = rxb->_offset;
1359                entry->rxb._rx_page_order = rxb->_rx_page_order;
1360                entry->fn = rx_h->fn;
1361                entry->context = rx_h->context;
1362                spin_lock(&mvm->async_handlers_lock);
1363                list_add_tail(&entry->list, &mvm->async_handlers_list);
1364                spin_unlock(&mvm->async_handlers_lock);
1365                schedule_work(&mvm->async_handlers_wk);
1366                break;
1367        }
1368}
1369
1370static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1371                       struct napi_struct *napi,
1372                       struct iwl_rx_cmd_buffer *rxb)
1373{
1374        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1375        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1376        u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1377
1378        if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1379                iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1380        else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1381                iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1382        else
1383                iwl_mvm_rx_common(mvm, rxb, pkt);
1384}
1385
1386void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1387                   struct napi_struct *napi,
1388                   struct iwl_rx_cmd_buffer *rxb)
1389{
1390        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1391        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1392        u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1393
1394        if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1395                iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1396        else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1397                                         RX_QUEUES_NOTIFICATION)))
1398                iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1399        else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1400                iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1401        else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1402                iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1403        else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1404                iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1405        else
1406                iwl_mvm_rx_common(mvm, rxb, pkt);
1407}
1408
1409static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1410                             const struct iwl_device_cmd *cmd)
1411{
1412        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1413
1414        /*
1415         * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1416         * commands that need to block the Tx queues.
1417         */
1418        iwl_trans_block_txq_ptrs(mvm->trans, false);
1419}
1420
1421static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1422{
1423        return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1424                queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1425}
1426
1427static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1428                                       int hw_queue, bool start)
1429{
1430        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1431        struct ieee80211_sta *sta;
1432        struct ieee80211_txq *txq;
1433        struct iwl_mvm_txq *mvmtxq;
1434        int i;
1435        unsigned long tid_bitmap;
1436        struct iwl_mvm_sta *mvmsta;
1437        u8 sta_id;
1438
1439        sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1440                mvm->tvqm_info[hw_queue].sta_id :
1441                mvm->queue_info[hw_queue].ra_sta_id;
1442
1443        if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1444                return;
1445
1446        rcu_read_lock();
1447
1448        sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1449        if (IS_ERR_OR_NULL(sta))
1450                goto out;
1451        mvmsta = iwl_mvm_sta_from_mac80211(sta);
1452
1453        if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1454                if (!start)
1455                        ieee80211_stop_queues(mvm->hw);
1456                else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1457                        ieee80211_wake_queues(mvm->hw);
1458
1459                goto out;
1460        }
1461
1462        if (iwl_mvm_has_new_tx_api(mvm)) {
1463                int tid = mvm->tvqm_info[hw_queue].txq_tid;
1464
1465                tid_bitmap = BIT(tid);
1466        } else {
1467                tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1468        }
1469
1470        for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1471                int tid = i;
1472
1473                if (tid == IWL_MAX_TID_COUNT)
1474                        tid = IEEE80211_NUM_TIDS;
1475
1476                txq = sta->txq[tid];
1477                mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1478                mvmtxq->stopped = !start;
1479
1480                if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1481                        iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1482        }
1483
1484out:
1485        rcu_read_unlock();
1486}
1487
1488static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1489{
1490        iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1491}
1492
1493static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1494{
1495        iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1496}
1497
1498static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1499{
1500        bool state = iwl_mvm_is_radio_killed(mvm);
1501
1502        if (state)
1503                wake_up(&mvm->rx_sync_waitq);
1504
1505        wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1506}
1507
1508void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1509{
1510        if (state)
1511                set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1512        else
1513                clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1514
1515        iwl_mvm_set_rfkill_state(mvm);
1516}
1517
1518static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1519{
1520        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1521        bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1522        bool unified = iwl_mvm_has_unified_ucode(mvm);
1523
1524        if (state)
1525                set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1526        else
1527                clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1528
1529        iwl_mvm_set_rfkill_state(mvm);
1530
1531         /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1532        if (rfkill_safe_init_done)
1533                iwl_abort_notification_waits(&mvm->notif_wait);
1534
1535        /*
1536         * Don't ask the transport to stop the firmware. We'll do it
1537         * after cfg80211 takes us down.
1538         */
1539        if (unified)
1540                return false;
1541
1542        /*
1543         * Stop the device if we run OPERATIONAL firmware or if we are in the
1544         * middle of the calibrations.
1545         */
1546        return state && rfkill_safe_init_done;
1547}
1548
1549static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1550{
1551        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1552        struct ieee80211_tx_info *info;
1553
1554        info = IEEE80211_SKB_CB(skb);
1555        iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1556        ieee80211_free_txskb(mvm->hw, skb);
1557}
1558
1559struct iwl_mvm_reprobe {
1560        struct device *dev;
1561        struct work_struct work;
1562};
1563
1564static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1565{
1566        struct iwl_mvm_reprobe *reprobe;
1567
1568        reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1569        if (device_reprobe(reprobe->dev))
1570                dev_err(reprobe->dev, "reprobe failed!\n");
1571        put_device(reprobe->dev);
1572        kfree(reprobe);
1573        module_put(THIS_MODULE);
1574}
1575
1576void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
1577{
1578        iwl_abort_notification_waits(&mvm->notif_wait);
1579        iwl_dbg_tlv_del_timers(mvm->trans);
1580
1581        /*
1582         * This is a bit racy, but worst case we tell mac80211 about
1583         * a stopped/aborted scan when that was already done which
1584         * is not a problem. It is necessary to abort any os scan
1585         * here because mac80211 requires having the scan cleared
1586         * before restarting.
1587         * We'll reset the scan_status to NONE in restart cleanup in
1588         * the next start() call from mac80211. If restart isn't called
1589         * (no fw restart) scan status will stay busy.
1590         */
1591        iwl_mvm_report_scan_aborted(mvm);
1592
1593        /*
1594         * If we're restarting already, don't cycle restarts.
1595         * If INIT fw asserted, it will likely fail again.
1596         * If WoWLAN fw asserted, don't restart either, mac80211
1597         * can't recover this since we're already half suspended.
1598         */
1599        if (!mvm->fw_restart && fw_error) {
1600                iwl_fw_error_collect(&mvm->fwrt, false);
1601        } else if (test_bit(IWL_MVM_STATUS_STARTING,
1602                            &mvm->status)) {
1603                IWL_ERR(mvm, "Starting mac, retry will be triggered anyway\n");
1604        } else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1605                struct iwl_mvm_reprobe *reprobe;
1606
1607                IWL_ERR(mvm,
1608                        "Firmware error during reconfiguration - reprobe!\n");
1609
1610                /*
1611                 * get a module reference to avoid doing this while unloading
1612                 * anyway and to avoid scheduling a work with code that's
1613                 * being removed.
1614                 */
1615                if (!try_module_get(THIS_MODULE)) {
1616                        IWL_ERR(mvm, "Module is being unloaded - abort\n");
1617                        return;
1618                }
1619
1620                reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1621                if (!reprobe) {
1622                        module_put(THIS_MODULE);
1623                        return;
1624                }
1625                reprobe->dev = get_device(mvm->trans->dev);
1626                INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1627                schedule_work(&reprobe->work);
1628        } else if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1629                            &mvm->status)) {
1630                IWL_ERR(mvm, "HW restart already requested, but not started\n");
1631        } else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
1632                   mvm->hw_registered &&
1633                   !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
1634                /* This should be first thing before trying to collect any
1635                 * data to avoid endless loops if any HW error happens while
1636                 * collecting debug data.
1637                 */
1638                set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1639
1640                if (mvm->fw->ucode_capa.error_log_size) {
1641                        u32 src_size = mvm->fw->ucode_capa.error_log_size;
1642                        u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
1643                        u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
1644
1645                        if (recover_buf) {
1646                                mvm->error_recovery_buf = recover_buf;
1647                                iwl_trans_read_mem_bytes(mvm->trans,
1648                                                         src_addr,
1649                                                         recover_buf,
1650                                                         src_size);
1651                        }
1652                }
1653
1654                iwl_fw_error_collect(&mvm->fwrt, false);
1655
1656                if (fw_error && mvm->fw_restart > 0)
1657                        mvm->fw_restart--;
1658                ieee80211_restart_hw(mvm->hw);
1659        }
1660}
1661
1662static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode, bool sync)
1663{
1664        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1665
1666        if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status))
1667                iwl_mvm_dump_nic_error_log(mvm);
1668
1669        if (sync) {
1670                iwl_fw_error_collect(&mvm->fwrt, true);
1671                /*
1672                 * Currently, the only case for sync=true is during
1673                 * shutdown, so just stop in this case. If/when that
1674                 * changes, we need to be a bit smarter here.
1675                 */
1676                return;
1677        }
1678
1679        /*
1680         * If the firmware crashes while we're already considering it
1681         * to be dead then don't ask for a restart, that cannot do
1682         * anything useful anyway.
1683         */
1684        if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
1685                return;
1686
1687        iwl_mvm_nic_restart(mvm, true);
1688}
1689
1690static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1691{
1692        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1693
1694        WARN_ON(1);
1695        iwl_mvm_nic_restart(mvm, true);
1696}
1697
1698static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
1699                                       enum iwl_fw_ini_time_point tp_id,
1700                                       union iwl_dbg_tlv_tp_data *tp_data)
1701{
1702        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1703
1704        iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
1705}
1706
1707#define IWL_MVM_COMMON_OPS                                      \
1708        /* these could be differentiated */                     \
1709        .async_cb = iwl_mvm_async_cb,                           \
1710        .queue_full = iwl_mvm_stop_sw_queue,                    \
1711        .queue_not_full = iwl_mvm_wake_sw_queue,                \
1712        .hw_rf_kill = iwl_mvm_set_hw_rfkill_state,              \
1713        .free_skb = iwl_mvm_free_skb,                           \
1714        .nic_error = iwl_mvm_nic_error,                         \
1715        .cmd_queue_full = iwl_mvm_cmd_queue_full,               \
1716        .nic_config = iwl_mvm_nic_config,                       \
1717        /* as we only register one, these MUST be common! */    \
1718        .start = iwl_op_mode_mvm_start,                         \
1719        .stop = iwl_op_mode_mvm_stop,                           \
1720        .time_point = iwl_op_mode_mvm_time_point
1721
1722static const struct iwl_op_mode_ops iwl_mvm_ops = {
1723        IWL_MVM_COMMON_OPS,
1724        .rx = iwl_mvm_rx,
1725};
1726
1727static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1728                              struct napi_struct *napi,
1729                              struct iwl_rx_cmd_buffer *rxb,
1730                              unsigned int queue)
1731{
1732        struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1733        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1734        u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1735
1736        if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
1737                iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
1738        else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1739                                         RX_QUEUES_NOTIFICATION)))
1740                iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
1741        else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1742                iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
1743}
1744
1745static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1746        IWL_MVM_COMMON_OPS,
1747        .rx = iwl_mvm_rx_mq,
1748        .rx_rss = iwl_mvm_rx_mq_rss,
1749};
1750