File Index
Files and directories
Sign in
FILE

ops.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/intel/iwlwifi/mvm/ops.c C
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2025 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/rtnetlink.h>
   9#include <linux/vmalloc.h>
  10#include <net/mac80211.h>
  11
  12#include "fw/notif-wait.h"
  13#include "iwl-trans.h"
  14#include "iwl-op-mode.h"
  15#include "fw/img.h"
  16#include "iwl-debug.h"
  17#include "iwl-drv.h"
  18#include "iwl-modparams.h"
  19#include "mvm.h"
  20#include "iwl-phy-db.h"
  21#include "iwl-nvm-utils.h"
  22#include "iwl-csr.h"
  23#include "iwl-io.h"
  24#include "iwl-prph.h"
  25#include "rs.h"
  26#include "fw/api/scan.h"
  27#include "fw/api/rfi.h"
  28#include "time-event.h"
  29#include "fw-api.h"
  30#include "fw/acpi.h"
  31#include "fw/uefi.h"
  32#include "time-sync.h"
  33
  34#define DRV_DESCRIPTION	"The new Intel(R) wireless AGN driver for Linux"
  35MODULE_DESCRIPTION(DRV_DESCRIPTION);
  36MODULE_LICENSE("GPL");
  37MODULE_IMPORT_NS("IWLWIFI");
  38
  39static const struct iwl_op_mode_ops iwl_mvm_ops;
  40static const struct iwl_op_mode_ops iwl_mvm_ops_mq;
  41
  42struct iwl_mvm_mod_params iwlmvm_mod_params = {
  43	.power_scheme = IWL_POWER_SCHEME_BPS,
  44};
  45
  46module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, 0444);
  47MODULE_PARM_DESC(power_scheme,
  48		 "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
  49
  50/*
  51 * module init and exit functions
  52 */
  53static int __init iwl_mvm_init(void)
  54{
  55	int ret;
  56
  57	ret = iwl_mvm_rate_control_register();
  58	if (ret) {
  59		pr_err("Unable to register rate control algorithm: %d\n", ret);
  60		return ret;
  61	}
  62
  63	ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
  64	if (ret) {
  65		pr_err("Unable to register MVM op_mode: %d\n", ret);
  66		iwl_mvm_rate_control_unregister();
  67	}
  68
  69	return ret;
  70}
  71module_init(iwl_mvm_init);
  72
  73static void __exit iwl_mvm_exit(void)
  74{
  75	iwl_opmode_deregister("iwlmvm");
  76	iwl_mvm_rate_control_unregister();
  77}
  78module_exit(iwl_mvm_exit);
  79
  80static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
  81{
  82	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  83	u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
  84	u32 reg_val;
  85	u32 phy_config = iwl_mvm_get_phy_config(mvm);
  86
  87	radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
  88			 FW_PHY_CFG_RADIO_TYPE_POS;
  89	radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
  90			 FW_PHY_CFG_RADIO_STEP_POS;
  91	radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
  92			 FW_PHY_CFG_RADIO_DASH_POS;
  93
  94	IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
  95		       radio_cfg_step, radio_cfg_dash);
  96
  97	if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
  98		return;
  99
 100	/* SKU control */
 101	reg_val = CSR_HW_REV_STEP_DASH(mvm->trans->info.hw_rev);
 102
 103	/* radio configuration */
 104	reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
 105	reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
 106	reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
 107
 108	WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
 109		 ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
 110
 111	/*
 112	 * TODO: Bits 7-8 of CSR in 8000 HW family and higher set the ADC
 113	 * sampling, and shouldn't be set to any non-zero value.
 114	 * The same is supposed to be true of the other HW, but unsetting
 115	 * them (such as the 7260) causes automatic tests to fail on seemingly
 116	 * unrelated errors. Need to further investigate this, but for now
 117	 * we'll separate cases.
 118	 */
 119	if (mvm->trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_8000)
 120		reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
 121
 122	if (iwl_fw_dbg_is_d3_debug_enabled(&mvm->fwrt))
 123		reg_val |= CSR_HW_IF_CONFIG_REG_D3_DEBUG;
 124
 125	iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
 126				CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP_DASH |
 127				CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
 128				CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
 129				CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
 130				CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
 131				CSR_HW_IF_CONFIG_REG_BIT_MAC_SI   |
 132				CSR_HW_IF_CONFIG_REG_D3_DEBUG,
 133				reg_val);
 134
 135	/*
 136	 * W/A : NIC is stuck in a reset state after Early PCIe power off
 137	 * (PCIe power is lost before PERST# is asserted), causing ME FW
 138	 * to lose ownership and not being able to obtain it back.
 139	 */
 140	if (!mvm->trans->mac_cfg->base->apmg_not_supported)
 141		iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
 142				       APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
 143				       ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
 144}
 145
 146static void iwl_mvm_rx_monitor_notif(struct iwl_mvm *mvm,
 147				     struct iwl_rx_cmd_buffer *rxb)
 148{
 149	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 150	struct iwl_datapath_monitor_notif *notif = (void *)pkt->data;
 151	struct ieee80211_supported_band *sband;
 152	const struct ieee80211_sta_he_cap *he_cap;
 153	struct ieee80211_vif *vif;
 154
 155	if (notif->type != cpu_to_le32(IWL_DP_MON_NOTIF_TYPE_EXT_CCA))
 156		return;
 157
 158	/* FIXME: should fetch the link and not the vif */
 159	vif = iwl_mvm_get_vif_by_macid(mvm, notif->link_id);
 160	if (!vif || vif->type != NL80211_IFTYPE_STATION)
 161		return;
 162
 163	if (!vif->bss_conf.chanreq.oper.chan ||
 164	    vif->bss_conf.chanreq.oper.chan->band != NL80211_BAND_2GHZ ||
 165	    vif->bss_conf.chanreq.oper.width < NL80211_CHAN_WIDTH_40)
 166		return;
 167
 168	if (!vif->cfg.assoc)
 169		return;
 170
 171	/* this shouldn't happen *again*, ignore it */
 172	if (mvm->cca_40mhz_workaround)
 173		return;
 174
 175	/*
 176	 * We'll decrement this on disconnect - so set to 2 since we'll
 177	 * still have to disconnect from the current AP first.
 178	 */
 179	mvm->cca_40mhz_workaround = 2;
 180
 181	/*
 182	 * This capability manipulation isn't really ideal, but it's the
 183	 * easiest choice - otherwise we'd have to do some major changes
 184	 * in mac80211 to support this, which isn't worth it. This does
 185	 * mean that userspace may have outdated information, but that's
 186	 * actually not an issue at all.
 187	 */
 188	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
 189
 190	WARN_ON(!sband->ht_cap.ht_supported);
 191	WARN_ON(!(sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40));
 192	sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
 193
 194	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
 195
 196	if (he_cap) {
 197		/* we know that ours is writable */
 198		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
 199
 200		WARN_ON(!he->has_he);
 201		WARN_ON(!(he->he_cap_elem.phy_cap_info[0] &
 202				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G));
 203		he->he_cap_elem.phy_cap_info[0] &=
 204			~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
 205	}
 206
 207	ieee80211_disconnect(vif, true);
 208}
 209
 210void iwl_mvm_update_link_smps(struct ieee80211_vif *vif,
 211			      struct ieee80211_bss_conf *link_conf)
 212{
 213	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 214	struct iwl_mvm *mvm = mvmvif->mvm;
 215	enum ieee80211_smps_mode mode = IEEE80211_SMPS_AUTOMATIC;
 216
 217	if (!link_conf)
 218		return;
 219
 220	if (mvm->fw_static_smps_request &&
 221	    link_conf->chanreq.oper.width == NL80211_CHAN_WIDTH_160 &&
 222	    link_conf->he_support)
 223		mode = IEEE80211_SMPS_STATIC;
 224
 225	iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_FW, mode,
 226			    link_conf->link_id);
 227}
 228
 229static void iwl_mvm_intf_dual_chain_req(void *data, u8 *mac,
 230					struct ieee80211_vif *vif)
 231{
 232	struct ieee80211_bss_conf *link_conf;
 233	unsigned int link_id;
 234
 235	rcu_read_lock();
 236
 237	for_each_vif_active_link(vif, link_conf, link_id)
 238		iwl_mvm_update_link_smps(vif, link_conf);
 239
 240	rcu_read_unlock();
 241}
 242
 243static void iwl_mvm_rx_thermal_dual_chain_req(struct iwl_mvm *mvm,
 244					      struct iwl_rx_cmd_buffer *rxb)
 245{
 246	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 247	struct iwl_thermal_dual_chain_request *req = (void *)pkt->data;
 248
 249	/* firmware is expected to handle that in RLC offload mode */
 250	if (IWL_FW_CHECK(mvm, iwl_mvm_has_rlc_offload(mvm),
 251			 "Got THERMAL_DUAL_CHAIN_REQUEST (0x%x) in RLC offload mode\n",
 252			 req->event))
 253		return;
 254
 255	/*
 256	 * We could pass it to the iterator data, but also need to remember
 257	 * it for new interfaces that are added while in this state.
 258	 */
 259	mvm->fw_static_smps_request =
 260		req->event == cpu_to_le32(THERMAL_DUAL_CHAIN_REQ_DISABLE);
 261	ieee80211_iterate_interfaces(mvm->hw,
 262				     IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER,
 263				     iwl_mvm_intf_dual_chain_req, NULL);
 264}
 265
 266/**
 267 * enum iwl_rx_handler_context: context for Rx handler
 268 * @RX_HANDLER_SYNC : this means that it will be called in the Rx path
 269 *	which can't acquire mvm->mutex.
 270 * @RX_HANDLER_ASYNC_LOCKED : If the handler needs to hold mvm->mutex
 271 *	(and only in this case!), it should be set as ASYNC. In that case,
 272 *	it will be called from a worker with mvm->mutex held.
 273 * @RX_HANDLER_ASYNC_UNLOCKED : in case the handler needs to lock the
 274 *	mutex itself, it will be called from a worker without mvm->mutex held.
 275 * @RX_HANDLER_ASYNC_LOCKED_WIPHY: If the handler needs to hold the wiphy lock
 276 *	and mvm->mutex. Will be handled with the wiphy_work queue infra
 277 *	instead of regular work queue.
 278 */
 279enum iwl_rx_handler_context {
 280	RX_HANDLER_SYNC,
 281	RX_HANDLER_ASYNC_LOCKED,
 282	RX_HANDLER_ASYNC_UNLOCKED,
 283	RX_HANDLER_ASYNC_LOCKED_WIPHY,
 284};
 285
 286/**
 287 * struct iwl_rx_handlers: handler for FW notification
 288 * @cmd_id: command id
 289 * @min_size: minimum size to expect for the notification
 290 * @context: see &iwl_rx_handler_context
 291 * @fn: the function is called when notification is received
 292 */
 293struct iwl_rx_handlers {
 294	u16 cmd_id, min_size;
 295	enum iwl_rx_handler_context context;
 296	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
 297};
 298
 299#define RX_HANDLER_NO_SIZE(_cmd_id, _fn, _context)		\
 300	{ .cmd_id = _cmd_id, .fn = _fn, .context = _context, }
 301#define RX_HANDLER_GRP_NO_SIZE(_grp, _cmd, _fn, _context)	\
 302	{ .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn, .context = _context, }
 303#define RX_HANDLER(_cmd_id, _fn, _context, _struct)		\
 304	{ .cmd_id = _cmd_id, .fn = _fn,				\
 305	  .context = _context, .min_size = sizeof(_struct), }
 306#define RX_HANDLER_GRP(_grp, _cmd, _fn, _context, _struct)	\
 307	{ .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn,		\
 308	  .context = _context, .min_size = sizeof(_struct), }
 309
 310/*
 311 * Handlers for fw notifications
 312 * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
 313 * This list should be in order of frequency for performance purposes.
 314 *
 315 * The handler can be one from three contexts, see &iwl_rx_handler_context
 316 */
 317static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
 318	RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, RX_HANDLER_SYNC,
 319		   struct iwl_tx_resp),
 320	RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, RX_HANDLER_SYNC,
 321		   struct iwl_mvm_ba_notif),
 322
 323	RX_HANDLER_GRP(DATA_PATH_GROUP, TLC_MNG_UPDATE_NOTIF,
 324		       iwl_mvm_tlc_update_notif, RX_HANDLER_SYNC,
 325		       struct iwl_tlc_update_notif),
 326
 327	RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_old_notif,
 328		   RX_HANDLER_ASYNC_LOCKED_WIPHY,
 329		   struct iwl_bt_coex_prof_old_notif),
 330	RX_HANDLER_NO_SIZE(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif,
 331			   RX_HANDLER_ASYNC_LOCKED),
 332	RX_HANDLER_NO_SIZE(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics,
 333			   RX_HANDLER_ASYNC_LOCKED),
 334
 335	RX_HANDLER_GRP(STATISTICS_GROUP, STATISTICS_OPER_NOTIF,
 336		       iwl_mvm_handle_rx_system_oper_stats,
 337		       RX_HANDLER_ASYNC_LOCKED_WIPHY,
 338		       struct iwl_system_statistics_notif_oper),
 339	RX_HANDLER_GRP(STATISTICS_GROUP, STATISTICS_OPER_PART1_NOTIF,
 340		       iwl_mvm_handle_rx_system_oper_part1_stats,
 341		       RX_HANDLER_ASYNC_LOCKED,
 342		       struct iwl_system_statistics_part1_notif_oper),
 343	RX_HANDLER_GRP(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF,
 344		       iwl_mvm_handle_rx_system_end_stats_notif,
 345		       RX_HANDLER_ASYNC_LOCKED,
 346		       struct iwl_system_statistics_end_notif),
 347
 348	RX_HANDLER(BA_WINDOW_STATUS_NOTIFICATION_ID,
 349		   iwl_mvm_window_status_notif, RX_HANDLER_SYNC,
 350		   struct iwl_ba_window_status_notif),
 351
 352	RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif,
 353		   RX_HANDLER_SYNC, struct iwl_time_event_notif),
 354	RX_HANDLER_GRP(MAC_CONF_GROUP, SESSION_PROTECTION_NOTIF,
 355		       iwl_mvm_rx_session_protect_notif, RX_HANDLER_SYNC,
 356		       struct iwl_session_prot_notif),
 357	RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc,
 358		   RX_HANDLER_ASYNC_LOCKED, struct iwl_mcc_chub_notif),
 359
 360	RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, RX_HANDLER_SYNC,
 361		   struct iwl_mvm_eosp_notification),
 362
 363	RX_HANDLER(SCAN_ITERATION_COMPLETE,
 364		   iwl_mvm_rx_lmac_scan_iter_complete_notif, RX_HANDLER_SYNC,
 365		   struct iwl_lmac_scan_complete_notif),
 366	RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
 367		   iwl_mvm_rx_lmac_scan_complete_notif,
 368		   RX_HANDLER_ASYNC_LOCKED, struct iwl_periodic_scan_complete),
 369	RX_HANDLER_NO_SIZE(MATCH_FOUND_NOTIFICATION,
 370			   iwl_mvm_rx_scan_match_found,
 371			   RX_HANDLER_SYNC),
 372	RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
 373		   RX_HANDLER_ASYNC_LOCKED,
 374		   struct iwl_umac_scan_complete),
 375	RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
 376		   iwl_mvm_rx_umac_scan_iter_complete_notif, RX_HANDLER_SYNC,
 377		   struct iwl_umac_scan_iter_complete_notif),
 378
 379	RX_HANDLER(MISSED_BEACONS_NOTIFICATION,
 380		   iwl_mvm_rx_missed_beacons_notif_legacy,
 381		   RX_HANDLER_ASYNC_LOCKED_WIPHY,
 382		   struct iwl_missed_beacons_notif_v4),
 383
 384	RX_HANDLER_GRP(MAC_CONF_GROUP, MISSED_BEACONS_NOTIF,
 385		       iwl_mvm_rx_missed_beacons_notif,
 386		       RX_HANDLER_ASYNC_LOCKED_WIPHY,
 387		       struct iwl_missed_beacons_notif),
 388	RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, RX_HANDLER_SYNC,
 389		   struct iwl_error_resp),
 390	RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
 391		   iwl_mvm_power_uapsd_misbehaving_ap_notif, RX_HANDLER_SYNC,
 392		   struct iwl_uapsd_misbehaving_ap_notif),
 393	RX_HANDLER_NO_SIZE(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif,
 394			   RX_HANDLER_ASYNC_LOCKED),
 395	RX_HANDLER_GRP_NO_SIZE(PHY_OPS_GROUP, DTS_MEASUREMENT_NOTIF_WIDE,
 396			       iwl_mvm_temp_notif, RX_HANDLER_ASYNC_UNLOCKED),
 397	RX_HANDLER_GRP(PHY_OPS_GROUP, CT_KILL_NOTIFICATION,
 398		       iwl_mvm_ct_kill_notif, RX_HANDLER_SYNC,
 399		       struct ct_kill_notif),
 400
 401	RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
 402		   RX_HANDLER_ASYNC_LOCKED,
 403		   struct iwl_tdls_channel_switch_notif),
 404	RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif,
 405		   RX_HANDLER_SYNC, struct iwl_mfuart_load_notif_v1),
 406	RX_HANDLER_GRP(LOCATION_GROUP, TOF_RESPONDER_STATS,
 407		       iwl_mvm_ftm_responder_stats, RX_HANDLER_ASYNC_LOCKED,
 408		       struct iwl_ftm_responder_stats),
 409
 410	RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_RANGE_RESPONSE_NOTIF,
 411			       iwl_mvm_ftm_range_resp, RX_HANDLER_ASYNC_LOCKED),
 412	RX_HANDLER_GRP_NO_SIZE(LOCATION_GROUP, TOF_LC_NOTIF,
 413			       iwl_mvm_ftm_lc_notif, RX_HANDLER_ASYNC_LOCKED),
 414
 415	RX_HANDLER_GRP(DEBUG_GROUP, MFU_ASSERT_DUMP_NTF,
 416		       iwl_mvm_mfu_assert_dump_notif, RX_HANDLER_SYNC,
 417		       struct iwl_mfu_assert_dump_notif),
 418	RX_HANDLER_GRP(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF,
 419		       iwl_mvm_rx_stored_beacon_notif, RX_HANDLER_SYNC,
 420		       struct iwl_stored_beacon_notif_v2),
 421	RX_HANDLER_GRP(DATA_PATH_GROUP, MU_GROUP_MGMT_NOTIF,
 422		       iwl_mvm_mu_mimo_grp_notif, RX_HANDLER_SYNC,
 423		       struct iwl_mu_group_mgmt_notif),
 424	RX_HANDLER_GRP(DATA_PATH_GROUP, STA_PM_NOTIF,
 425		       iwl_mvm_sta_pm_notif, RX_HANDLER_SYNC,
 426		       struct iwl_mvm_pm_state_notification),
 427	RX_HANDLER_GRP(MAC_CONF_GROUP, PROBE_RESPONSE_DATA_NOTIF,
 428		       iwl_mvm_probe_resp_data_notif,
 429		       RX_HANDLER_ASYNC_LOCKED,
 430		       struct iwl_probe_resp_data_notif),
 431	RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_START_NOTIF,
 432		       iwl_mvm_channel_switch_start_notif,
 433		       RX_HANDLER_SYNC, struct iwl_channel_switch_start_notif),
 434	RX_HANDLER_GRP(MAC_CONF_GROUP, CHANNEL_SWITCH_ERROR_NOTIF,
 435		       iwl_mvm_channel_switch_error_notif,
 436		       RX_HANDLER_ASYNC_UNLOCKED,
 437		       struct iwl_channel_switch_error_notif),
 438
 439	RX_HANDLER_GRP(DATA_PATH_GROUP, MONITOR_NOTIF,
 440		       iwl_mvm_rx_monitor_notif, RX_HANDLER_ASYNC_LOCKED,
 441		       struct iwl_datapath_monitor_notif),
 442
 443	RX_HANDLER_GRP(DATA_PATH_GROUP, THERMAL_DUAL_CHAIN_REQUEST,
 444		       iwl_mvm_rx_thermal_dual_chain_req,
 445		       RX_HANDLER_ASYNC_LOCKED,
 446		       struct iwl_thermal_dual_chain_request),
 447
 448	RX_HANDLER_GRP(SYSTEM_GROUP, RFI_DEACTIVATE_NOTIF,
 449		       iwl_rfi_deactivate_notif_handler, RX_HANDLER_ASYNC_UNLOCKED,
 450		       struct iwl_rfi_deactivate_notif),
 451
 452	RX_HANDLER_GRP(LEGACY_GROUP,
 453		       WNM_80211V_TIMING_MEASUREMENT_NOTIFICATION,
 454		       iwl_mvm_time_sync_msmt_event, RX_HANDLER_SYNC,
 455		       struct iwl_time_msmt_notify),
 456	RX_HANDLER_GRP(LEGACY_GROUP,
 457		       WNM_80211V_TIMING_MEASUREMENT_CONFIRM_NOTIFICATION,
 458		       iwl_mvm_time_sync_msmt_confirm_event, RX_HANDLER_SYNC,
 459		       struct iwl_time_msmt_cfm_notify),
 460	RX_HANDLER_GRP(MAC_CONF_GROUP, ROC_NOTIF,
 461		       iwl_mvm_rx_roc_notif, RX_HANDLER_ASYNC_LOCKED,
 462		       struct iwl_roc_notif),
 463	RX_HANDLER_GRP(SCAN_GROUP, CHANNEL_SURVEY_NOTIF,
 464		       iwl_mvm_rx_channel_survey_notif, RX_HANDLER_ASYNC_LOCKED,
 465		       struct iwl_umac_scan_channel_survey_notif),
 466	RX_HANDLER_GRP(DATA_PATH_GROUP, BEACON_FILTER_IN_NOTIF,
 467		       iwl_mvm_rx_beacon_filter_notif,
 468		       RX_HANDLER_ASYNC_LOCKED,
 469		       /* same size as v1 */
 470		       struct iwl_beacon_filter_notif),
 471};
 472#undef RX_HANDLER
 473#undef RX_HANDLER_GRP
 474
 475/* Please keep this array *SORTED* by hex value.
 476 * Access is done through binary search
 477 */
 478static const struct iwl_hcmd_names iwl_mvm_legacy_names[] = {
 479	HCMD_NAME(UCODE_ALIVE_NTFY),
 480	HCMD_NAME(REPLY_ERROR),
 481	HCMD_NAME(ECHO_CMD),
 482	HCMD_NAME(INIT_COMPLETE_NOTIF),
 483	HCMD_NAME(PHY_CONTEXT_CMD),
 484	HCMD_NAME(DBG_CFG),
 485	HCMD_NAME(SCAN_CFG_CMD),
 486	HCMD_NAME(SCAN_REQ_UMAC),
 487	HCMD_NAME(SCAN_ABORT_UMAC),
 488	HCMD_NAME(SCAN_COMPLETE_UMAC),
 489	HCMD_NAME(BA_WINDOW_STATUS_NOTIFICATION_ID),
 490	HCMD_NAME(ADD_STA_KEY),
 491	HCMD_NAME(ADD_STA),
 492	HCMD_NAME(REMOVE_STA),
 493	HCMD_NAME(TX_CMD),
 494	HCMD_NAME(SCD_QUEUE_CFG),
 495	HCMD_NAME(TXPATH_FLUSH),
 496	HCMD_NAME(MGMT_MCAST_KEY),
 497	HCMD_NAME(WEP_KEY),
 498	HCMD_NAME(SHARED_MEM_CFG),
 499	HCMD_NAME(TDLS_CHANNEL_SWITCH_CMD),
 500	HCMD_NAME(MAC_CONTEXT_CMD),
 501	HCMD_NAME(TIME_EVENT_CMD),
 502	HCMD_NAME(TIME_EVENT_NOTIFICATION),
 503	HCMD_NAME(BINDING_CONTEXT_CMD),
 504	HCMD_NAME(TIME_QUOTA_CMD),
 505	HCMD_NAME(NON_QOS_TX_COUNTER_CMD),
 506	HCMD_NAME(LEDS_CMD),
 507	HCMD_NAME(LQ_CMD),
 508	HCMD_NAME(FW_PAGING_BLOCK_CMD),
 509	HCMD_NAME(SCAN_OFFLOAD_REQUEST_CMD),
 510	HCMD_NAME(SCAN_OFFLOAD_ABORT_CMD),
 511	HCMD_NAME(HOT_SPOT_CMD),
 512	HCMD_NAME(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
 513	HCMD_NAME(BT_COEX_UPDATE_REDUCED_TXP),
 514	HCMD_NAME(BT_COEX_CI),
 515	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_NOTIFICATION),
 516	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_CONFIRM_NOTIFICATION),
 517	HCMD_NAME(PHY_CONFIGURATION_CMD),
 518	HCMD_NAME(CALIB_RES_NOTIF_PHY_DB),
 519	HCMD_NAME(PHY_DB_CMD),
 520	HCMD_NAME(SCAN_OFFLOAD_COMPLETE),
 521	HCMD_NAME(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
 522	HCMD_NAME(POWER_TABLE_CMD),
 523	HCMD_NAME(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
 524	HCMD_NAME(REPLY_THERMAL_MNG_BACKOFF),
 525	HCMD_NAME(NVM_ACCESS_CMD),
 526	HCMD_NAME(BEACON_NOTIFICATION),
 527	HCMD_NAME(BEACON_TEMPLATE_CMD),
 528	HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
 529	HCMD_NAME(BT_CONFIG),
 530	HCMD_NAME(STATISTICS_CMD),
 531	HCMD_NAME(STATISTICS_NOTIFICATION),
 532	HCMD_NAME(EOSP_NOTIFICATION),
 533	HCMD_NAME(REDUCE_TX_POWER_CMD),
 534	HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
 535	HCMD_NAME(TDLS_CONFIG_CMD),
 536	HCMD_NAME(MAC_PM_POWER_TABLE),
 537	HCMD_NAME(TDLS_CHANNEL_SWITCH_NOTIFICATION),
 538	HCMD_NAME(MFUART_LOAD_NOTIFICATION),
 539	HCMD_NAME(RSS_CONFIG_CMD),
 540	HCMD_NAME(SCAN_ITERATION_COMPLETE_UMAC),
 541	HCMD_NAME(REPLY_RX_PHY_CMD),
 542	HCMD_NAME(REPLY_RX_MPDU_CMD),
 543	HCMD_NAME(BAR_FRAME_RELEASE),
 544	HCMD_NAME(FRAME_RELEASE),
 545	HCMD_NAME(BA_NOTIF),
 546	HCMD_NAME(MCC_UPDATE_CMD),
 547	HCMD_NAME(MCC_CHUB_UPDATE_CMD),
 548	HCMD_NAME(MARKER_CMD),
 549	HCMD_NAME(BT_PROFILE_NOTIFICATION),
 550	HCMD_NAME(MCAST_FILTER_CMD),
 551	HCMD_NAME(REPLY_SF_CFG_CMD),
 552	HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
 553	HCMD_NAME(D3_CONFIG_CMD),
 554	HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
 555	HCMD_NAME(MATCH_FOUND_NOTIFICATION),
 556	HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
 557	HCMD_NAME(WOWLAN_PATTERNS),
 558	HCMD_NAME(WOWLAN_CONFIGURATION),
 559	HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
 560	HCMD_NAME(WOWLAN_TKIP_PARAM),
 561	HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
 562	HCMD_NAME(WOWLAN_GET_STATUSES),
 563	HCMD_NAME(SCAN_ITERATION_COMPLETE),
 564	HCMD_NAME(D0I3_END_CMD),
 565	HCMD_NAME(LTR_CONFIG),
 566	HCMD_NAME(LDBG_CONFIG_CMD),
 567	HCMD_NAME(DEBUG_LOG_MSG),
 568};
 569
 570/* Please keep this array *SORTED* by hex value.
 571 * Access is done through binary search
 572 */
 573static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
 574	HCMD_NAME(SHARED_MEM_CFG_CMD),
 575	HCMD_NAME(SOC_CONFIGURATION_CMD),
 576	HCMD_NAME(INIT_EXTENDED_CFG_CMD),
 577	HCMD_NAME(FW_ERROR_RECOVERY_CMD),
 578	HCMD_NAME(RFI_CONFIG_CMD),
 579	HCMD_NAME(RFI_GET_FREQ_TABLE_CMD),
 580	HCMD_NAME(SYSTEM_FEATURES_CONTROL_CMD),
 581	HCMD_NAME(SYSTEM_STATISTICS_CMD),
 582	HCMD_NAME(SYSTEM_STATISTICS_END_NOTIF),
 583	HCMD_NAME(RFI_DEACTIVATE_NOTIF),
 584};
 585
 586/* Please keep this array *SORTED* by hex value.
 587 * Access is done through binary search
 588 */
 589static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
 590	HCMD_NAME(LOW_LATENCY_CMD),
 591	HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
 592	HCMD_NAME(SESSION_PROTECTION_CMD),
 593	HCMD_NAME(CANCEL_CHANNEL_SWITCH_CMD),
 594	HCMD_NAME(MAC_CONFIG_CMD),
 595	HCMD_NAME(LINK_CONFIG_CMD),
 596	HCMD_NAME(STA_CONFIG_CMD),
 597	HCMD_NAME(AUX_STA_CMD),
 598	HCMD_NAME(STA_REMOVE_CMD),
 599	HCMD_NAME(STA_DISABLE_TX_CMD),
 600	HCMD_NAME(ROC_CMD),
 601	HCMD_NAME(ROC_NOTIF),
 602	HCMD_NAME(CHANNEL_SWITCH_ERROR_NOTIF),
 603	HCMD_NAME(MISSED_VAP_NOTIF),
 604	HCMD_NAME(SESSION_PROTECTION_NOTIF),
 605	HCMD_NAME(PROBE_RESPONSE_DATA_NOTIF),
 606	HCMD_NAME(CHANNEL_SWITCH_START_NOTIF),
 607};
 608
 609/* Please keep this array *SORTED* by hex value.
 610 * Access is done through binary search
 611 */
 612static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
 613	HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
 614	HCMD_NAME(CTDP_CONFIG_CMD),
 615	HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
 616	HCMD_NAME(PER_CHAIN_LIMIT_OFFSET_CMD),
 617	HCMD_NAME(AP_TX_POWER_CONSTRAINTS_CMD),
 618	HCMD_NAME(CT_KILL_NOTIFICATION),
 619	HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
 620};
 621
 622/* Please keep this array *SORTED* by hex value.
 623 * Access is done through binary search
 624 */
 625static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
 626	HCMD_NAME(DQA_ENABLE_CMD),
 627	HCMD_NAME(UPDATE_MU_GROUPS_CMD),
 628	HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
 629	HCMD_NAME(WNM_PLATFORM_PTM_REQUEST_CMD),
 630	HCMD_NAME(WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
 631	HCMD_NAME(STA_HE_CTXT_CMD),
 632	HCMD_NAME(RLC_CONFIG_CMD),
 633	HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
 634	HCMD_NAME(TLC_MNG_CONFIG_CMD),
 635	HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
 636	HCMD_NAME(SCD_QUEUE_CONFIG_CMD),
 637	HCMD_NAME(SEC_KEY_CMD),
 638	HCMD_NAME(MONITOR_NOTIF),
 639	HCMD_NAME(THERMAL_DUAL_CHAIN_REQUEST),
 640	HCMD_NAME(BEACON_FILTER_IN_NOTIF),
 641	HCMD_NAME(STA_PM_NOTIF),
 642	HCMD_NAME(MU_GROUP_MGMT_NOTIF),
 643	HCMD_NAME(RX_QUEUES_NOTIFICATION),
 644};
 645
 646/* Please keep this array *SORTED* by hex value.
 647 * Access is done through binary search
 648 */
 649static const struct iwl_hcmd_names iwl_mvm_statistics_names[] = {
 650	HCMD_NAME(STATISTICS_OPER_NOTIF),
 651	HCMD_NAME(STATISTICS_OPER_PART1_NOTIF),
 652};
 653
 654/* Please keep this array *SORTED* by hex value.
 655 * Access is done through binary search
 656 */
 657static const struct iwl_hcmd_names iwl_mvm_debug_names[] = {
 658	HCMD_NAME(LMAC_RD_WR),
 659	HCMD_NAME(UMAC_RD_WR),
 660	HCMD_NAME(HOST_EVENT_CFG),
 661	HCMD_NAME(DBGC_SUSPEND_RESUME),
 662	HCMD_NAME(BUFFER_ALLOCATION),
 663	HCMD_NAME(GET_TAS_STATUS),
 664	HCMD_NAME(FW_DUMP_COMPLETE_CMD),
 665	HCMD_NAME(FW_CLEAR_BUFFER),
 666	HCMD_NAME(MFU_ASSERT_DUMP_NTF),
 667};
 668
 669/* Please keep this array *SORTED* by hex value.
 670 * Access is done through binary search
 671 */
 672static const struct iwl_hcmd_names iwl_mvm_scan_names[] = {
 673	HCMD_NAME(CHANNEL_SURVEY_NOTIF),
 674	HCMD_NAME(OFFLOAD_MATCH_INFO_NOTIF),
 675};
 676
 677/* Please keep this array *SORTED* by hex value.
 678 * Access is done through binary search
 679 */
 680static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
 681	HCMD_NAME(TOF_RANGE_REQ_CMD),
 682	HCMD_NAME(TOF_CONFIG_CMD),
 683	HCMD_NAME(TOF_RANGE_ABORT_CMD),
 684	HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
 685	HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
 686	HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
 687	HCMD_NAME(TOF_LC_NOTIF),
 688	HCMD_NAME(TOF_RESPONDER_STATS),
 689	HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
 690	HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
 691};
 692
 693/* Please keep this array *SORTED* by hex value.
 694 * Access is done through binary search
 695 */
 696static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
 697	HCMD_NAME(WOWLAN_WAKE_PKT_NOTIFICATION),
 698	HCMD_NAME(WOWLAN_INFO_NOTIFICATION),
 699	HCMD_NAME(D3_END_NOTIFICATION),
 700	HCMD_NAME(STORED_BEACON_NTF),
 701};
 702
 703/* Please keep this array *SORTED* by hex value.
 704 * Access is done through binary search
 705 */
 706static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
 707	HCMD_NAME(NVM_ACCESS_COMPLETE),
 708	HCMD_NAME(NVM_GET_INFO),
 709	HCMD_NAME(TAS_CONFIG),
 710};
 711
 712/* Please keep this array *SORTED* by hex value.
 713 * Access is done through binary search
 714 */
 715static const struct iwl_hcmd_names iwl_mvm_bt_coex_names[] = {
 716	HCMD_NAME(PROFILE_NOTIF),
 717};
 718
 719VISIBLE_IF_IWLWIFI_KUNIT
 720const struct iwl_hcmd_arr iwl_mvm_groups[] = {
 721	[LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
 722	[LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
 723	[SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
 724	[MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
 725	[PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
 726	[DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
 727	[SCAN_GROUP] = HCMD_ARR(iwl_mvm_scan_names),
 728	[LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
 729	[BT_COEX_GROUP] = HCMD_ARR(iwl_mvm_bt_coex_names),
 730	[PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
 731	[REGULATORY_AND_NVM_GROUP] =
 732		HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
 733	[DEBUG_GROUP] = HCMD_ARR(iwl_mvm_debug_names),
 734	[STATISTICS_GROUP] = HCMD_ARR(iwl_mvm_statistics_names),
 735};
 736EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mvm_groups);
 737#if IS_ENABLED(CONFIG_IWLWIFI_KUNIT_TESTS)
 738const unsigned int iwl_mvm_groups_size = ARRAY_SIZE(iwl_mvm_groups);
 739EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mvm_groups_size);
 740#endif
 741
 742/* this forward declaration can avoid to export the function */
 743static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
 744static void iwl_mvm_async_handlers_wiphy_wk(struct wiphy *wiphy,
 745					    struct wiphy_work *work);
 746
 747static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
 748{
 749	const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
 750	u64 dflt_pwr_limit;
 751
 752	if (!backoff)
 753		return 0;
 754
 755	iwl_bios_get_pwr_limit(&mvm->fwrt, &dflt_pwr_limit);
 756
 757	while (backoff->pwr) {
 758		if (dflt_pwr_limit >= backoff->pwr)
 759			return backoff->backoff;
 760
 761		backoff++;
 762	}
 763
 764	return 0;
 765}
 766
 767static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
 768{
 769	struct iwl_mvm *mvm =
 770		container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
 771	struct ieee80211_vif *tx_blocked_vif;
 772	struct iwl_mvm_vif *mvmvif;
 773
 774	guard(mvm)(mvm);
 775
 776	tx_blocked_vif =
 777		rcu_dereference_protected(mvm->csa_tx_blocked_vif,
 778					  lockdep_is_held(&mvm->mutex));
 779
 780	if (!tx_blocked_vif)
 781		return;
 782
 783	mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
 784	iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
 785	RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
 786}
 787
 788static void iwl_mvm_fwrt_dump_start(void *ctx)
 789{
 790	struct iwl_mvm *mvm = ctx;
 791
 792	mutex_lock(&mvm->mutex);
 793}
 794
 795static void iwl_mvm_fwrt_dump_end(void *ctx)
 796{
 797	struct iwl_mvm *mvm = ctx;
 798
 799	mutex_unlock(&mvm->mutex);
 800}
 801
 802static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
 803{
 804	struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
 805
 806	guard(mvm)(mvm);
 807	return iwl_mvm_send_cmd(mvm, host_cmd);
 808}
 809
 810static bool iwl_mvm_d3_debug_enable(void *ctx)
 811{
 812	return IWL_MVM_D3_DEBUG;
 813}
 814
 815static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
 816	.dump_start = iwl_mvm_fwrt_dump_start,
 817	.dump_end = iwl_mvm_fwrt_dump_end,
 818	.send_hcmd = iwl_mvm_fwrt_send_hcmd,
 819	.d3_debug_enable = iwl_mvm_d3_debug_enable,
 820};
 821
 822static int iwl_mvm_start_get_nvm(struct iwl_mvm *mvm)
 823{
 824	struct iwl_trans *trans = mvm->trans;
 825	int ret;
 826
 827	if (trans->csme_own) {
 828		if (WARN(!mvm->mei_registered,
 829			 "csme is owner, but we aren't registered to iwlmei\n"))
 830			goto get_nvm_from_fw;
 831
 832		mvm->mei_nvm_data = iwl_mei_get_nvm();
 833		if (mvm->mei_nvm_data) {
 834			/*
 835			 * mvm->mei_nvm_data is set and because of that,
 836			 * we'll load the NVM from the FW when we'll get
 837			 * ownership.
 838			 */
 839			mvm->nvm_data =
 840				iwl_parse_mei_nvm_data(trans, trans->cfg,
 841						       mvm->mei_nvm_data,
 842						       mvm->fw,
 843						       mvm->set_tx_ant,
 844						       mvm->set_rx_ant);
 845			return 0;
 846		}
 847
 848		IWL_ERR(mvm,
 849			"Got a NULL NVM from CSME, trying to get it from the device\n");
 850	}
 851
 852get_nvm_from_fw:
 853	rtnl_lock();
 854	wiphy_lock(mvm->hw->wiphy);
 855	mutex_lock(&mvm->mutex);
 856
 857	ret = iwl_trans_start_hw(mvm->trans);
 858	if (ret) {
 859		mutex_unlock(&mvm->mutex);
 860		wiphy_unlock(mvm->hw->wiphy);
 861		rtnl_unlock();
 862		return ret;
 863	}
 864
 865	ret = iwl_run_init_mvm_ucode(mvm);
 866	if (ret && ret != -ERFKILL)
 867		iwl_fw_dbg_error_collect(&mvm->fwrt, FW_DBG_TRIGGER_DRIVER);
 868	if (!ret && iwl_mvm_is_lar_supported(mvm)) {
 869		mvm->hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
 870		ret = iwl_mvm_init_mcc(mvm);
 871	}
 872
 873	iwl_mvm_stop_device(mvm);
 874
 875	mutex_unlock(&mvm->mutex);
 876	wiphy_unlock(mvm->hw->wiphy);
 877	rtnl_unlock();
 878
 879	if (ret)
 880		IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
 881
 882	/* no longer need this regardless of failure or not */
 883	mvm->fw_product_reset = false;
 884
 885	return ret;
 886}
 887
 888static int iwl_mvm_start_post_nvm(struct iwl_mvm *mvm)
 889{
 890	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
 891	int ret;
 892
 893	iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
 894
 895	ret = iwl_mvm_mac_setup_register(mvm);
 896	if (ret)
 897		return ret;
 898
 899	mvm->hw_registered = true;
 900
 901	iwl_mvm_dbgfs_register(mvm);
 902
 903	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
 904					 mvm->mei_rfkill_blocked,
 905					 RFKILL_HARD_BLOCK_NOT_OWNER);
 906
 907	iwl_mvm_mei_set_sw_rfkill_state(mvm);
 908
 909	return 0;
 910}
 911
 912struct iwl_mvm_frob_txf_data {
 913	u8 *buf;
 914	size_t buflen;
 915};
 916
 917static void iwl_mvm_frob_txf_key_iter(struct ieee80211_hw *hw,
 918				      struct ieee80211_vif *vif,
 919				      struct ieee80211_sta *sta,
 920				      struct ieee80211_key_conf *key,
 921				      void *data)
 922{
 923	struct iwl_mvm_frob_txf_data *txf = data;
 924	u8 keylen, match, matchend;
 925	u8 *keydata;
 926	size_t i;
 927
 928	switch (key->cipher) {
 929	case WLAN_CIPHER_SUITE_CCMP:
 930		keydata = key->key;
 931		keylen = key->keylen;
 932		break;
 933	case WLAN_CIPHER_SUITE_WEP40:
 934	case WLAN_CIPHER_SUITE_WEP104:
 935	case WLAN_CIPHER_SUITE_TKIP:
 936		/*
 937		 * WEP has short keys which might show up in the payload,
 938		 * and then you can deduce the key, so in this case just
 939		 * remove all FIFO data.
 940		 * For TKIP, we don't know the phase 2 keys here, so same.
 941		 */
 942		memset(txf->buf, 0xBB, txf->buflen);
 943		return;
 944	default:
 945		return;
 946	}
 947
 948	/* scan for key material and clear it out */
 949	match = 0;
 950	for (i = 0; i < txf->buflen; i++) {
 951		if (txf->buf[i] != keydata[match]) {
 952			match = 0;
 953			continue;
 954		}
 955		match++;
 956		if (match == keylen) {
 957			memset(txf->buf + i - keylen, 0xAA, keylen);
 958			match = 0;
 959		}
 960	}
 961
 962	/* we're dealing with a FIFO, so check wrapped around data */
 963	matchend = match;
 964	for (i = 0; match && i < keylen - match; i++) {
 965		if (txf->buf[i] != keydata[match])
 966			break;
 967		match++;
 968		if (match == keylen) {
 969			memset(txf->buf, 0xAA, i + 1);
 970			memset(txf->buf + txf->buflen - matchend, 0xAA,
 971			       matchend);
 972			break;
 973		}
 974	}
 975}
 976
 977static void iwl_mvm_frob_txf(void *ctx, void *buf, size_t buflen)
 978{
 979	struct iwl_mvm_frob_txf_data txf = {
 980		.buf = buf,
 981		.buflen = buflen,
 982	};
 983	struct iwl_mvm *mvm = ctx;
 984
 985	/* embedded key material exists only on old API */
 986	if (iwl_mvm_has_new_tx_api(mvm))
 987		return;
 988
 989	rcu_read_lock();
 990	ieee80211_iter_keys_rcu(mvm->hw, NULL, iwl_mvm_frob_txf_key_iter, &txf);
 991	rcu_read_unlock();
 992}
 993
 994static void iwl_mvm_frob_hcmd(void *ctx, void *hcmd, size_t len)
 995{
 996	/* we only use wide headers for commands */
 997	struct iwl_cmd_header_wide *hdr = hcmd;
 998	unsigned int frob_start = sizeof(*hdr), frob_end = 0;
 999
1000	if (len < sizeof(hdr))
1001		return;
1002
1003	/* all the commands we care about are in LONG_GROUP */
1004	if (hdr->group_id != LONG_GROUP)
1005		return;
1006
1007	switch (hdr->cmd) {
1008	case WEP_KEY:
1009	case WOWLAN_TKIP_PARAM:
1010	case WOWLAN_KEK_KCK_MATERIAL:
1011	case ADD_STA_KEY:
1012		/*
1013		 * blank out everything here, easier than dealing
1014		 * with the various versions of the command
1015		 */
1016		frob_end = INT_MAX;
1017		break;
1018	case MGMT_MCAST_KEY:
1019		frob_start = offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
1020		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) !=
1021			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
1022
1023		frob_end = offsetofend(struct iwl_mvm_mgmt_mcast_key_cmd, igtk);
1024		BUILD_BUG_ON(offsetof(struct iwl_mvm_mgmt_mcast_key_cmd, igtk) <
1025			     offsetof(struct iwl_mvm_mgmt_mcast_key_cmd_v1, igtk));
1026		break;
1027	}
1028
1029	if (frob_start >= frob_end)
1030		return;
1031
1032	if (frob_end > len)
1033		frob_end = len;
1034
1035	memset((u8 *)hcmd + frob_start, 0xAA, frob_end - frob_start);
1036}
1037
1038static void iwl_mvm_frob_mem(void *ctx, u32 mem_addr, void *mem, size_t buflen)
1039{
1040	const struct iwl_dump_exclude *excl;
1041	struct iwl_mvm *mvm = ctx;
1042	int i;
1043
1044	switch (mvm->fwrt.cur_fw_img) {
1045	case IWL_UCODE_INIT:
1046	default:
1047		/* not relevant */
1048		return;
1049	case IWL_UCODE_REGULAR:
1050	case IWL_UCODE_REGULAR_USNIFFER:
1051		excl = mvm->fw->dump_excl;
1052		break;
1053	case IWL_UCODE_WOWLAN:
1054		excl = mvm->fw->dump_excl_wowlan;
1055		break;
1056	}
1057
1058	BUILD_BUG_ON(sizeof(mvm->fw->dump_excl) !=
1059		     sizeof(mvm->fw->dump_excl_wowlan));
1060
1061	for (i = 0; i < ARRAY_SIZE(mvm->fw->dump_excl); i++) {
1062		u32 start, end;
1063
1064		if (!excl[i].addr || !excl[i].size)
1065			continue;
1066
1067		start = excl[i].addr;
1068		end = start + excl[i].size;
1069
1070		if (end <= mem_addr || start >= mem_addr + buflen)
1071			continue;
1072
1073		if (start < mem_addr)
1074			start = mem_addr;
1075
1076		if (end > mem_addr + buflen)
1077			end = mem_addr + buflen;
1078
1079		memset((u8 *)mem + start - mem_addr, 0xAA, end - start);
1080	}
1081}
1082
1083static const struct iwl_dump_sanitize_ops iwl_mvm_sanitize_ops = {
1084	.frob_txf = iwl_mvm_frob_txf,
1085	.frob_hcmd = iwl_mvm_frob_hcmd,
1086	.frob_mem = iwl_mvm_frob_mem,
1087};
1088
1089static void iwl_mvm_me_conn_status(void *priv, const struct iwl_mei_conn_info *conn_info)
1090{
1091	struct iwl_mvm *mvm = priv;
1092	struct iwl_mvm_csme_conn_info *prev_conn_info, *curr_conn_info;
1093
1094	/*
1095	 * This is protected by the guarantee that this function will not be
1096	 * called twice on two different threads
1097	 */
1098	prev_conn_info = rcu_dereference_protected(mvm->csme_conn_info, true);
1099
1100	curr_conn_info = kzalloc(sizeof(*curr_conn_info), GFP_KERNEL);
1101	if (!curr_conn_info)
1102		return;
1103
1104	curr_conn_info->conn_info = *conn_info;
1105
1106	rcu_assign_pointer(mvm->csme_conn_info, curr_conn_info);
1107
1108	if (prev_conn_info)
1109		kfree_rcu(prev_conn_info, rcu_head);
1110}
1111
1112static void iwl_mvm_mei_rfkill(void *priv, bool blocked,
1113			       bool csme_taking_ownership)
1114{
1115	struct iwl_mvm *mvm = priv;
1116
1117	if (blocked && !csme_taking_ownership)
1118		return;
1119
1120	mvm->mei_rfkill_blocked = blocked;
1121	if (!mvm->hw_registered)
1122		return;
1123
1124	wiphy_rfkill_set_hw_state_reason(mvm->hw->wiphy,
1125					 mvm->mei_rfkill_blocked,
1126					 RFKILL_HARD_BLOCK_NOT_OWNER);
1127}
1128
1129static void iwl_mvm_mei_roaming_forbidden(void *priv, bool forbidden)
1130{
1131	struct iwl_mvm *mvm = priv;
1132
1133	if (!mvm->hw_registered || !mvm->csme_vif)
1134		return;
1135
1136	iwl_mvm_send_roaming_forbidden_event(mvm, mvm->csme_vif, forbidden);
1137}
1138
1139static void iwl_mvm_sap_connected_wk(struct work_struct *wk)
1140{
1141	struct iwl_mvm *mvm =
1142		container_of(wk, struct iwl_mvm, sap_connected_wk);
1143	int ret;
1144
1145	ret = iwl_mvm_start_get_nvm(mvm);
1146	if (ret)
1147		goto out_free;
1148
1149	ret = iwl_mvm_start_post_nvm(mvm);
1150	if (ret)
1151		goto out_free;
1152
1153	return;
1154
1155out_free:
1156	IWL_ERR(mvm, "Couldn't get started...\n");
1157	iwl_mei_start_unregister();
1158	iwl_mei_unregister_complete();
1159	iwl_fw_flush_dumps(&mvm->fwrt);
1160	iwl_mvm_thermal_exit(mvm);
1161	iwl_fw_runtime_free(&mvm->fwrt);
1162	iwl_phy_db_free(mvm->phy_db);
1163	kfree(mvm->scan_cmd);
1164	iwl_trans_op_mode_leave(mvm->trans);
1165	kfree(mvm->nvm_data);
1166	kfree(mvm->mei_nvm_data);
1167
1168	ieee80211_free_hw(mvm->hw);
1169}
1170
1171static void iwl_mvm_mei_sap_connected(void *priv)
1172{
1173	struct iwl_mvm *mvm = priv;
1174
1175	if (!mvm->hw_registered)
1176		schedule_work(&mvm->sap_connected_wk);
1177}
1178
1179static void iwl_mvm_mei_nic_stolen(void *priv)
1180{
1181	struct iwl_mvm *mvm = priv;
1182
1183	rtnl_lock();
1184	cfg80211_shutdown_all_interfaces(mvm->hw->wiphy);
1185	rtnl_unlock();
1186}
1187
1188static const struct iwl_mei_ops mei_ops = {
1189	.me_conn_status = iwl_mvm_me_conn_status,
1190	.rfkill = iwl_mvm_mei_rfkill,
1191	.roaming_forbidden = iwl_mvm_mei_roaming_forbidden,
1192	.sap_connected = iwl_mvm_mei_sap_connected,
1193	.nic_stolen = iwl_mvm_mei_nic_stolen,
1194};
1195
1196static struct iwl_op_mode *
1197iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_rf_cfg *cfg,
1198		      const struct iwl_fw *fw, struct dentry *dbgfs_dir)
1199{
1200	struct ieee80211_hw *hw;
1201	struct iwl_op_mode *op_mode;
1202	struct iwl_mvm *mvm;
1203	static const u8 no_reclaim_cmds[] = {
1204		TX_CMD,
1205	};
1206	u32 max_agg;
1207	size_t scan_size;
1208	u32 min_backoff;
1209	struct iwl_mvm_csme_conn_info *csme_conn_info __maybe_unused;
1210	int ratecheck;
1211	int err;
1212
1213	/*
1214	 * We use IWL_STATION_COUNT_MAX to check the validity of the station
1215	 * index all over the driver - check that its value corresponds to the
1216	 * array size.
1217	 */
1218	BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) !=
1219		     IWL_STATION_COUNT_MAX);
1220
1221	/********************************
1222	 * 1. Allocating and configuring HW data
1223	 ********************************/
1224	hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
1225				sizeof(struct iwl_mvm),
1226				iwl_mvm_has_mld_api(fw) ? &iwl_mvm_mld_hw_ops :
1227				&iwl_mvm_hw_ops);
1228	if (!hw)
1229		return ERR_PTR(-ENOMEM);
1230
1231	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
1232		max_agg = 512;
1233	else
1234		max_agg = IEEE80211_MAX_AMPDU_BUF_HE;
1235
1236	hw->max_rx_aggregation_subframes = max_agg;
1237
1238	op_mode = hw->priv;
1239
1240	mvm = IWL_OP_MODE_GET_MVM(op_mode);
1241	mvm->dev = trans->dev;
1242	mvm->trans = trans;
1243	mvm->cfg = cfg;
1244	mvm->fw = fw;
1245	mvm->hw = hw;
1246
1247	iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
1248			    &iwl_mvm_sanitize_ops, mvm, dbgfs_dir);
1249
1250	iwl_mvm_get_bios_tables(mvm);
1251	iwl_uefi_get_sgom_table(trans, &mvm->fwrt);
1252	iwl_uefi_get_step_table(trans);
1253	iwl_bios_setup_step(trans, &mvm->fwrt);
1254
1255	mvm->init_status = 0;
1256
1257	/* start with v1 rates */
1258	mvm->fw_rates_ver = 1;
1259
1260	/* check for rates version 2 */
1261	ratecheck =
1262		(iwl_fw_lookup_cmd_ver(mvm->fw, TX_CMD, 0) >= 8) +
1263		(iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
1264					 TLC_MNG_UPDATE_NOTIF, 0) >= 3) +
1265		(iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1266					 REPLY_RX_MPDU_CMD, 0) >= 4) +
1267		(iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TX_CMD, 0) >= 6);
1268	if (ratecheck != 0 && ratecheck != 4) {
1269		IWL_ERR(mvm, "Firmware has inconsistent rates\n");
1270		err = -EINVAL;
1271		goto out_free;
1272	}
1273	if (ratecheck == 4)
1274		mvm->fw_rates_ver = 2;
1275
1276	/* check for rates version 3 */
1277	ratecheck =
1278		(iwl_fw_lookup_cmd_ver(mvm->fw, TX_CMD, 0) >= 11) +
1279		(iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
1280					 TLC_MNG_UPDATE_NOTIF, 0) >= 4) +
1281		(iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1282					 REPLY_RX_MPDU_CMD, 0) >= 6) +
1283		(iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
1284					 RX_NO_DATA_NOTIF, 0) >= 4) +
1285		(iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, TX_CMD, 0) >= 9);
1286	if (ratecheck != 0 && ratecheck != 5) {
1287		IWL_ERR(mvm, "Firmware has inconsistent rates\n");
1288		err = -EINVAL;
1289		goto out_free;
1290	}
1291	if (ratecheck == 5)
1292		mvm->fw_rates_ver = 3;
1293
1294	trans->conf.rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1295
1296	if (iwl_mvm_has_new_rx_api(mvm)) {
1297		op_mode->ops = &iwl_mvm_ops_mq;
1298		trans->conf.rx_mpdu_cmd_hdr_size =
1299			(trans->mac_cfg->device_family >=
1300			 IWL_DEVICE_FAMILY_AX210) ?
1301			sizeof(struct iwl_rx_mpdu_desc) :
1302			IWL_RX_DESC_SIZE_V1;
1303	} else {
1304		op_mode->ops = &iwl_mvm_ops;
1305		trans->conf.rx_mpdu_cmd_hdr_size =
1306			sizeof(struct iwl_rx_mpdu_res_start);
1307
1308		if (WARN_ON(trans->info.num_rxqs > 1)) {
1309			err = -EINVAL;
1310			goto out_free;
1311		}
1312	}
1313
1314	if (iwl_mvm_has_new_tx_api(mvm)) {
1315		/*
1316		 * If we have the new TX/queue allocation API initialize them
1317		 * all to invalid numbers. We'll rewrite the ones that we need
1318		 * later, but that doesn't happen for all of them all of the
1319		 * time (e.g. P2P Device is optional), and if a dynamic queue
1320		 * ends up getting number 2 (IWL_MVM_DQA_P2P_DEVICE_QUEUE) then
1321		 * iwl_mvm_is_static_queue() erroneously returns true, and we
1322		 * might have things getting stuck.
1323		 */
1324		mvm->aux_queue = IWL_MVM_INVALID_QUEUE;
1325		mvm->snif_queue = IWL_MVM_INVALID_QUEUE;
1326		mvm->probe_queue = IWL_MVM_INVALID_QUEUE;
1327		mvm->p2p_dev_queue = IWL_MVM_INVALID_QUEUE;
1328	} else {
1329		mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
1330		mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
1331		mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1332		mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1333	}
1334
1335	mvm->sf_state = SF_UNINIT;
1336	if (iwl_mvm_has_unified_ucode(mvm))
1337		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1338	else
1339		iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
1340	mvm->drop_bcn_ap_mode = true;
1341
1342	mutex_init(&mvm->mutex);
1343	spin_lock_init(&mvm->async_handlers_lock);
1344	INIT_LIST_HEAD(&mvm->time_event_list);
1345	INIT_LIST_HEAD(&mvm->aux_roc_te_list);
1346	INIT_LIST_HEAD(&mvm->async_handlers_list);
1347	spin_lock_init(&mvm->time_event_lock);
1348	INIT_LIST_HEAD(&mvm->ftm_initiator.loc_list);
1349	INIT_LIST_HEAD(&mvm->ftm_initiator.pasn_list);
1350	INIT_LIST_HEAD(&mvm->resp_pasn_list);
1351
1352	INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
1353	INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
1354	INIT_WORK(&mvm->sap_connected_wk, iwl_mvm_sap_connected_wk);
1355	INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
1356	INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
1357	INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
1358	INIT_LIST_HEAD(&mvm->add_stream_txqs);
1359	spin_lock_init(&mvm->add_stream_lock);
1360
1361	wiphy_work_init(&mvm->async_handlers_wiphy_wk,
1362			iwl_mvm_async_handlers_wiphy_wk);
1363
1364	init_waitqueue_head(&mvm->rx_sync_waitq);
1365
1366	mvm->queue_sync_state = 0;
1367
1368	SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
1369
1370	spin_lock_init(&mvm->tcm.lock);
1371	INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
1372	mvm->tcm.ts = jiffies;
1373	mvm->tcm.ll_ts = jiffies;
1374	mvm->tcm.uapsd_nonagg_ts = jiffies;
1375
1376	INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
1377
1378	mvm->cmd_ver.range_resp =
1379		iwl_fw_lookup_notif_ver(mvm->fw, LOCATION_GROUP,
1380					TOF_RANGE_RESPONSE_NOTIF, 5);
1381	/* we only support up to version 9 */
1382	if (WARN_ON_ONCE(mvm->cmd_ver.range_resp > 9)) {
1383		err = -EINVAL;
1384		goto out_free;
1385	}
1386
1387	/*
1388	 * Populate the state variables that the transport layer needs
1389	 * to know about.
1390	 */
1391	BUILD_BUG_ON(sizeof(no_reclaim_cmds) >
1392		     sizeof(trans->conf.no_reclaim_cmds));
1393	memcpy(trans->conf.no_reclaim_cmds, no_reclaim_cmds,
1394	       sizeof(no_reclaim_cmds));
1395	trans->conf.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1396
1397	trans->conf.rx_buf_size = iwl_amsdu_size_to_rxb_size();
1398
1399	trans->conf.wide_cmd_header = true;
1400
1401	trans->conf.command_groups = iwl_mvm_groups;
1402	trans->conf.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
1403
1404	trans->conf.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
1405	trans->conf.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
1406	trans->conf.scd_set_active = true;
1407
1408	trans->conf.cb_data_offs = offsetof(struct ieee80211_tx_info,
1409					    driver_data[2]);
1410
1411	snprintf(mvm->hw->wiphy->fw_version,
1412		 sizeof(mvm->hw->wiphy->fw_version),
1413		 "%.31s", fw->fw_version);
1414
1415	trans->conf.fw_reset_handshake =
1416		fw_has_capa(&mvm->fw->ucode_capa,
1417			    IWL_UCODE_TLV_CAPA_FW_RESET_HANDSHAKE);
1418
1419	trans->conf.queue_alloc_cmd_ver =
1420		iwl_fw_lookup_cmd_ver(mvm->fw,
1421				      WIDE_ID(DATA_PATH_GROUP,
1422					      SCD_QUEUE_CONFIG_CMD),
1423				      0);
1424	mvm->sta_remove_requires_queue_remove =
1425		trans->conf.queue_alloc_cmd_ver > 0;
1426
1427	mvm->mld_api_is_used = iwl_mvm_has_mld_api(mvm->fw);
1428
1429	/* Configure transport layer */
1430	iwl_trans_op_mode_enter(mvm->trans, op_mode);
1431
1432	trans->dbg.dest_tlv = mvm->fw->dbg.dest_tlv;
1433	trans->dbg.n_dest_reg = mvm->fw->dbg.n_dest_reg;
1434
1435	/* set up notification wait support */
1436	iwl_notification_wait_init(&mvm->notif_wait);
1437
1438	/* Init phy db */
1439	mvm->phy_db = iwl_phy_db_init(trans);
1440	if (!mvm->phy_db) {
1441		IWL_ERR(mvm, "Cannot init phy_db\n");
1442		err = -ENOMEM;
1443		goto out_free;
1444	}
1445
1446	if (iwlwifi_mod_params.nvm_file)
1447		mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
1448	else
1449		IWL_DEBUG_EEPROM(mvm->trans->dev,
1450				 "working without external nvm file\n");
1451
1452	scan_size = iwl_mvm_scan_size(mvm);
1453
1454	mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
1455	if (!mvm->scan_cmd) {
1456		err = -ENOMEM;
1457		goto out_free;
1458	}
1459	mvm->scan_cmd_size = scan_size;
1460
1461	/* invalidate ids to prevent accidental removal of sta_id 0 */
1462	mvm->aux_sta.sta_id = IWL_INVALID_STA;
1463	mvm->snif_sta.sta_id = IWL_INVALID_STA;
1464
1465	/* Set EBS as successful as long as not stated otherwise by the FW. */
1466	mvm->last_ebs_successful = true;
1467
1468	min_backoff = iwl_mvm_min_backoff(mvm);
1469	iwl_mvm_thermal_initialize(mvm, min_backoff);
1470
1471	if (!iwl_mvm_has_new_rx_stats_api(mvm))
1472		memset(&mvm->rx_stats_v3, 0,
1473		       sizeof(struct mvm_statistics_rx_v3));
1474	else
1475		memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
1476
1477	iwl_mvm_ftm_initiator_smooth_config(mvm);
1478
1479	iwl_mvm_init_time_sync(&mvm->time_sync);
1480
1481	mvm->debugfs_dir = dbgfs_dir;
1482
1483	mvm->mei_registered = !iwl_mei_register(mvm, &mei_ops);
1484
1485	iwl_mvm_mei_scan_filter_init(&mvm->mei_scan_filter);
1486
1487	err = iwl_mvm_start_get_nvm(mvm);
1488	if (err) {
1489		/*
1490		 * Getting NVM failed while CSME is the owner, but we are
1491		 * registered to MEI, we'll get the NVM later when it'll be
1492		 * possible to get it from CSME.
1493		 */
1494		if (trans->csme_own && mvm->mei_registered)
1495			return op_mode;
1496
1497		goto out_thermal_exit;
1498	}
1499
1500
1501	err = iwl_mvm_start_post_nvm(mvm);
1502	if (err)
1503		goto out_thermal_exit;
1504
1505	return op_mode;
1506
1507 out_thermal_exit:
1508	iwl_mvm_thermal_exit(mvm);
1509	if (mvm->mei_registered) {
1510		iwl_mei_start_unregister();
1511		iwl_mei_unregister_complete();
1512	}
1513 out_free:
1514	iwl_fw_flush_dumps(&mvm->fwrt);
1515	iwl_fw_runtime_free(&mvm->fwrt);
1516
1517	iwl_phy_db_free(mvm->phy_db);
1518	kfree(mvm->scan_cmd);
1519	iwl_trans_op_mode_leave(trans);
1520
1521	ieee80211_free_hw(mvm->hw);
1522	return ERR_PTR(err);
1523}
1524
1525void iwl_mvm_stop_device(struct iwl_mvm *mvm)
1526{
1527	lockdep_assert_held(&mvm->mutex);
1528
1529	iwl_fw_cancel_timestamp(&mvm->fwrt);
1530
1531	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1532
1533	iwl_mvm_pause_tcm(mvm, false);
1534
1535	iwl_fw_dbg_stop_sync(&mvm->fwrt);
1536	iwl_trans_stop_device(mvm->trans);
1537	iwl_free_fw_paging(&mvm->fwrt);
1538	iwl_fw_dump_conf_clear(&mvm->fwrt);
1539	iwl_mvm_mei_device_state(mvm, false);
1540}
1541
1542static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
1543{
1544	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1545	int i;
1546
1547	if (mvm->mei_registered) {
1548		rtnl_lock();
1549		iwl_mei_set_netdev(NULL);
1550		rtnl_unlock();
1551		iwl_mei_start_unregister();
1552	}
1553
1554	/*
1555	 * After we unregister from mei, the worker can't be scheduled
1556	 * anymore.
1557	 */
1558	cancel_work_sync(&mvm->sap_connected_wk);
1559
1560	iwl_mvm_leds_exit(mvm);
1561
1562	iwl_mvm_thermal_exit(mvm);
1563
1564	/*
1565	 * If we couldn't get ownership on the device and we couldn't
1566	 * get the NVM from CSME, we haven't registered to mac80211.
1567	 * In that case, we didn't fail op_mode_start, because we are
1568	 * waiting for CSME to allow us to get the NVM to register to
1569	 * mac80211. If that didn't happen, we haven't registered to
1570	 * mac80211, hence the if below.
1571	 */
1572	if (mvm->hw_registered)
1573		ieee80211_unregister_hw(mvm->hw);
1574
1575	kfree(mvm->scan_cmd);
1576	kfree(mvm->mcast_filter_cmd);
1577	mvm->mcast_filter_cmd = NULL;
1578
1579	kfree(mvm->error_recovery_buf);
1580	mvm->error_recovery_buf = NULL;
1581
1582	iwl_mvm_ptp_remove(mvm);
1583
1584	iwl_trans_op_mode_leave(mvm->trans);
1585
1586	iwl_phy_db_free(mvm->phy_db);
1587	mvm->phy_db = NULL;
1588
1589	kfree(mvm->nvm_data);
1590	kfree(mvm->mei_nvm_data);
1591	kfree(rcu_access_pointer(mvm->csme_conn_info));
1592	kfree(mvm->temp_nvm_data);
1593	for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
1594		kfree(mvm->nvm_sections[i].data);
1595	kfree(mvm->acs_survey);
1596
1597	cancel_delayed_work_sync(&mvm->tcm.work);
1598
1599	iwl_fw_runtime_free(&mvm->fwrt);
1600	mutex_destroy(&mvm->mutex);
1601
1602	if (mvm->mei_registered)
1603		iwl_mei_unregister_complete();
1604
1605	ieee80211_free_hw(mvm->hw);
1606}
1607
1608struct iwl_async_handler_entry {
1609	struct list_head list;
1610	struct iwl_rx_cmd_buffer rxb;
1611	enum iwl_rx_handler_context context;
1612	void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
1613};
1614
1615void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
1616{
1617	struct iwl_async_handler_entry *entry, *tmp;
1618
1619	spin_lock_bh(&mvm->async_handlers_lock);
1620	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1621		iwl_free_rxb(&entry->rxb);
1622		list_del(&entry->list);
1623		kfree(entry);
1624	}
1625	spin_unlock_bh(&mvm->async_handlers_lock);
1626}
1627
1628/*
1629 * This function receives a bitmap of rx async handler contexts
1630 * (&iwl_rx_handler_context) to handle, and runs only them
1631 */
1632static void iwl_mvm_async_handlers_by_context(struct iwl_mvm *mvm,
1633					      u8 contexts)
1634{
1635	struct iwl_async_handler_entry *entry, *tmp;
1636	LIST_HEAD(local_list);
1637
1638	/*
1639	 * Sync with Rx path with a lock. Remove all the entries of the
1640	 * wanted contexts from this list, add them to a local one (lock free),
1641	 * and then handle them.
1642	 */
1643	spin_lock_bh(&mvm->async_handlers_lock);
1644	list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
1645		if (!(BIT(entry->context) & contexts))
1646			continue;
1647		list_del(&entry->list);
1648		list_add_tail(&entry->list, &local_list);
1649	}
1650	spin_unlock_bh(&mvm->async_handlers_lock);
1651
1652	list_for_each_entry_safe(entry, tmp, &local_list, list) {
1653		if (entry->context != RX_HANDLER_ASYNC_UNLOCKED)
1654			mutex_lock(&mvm->mutex);
1655		entry->fn(mvm, &entry->rxb);
1656		iwl_free_rxb(&entry->rxb);
1657		list_del(&entry->list);
1658		if (entry->context != RX_HANDLER_ASYNC_UNLOCKED)
1659			mutex_unlock(&mvm->mutex);
1660		kfree(entry);
1661	}
1662}
1663
1664static void iwl_mvm_async_handlers_wiphy_wk(struct wiphy *wiphy,
1665					    struct wiphy_work *wk)
1666{
1667	struct iwl_mvm *mvm =
1668		container_of(wk, struct iwl_mvm, async_handlers_wiphy_wk);
1669	u8 contexts = BIT(RX_HANDLER_ASYNC_LOCKED_WIPHY);
1670
1671	iwl_mvm_async_handlers_by_context(mvm, contexts);
1672}
1673
1674static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
1675{
1676	struct iwl_mvm *mvm =
1677		container_of(wk, struct iwl_mvm, async_handlers_wk);
1678	u8 contexts = BIT(RX_HANDLER_ASYNC_LOCKED) |
1679		      BIT(RX_HANDLER_ASYNC_UNLOCKED);
1680
1681	iwl_mvm_async_handlers_by_context(mvm, contexts);
1682}
1683
1684static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
1685					    struct iwl_rx_packet *pkt)
1686{
1687	struct iwl_fw_dbg_trigger_tlv *trig;
1688	struct iwl_fw_dbg_trigger_cmd *cmds_trig;
1689	int i;
1690
1691	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
1692				     FW_DBG_TRIGGER_FW_NOTIF);
1693	if (!trig)
1694		return;
1695
1696	cmds_trig = (void *)trig->data;
1697
1698	for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1699		/* don't collect on CMD 0 */
1700		if (!cmds_trig->cmds[i].cmd_id)
1701			break;
1702
1703		if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1704		    cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
1705			continue;
1706
1707		iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1708					"CMD 0x%02x.%02x received",
1709					pkt->hdr.group_id, pkt->hdr.cmd);
1710		break;
1711	}
1712}
1713
1714static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1715			      struct iwl_rx_cmd_buffer *rxb,
1716			      struct iwl_rx_packet *pkt)
1717{
1718	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1719	int i;
1720	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1721
1722	iwl_dbg_tlv_time_point(&mvm->fwrt,
1723			       IWL_FW_INI_TIME_POINT_FW_RSP_OR_NOTIF, &tp_data);
1724	iwl_mvm_rx_check_trigger(mvm, pkt);
1725
1726	/*
1727	 * Do the notification wait before RX handlers so
1728	 * even if the RX handler consumes the RXB we have
1729	 * access to it in the notification wait entry.
1730	 */
1731	iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1732
1733	for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1734		const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
1735		struct iwl_async_handler_entry *entry;
1736
1737		if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
1738			continue;
1739
1740		if (IWL_FW_CHECK(mvm, pkt_len < rx_h->min_size,
1741				 "unexpected notification 0x%04x size %d, need %d\n",
1742				 rx_h->cmd_id, pkt_len, rx_h->min_size))
1743			return;
1744
1745		if (rx_h->context == RX_HANDLER_SYNC) {
1746			rx_h->fn(mvm, rxb);
1747			return;
1748		}
1749
1750		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1751		/* we can't do much... */
1752		if (!entry)
1753			return;
1754
1755		entry->rxb._page = rxb_steal_page(rxb);
1756		entry->rxb._offset = rxb->_offset;
1757		entry->rxb._rx_page_order = rxb->_rx_page_order;
1758		entry->fn = rx_h->fn;
1759		entry->context = rx_h->context;
1760		spin_lock(&mvm->async_handlers_lock);
1761		list_add_tail(&entry->list, &mvm->async_handlers_list);
1762		spin_unlock(&mvm->async_handlers_lock);
1763		if (rx_h->context == RX_HANDLER_ASYNC_LOCKED_WIPHY)
1764			wiphy_work_queue(mvm->hw->wiphy,
1765					 &mvm->async_handlers_wiphy_wk);
1766		else
1767			schedule_work(&mvm->async_handlers_wk);
1768		break;
1769	}
1770}
1771
1772static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1773		       struct napi_struct *napi,
1774		       struct iwl_rx_cmd_buffer *rxb)
1775{
1776	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1777	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1778	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1779
1780	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1781		iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
1782	else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
1783		iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1784	else
1785		iwl_mvm_rx_common(mvm, rxb, pkt);
1786}
1787
1788void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1789		   struct napi_struct *napi,
1790		   struct iwl_rx_cmd_buffer *rxb)
1791{
1792	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1793	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1794	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
1795
1796	if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
1797		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
1798	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1799					 RX_QUEUES_NOTIFICATION)))
1800		iwl_mvm_rx_queue_notif(mvm, napi, rxb, 0);
1801	else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
1802		iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
1803	else if (cmd == WIDE_ID(LEGACY_GROUP, BAR_FRAME_RELEASE))
1804		iwl_mvm_rx_bar_frame_release(mvm, napi, rxb, 0);
1805	else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1806		iwl_mvm_rx_monitor_no_data(mvm, napi, rxb, 0);
1807	else
1808		iwl_mvm_rx_common(mvm, rxb, pkt);
1809}
1810
1811static int iwl_mvm_is_static_queue(struct iwl_mvm *mvm, int queue)
1812{
1813	return queue == mvm->aux_queue || queue == mvm->probe_queue ||
1814		queue == mvm->p2p_dev_queue || queue == mvm->snif_queue;
1815}
1816
1817static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1818				       int hw_queue, bool start)
1819{
1820	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1821	struct ieee80211_sta *sta;
1822	struct ieee80211_txq *txq;
1823	struct iwl_mvm_txq *mvmtxq;
1824	int i;
1825	unsigned long tid_bitmap;
1826	struct iwl_mvm_sta *mvmsta;
1827	u8 sta_id;
1828
1829	sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1830		mvm->tvqm_info[hw_queue].sta_id :
1831		mvm->queue_info[hw_queue].ra_sta_id;
1832
1833	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
1834		return;
1835
1836	rcu_read_lock();
1837
1838	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1839	if (IS_ERR_OR_NULL(sta))
1840		goto out;
1841	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1842
1843	if (iwl_mvm_is_static_queue(mvm, hw_queue)) {
1844		if (!start)
1845			ieee80211_stop_queues(mvm->hw);
1846		else if (mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1847			ieee80211_wake_queues(mvm->hw);
1848
1849		goto out;
1850	}
1851
1852	if (iwl_mvm_has_new_tx_api(mvm)) {
1853		int tid = mvm->tvqm_info[hw_queue].txq_tid;
1854
1855		tid_bitmap = BIT(tid);
1856	} else {
1857		tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1858	}
1859
1860	for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1861		int tid = i;
1862
1863		if (tid == IWL_MAX_TID_COUNT)
1864			tid = IEEE80211_NUM_TIDS;
1865
1866		txq = sta->txq[tid];
1867		mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1868		if (start)
1869			clear_bit(IWL_MVM_TXQ_STATE_STOP_FULL, &mvmtxq->state);
1870		else
1871			set_bit(IWL_MVM_TXQ_STATE_STOP_FULL, &mvmtxq->state);
1872
1873		if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST) {
1874			local_bh_disable();
1875			iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1876			local_bh_enable();
1877		}
1878	}
1879
1880out:
1881	rcu_read_unlock();
1882}
1883
1884static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1885{
1886	iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1887}
1888
1889static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1890{
1891	iwl_mvm_queue_state_change(op_mode, hw_queue, true);
1892}
1893
1894static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1895{
1896	wiphy_rfkill_set_hw_state(mvm->hw->wiphy,
1897				  iwl_mvm_is_radio_killed(mvm));
1898}
1899
1900void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1901{
1902	if (state)
1903		set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1904	else
1905		clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1906
1907	iwl_mvm_set_rfkill_state(mvm);
1908}
1909
1910struct iwl_mvm_csme_conn_info *iwl_mvm_get_csme_conn_info(struct iwl_mvm *mvm)
1911{
1912	return rcu_dereference_protected(mvm->csme_conn_info,
1913					 lockdep_is_held(&mvm->mutex));
1914}
1915
1916static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1917{
1918	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1919	bool rfkill_safe_init_done = READ_ONCE(mvm->rfkill_safe_init_done);
1920	bool unified = iwl_mvm_has_unified_ucode(mvm);
1921
1922	if (state)
1923		set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1924	else
1925		clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1926
1927	iwl_mvm_set_rfkill_state(mvm);
1928
1929	 /* iwl_run_init_mvm_ucode is waiting for results, abort it. */
1930	if (rfkill_safe_init_done)
1931		iwl_abort_notification_waits(&mvm->notif_wait);
1932
1933	/*
1934	 * Don't ask the transport to stop the firmware. We'll do it
1935	 * after cfg80211 takes us down.
1936	 */
1937	if (unified)
1938		return false;
1939
1940	/*
1941	 * Stop the device if we run OPERATIONAL firmware or if we are in the
1942	 * middle of the calibrations.
1943	 */
1944	return state && rfkill_safe_init_done;
1945}
1946
1947static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1948{
1949	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1950	struct ieee80211_tx_info *info;
1951
1952	info = IEEE80211_SKB_CB(skb);
1953	iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1954	ieee80211_free_txskb(mvm->hw, skb);
1955}
1956
1957static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode,
1958			      enum iwl_fw_error_type type)
1959{
1960	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1961
1962	iwl_abort_notification_waits(&mvm->notif_wait);
1963	iwl_dbg_tlv_del_timers(mvm->trans);
1964
1965	if (type == IWL_ERR_TYPE_CMD_QUEUE_FULL)
1966		IWL_ERR(mvm, "Command queue full!\n");
1967	else if (!iwl_trans_is_dead(mvm->trans) &&
1968		 !test_and_clear_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE,
1969				     &mvm->status))
1970		iwl_mvm_dump_nic_error_log(mvm);
1971
1972	/*
1973	 * This should be first thing before trying to collect any
1974	 * data to avoid endless loops if any HW error happens while
1975	 * collecting debug data.
1976	 * It might not actually be true that we'll restart, but the
1977	 * setting of the bit doesn't matter if we're going to be
1978	 * unbound either.
1979	 */
1980	if (type != IWL_ERR_TYPE_RESET_HS_TIMEOUT)
1981		set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1982}
1983
1984static void iwl_mvm_dump_error(struct iwl_op_mode *op_mode,
1985			       struct iwl_fw_error_dump_mode *mode)
1986{
1987	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1988
1989	/* if we come in from opmode we have the mutex held */
1990	if (mode->context == IWL_ERR_CONTEXT_FROM_OPMODE) {
1991		lockdep_assert_held(&mvm->mutex);
1992		iwl_fw_error_collect(&mvm->fwrt);
1993	} else {
1994		mutex_lock(&mvm->mutex);
1995		if (mode->context != IWL_ERR_CONTEXT_ABORT)
1996			iwl_fw_error_collect(&mvm->fwrt);
1997		mutex_unlock(&mvm->mutex);
1998	}
1999}
2000
2001static bool iwl_mvm_sw_reset(struct iwl_op_mode *op_mode,
2002			     enum iwl_fw_error_type type)
2003{
2004	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2005
2006	/*
2007	 * If the firmware crashes while we're already considering it
2008	 * to be dead then don't ask for a restart, that cannot do
2009	 * anything useful anyway.
2010	 */
2011	if (!test_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status))
2012		return false;
2013
2014	/*
2015	 * This is a bit racy, but worst case we tell mac80211 about
2016	 * a stopped/aborted scan when that was already done which
2017	 * is not a problem. It is necessary to abort any os scan
2018	 * here because mac80211 requires having the scan cleared
2019	 * before restarting.
2020	 * We'll reset the scan_status to NONE in restart cleanup in
2021	 * the next start() call from mac80211. If restart isn't called
2022	 * (no fw restart) scan status will stay busy.
2023	 */
2024	iwl_mvm_report_scan_aborted(mvm);
2025
2026	/*
2027	 * If INIT fw asserted, it will likely fail again.
2028	 * If WoWLAN fw asserted, don't restart either, mac80211
2029	 * can't recover this since we're already half suspended.
2030	 */
2031	if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR && mvm->hw_registered) {
2032		if (mvm->fw->ucode_capa.error_log_size) {
2033			u32 src_size = mvm->fw->ucode_capa.error_log_size;
2034			u32 src_addr = mvm->fw->ucode_capa.error_log_addr;
2035			u8 *recover_buf = kzalloc(src_size, GFP_ATOMIC);
2036
2037			if (recover_buf) {
2038				mvm->error_recovery_buf = recover_buf;
2039				iwl_trans_read_mem_bytes(mvm->trans,
2040							 src_addr,
2041							 recover_buf,
2042							 src_size);
2043			}
2044		}
2045
2046		if (mvm->fwrt.trans->dbg.restart_required) {
2047			IWL_DEBUG_INFO(mvm, "FW restart requested after debug collection\n");
2048			mvm->fwrt.trans->dbg.restart_required = false;
2049			ieee80211_restart_hw(mvm->hw);
2050			return true;
2051		} else if (mvm->trans->mac_cfg->device_family <= IWL_DEVICE_FAMILY_8000) {
2052			ieee80211_restart_hw(mvm->hw);
2053			return true;
2054		}
2055	}
2056
2057	return false;
2058}
2059
2060static void iwl_op_mode_mvm_time_point(struct iwl_op_mode *op_mode,
2061				       enum iwl_fw_ini_time_point tp_id,
2062				       union iwl_dbg_tlv_tp_data *tp_data)
2063{
2064	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2065
2066	iwl_dbg_tlv_time_point(&mvm->fwrt, tp_id, tp_data);
2067}
2068
2069static void iwl_mvm_dump(struct iwl_op_mode *op_mode)
2070{
2071	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2072	struct iwl_fw_runtime *fwrt = &mvm->fwrt;
2073
2074	if (!iwl_trans_fw_running(fwrt->trans))
2075		return;
2076
2077	iwl_dbg_tlv_time_point(fwrt, IWL_FW_INI_TIME_POINT_USER_TRIGGER, NULL);
2078}
2079
2080#ifdef CONFIG_PM_SLEEP
2081static void iwl_op_mode_mvm_device_powered_off(struct iwl_op_mode *op_mode)
2082{
2083	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2084
2085	mutex_lock(&mvm->mutex);
2086	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
2087	iwl_mvm_stop_device(mvm);
2088	mvm->fast_resume = false;
2089	mutex_unlock(&mvm->mutex);
2090}
2091#else
2092static void iwl_op_mode_mvm_device_powered_off(struct iwl_op_mode *op_mode)
2093{}
2094#endif
2095
2096#define IWL_MVM_COMMON_OPS					\
2097	/* these could be differentiated */			\
2098	.queue_full = iwl_mvm_stop_sw_queue,			\
2099	.queue_not_full = iwl_mvm_wake_sw_queue,		\
2100	.hw_rf_kill = iwl_mvm_set_hw_rfkill_state,		\
2101	.free_skb = iwl_mvm_free_skb,				\
2102	.nic_error = iwl_mvm_nic_error,				\
2103	.dump_error = iwl_mvm_dump_error,			\
2104	.sw_reset = iwl_mvm_sw_reset,				\
2105	.nic_config = iwl_mvm_nic_config,			\
2106	/* as we only register one, these MUST be common! */	\
2107	.start = iwl_op_mode_mvm_start,				\
2108	.stop = iwl_op_mode_mvm_stop,				\
2109	.time_point = iwl_op_mode_mvm_time_point,		\
2110	.device_powered_off = iwl_op_mode_mvm_device_powered_off
2111
2112static const struct iwl_op_mode_ops iwl_mvm_ops = {
2113	IWL_MVM_COMMON_OPS,
2114	.rx = iwl_mvm_rx,
2115};
2116
2117static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
2118			      struct napi_struct *napi,
2119			      struct iwl_rx_cmd_buffer *rxb,
2120			      unsigned int queue)
2121{
2122	struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
2123	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2124	u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
2125
2126	if (unlikely(queue >= mvm->trans->info.num_rxqs))
2127		return;
2128
2129	if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
2130		iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
2131	else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
2132					 RX_QUEUES_NOTIFICATION)))
2133		iwl_mvm_rx_queue_notif(mvm, napi, rxb, queue);
2134	else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
2135		iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
2136}
2137
2138static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
2139	IWL_MVM_COMMON_OPS,
2140	.rx = iwl_mvm_rx_mq,
2141	.rx_rss = iwl_mvm_rx_mq_rss,
2142	.dump = iwl_mvm_dump,
2143};
~/ Read-only file preview.