iwlwifi: mvm: prevent sched scan while not idle
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / utils.c
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  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <net/mac80211.h>
64
65 #include "iwl-debug.h"
66 #include "iwl-io.h"
67
68 #include "mvm.h"
69 #include "fw-api-rs.h"
70
71 /*
72  * Will return 0 even if the cmd failed when RFKILL is asserted unless
73  * CMD_WANT_SKB is set in cmd->flags.
74  */
75 int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
76 {
77         int ret;
78
79 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
80         if (WARN_ON(mvm->d3_test_active))
81                 return -EIO;
82 #endif
83
84         /*
85          * Synchronous commands from this op-mode must hold
86          * the mutex, this ensures we don't try to send two
87          * (or more) synchronous commands at a time.
88          */
89         if (!(cmd->flags & CMD_ASYNC))
90                 lockdep_assert_held(&mvm->mutex);
91
92         ret = iwl_trans_send_cmd(mvm->trans, cmd);
93
94         /*
95          * If the caller wants the SKB, then don't hide any problems, the
96          * caller might access the response buffer which will be NULL if
97          * the command failed.
98          */
99         if (cmd->flags & CMD_WANT_SKB)
100                 return ret;
101
102         /* Silently ignore failures if RFKILL is asserted */
103         if (!ret || ret == -ERFKILL)
104                 return 0;
105         return ret;
106 }
107
108 int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u8 id,
109                          u32 flags, u16 len, const void *data)
110 {
111         struct iwl_host_cmd cmd = {
112                 .id = id,
113                 .len = { len, },
114                 .data = { data, },
115                 .flags = flags,
116         };
117
118         return iwl_mvm_send_cmd(mvm, &cmd);
119 }
120
121 /*
122  * We assume that the caller set the status to the sucess value
123  */
124 int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
125                             u32 *status)
126 {
127         struct iwl_rx_packet *pkt;
128         struct iwl_cmd_response *resp;
129         int ret, resp_len;
130
131         lockdep_assert_held(&mvm->mutex);
132
133 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
134         if (WARN_ON(mvm->d3_test_active))
135                 return -EIO;
136 #endif
137
138         /*
139          * Only synchronous commands can wait for status,
140          * we use WANT_SKB so the caller can't.
141          */
142         if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
143                       "cmd flags %x", cmd->flags))
144                 return -EINVAL;
145
146         cmd->flags |= CMD_SYNC | CMD_WANT_SKB;
147
148         ret = iwl_trans_send_cmd(mvm->trans, cmd);
149         if (ret == -ERFKILL) {
150                 /*
151                  * The command failed because of RFKILL, don't update
152                  * the status, leave it as success and return 0.
153                  */
154                 return 0;
155         } else if (ret) {
156                 return ret;
157         }
158
159         pkt = cmd->resp_pkt;
160         /* Can happen if RFKILL is asserted */
161         if (!pkt) {
162                 ret = 0;
163                 goto out_free_resp;
164         }
165
166         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
167                 ret = -EIO;
168                 goto out_free_resp;
169         }
170
171         resp_len = iwl_rx_packet_payload_len(pkt);
172         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
173                 ret = -EIO;
174                 goto out_free_resp;
175         }
176
177         resp = (void *)pkt->data;
178         *status = le32_to_cpu(resp->status);
179  out_free_resp:
180         iwl_free_resp(cmd);
181         return ret;
182 }
183
184 /*
185  * We assume that the caller set the status to the sucess value
186  */
187 int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u8 id, u16 len,
188                                 const void *data, u32 *status)
189 {
190         struct iwl_host_cmd cmd = {
191                 .id = id,
192                 .len = { len, },
193                 .data = { data, },
194         };
195
196         return iwl_mvm_send_cmd_status(mvm, &cmd, status);
197 }
198
199 #define IWL_DECLARE_RATE_INFO(r) \
200         [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
201
202 /*
203  * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
204  */
205 static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
206         IWL_DECLARE_RATE_INFO(1),
207         IWL_DECLARE_RATE_INFO(2),
208         IWL_DECLARE_RATE_INFO(5),
209         IWL_DECLARE_RATE_INFO(11),
210         IWL_DECLARE_RATE_INFO(6),
211         IWL_DECLARE_RATE_INFO(9),
212         IWL_DECLARE_RATE_INFO(12),
213         IWL_DECLARE_RATE_INFO(18),
214         IWL_DECLARE_RATE_INFO(24),
215         IWL_DECLARE_RATE_INFO(36),
216         IWL_DECLARE_RATE_INFO(48),
217         IWL_DECLARE_RATE_INFO(54),
218 };
219
220 int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
221                                         enum ieee80211_band band)
222 {
223         int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
224         int idx;
225         int band_offset = 0;
226
227         /* Legacy rate format, search for match in table */
228         if (band == IEEE80211_BAND_5GHZ)
229                 band_offset = IWL_FIRST_OFDM_RATE;
230         for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
231                 if (fw_rate_idx_to_plcp[idx] == rate)
232                         return idx - band_offset;
233
234         return -1;
235 }
236
237 u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
238 {
239         /* Get PLCP rate for tx_cmd->rate_n_flags */
240         return fw_rate_idx_to_plcp[rate_idx];
241 }
242
243 int iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
244                           struct iwl_device_cmd *cmd)
245 {
246         struct iwl_rx_packet *pkt = rxb_addr(rxb);
247         struct iwl_error_resp *err_resp = (void *)pkt->data;
248
249         IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
250                 le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
251         IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
252                 le16_to_cpu(err_resp->bad_cmd_seq_num),
253                 le32_to_cpu(err_resp->error_service));
254         IWL_ERR(mvm, "FW Error notification: timestamp 0x%16llX\n",
255                 le64_to_cpu(err_resp->timestamp));
256         return 0;
257 }
258
259 /*
260  * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
261  * The parameter should also be a combination of ANT_[ABC].
262  */
263 u8 first_antenna(u8 mask)
264 {
265         BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
266         if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
267                 return BIT(0);
268         return BIT(ffs(mask) - 1);
269 }
270
271 /*
272  * Toggles between TX antennas to send the probe request on.
273  * Receives the bitmask of valid TX antennas and the *index* used
274  * for the last TX, and returns the next valid *index* to use.
275  * In order to set it in the tx_cmd, must do BIT(idx).
276  */
277 u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
278 {
279         u8 ind = last_idx;
280         int i;
281
282         for (i = 0; i < RATE_MCS_ANT_NUM; i++) {
283                 ind = (ind + 1) % RATE_MCS_ANT_NUM;
284                 if (valid & BIT(ind))
285                         return ind;
286         }
287
288         WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
289         return last_idx;
290 }
291
292 static const struct {
293         const char *name;
294         u8 num;
295 } advanced_lookup[] = {
296         { "NMI_INTERRUPT_WDG", 0x34 },
297         { "SYSASSERT", 0x35 },
298         { "UCODE_VERSION_MISMATCH", 0x37 },
299         { "BAD_COMMAND", 0x38 },
300         { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
301         { "FATAL_ERROR", 0x3D },
302         { "NMI_TRM_HW_ERR", 0x46 },
303         { "NMI_INTERRUPT_TRM", 0x4C },
304         { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
305         { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
306         { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
307         { "NMI_INTERRUPT_HOST", 0x66 },
308         { "NMI_INTERRUPT_ACTION_PT", 0x7C },
309         { "NMI_INTERRUPT_UNKNOWN", 0x84 },
310         { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
311         { "ADVANCED_SYSASSERT", 0 },
312 };
313
314 static const char *desc_lookup(u32 num)
315 {
316         int i;
317
318         for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
319                 if (advanced_lookup[i].num == num)
320                         return advanced_lookup[i].name;
321
322         /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
323         return advanced_lookup[i].name;
324 }
325
326 /*
327  * Note: This structure is read from the device with IO accesses,
328  * and the reading already does the endian conversion. As it is
329  * read with u32-sized accesses, any members with a different size
330  * need to be ordered correctly though!
331  */
332 struct iwl_error_event_table {
333         u32 valid;              /* (nonzero) valid, (0) log is empty */
334         u32 error_id;           /* type of error */
335         u32 pc;                 /* program counter */
336         u32 blink1;             /* branch link */
337         u32 blink2;             /* branch link */
338         u32 ilink1;             /* interrupt link */
339         u32 ilink2;             /* interrupt link */
340         u32 data1;              /* error-specific data */
341         u32 data2;              /* error-specific data */
342         u32 data3;              /* error-specific data */
343         u32 bcon_time;          /* beacon timer */
344         u32 tsf_low;            /* network timestamp function timer */
345         u32 tsf_hi;             /* network timestamp function timer */
346         u32 gp1;                /* GP1 timer register */
347         u32 gp2;                /* GP2 timer register */
348         u32 gp3;                /* GP3 timer register */
349         u32 ucode_ver;          /* uCode version */
350         u32 hw_ver;             /* HW Silicon version */
351         u32 brd_ver;            /* HW board version */
352         u32 log_pc;             /* log program counter */
353         u32 frame_ptr;          /* frame pointer */
354         u32 stack_ptr;          /* stack pointer */
355         u32 hcmd;               /* last host command header */
356         u32 isr0;               /* isr status register LMPM_NIC_ISR0:
357                                  * rxtx_flag */
358         u32 isr1;               /* isr status register LMPM_NIC_ISR1:
359                                  * host_flag */
360         u32 isr2;               /* isr status register LMPM_NIC_ISR2:
361                                  * enc_flag */
362         u32 isr3;               /* isr status register LMPM_NIC_ISR3:
363                                  * time_flag */
364         u32 isr4;               /* isr status register LMPM_NIC_ISR4:
365                                  * wico interrupt */
366         u32 isr_pref;           /* isr status register LMPM_NIC_PREF_STAT */
367         u32 wait_event;         /* wait event() caller address */
368         u32 l2p_control;        /* L2pControlField */
369         u32 l2p_duration;       /* L2pDurationField */
370         u32 l2p_mhvalid;        /* L2pMhValidBits */
371         u32 l2p_addr_match;     /* L2pAddrMatchStat */
372         u32 lmpm_pmg_sel;       /* indicate which clocks are turned on
373                                  * (LMPM_PMG_SEL) */
374         u32 u_timestamp;        /* indicate when the date and time of the
375                                  * compilation */
376         u32 flow_handler;       /* FH read/write pointers, RX credit */
377 } __packed;
378
379 /*
380  * UMAC error struct - relevant starting from family 8000 chip.
381  * Note: This structure is read from the device with IO accesses,
382  * and the reading already does the endian conversion. As it is
383  * read with u32-sized accesses, any members with a different size
384  * need to be ordered correctly though!
385  */
386 struct iwl_umac_error_event_table {
387         u32 valid;              /* (nonzero) valid, (0) log is empty */
388         u32 error_id;           /* type of error */
389         u32 pc;                 /* program counter */
390         u32 blink1;             /* branch link */
391         u32 blink2;             /* branch link */
392         u32 ilink1;             /* interrupt link */
393         u32 ilink2;             /* interrupt link */
394         u32 data1;              /* error-specific data */
395         u32 data2;              /* error-specific data */
396         u32 line;               /* source code line of error */
397         u32 umac_ver;           /* umac version */
398 } __packed;
399
400 #define ERROR_START_OFFSET  (1 * sizeof(u32))
401 #define ERROR_ELEM_SIZE     (7 * sizeof(u32))
402
403 static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
404 {
405         struct iwl_trans *trans = mvm->trans;
406         struct iwl_umac_error_event_table table;
407         u32 base;
408
409         base = mvm->umac_error_event_table;
410
411         if (base < 0x800000 || base >= 0x80C000) {
412                 IWL_ERR(mvm,
413                         "Not valid error log pointer 0x%08X for %s uCode\n",
414                         base,
415                         (mvm->cur_ucode == IWL_UCODE_INIT)
416                                         ? "Init" : "RT");
417                 return;
418         }
419
420         iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
421
422         if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
423                 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
424                 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
425                         mvm->status, table.valid);
426         }
427
428         IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
429                 desc_lookup(table.error_id));
430         IWL_ERR(mvm, "0x%08X | umac uPc\n", table.pc);
431         IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
432         IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
433         IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
434         IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
435         IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
436         IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
437         IWL_ERR(mvm, "0x%08X | umac version\n", table.umac_ver);
438 }
439
440 void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
441 {
442         struct iwl_trans *trans = mvm->trans;
443         struct iwl_error_event_table table;
444         u32 base;
445
446         base = mvm->error_event_table;
447         if (mvm->cur_ucode == IWL_UCODE_INIT) {
448                 if (!base)
449                         base = mvm->fw->init_errlog_ptr;
450         } else {
451                 if (!base)
452                         base = mvm->fw->inst_errlog_ptr;
453         }
454
455         if (base < 0x800000) {
456                 IWL_ERR(mvm,
457                         "Not valid error log pointer 0x%08X for %s uCode\n",
458                         base,
459                         (mvm->cur_ucode == IWL_UCODE_INIT)
460                                         ? "Init" : "RT");
461                 return;
462         }
463
464         iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
465
466         if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
467                 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
468                 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
469                         mvm->status, table.valid);
470         }
471
472         IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
473
474         trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
475                                       table.data1, table.data2, table.data3,
476                                       table.blink1, table.blink2, table.ilink1,
477                                       table.ilink2, table.bcon_time, table.gp1,
478                                       table.gp2, table.gp3, table.ucode_ver,
479                                       table.hw_ver, table.brd_ver);
480         IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
481                 desc_lookup(table.error_id));
482         IWL_ERR(mvm, "0x%08X | uPc\n", table.pc);
483         IWL_ERR(mvm, "0x%08X | branchlink1\n", table.blink1);
484         IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
485         IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
486         IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
487         IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
488         IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
489         IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
490         IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
491         IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
492         IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
493         IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
494         IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
495         IWL_ERR(mvm, "0x%08X | time gp3\n", table.gp3);
496         IWL_ERR(mvm, "0x%08X | uCode version\n", table.ucode_ver);
497         IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
498         IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
499         IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
500         IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
501         IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
502         IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
503         IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
504         IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
505         IWL_ERR(mvm, "0x%08X | isr_pref\n", table.isr_pref);
506         IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
507         IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
508         IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
509         IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
510         IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
511         IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
512         IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
513         IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
514
515         if (mvm->support_umac_log)
516                 iwl_mvm_dump_umac_error_log(mvm);
517 }
518
519 void iwl_mvm_fw_error_sram_dump(struct iwl_mvm *mvm)
520 {
521         const struct fw_img *img;
522         u32 ofs, sram_len;
523         void *sram;
524
525         if (!mvm->ucode_loaded || mvm->fw_error_sram)
526                 return;
527
528         img = &mvm->fw->img[mvm->cur_ucode];
529         ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
530         sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
531
532         sram = kzalloc(sram_len, GFP_ATOMIC);
533         if (!sram)
534                 return;
535
536         iwl_trans_read_mem_bytes(mvm->trans, ofs, sram, sram_len);
537         mvm->fw_error_sram = sram;
538         mvm->fw_error_sram_len = sram_len;
539 }
540
541 /**
542  * iwl_mvm_send_lq_cmd() - Send link quality command
543  * @init: This command is sent as part of station initialization right
544  *        after station has been added.
545  *
546  * The link quality command is sent as the last step of station creation.
547  * This is the special case in which init is set and we call a callback in
548  * this case to clear the state indicating that station creation is in
549  * progress.
550  */
551 int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq, bool init)
552 {
553         struct iwl_host_cmd cmd = {
554                 .id = LQ_CMD,
555                 .len = { sizeof(struct iwl_lq_cmd), },
556                 .flags = init ? CMD_SYNC : CMD_ASYNC,
557                 .data = { lq, },
558         };
559
560         if (WARN_ON(lq->sta_id == IWL_MVM_STATION_COUNT))
561                 return -EINVAL;
562
563         return iwl_mvm_send_cmd(mvm, &cmd);
564 }
565
566 /**
567  * iwl_mvm_update_smps - Get a requst to change the SMPS mode
568  * @req_type: The part of the driver who call for a change.
569  * @smps_requests: The request to change the SMPS mode.
570  *
571  * Get a requst to change the SMPS mode,
572  * and change it according to all other requests in the driver.
573  */
574 void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
575                          enum iwl_mvm_smps_type_request req_type,
576                          enum ieee80211_smps_mode smps_request)
577 {
578         struct iwl_mvm_vif *mvmvif;
579         enum ieee80211_smps_mode smps_mode;
580         int i;
581
582         lockdep_assert_held(&mvm->mutex);
583
584         /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
585         if (num_of_ant(mvm->fw->valid_rx_ant) == 1)
586                 return;
587
588         if (vif->type == NL80211_IFTYPE_AP)
589                 smps_mode = IEEE80211_SMPS_OFF;
590         else
591                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
592
593         mvmvif = iwl_mvm_vif_from_mac80211(vif);
594         mvmvif->smps_requests[req_type] = smps_request;
595         for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
596                 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
597                         smps_mode = IEEE80211_SMPS_STATIC;
598                         break;
599                 }
600                 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
601                         smps_mode = IEEE80211_SMPS_DYNAMIC;
602         }
603
604         ieee80211_request_smps(vif, smps_mode);
605 }
606
607 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
608                                bool value)
609 {
610         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
611         int res;
612
613         lockdep_assert_held(&mvm->mutex);
614
615         if (mvmvif->low_latency == value)
616                 return 0;
617
618         mvmvif->low_latency = value;
619
620         res = iwl_mvm_update_quotas(mvm, NULL);
621         if (res)
622                 return res;
623
624         iwl_mvm_bt_coex_vif_change(mvm);
625
626         return iwl_mvm_power_update_mac(mvm, vif);
627 }
628
629 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
630 {
631         bool *result = _data;
632
633         if (iwl_mvm_vif_low_latency(iwl_mvm_vif_from_mac80211(vif)))
634                 *result = true;
635 }
636
637 bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
638 {
639         bool result = false;
640
641         ieee80211_iterate_active_interfaces_atomic(
642                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
643                         iwl_mvm_ll_iter, &result);
644
645         return result;
646 }
647
648 static void iwl_mvm_idle_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
649 {
650         bool *idle = _data;
651
652         if (!vif->bss_conf.idle)
653                 *idle = false;
654 }
655
656 bool iwl_mvm_is_idle(struct iwl_mvm *mvm)
657 {
658         bool idle = true;
659
660         ieee80211_iterate_active_interfaces_atomic(
661                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
662                         iwl_mvm_idle_iter, &idle);
663
664         return idle;
665 }