blob: b002a7afb5f591d8434b0858c37610392a2ec5ea [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Sara Sharon87d0e1a2017-01-11 21:33:38 +020010 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Golan Ben Amia8cbb462017-10-22 15:58:26 +030011 * Copyright(c) 2018 Intel Corporation
Johannes Berg8ca151b2013-01-24 14:25:36 +010012 *
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 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * USA
26 *
27 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020028 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010029 *
30 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020031 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010032 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 *
34 * BSD LICENSE
35 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020036 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020037 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Sara Sharon87d0e1a2017-01-11 21:33:38 +020038 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Golan Ben Amia8cbb462017-10-22 15:58:26 +030039 * Copyright(c) 2018 Intel Corporation
Johannes Berg8ca151b2013-01-24 14:25:36 +010040 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 *
46 * * Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * * Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in
50 * the documentation and/or other materials provided with the
51 * distribution.
52 * * Neither the name Intel Corporation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
57 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
58 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
59 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
60 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
62 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
66 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 *
68 *****************************************************************************/
69#include <net/mac80211.h>
70
71#include "iwl-debug.h"
72#include "iwl-io.h"
Emmanuel Grumbach7b445f32014-03-20 11:08:19 +020073#include "iwl-prph.h"
Luca Coelho6b54ebf2017-05-05 16:11:24 +030074#include "iwl-csr.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010075#include "mvm.h"
Johannes Bergd172a5e2017-06-02 15:15:53 +020076#include "fw/api/rs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010077
78/*
79 * Will return 0 even if the cmd failed when RFKILL is asserted unless
80 * CMD_WANT_SKB is set in cmd->flags.
81 */
82int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
83{
84 int ret;
85
Johannes Bergdebff612013-05-14 13:53:45 +020086#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
87 if (WARN_ON(mvm->d3_test_active))
88 return -EIO;
89#endif
90
Johannes Berg8ca151b2013-01-24 14:25:36 +010091 /*
92 * Synchronous commands from this op-mode must hold
93 * the mutex, this ensures we don't try to send two
94 * (or more) synchronous commands at a time.
95 */
Luca Coelho71b12302016-03-11 12:12:16 +020096 if (!(cmd->flags & CMD_ASYNC)) {
Johannes Berg8ca151b2013-01-24 14:25:36 +010097 lockdep_assert_held(&mvm->mutex);
Luca Coelho71b12302016-03-11 12:12:16 +020098 if (!(cmd->flags & CMD_SEND_IN_IDLE))
99 iwl_mvm_ref(mvm, IWL_MVM_REF_SENDING_CMD);
100 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101
102 ret = iwl_trans_send_cmd(mvm->trans, cmd);
103
Luca Coelho71b12302016-03-11 12:12:16 +0200104 if (!(cmd->flags & (CMD_ASYNC | CMD_SEND_IN_IDLE)))
105 iwl_mvm_unref(mvm, IWL_MVM_REF_SENDING_CMD);
106
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107 /*
108 * If the caller wants the SKB, then don't hide any problems, the
109 * caller might access the response buffer which will be NULL if
110 * the command failed.
111 */
112 if (cmd->flags & CMD_WANT_SKB)
113 return ret;
114
115 /* Silently ignore failures if RFKILL is asserted */
116 if (!ret || ret == -ERFKILL)
117 return 0;
118 return ret;
119}
120
Aviya Erenfeldab021652015-06-09 16:45:52 +0300121int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100122 u32 flags, u16 len, const void *data)
123{
124 struct iwl_host_cmd cmd = {
125 .id = id,
126 .len = { len, },
127 .data = { data, },
128 .flags = flags,
129 };
130
131 return iwl_mvm_send_cmd(mvm, &cmd);
132}
133
134/*
Sara Sharon0d365ae2015-03-31 12:24:05 +0300135 * We assume that the caller set the status to the success value
Johannes Berg8ca151b2013-01-24 14:25:36 +0100136 */
137int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
138 u32 *status)
139{
140 struct iwl_rx_packet *pkt;
141 struct iwl_cmd_response *resp;
142 int ret, resp_len;
143
144 lockdep_assert_held(&mvm->mutex);
145
Johannes Bergdebff612013-05-14 13:53:45 +0200146#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
147 if (WARN_ON(mvm->d3_test_active))
148 return -EIO;
149#endif
150
Johannes Berg8ca151b2013-01-24 14:25:36 +0100151 /*
152 * Only synchronous commands can wait for status,
153 * we use WANT_SKB so the caller can't.
154 */
155 if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
156 "cmd flags %x", cmd->flags))
157 return -EINVAL;
158
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300159 cmd->flags |= CMD_WANT_SKB;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100160
161 ret = iwl_trans_send_cmd(mvm->trans, cmd);
162 if (ret == -ERFKILL) {
163 /*
164 * The command failed because of RFKILL, don't update
165 * the status, leave it as success and return 0.
166 */
167 return 0;
168 } else if (ret) {
169 return ret;
170 }
171
172 pkt = cmd->resp_pkt;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100173
Johannes Berg65b30342014-01-08 13:16:33 +0100174 resp_len = iwl_rx_packet_payload_len(pkt);
175 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100176 ret = -EIO;
177 goto out_free_resp;
178 }
179
180 resp = (void *)pkt->data;
181 *status = le32_to_cpu(resp->status);
182 out_free_resp:
183 iwl_free_resp(cmd);
184 return ret;
185}
186
187/*
188 * We assume that the caller set the status to the sucess value
189 */
Aviya Erenfeldab021652015-06-09 16:45:52 +0300190int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100191 const void *data, u32 *status)
192{
193 struct iwl_host_cmd cmd = {
194 .id = id,
195 .len = { len, },
196 .data = { data, },
197 };
198
199 return iwl_mvm_send_cmd_status(mvm, &cmd, status);
200}
201
202#define IWL_DECLARE_RATE_INFO(r) \
203 [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
204
205/*
206 * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
207 */
208static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
209 IWL_DECLARE_RATE_INFO(1),
210 IWL_DECLARE_RATE_INFO(2),
211 IWL_DECLARE_RATE_INFO(5),
212 IWL_DECLARE_RATE_INFO(11),
213 IWL_DECLARE_RATE_INFO(6),
214 IWL_DECLARE_RATE_INFO(9),
215 IWL_DECLARE_RATE_INFO(12),
216 IWL_DECLARE_RATE_INFO(18),
217 IWL_DECLARE_RATE_INFO(24),
218 IWL_DECLARE_RATE_INFO(36),
219 IWL_DECLARE_RATE_INFO(48),
220 IWL_DECLARE_RATE_INFO(54),
221};
222
223int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
Johannes Berg57fbcce2016-04-12 15:56:15 +0200224 enum nl80211_band band)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100225{
226 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
227 int idx;
228 int band_offset = 0;
229
230 /* Legacy rate format, search for match in table */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200231 if (band == NL80211_BAND_5GHZ)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100232 band_offset = IWL_FIRST_OFDM_RATE;
233 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
234 if (fw_rate_idx_to_plcp[idx] == rate)
235 return idx - band_offset;
236
237 return -1;
238}
239
240u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
241{
242 /* Get PLCP rate for tx_cmd->rate_n_flags */
243 return fw_rate_idx_to_plcp[rate_idx];
244}
245
Johannes Berg04168412015-06-23 21:22:09 +0200246void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100247{
248 struct iwl_rx_packet *pkt = rxb_addr(rxb);
249 struct iwl_error_resp *err_resp = (void *)pkt->data;
250
251 IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
252 le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
253 IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
254 le16_to_cpu(err_resp->bad_cmd_seq_num),
255 le32_to_cpu(err_resp->error_service));
256 IWL_ERR(mvm, "FW Error notification: timestamp 0x%16llX\n",
257 le64_to_cpu(err_resp->timestamp));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100258}
259
260/*
261 * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
262 * The parameter should also be a combination of ANT_[ABC].
263 */
264u8 first_antenna(u8 mask)
265{
266 BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
Emmanuel Grumbachd7dad552013-04-14 14:41:03 +0300267 if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
268 return BIT(0);
269 return BIT(ffs(mask) - 1);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100270}
271
272/*
273 * Toggles between TX antennas to send the probe request on.
274 * Receives the bitmask of valid TX antennas and the *index* used
275 * for the last TX, and returns the next valid *index* to use.
276 * In order to set it in the tx_cmd, must do BIT(idx).
277 */
278u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
279{
280 u8 ind = last_idx;
281 int i;
282
Naftali Goldstein2c2b4bb2018-01-15 12:32:30 +0200283 for (i = 0; i < MAX_ANT_NUM; i++) {
284 ind = (ind + 1) % MAX_ANT_NUM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100285 if (valid & BIT(ind))
286 return ind;
287 }
288
289 WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
290 return last_idx;
291}
292
Johannes Berge5209262014-01-20 23:38:59 +0100293static const struct {
294 const char *name;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100295 u8 num;
296} advanced_lookup[] = {
297 { "NMI_INTERRUPT_WDG", 0x34 },
298 { "SYSASSERT", 0x35 },
299 { "UCODE_VERSION_MISMATCH", 0x37 },
300 { "BAD_COMMAND", 0x38 },
301 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
302 { "FATAL_ERROR", 0x3D },
303 { "NMI_TRM_HW_ERR", 0x46 },
304 { "NMI_INTERRUPT_TRM", 0x4C },
305 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
306 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
307 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
308 { "NMI_INTERRUPT_HOST", 0x66 },
309 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
310 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
311 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
312 { "ADVANCED_SYSASSERT", 0 },
313};
314
315static const char *desc_lookup(u32 num)
316{
317 int i;
318
319 for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
320 if (advanced_lookup[i].num == num)
321 return advanced_lookup[i].name;
322
323 /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
324 return advanced_lookup[i].name;
325}
326
327/*
328 * Note: This structure is read from the device with IO accesses,
329 * and the reading already does the endian conversion. As it is
330 * read with u32-sized accesses, any members with a different size
331 * need to be ordered correctly though!
332 */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200333struct iwl_error_event_table_v1 {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100334 u32 valid; /* (nonzero) valid, (0) log is empty */
335 u32 error_id; /* type of error */
336 u32 pc; /* program counter */
337 u32 blink1; /* branch link */
338 u32 blink2; /* branch link */
339 u32 ilink1; /* interrupt link */
340 u32 ilink2; /* interrupt link */
341 u32 data1; /* error-specific data */
342 u32 data2; /* error-specific data */
343 u32 data3; /* error-specific data */
344 u32 bcon_time; /* beacon timer */
345 u32 tsf_low; /* network timestamp function timer */
346 u32 tsf_hi; /* network timestamp function timer */
347 u32 gp1; /* GP1 timer register */
348 u32 gp2; /* GP2 timer register */
349 u32 gp3; /* GP3 timer register */
350 u32 ucode_ver; /* uCode version */
351 u32 hw_ver; /* HW Silicon version */
352 u32 brd_ver; /* HW board version */
353 u32 log_pc; /* log program counter */
354 u32 frame_ptr; /* frame pointer */
355 u32 stack_ptr; /* stack pointer */
356 u32 hcmd; /* last host command header */
357 u32 isr0; /* isr status register LMPM_NIC_ISR0:
358 * rxtx_flag */
359 u32 isr1; /* isr status register LMPM_NIC_ISR1:
360 * host_flag */
361 u32 isr2; /* isr status register LMPM_NIC_ISR2:
362 * enc_flag */
363 u32 isr3; /* isr status register LMPM_NIC_ISR3:
364 * time_flag */
365 u32 isr4; /* isr status register LMPM_NIC_ISR4:
366 * wico interrupt */
367 u32 isr_pref; /* isr status register LMPM_NIC_PREF_STAT */
368 u32 wait_event; /* wait event() caller address */
369 u32 l2p_control; /* L2pControlField */
370 u32 l2p_duration; /* L2pDurationField */
371 u32 l2p_mhvalid; /* L2pMhValidBits */
372 u32 l2p_addr_match; /* L2pAddrMatchStat */
373 u32 lmpm_pmg_sel; /* indicate which clocks are turned on
374 * (LMPM_PMG_SEL) */
375 u32 u_timestamp; /* indicate when the date and time of the
376 * compilation */
377 u32 flow_handler; /* FH read/write pointers, RX credit */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200378} __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
379
380struct iwl_error_event_table {
381 u32 valid; /* (nonzero) valid, (0) log is empty */
382 u32 error_id; /* type of error */
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200383 u32 trm_hw_status0; /* TRM HW status */
384 u32 trm_hw_status1; /* TRM HW status */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200385 u32 blink2; /* branch link */
386 u32 ilink1; /* interrupt link */
387 u32 ilink2; /* interrupt link */
388 u32 data1; /* error-specific data */
389 u32 data2; /* error-specific data */
390 u32 data3; /* error-specific data */
391 u32 bcon_time; /* beacon timer */
392 u32 tsf_low; /* network timestamp function timer */
393 u32 tsf_hi; /* network timestamp function timer */
394 u32 gp1; /* GP1 timer register */
395 u32 gp2; /* GP2 timer register */
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200396 u32 fw_rev_type; /* firmware revision type */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200397 u32 major; /* uCode version major */
398 u32 minor; /* uCode version minor */
399 u32 hw_ver; /* HW Silicon version */
400 u32 brd_ver; /* HW board version */
401 u32 log_pc; /* log program counter */
402 u32 frame_ptr; /* frame pointer */
403 u32 stack_ptr; /* stack pointer */
404 u32 hcmd; /* last host command header */
405 u32 isr0; /* isr status register LMPM_NIC_ISR0:
406 * rxtx_flag */
407 u32 isr1; /* isr status register LMPM_NIC_ISR1:
408 * host_flag */
409 u32 isr2; /* isr status register LMPM_NIC_ISR2:
410 * enc_flag */
411 u32 isr3; /* isr status register LMPM_NIC_ISR3:
412 * time_flag */
413 u32 isr4; /* isr status register LMPM_NIC_ISR4:
414 * wico interrupt */
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200415 u32 last_cmd_id; /* last HCMD id handled by the firmware */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200416 u32 wait_event; /* wait event() caller address */
417 u32 l2p_control; /* L2pControlField */
418 u32 l2p_duration; /* L2pDurationField */
419 u32 l2p_mhvalid; /* L2pMhValidBits */
420 u32 l2p_addr_match; /* L2pAddrMatchStat */
421 u32 lmpm_pmg_sel; /* indicate which clocks are turned on
422 * (LMPM_PMG_SEL) */
423 u32 u_timestamp; /* indicate when the date and time of the
424 * compilation */
425 u32 flow_handler; /* FH read/write pointers, RX credit */
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200426} __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100427
Eran Harary01a9ca52014-02-03 09:29:57 +0200428/*
429 * UMAC error struct - relevant starting from family 8000 chip.
430 * Note: This structure is read from the device with IO accesses,
431 * and the reading already does the endian conversion. As it is
432 * read with u32-sized accesses, any members with a different size
433 * need to be ordered correctly though!
434 */
435struct iwl_umac_error_event_table {
436 u32 valid; /* (nonzero) valid, (0) log is empty */
437 u32 error_id; /* type of error */
Eran Harary01a9ca52014-02-03 09:29:57 +0200438 u32 blink1; /* branch link */
439 u32 blink2; /* branch link */
440 u32 ilink1; /* interrupt link */
441 u32 ilink2; /* interrupt link */
442 u32 data1; /* error-specific data */
443 u32 data2; /* error-specific data */
Eran Harary32be1a82014-08-24 08:02:46 +0300444 u32 data3; /* error-specific data */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200445 u32 umac_major;
446 u32 umac_minor;
Eran Harary32be1a82014-08-24 08:02:46 +0300447 u32 frame_pointer; /* core register 27*/
448 u32 stack_pointer; /* core register 28 */
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200449 u32 cmd_header; /* latest host cmd sent to UMAC */
Eran Harary32be1a82014-08-24 08:02:46 +0300450 u32 nic_isr_pref; /* ISR status register */
Eran Harary01a9ca52014-02-03 09:29:57 +0200451} __packed;
452
Johannes Berg8ca151b2013-01-24 14:25:36 +0100453#define ERROR_START_OFFSET (1 * sizeof(u32))
454#define ERROR_ELEM_SIZE (7 * sizeof(u32))
455
Eran Harary01a9ca52014-02-03 09:29:57 +0200456static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
457{
458 struct iwl_trans *trans = mvm->trans;
459 struct iwl_umac_error_event_table table;
Eran Harary01a9ca52014-02-03 09:29:57 +0200460
Luca Coelhofb5b2842017-10-02 15:44:20 +0300461 if (!mvm->support_umac_log)
Eran Harary01a9ca52014-02-03 09:29:57 +0200462 return;
Eran Harary01a9ca52014-02-03 09:29:57 +0200463
Luca Coelhofb5b2842017-10-02 15:44:20 +0300464 iwl_trans_read_mem_bytes(trans, mvm->umac_error_event_table, &table,
465 sizeof(table));
Eran Harary01a9ca52014-02-03 09:29:57 +0200466
467 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
468 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
469 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
470 mvm->status, table.valid);
471 }
472
Johannes Bergedbad052015-01-28 23:43:24 +0100473 IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
Eran Harary01a9ca52014-02-03 09:29:57 +0200474 desc_lookup(table.error_id));
Eran Harary01a9ca52014-02-03 09:29:57 +0200475 IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
476 IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
477 IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
478 IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
479 IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
480 IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
Eran Harary32be1a82014-08-24 08:02:46 +0300481 IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200482 IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
483 IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
Eran Harary32be1a82014-08-24 08:02:46 +0300484 IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
485 IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
486 IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
487 IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
Eran Harary01a9ca52014-02-03 09:29:57 +0200488}
489
Sara Sharon5c228d62016-11-24 13:48:27 +0200490static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u32 base)
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200491{
492 struct iwl_trans *trans = mvm->trans;
493 struct iwl_error_event_table table;
Luca Coelho6b54ebf2017-05-05 16:11:24 +0300494 u32 val;
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200495
Johannes Berg702e9752017-06-02 11:56:58 +0200496 if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200497 if (!base)
498 base = mvm->fw->init_errlog_ptr;
499 } else {
500 if (!base)
501 base = mvm->fw->inst_errlog_ptr;
502 }
503
Sara Sharon3ac37d02016-09-08 10:44:38 +0300504 if (base < 0x400000) {
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200505 IWL_ERR(mvm,
506 "Not valid error log pointer 0x%08X for %s uCode\n",
507 base,
Johannes Berg702e9752017-06-02 11:56:58 +0200508 (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
509 ? "Init" : "RT");
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200510 return;
511 }
512
Luca Coelho6b54ebf2017-05-05 16:11:24 +0300513 /* check if there is a HW error */
514 val = iwl_trans_read_mem32(trans, base);
515 if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
516 int err;
517
518 IWL_ERR(trans, "HW error, resetting before reading\n");
519
520 /* reset the device */
Golan Ben Ami870c2a12017-10-25 17:02:59 +0300521 iwl_trans_sw_reset(trans);
Luca Coelho6b54ebf2017-05-05 16:11:24 +0300522
523 /* set INIT_DONE flag */
524 iwl_set_bit(trans, CSR_GP_CNTRL,
Golan Ben Amia8cbb462017-10-22 15:58:26 +0300525 BIT(trans->cfg->csr->flag_init_done));
Luca Coelho6b54ebf2017-05-05 16:11:24 +0300526
527 /* and wait for clock stabilization */
528 if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
529 udelay(2);
530
531 err = iwl_poll_bit(trans, CSR_GP_CNTRL,
Golan Ben Amia8cbb462017-10-22 15:58:26 +0300532 BIT(trans->cfg->csr->flag_mac_clock_ready),
533 BIT(trans->cfg->csr->flag_mac_clock_ready),
Luca Coelho6b54ebf2017-05-05 16:11:24 +0300534 25000);
535 if (err < 0) {
536 IWL_DEBUG_INFO(trans,
537 "Failed to reset the card for the dump\n");
538 return;
539 }
540 }
541
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200542 iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
543
544 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
545 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
546 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
547 mvm->status, table.valid);
548 }
549
550 /* Do not change this output - scripts rely on it */
551
552 IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
553
Alexei Starovoitov4fe43c22018-03-28 12:05:35 -0700554 trace_iwlwifi_dev_ucode_error(trans->dev, &table, table.hw_ver, table.brd_ver);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200555 IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
556 desc_lookup(table.error_id));
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200557 IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
558 IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200559 IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
560 IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
561 IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
562 IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
563 IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
564 IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
565 IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
566 IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
567 IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
568 IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
569 IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200570 IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200571 IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
572 IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
573 IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
574 IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
575 IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
576 IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
577 IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
578 IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
579 IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
580 IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
Emmanuel Grumbach7d3ca7f2016-03-07 10:16:38 +0200581 IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200582 IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
583 IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
584 IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
585 IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
586 IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
587 IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
588 IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
589 IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
Sara Sharon5c228d62016-11-24 13:48:27 +0200590}
591
592void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
593{
Sara Sharon6362ab72017-11-20 18:02:05 +0200594 if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
595 IWL_ERR(mvm,
596 "DEVICE_ENABLED bit is not set. Aborting dump.\n");
597 return;
598 }
599
Sara Sharon5c228d62016-11-24 13:48:27 +0200600 iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[0]);
601
602 if (mvm->error_event_table[1])
603 iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[1]);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200604
Luca Coelhofb5b2842017-10-02 15:44:20 +0300605 iwl_mvm_dump_umac_error_log(mvm);
Emmanuel Grumbach7e1223b2015-02-03 20:11:48 +0200606}
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200607
Liad Kaufman9794c642015-08-19 17:34:28 +0300608int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id, u8 minq, u8 maxq)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300609{
610 int i;
611
612 lockdep_assert_held(&mvm->queue_info_lock);
613
Sara Sharon396952e2017-02-22 19:40:55 +0200614 /* This should not be hit with new TX path */
615 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
616 return -ENOSPC;
617
Liad Kaufman9794c642015-08-19 17:34:28 +0300618 /* Start by looking for a free queue */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300619 for (i = minq; i <= maxq; i++)
620 if (mvm->queue_info[i].hw_queue_refcount == 0 &&
Liad Kaufmancf961e12015-08-13 19:16:08 +0300621 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300622 return i;
623
Liad Kaufman9794c642015-08-19 17:34:28 +0300624 /*
625 * If no free queue found - settle for an inactive one to reconfigure
626 * Make sure that the inactive queue either already belongs to this STA,
627 * or that if it belongs to another one - it isn't the reserved queue
628 */
629 for (i = minq; i <= maxq; i++)
630 if (mvm->queue_info[i].status == IWL_MVM_QUEUE_INACTIVE &&
631 (sta_id == mvm->queue_info[i].ra_sta_id ||
632 !mvm->queue_info[i].reserved))
633 return i;
634
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300635 return -ENOSPC;
Avri Altman3edf8ff2014-07-30 11:41:01 +0300636}
637
Liad Kaufmancf961e12015-08-13 19:16:08 +0300638int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
639 int tid, int frame_limit, u16 ssn)
640{
641 struct iwl_scd_txq_cfg_cmd cmd = {
642 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200643 .action = SCD_CFG_ENABLE_QUEUE,
Liad Kaufmancf961e12015-08-13 19:16:08 +0300644 .window = frame_limit,
645 .sta_id = sta_id,
646 .ssn = cpu_to_le16(ssn),
647 .tx_fifo = fifo,
648 .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
649 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
650 .tid = tid,
651 };
652 int ret;
653
Sara Sharonbb497012016-09-29 14:52:40 +0300654 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
655 return -EINVAL;
656
Liad Kaufmancf961e12015-08-13 19:16:08 +0300657 spin_lock_bh(&mvm->queue_info_lock);
658 if (WARN(mvm->queue_info[queue].hw_queue_refcount == 0,
659 "Trying to reconfig unallocated queue %d\n", queue)) {
660 spin_unlock_bh(&mvm->queue_info_lock);
661 return -ENXIO;
662 }
663 spin_unlock_bh(&mvm->queue_info_lock);
664
665 IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
666
667 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
668 WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
669 queue, fifo, ret);
670
671 return ret;
672}
673
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200674static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm, int queue,
675 int mac80211_queue, u8 sta_id, u8 tid)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300676{
677 bool enable_queue = true;
678
679 spin_lock_bh(&mvm->queue_info_lock);
680
681 /* Make sure this TID isn't already enabled */
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200682 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300683 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufmandf88c082016-11-24 15:31:00 +0200684 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200685 queue, tid);
686 return false;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300687 }
688
689 /* Update mappings and refcounts */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300690 if (mvm->queue_info[queue].hw_queue_refcount > 0)
691 enable_queue = false;
692
Johannes Berg37e474a2017-06-19 22:31:56 +0200693 if (mac80211_queue != IEEE80211_INVAL_HW_QUEUE) {
694 WARN(mac80211_queue >=
695 BITS_PER_BYTE * sizeof(mvm->hw_queue_to_mac80211[0]),
696 "cannot track mac80211 queue %d (queue %d, sta %d, tid %d)\n",
697 mac80211_queue, queue, sta_id, tid);
698 mvm->hw_queue_to_mac80211[queue] |= BIT(mac80211_queue);
699 }
Sara Sharon34e10862017-02-23 13:15:07 +0200700
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300701 mvm->queue_info[queue].hw_queue_refcount++;
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200702 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
703 mvm->queue_info[queue].ra_sta_id = sta_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300704
Liad Kaufman42db09c2016-05-02 14:01:14 +0300705 if (enable_queue) {
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200706 if (tid != IWL_MAX_TID_COUNT)
Liad Kaufman42db09c2016-05-02 14:01:14 +0300707 mvm->queue_info[queue].mac80211_ac =
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200708 tid_to_mac80211_ac[tid];
Liad Kaufman42db09c2016-05-02 14:01:14 +0300709 else
710 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200711
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200712 mvm->queue_info[queue].txq_tid = tid;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300713 }
714
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300715 IWL_DEBUG_TX_QUEUES(mvm,
716 "Enabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
717 queue, mvm->queue_info[queue].hw_queue_refcount,
Sara Sharon34e10862017-02-23 13:15:07 +0200718 mvm->hw_queue_to_mac80211[queue]);
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300719
720 spin_unlock_bh(&mvm->queue_info_lock);
721
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200722 return enable_queue;
723}
724
Sara Sharon310181e2017-01-17 14:27:48 +0200725int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm, int mac80211_queue,
726 u8 sta_id, u8 tid, unsigned int timeout)
727{
728 struct iwl_tx_queue_cfg_cmd cmd = {
729 .flags = cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
730 .sta_id = sta_id,
731 .tid = tid,
732 };
Sara Sharon251985c2018-01-04 14:06:07 +0200733 int queue, size = IWL_DEFAULT_QUEUE_SIZE;
Sara Sharon310181e2017-01-17 14:27:48 +0200734
Sara Sharon251985c2018-01-04 14:06:07 +0200735 if (cmd.tid == IWL_MAX_TID_COUNT) {
Sara Sharon310181e2017-01-17 14:27:48 +0200736 cmd.tid = IWL_MGMT_TID;
Sara Sharon251985c2018-01-04 14:06:07 +0200737 size = IWL_MGMT_QUEUE_SIZE;
738 }
Sara Sharon310181e2017-01-17 14:27:48 +0200739 queue = iwl_trans_txq_alloc(mvm->trans, (void *)&cmd,
Sara Sharon251985c2018-01-04 14:06:07 +0200740 SCD_QUEUE_CFG, size, timeout);
Sara Sharon310181e2017-01-17 14:27:48 +0200741
742 if (queue < 0) {
743 IWL_DEBUG_TX_QUEUES(mvm,
744 "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
745 sta_id, tid, queue);
746 return queue;
747 }
748
749 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
750 queue, sta_id, tid);
751
Sara Sharon34e10862017-02-23 13:15:07 +0200752 mvm->hw_queue_to_mac80211[queue] |= BIT(mac80211_queue);
753 IWL_DEBUG_TX_QUEUES(mvm,
754 "Enabling TXQ #%d (mac80211 map:0x%x)\n",
755 queue, mvm->hw_queue_to_mac80211[queue]);
Sara Sharon310181e2017-01-17 14:27:48 +0200756
757 return queue;
758}
759
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300760bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
Sara Sharon87d0e1a2017-01-11 21:33:38 +0200761 u16 ssn, const struct iwl_trans_txq_scd_cfg *cfg,
762 unsigned int wdg_timeout)
763{
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300764 struct iwl_scd_txq_cfg_cmd cmd = {
765 .scd_queue = queue,
766 .action = SCD_CFG_ENABLE_QUEUE,
767 .window = cfg->frame_limit,
768 .sta_id = cfg->sta_id,
769 .ssn = cpu_to_le16(ssn),
770 .tx_fifo = cfg->fifo,
771 .aggregate = cfg->aggregate,
772 .tid = cfg->tid,
773 };
774 bool inc_ssn;
775
Sara Sharon310181e2017-01-17 14:27:48 +0200776 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300777 return false;
Sara Sharon310181e2017-01-17 14:27:48 +0200778
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300779 /* Send the enabling command if we need to */
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300780 if (!iwl_mvm_update_txq_mapping(mvm, queue, mac80211_queue,
781 cfg->sta_id, cfg->tid))
782 return false;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300783
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300784 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
785 NULL, wdg_timeout);
786 if (inc_ssn)
787 le16_add_cpu(&cmd.ssn, 1);
788
789 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
790 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
791
792 return inc_ssn;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300793}
794
Sara Sharoncf90da32016-11-07 15:22:28 +0200795int iwl_mvm_disable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
796 u8 tid, u8 flags)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300797{
Sara Sharon310181e2017-01-17 14:27:48 +0200798 struct iwl_scd_txq_cfg_cmd cmd = {
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200799 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200800 .action = SCD_CFG_DISABLE_QUEUE,
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200801 };
Luca Coelho9a233bb2017-12-18 20:13:07 +0200802 bool remove_mac_queue = mac80211_queue != IEEE80211_INVAL_HW_QUEUE;
Sara Sharon34e10862017-02-23 13:15:07 +0200803 int ret;
804
Luca Coelho9a233bb2017-12-18 20:13:07 +0200805 if (WARN_ON(remove_mac_queue && mac80211_queue >= IEEE80211_MAX_QUEUES))
806 return -EINVAL;
807
Sara Sharon34e10862017-02-23 13:15:07 +0200808 if (iwl_mvm_has_new_tx_api(mvm)) {
809 spin_lock_bh(&mvm->queue_info_lock);
Luca Coelho9a233bb2017-12-18 20:13:07 +0200810
811 if (remove_mac_queue)
812 mvm->hw_queue_to_mac80211[queue] &=
813 ~BIT(mac80211_queue);
814
Sara Sharon34e10862017-02-23 13:15:07 +0200815 spin_unlock_bh(&mvm->queue_info_lock);
816
817 iwl_trans_txq_free(mvm->trans, queue);
818
819 return 0;
820 }
Avri Altman3edf8ff2014-07-30 11:41:01 +0300821
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300822 spin_lock_bh(&mvm->queue_info_lock);
823
824 if (WARN_ON(mvm->queue_info[queue].hw_queue_refcount == 0)) {
825 spin_unlock_bh(&mvm->queue_info_lock);
Sara Sharoncf90da32016-11-07 15:22:28 +0200826 return 0;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300827 }
828
829 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
830
831 /*
832 * If there is another TID with the same AC - don't remove the MAC queue
833 * from the mapping
834 */
835 if (tid < IWL_MAX_TID_COUNT) {
836 unsigned long tid_bitmap =
837 mvm->queue_info[queue].tid_bitmap;
838 int ac = tid_to_mac80211_ac[tid];
839 int i;
840
841 for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT) {
842 if (tid_to_mac80211_ac[i] == ac)
843 remove_mac_queue = false;
844 }
845 }
846
847 if (remove_mac_queue)
Sara Sharon34e10862017-02-23 13:15:07 +0200848 mvm->hw_queue_to_mac80211[queue] &=
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300849 ~BIT(mac80211_queue);
850 mvm->queue_info[queue].hw_queue_refcount--;
851
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200852 cmd.action = mvm->queue_info[queue].hw_queue_refcount ?
853 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
854 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300855 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300856
857 IWL_DEBUG_TX_QUEUES(mvm,
858 "Disabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
859 queue,
860 mvm->queue_info[queue].hw_queue_refcount,
Sara Sharon34e10862017-02-23 13:15:07 +0200861 mvm->hw_queue_to_mac80211[queue]);
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300862
863 /* If the queue is still enabled - nothing left to do in this func */
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200864 if (cmd.action == SCD_CFG_ENABLE_QUEUE) {
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300865 spin_unlock_bh(&mvm->queue_info_lock);
Sara Sharoncf90da32016-11-07 15:22:28 +0200866 return 0;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300867 }
868
Liad Kaufmanf02669b2016-02-28 16:15:07 +0200869 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200870 cmd.tid = mvm->queue_info[queue].txq_tid;
Liad Kaufmanf02669b2016-02-28 16:15:07 +0200871
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300872 /* Make sure queue info is correct even though we overwrite it */
873 WARN(mvm->queue_info[queue].hw_queue_refcount ||
874 mvm->queue_info[queue].tid_bitmap ||
Sara Sharon34e10862017-02-23 13:15:07 +0200875 mvm->hw_queue_to_mac80211[queue],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300876 "TXQ #%d info out-of-sync - refcount=%d, mac map=0x%x, tid=0x%x\n",
877 queue, mvm->queue_info[queue].hw_queue_refcount,
Sara Sharon34e10862017-02-23 13:15:07 +0200878 mvm->hw_queue_to_mac80211[queue],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300879 mvm->queue_info[queue].tid_bitmap);
880
881 /* If we are here - the queue is freed and we can zero out these vals */
882 mvm->queue_info[queue].hw_queue_refcount = 0;
883 mvm->queue_info[queue].tid_bitmap = 0;
Sara Sharon34e10862017-02-23 13:15:07 +0200884 mvm->hw_queue_to_mac80211[queue] = 0;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300885
Liad Kaufman9794c642015-08-19 17:34:28 +0300886 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
887 mvm->queue_info[queue].reserved = false;
888
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300889 spin_unlock_bh(&mvm->queue_info_lock);
890
Sara Sharon34e10862017-02-23 13:15:07 +0200891 iwl_trans_txq_disable(mvm->trans, queue, false);
892 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
893 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
Sara Sharon310181e2017-01-17 14:27:48 +0200894
Sara Sharon34e10862017-02-23 13:15:07 +0200895 if (ret)
896 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
897 queue, ret);
898 return ret;
Avri Altman3edf8ff2014-07-30 11:41:01 +0300899}
900
Johannes Berg8ca151b2013-01-24 14:25:36 +0100901/**
902 * iwl_mvm_send_lq_cmd() - Send link quality command
903 * @init: This command is sent as part of station initialization right
904 * after station has been added.
905 *
906 * The link quality command is sent as the last step of station creation.
907 * This is the special case in which init is set and we call a callback in
908 * this case to clear the state indicating that station creation is in
909 * progress.
910 */
Eyal Shapira9e680942013-11-09 00:16:16 +0200911int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq, bool init)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100912{
913 struct iwl_host_cmd cmd = {
914 .id = LQ_CMD,
915 .len = { sizeof(struct iwl_lq_cmd), },
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300916 .flags = init ? 0 : CMD_ASYNC,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100917 .data = { lq, },
918 };
919
Gregory Greenman6fef00d2017-11-30 05:16:16 +0200920 if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
Emmanuel Grumbach4243edb2017-12-13 11:38:48 +0200921 iwl_mvm_has_tlc_offload(mvm)))
Johannes Berg8ca151b2013-01-24 14:25:36 +0100922 return -EINVAL;
923
Johannes Berg8ca151b2013-01-24 14:25:36 +0100924 return iwl_mvm_send_cmd(mvm, &cmd);
925}
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300926
927/**
Sara Sharon0d365ae2015-03-31 12:24:05 +0300928 * iwl_mvm_update_smps - Get a request to change the SMPS mode
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300929 * @req_type: The part of the driver who call for a change.
930 * @smps_requests: The request to change the SMPS mode.
931 *
932 * Get a requst to change the SMPS mode,
933 * and change it according to all other requests in the driver.
934 */
935void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
936 enum iwl_mvm_smps_type_request req_type,
937 enum ieee80211_smps_mode smps_request)
938{
939 struct iwl_mvm_vif *mvmvif;
Emmanuel Grumbachf6415f62013-12-26 15:32:40 +0200940 enum ieee80211_smps_mode smps_mode;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300941 int i;
942
943 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbach710e4d02013-11-28 15:25:21 +0200944
945 /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
Moshe Harela0544272014-12-08 21:13:14 +0200946 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
Emmanuel Grumbach710e4d02013-11-28 15:25:21 +0200947 return;
948
Emmanuel Grumbachf6415f62013-12-26 15:32:40 +0200949 if (vif->type == NL80211_IFTYPE_AP)
950 smps_mode = IEEE80211_SMPS_OFF;
951 else
952 smps_mode = IEEE80211_SMPS_AUTOMATIC;
953
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300954 mvmvif = iwl_mvm_vif_from_mac80211(vif);
955 mvmvif->smps_requests[req_type] = smps_request;
956 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
957 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
958 smps_mode = IEEE80211_SMPS_STATIC;
959 break;
960 }
961 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
962 smps_mode = IEEE80211_SMPS_DYNAMIC;
963 }
964
965 ieee80211_request_smps(vif, smps_mode);
966}
Johannes Berga21d7bc2013-11-12 17:30:52 +0100967
Johannes Berg33cef922015-01-21 21:41:29 +0100968int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
Johannes Berg91a8bcd2015-01-14 18:12:41 +0100969{
Johannes Berg33cef922015-01-21 21:41:29 +0100970 struct iwl_statistics_cmd scmd = {
971 .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
972 };
Johannes Berg91a8bcd2015-01-14 18:12:41 +0100973 struct iwl_host_cmd cmd = {
974 .id = STATISTICS_CMD,
975 .len[0] = sizeof(scmd),
976 .data[0] = &scmd,
977 .flags = CMD_WANT_SKB,
978 };
979 int ret;
980
981 ret = iwl_mvm_send_cmd(mvm, &cmd);
982 if (ret)
983 return ret;
984
985 iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
986 iwl_free_resp(&cmd);
987
Johannes Berg33cef922015-01-21 21:41:29 +0100988 if (clear)
989 iwl_mvm_accu_radio_stats(mvm);
990
Johannes Berg91a8bcd2015-01-14 18:12:41 +0100991 return 0;
992}
993
994void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
995{
996 mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
997 mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
998 mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
999 mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
1000}
1001
Emmanuel Grumbach5c904222014-05-18 09:16:45 +03001002static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
1003 struct ieee80211_vif *vif)
1004{
1005 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1006 bool *result = _data;
1007 int i;
1008
1009 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
1010 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
1011 mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
1012 *result = false;
1013 }
1014}
1015
1016bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
1017{
1018 bool result = true;
1019
1020 lockdep_assert_held(&mvm->mutex);
1021
Moshe Harela0544272014-12-08 21:13:14 +02001022 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
Emmanuel Grumbach5c904222014-05-18 09:16:45 +03001023 return false;
1024
Eyal Shapirac93edc62014-12-31 19:30:15 +02001025 if (mvm->cfg->rx_with_siso_diversity)
Emmanuel Grumbach5c904222014-05-18 09:16:45 +03001026 return false;
1027
1028 ieee80211_iterate_active_interfaces_atomic(
1029 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1030 iwl_mvm_diversity_iter, &result);
1031
1032 return result;
1033}
1034
Johannes Berga21d7bc2013-11-12 17:30:52 +01001035int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Sara Sharon9b137862017-12-27 12:16:33 +02001036 bool low_latency,
1037 enum iwl_mvm_low_latency_cause cause)
Johannes Berga21d7bc2013-11-12 17:30:52 +01001038{
1039 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berge03f9bef2013-11-13 17:16:19 +01001040 int res;
Sara Sharon9b137862017-12-27 12:16:33 +02001041 bool prev;
Johannes Berga21d7bc2013-11-12 17:30:52 +01001042
1043 lockdep_assert_held(&mvm->mutex);
1044
Sara Sharon9b137862017-12-27 12:16:33 +02001045 prev = iwl_mvm_vif_low_latency(mvmvif);
1046 iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
1047
Shaul Triebitz09f1ee82017-12-10 17:11:38 +02001048 low_latency = iwl_mvm_vif_low_latency(mvmvif);
1049
1050 if (low_latency == prev)
Johannes Berg3510aea2014-03-18 10:21:02 +01001051 return 0;
1052
Shaul Triebitz09f1ee82017-12-10 17:11:38 +02001053 if (fw_has_capa(&mvm->fw->ucode_capa,
1054 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA)) {
1055 struct iwl_mac_low_latency_cmd cmd = {
1056 .mac_id = cpu_to_le32(mvmvif->id)
1057 };
1058
1059 if (low_latency) {
1060 /* currently we don't care about the direction */
1061 cmd.low_latency_rx = 1;
1062 cmd.low_latency_tx = 1;
1063 }
1064 res = iwl_mvm_send_cmd_pdu(mvm,
1065 iwl_cmd_id(LOW_LATENCY_CMD,
1066 MAC_CONF_GROUP, 0),
1067 0, sizeof(cmd), &cmd);
1068 if (res)
1069 IWL_ERR(mvm, "Failed to send low latency command\n");
1070 }
1071
Emmanuel Grumbach7754ae72015-02-26 15:14:35 +02001072 res = iwl_mvm_update_quotas(mvm, false, NULL);
Johannes Berge03f9bef2013-11-13 17:16:19 +01001073 if (res)
1074 return res;
Emmanuel Grumbach0ee5bcd2014-01-15 16:57:54 +02001075
1076 iwl_mvm_bt_coex_vif_change(mvm);
1077
Arik Nemtsov999609f2014-05-15 17:31:51 +03001078 return iwl_mvm_power_update_mac(mvm);
Johannes Berga21d7bc2013-11-12 17:30:52 +01001079}
Alexander Bondar50df8a32014-03-12 20:30:51 +02001080
Sara Sharonb66b5812017-01-31 10:39:44 +02001081struct iwl_mvm_low_latency_iter {
1082 bool result;
1083 bool result_per_band[NUM_NL80211_BANDS];
1084};
1085
Alexander Bondar50df8a32014-03-12 20:30:51 +02001086static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
1087{
Sara Sharonb66b5812017-01-31 10:39:44 +02001088 struct iwl_mvm_low_latency_iter *result = _data;
1089 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1090 enum nl80211_band band;
Alexander Bondar50df8a32014-03-12 20:30:51 +02001091
Sara Sharonb66b5812017-01-31 10:39:44 +02001092 if (iwl_mvm_vif_low_latency(mvmvif)) {
1093 result->result = true;
1094
1095 if (!mvmvif->phy_ctxt)
1096 return;
1097
1098 band = mvmvif->phy_ctxt->channel->band;
1099 result->result_per_band[band] = true;
1100 }
Alexander Bondar50df8a32014-03-12 20:30:51 +02001101}
1102
1103bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
1104{
Sara Sharonb66b5812017-01-31 10:39:44 +02001105 struct iwl_mvm_low_latency_iter data = {};
Alexander Bondar50df8a32014-03-12 20:30:51 +02001106
1107 ieee80211_iterate_active_interfaces_atomic(
1108 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Sara Sharonb66b5812017-01-31 10:39:44 +02001109 iwl_mvm_ll_iter, &data);
Alexander Bondar50df8a32014-03-12 20:30:51 +02001110
Sara Sharonb66b5812017-01-31 10:39:44 +02001111 return data.result;
1112}
1113
1114bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
1115{
1116 struct iwl_mvm_low_latency_iter data = {};
1117
1118 ieee80211_iterate_active_interfaces_atomic(
1119 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1120 iwl_mvm_ll_iter, &data);
1121
1122 return data.result_per_band[band];
Alexander Bondar50df8a32014-03-12 20:30:51 +02001123}
David Spinadelbd5e4742014-04-24 13:15:29 +03001124
Luciano Coelho7f549e22014-10-02 15:38:04 +03001125struct iwl_bss_iter_data {
1126 struct ieee80211_vif *vif;
1127 bool error;
1128};
1129
1130static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
1131 struct ieee80211_vif *vif)
1132{
1133 struct iwl_bss_iter_data *data = _data;
1134
1135 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
1136 return;
1137
1138 if (data->vif) {
1139 data->error = true;
1140 return;
1141 }
1142
1143 data->vif = vif;
1144}
1145
1146struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
1147{
1148 struct iwl_bss_iter_data bss_iter_data = {};
1149
1150 ieee80211_iterate_active_interfaces_atomic(
1151 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1152 iwl_mvm_bss_iface_iterator, &bss_iter_data);
1153
1154 if (bss_iter_data.error) {
1155 IWL_ERR(mvm, "More than one managed interface active!\n");
1156 return ERR_PTR(-EINVAL);
1157 }
1158
1159 return bss_iter_data.vif;
1160}
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001161
Haim Dreyfussd4a7e7082017-02-02 14:49:50 +02001162struct iwl_sta_iter_data {
1163 bool assoc;
1164};
1165
1166static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
1167 struct ieee80211_vif *vif)
1168{
1169 struct iwl_sta_iter_data *data = _data;
1170
1171 if (vif->type != NL80211_IFTYPE_STATION)
1172 return;
1173
1174 if (vif->bss_conf.assoc)
1175 data->assoc = true;
1176}
1177
1178bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
1179{
1180 struct iwl_sta_iter_data data = {
1181 .assoc = false,
1182 };
1183
1184 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1185 IEEE80211_IFACE_ITER_NORMAL,
1186 iwl_mvm_sta_iface_iterator,
1187 &data);
1188 return data.assoc;
1189}
1190
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001191unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
1192 struct ieee80211_vif *vif,
1193 bool tdls, bool cmd_q)
1194{
1195 struct iwl_fw_dbg_trigger_tlv *trigger;
1196 struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
1197 unsigned int default_timeout =
1198 cmd_q ? IWL_DEF_WD_TIMEOUT : mvm->cfg->base_params->wd_timeout;
1199
Emmanuel Grumbach0b9832b2017-11-13 09:50:47 +02001200 if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
1201 /*
1202 * We can't know when the station is asleep or awake, so we
1203 * must disable the queue hang detection.
1204 */
1205 if (fw_has_capa(&mvm->fw->ucode_capa,
1206 IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
1207 vif && vif->type == NL80211_IFTYPE_AP)
1208 return IWL_WATCHDOG_DISABLED;
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001209 return iwlmvm_mod_params.tfd_q_hang_detect ?
1210 default_timeout : IWL_WATCHDOG_DISABLED;
Emmanuel Grumbach0b9832b2017-11-13 09:50:47 +02001211 }
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001212
1213 trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
1214 txq_timer = (void *)trigger->data;
1215
1216 if (tdls)
1217 return le32_to_cpu(txq_timer->tdls);
1218
1219 if (cmd_q)
1220 return le32_to_cpu(txq_timer->command_queue);
1221
1222 if (WARN_ON(!vif))
1223 return default_timeout;
1224
1225 switch (ieee80211_vif_type_p2p(vif)) {
1226 case NL80211_IFTYPE_ADHOC:
1227 return le32_to_cpu(txq_timer->ibss);
1228 case NL80211_IFTYPE_STATION:
1229 return le32_to_cpu(txq_timer->bss);
1230 case NL80211_IFTYPE_AP:
1231 return le32_to_cpu(txq_timer->softap);
1232 case NL80211_IFTYPE_P2P_CLIENT:
1233 return le32_to_cpu(txq_timer->p2p_client);
1234 case NL80211_IFTYPE_P2P_GO:
1235 return le32_to_cpu(txq_timer->p2p_go);
1236 case NL80211_IFTYPE_P2P_DEVICE:
1237 return le32_to_cpu(txq_timer->p2p_device);
Emmanuel Grumbachd1b275f2017-11-15 14:12:30 +02001238 case NL80211_IFTYPE_MONITOR:
1239 return default_timeout;
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001240 default:
1241 WARN_ON(1);
1242 return mvm->cfg->base_params->wd_timeout;
1243 }
1244}
Emmanuel Grumbach31755202015-03-30 10:55:57 +03001245
1246void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1247 const char *errmsg)
1248{
1249 struct iwl_fw_dbg_trigger_tlv *trig;
1250 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
1251
1252 if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
1253 goto out;
1254
1255 trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
1256 trig_mlme = (void *)trig->data;
Johannes Berg7174beb2017-06-01 16:03:19 +02001257 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1258 ieee80211_vif_to_wdev(vif), trig))
Emmanuel Grumbach31755202015-03-30 10:55:57 +03001259 goto out;
1260
1261 if (trig_mlme->stop_connection_loss &&
1262 --trig_mlme->stop_connection_loss)
1263 goto out;
1264
Johannes Berg7174beb2017-06-01 16:03:19 +02001265 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
Emmanuel Grumbach31755202015-03-30 10:55:57 +03001266
1267out:
1268 ieee80211_connection_loss(vif);
1269}
Aviya Erenfeld03098262016-02-18 14:09:33 +02001270
Liad Kaufman9794c642015-08-19 17:34:28 +03001271/*
1272 * Remove inactive TIDs of a given queue.
1273 * If all queue TIDs are inactive - mark the queue as inactive
1274 * If only some the queue TIDs are inactive - unmap them from the queue
1275 */
1276static void iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1277 struct iwl_mvm_sta *mvmsta, int queue,
1278 unsigned long tid_bitmap)
1279{
1280 int tid;
1281
1282 lockdep_assert_held(&mvmsta->lock);
1283 lockdep_assert_held(&mvm->queue_info_lock);
1284
Sara Sharonbb497012016-09-29 14:52:40 +03001285 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1286 return;
1287
Liad Kaufman9794c642015-08-19 17:34:28 +03001288 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1289 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1290 /* If some TFDs are still queued - don't mark TID as inactive */
Liad Kaufmandd321622017-04-05 16:25:11 +03001291 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
Liad Kaufman9794c642015-08-19 17:34:28 +03001292 tid_bitmap &= ~BIT(tid);
Emmanuel Grumbach59df97f2017-05-07 13:31:55 +03001293
1294 /* Don't mark as inactive any TID that has an active BA */
1295 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1296 tid_bitmap &= ~BIT(tid);
Liad Kaufman9794c642015-08-19 17:34:28 +03001297 }
1298
1299 /* If all TIDs in the queue are inactive - mark queue as inactive. */
1300 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1301 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
1302
1303 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1)
1304 mvmsta->tid_data[tid].is_tid_active = false;
1305
1306 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d marked as inactive\n",
1307 queue);
1308 return;
1309 }
1310
1311 /*
1312 * If we are here, this is a shared queue and not all TIDs timed-out.
1313 * Remove the ones that did.
1314 */
1315 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1316 int mac_queue = mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]];
1317
Sara Sharon6862fce2017-02-22 19:34:17 +02001318 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
Sara Sharon34e10862017-02-23 13:15:07 +02001319 mvm->hw_queue_to_mac80211[queue] &= ~BIT(mac_queue);
Liad Kaufman9794c642015-08-19 17:34:28 +03001320 mvm->queue_info[queue].hw_queue_refcount--;
1321 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1322 mvmsta->tid_data[tid].is_tid_active = false;
1323
1324 IWL_DEBUG_TX_QUEUES(mvm,
1325 "Removing inactive TID %d from shared Q:%d\n",
1326 tid, queue);
1327 }
1328
1329 IWL_DEBUG_TX_QUEUES(mvm,
1330 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1331 mvm->queue_info[queue].tid_bitmap);
1332
1333 /*
1334 * There may be different TIDs with the same mac queues, so make
1335 * sure all TIDs have existing corresponding mac queues enabled
1336 */
1337 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1338 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Sara Sharon34e10862017-02-23 13:15:07 +02001339 mvm->hw_queue_to_mac80211[queue] |=
Liad Kaufman9794c642015-08-19 17:34:28 +03001340 BIT(mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]]);
1341 }
1342
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001343 /* If the queue is marked as shared - "unshare" it */
1344 if (mvm->queue_info[queue].hw_queue_refcount == 1 &&
1345 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1346 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RECONFIGURING;
1347 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1348 queue);
1349 }
Liad Kaufman9794c642015-08-19 17:34:28 +03001350}
1351
1352void iwl_mvm_inactivity_check(struct iwl_mvm *mvm)
1353{
1354 unsigned long timeout_queues_map = 0;
1355 unsigned long now = jiffies;
1356 int i;
1357
Sara Sharonbb497012016-09-29 14:52:40 +03001358 if (iwl_mvm_has_new_tx_api(mvm))
1359 return;
1360
Liad Kaufman9794c642015-08-19 17:34:28 +03001361 spin_lock_bh(&mvm->queue_info_lock);
1362 for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
1363 if (mvm->queue_info[i].hw_queue_refcount > 0)
1364 timeout_queues_map |= BIT(i);
1365 spin_unlock_bh(&mvm->queue_info_lock);
1366
1367 rcu_read_lock();
1368
1369 /*
1370 * If a queue time outs - mark it as INACTIVE (don't remove right away
1371 * if we don't have to.) This is an optimization in case traffic comes
1372 * later, and we don't HAVE to use a currently-inactive queue
1373 */
1374 for_each_set_bit(i, &timeout_queues_map, IWL_MAX_HW_QUEUES) {
1375 struct ieee80211_sta *sta;
1376 struct iwl_mvm_sta *mvmsta;
1377 u8 sta_id;
1378 int tid;
1379 unsigned long inactive_tid_bitmap = 0;
1380 unsigned long queue_tid_bitmap;
1381
1382 spin_lock_bh(&mvm->queue_info_lock);
1383 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1384
1385 /* If TXQ isn't in active use anyway - nothing to do here... */
Liad Kaufman42db09c2016-05-02 14:01:14 +03001386 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1387 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED) {
Liad Kaufman9794c642015-08-19 17:34:28 +03001388 spin_unlock_bh(&mvm->queue_info_lock);
1389 continue;
1390 }
1391
1392 /* Check to see if there are inactive TIDs on this queue */
1393 for_each_set_bit(tid, &queue_tid_bitmap,
1394 IWL_MAX_TID_COUNT + 1) {
1395 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1396 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1397 continue;
1398
1399 inactive_tid_bitmap |= BIT(tid);
1400 }
1401 spin_unlock_bh(&mvm->queue_info_lock);
1402
1403 /* If all TIDs are active - finish check on this queue */
1404 if (!inactive_tid_bitmap)
1405 continue;
1406
1407 /*
1408 * If we are here - the queue hadn't been served recently and is
1409 * in use
1410 */
1411
1412 sta_id = mvm->queue_info[i].ra_sta_id;
1413 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1414
1415 /*
1416 * If the STA doesn't exist anymore, it isn't an error. It could
1417 * be that it was removed since getting the queues, and in this
1418 * case it should've inactivated its queues anyway.
1419 */
1420 if (IS_ERR_OR_NULL(sta))
1421 continue;
1422
1423 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1424
1425 spin_lock_bh(&mvmsta->lock);
1426 spin_lock(&mvm->queue_info_lock);
1427 iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1428 inactive_tid_bitmap);
1429 spin_unlock(&mvm->queue_info_lock);
1430 spin_unlock_bh(&mvmsta->lock);
1431 }
1432
1433 rcu_read_unlock();
1434}
1435
Emmanuel Grumbach528a5422017-08-31 11:52:30 +03001436void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
1437 struct ieee80211_vif *vif,
1438 const struct ieee80211_sta *sta,
1439 u16 tid)
1440{
1441 struct iwl_fw_dbg_trigger_tlv *trig;
1442 struct iwl_fw_dbg_trigger_ba *ba_trig;
1443
1444 if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
1445 return;
1446
1447 trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
1448 ba_trig = (void *)trig->data;
1449 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1450 ieee80211_vif_to_wdev(vif), trig))
1451 return;
1452
1453 if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
1454 return;
1455
1456 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1457 "Frame from %pM timed out, tid %d",
1458 sta->addr, tid);
1459}
1460
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001461u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
1462{
1463 if (!elapsed)
1464 return 0;
1465
1466 return (100 * airtime / elapsed) / USEC_PER_MSEC;
1467}
1468
1469static enum iwl_mvm_traffic_load
1470iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
1471{
1472 u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
1473
1474 if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
1475 return IWL_MVM_TRAFFIC_HIGH;
1476 if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
1477 return IWL_MVM_TRAFFIC_MEDIUM;
1478
1479 return IWL_MVM_TRAFFIC_LOW;
1480}
1481
1482struct iwl_mvm_tcm_iter_data {
1483 struct iwl_mvm *mvm;
1484 bool any_sent;
1485};
1486
1487static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
1488{
1489 struct iwl_mvm_tcm_iter_data *data = _data;
1490 struct iwl_mvm *mvm = data->mvm;
1491 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1492 bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
1493
1494 if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
1495 return;
1496
1497 low_latency = mvm->tcm.result.low_latency[mvmvif->id];
1498
1499 if (!mvm->tcm.result.change[mvmvif->id] &&
1500 prev == low_latency) {
1501 iwl_mvm_update_quotas(mvm, false, NULL);
1502 return;
1503 }
1504
1505 if (prev != low_latency) {
1506 /* this sends traffic load and updates quota as well */
1507 iwl_mvm_update_low_latency(mvm, vif, low_latency,
1508 LOW_LATENCY_TRAFFIC);
1509 } else {
1510 iwl_mvm_update_quotas(mvm, false, NULL);
1511 }
1512
1513 data->any_sent = true;
1514}
1515
1516static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
1517{
1518 struct iwl_mvm_tcm_iter_data data = {
1519 .mvm = mvm,
1520 .any_sent = false,
1521 };
1522
1523 mutex_lock(&mvm->mutex);
1524
1525 ieee80211_iterate_active_interfaces(
1526 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1527 iwl_mvm_tcm_iter, &data);
1528
1529 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1530 iwl_mvm_config_scan(mvm);
1531
1532 mutex_unlock(&mvm->mutex);
1533}
1534
Johannes Bergb0ffe452014-11-11 12:57:03 +01001535static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
1536{
1537 struct iwl_mvm *mvm;
1538 struct iwl_mvm_vif *mvmvif;
1539 struct ieee80211_vif *vif;
1540
1541 mvmvif = container_of(wk, struct iwl_mvm_vif,
1542 uapsd_nonagg_detected_wk.work);
1543 vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1544 mvm = mvmvif->mvm;
1545
1546 if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
1547 return;
1548
1549 /* remember that this AP is broken */
1550 memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
1551 vif->bss_conf.bssid, ETH_ALEN);
1552 mvm->uapsd_noagg_bssid_write_idx++;
1553 if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
1554 mvm->uapsd_noagg_bssid_write_idx = 0;
1555
1556 iwl_mvm_connection_loss(mvm, vif,
1557 "AP isn't using AMPDU with uAPSD enabled");
1558}
1559
1560static void iwl_mvm_uapsd_agg_disconnect_iter(void *data, u8 *mac,
1561 struct ieee80211_vif *vif)
1562{
1563 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1564 struct iwl_mvm *mvm = mvmvif->mvm;
1565 int *mac_id = data;
1566
1567 if (vif->type != NL80211_IFTYPE_STATION)
1568 return;
1569
1570 if (mvmvif->id != *mac_id)
1571 return;
1572
1573 if (!vif->bss_conf.assoc)
1574 return;
1575
1576 if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
1577 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
1578 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
1579 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
1580 return;
1581
1582 if (mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected)
1583 return;
1584
1585 mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected = true;
1586 IWL_INFO(mvm,
1587 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
1588 schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
1589}
1590
1591static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
1592 unsigned int elapsed,
1593 int mac)
1594{
1595 u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
1596 u64 tpt;
1597 unsigned long rate;
1598
1599 rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
1600
1601 if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
1602 mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
1603 return;
1604
1605 if (iwl_mvm_has_new_rx_api(mvm)) {
1606 tpt = 8 * bytes; /* kbps */
1607 do_div(tpt, elapsed);
1608 rate *= 1000; /* kbps */
1609 if (tpt < 22 * rate / 100)
1610 return;
1611 } else {
1612 /*
1613 * the rate here is actually the threshold, in 100Kbps units,
1614 * so do the needed conversion from bytes to 100Kbps:
1615 * 100kb = bits / (100 * 1000),
1616 * 100kbps = 100kb / (msecs / 1000) ==
1617 * (bits / (100 * 1000)) / (msecs / 1000) ==
1618 * bits / (100 * msecs)
1619 */
1620 tpt = (8 * bytes);
1621 do_div(tpt, elapsed * 100);
1622 if (tpt < rate)
1623 return;
1624 }
1625
1626 ieee80211_iterate_active_interfaces_atomic(
1627 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1628 iwl_mvm_uapsd_agg_disconnect_iter, &mac);
1629}
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001630
Sara Sharonb66b5812017-01-31 10:39:44 +02001631static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
1632 struct ieee80211_vif *vif)
1633{
1634 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1635 u32 *band = _data;
1636
1637 if (!mvmvif->phy_ctxt)
1638 return;
1639
1640 band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
1641}
1642
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001643static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
1644 unsigned long ts,
1645 bool handle_uapsd)
1646{
1647 unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
Johannes Bergb0ffe452014-11-11 12:57:03 +01001648 unsigned int uapsd_elapsed =
1649 jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001650 u32 total_airtime = 0;
Sara Sharonb66b5812017-01-31 10:39:44 +02001651 u32 band_airtime[NUM_NL80211_BANDS] = {0};
1652 u32 band[NUM_MAC_INDEX_DRIVER] = {0};
1653 int ac, mac, i;
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001654 bool low_latency = false;
Sara Sharonb66b5812017-01-31 10:39:44 +02001655 enum iwl_mvm_traffic_load load, band_load;
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001656 bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
1657
1658 if (handle_ll)
1659 mvm->tcm.ll_ts = ts;
1660 if (handle_uapsd)
1661 mvm->tcm.uapsd_nonagg_ts = ts;
1662
1663 mvm->tcm.result.elapsed = elapsed;
1664
Sara Sharonb66b5812017-01-31 10:39:44 +02001665 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1666 IEEE80211_IFACE_ITER_NORMAL,
1667 iwl_mvm_tcm_iterator,
1668 &band);
1669
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001670 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1671 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1672 u32 vo_vi_pkts = 0;
1673 u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1674
1675 total_airtime += airtime;
Sara Sharonb66b5812017-01-31 10:39:44 +02001676 band_airtime[band[mac]] += airtime;
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001677
1678 load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1679 mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1680 mvm->tcm.result.load[mac] = load;
1681 mvm->tcm.result.airtime[mac] = airtime;
1682
1683 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1684 vo_vi_pkts += mdata->rx.pkts[ac] +
1685 mdata->tx.pkts[ac];
1686
1687 /* enable immediately with enough packets but defer disabling */
1688 if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1689 mvm->tcm.result.low_latency[mac] = true;
1690 else if (handle_ll)
1691 mvm->tcm.result.low_latency[mac] = false;
1692
1693 if (handle_ll) {
1694 /* clear old data */
1695 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1696 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1697 }
1698 low_latency |= mvm->tcm.result.low_latency[mac];
1699
Johannes Bergb0ffe452014-11-11 12:57:03 +01001700 if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1701 iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1702 mac);
1703 /* clear old data */
1704 if (handle_uapsd)
1705 mdata->uapsd_nonagg_detect.rx_bytes = 0;
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001706 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1707 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1708 }
1709
1710 load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1711 mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
1712 mvm->tcm.result.global_load = load;
1713
Sara Sharonb66b5812017-01-31 10:39:44 +02001714 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1715 band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1716 mvm->tcm.result.band_load[i] = band_load;
1717 }
1718
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001719 /*
1720 * If the current load isn't low we need to force re-evaluation
1721 * in the TCM period, so that we can return to low load if there
1722 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1723 * triggered by traffic).
1724 */
1725 if (load != IWL_MVM_TRAFFIC_LOW)
1726 return MVM_TCM_PERIOD;
1727 /*
1728 * If low-latency is active we need to force re-evaluation after
1729 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1730 * when there's no traffic at all.
1731 */
1732 if (low_latency)
1733 return MVM_LL_PERIOD;
1734 /*
1735 * Otherwise, we don't need to run the work struct because we're
1736 * in the default "idle" state - traffic indication is low (which
1737 * also covers the "no traffic" case) and low-latency is disabled
1738 * so there's no state that may need to be disabled when there's
1739 * no traffic at all.
1740 *
1741 * Note that this has no impact on the regular scheduling of the
1742 * updates triggered by traffic - those happen whenever one of the
1743 * two timeouts expire (if there's traffic at all.)
1744 */
1745 return 0;
1746}
1747
1748void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1749{
1750 unsigned long ts = jiffies;
1751 bool handle_uapsd =
Johannes Bergb0ffe452014-11-11 12:57:03 +01001752 time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1753 msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001754
1755 spin_lock(&mvm->tcm.lock);
1756 if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1757 spin_unlock(&mvm->tcm.lock);
1758 return;
1759 }
1760 spin_unlock(&mvm->tcm.lock);
1761
Johannes Bergb0ffe452014-11-11 12:57:03 +01001762 if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1763 mutex_lock(&mvm->mutex);
1764 if (iwl_mvm_request_statistics(mvm, true))
1765 handle_uapsd = false;
1766 mutex_unlock(&mvm->mutex);
1767 }
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001768
1769 spin_lock(&mvm->tcm.lock);
1770 /* re-check if somebody else won the recheck race */
1771 if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1772 /* calculate statistics */
1773 unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1774 handle_uapsd);
1775
1776 /* the memset needs to be visible before the timestamp */
1777 smp_mb();
1778 mvm->tcm.ts = ts;
1779 if (work_delay)
1780 schedule_delayed_work(&mvm->tcm.work, work_delay);
1781 }
1782 spin_unlock(&mvm->tcm.lock);
1783
1784 iwl_mvm_tcm_results(mvm);
1785}
1786
1787void iwl_mvm_tcm_work(struct work_struct *work)
1788{
1789 struct delayed_work *delayed_work = to_delayed_work(work);
1790 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1791 tcm.work);
1792
1793 iwl_mvm_recalc_tcm(mvm);
1794}
1795
1796void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1797{
1798 spin_lock_bh(&mvm->tcm.lock);
1799 mvm->tcm.paused = true;
1800 spin_unlock_bh(&mvm->tcm.lock);
1801 if (with_cancel)
1802 cancel_delayed_work_sync(&mvm->tcm.work);
1803}
1804
1805void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1806{
1807 int mac;
1808
1809 spin_lock_bh(&mvm->tcm.lock);
1810 mvm->tcm.ts = jiffies;
1811 mvm->tcm.ll_ts = jiffies;
1812 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1813 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1814
1815 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1816 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1817 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1818 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1819 }
1820 /* The TCM data needs to be reset before "paused" flag changes */
1821 smp_mb();
1822 mvm->tcm.paused = false;
1823 spin_unlock_bh(&mvm->tcm.lock);
1824}
1825
Johannes Bergb0ffe452014-11-11 12:57:03 +01001826void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1827{
1828 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1829
1830 INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1831 iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1832}
1833
1834void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1835{
1836 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1837
1838 cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1839}
1840
Luca Coelho7d9d0d52018-04-12 16:15:07 +03001841
Roee Zamirb3bee582016-08-02 13:55:13 +03001842void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
1843{
1844 bool ps_disabled;
1845
1846 lockdep_assert_held(&mvm->mutex);
1847
1848 /* Disable power save when reading GP2 */
1849 ps_disabled = mvm->ps_disabled;
1850 if (!ps_disabled) {
1851 mvm->ps_disabled = true;
1852 iwl_mvm_power_update_device(mvm);
1853 }
1854
1855 *gp2 = iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
1856 *boottime = ktime_get_boot_ns();
1857
1858 if (!ps_disabled) {
1859 mvm->ps_disabled = ps_disabled;
1860 iwl_mvm_power_update_device(mvm);
1861 }
1862}