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