iwlwifi: mvm: support CHANNEL_SWITCH_TIME_EVENT_CMD command
[linux-2.6-block.git] / drivers / net / wireless / intel / iwlwifi / mvm / ops.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
4fb06283 9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
de8ba41b 10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
8745f12a 11 * Copyright(c) 2018 Intel Corporation
8ca151b5
JB
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
8ca151b5 22 * The full GNU General Public License is included in this distribution
410dc5aa 23 * in the file called COPYING.
8ca151b5
JB
24 *
25 * Contact Information:
cb2f8277 26 * Intel Linux Wireless <linuxwifi@intel.com>
8ca151b5
JB
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 * BSD LICENSE
30 *
51368bf7 31 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
4fb06283 32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
de8ba41b 33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
8745f12a 34 * Copyright(c) 2018 Intel Corporation
8ca151b5
JB
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
46 * distribution.
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 *****************************************************************************/
64#include <linux/module.h>
1bd3cbc1 65#include <linux/vmalloc.h>
8ca151b5
JB
66#include <net/mac80211.h>
67
9fca9d5c 68#include "fw/notif-wait.h"
8ca151b5
JB
69#include "iwl-trans.h"
70#include "iwl-op-mode.h"
d962f9b1 71#include "fw/img.h"
8ca151b5
JB
72#include "iwl-debug.h"
73#include "iwl-drv.h"
74#include "iwl-modparams.h"
75#include "mvm.h"
76#include "iwl-phy-db.h"
77#include "iwl-eeprom-parse.h"
78#include "iwl-csr.h"
79#include "iwl-io.h"
80#include "iwl-prph.h"
81#include "rs.h"
d172a5ef 82#include "fw/api/scan.h"
8ca151b5 83#include "time-event.h"
39bdb17e 84#include "fw-api.h"
d172a5ef 85#include "fw/api/scan.h"
f2abcfa6 86#include "fw/acpi.h"
8ca151b5 87
8ca151b5 88#define DRV_DESCRIPTION "The new Intel(R) wireless AGN driver for Linux"
8ca151b5 89MODULE_DESCRIPTION(DRV_DESCRIPTION);
8ca151b5
JB
90MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
91MODULE_LICENSE("GPL");
92
93static const struct iwl_op_mode_ops iwl_mvm_ops;
0316d30e 94static const struct iwl_op_mode_ops iwl_mvm_ops_mq;
8ca151b5
JB
95
96struct iwl_mvm_mod_params iwlmvm_mod_params = {
97 .power_scheme = IWL_POWER_SCHEME_BPS,
ce71c2f7 98 .tfd_q_hang_detect = true
8ca151b5
JB
99 /* rest of fields are 0 by default */
100};
101
2ef00c53 102module_param_named(init_dbg, iwlmvm_mod_params.init_dbg, bool, 0444);
8ca151b5
JB
103MODULE_PARM_DESC(init_dbg,
104 "set to true to debug an ASSERT in INIT fw (default: false");
2ef00c53 105module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, 0444);
8ca151b5
JB
106MODULE_PARM_DESC(power_scheme,
107 "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
ce71c2f7 108module_param_named(tfd_q_hang_detect, iwlmvm_mod_params.tfd_q_hang_detect,
2ef00c53 109 bool, 0444);
ce71c2f7
EG
110MODULE_PARM_DESC(tfd_q_hang_detect,
111 "TFD queues hang detection (default: true");
8ca151b5
JB
112
113/*
114 * module init and exit functions
115 */
116static int __init iwl_mvm_init(void)
117{
118 int ret;
119
120 ret = iwl_mvm_rate_control_register();
121 if (ret) {
122 pr_err("Unable to register rate control algorithm: %d\n", ret);
123 return ret;
124 }
125
126 ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
9f66a397 127 if (ret)
8ca151b5 128 pr_err("Unable to register MVM op_mode: %d\n", ret);
8ca151b5
JB
129
130 return ret;
131}
132module_init(iwl_mvm_init);
133
134static void __exit iwl_mvm_exit(void)
135{
136 iwl_opmode_deregister("iwlmvm");
137 iwl_mvm_rate_control_unregister();
138}
139module_exit(iwl_mvm_exit);
140
141static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
142{
143 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
144 u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
145 u32 reg_val = 0;
a0544272
MH
146 u32 phy_config = iwl_mvm_get_phy_config(mvm);
147
148 radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
149 FW_PHY_CFG_RADIO_TYPE_POS;
150 radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
151 FW_PHY_CFG_RADIO_STEP_POS;
152 radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
153 FW_PHY_CFG_RADIO_DASH_POS;
8ca151b5
JB
154
155 /* SKU control */
156 reg_val |= CSR_HW_REV_STEP(mvm->trans->hw_rev) <<
157 CSR_HW_IF_CONFIG_REG_POS_MAC_STEP;
158 reg_val |= CSR_HW_REV_DASH(mvm->trans->hw_rev) <<
159 CSR_HW_IF_CONFIG_REG_POS_MAC_DASH;
160
161 /* radio configuration */
162 reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
163 reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
164 reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
165
166 WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
167 ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
168
9b1fcc11 169 /*
6e584873
SS
170 * TODO: Bits 7-8 of CSR in 8000 HW family and higher set the ADC
171 * sampling, and shouldn't be set to any non-zero value.
172 * The same is supposed to be true of the other HW, but unsetting
173 * them (such as the 7260) causes automatic tests to fail on seemingly
174 * unrelated errors. Need to further investigate this, but for now
175 * we'll separate cases.
9b1fcc11 176 */
6e584873 177 if (mvm->trans->cfg->device_family < IWL_DEVICE_FAMILY_8000)
9b1fcc11 178 reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
8ca151b5 179
2d8c2615
SM
180 if (iwl_fw_dbg_is_d3_debug_enabled(&mvm->fwrt))
181 reg_val |= CSR_HW_IF_CONFIG_REG_D3_DEBUG;
182
e139dc4a
LE
183 iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
184 CSR_HW_IF_CONFIG_REG_MSK_MAC_DASH |
185 CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP |
186 CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
187 CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
188 CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
189 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
2d8c2615
SM
190 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI |
191 CSR_HW_IF_CONFIG_REG_D3_DEBUG,
e139dc4a 192 reg_val);
8ca151b5
JB
193
194 IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
195 radio_cfg_step, radio_cfg_dash);
196
197 /*
198 * W/A : NIC is stuck in a reset state after Early PCIe power off
199 * (PCIe power is lost before PERST# is asserted), causing ME FW
200 * to lose ownership and not being able to obtain it back.
201 */
95411d04 202 if (!mvm->trans->cfg->apmg_not_supported)
3073d8c0
EH
203 iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
204 APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
205 ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
8ca151b5
JB
206}
207
c9cb14a6
CRI
208/**
209 * enum iwl_rx_handler_context context for Rx handler
210 * @RX_HANDLER_SYNC : this means that it will be called in the Rx path
211 * which can't acquire mvm->mutex.
212 * @RX_HANDLER_ASYNC_LOCKED : If the handler needs to hold mvm->mutex
213 * (and only in this case!), it should be set as ASYNC. In that case,
214 * it will be called from a worker with mvm->mutex held.
215 * @RX_HANDLER_ASYNC_UNLOCKED : in case the handler needs to lock the
216 * mutex itself, it will be called from a worker without mvm->mutex held.
217 */
218enum iwl_rx_handler_context {
219 RX_HANDLER_SYNC,
220 RX_HANDLER_ASYNC_LOCKED,
221 RX_HANDLER_ASYNC_UNLOCKED,
222};
223
224/**
225 * struct iwl_rx_handlers handler for FW notification
226 * @cmd_id: command id
227 * @context: see &iwl_rx_handler_context
228 * @fn: the function is called when notification is received
229 */
8ca151b5 230struct iwl_rx_handlers {
1230b16b 231 u16 cmd_id;
c9cb14a6 232 enum iwl_rx_handler_context context;
0416841d 233 void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
8ca151b5
JB
234};
235
c9cb14a6
CRI
236#define RX_HANDLER(_cmd_id, _fn, _context) \
237 { .cmd_id = _cmd_id, .fn = _fn, .context = _context }
238#define RX_HANDLER_GRP(_grp, _cmd, _fn, _context) \
239 { .cmd_id = WIDE_ID(_grp, _cmd), .fn = _fn, .context = _context }
8ca151b5
JB
240
241/*
242 * Handlers for fw notifications
243 * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
244 * This list should be in order of frequency for performance purposes.
245 *
c9cb14a6 246 * The handler can be one from three contexts, see &iwl_rx_handler_context
8ca151b5
JB
247 */
248static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
c9cb14a6
CRI
249 RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, RX_HANDLER_SYNC),
250 RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, RX_HANDLER_SYNC),
251
84226ca1
GG
252 RX_HANDLER_GRP(DATA_PATH_GROUP, TLC_MNG_UPDATE_NOTIF,
253 iwl_mvm_tlc_update_notif, RX_HANDLER_SYNC),
254
c9cb14a6
CRI
255 RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_notif,
256 RX_HANDLER_ASYNC_LOCKED),
257 RX_HANDLER(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif,
258 RX_HANDLER_ASYNC_LOCKED),
259 RX_HANDLER(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics,
260 RX_HANDLER_ASYNC_LOCKED),
f421f9c3 261
3af512d6 262 RX_HANDLER(BA_WINDOW_STATUS_NOTIFICATION_ID,
c9cb14a6 263 iwl_mvm_window_status_notif, RX_HANDLER_SYNC),
3af512d6 264
c9cb14a6
CRI
265 RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif,
266 RX_HANDLER_SYNC),
267 RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc,
268 RX_HANDLER_ASYNC_LOCKED),
497b49d2 269
c9cb14a6 270 RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, RX_HANDLER_SYNC),
3e56eadf 271
e5d74646 272 RX_HANDLER(SCAN_ITERATION_COMPLETE,
c9cb14a6 273 iwl_mvm_rx_lmac_scan_iter_complete_notif, RX_HANDLER_SYNC),
35a000b7 274 RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
c9cb14a6
CRI
275 iwl_mvm_rx_lmac_scan_complete_notif,
276 RX_HANDLER_ASYNC_LOCKED),
6e56f01d 277 RX_HANDLER(MATCH_FOUND_NOTIFICATION, iwl_mvm_rx_scan_match_found,
c9cb14a6 278 RX_HANDLER_SYNC),
d2496221 279 RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
c9cb14a6 280 RX_HANDLER_ASYNC_LOCKED),
ee9219b2 281 RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
c9cb14a6 282 iwl_mvm_rx_umac_scan_iter_complete_notif, RX_HANDLER_SYNC),
497b49d2 283
c9cb14a6
CRI
284 RX_HANDLER(CARD_STATE_NOTIFICATION, iwl_mvm_rx_card_state_notif,
285 RX_HANDLER_SYNC),
8ca151b5 286
d64048ed 287 RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
c9cb14a6 288 RX_HANDLER_SYNC),
d64048ed 289
c9cb14a6 290 RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, RX_HANDLER_SYNC),
175a70b7 291 RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
c9cb14a6
CRI
292 iwl_mvm_power_uapsd_misbehaving_ap_notif, RX_HANDLER_SYNC),
293 RX_HANDLER(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif,
294 RX_HANDLER_ASYNC_LOCKED),
09eef330 295 RX_HANDLER_GRP(PHY_OPS_GROUP, DTS_MEASUREMENT_NOTIF_WIDE,
ec77a33e 296 iwl_mvm_temp_notif, RX_HANDLER_ASYNC_UNLOCKED),
0a3b7119 297 RX_HANDLER_GRP(PHY_OPS_GROUP, CT_KILL_NOTIFICATION,
c9cb14a6 298 iwl_mvm_ct_kill_notif, RX_HANDLER_SYNC),
ea9af24d 299
1d3c3f63 300 RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
c9cb14a6
CRI
301 RX_HANDLER_ASYNC_LOCKED),
302 RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif,
303 RX_HANDLER_SYNC),
bdccdb85
GBA
304 RX_HANDLER_GRP(DEBUG_GROUP, MFU_ASSERT_DUMP_NTF,
305 iwl_mvm_mfu_assert_dump_notif, RX_HANDLER_SYNC),
0db056d3 306 RX_HANDLER_GRP(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF,
c9cb14a6 307 iwl_mvm_rx_stored_beacon_notif, RX_HANDLER_SYNC),
f92659a1 308 RX_HANDLER_GRP(DATA_PATH_GROUP, MU_GROUP_MGMT_NOTIF,
c9cb14a6 309 iwl_mvm_mu_mimo_grp_notif, RX_HANDLER_SYNC),
65e25482
JB
310 RX_HANDLER_GRP(DATA_PATH_GROUP, STA_PM_NOTIF,
311 iwl_mvm_sta_pm_notif, RX_HANDLER_SYNC),
8ca151b5
JB
312};
313#undef RX_HANDLER
1230b16b 314#undef RX_HANDLER_GRP
39bdb17e
SD
315
316/* Please keep this array *SORTED* by hex value.
317 * Access is done through binary search
318 */
319static const struct iwl_hcmd_names iwl_mvm_legacy_names[] = {
320 HCMD_NAME(MVM_ALIVE),
321 HCMD_NAME(REPLY_ERROR),
322 HCMD_NAME(ECHO_CMD),
323 HCMD_NAME(INIT_COMPLETE_NOTIF),
324 HCMD_NAME(PHY_CONTEXT_CMD),
325 HCMD_NAME(DBG_CFG),
39bdb17e
SD
326 HCMD_NAME(SCAN_CFG_CMD),
327 HCMD_NAME(SCAN_REQ_UMAC),
328 HCMD_NAME(SCAN_ABORT_UMAC),
329 HCMD_NAME(SCAN_COMPLETE_UMAC),
3af512d6 330 HCMD_NAME(BA_WINDOW_STATUS_NOTIFICATION_ID),
39bdb17e
SD
331 HCMD_NAME(ADD_STA_KEY),
332 HCMD_NAME(ADD_STA),
333 HCMD_NAME(REMOVE_STA),
334 HCMD_NAME(FW_GET_ITEM_CMD),
335 HCMD_NAME(TX_CMD),
336 HCMD_NAME(SCD_QUEUE_CFG),
337 HCMD_NAME(TXPATH_FLUSH),
338 HCMD_NAME(MGMT_MCAST_KEY),
339 HCMD_NAME(WEP_KEY),
340 HCMD_NAME(SHARED_MEM_CFG),
341 HCMD_NAME(TDLS_CHANNEL_SWITCH_CMD),
342 HCMD_NAME(MAC_CONTEXT_CMD),
343 HCMD_NAME(TIME_EVENT_CMD),
344 HCMD_NAME(TIME_EVENT_NOTIFICATION),
345 HCMD_NAME(BINDING_CONTEXT_CMD),
346 HCMD_NAME(TIME_QUOTA_CMD),
347 HCMD_NAME(NON_QOS_TX_COUNTER_CMD),
7089ae63 348 HCMD_NAME(LEDS_CMD),
39bdb17e
SD
349 HCMD_NAME(LQ_CMD),
350 HCMD_NAME(FW_PAGING_BLOCK_CMD),
351 HCMD_NAME(SCAN_OFFLOAD_REQUEST_CMD),
352 HCMD_NAME(SCAN_OFFLOAD_ABORT_CMD),
353 HCMD_NAME(HOT_SPOT_CMD),
354 HCMD_NAME(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
39bdb17e
SD
355 HCMD_NAME(BT_COEX_UPDATE_REDUCED_TXP),
356 HCMD_NAME(BT_COEX_CI),
357 HCMD_NAME(PHY_CONFIGURATION_CMD),
358 HCMD_NAME(CALIB_RES_NOTIF_PHY_DB),
176aa60b 359 HCMD_NAME(PHY_DB_CMD),
39bdb17e
SD
360 HCMD_NAME(SCAN_OFFLOAD_COMPLETE),
361 HCMD_NAME(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
39bdb17e
SD
362 HCMD_NAME(POWER_TABLE_CMD),
363 HCMD_NAME(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
364 HCMD_NAME(REPLY_THERMAL_MNG_BACKOFF),
365 HCMD_NAME(DC2DC_CONFIG_CMD),
366 HCMD_NAME(NVM_ACCESS_CMD),
39bdb17e
SD
367 HCMD_NAME(BEACON_NOTIFICATION),
368 HCMD_NAME(BEACON_TEMPLATE_CMD),
369 HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
370 HCMD_NAME(BT_CONFIG),
371 HCMD_NAME(STATISTICS_CMD),
372 HCMD_NAME(STATISTICS_NOTIFICATION),
373 HCMD_NAME(EOSP_NOTIFICATION),
374 HCMD_NAME(REDUCE_TX_POWER_CMD),
39bdb17e
SD
375 HCMD_NAME(CARD_STATE_NOTIFICATION),
376 HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
377 HCMD_NAME(TDLS_CONFIG_CMD),
378 HCMD_NAME(MAC_PM_POWER_TABLE),
379 HCMD_NAME(TDLS_CHANNEL_SWITCH_NOTIFICATION),
380 HCMD_NAME(MFUART_LOAD_NOTIFICATION),
43413a97 381 HCMD_NAME(RSS_CONFIG_CMD),
39bdb17e
SD
382 HCMD_NAME(SCAN_ITERATION_COMPLETE_UMAC),
383 HCMD_NAME(REPLY_RX_PHY_CMD),
384 HCMD_NAME(REPLY_RX_MPDU_CMD),
3e73aa3b 385 HCMD_NAME(FRAME_RELEASE),
39bdb17e
SD
386 HCMD_NAME(BA_NOTIF),
387 HCMD_NAME(MCC_UPDATE_CMD),
388 HCMD_NAME(MCC_CHUB_UPDATE_CMD),
389 HCMD_NAME(MARKER_CMD),
39bdb17e
SD
390 HCMD_NAME(BT_PROFILE_NOTIFICATION),
391 HCMD_NAME(BCAST_FILTER_CMD),
392 HCMD_NAME(MCAST_FILTER_CMD),
393 HCMD_NAME(REPLY_SF_CFG_CMD),
394 HCMD_NAME(REPLY_BEACON_FILTERING_CMD),
395 HCMD_NAME(D3_CONFIG_CMD),
396 HCMD_NAME(PROT_OFFLOAD_CONFIG_CMD),
397 HCMD_NAME(OFFLOADS_QUERY_CMD),
398 HCMD_NAME(REMOTE_WAKE_CONFIG_CMD),
399 HCMD_NAME(MATCH_FOUND_NOTIFICATION),
39bdb17e
SD
400 HCMD_NAME(DTS_MEASUREMENT_NOTIFICATION),
401 HCMD_NAME(WOWLAN_PATTERNS),
402 HCMD_NAME(WOWLAN_CONFIGURATION),
403 HCMD_NAME(WOWLAN_TSC_RSC_PARAM),
404 HCMD_NAME(WOWLAN_TKIP_PARAM),
405 HCMD_NAME(WOWLAN_KEK_KCK_MATERIAL),
406 HCMD_NAME(WOWLAN_GET_STATUSES),
39bdb17e
SD
407 HCMD_NAME(SCAN_ITERATION_COMPLETE),
408 HCMD_NAME(D0I3_END_CMD),
409 HCMD_NAME(LTR_CONFIG),
8ca151b5 410};
39bdb17e 411
5b086414
GBA
412/* Please keep this array *SORTED* by hex value.
413 * Access is done through binary search
414 */
415static const struct iwl_hcmd_names iwl_mvm_system_names[] = {
416 HCMD_NAME(SHARED_MEM_CFG_CMD),
4399caaa 417 HCMD_NAME(INIT_EXTENDED_CFG_CMD),
5b086414
GBA
418};
419
03098268
AE
420/* Please keep this array *SORTED* by hex value.
421 * Access is done through binary search
422 */
423static const struct iwl_hcmd_names iwl_mvm_mac_conf_names[] = {
74a10252 424 HCMD_NAME(CHANNEL_SWITCH_TIME_EVENT_CMD),
d3a108a4 425 HCMD_NAME(CHANNEL_SWITCH_NOA_NOTIF),
03098268
AE
426};
427
39bdb17e
SD
428/* Please keep this array *SORTED* by hex value.
429 * Access is done through binary search
430 */
431static const struct iwl_hcmd_names iwl_mvm_phy_names[] = {
432 HCMD_NAME(CMD_DTS_MEASUREMENT_TRIGGER_WIDE),
5c89e7bc 433 HCMD_NAME(CTDP_CONFIG_CMD),
c221daf2 434 HCMD_NAME(TEMP_REPORTING_THRESHOLDS_CMD),
a6bff3cb 435 HCMD_NAME(GEO_TX_POWER_LIMIT),
0a3b7119 436 HCMD_NAME(CT_KILL_NOTIFICATION),
39bdb17e
SD
437 HCMD_NAME(DTS_MEASUREMENT_NOTIF_WIDE),
438};
439
e0d8fdec
SS
440/* Please keep this array *SORTED* by hex value.
441 * Access is done through binary search
442 */
443static const struct iwl_hcmd_names iwl_mvm_data_path_names[] = {
ddef2f98 444 HCMD_NAME(DQA_ENABLE_CMD),
e0d8fdec 445 HCMD_NAME(UPDATE_MU_GROUPS_CMD),
94bb4481 446 HCMD_NAME(TRIGGER_RX_QUEUES_NOTIF_CMD),
514c3069 447 HCMD_NAME(STA_HE_CTXT_CMD),
8edbfaa1 448 HCMD_NAME(RFH_QUEUE_CONFIG_CMD),
5213e8a8 449 HCMD_NAME(CHEST_COLLECTOR_FILTER_CONFIG_CMD),
65e25482 450 HCMD_NAME(STA_PM_NOTIF),
f92659a1 451 HCMD_NAME(MU_GROUP_MGMT_NOTIF),
94bb4481 452 HCMD_NAME(RX_QUEUES_NOTIFICATION),
e0d8fdec
SS
453};
454
bdccdb85
GBA
455/* Please keep this array *SORTED* by hex value.
456 * Access is done through binary search
457 */
458static const struct iwl_hcmd_names iwl_mvm_debug_names[] = {
459 HCMD_NAME(MFU_ASSERT_DUMP_NTF),
460};
461
6c161980
JB
462/* Please keep this array *SORTED* by hex value.
463 * Access is done through binary search
464 */
465static const struct iwl_hcmd_names iwl_mvm_location_names[] = {
466 HCMD_NAME(TOF_RANGE_REQ_CMD),
467 HCMD_NAME(TOF_CONFIG_CMD),
468 HCMD_NAME(TOF_RANGE_ABORT_CMD),
469 HCMD_NAME(TOF_RANGE_REQ_EXT_CMD),
470 HCMD_NAME(TOF_RESPONDER_CONFIG_CMD),
471 HCMD_NAME(TOF_RESPONDER_DYN_CONFIG_CMD),
472 HCMD_NAME(TOF_LC_NOTIF),
473 HCMD_NAME(TOF_RESPONDER_STATS),
474 HCMD_NAME(TOF_MCSI_DEBUG_NOTIF),
475 HCMD_NAME(TOF_RANGE_RESPONSE_NOTIF),
476};
477
0db056d3
SS
478/* Please keep this array *SORTED* by hex value.
479 * Access is done through binary search
480 */
481static const struct iwl_hcmd_names iwl_mvm_prot_offload_names[] = {
482 HCMD_NAME(STORED_BEACON_NTF),
483};
484
1f370650
SS
485/* Please keep this array *SORTED* by hex value.
486 * Access is done through binary search
487 */
488static const struct iwl_hcmd_names iwl_mvm_regulatory_and_nvm_names[] = {
489 HCMD_NAME(NVM_ACCESS_COMPLETE),
e9e1ba3d 490 HCMD_NAME(NVM_GET_INFO),
1f370650
SS
491};
492
39bdb17e
SD
493static const struct iwl_hcmd_arr iwl_mvm_groups[] = {
494 [LEGACY_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
495 [LONG_GROUP] = HCMD_ARR(iwl_mvm_legacy_names),
5b086414 496 [SYSTEM_GROUP] = HCMD_ARR(iwl_mvm_system_names),
03098268 497 [MAC_CONF_GROUP] = HCMD_ARR(iwl_mvm_mac_conf_names),
39bdb17e 498 [PHY_OPS_GROUP] = HCMD_ARR(iwl_mvm_phy_names),
e0d8fdec 499 [DATA_PATH_GROUP] = HCMD_ARR(iwl_mvm_data_path_names),
6c161980 500 [LOCATION_GROUP] = HCMD_ARR(iwl_mvm_location_names),
0db056d3 501 [PROT_OFFLOAD_GROUP] = HCMD_ARR(iwl_mvm_prot_offload_names),
1f370650
SS
502 [REGULATORY_AND_NVM_GROUP] =
503 HCMD_ARR(iwl_mvm_regulatory_and_nvm_names),
39bdb17e
SD
504};
505
8ca151b5
JB
506/* this forward declaration can avoid to export the function */
507static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
a75b9b33 508#ifdef CONFIG_PM
37577fe2 509static void iwl_mvm_d0i3_exit_work(struct work_struct *wk);
a75b9b33 510#endif
8ca151b5 511
f2abcfa6 512static u32 iwl_mvm_min_backoff(struct iwl_mvm *mvm)
0c0e2c71 513{
f2abcfa6
LC
514 const struct iwl_pwr_tx_backoff *backoff = mvm->cfg->pwr_tx_backoffs;
515 u64 dflt_pwr_limit;
0c0e2c71 516
f2abcfa6 517 if (!backoff)
0c0e2c71
IY
518 return 0;
519
f2abcfa6 520 dflt_pwr_limit = iwl_acpi_get_pwr_limit(mvm->dev);
0c0e2c71 521
f2abcfa6
LC
522 while (backoff->pwr) {
523 if (dflt_pwr_limit >= backoff->pwr)
524 return backoff->backoff;
525
526 backoff++;
0c0e2c71
IY
527 }
528
529 return 0;
530}
531
d3a108a4
AO
532static void iwl_mvm_tx_unblock_dwork(struct work_struct *work)
533{
534 struct iwl_mvm *mvm =
535 container_of(work, struct iwl_mvm, cs_tx_unblock_dwork.work);
536 struct ieee80211_vif *tx_blocked_vif;
537 struct iwl_mvm_vif *mvmvif;
538
539 mutex_lock(&mvm->mutex);
540
541 tx_blocked_vif =
542 rcu_dereference_protected(mvm->csa_tx_blocked_vif,
543 lockdep_is_held(&mvm->mutex));
544
545 if (!tx_blocked_vif)
546 goto unlock;
547
548 mvmvif = iwl_mvm_vif_from_mac80211(tx_blocked_vif);
549 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
550 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
551unlock:
552 mutex_unlock(&mvm->mutex);
553}
554
7174beb6
JB
555static int iwl_mvm_fwrt_dump_start(void *ctx)
556{
557 struct iwl_mvm *mvm = ctx;
558 int ret;
559
560 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_FW_DBG_COLLECT);
561 if (ret)
562 return ret;
563
564 mutex_lock(&mvm->mutex);
565
566 return 0;
567}
568
569static void iwl_mvm_fwrt_dump_end(void *ctx)
570{
571 struct iwl_mvm *mvm = ctx;
572
573 mutex_unlock(&mvm->mutex);
574
575 iwl_mvm_unref(mvm, IWL_MVM_REF_FW_DBG_COLLECT);
576}
577
8745f12a
ST
578static bool iwl_mvm_fwrt_fw_running(void *ctx)
579{
580 return iwl_mvm_firmware_running(ctx);
581}
582
d3f4b6de
SM
583static int iwl_mvm_fwrt_send_hcmd(void *ctx, struct iwl_host_cmd *host_cmd)
584{
585 struct iwl_mvm *mvm = (struct iwl_mvm *)ctx;
586 int ret;
587
588 mutex_lock(&mvm->mutex);
589 ret = iwl_mvm_send_cmd(mvm, host_cmd);
590 mutex_unlock(&mvm->mutex);
591
592 return ret;
593}
594
7174beb6
JB
595static const struct iwl_fw_runtime_ops iwl_mvm_fwrt_ops = {
596 .dump_start = iwl_mvm_fwrt_dump_start,
597 .dump_end = iwl_mvm_fwrt_dump_end,
8745f12a 598 .fw_running = iwl_mvm_fwrt_fw_running,
d3f4b6de 599 .send_hcmd = iwl_mvm_fwrt_send_hcmd,
7174beb6
JB
600};
601
8ca151b5
JB
602static struct iwl_op_mode *
603iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
604 const struct iwl_fw *fw, struct dentry *dbgfs_dir)
605{
606 struct ieee80211_hw *hw;
607 struct iwl_op_mode *op_mode;
608 struct iwl_mvm *mvm;
609 struct iwl_trans_config trans_cfg = {};
610 static const u8 no_reclaim_cmds[] = {
611 TX_CMD,
612 };
613 int err, scan_size;
0c0e2c71 614 u32 min_backoff;
034925cb 615 enum iwl_amsdu_size rb_size_default;
8ca151b5 616
c4d83271
EG
617 /*
618 * We use IWL_MVM_STATION_COUNT to check the validity of the station
619 * index all over the driver - check that its value corresponds to the
620 * array size.
621 */
622 BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) != IWL_MVM_STATION_COUNT);
623
8ca151b5
JB
624 /********************************
625 * 1. Allocating and configuring HW data
626 ********************************/
627 hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
628 sizeof(struct iwl_mvm),
629 &iwl_mvm_hw_ops);
630 if (!hw)
631 return NULL;
632
745160ee
OG
633 if (cfg->max_rx_agg_size)
634 hw->max_rx_aggregation_subframes = cfg->max_rx_agg_size;
1eda295f
JB
635 else
636 hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
745160ee 637
77d96730
GG
638 if (cfg->max_tx_agg_size)
639 hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
1eda295f
JB
640 else
641 hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
77d96730 642
8ca151b5 643 op_mode = hw->priv;
8ca151b5
JB
644
645 mvm = IWL_OP_MODE_GET_MVM(op_mode);
646 mvm->dev = trans->dev;
647 mvm->trans = trans;
648 mvm->cfg = cfg;
649 mvm->fw = fw;
650 mvm->hw = hw;
651
93b167c1
MG
652 iwl_fw_runtime_init(&mvm->fwrt, trans, fw, &iwl_mvm_fwrt_ops, mvm,
653 dbgfs_dir);
235acb18 654
de8ba41b
LK
655 mvm->init_status = 0;
656
0316d30e
JB
657 if (iwl_mvm_has_new_rx_api(mvm)) {
658 op_mode->ops = &iwl_mvm_ops_mq;
18ead597
GBA
659 trans->rx_mpdu_cmd_hdr_size =
660 (trans->cfg->device_family >=
661 IWL_DEVICE_FAMILY_22560) ?
662 sizeof(struct iwl_rx_mpdu_desc) :
663 IWL_RX_DESC_SIZE_V1;
0316d30e
JB
664 } else {
665 op_mode->ops = &iwl_mvm_ops;
25c2b22c
SS
666 trans->rx_mpdu_cmd_hdr_size =
667 sizeof(struct iwl_rx_mpdu_res_start);
0316d30e
JB
668
669 if (WARN_ON(trans->num_rx_queues > 1))
670 goto out_free;
671 }
672
3b37f4c9 673 mvm->fw_restart = iwlwifi_mod_params.fw_restart ? -1 : 0;
291aa7c4 674
c8f54701 675 mvm->aux_queue = IWL_MVM_DQA_AUX_QUEUE;
b13f43a4 676 mvm->snif_queue = IWL_MVM_DQA_INJECT_MONITOR_QUEUE;
c8f54701
JB
677 mvm->probe_queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
678 mvm->p2p_dev_queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
679
1f3b0ff8 680 mvm->sf_state = SF_UNINIT;
7d6222e2 681 if (iwl_mvm_has_unified_ucode(mvm))
702e975d 682 iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_REGULAR);
1f370650 683 else
702e975d 684 iwl_fw_set_current_image(&mvm->fwrt, IWL_UCODE_INIT);
c89e333d 685 mvm->drop_bcn_ap_mode = true;
19e737c9 686
8ca151b5 687 mutex_init(&mvm->mutex);
d15a747f 688 mutex_init(&mvm->d0i3_suspend_mutex);
8ca151b5
JB
689 spin_lock_init(&mvm->async_handlers_lock);
690 INIT_LIST_HEAD(&mvm->time_event_list);
b112889c 691 INIT_LIST_HEAD(&mvm->aux_roc_te_list);
8ca151b5
JB
692 INIT_LIST_HEAD(&mvm->async_handlers_list);
693 spin_lock_init(&mvm->time_event_lock);
694
695 INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
696 INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
a75b9b33 697#ifdef CONFIG_PM
37577fe2 698 INIT_WORK(&mvm->d0i3_exit_work, iwl_mvm_d0i3_exit_work);
a75b9b33 699#endif
1d3c3f63 700 INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
69e04642 701 INIT_DELAYED_WORK(&mvm->scan_timeout_dwork, iwl_mvm_scan_timeout_wk);
24afba76 702 INIT_WORK(&mvm->add_stream_wk, iwl_mvm_add_new_dqa_stream_wk);
cfbc6c4c 703 INIT_LIST_HEAD(&mvm->add_stream_txqs);
8ca151b5 704
b2492501 705 spin_lock_init(&mvm->d0i3_tx_lock);
576eeee9 706 spin_lock_init(&mvm->refs_lock);
b2492501
AN
707 skb_queue_head_init(&mvm->d0i3_tx);
708 init_waitqueue_head(&mvm->d0i3_exit_waitq);
3a732c65 709 init_waitqueue_head(&mvm->rx_sync_waitq);
b2492501 710
0636b938
SS
711 atomic_set(&mvm->queue_sync_counter, 0);
712
8ca151b5
JB
713 SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
714
7d9d0d56
LC
715 spin_lock_init(&mvm->tcm.lock);
716 INIT_DELAYED_WORK(&mvm->tcm.work, iwl_mvm_tcm_work);
717 mvm->tcm.ts = jiffies;
718 mvm->tcm.ll_ts = jiffies;
719 mvm->tcm.uapsd_nonagg_ts = jiffies;
720
d3a108a4
AO
721 INIT_DELAYED_WORK(&mvm->cs_tx_unblock_dwork, iwl_mvm_tx_unblock_dwork);
722
8ca151b5
JB
723 /*
724 * Populate the state variables that the transport layer needs
725 * to know about.
726 */
727 trans_cfg.op_mode = op_mode;
728 trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
729 trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
034925cb
ST
730
731 if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
732 rb_size_default = IWL_AMSDU_2K;
733 else
734 rb_size_default = IWL_AMSDU_4K;
735
6c4fbcbc 736 switch (iwlwifi_mod_params.amsdu_size) {
4bdd4dfe 737 case IWL_AMSDU_DEF:
034925cb
ST
738 trans_cfg.rx_buf_size = rb_size_default;
739 break;
6c4fbcbc
EG
740 case IWL_AMSDU_4K:
741 trans_cfg.rx_buf_size = IWL_AMSDU_4K;
742 break;
743 case IWL_AMSDU_8K:
744 trans_cfg.rx_buf_size = IWL_AMSDU_8K;
745 break;
746 case IWL_AMSDU_12K:
747 trans_cfg.rx_buf_size = IWL_AMSDU_12K;
748 break;
749 default:
750 pr_err("%s: Unsupported amsdu_size: %d\n", KBUILD_MODNAME,
751 iwlwifi_mod_params.amsdu_size);
034925cb 752 trans_cfg.rx_buf_size = rb_size_default;
6c4fbcbc 753 }
4bdd4dfe 754
e6aeeb4f
JB
755 BUILD_BUG_ON(sizeof(struct iwl_ldbg_config_cmd) !=
756 LDBG_CFG_COMMAND_SIZE);
757
4b87e5af 758 trans->wide_cmd_header = true;
2a182fbb
GBA
759 trans_cfg.bc_table_dword =
760 mvm->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560;
8ca151b5 761
39bdb17e
SD
762 trans_cfg.command_groups = iwl_mvm_groups;
763 trans_cfg.command_groups_size = ARRAY_SIZE(iwl_mvm_groups);
8ca151b5 764
c8f54701 765 trans_cfg.cmd_queue = IWL_MVM_DQA_CMD_QUEUE;
b2d81db7 766 trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
3a736bcb 767 trans_cfg.scd_set_active = true;
8ca151b5 768
21cb3222
JB
769 trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
770 driver_data[2]);
771
41837ca9 772 trans_cfg.sw_csum_tx = IWL_MVM_SW_TX_CSUM_OFFLOAD;
b4821767 773
4cf677fd
EG
774 /* Set a short watchdog for the command queue */
775 trans_cfg.cmd_q_wdg_timeout =
5d42e7b2 776 iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
4cf677fd 777
8ca151b5
JB
778 snprintf(mvm->hw->wiphy->fw_version,
779 sizeof(mvm->hw->wiphy->fw_version),
780 "%s", fw->fw_version);
781
782 /* Configure transport layer */
783 iwl_trans_configure(mvm->trans, &trans_cfg);
784
785 trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
17b809c9
SS
786 trans->dbg_dest_tlv = mvm->fw->dbg.dest_tlv;
787 trans->dbg_n_dest_reg = mvm->fw->dbg.n_dest_reg;
788 memcpy(trans->dbg_conf_tlv, mvm->fw->dbg.conf_tlv,
09e350f7 789 sizeof(trans->dbg_conf_tlv));
17b809c9 790 trans->dbg_trigger_tlv = mvm->fw->dbg.trigger_tlv;
8ca151b5 791
132db31c
GBA
792 trans->iml = mvm->fw->iml;
793 trans->iml_len = mvm->fw->iml_len;
794
8ca151b5
JB
795 /* set up notification wait support */
796 iwl_notification_wait_init(&mvm->notif_wait);
797
798 /* Init phy db */
799 mvm->phy_db = iwl_phy_db_init(trans);
800 if (!mvm->phy_db) {
801 IWL_ERR(mvm, "Cannot init phy_db\n");
802 goto out_free;
803 }
804
805 IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
806 mvm->cfg->name, mvm->trans->hw_rev);
807
4fb06283 808 if (iwlwifi_mod_params.nvm_file)
e02a9d60 809 mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
4fb06283
EH
810 else
811 IWL_DEBUG_EEPROM(mvm->trans->dev,
812 "working without external nvm file\n");
9ee718aa 813
56f2929b
SS
814 err = iwl_trans_start_hw(mvm->trans);
815 if (err)
14b485f0
EH
816 goto out_free;
817
56f2929b
SS
818 mutex_lock(&mvm->mutex);
819 iwl_mvm_ref(mvm, IWL_MVM_REF_INIT_UCODE);
8c5f47b1 820 err = iwl_run_init_mvm_ucode(mvm, true);
f38efdb2
SM
821 if (test_bit(IWL_FWRT_STATUS_WAIT_ALIVE, &mvm->fwrt.status))
822 iwl_fw_alive_error_dump(&mvm->fwrt);
f4744258 823 if (!iwlmvm_mod_params.init_dbg || !err)
56f2929b
SS
824 iwl_mvm_stop_device(mvm);
825 iwl_mvm_unref(mvm, IWL_MVM_REF_INIT_UCODE);
826 mutex_unlock(&mvm->mutex);
de8ba41b 827 if (err < 0) {
56f2929b
SS
828 IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", err);
829 goto out_free;
8ca151b5
JB
830 }
831
d2496221 832 scan_size = iwl_mvm_scan_size(mvm);
fb98be5e 833
8ca151b5
JB
834 mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
835 if (!mvm->scan_cmd)
836 goto out_free;
837
5a4b2afa
HD
838 /* Set EBS as successful as long as not stated otherwise by the FW. */
839 mvm->last_ebs_successful = true;
840
8ca151b5
JB
841 err = iwl_mvm_mac_setup_register(mvm);
842 if (err)
843 goto out_free;
1f370650 844 mvm->hw_registered = true;
8ca151b5 845
f2abcfa6 846 min_backoff = iwl_mvm_min_backoff(mvm);
04ddc2aa
CRI
847 iwl_mvm_thermal_initialize(mvm, min_backoff);
848
8ca151b5
JB
849 err = iwl_mvm_dbgfs_register(mvm, dbgfs_dir);
850 if (err)
851 goto out_unregister;
852
678d9b6d
LK
853 if (!iwl_mvm_has_new_rx_stats_api(mvm))
854 memset(&mvm->rx_stats_v3, 0,
855 sizeof(struct mvm_statistics_rx_v3));
856 else
857 memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
3848ab66 858
33c85ead
LC
859 /* The transport always starts with a taken reference, we can
860 * release it now if d0i3 is supported */
861 if (iwl_mvm_is_d0i3_supported(mvm))
862 iwl_trans_unref(mvm->trans);
7498cf4c 863
656fca00
AS
864 iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
865
8ca151b5
JB
866 return op_mode;
867
868 out_unregister:
de8ba41b
LK
869 if (iwlmvm_mod_params.init_dbg)
870 return op_mode;
871
8ca151b5 872 ieee80211_unregister_hw(mvm->hw);
1f370650 873 mvm->hw_registered = false;
91b0d119 874 iwl_mvm_leds_exit(mvm);
c221daf2 875 iwl_mvm_thermal_exit(mvm);
8ca151b5 876 out_free:
7174beb6 877 iwl_fw_flush_dump(&mvm->fwrt);
54f3f994 878 iwl_fw_runtime_free(&mvm->fwrt);
de8ba41b
LK
879
880 if (iwlmvm_mod_params.init_dbg)
881 return op_mode;
8ca151b5
JB
882 iwl_phy_db_free(mvm->phy_db);
883 kfree(mvm->scan_cmd);
56f2929b
SS
884 iwl_trans_op_mode_leave(trans);
885
8ca151b5
JB
886 ieee80211_free_hw(mvm->hw);
887 return NULL;
888}
889
890static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
891{
892 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
893 int i;
894
e27deb45
LC
895 /* If d0i3 is supported, we have released the reference that
896 * the transport started with, so we should take it back now
897 * that we are leaving.
898 */
899 if (iwl_mvm_is_d0i3_supported(mvm))
900 iwl_trans_ref(mvm->trans);
901
8ca151b5
JB
902 iwl_mvm_leds_exit(mvm);
903
c221daf2 904 iwl_mvm_thermal_exit(mvm);
9ee718aa 905
de8ba41b
LK
906 if (mvm->init_status & IWL_MVM_INIT_STATUS_REG_HW_INIT_COMPLETE) {
907 ieee80211_unregister_hw(mvm->hw);
908 mvm->init_status &= ~IWL_MVM_INIT_STATUS_REG_HW_INIT_COMPLETE;
909 }
8ca151b5
JB
910
911 kfree(mvm->scan_cmd);
e59647ea
EP
912 kfree(mvm->mcast_filter_cmd);
913 mvm->mcast_filter_cmd = NULL;
8ca151b5 914
afc66bb7
JB
915#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_IWLWIFI_DEBUGFS)
916 kfree(mvm->d3_resume_sram);
917#endif
a4082843 918 iwl_trans_op_mode_leave(mvm->trans);
8ca151b5
JB
919
920 iwl_phy_db_free(mvm->phy_db);
921 mvm->phy_db = NULL;
922
1dad3e0a 923 kfree(mvm->nvm_data);
ae2b21b0 924 for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
8ca151b5
JB
925 kfree(mvm->nvm_sections[i].data);
926
7d9d0d56
LC
927 cancel_delayed_work_sync(&mvm->tcm.work);
928
54f3f994 929 iwl_fw_runtime_free(&mvm->fwrt);
a2a57a35
EG
930 mutex_destroy(&mvm->mutex);
931 mutex_destroy(&mvm->d0i3_suspend_mutex);
932
8ca151b5
JB
933 ieee80211_free_hw(mvm->hw);
934}
935
936struct iwl_async_handler_entry {
937 struct list_head list;
938 struct iwl_rx_cmd_buffer rxb;
c9cb14a6 939 enum iwl_rx_handler_context context;
0416841d 940 void (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb);
8ca151b5
JB
941};
942
943void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
944{
945 struct iwl_async_handler_entry *entry, *tmp;
946
947 spin_lock_bh(&mvm->async_handlers_lock);
948 list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
949 iwl_free_rxb(&entry->rxb);
950 list_del(&entry->list);
951 kfree(entry);
952 }
953 spin_unlock_bh(&mvm->async_handlers_lock);
954}
955
956static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
957{
958 struct iwl_mvm *mvm =
959 container_of(wk, struct iwl_mvm, async_handlers_wk);
960 struct iwl_async_handler_entry *entry, *tmp;
8098203f 961 LIST_HEAD(local_list);
8ca151b5
JB
962
963 /* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
8ca151b5
JB
964
965 /*
966 * Sync with Rx path with a lock. Remove all the entries from this list,
967 * add them to a local one (lock free), and then handle them.
968 */
969 spin_lock_bh(&mvm->async_handlers_lock);
970 list_splice_init(&mvm->async_handlers_list, &local_list);
971 spin_unlock_bh(&mvm->async_handlers_lock);
972
973 list_for_each_entry_safe(entry, tmp, &local_list, list) {
c9cb14a6
CRI
974 if (entry->context == RX_HANDLER_ASYNC_LOCKED)
975 mutex_lock(&mvm->mutex);
0416841d 976 entry->fn(mvm, &entry->rxb);
8ca151b5
JB
977 iwl_free_rxb(&entry->rxb);
978 list_del(&entry->list);
c9cb14a6
CRI
979 if (entry->context == RX_HANDLER_ASYNC_LOCKED)
980 mutex_unlock(&mvm->mutex);
8ca151b5
JB
981 kfree(entry);
982 }
8ca151b5
JB
983}
984
917f39bb
EG
985static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
986 struct iwl_rx_packet *pkt)
987{
988 struct iwl_fw_dbg_trigger_tlv *trig;
989 struct iwl_fw_dbg_trigger_cmd *cmds_trig;
917f39bb
EG
990 int i;
991
6c042d75
SS
992 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL,
993 FW_DBG_TRIGGER_FW_NOTIF);
994 if (!trig)
917f39bb
EG
995 return;
996
917f39bb
EG
997 cmds_trig = (void *)trig->data;
998
917f39bb
EG
999 for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
1000 /* don't collect on CMD 0 */
1001 if (!cmds_trig->cmds[i].cmd_id)
1002 break;
1003
0ab66e6d
SS
1004 if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd ||
1005 cmds_trig->cmds[i].group_id != pkt->hdr.group_id)
917f39bb
EG
1006 continue;
1007
7174beb6
JB
1008 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1009 "CMD 0x%02x.%02x received",
1010 pkt->hdr.group_id, pkt->hdr.cmd);
917f39bb
EG
1011 break;
1012 }
1013}
1014
0316d30e
JB
1015static void iwl_mvm_rx_common(struct iwl_mvm *mvm,
1016 struct iwl_rx_cmd_buffer *rxb,
1017 struct iwl_rx_packet *pkt)
8ca151b5 1018{
0316d30e 1019 int i;
1738d60b 1020
917f39bb
EG
1021 iwl_mvm_rx_check_trigger(mvm, pkt);
1022
8ca151b5
JB
1023 /*
1024 * Do the notification wait before RX handlers so
1025 * even if the RX handler consumes the RXB we have
1026 * access to it in the notification wait entry.
1027 */
1028 iwl_notification_wait_notify(&mvm->notif_wait, pkt);
1029
1030 for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
1031 const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
36eed56a
EG
1032 struct iwl_async_handler_entry *entry;
1033
1230b16b 1034 if (rx_h->cmd_id != WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd))
36eed56a
EG
1035 continue;
1036
c9cb14a6 1037 if (rx_h->context == RX_HANDLER_SYNC) {
0416841d 1038 rx_h->fn(mvm, rxb);
f7e6469f 1039 return;
0416841d 1040 }
36eed56a
EG
1041
1042 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1043 /* we can't do much... */
1044 if (!entry)
f7e6469f 1045 return;
36eed56a
EG
1046
1047 entry->rxb._page = rxb_steal_page(rxb);
1048 entry->rxb._offset = rxb->_offset;
1049 entry->rxb._rx_page_order = rxb->_rx_page_order;
1050 entry->fn = rx_h->fn;
c9cb14a6 1051 entry->context = rx_h->context;
36eed56a
EG
1052 spin_lock(&mvm->async_handlers_lock);
1053 list_add_tail(&entry->list, &mvm->async_handlers_list);
1054 spin_unlock(&mvm->async_handlers_lock);
1055 schedule_work(&mvm->async_handlers_wk);
07fb3299 1056 break;
8ca151b5 1057 }
8ca151b5
JB
1058}
1059
0316d30e
JB
1060static void iwl_mvm_rx(struct iwl_op_mode *op_mode,
1061 struct napi_struct *napi,
1062 struct iwl_rx_cmd_buffer *rxb)
1063{
1064 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1065 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
61b0f5d7 1066 u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
0316d30e 1067
61b0f5d7 1068 if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
0316d30e 1069 iwl_mvm_rx_rx_mpdu(mvm, napi, rxb);
61b0f5d7 1070 else if (cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_PHY_CMD))
0316d30e
JB
1071 iwl_mvm_rx_rx_phy_cmd(mvm, rxb);
1072 else
1073 iwl_mvm_rx_common(mvm, rxb, pkt);
1074}
1075
1076static void iwl_mvm_rx_mq(struct iwl_op_mode *op_mode,
1077 struct napi_struct *napi,
1078 struct iwl_rx_cmd_buffer *rxb)
1079{
1080 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1081 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
61b0f5d7 1082 u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
0316d30e 1083
61b0f5d7 1084 if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
780e87c2 1085 iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, 0);
61b0f5d7
JB
1086 else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1087 RX_QUEUES_NOTIFICATION)))
94bb4481 1088 iwl_mvm_rx_queue_notif(mvm, rxb, 0);
61b0f5d7 1089 else if (cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE))
58035432 1090 iwl_mvm_rx_frame_release(mvm, napi, rxb, 0);
bf9dfeda
ST
1091 else if (cmd == WIDE_ID(DATA_PATH_GROUP, RX_NO_DATA_NOTIF))
1092 iwl_mvm_rx_monitor_ndp(mvm, napi, rxb, 0);
0316d30e
JB
1093 else
1094 iwl_mvm_rx_common(mvm, rxb, pkt);
1095}
1096
156f92f2
EG
1097static void iwl_mvm_async_cb(struct iwl_op_mode *op_mode,
1098 const struct iwl_device_cmd *cmd)
1099{
1100 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1101
1102 /*
1103 * For now, we only set the CMD_WANT_ASYNC_CALLBACK for ADD_STA
1104 * commands that need to block the Tx queues.
1105 */
1106 iwl_trans_block_txq_ptrs(mvm->trans, false);
1107}
1108
cfbc6c4c
SS
1109static void iwl_mvm_queue_state_change(struct iwl_op_mode *op_mode,
1110 int hw_queue, bool start)
8ca151b5
JB
1111{
1112 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
cfbc6c4c
SS
1113 struct ieee80211_sta *sta;
1114 struct ieee80211_txq *txq;
1115 struct iwl_mvm_txq *mvmtxq;
1116 int i;
1117 unsigned long tid_bitmap;
1118 struct iwl_mvm_sta *mvmsta;
1119 u8 sta_id;
b4f7a9d1 1120
cfbc6c4c
SS
1121 sta_id = iwl_mvm_has_new_tx_api(mvm) ?
1122 mvm->tvqm_info[hw_queue].sta_id :
1123 mvm->queue_info[hw_queue].ra_sta_id;
b4f7a9d1 1124
cfbc6c4c 1125 if (WARN_ON_ONCE(sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
8ca151b5 1126 return;
8ca151b5 1127
cfbc6c4c
SS
1128 rcu_read_lock();
1129
1130 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1131 if (IS_ERR_OR_NULL(sta))
1132 goto out;
1133 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1134
1135 if (iwl_mvm_has_new_tx_api(mvm)) {
1136 int tid = mvm->tvqm_info[hw_queue].txq_tid;
1137
1138 tid_bitmap = BIT(tid);
1139 } else {
1140 tid_bitmap = mvm->queue_info[hw_queue].tid_bitmap;
1141 }
1142
1143 for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1144 int tid = i;
1145
1146 if (tid == IWL_MAX_TID_COUNT)
1147 tid = IEEE80211_NUM_TIDS;
4ecafae9 1148
cfbc6c4c
SS
1149 txq = sta->txq[tid];
1150 mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1151 mvmtxq->stopped = !start;
1152
1153 if (start && mvmsta->sta_state != IEEE80211_STA_NOTEXIST)
1154 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
4ecafae9 1155 }
cfbc6c4c
SS
1156
1157out:
1158 rcu_read_unlock();
8ca151b5
JB
1159}
1160
cfbc6c4c 1161static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
b4f7a9d1 1162{
cfbc6c4c
SS
1163 iwl_mvm_queue_state_change(op_mode, hw_queue, false);
1164}
b4f7a9d1 1165
cfbc6c4c
SS
1166static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int hw_queue)
1167{
1168 iwl_mvm_queue_state_change(op_mode, hw_queue, true);
b4f7a9d1
LK
1169}
1170
6ad04359
JB
1171static void iwl_mvm_set_rfkill_state(struct iwl_mvm *mvm)
1172{
1173 bool state = iwl_mvm_is_radio_killed(mvm);
1174
1175 if (state)
1176 wake_up(&mvm->rx_sync_waitq);
1177
1178 wiphy_rfkill_set_hw_state(mvm->hw->wiphy, state);
1179}
1180
9ee718aa
EL
1181void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
1182{
1183 if (state)
1184 set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1185 else
1186 clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
1187
6ad04359 1188 iwl_mvm_set_rfkill_state(mvm);
9ee718aa
EL
1189}
1190
14cfca71 1191static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
8ca151b5
JB
1192{
1193 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
6aa7de05 1194 bool calibrating = READ_ONCE(mvm->calibrating);
8ca151b5
JB
1195
1196 if (state)
1197 set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1198 else
1199 clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
1200
6ad04359 1201 iwl_mvm_set_rfkill_state(mvm);
14cfca71 1202
31b8b343
EG
1203 /* iwl_run_init_mvm_ucode is waiting for results, abort it */
1204 if (calibrating)
1205 iwl_abort_notification_waits(&mvm->notif_wait);
1206
1207 /*
1208 * Stop the device if we run OPERATIONAL firmware or if we are in the
1209 * middle of the calibrations.
1210 */
702e975d 1211 return state && (mvm->fwrt.cur_fw_img != IWL_UCODE_INIT || calibrating);
8ca151b5
JB
1212}
1213
1214static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1215{
1216 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1217 struct ieee80211_tx_info *info;
1218
1219 info = IEEE80211_SKB_CB(skb);
1220 iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1221 ieee80211_free_txskb(mvm->hw, skb);
1222}
1223
ac1ed416
JB
1224struct iwl_mvm_reprobe {
1225 struct device *dev;
1226 struct work_struct work;
1227};
1228
1229static void iwl_mvm_reprobe_wk(struct work_struct *wk)
1230{
1231 struct iwl_mvm_reprobe *reprobe;
1232
1233 reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
1234 if (device_reprobe(reprobe->dev))
1235 dev_err(reprobe->dev, "reprobe failed!\n");
1236 kfree(reprobe);
1237 module_put(THIS_MODULE);
1238}
1239
b08c1d97 1240void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
8ca151b5 1241{
8ca151b5
JB
1242 iwl_abort_notification_waits(&mvm->notif_wait);
1243
992f81fc
DS
1244 /*
1245 * This is a bit racy, but worst case we tell mac80211 about
1246 * a stopped/aborted scan when that was already done which
1247 * is not a problem. It is necessary to abort any os scan
1248 * here because mac80211 requires having the scan cleared
1249 * before restarting.
1250 * We'll reset the scan_status to NONE in restart cleanup in
1251 * the next start() call from mac80211. If restart isn't called
1252 * (no fw restart) scan status will stay busy.
1253 */
4ffb3650 1254 iwl_mvm_report_scan_aborted(mvm);
992f81fc 1255
8ca151b5
JB
1256 /*
1257 * If we're restarting already, don't cycle restarts.
1258 * If INIT fw asserted, it will likely fail again.
1259 * If WoWLAN fw asserted, don't restart either, mac80211
1260 * can't recover this since we're already half suspended.
1261 */
3b37f4c9 1262 if (!mvm->fw_restart && fw_error) {
7174beb6 1263 iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
124e0eba 1264 false, 0);
bf8b286f 1265 } else if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
ac1ed416
JB
1266 struct iwl_mvm_reprobe *reprobe;
1267
1268 IWL_ERR(mvm,
1269 "Firmware error during reconfiguration - reprobe!\n");
1270
1271 /*
1272 * get a module reference to avoid doing this while unloading
1273 * anyway and to avoid scheduling a work with code that's
1274 * being removed.
1275 */
1276 if (!try_module_get(THIS_MODULE)) {
1277 IWL_ERR(mvm, "Module is being unloaded - abort\n");
1278 return;
1279 }
1280
1281 reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
1282 if (!reprobe) {
1283 module_put(THIS_MODULE);
1284 return;
1285 }
1286 reprobe->dev = mvm->trans->dev;
1287 INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
1288 schedule_work(&reprobe->work);
702e975d 1289 } else if (mvm->fwrt.cur_fw_img == IWL_UCODE_REGULAR &&
f60c9e59
EG
1290 mvm->hw_registered &&
1291 !test_bit(STATUS_TRANS_DEAD, &mvm->trans->status)) {
7498cf4c
EP
1292 /* don't let the transport/FW power down */
1293 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1294
3b37f4c9
JB
1295 if (fw_error && mvm->fw_restart > 0)
1296 mvm->fw_restart--;
bf8b286f 1297 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
8ca151b5
JB
1298 ieee80211_restart_hw(mvm->hw);
1299 }
1300}
1301
715c998f
EG
1302static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode)
1303{
1304 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1305
f60c9e59
EG
1306 if (!test_bit(STATUS_TRANS_DEAD, &mvm->trans->status))
1307 iwl_mvm_dump_nic_error_log(mvm);
1bd3cbc1 1308
b08c1d97 1309 iwl_mvm_nic_restart(mvm, true);
715c998f
EG
1310}
1311
8ca151b5
JB
1312static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
1313{
715c998f
EG
1314 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1315
8ca151b5 1316 WARN_ON(1);
b08c1d97 1317 iwl_mvm_nic_restart(mvm, true);
8ca151b5
JB
1318}
1319
a75b9b33 1320#ifdef CONFIG_PM
37577fe2
EP
1321struct iwl_d0i3_iter_data {
1322 struct iwl_mvm *mvm;
a3f7ba5c 1323 struct ieee80211_vif *connected_vif;
37577fe2
EP
1324 u8 ap_sta_id;
1325 u8 vif_count;
b2492501
AN
1326 u8 offloading_tid;
1327 bool disable_offloading;
37577fe2
EP
1328};
1329
b2492501
AN
1330static bool iwl_mvm_disallow_offloading(struct iwl_mvm *mvm,
1331 struct ieee80211_vif *vif,
1332 struct iwl_d0i3_iter_data *iter_data)
1333{
1334 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
b2492501
AN
1335 struct iwl_mvm_sta *mvmsta;
1336 u32 available_tids = 0;
1337 u8 tid;
1338
1339 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION ||
0ae98812 1340 mvmvif->ap_sta_id == IWL_MVM_INVALID_STA))
b2492501
AN
1341 return false;
1342
13303c0f
SS
1343 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1344 if (!mvmsta)
b2492501
AN
1345 return false;
1346
b2492501
AN
1347 spin_lock_bh(&mvmsta->lock);
1348 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1349 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1350
1351 /*
1352 * in case of pending tx packets, don't use this tid
1353 * for offloading in order to prevent reuse of the same
1354 * qos seq counters.
1355 */
dd32162d 1356 if (iwl_mvm_tid_queued(mvm, tid_data))
b2492501
AN
1357 continue;
1358
1359 if (tid_data->state != IWL_AGG_OFF)
1360 continue;
1361
1362 available_tids |= BIT(tid);
1363 }
1364 spin_unlock_bh(&mvmsta->lock);
1365
1366 /*
1367 * disallow protocol offloading if we have no available tid
1368 * (with no pending frames and no active aggregation,
1369 * as we don't handle "holes" properly - the scheduler needs the
1370 * frame's seq number and TFD index to match)
1371 */
1372 if (!available_tids)
1373 return true;
1374
1375 /* for simplicity, just use the first available tid */
1376 iter_data->offloading_tid = ffs(available_tids) - 1;
1377 return false;
1378}
1379
d6230972
EP
1380static void iwl_mvm_enter_d0i3_iterator(void *_data, u8 *mac,
1381 struct ieee80211_vif *vif)
1382{
37577fe2
EP
1383 struct iwl_d0i3_iter_data *data = _data;
1384 struct iwl_mvm *mvm = data->mvm;
1385 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
d6230972
EP
1386 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
1387
1388 IWL_DEBUG_RPM(mvm, "entering D0i3 - vif %pM\n", vif->addr);
1389 if (vif->type != NL80211_IFTYPE_STATION ||
1390 !vif->bss_conf.assoc)
1391 return;
1392
b2492501
AN
1393 /*
1394 * in case of pending tx packets or active aggregations,
1395 * avoid offloading features in order to prevent reuse of
1396 * the same qos seq counters.
1397 */
1398 if (iwl_mvm_disallow_offloading(mvm, vif, data))
1399 data->disable_offloading = true;
1400
d6230972 1401 iwl_mvm_update_d0i3_power_mode(mvm, vif, true, flags);
c97dab40
SS
1402 iwl_mvm_send_proto_offload(mvm, vif, data->disable_offloading,
1403 false, flags);
d6230972
EP
1404
1405 /*
1406 * on init/association, mvm already configures POWER_TABLE_CMD
1407 * and REPLY_MCAST_FILTER_CMD, so currently don't
1408 * reconfigure them (we might want to use different
1409 * params later on, though).
1410 */
37577fe2
EP
1411 data->ap_sta_id = mvmvif->ap_sta_id;
1412 data->vif_count++;
a3f7ba5c
EP
1413
1414 /*
1415 * no new commands can be sent at this stage, so it's safe
1416 * to save the vif pointer during d0i3 entrance.
1417 */
1418 data->connected_vif = vif;
d6230972
EP
1419}
1420
1a95c8df 1421static void iwl_mvm_set_wowlan_data(struct iwl_mvm *mvm,
c8b06a99 1422 struct iwl_wowlan_config_cmd *cmd,
1a95c8df
EP
1423 struct iwl_d0i3_iter_data *iter_data)
1424{
1425 struct ieee80211_sta *ap_sta;
1426 struct iwl_mvm_sta *mvm_ap_sta;
1427
0ae98812 1428 if (iter_data->ap_sta_id == IWL_MVM_INVALID_STA)
1a95c8df
EP
1429 return;
1430
1431 rcu_read_lock();
1432
1433 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[iter_data->ap_sta_id]);
1434 if (IS_ERR_OR_NULL(ap_sta))
1435 goto out;
1436
1437 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
c8b06a99 1438 cmd->is_11n_connection = ap_sta->ht_cap.ht_supported;
b2492501 1439 cmd->offloading_tid = iter_data->offloading_tid;
70b4c536 1440 cmd->flags = ENABLE_L3_FILTERING | ENABLE_NBNS_FILTERING |
0db056d3 1441 ENABLE_DHCP_FILTERING | ENABLE_STORE_BEACON;
1a95c8df
EP
1442 /*
1443 * The d0i3 uCode takes care of the nonqos counters,
1444 * so configure only the qos seq ones.
1445 */
c8b06a99 1446 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, cmd);
1a95c8df
EP
1447out:
1448 rcu_read_unlock();
1449}
6735943f
EP
1450
1451int iwl_mvm_enter_d0i3(struct iwl_op_mode *op_mode)
b3370d47
EP
1452{
1453 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
98ee7783 1454 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
b77f06d9 1455 int ret;
37577fe2
EP
1456 struct iwl_d0i3_iter_data d0i3_iter_data = {
1457 .mvm = mvm,
1458 };
c8b06a99
EG
1459 struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1460 .wakeup_filter = cpu_to_le32(IWL_WOWLAN_WAKEUP_RX_FRAME |
1461 IWL_WOWLAN_WAKEUP_BEACON_MISS |
0db056d3 1462 IWL_WOWLAN_WAKEUP_LINK_CHANGE),
b77f06d9 1463 };
98ee7783
AN
1464 struct iwl_d3_manager_config d3_cfg_cmd = {
1465 .min_sleep_time = cpu_to_le32(1000),
d9f1fc20 1466 .wakeup_flags = cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR),
98ee7783 1467 };
b3370d47
EP
1468
1469 IWL_DEBUG_RPM(mvm, "MVM entering D0i3\n");
98ee7783 1470
702e975d 1471 if (WARN_ON_ONCE(mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR))
08f0d23d
EP
1472 return -EINVAL;
1473
b2492501 1474 set_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
b2492501 1475
f4cf8680
EP
1476 /*
1477 * iwl_mvm_ref_sync takes a reference before checking the flag.
1478 * so by checking there is no held reference we prevent a state
1479 * in which iwl_mvm_ref_sync continues successfully while we
1480 * configure the firmware to enter d0i3
1481 */
1482 if (iwl_mvm_ref_taken(mvm)) {
1483 IWL_DEBUG_RPM(mvm->trans, "abort d0i3 due to taken ref\n");
1484 clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
caf1578a 1485 wake_up(&mvm->d0i3_exit_waitq);
f4cf8680
EP
1486 return 1;
1487 }
1488
d6230972
EP
1489 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1490 IEEE80211_IFACE_ITER_NORMAL,
1491 iwl_mvm_enter_d0i3_iterator,
37577fe2
EP
1492 &d0i3_iter_data);
1493 if (d0i3_iter_data.vif_count == 1) {
1494 mvm->d0i3_ap_sta_id = d0i3_iter_data.ap_sta_id;
b2492501 1495 mvm->d0i3_offloading = !d0i3_iter_data.disable_offloading;
37577fe2
EP
1496 } else {
1497 WARN_ON_ONCE(d0i3_iter_data.vif_count > 1);
0ae98812 1498 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
b2492501 1499 mvm->d0i3_offloading = false;
37577fe2 1500 }
d6230972 1501
7d9d0d56 1502 iwl_mvm_pause_tcm(mvm, true);
ecc7c518
EG
1503 /* make sure we have no running tx while configuring the seqno */
1504 synchronize_net();
1505
eb3908d3 1506 /* Flush the hw queues, in case something got queued during entry */
d167e81a
MG
1507 /* TODO new tx api */
1508 if (iwl_mvm_has_new_tx_api(mvm)) {
1509 WARN_ONCE(1, "d0i3: Need to implement flush TX queue\n");
1510 } else {
1511 ret = iwl_mvm_flush_tx_path(mvm, iwl_mvm_flushable_queues(mvm),
1512 flags);
1513 if (ret)
1514 return ret;
1515 }
eb3908d3 1516
183edd84 1517 /* configure wowlan configuration only if needed */
0ae98812 1518 if (mvm->d0i3_ap_sta_id != IWL_MVM_INVALID_STA) {
0db056d3
SS
1519 /* wake on beacons only if beacon storing isn't supported */
1520 if (!fw_has_capa(&mvm->fw->ucode_capa,
1521 IWL_UCODE_TLV_CAPA_BEACON_STORING))
1522 wowlan_config_cmd.wakeup_filter |=
1523 cpu_to_le32(IWL_WOWLAN_WAKEUP_BCN_FILTERING);
1524
a3f7ba5c
EP
1525 iwl_mvm_wowlan_config_key_params(mvm,
1526 d0i3_iter_data.connected_vif,
1527 true, flags);
1528
183edd84
EP
1529 iwl_mvm_set_wowlan_data(mvm, &wowlan_config_cmd,
1530 &d0i3_iter_data);
1531
1532 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, flags,
1533 sizeof(wowlan_config_cmd),
1534 &wowlan_config_cmd);
1535 if (ret)
1536 return ret;
1537 }
b77f06d9 1538
98ee7783
AN
1539 return iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD,
1540 flags | CMD_MAKE_TRANS_IDLE,
1541 sizeof(d3_cfg_cmd), &d3_cfg_cmd);
b3370d47
EP
1542}
1543
d6230972
EP
1544static void iwl_mvm_exit_d0i3_iterator(void *_data, u8 *mac,
1545 struct ieee80211_vif *vif)
1546{
1547 struct iwl_mvm *mvm = _data;
1548 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO;
1549
1550 IWL_DEBUG_RPM(mvm, "exiting D0i3 - vif %pM\n", vif->addr);
1551 if (vif->type != NL80211_IFTYPE_STATION ||
1552 !vif->bss_conf.assoc)
1553 return;
1554
1555 iwl_mvm_update_d0i3_power_mode(mvm, vif, false, flags);
1556}
1557
a3f7ba5c 1558struct iwl_mvm_d0i3_exit_work_iter_data {
b3df2247 1559 struct iwl_mvm *mvm;
a3f7ba5c 1560 struct iwl_wowlan_status *status;
b3df2247
DS
1561 u32 wakeup_reasons;
1562};
1563
a3f7ba5c
EP
1564static void iwl_mvm_d0i3_exit_work_iter(void *_data, u8 *mac,
1565 struct ieee80211_vif *vif)
37577fe2 1566{
a3f7ba5c 1567 struct iwl_mvm_d0i3_exit_work_iter_data *data = _data;
37577fe2 1568 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
a3f7ba5c 1569 u32 reasons = data->wakeup_reasons;
37577fe2 1570
a3f7ba5c
EP
1571 /* consider only the relevant station interface */
1572 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1573 data->mvm->d0i3_ap_sta_id != mvmvif->ap_sta_id)
1574 return;
1575
1576 if (reasons & IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH)
1577 iwl_mvm_connection_loss(data->mvm, vif, "D0i3");
1578 else if (reasons & IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON)
1579 ieee80211_beacon_loss(vif);
1580 else
1581 iwl_mvm_d0i3_update_keys(data->mvm, vif, data->status);
37577fe2
EP
1582}
1583
b2492501
AN
1584void iwl_mvm_d0i3_enable_tx(struct iwl_mvm *mvm, __le16 *qos_seq)
1585{
1586 struct ieee80211_sta *sta = NULL;
1587 struct iwl_mvm_sta *mvm_ap_sta;
1588 int i;
1589 bool wake_queues = false;
1590
1591 lockdep_assert_held(&mvm->mutex);
1592
1593 spin_lock_bh(&mvm->d0i3_tx_lock);
1594
0ae98812 1595 if (mvm->d0i3_ap_sta_id == IWL_MVM_INVALID_STA)
b2492501
AN
1596 goto out;
1597
1598 IWL_DEBUG_RPM(mvm, "re-enqueue packets\n");
1599
1600 /* get the sta in order to update seq numbers and re-enqueue skbs */
1601 sta = rcu_dereference_protected(
1602 mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id],
1603 lockdep_is_held(&mvm->mutex));
1604
1605 if (IS_ERR_OR_NULL(sta)) {
1606 sta = NULL;
1607 goto out;
1608 }
1609
1610 if (mvm->d0i3_offloading && qos_seq) {
1611 /* update qos seq numbers if offloading was enabled */
9d8ce6af 1612 mvm_ap_sta = iwl_mvm_sta_from_mac80211(sta);
b2492501
AN
1613 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1614 u16 seq = le16_to_cpu(qos_seq[i]);
1615 /* firmware stores last-used one, we store next one */
1616 seq += 0x10;
1617 mvm_ap_sta->tid_data[i].seq_number = seq;
1618 }
1619 }
1620out:
1621 /* re-enqueue (or drop) all packets */
1622 while (!skb_queue_empty(&mvm->d0i3_tx)) {
1623 struct sk_buff *skb = __skb_dequeue(&mvm->d0i3_tx);
1624
1625 if (!sta || iwl_mvm_tx_skb(mvm, skb, sta))
1626 ieee80211_free_txskb(mvm->hw, skb);
1627
1628 /* if the skb_queue is not empty, we need to wake queues */
1629 wake_queues = true;
1630 }
1631 clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1632 wake_up(&mvm->d0i3_exit_waitq);
0ae98812 1633 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
b2492501
AN
1634 if (wake_queues)
1635 ieee80211_wake_queues(mvm->hw);
1636
1637 spin_unlock_bh(&mvm->d0i3_tx_lock);
1638}
1639
37577fe2
EP
1640static void iwl_mvm_d0i3_exit_work(struct work_struct *wk)
1641{
1642 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, d0i3_exit_work);
a3f7ba5c
EP
1643 struct iwl_mvm_d0i3_exit_work_iter_data iter_data = {
1644 .mvm = mvm,
1645 };
1646
3afec639 1647 struct iwl_wowlan_status *status;
a3f7ba5c 1648 u32 wakeup_reasons = 0;
b2492501 1649 __le16 *qos_seq = NULL;
37577fe2
EP
1650
1651 mutex_lock(&mvm->mutex);
2afa6a73
LC
1652
1653 status = iwl_mvm_send_wowlan_get_status(mvm);
1654 if (IS_ERR_OR_NULL(status)) {
1655 /* set to NULL so we don't need to check before kfree'ing */
1656 status = NULL;
37577fe2 1657 goto out;
2afa6a73 1658 }
37577fe2 1659
37577fe2 1660 wakeup_reasons = le32_to_cpu(status->wakeup_reasons);
b2492501 1661 qos_seq = status->qos_seq_ctr;
37577fe2
EP
1662
1663 IWL_DEBUG_RPM(mvm, "wakeup reasons: 0x%x\n", wakeup_reasons);
1664
a3f7ba5c
EP
1665 iter_data.wakeup_reasons = wakeup_reasons;
1666 iter_data.status = status;
1667 ieee80211_iterate_active_interfaces(mvm->hw,
1668 IEEE80211_IFACE_ITER_NORMAL,
1669 iwl_mvm_d0i3_exit_work_iter,
1670 &iter_data);
37577fe2 1671out:
b2492501 1672 iwl_mvm_d0i3_enable_tx(mvm, qos_seq);
47c8b154 1673
7c014e35
EP
1674 IWL_DEBUG_INFO(mvm, "d0i3 exit completed (wakeup reasons: 0x%x)\n",
1675 wakeup_reasons);
1676
e5629be7 1677 /* qos_seq might point inside resp_pkt, so free it only now */
2afa6a73 1678 kfree(status);
e5629be7 1679
47c8b154
JD
1680 /* the FW might have updated the regdomain */
1681 iwl_mvm_update_changed_regdom(mvm);
1682
7d9d0d56 1683 iwl_mvm_resume_tcm(mvm);
d15a747f 1684 iwl_mvm_unref(mvm, IWL_MVM_REF_EXIT_WORK);
37577fe2
EP
1685 mutex_unlock(&mvm->mutex);
1686}
1687
d15a747f 1688int _iwl_mvm_exit_d0i3(struct iwl_mvm *mvm)
b3370d47 1689{
98ee7783
AN
1690 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
1691 CMD_WAKE_UP_TRANS;
d6230972 1692 int ret;
b3370d47
EP
1693
1694 IWL_DEBUG_RPM(mvm, "MVM exiting D0i3\n");
98ee7783 1695
702e975d 1696 if (WARN_ON_ONCE(mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR))
08f0d23d
EP
1697 return -EINVAL;
1698
d15a747f
EP
1699 mutex_lock(&mvm->d0i3_suspend_mutex);
1700 if (test_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags)) {
1701 IWL_DEBUG_RPM(mvm, "Deferring d0i3 exit until resume\n");
1702 __set_bit(D0I3_PENDING_WAKEUP, &mvm->d0i3_suspend_flags);
1703 mutex_unlock(&mvm->d0i3_suspend_mutex);
1704 return 0;
1705 }
1706 mutex_unlock(&mvm->d0i3_suspend_mutex);
1707
d6230972
EP
1708 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
1709 if (ret)
37577fe2 1710 goto out;
d6230972
EP
1711
1712 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1713 IEEE80211_IFACE_ITER_NORMAL,
1714 iwl_mvm_exit_d0i3_iterator,
1715 mvm);
37577fe2
EP
1716out:
1717 schedule_work(&mvm->d0i3_exit_work);
1718 return ret;
b3370d47
EP
1719}
1720
6735943f 1721int iwl_mvm_exit_d0i3(struct iwl_op_mode *op_mode)
d15a747f
EP
1722{
1723 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
1724
1725 iwl_mvm_ref(mvm, IWL_MVM_REF_EXIT_WORK);
1726 return _iwl_mvm_exit_d0i3(mvm);
1727}
1728
a75b9b33
LC
1729#define IWL_MVM_D0I3_OPS \
1730 .enter_d0i3 = iwl_mvm_enter_d0i3, \
1731 .exit_d0i3 = iwl_mvm_exit_d0i3,
1732#else /* CONFIG_PM */
1733#define IWL_MVM_D0I3_OPS
1734#endif /* CONFIG_PM */
1735
0316d30e
JB
1736#define IWL_MVM_COMMON_OPS \
1737 /* these could be differentiated */ \
156f92f2 1738 .async_cb = iwl_mvm_async_cb, \
0316d30e
JB
1739 .queue_full = iwl_mvm_stop_sw_queue, \
1740 .queue_not_full = iwl_mvm_wake_sw_queue, \
1741 .hw_rf_kill = iwl_mvm_set_hw_rfkill_state, \
1742 .free_skb = iwl_mvm_free_skb, \
1743 .nic_error = iwl_mvm_nic_error, \
1744 .cmd_queue_full = iwl_mvm_cmd_queue_full, \
1745 .nic_config = iwl_mvm_nic_config, \
a75b9b33 1746 IWL_MVM_D0I3_OPS \
0316d30e
JB
1747 /* as we only register one, these MUST be common! */ \
1748 .start = iwl_op_mode_mvm_start, \
1749 .stop = iwl_op_mode_mvm_stop
1750
8ca151b5 1751static const struct iwl_op_mode_ops iwl_mvm_ops = {
0316d30e
JB
1752 IWL_MVM_COMMON_OPS,
1753 .rx = iwl_mvm_rx,
1754};
1755
1756static void iwl_mvm_rx_mq_rss(struct iwl_op_mode *op_mode,
1757 struct napi_struct *napi,
1758 struct iwl_rx_cmd_buffer *rxb,
1759 unsigned int queue)
1760{
1761 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
585a6fcc 1762 struct iwl_rx_packet *pkt = rxb_addr(rxb);
61b0f5d7 1763 u16 cmd = WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd);
0316d30e 1764
61b0f5d7 1765 if (unlikely(cmd == WIDE_ID(LEGACY_GROUP, FRAME_RELEASE)))
a338384b 1766 iwl_mvm_rx_frame_release(mvm, napi, rxb, queue);
61b0f5d7
JB
1767 else if (unlikely(cmd == WIDE_ID(DATA_PATH_GROUP,
1768 RX_QUEUES_NOTIFICATION)))
94bb4481 1769 iwl_mvm_rx_queue_notif(mvm, rxb, queue);
61b0f5d7 1770 else if (likely(cmd == WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD)))
585a6fcc 1771 iwl_mvm_rx_mpdu_mq(mvm, napi, rxb, queue);
0316d30e
JB
1772}
1773
1774static const struct iwl_op_mode_ops iwl_mvm_ops_mq = {
1775 IWL_MVM_COMMON_OPS,
1776 .rx = iwl_mvm_rx_mq,
1777 .rx_rss = iwl_mvm_rx_mq_rss,
8ca151b5 1778};