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