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