linux/drivers/net/wireless/intel/iwlwifi/mvm/utils.c
<<
>>
Prefs
   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, 2018 - 2020 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of version 2 of the GNU General Public License as
  14 * published by the Free Software Foundation.
  15 *
  16 * This program is distributed in the hope that it will be useful, but
  17 * WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19 * General Public License for more details.
  20 *
  21 * The full GNU General Public License is included in this distribution
  22 * in the file called COPYING.
  23 *
  24 * Contact Information:
  25 *  Intel Linux Wireless <linuxwifi@intel.com>
  26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27 *
  28 * BSD LICENSE
  29 *
  30 * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation. All rights reserved.
  31 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  32 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  33 * All rights reserved.
  34 *
  35 * Redistribution and use in source and binary forms, with or without
  36 * modification, are permitted provided that the following conditions
  37 * are met:
  38 *
  39 *  * Redistributions of source code must retain the above copyright
  40 *    notice, this list of conditions and the following disclaimer.
  41 *  * Redistributions in binary form must reproduce the above copyright
  42 *    notice, this list of conditions and the following disclaimer in
  43 *    the documentation and/or other materials provided with the
  44 *    distribution.
  45 *  * Neither the name Intel Corporation nor the names of its
  46 *    contributors may be used to endorse or promote products derived
  47 *    from this software without specific prior written permission.
  48 *
  49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60 *
  61 *****************************************************************************/
  62#include <net/mac80211.h>
  63
  64#include "iwl-debug.h"
  65#include "iwl-io.h"
  66#include "iwl-prph.h"
  67#include "iwl-csr.h"
  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 */
  75int 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
 108int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 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 success value
 123 */
 124int 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_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
 161        resp_len = iwl_rx_packet_payload_len(pkt);
 162        if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
 163                ret = -EIO;
 164                goto out_free_resp;
 165        }
 166
 167        resp = (void *)pkt->data;
 168        *status = le32_to_cpu(resp->status);
 169 out_free_resp:
 170        iwl_free_resp(cmd);
 171        return ret;
 172}
 173
 174/*
 175 * We assume that the caller set the status to the sucess value
 176 */
 177int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
 178                                const void *data, u32 *status)
 179{
 180        struct iwl_host_cmd cmd = {
 181                .id = id,
 182                .len = { len, },
 183                .data = { data, },
 184        };
 185
 186        return iwl_mvm_send_cmd_status(mvm, &cmd, status);
 187}
 188
 189#define IWL_DECLARE_RATE_INFO(r) \
 190        [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
 191
 192/*
 193 * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
 194 */
 195static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
 196        IWL_DECLARE_RATE_INFO(1),
 197        IWL_DECLARE_RATE_INFO(2),
 198        IWL_DECLARE_RATE_INFO(5),
 199        IWL_DECLARE_RATE_INFO(11),
 200        IWL_DECLARE_RATE_INFO(6),
 201        IWL_DECLARE_RATE_INFO(9),
 202        IWL_DECLARE_RATE_INFO(12),
 203        IWL_DECLARE_RATE_INFO(18),
 204        IWL_DECLARE_RATE_INFO(24),
 205        IWL_DECLARE_RATE_INFO(36),
 206        IWL_DECLARE_RATE_INFO(48),
 207        IWL_DECLARE_RATE_INFO(54),
 208};
 209
 210int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
 211                                        enum nl80211_band band)
 212{
 213        int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
 214        int idx;
 215        int band_offset = 0;
 216
 217        /* Legacy rate format, search for match in table */
 218        if (band != NL80211_BAND_2GHZ)
 219                band_offset = IWL_FIRST_OFDM_RATE;
 220        for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
 221                if (fw_rate_idx_to_plcp[idx] == rate)
 222                        return idx - band_offset;
 223
 224        return -1;
 225}
 226
 227u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
 228{
 229        /* Get PLCP rate for tx_cmd->rate_n_flags */
 230        return fw_rate_idx_to_plcp[rate_idx];
 231}
 232
 233u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
 234{
 235        static const u8 mac80211_ac_to_ucode_ac[] = {
 236                AC_VO,
 237                AC_VI,
 238                AC_BE,
 239                AC_BK
 240        };
 241
 242        return mac80211_ac_to_ucode_ac[ac];
 243}
 244
 245void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
 246{
 247        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 248        struct iwl_error_resp *err_resp = (void *)pkt->data;
 249
 250        IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
 251                le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
 252        IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
 253                le16_to_cpu(err_resp->bad_cmd_seq_num),
 254                le32_to_cpu(err_resp->error_service));
 255        IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
 256                le64_to_cpu(err_resp->timestamp));
 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 */
 263u8 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 */
 277u8 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 < MAX_ANT_NUM; i++) {
 283                ind = (ind + 1) % MAX_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#define FW_SYSASSERT_CPU_MASK 0xf0000000
 293static const struct {
 294        const char *name;
 295        u8 num;
 296} advanced_lookup[] = {
 297        { "NMI_INTERRUPT_WDG", 0x34 },
 298        { "SYSASSERT", 0x35 },
 299        { "UCODE_VERSION_MISMATCH", 0x37 },
 300        { "BAD_COMMAND", 0x38 },
 301        { "BAD_COMMAND", 0x39 },
 302        { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
 303        { "FATAL_ERROR", 0x3D },
 304        { "NMI_TRM_HW_ERR", 0x46 },
 305        { "NMI_INTERRUPT_TRM", 0x4C },
 306        { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
 307        { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
 308        { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
 309        { "NMI_INTERRUPT_HOST", 0x66 },
 310        { "NMI_INTERRUPT_LMAC_FATAL", 0x70 },
 311        { "NMI_INTERRUPT_UMAC_FATAL", 0x71 },
 312        { "NMI_INTERRUPT_OTHER_LMAC_FATAL", 0x73 },
 313        { "NMI_INTERRUPT_ACTION_PT", 0x7C },
 314        { "NMI_INTERRUPT_UNKNOWN", 0x84 },
 315        { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
 316        { "ADVANCED_SYSASSERT", 0 },
 317};
 318
 319static const char *desc_lookup(u32 num)
 320{
 321        int i;
 322
 323        for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
 324                if (advanced_lookup[i].num == (num & ~FW_SYSASSERT_CPU_MASK))
 325                        return advanced_lookup[i].name;
 326
 327        /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
 328        return advanced_lookup[i].name;
 329}
 330
 331/*
 332 * Note: This structure is read from the device with IO accesses,
 333 * and the reading already does the endian conversion. As it is
 334 * read with u32-sized accesses, any members with a different size
 335 * need to be ordered correctly though!
 336 */
 337struct iwl_error_event_table_v1 {
 338        u32 valid;              /* (nonzero) valid, (0) log is empty */
 339        u32 error_id;           /* type of error */
 340        u32 pc;                 /* program counter */
 341        u32 blink1;             /* branch link */
 342        u32 blink2;             /* branch link */
 343        u32 ilink1;             /* interrupt link */
 344        u32 ilink2;             /* interrupt link */
 345        u32 data1;              /* error-specific data */
 346        u32 data2;              /* error-specific data */
 347        u32 data3;              /* error-specific data */
 348        u32 bcon_time;          /* beacon timer */
 349        u32 tsf_low;            /* network timestamp function timer */
 350        u32 tsf_hi;             /* network timestamp function timer */
 351        u32 gp1;                /* GP1 timer register */
 352        u32 gp2;                /* GP2 timer register */
 353        u32 gp3;                /* GP3 timer register */
 354        u32 ucode_ver;          /* uCode version */
 355        u32 hw_ver;             /* HW Silicon version */
 356        u32 brd_ver;            /* HW board version */
 357        u32 log_pc;             /* log program counter */
 358        u32 frame_ptr;          /* frame pointer */
 359        u32 stack_ptr;          /* stack pointer */
 360        u32 hcmd;               /* last host command header */
 361        u32 isr0;               /* isr status register LMPM_NIC_ISR0:
 362                                 * rxtx_flag */
 363        u32 isr1;               /* isr status register LMPM_NIC_ISR1:
 364                                 * host_flag */
 365        u32 isr2;               /* isr status register LMPM_NIC_ISR2:
 366                                 * enc_flag */
 367        u32 isr3;               /* isr status register LMPM_NIC_ISR3:
 368                                 * time_flag */
 369        u32 isr4;               /* isr status register LMPM_NIC_ISR4:
 370                                 * wico interrupt */
 371        u32 isr_pref;           /* isr status register LMPM_NIC_PREF_STAT */
 372        u32 wait_event;         /* wait event() caller address */
 373        u32 l2p_control;        /* L2pControlField */
 374        u32 l2p_duration;       /* L2pDurationField */
 375        u32 l2p_mhvalid;        /* L2pMhValidBits */
 376        u32 l2p_addr_match;     /* L2pAddrMatchStat */
 377        u32 lmpm_pmg_sel;       /* indicate which clocks are turned on
 378                                 * (LMPM_PMG_SEL) */
 379        u32 u_timestamp;        /* indicate when the date and time of the
 380                                 * compilation */
 381        u32 flow_handler;       /* FH read/write pointers, RX credit */
 382} __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
 383
 384struct iwl_error_event_table {
 385        u32 valid;              /* (nonzero) valid, (0) log is empty */
 386        u32 error_id;           /* type of error */
 387        u32 trm_hw_status0;     /* TRM HW status */
 388        u32 trm_hw_status1;     /* TRM HW status */
 389        u32 blink2;             /* branch link */
 390        u32 ilink1;             /* interrupt link */
 391        u32 ilink2;             /* interrupt link */
 392        u32 data1;              /* error-specific data */
 393        u32 data2;              /* error-specific data */
 394        u32 data3;              /* error-specific data */
 395        u32 bcon_time;          /* beacon timer */
 396        u32 tsf_low;            /* network timestamp function timer */
 397        u32 tsf_hi;             /* network timestamp function timer */
 398        u32 gp1;                /* GP1 timer register */
 399        u32 gp2;                /* GP2 timer register */
 400        u32 fw_rev_type;        /* firmware revision type */
 401        u32 major;              /* uCode version major */
 402        u32 minor;              /* uCode version minor */
 403        u32 hw_ver;             /* HW Silicon version */
 404        u32 brd_ver;            /* HW board version */
 405        u32 log_pc;             /* log program counter */
 406        u32 frame_ptr;          /* frame pointer */
 407        u32 stack_ptr;          /* stack pointer */
 408        u32 hcmd;               /* last host command header */
 409        u32 isr0;               /* isr status register LMPM_NIC_ISR0:
 410                                 * rxtx_flag */
 411        u32 isr1;               /* isr status register LMPM_NIC_ISR1:
 412                                 * host_flag */
 413        u32 isr2;               /* isr status register LMPM_NIC_ISR2:
 414                                 * enc_flag */
 415        u32 isr3;               /* isr status register LMPM_NIC_ISR3:
 416                                 * time_flag */
 417        u32 isr4;               /* isr status register LMPM_NIC_ISR4:
 418                                 * wico interrupt */
 419        u32 last_cmd_id;        /* last HCMD id handled by the firmware */
 420        u32 wait_event;         /* wait event() caller address */
 421        u32 l2p_control;        /* L2pControlField */
 422        u32 l2p_duration;       /* L2pDurationField */
 423        u32 l2p_mhvalid;        /* L2pMhValidBits */
 424        u32 l2p_addr_match;     /* L2pAddrMatchStat */
 425        u32 lmpm_pmg_sel;       /* indicate which clocks are turned on
 426                                 * (LMPM_PMG_SEL) */
 427        u32 u_timestamp;        /* indicate when the date and time of the
 428                                 * compilation */
 429        u32 flow_handler;       /* FH read/write pointers, RX credit */
 430} __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
 431
 432/*
 433 * UMAC error struct - relevant starting from family 8000 chip.
 434 * Note: This structure is read from the device with IO accesses,
 435 * and the reading already does the endian conversion. As it is
 436 * read with u32-sized accesses, any members with a different size
 437 * need to be ordered correctly though!
 438 */
 439struct iwl_umac_error_event_table {
 440        u32 valid;              /* (nonzero) valid, (0) log is empty */
 441        u32 error_id;           /* type of error */
 442        u32 blink1;             /* branch link */
 443        u32 blink2;             /* branch link */
 444        u32 ilink1;             /* interrupt link */
 445        u32 ilink2;             /* interrupt link */
 446        u32 data1;              /* error-specific data */
 447        u32 data2;              /* error-specific data */
 448        u32 data3;              /* error-specific data */
 449        u32 umac_major;
 450        u32 umac_minor;
 451        u32 frame_pointer;      /* core register 27*/
 452        u32 stack_pointer;      /* core register 28 */
 453        u32 cmd_header;         /* latest host cmd sent to UMAC */
 454        u32 nic_isr_pref;       /* ISR status register */
 455} __packed;
 456
 457#define ERROR_START_OFFSET  (1 * sizeof(u32))
 458#define ERROR_ELEM_SIZE     (7 * sizeof(u32))
 459
 460static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
 461{
 462        struct iwl_trans *trans = mvm->trans;
 463        struct iwl_umac_error_event_table table;
 464        u32 base = mvm->trans->dbg.umac_error_event_table;
 465
 466        if (!mvm->support_umac_log &&
 467            !(mvm->trans->dbg.error_event_table_tlv_status &
 468              IWL_ERROR_EVENT_TABLE_UMAC))
 469                return;
 470
 471        iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
 472
 473        if (table.valid)
 474                mvm->fwrt.dump.umac_err_id = table.error_id;
 475
 476        if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
 477                IWL_ERR(trans, "Start IWL Error Log Dump:\n");
 478                IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
 479                        mvm->status, table.valid);
 480        }
 481
 482        IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
 483                desc_lookup(table.error_id));
 484        IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
 485        IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
 486        IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
 487        IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
 488        IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
 489        IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
 490        IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
 491        IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
 492        IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
 493        IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
 494        IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
 495        IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
 496        IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
 497}
 498
 499static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u8 lmac_num)
 500{
 501        struct iwl_trans *trans = mvm->trans;
 502        struct iwl_error_event_table table;
 503        u32 val, base = mvm->trans->dbg.lmac_error_event_table[lmac_num];
 504
 505        if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
 506                if (!base)
 507                        base = mvm->fw->init_errlog_ptr;
 508        } else {
 509                if (!base)
 510                        base = mvm->fw->inst_errlog_ptr;
 511        }
 512
 513        if (base < 0x400000) {
 514                IWL_ERR(mvm,
 515                        "Not valid error log pointer 0x%08X for %s uCode\n",
 516                        base,
 517                        (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
 518                        ? "Init" : "RT");
 519                return;
 520        }
 521
 522        /* check if there is a HW error */
 523        val = iwl_trans_read_mem32(trans, base);
 524        if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
 525                int err;
 526
 527                IWL_ERR(trans, "HW error, resetting before reading\n");
 528
 529                /* reset the device */
 530                iwl_trans_sw_reset(trans);
 531
 532                err = iwl_finish_nic_init(trans, trans->trans_cfg);
 533                if (err)
 534                        return;
 535        }
 536
 537        iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
 538
 539        if (table.valid)
 540                mvm->fwrt.dump.lmac_err_id[lmac_num] = table.error_id;
 541
 542        if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
 543                IWL_ERR(trans, "Start IWL Error Log Dump:\n");
 544                IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
 545                        mvm->status, table.valid);
 546        }
 547
 548        /* Do not change this output - scripts rely on it */
 549
 550        IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
 551
 552        IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
 553                desc_lookup(table.error_id));
 554        IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
 555        IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
 556        IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
 557        IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
 558        IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
 559        IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
 560        IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
 561        IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
 562        IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
 563        IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
 564        IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
 565        IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
 566        IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
 567        IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
 568        IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
 569        IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
 570        IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
 571        IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
 572        IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
 573        IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
 574        IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
 575        IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
 576        IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
 577        IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
 578        IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
 579        IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
 580        IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
 581        IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
 582        IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
 583        IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
 584        IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
 585        IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
 586        IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
 587}
 588
 589static void iwl_mvm_dump_iml_error_log(struct iwl_mvm *mvm)
 590{
 591        struct iwl_trans *trans = mvm->trans;
 592        u32 error;
 593
 594        error = iwl_read_umac_prph(trans, UMAG_SB_CPU_2_STATUS);
 595
 596        IWL_ERR(trans, "IML/ROM dump:\n");
 597
 598        if (error & 0xFFFF0000)
 599                IWL_ERR(trans, "IML/ROM SYSASSERT:\n");
 600
 601        IWL_ERR(mvm, "0x%08X | IML/ROM error/state\n", error);
 602        IWL_ERR(mvm, "0x%08X | IML/ROM data1\n",
 603                iwl_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS));
 604}
 605
 606void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
 607{
 608        if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
 609                IWL_ERR(mvm,
 610                        "DEVICE_ENABLED bit is not set. Aborting dump.\n");
 611                return;
 612        }
 613
 614        iwl_mvm_dump_lmac_error_log(mvm, 0);
 615
 616        if (mvm->trans->dbg.lmac_error_event_table[1])
 617                iwl_mvm_dump_lmac_error_log(mvm, 1);
 618
 619        iwl_mvm_dump_umac_error_log(mvm);
 620
 621        if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
 622                iwl_mvm_dump_iml_error_log(mvm);
 623
 624        iwl_fw_error_print_fseq_regs(&mvm->fwrt);
 625}
 626
 627int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
 628                         int tid, int frame_limit, u16 ssn)
 629{
 630        struct iwl_scd_txq_cfg_cmd cmd = {
 631                .scd_queue = queue,
 632                .action = SCD_CFG_ENABLE_QUEUE,
 633                .window = frame_limit,
 634                .sta_id = sta_id,
 635                .ssn = cpu_to_le16(ssn),
 636                .tx_fifo = fifo,
 637                .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
 638                              queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
 639                .tid = tid,
 640        };
 641        int ret;
 642
 643        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 644                return -EINVAL;
 645
 646        if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
 647                 "Trying to reconfig unallocated queue %d\n", queue))
 648                return -ENXIO;
 649
 650        IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
 651
 652        ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
 653        WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
 654                  queue, fifo, ret);
 655
 656        return ret;
 657}
 658
 659/**
 660 * iwl_mvm_send_lq_cmd() - Send link quality command
 661 * @sync: This command can be sent synchronously.
 662 *
 663 * The link quality command is sent as the last step of station creation.
 664 * This is the special case in which init is set and we call a callback in
 665 * this case to clear the state indicating that station creation is in
 666 * progress.
 667 */
 668int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
 669{
 670        struct iwl_host_cmd cmd = {
 671                .id = LQ_CMD,
 672                .len = { sizeof(struct iwl_lq_cmd), },
 673                .flags = CMD_ASYNC,
 674                .data = { lq, },
 675        };
 676
 677        if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
 678                    iwl_mvm_has_tlc_offload(mvm)))
 679                return -EINVAL;
 680
 681        return iwl_mvm_send_cmd(mvm, &cmd);
 682}
 683
 684/**
 685 * iwl_mvm_update_smps - Get a request to change the SMPS mode
 686 * @req_type: The part of the driver who call for a change.
 687 * @smps_requests: The request to change the SMPS mode.
 688 *
 689 * Get a requst to change the SMPS mode,
 690 * and change it according to all other requests in the driver.
 691 */
 692void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 693                         enum iwl_mvm_smps_type_request req_type,
 694                         enum ieee80211_smps_mode smps_request)
 695{
 696        struct iwl_mvm_vif *mvmvif;
 697        enum ieee80211_smps_mode smps_mode;
 698        int i;
 699
 700        lockdep_assert_held(&mvm->mutex);
 701
 702        /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
 703        if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
 704                return;
 705
 706        if (vif->type == NL80211_IFTYPE_AP)
 707                smps_mode = IEEE80211_SMPS_OFF;
 708        else
 709                smps_mode = IEEE80211_SMPS_AUTOMATIC;
 710
 711        mvmvif = iwl_mvm_vif_from_mac80211(vif);
 712        mvmvif->smps_requests[req_type] = smps_request;
 713        for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
 714                if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
 715                        smps_mode = IEEE80211_SMPS_STATIC;
 716                        break;
 717                }
 718                if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
 719                        smps_mode = IEEE80211_SMPS_DYNAMIC;
 720        }
 721
 722        ieee80211_request_smps(vif, smps_mode);
 723}
 724
 725int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
 726{
 727        struct iwl_statistics_cmd scmd = {
 728                .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
 729        };
 730        struct iwl_host_cmd cmd = {
 731                .id = STATISTICS_CMD,
 732                .len[0] = sizeof(scmd),
 733                .data[0] = &scmd,
 734                .flags = CMD_WANT_SKB,
 735        };
 736        int ret;
 737
 738        ret = iwl_mvm_send_cmd(mvm, &cmd);
 739        if (ret)
 740                return ret;
 741
 742        iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
 743        iwl_free_resp(&cmd);
 744
 745        if (clear)
 746                iwl_mvm_accu_radio_stats(mvm);
 747
 748        return 0;
 749}
 750
 751void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
 752{
 753        mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
 754        mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
 755        mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
 756        mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
 757}
 758
 759static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
 760                                   struct ieee80211_vif *vif)
 761{
 762        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 763        bool *result = _data;
 764        int i;
 765
 766        for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
 767                if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
 768                    mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
 769                        *result = false;
 770        }
 771}
 772
 773bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
 774{
 775        bool result = true;
 776
 777        lockdep_assert_held(&mvm->mutex);
 778
 779        if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
 780                return false;
 781
 782        if (mvm->cfg->rx_with_siso_diversity)
 783                return false;
 784
 785        ieee80211_iterate_active_interfaces_atomic(
 786                        mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
 787                        iwl_mvm_diversity_iter, &result);
 788
 789        return result;
 790}
 791
 792void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
 793                                  bool low_latency, u16 mac_id)
 794{
 795        struct iwl_mac_low_latency_cmd cmd = {
 796                .mac_id = cpu_to_le32(mac_id)
 797        };
 798
 799        if (!fw_has_capa(&mvm->fw->ucode_capa,
 800                         IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
 801                return;
 802
 803        if (low_latency) {
 804                /* currently we don't care about the direction */
 805                cmd.low_latency_rx = 1;
 806                cmd.low_latency_tx = 1;
 807        }
 808
 809        if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(LOW_LATENCY_CMD,
 810                                                 MAC_CONF_GROUP, 0),
 811                                 0, sizeof(cmd), &cmd))
 812                IWL_ERR(mvm, "Failed to send low latency command\n");
 813}
 814
 815int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 816                               bool low_latency,
 817                               enum iwl_mvm_low_latency_cause cause)
 818{
 819        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 820        int res;
 821        bool prev;
 822
 823        lockdep_assert_held(&mvm->mutex);
 824
 825        prev = iwl_mvm_vif_low_latency(mvmvif);
 826        iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
 827
 828        low_latency = iwl_mvm_vif_low_latency(mvmvif);
 829
 830        if (low_latency == prev)
 831                return 0;
 832
 833        iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
 834
 835        res = iwl_mvm_update_quotas(mvm, false, NULL);
 836        if (res)
 837                return res;
 838
 839        iwl_mvm_bt_coex_vif_change(mvm);
 840
 841        return iwl_mvm_power_update_mac(mvm);
 842}
 843
 844struct iwl_mvm_low_latency_iter {
 845        bool result;
 846        bool result_per_band[NUM_NL80211_BANDS];
 847};
 848
 849static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
 850{
 851        struct iwl_mvm_low_latency_iter *result = _data;
 852        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 853        enum nl80211_band band;
 854
 855        if (iwl_mvm_vif_low_latency(mvmvif)) {
 856                result->result = true;
 857
 858                if (!mvmvif->phy_ctxt)
 859                        return;
 860
 861                band = mvmvif->phy_ctxt->channel->band;
 862                result->result_per_band[band] = true;
 863        }
 864}
 865
 866bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
 867{
 868        struct iwl_mvm_low_latency_iter data = {};
 869
 870        ieee80211_iterate_active_interfaces_atomic(
 871                        mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
 872                        iwl_mvm_ll_iter, &data);
 873
 874        return data.result;
 875}
 876
 877bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
 878{
 879        struct iwl_mvm_low_latency_iter data = {};
 880
 881        ieee80211_iterate_active_interfaces_atomic(
 882                        mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
 883                        iwl_mvm_ll_iter, &data);
 884
 885        return data.result_per_band[band];
 886}
 887
 888struct iwl_bss_iter_data {
 889        struct ieee80211_vif *vif;
 890        bool error;
 891};
 892
 893static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
 894                                       struct ieee80211_vif *vif)
 895{
 896        struct iwl_bss_iter_data *data = _data;
 897
 898        if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
 899                return;
 900
 901        if (data->vif) {
 902                data->error = true;
 903                return;
 904        }
 905
 906        data->vif = vif;
 907}
 908
 909struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
 910{
 911        struct iwl_bss_iter_data bss_iter_data = {};
 912
 913        ieee80211_iterate_active_interfaces_atomic(
 914                mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
 915                iwl_mvm_bss_iface_iterator, &bss_iter_data);
 916
 917        if (bss_iter_data.error) {
 918                IWL_ERR(mvm, "More than one managed interface active!\n");
 919                return ERR_PTR(-EINVAL);
 920        }
 921
 922        return bss_iter_data.vif;
 923}
 924
 925struct iwl_sta_iter_data {
 926        bool assoc;
 927};
 928
 929static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
 930                                       struct ieee80211_vif *vif)
 931{
 932        struct iwl_sta_iter_data *data = _data;
 933
 934        if (vif->type != NL80211_IFTYPE_STATION)
 935                return;
 936
 937        if (vif->bss_conf.assoc)
 938                data->assoc = true;
 939}
 940
 941bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
 942{
 943        struct iwl_sta_iter_data data = {
 944                .assoc = false,
 945        };
 946
 947        ieee80211_iterate_active_interfaces_atomic(mvm->hw,
 948                                                   IEEE80211_IFACE_ITER_NORMAL,
 949                                                   iwl_mvm_sta_iface_iterator,
 950                                                   &data);
 951        return data.assoc;
 952}
 953
 954unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
 955                                    struct ieee80211_vif *vif,
 956                                    bool tdls, bool cmd_q)
 957{
 958        struct iwl_fw_dbg_trigger_tlv *trigger;
 959        struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
 960        unsigned int default_timeout = cmd_q ?
 961                IWL_DEF_WD_TIMEOUT :
 962                mvm->trans->trans_cfg->base_params->wd_timeout;
 963
 964        if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
 965                /*
 966                 * We can't know when the station is asleep or awake, so we
 967                 * must disable the queue hang detection.
 968                 */
 969                if (fw_has_capa(&mvm->fw->ucode_capa,
 970                                IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
 971                    vif && vif->type == NL80211_IFTYPE_AP)
 972                        return IWL_WATCHDOG_DISABLED;
 973                return default_timeout;
 974        }
 975
 976        trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
 977        txq_timer = (void *)trigger->data;
 978
 979        if (tdls)
 980                return le32_to_cpu(txq_timer->tdls);
 981
 982        if (cmd_q)
 983                return le32_to_cpu(txq_timer->command_queue);
 984
 985        if (WARN_ON(!vif))
 986                return default_timeout;
 987
 988        switch (ieee80211_vif_type_p2p(vif)) {
 989        case NL80211_IFTYPE_ADHOC:
 990                return le32_to_cpu(txq_timer->ibss);
 991        case NL80211_IFTYPE_STATION:
 992                return le32_to_cpu(txq_timer->bss);
 993        case NL80211_IFTYPE_AP:
 994                return le32_to_cpu(txq_timer->softap);
 995        case NL80211_IFTYPE_P2P_CLIENT:
 996                return le32_to_cpu(txq_timer->p2p_client);
 997        case NL80211_IFTYPE_P2P_GO:
 998                return le32_to_cpu(txq_timer->p2p_go);
 999        case NL80211_IFTYPE_P2P_DEVICE:
1000                return le32_to_cpu(txq_timer->p2p_device);
1001        case NL80211_IFTYPE_MONITOR:
1002                return default_timeout;
1003        default:
1004                WARN_ON(1);
1005                return mvm->trans->trans_cfg->base_params->wd_timeout;
1006        }
1007}
1008
1009void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1010                             const char *errmsg)
1011{
1012        struct iwl_fw_dbg_trigger_tlv *trig;
1013        struct iwl_fw_dbg_trigger_mlme *trig_mlme;
1014
1015        trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1016                                     FW_DBG_TRIGGER_MLME);
1017        if (!trig)
1018                goto out;
1019
1020        trig_mlme = (void *)trig->data;
1021
1022        if (trig_mlme->stop_connection_loss &&
1023            --trig_mlme->stop_connection_loss)
1024                goto out;
1025
1026        iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
1027
1028out:
1029        ieee80211_connection_loss(vif);
1030}
1031
1032void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
1033                                          struct ieee80211_vif *vif,
1034                                          const struct ieee80211_sta *sta,
1035                                          u16 tid)
1036{
1037        struct iwl_fw_dbg_trigger_tlv *trig;
1038        struct iwl_fw_dbg_trigger_ba *ba_trig;
1039
1040        trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1041                                     FW_DBG_TRIGGER_BA);
1042        if (!trig)
1043                return;
1044
1045        ba_trig = (void *)trig->data;
1046
1047        if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
1048                return;
1049
1050        iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1051                                "Frame from %pM timed out, tid %d",
1052                                sta->addr, tid);
1053}
1054
1055u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
1056{
1057        if (!elapsed)
1058                return 0;
1059
1060        return (100 * airtime / elapsed) / USEC_PER_MSEC;
1061}
1062
1063static enum iwl_mvm_traffic_load
1064iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
1065{
1066        u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
1067
1068        if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
1069                return IWL_MVM_TRAFFIC_HIGH;
1070        if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
1071                return IWL_MVM_TRAFFIC_MEDIUM;
1072
1073        return IWL_MVM_TRAFFIC_LOW;
1074}
1075
1076struct iwl_mvm_tcm_iter_data {
1077        struct iwl_mvm *mvm;
1078        bool any_sent;
1079};
1080
1081static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
1082{
1083        struct iwl_mvm_tcm_iter_data *data = _data;
1084        struct iwl_mvm *mvm = data->mvm;
1085        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1086        bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
1087
1088        if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
1089                return;
1090
1091        low_latency = mvm->tcm.result.low_latency[mvmvif->id];
1092
1093        if (!mvm->tcm.result.change[mvmvif->id] &&
1094            prev == low_latency) {
1095                iwl_mvm_update_quotas(mvm, false, NULL);
1096                return;
1097        }
1098
1099        if (prev != low_latency) {
1100                /* this sends traffic load and updates quota as well */
1101                iwl_mvm_update_low_latency(mvm, vif, low_latency,
1102                                           LOW_LATENCY_TRAFFIC);
1103        } else {
1104                iwl_mvm_update_quotas(mvm, false, NULL);
1105        }
1106
1107        data->any_sent = true;
1108}
1109
1110static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
1111{
1112        struct iwl_mvm_tcm_iter_data data = {
1113                .mvm = mvm,
1114                .any_sent = false,
1115        };
1116
1117        mutex_lock(&mvm->mutex);
1118
1119        ieee80211_iterate_active_interfaces(
1120                mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1121                iwl_mvm_tcm_iter, &data);
1122
1123        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1124                iwl_mvm_config_scan(mvm);
1125
1126        mutex_unlock(&mvm->mutex);
1127}
1128
1129static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
1130{
1131        struct iwl_mvm *mvm;
1132        struct iwl_mvm_vif *mvmvif;
1133        struct ieee80211_vif *vif;
1134
1135        mvmvif = container_of(wk, struct iwl_mvm_vif,
1136                              uapsd_nonagg_detected_wk.work);
1137        vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1138        mvm = mvmvif->mvm;
1139
1140        if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
1141                return;
1142
1143        /* remember that this AP is broken */
1144        memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
1145               vif->bss_conf.bssid, ETH_ALEN);
1146        mvm->uapsd_noagg_bssid_write_idx++;
1147        if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
1148                mvm->uapsd_noagg_bssid_write_idx = 0;
1149
1150        iwl_mvm_connection_loss(mvm, vif,
1151                                "AP isn't using AMPDU with uAPSD enabled");
1152}
1153
1154static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
1155                                         struct ieee80211_vif *vif)
1156{
1157        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1158
1159        if (vif->type != NL80211_IFTYPE_STATION)
1160                return;
1161
1162        if (!vif->bss_conf.assoc)
1163                return;
1164
1165        if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
1166            !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
1167            !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
1168            !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
1169                return;
1170
1171        if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
1172                return;
1173
1174        mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
1175        IWL_INFO(mvm,
1176                 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
1177        schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
1178}
1179
1180static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
1181                                                 unsigned int elapsed,
1182                                                 int mac)
1183{
1184        u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
1185        u64 tpt;
1186        unsigned long rate;
1187        struct ieee80211_vif *vif;
1188
1189        rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
1190
1191        if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
1192            mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
1193                return;
1194
1195        if (iwl_mvm_has_new_rx_api(mvm)) {
1196                tpt = 8 * bytes; /* kbps */
1197                do_div(tpt, elapsed);
1198                rate *= 1000; /* kbps */
1199                if (tpt < 22 * rate / 100)
1200                        return;
1201        } else {
1202                /*
1203                 * the rate here is actually the threshold, in 100Kbps units,
1204                 * so do the needed conversion from bytes to 100Kbps:
1205                 * 100kb = bits / (100 * 1000),
1206                 * 100kbps = 100kb / (msecs / 1000) ==
1207                 *           (bits / (100 * 1000)) / (msecs / 1000) ==
1208                 *           bits / (100 * msecs)
1209                 */
1210                tpt = (8 * bytes);
1211                do_div(tpt, elapsed * 100);
1212                if (tpt < rate)
1213                        return;
1214        }
1215
1216        rcu_read_lock();
1217        vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
1218        if (vif)
1219                iwl_mvm_uapsd_agg_disconnect(mvm, vif);
1220        rcu_read_unlock();
1221}
1222
1223static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
1224                                 struct ieee80211_vif *vif)
1225{
1226        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1227        u32 *band = _data;
1228
1229        if (!mvmvif->phy_ctxt)
1230                return;
1231
1232        band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
1233}
1234
1235static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
1236                                            unsigned long ts,
1237                                            bool handle_uapsd)
1238{
1239        unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
1240        unsigned int uapsd_elapsed =
1241                jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
1242        u32 total_airtime = 0;
1243        u32 band_airtime[NUM_NL80211_BANDS] = {0};
1244        u32 band[NUM_MAC_INDEX_DRIVER] = {0};
1245        int ac, mac, i;
1246        bool low_latency = false;
1247        enum iwl_mvm_traffic_load load, band_load;
1248        bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
1249
1250        if (handle_ll)
1251                mvm->tcm.ll_ts = ts;
1252        if (handle_uapsd)
1253                mvm->tcm.uapsd_nonagg_ts = ts;
1254
1255        mvm->tcm.result.elapsed = elapsed;
1256
1257        ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1258                                                   IEEE80211_IFACE_ITER_NORMAL,
1259                                                   iwl_mvm_tcm_iterator,
1260                                                   &band);
1261
1262        for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1263                struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1264                u32 vo_vi_pkts = 0;
1265                u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1266
1267                total_airtime += airtime;
1268                band_airtime[band[mac]] += airtime;
1269
1270                load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1271                mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1272                mvm->tcm.result.load[mac] = load;
1273                mvm->tcm.result.airtime[mac] = airtime;
1274
1275                for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1276                        vo_vi_pkts += mdata->rx.pkts[ac] +
1277                                      mdata->tx.pkts[ac];
1278
1279                /* enable immediately with enough packets but defer disabling */
1280                if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1281                        mvm->tcm.result.low_latency[mac] = true;
1282                else if (handle_ll)
1283                        mvm->tcm.result.low_latency[mac] = false;
1284
1285                if (handle_ll) {
1286                        /* clear old data */
1287                        memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1288                        memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1289                }
1290                low_latency |= mvm->tcm.result.low_latency[mac];
1291
1292                if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1293                        iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1294                                                             mac);
1295                /* clear old data */
1296                if (handle_uapsd)
1297                        mdata->uapsd_nonagg_detect.rx_bytes = 0;
1298                memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1299                memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1300        }
1301
1302        load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1303        mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
1304        mvm->tcm.result.global_load = load;
1305
1306        for (i = 0; i < NUM_NL80211_BANDS; i++) {
1307                band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1308                mvm->tcm.result.band_load[i] = band_load;
1309        }
1310
1311        /*
1312         * If the current load isn't low we need to force re-evaluation
1313         * in the TCM period, so that we can return to low load if there
1314         * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1315         * triggered by traffic).
1316         */
1317        if (load != IWL_MVM_TRAFFIC_LOW)
1318                return MVM_TCM_PERIOD;
1319        /*
1320         * If low-latency is active we need to force re-evaluation after
1321         * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1322         * when there's no traffic at all.
1323         */
1324        if (low_latency)
1325                return MVM_LL_PERIOD;
1326        /*
1327         * Otherwise, we don't need to run the work struct because we're
1328         * in the default "idle" state - traffic indication is low (which
1329         * also covers the "no traffic" case) and low-latency is disabled
1330         * so there's no state that may need to be disabled when there's
1331         * no traffic at all.
1332         *
1333         * Note that this has no impact on the regular scheduling of the
1334         * updates triggered by traffic - those happen whenever one of the
1335         * two timeouts expire (if there's traffic at all.)
1336         */
1337        return 0;
1338}
1339
1340void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1341{
1342        unsigned long ts = jiffies;
1343        bool handle_uapsd =
1344                time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1345                               msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1346
1347        spin_lock(&mvm->tcm.lock);
1348        if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1349                spin_unlock(&mvm->tcm.lock);
1350                return;
1351        }
1352        spin_unlock(&mvm->tcm.lock);
1353
1354        if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1355                mutex_lock(&mvm->mutex);
1356                if (iwl_mvm_request_statistics(mvm, true))
1357                        handle_uapsd = false;
1358                mutex_unlock(&mvm->mutex);
1359        }
1360
1361        spin_lock(&mvm->tcm.lock);
1362        /* re-check if somebody else won the recheck race */
1363        if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1364                /* calculate statistics */
1365                unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1366                                                                  handle_uapsd);
1367
1368                /* the memset needs to be visible before the timestamp */
1369                smp_mb();
1370                mvm->tcm.ts = ts;
1371                if (work_delay)
1372                        schedule_delayed_work(&mvm->tcm.work, work_delay);
1373        }
1374        spin_unlock(&mvm->tcm.lock);
1375
1376        iwl_mvm_tcm_results(mvm);
1377}
1378
1379void iwl_mvm_tcm_work(struct work_struct *work)
1380{
1381        struct delayed_work *delayed_work = to_delayed_work(work);
1382        struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1383                                           tcm.work);
1384
1385        iwl_mvm_recalc_tcm(mvm);
1386}
1387
1388void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1389{
1390        spin_lock_bh(&mvm->tcm.lock);
1391        mvm->tcm.paused = true;
1392        spin_unlock_bh(&mvm->tcm.lock);
1393        if (with_cancel)
1394                cancel_delayed_work_sync(&mvm->tcm.work);
1395}
1396
1397void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1398{
1399        int mac;
1400        bool low_latency = false;
1401
1402        spin_lock_bh(&mvm->tcm.lock);
1403        mvm->tcm.ts = jiffies;
1404        mvm->tcm.ll_ts = jiffies;
1405        for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1406                struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1407
1408                memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1409                memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1410                memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1411                memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1412
1413                if (mvm->tcm.result.low_latency[mac])
1414                        low_latency = true;
1415        }
1416        /* The TCM data needs to be reset before "paused" flag changes */
1417        smp_mb();
1418        mvm->tcm.paused = false;
1419
1420        /*
1421         * if the current load is not low or low latency is active, force
1422         * re-evaluation to cover the case of no traffic.
1423         */
1424        if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1425                schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1426        else if (low_latency)
1427                schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1428
1429        spin_unlock_bh(&mvm->tcm.lock);
1430}
1431
1432void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1433{
1434        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1435
1436        INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1437                          iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1438}
1439
1440void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1441{
1442        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1443
1444        cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1445}
1446
1447u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1448{
1449        u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1450
1451        if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1452            mvm->trans->cfg->gp2_reg_addr)
1453                reg_addr = mvm->trans->cfg->gp2_reg_addr;
1454
1455        return iwl_read_prph(mvm->trans, reg_addr);
1456}
1457
1458void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
1459{
1460        bool ps_disabled;
1461
1462        lockdep_assert_held(&mvm->mutex);
1463
1464        /* Disable power save when reading GP2 */
1465        ps_disabled = mvm->ps_disabled;
1466        if (!ps_disabled) {
1467                mvm->ps_disabled = true;
1468                iwl_mvm_power_update_device(mvm);
1469        }
1470
1471        *gp2 = iwl_mvm_get_systime(mvm);
1472        *boottime = ktime_get_boottime_ns();
1473
1474        if (!ps_disabled) {
1475                mvm->ps_disabled = ps_disabled;
1476                iwl_mvm_power_update_device(mvm);
1477        }
1478}
1479