linux/drivers/net/wireless/intel/iwlwifi/mvm/fw.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 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10 * Copyright(c) 2016 - 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 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24 * USA
  25 *
  26 * The full GNU General Public License is included in this distribution
  27 * in the file called COPYING.
  28 *
  29 * Contact Information:
  30 *  Intel Linux Wireless <linuxwifi@intel.com>
  31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32 *
  33 * BSD LICENSE
  34 *
  35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38 * All rights reserved.
  39 *
  40 * Redistribution and use in source and binary forms, with or without
  41 * modification, are permitted provided that the following conditions
  42 * are met:
  43 *
  44 *  * Redistributions of source code must retain the above copyright
  45 *    notice, this list of conditions and the following disclaimer.
  46 *  * Redistributions in binary form must reproduce the above copyright
  47 *    notice, this list of conditions and the following disclaimer in
  48 *    the documentation and/or other materials provided with the
  49 *    distribution.
  50 *  * Neither the name Intel Corporation nor the names of its
  51 *    contributors may be used to endorse or promote products derived
  52 *    from this software without specific prior written permission.
  53 *
  54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65 *
  66 *****************************************************************************/
  67#include <net/mac80211.h>
  68#include <linux/netdevice.h>
  69#include <linux/acpi.h>
  70
  71#include "iwl-trans.h"
  72#include "iwl-op-mode.h"
  73#include "fw/img.h"
  74#include "iwl-debug.h"
  75#include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
  76#include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
  77#include "iwl-prph.h"
  78#include "iwl-eeprom-parse.h"
  79
  80#include "mvm.h"
  81#include "fw/dbg.h"
  82#include "iwl-phy-db.h"
  83
  84#define MVM_UCODE_ALIVE_TIMEOUT HZ
  85#define MVM_UCODE_CALIB_TIMEOUT (2*HZ)
  86
  87#define UCODE_VALID_OK  cpu_to_le32(0x1)
  88
  89struct iwl_mvm_alive_data {
  90        bool valid;
  91        u32 scd_base_addr;
  92};
  93
  94static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
  95{
  96        struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
  97                .valid = cpu_to_le32(valid_tx_ant),
  98        };
  99
 100        IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
 101        return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, 0,
 102                                    sizeof(tx_ant_cmd), &tx_ant_cmd);
 103}
 104
 105static int iwl_send_rss_cfg_cmd(struct iwl_mvm *mvm)
 106{
 107        int i;
 108        struct iwl_rss_config_cmd cmd = {
 109                .flags = cpu_to_le32(IWL_RSS_ENABLE),
 110                .hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
 111                             IWL_RSS_HASH_TYPE_IPV4_UDP |
 112                             IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
 113                             IWL_RSS_HASH_TYPE_IPV6_TCP |
 114                             IWL_RSS_HASH_TYPE_IPV6_UDP |
 115                             IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
 116        };
 117
 118        if (mvm->trans->num_rx_queues == 1)
 119                return 0;
 120
 121        /* Do not direct RSS traffic to Q 0 which is our fallback queue */
 122        for (i = 0; i < ARRAY_SIZE(cmd.indirection_table); i++)
 123                cmd.indirection_table[i] =
 124                        1 + (i % (mvm->trans->num_rx_queues - 1));
 125        netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
 126
 127        return iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0, sizeof(cmd), &cmd);
 128}
 129
 130static int iwl_mvm_send_dqa_cmd(struct iwl_mvm *mvm)
 131{
 132        struct iwl_dqa_enable_cmd dqa_cmd = {
 133                .cmd_queue = cpu_to_le32(IWL_MVM_DQA_CMD_QUEUE),
 134        };
 135        u32 cmd_id = iwl_cmd_id(DQA_ENABLE_CMD, DATA_PATH_GROUP, 0);
 136        int ret;
 137
 138        ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(dqa_cmd), &dqa_cmd);
 139        if (ret)
 140                IWL_ERR(mvm, "Failed to send DQA enabling command: %d\n", ret);
 141        else
 142                IWL_DEBUG_FW(mvm, "Working in DQA mode\n");
 143
 144        return ret;
 145}
 146
 147void iwl_mvm_mfu_assert_dump_notif(struct iwl_mvm *mvm,
 148                                   struct iwl_rx_cmd_buffer *rxb)
 149{
 150        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 151        struct iwl_mfu_assert_dump_notif *mfu_dump_notif = (void *)pkt->data;
 152        __le32 *dump_data = mfu_dump_notif->data;
 153        int n_words = le32_to_cpu(mfu_dump_notif->data_size) / sizeof(__le32);
 154        int i;
 155
 156        if (mfu_dump_notif->index_num == 0)
 157                IWL_INFO(mvm, "MFUART assert id 0x%x occurred\n",
 158                         le32_to_cpu(mfu_dump_notif->assert_id));
 159
 160        for (i = 0; i < n_words; i++)
 161                IWL_DEBUG_INFO(mvm,
 162                               "MFUART assert dump, dword %u: 0x%08x\n",
 163                               le16_to_cpu(mfu_dump_notif->index_num) *
 164                               n_words + i,
 165                               le32_to_cpu(dump_data[i]));
 166}
 167
 168static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
 169                         struct iwl_rx_packet *pkt, void *data)
 170{
 171        struct iwl_mvm *mvm =
 172                container_of(notif_wait, struct iwl_mvm, notif_wait);
 173        struct iwl_mvm_alive_data *alive_data = data;
 174        struct mvm_alive_resp_v3 *palive3;
 175        struct mvm_alive_resp *palive;
 176        struct iwl_umac_alive *umac;
 177        struct iwl_lmac_alive *lmac1;
 178        struct iwl_lmac_alive *lmac2 = NULL;
 179        u16 status;
 180
 181        if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive)) {
 182                palive = (void *)pkt->data;
 183                umac = &palive->umac_data;
 184                lmac1 = &palive->lmac_data[0];
 185                lmac2 = &palive->lmac_data[1];
 186                status = le16_to_cpu(palive->status);
 187        } else {
 188                palive3 = (void *)pkt->data;
 189                umac = &palive3->umac_data;
 190                lmac1 = &palive3->lmac_data;
 191                status = le16_to_cpu(palive3->status);
 192        }
 193
 194        mvm->error_event_table[0] = le32_to_cpu(lmac1->error_event_table_ptr);
 195        if (lmac2)
 196                mvm->error_event_table[1] =
 197                        le32_to_cpu(lmac2->error_event_table_ptr);
 198        mvm->log_event_table = le32_to_cpu(lmac1->log_event_table_ptr);
 199        mvm->sf_space.addr = le32_to_cpu(lmac1->st_fwrd_addr);
 200        mvm->sf_space.size = le32_to_cpu(lmac1->st_fwrd_size);
 201
 202        mvm->umac_error_event_table = le32_to_cpu(umac->error_info_addr);
 203
 204        alive_data->scd_base_addr = le32_to_cpu(lmac1->scd_base_ptr);
 205        alive_data->valid = status == IWL_ALIVE_STATUS_OK;
 206        if (mvm->umac_error_event_table)
 207                mvm->support_umac_log = true;
 208
 209        IWL_DEBUG_FW(mvm,
 210                     "Alive ucode status 0x%04x revision 0x%01X 0x%01X\n",
 211                     status, lmac1->ver_type, lmac1->ver_subtype);
 212
 213        if (lmac2)
 214                IWL_DEBUG_FW(mvm, "Alive ucode CDB\n");
 215
 216        IWL_DEBUG_FW(mvm,
 217                     "UMAC version: Major - 0x%x, Minor - 0x%x\n",
 218                     le32_to_cpu(umac->umac_major),
 219                     le32_to_cpu(umac->umac_minor));
 220
 221        return true;
 222}
 223
 224static bool iwl_wait_init_complete(struct iwl_notif_wait_data *notif_wait,
 225                                   struct iwl_rx_packet *pkt, void *data)
 226{
 227        WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
 228
 229        return true;
 230}
 231
 232static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
 233                                  struct iwl_rx_packet *pkt, void *data)
 234{
 235        struct iwl_phy_db *phy_db = data;
 236
 237        if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
 238                WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
 239                return true;
 240        }
 241
 242        WARN_ON(iwl_phy_db_set_section(phy_db, pkt));
 243
 244        return false;
 245}
 246
 247static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
 248                                         enum iwl_ucode_type ucode_type)
 249{
 250        struct iwl_notification_wait alive_wait;
 251        struct iwl_mvm_alive_data alive_data;
 252        const struct fw_img *fw;
 253        int ret, i;
 254        enum iwl_ucode_type old_type = mvm->fwrt.cur_fw_img;
 255        static const u16 alive_cmd[] = { MVM_ALIVE };
 256        struct iwl_sf_region st_fwrd_space;
 257
 258        if (ucode_type == IWL_UCODE_REGULAR &&
 259            iwl_fw_dbg_conf_usniffer(mvm->fw, FW_DBG_START_FROM_ALIVE) &&
 260            !(fw_has_capa(&mvm->fw->ucode_capa,
 261                          IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED)))
 262                fw = iwl_get_ucode_image(mvm->fw, IWL_UCODE_REGULAR_USNIFFER);
 263        else
 264                fw = iwl_get_ucode_image(mvm->fw, ucode_type);
 265        if (WARN_ON(!fw))
 266                return -EINVAL;
 267        iwl_fw_set_current_image(&mvm->fwrt, ucode_type);
 268        clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
 269
 270        iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
 271                                   alive_cmd, ARRAY_SIZE(alive_cmd),
 272                                   iwl_alive_fn, &alive_data);
 273
 274        ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
 275        if (ret) {
 276                iwl_fw_set_current_image(&mvm->fwrt, old_type);
 277                iwl_remove_notification(&mvm->notif_wait, &alive_wait);
 278                return ret;
 279        }
 280
 281        /*
 282         * Some things may run in the background now, but we
 283         * just wait for the ALIVE notification here.
 284         */
 285        ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
 286                                    MVM_UCODE_ALIVE_TIMEOUT);
 287        if (ret) {
 288                struct iwl_trans *trans = mvm->trans;
 289
 290                if (trans->cfg->device_family == IWL_DEVICE_FAMILY_A000)
 291                        IWL_ERR(mvm,
 292                                "SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
 293                                iwl_read_prph(trans, UMAG_SB_CPU_1_STATUS),
 294                                iwl_read_prph(trans, UMAG_SB_CPU_2_STATUS));
 295                else if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
 296                        IWL_ERR(mvm,
 297                                "SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
 298                                iwl_read_prph(trans, SB_CPU_1_STATUS),
 299                                iwl_read_prph(trans, SB_CPU_2_STATUS));
 300                iwl_fw_set_current_image(&mvm->fwrt, old_type);
 301                return ret;
 302        }
 303
 304        if (!alive_data.valid) {
 305                IWL_ERR(mvm, "Loaded ucode is not valid!\n");
 306                iwl_fw_set_current_image(&mvm->fwrt, old_type);
 307                return -EIO;
 308        }
 309
 310        /*
 311         * update the sdio allocation according to the pointer we get in the
 312         * alive notification.
 313         */
 314        st_fwrd_space.addr = mvm->sf_space.addr;
 315        st_fwrd_space.size = mvm->sf_space.size;
 316        ret = iwl_trans_update_sf(mvm->trans, &st_fwrd_space);
 317        if (ret) {
 318                IWL_ERR(mvm, "Failed to update SF size. ret %d\n", ret);
 319                return ret;
 320        }
 321
 322        iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
 323
 324        /*
 325         * Note: all the queues are enabled as part of the interface
 326         * initialization, but in firmware restart scenarios they
 327         * could be stopped, so wake them up. In firmware restart,
 328         * mac80211 will have the queues stopped as well until the
 329         * reconfiguration completes. During normal startup, they
 330         * will be empty.
 331         */
 332
 333        memset(&mvm->queue_info, 0, sizeof(mvm->queue_info));
 334        mvm->queue_info[IWL_MVM_DQA_CMD_QUEUE].hw_queue_refcount = 1;
 335
 336        for (i = 0; i < IEEE80211_MAX_QUEUES; i++)
 337                atomic_set(&mvm->mac80211_queue_stop_count[i], 0);
 338
 339        set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
 340
 341        return 0;
 342}
 343
 344static int iwl_run_unified_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
 345{
 346        struct iwl_notification_wait init_wait;
 347        struct iwl_nvm_access_complete_cmd nvm_complete = {};
 348        struct iwl_init_extended_cfg_cmd init_cfg = {
 349                .init_flags = cpu_to_le32(BIT(IWL_INIT_NVM)),
 350        };
 351        static const u16 init_complete[] = {
 352                INIT_COMPLETE_NOTIF,
 353        };
 354        int ret;
 355
 356        lockdep_assert_held(&mvm->mutex);
 357
 358        iwl_init_notification_wait(&mvm->notif_wait,
 359                                   &init_wait,
 360                                   init_complete,
 361                                   ARRAY_SIZE(init_complete),
 362                                   iwl_wait_init_complete,
 363                                   NULL);
 364
 365        /* Will also start the device */
 366        ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
 367        if (ret) {
 368                IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
 369                goto error;
 370        }
 371
 372        /* Send init config command to mark that we are sending NVM access
 373         * commands
 374         */
 375        ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(SYSTEM_GROUP,
 376                                                INIT_EXTENDED_CFG_CMD), 0,
 377                                   sizeof(init_cfg), &init_cfg);
 378        if (ret) {
 379                IWL_ERR(mvm, "Failed to run init config command: %d\n",
 380                        ret);
 381                goto error;
 382        }
 383
 384        /* Load NVM to NIC if needed */
 385        if (mvm->nvm_file_name) {
 386                iwl_mvm_read_external_nvm(mvm);
 387                iwl_mvm_load_nvm_to_nic(mvm);
 388        }
 389
 390        if (IWL_MVM_PARSE_NVM && read_nvm) {
 391                ret = iwl_nvm_init(mvm);
 392                if (ret) {
 393                        IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
 394                        goto error;
 395                }
 396        }
 397
 398        ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(REGULATORY_AND_NVM_GROUP,
 399                                                NVM_ACCESS_COMPLETE), 0,
 400                                   sizeof(nvm_complete), &nvm_complete);
 401        if (ret) {
 402                IWL_ERR(mvm, "Failed to run complete NVM access: %d\n",
 403                        ret);
 404                goto error;
 405        }
 406
 407        /* We wait for the INIT complete notification */
 408        ret = iwl_wait_notification(&mvm->notif_wait, &init_wait,
 409                                    MVM_UCODE_ALIVE_TIMEOUT);
 410        if (ret)
 411                return ret;
 412
 413        /* Read the NVM only at driver load time, no need to do this twice */
 414        if (!IWL_MVM_PARSE_NVM && read_nvm) {
 415                mvm->nvm_data = iwl_fw_get_nvm(&mvm->fwrt);
 416                if (IS_ERR(mvm->nvm_data)) {
 417                        ret = PTR_ERR(mvm->nvm_data);
 418                        mvm->nvm_data = NULL;
 419                        IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
 420                        return ret;
 421                }
 422        }
 423
 424        return 0;
 425
 426error:
 427        iwl_remove_notification(&mvm->notif_wait, &init_wait);
 428        return ret;
 429}
 430
 431static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
 432{
 433        struct iwl_phy_cfg_cmd phy_cfg_cmd;
 434        enum iwl_ucode_type ucode_type = mvm->fwrt.cur_fw_img;
 435
 436        /* Set parameters */
 437        phy_cfg_cmd.phy_cfg = cpu_to_le32(iwl_mvm_get_phy_config(mvm));
 438
 439        /* set flags extra PHY configuration flags from the device's cfg */
 440        phy_cfg_cmd.phy_cfg |= cpu_to_le32(mvm->cfg->extra_phy_cfg_flags);
 441
 442        phy_cfg_cmd.calib_control.event_trigger =
 443                mvm->fw->default_calib[ucode_type].event_trigger;
 444        phy_cfg_cmd.calib_control.flow_trigger =
 445                mvm->fw->default_calib[ucode_type].flow_trigger;
 446
 447        IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
 448                       phy_cfg_cmd.phy_cfg);
 449
 450        return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, 0,
 451                                    sizeof(phy_cfg_cmd), &phy_cfg_cmd);
 452}
 453
 454int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
 455{
 456        struct iwl_notification_wait calib_wait;
 457        static const u16 init_complete[] = {
 458                INIT_COMPLETE_NOTIF,
 459                CALIB_RES_NOTIF_PHY_DB
 460        };
 461        int ret;
 462
 463        if (iwl_mvm_has_unified_ucode(mvm))
 464                return iwl_run_unified_mvm_ucode(mvm, true);
 465
 466        lockdep_assert_held(&mvm->mutex);
 467
 468        if (WARN_ON_ONCE(mvm->calibrating))
 469                return 0;
 470
 471        iwl_init_notification_wait(&mvm->notif_wait,
 472                                   &calib_wait,
 473                                   init_complete,
 474                                   ARRAY_SIZE(init_complete),
 475                                   iwl_wait_phy_db_entry,
 476                                   mvm->phy_db);
 477
 478        /* Will also start the device */
 479        ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
 480        if (ret) {
 481                IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
 482                goto remove_notif;
 483        }
 484
 485        if (mvm->cfg->device_family < IWL_DEVICE_FAMILY_8000) {
 486                ret = iwl_mvm_send_bt_init_conf(mvm);
 487                if (ret)
 488                        goto remove_notif;
 489        }
 490
 491        /* Read the NVM only at driver load time, no need to do this twice */
 492        if (read_nvm) {
 493                ret = iwl_nvm_init(mvm);
 494                if (ret) {
 495                        IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
 496                        goto remove_notif;
 497                }
 498        }
 499
 500        /* In case we read the NVM from external file, load it to the NIC */
 501        if (mvm->nvm_file_name)
 502                iwl_mvm_load_nvm_to_nic(mvm);
 503
 504        WARN_ON(iwl_nvm_check_version(mvm->nvm_data, mvm->trans));
 505
 506        /*
 507         * abort after reading the nvm in case RF Kill is on, we will complete
 508         * the init seq later when RF kill will switch to off
 509         */
 510        if (iwl_mvm_is_radio_hw_killed(mvm)) {
 511                IWL_DEBUG_RF_KILL(mvm,
 512                                  "jump over all phy activities due to RF kill\n");
 513                goto remove_notif;
 514        }
 515
 516        mvm->calibrating = true;
 517
 518        /* Send TX valid antennas before triggering calibrations */
 519        ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
 520        if (ret)
 521                goto remove_notif;
 522
 523        ret = iwl_send_phy_cfg_cmd(mvm);
 524        if (ret) {
 525                IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
 526                        ret);
 527                goto remove_notif;
 528        }
 529
 530        /*
 531         * Some things may run in the background now, but we
 532         * just wait for the calibration complete notification.
 533         */
 534        ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
 535                                    MVM_UCODE_CALIB_TIMEOUT);
 536        if (!ret)
 537                goto out;
 538
 539        if (iwl_mvm_is_radio_hw_killed(mvm)) {
 540                IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
 541                ret = 0;
 542        } else {
 543                IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
 544                        ret);
 545        }
 546
 547        goto out;
 548
 549remove_notif:
 550        iwl_remove_notification(&mvm->notif_wait, &calib_wait);
 551out:
 552        mvm->calibrating = false;
 553        if (iwlmvm_mod_params.init_dbg && !mvm->nvm_data) {
 554                /* we want to debug INIT and we have no NVM - fake */
 555                mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
 556                                        sizeof(struct ieee80211_channel) +
 557                                        sizeof(struct ieee80211_rate),
 558                                        GFP_KERNEL);
 559                if (!mvm->nvm_data)
 560                        return -ENOMEM;
 561                mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
 562                mvm->nvm_data->bands[0].n_channels = 1;
 563                mvm->nvm_data->bands[0].n_bitrates = 1;
 564                mvm->nvm_data->bands[0].bitrates =
 565                        (void *)mvm->nvm_data->channels + 1;
 566                mvm->nvm_data->bands[0].bitrates->hw_value = 10;
 567        }
 568
 569        return ret;
 570}
 571
 572static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
 573{
 574        struct iwl_ltr_config_cmd cmd = {
 575                .flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
 576        };
 577
 578        if (!mvm->trans->ltr_enabled)
 579                return 0;
 580
 581        return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
 582                                    sizeof(cmd), &cmd);
 583}
 584
 585#ifdef CONFIG_ACPI
 586#define ACPI_WRDS_METHOD                "WRDS"
 587#define ACPI_EWRD_METHOD                "EWRD"
 588#define ACPI_WGDS_METHOD                "WGDS"
 589#define ACPI_WIFI_DOMAIN                (0x07)
 590#define ACPI_WRDS_WIFI_DATA_SIZE        (IWL_MVM_SAR_TABLE_SIZE + 2)
 591#define ACPI_EWRD_WIFI_DATA_SIZE        ((IWL_MVM_SAR_PROFILE_NUM - 1) * \
 592                                         IWL_MVM_SAR_TABLE_SIZE + 3)
 593#define ACPI_WGDS_WIFI_DATA_SIZE        18
 594#define ACPI_WGDS_NUM_BANDS             2
 595#define ACPI_WGDS_TABLE_SIZE            3
 596
 597static int iwl_mvm_sar_set_profile(struct iwl_mvm *mvm,
 598                                   union acpi_object *table,
 599                                   struct iwl_mvm_sar_profile *profile,
 600                                   bool enabled)
 601{
 602        int i;
 603
 604        profile->enabled = enabled;
 605
 606        for (i = 0; i < IWL_MVM_SAR_TABLE_SIZE; i++) {
 607                if ((table[i].type != ACPI_TYPE_INTEGER) ||
 608                    (table[i].integer.value > U8_MAX))
 609                        return -EINVAL;
 610
 611                profile->table[i] = table[i].integer.value;
 612        }
 613
 614        return 0;
 615}
 616
 617static union acpi_object *iwl_mvm_sar_find_wifi_pkg(struct iwl_mvm *mvm,
 618                                                    union acpi_object *data,
 619                                                    int data_size)
 620{
 621        union acpi_object *wifi_pkg = NULL;
 622        int i;
 623
 624        /*
 625         * We need at least two packages, one for the revision and one
 626         * for the data itself.  Also check that the revision is valid
 627         * (i.e. it is an integer set to 0).
 628         */
 629        if (data->type != ACPI_TYPE_PACKAGE ||
 630            data->package.count < 2 ||
 631            data->package.elements[0].type != ACPI_TYPE_INTEGER ||
 632            data->package.elements[0].integer.value != 0) {
 633                IWL_DEBUG_RADIO(mvm, "Unsupported packages structure\n");
 634                return ERR_PTR(-EINVAL);
 635        }
 636
 637        /* loop through all the packages to find the one for WiFi */
 638        for (i = 1; i < data->package.count; i++) {
 639                union acpi_object *domain;
 640
 641                wifi_pkg = &data->package.elements[i];
 642
 643                /* Skip anything that is not a package with the right
 644                 * amount of elements (i.e. domain_type,
 645                 * enabled/disabled plus the actual data size.
 646                 */
 647                if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
 648                    wifi_pkg->package.count != data_size)
 649                        continue;
 650
 651                domain = &wifi_pkg->package.elements[0];
 652                if (domain->type == ACPI_TYPE_INTEGER &&
 653                    domain->integer.value == ACPI_WIFI_DOMAIN)
 654                        break;
 655
 656                wifi_pkg = NULL;
 657        }
 658
 659        if (!wifi_pkg)
 660                return ERR_PTR(-ENOENT);
 661
 662        return wifi_pkg;
 663}
 664
 665static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
 666{
 667        union acpi_object *wifi_pkg, *table;
 668        acpi_handle root_handle;
 669        acpi_handle handle;
 670        struct acpi_buffer wrds = {ACPI_ALLOCATE_BUFFER, NULL};
 671        acpi_status status;
 672        bool enabled;
 673        int ret;
 674
 675        root_handle = ACPI_HANDLE(mvm->dev);
 676        if (!root_handle) {
 677                IWL_DEBUG_RADIO(mvm,
 678                                "Could not retrieve root port ACPI handle\n");
 679                return -ENOENT;
 680        }
 681
 682        /* Get the method's handle */
 683        status = acpi_get_handle(root_handle, (acpi_string)ACPI_WRDS_METHOD,
 684                                 &handle);
 685        if (ACPI_FAILURE(status)) {
 686                IWL_DEBUG_RADIO(mvm, "WRDS method not found\n");
 687                return -ENOENT;
 688        }
 689
 690        /* Call WRDS with no arguments */
 691        status = acpi_evaluate_object(handle, NULL, NULL, &wrds);
 692        if (ACPI_FAILURE(status)) {
 693                IWL_DEBUG_RADIO(mvm, "WRDS invocation failed (0x%x)\n", status);
 694                return -ENOENT;
 695        }
 696
 697        wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, wrds.pointer,
 698                                             ACPI_WRDS_WIFI_DATA_SIZE);
 699        if (IS_ERR(wifi_pkg)) {
 700                ret = PTR_ERR(wifi_pkg);
 701                goto out_free;
 702        }
 703
 704        if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
 705                ret = -EINVAL;
 706                goto out_free;
 707        }
 708
 709        enabled = !!(wifi_pkg->package.elements[1].integer.value);
 710
 711        /* position of the actual table */
 712        table = &wifi_pkg->package.elements[2];
 713
 714        /* The profile from WRDS is officially profile 1, but goes
 715         * into sar_profiles[0] (because we don't have a profile 0).
 716         */
 717        ret = iwl_mvm_sar_set_profile(mvm, table, &mvm->sar_profiles[0],
 718                                      enabled);
 719
 720out_free:
 721        kfree(wrds.pointer);
 722        return ret;
 723}
 724
 725static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
 726{
 727        union acpi_object *wifi_pkg;
 728        acpi_handle root_handle;
 729        acpi_handle handle;
 730        struct acpi_buffer ewrd = {ACPI_ALLOCATE_BUFFER, NULL};
 731        acpi_status status;
 732        bool enabled;
 733        int i, n_profiles, ret;
 734
 735        root_handle = ACPI_HANDLE(mvm->dev);
 736        if (!root_handle) {
 737                IWL_DEBUG_RADIO(mvm,
 738                                "Could not retrieve root port ACPI handle\n");
 739                return -ENOENT;
 740        }
 741
 742        /* Get the method's handle */
 743        status = acpi_get_handle(root_handle, (acpi_string)ACPI_EWRD_METHOD,
 744                                 &handle);
 745        if (ACPI_FAILURE(status)) {
 746                IWL_DEBUG_RADIO(mvm, "EWRD method not found\n");
 747                return -ENOENT;
 748        }
 749
 750        /* Call EWRD with no arguments */
 751        status = acpi_evaluate_object(handle, NULL, NULL, &ewrd);
 752        if (ACPI_FAILURE(status)) {
 753                IWL_DEBUG_RADIO(mvm, "EWRD invocation failed (0x%x)\n", status);
 754                return -ENOENT;
 755        }
 756
 757        wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, ewrd.pointer,
 758                                             ACPI_EWRD_WIFI_DATA_SIZE);
 759        if (IS_ERR(wifi_pkg)) {
 760                ret = PTR_ERR(wifi_pkg);
 761                goto out_free;
 762        }
 763
 764        if ((wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) ||
 765            (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER)) {
 766                ret = -EINVAL;
 767                goto out_free;
 768        }
 769
 770        enabled = !!(wifi_pkg->package.elements[1].integer.value);
 771        n_profiles = wifi_pkg->package.elements[2].integer.value;
 772
 773        /* in case of BIOS bug */
 774        if (n_profiles <= 0) {
 775                ret = -EINVAL;
 776                goto out_free;
 777        }
 778
 779        for (i = 0; i < n_profiles; i++) {
 780                /* the tables start at element 3 */
 781                static int pos = 3;
 782
 783                /* The EWRD profiles officially go from 2 to 4, but we
 784                 * save them in sar_profiles[1-3] (because we don't
 785                 * have profile 0).  So in the array we start from 1.
 786                 */
 787                ret = iwl_mvm_sar_set_profile(mvm,
 788                                              &wifi_pkg->package.elements[pos],
 789                                              &mvm->sar_profiles[i + 1],
 790                                              enabled);
 791                if (ret < 0)
 792                        break;
 793
 794                /* go to the next table */
 795                pos += IWL_MVM_SAR_TABLE_SIZE;
 796        }
 797
 798out_free:
 799        kfree(ewrd.pointer);
 800        return ret;
 801}
 802
 803static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
 804{
 805        union acpi_object *wifi_pkg;
 806        acpi_handle root_handle;
 807        acpi_handle handle;
 808        struct acpi_buffer wgds = {ACPI_ALLOCATE_BUFFER, NULL};
 809        acpi_status status;
 810        int i, j, ret;
 811        int idx = 1;
 812
 813        root_handle = ACPI_HANDLE(mvm->dev);
 814        if (!root_handle) {
 815                IWL_DEBUG_RADIO(mvm,
 816                                "Could not retrieve root port ACPI handle\n");
 817                return -ENOENT;
 818        }
 819
 820        /* Get the method's handle */
 821        status = acpi_get_handle(root_handle, (acpi_string)ACPI_WGDS_METHOD,
 822                                 &handle);
 823        if (ACPI_FAILURE(status)) {
 824                IWL_DEBUG_RADIO(mvm, "WGDS method not found\n");
 825                return -ENOENT;
 826        }
 827
 828        /* Call WGDS with no arguments */
 829        status = acpi_evaluate_object(handle, NULL, NULL, &wgds);
 830        if (ACPI_FAILURE(status)) {
 831                IWL_DEBUG_RADIO(mvm, "WGDS invocation failed (0x%x)\n", status);
 832                return -ENOENT;
 833        }
 834
 835        wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, wgds.pointer,
 836                                             ACPI_WGDS_WIFI_DATA_SIZE);
 837        if (IS_ERR(wifi_pkg)) {
 838                ret = PTR_ERR(wifi_pkg);
 839                goto out_free;
 840        }
 841
 842        for (i = 0; i < IWL_NUM_GEO_PROFILES; i++) {
 843                for (j = 0; j < IWL_MVM_GEO_TABLE_SIZE; j++) {
 844                        union acpi_object *entry;
 845
 846                        entry = &wifi_pkg->package.elements[idx++];
 847                        if ((entry->type != ACPI_TYPE_INTEGER) ||
 848                            (entry->integer.value > U8_MAX)) {
 849                                ret = -EINVAL;
 850                                goto out_free;
 851                        }
 852
 853                        mvm->geo_profiles[i].values[j] = entry->integer.value;
 854                }
 855        }
 856        ret = 0;
 857out_free:
 858        kfree(wgds.pointer);
 859        return ret;
 860}
 861
 862int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a, int prof_b)
 863{
 864        struct iwl_dev_tx_power_cmd cmd = {
 865                .v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS),
 866        };
 867        int i, j, idx;
 868        int profs[IWL_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
 869        int len = sizeof(cmd);
 870
 871        BUILD_BUG_ON(IWL_NUM_CHAIN_LIMITS < 2);
 872        BUILD_BUG_ON(IWL_NUM_CHAIN_LIMITS * IWL_NUM_SUB_BANDS !=
 873                     IWL_MVM_SAR_TABLE_SIZE);
 874
 875        if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
 876                len = sizeof(cmd.v3);
 877
 878        for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
 879                struct iwl_mvm_sar_profile *prof;
 880
 881                /* don't allow SAR to be disabled (profile 0 means disable) */
 882                if (profs[i] == 0)
 883                        return -EPERM;
 884
 885                /* we are off by one, so allow up to IWL_MVM_SAR_PROFILE_NUM */
 886                if (profs[i] > IWL_MVM_SAR_PROFILE_NUM)
 887                        return -EINVAL;
 888
 889                /* profiles go from 1 to 4, so decrement to access the array */
 890                prof = &mvm->sar_profiles[profs[i] - 1];
 891
 892                /* if the profile is disabled, do nothing */
 893                if (!prof->enabled) {
 894                        IWL_DEBUG_RADIO(mvm, "SAR profile %d is disabled.\n",
 895                                        profs[i]);
 896                        /* if one of the profiles is disabled, we fail all */
 897                        return -ENOENT;
 898                }
 899
 900                IWL_DEBUG_RADIO(mvm, "  Chain[%d]:\n", i);
 901                for (j = 0; j < IWL_NUM_SUB_BANDS; j++) {
 902                        idx = (i * IWL_NUM_SUB_BANDS) + j;
 903                        cmd.v3.per_chain_restriction[i][j] =
 904                                cpu_to_le16(prof->table[idx]);
 905                        IWL_DEBUG_RADIO(mvm, "    Band[%d] = %d * .125dBm\n",
 906                                        j, prof->table[idx]);
 907                }
 908        }
 909
 910        IWL_DEBUG_RADIO(mvm, "Sending REDUCE_TX_POWER_CMD per chain\n");
 911
 912        return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
 913}
 914
 915int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
 916{
 917        struct iwl_geo_tx_power_profiles_resp *resp;
 918        int ret;
 919
 920        struct iwl_geo_tx_power_profiles_cmd geo_cmd = {
 921                .ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE),
 922        };
 923        struct iwl_host_cmd cmd = {
 924                .id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT),
 925                .len = { sizeof(geo_cmd), },
 926                .flags = CMD_WANT_SKB,
 927                .data = { &geo_cmd },
 928        };
 929
 930        ret = iwl_mvm_send_cmd(mvm, &cmd);
 931        if (ret) {
 932                IWL_ERR(mvm, "Failed to get geographic profile info %d\n", ret);
 933                return ret;
 934        }
 935
 936        resp = (void *)cmd.resp_pkt->data;
 937        ret = le32_to_cpu(resp->profile_idx);
 938        if (WARN_ON(ret > IWL_NUM_GEO_PROFILES)) {
 939                ret = -EIO;
 940                IWL_WARN(mvm, "Invalid geographic profile idx (%d)\n", ret);
 941        }
 942
 943        iwl_free_resp(&cmd);
 944        return ret;
 945}
 946
 947static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
 948{
 949        struct iwl_geo_tx_power_profiles_cmd cmd = {
 950                .ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_SET_TABLES),
 951        };
 952        int ret, i, j;
 953        u16 cmd_wide_id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT);
 954
 955        ret = iwl_mvm_sar_get_wgds_table(mvm);
 956        if (ret < 0) {
 957                IWL_DEBUG_RADIO(mvm,
 958                                "Geo SAR BIOS table invalid or unavailable. (%d)\n",
 959                                ret);
 960                /* we don't fail if the table is not available */
 961                return 0;
 962        }
 963
 964        IWL_DEBUG_RADIO(mvm, "Sending GEO_TX_POWER_LIMIT\n");
 965
 966        BUILD_BUG_ON(IWL_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
 967                     ACPI_WGDS_TABLE_SIZE !=  ACPI_WGDS_WIFI_DATA_SIZE);
 968
 969        for (i = 0; i < IWL_NUM_GEO_PROFILES; i++) {
 970                struct iwl_per_chain_offset *chain =
 971                        (struct iwl_per_chain_offset *)&cmd.table[i];
 972
 973                for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
 974                        u8 *value;
 975
 976                        value = &mvm->geo_profiles[i].values[j *
 977                                IWL_GEO_PER_CHAIN_SIZE];
 978                        chain[j].max_tx_power = cpu_to_le16(value[0]);
 979                        chain[j].chain_a = value[1];
 980                        chain[j].chain_b = value[2];
 981                        IWL_DEBUG_RADIO(mvm,
 982                                        "SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
 983                                        i, j, value[1], value[2], value[0]);
 984                }
 985        }
 986        return iwl_mvm_send_cmd_pdu(mvm, cmd_wide_id, 0, sizeof(cmd), &cmd);
 987}
 988
 989#else /* CONFIG_ACPI */
 990static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
 991{
 992        return -ENOENT;
 993}
 994
 995static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
 996{
 997        return -ENOENT;
 998}
 999
1000static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
1001{
1002        return 0;
1003}
1004
1005int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a,
1006                               int prof_b)
1007{
1008        return -ENOENT;
1009}
1010
1011int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
1012{
1013        return -ENOENT;
1014}
1015#endif /* CONFIG_ACPI */
1016
1017static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
1018{
1019        int ret;
1020
1021        ret = iwl_mvm_sar_get_wrds_table(mvm);
1022        if (ret < 0) {
1023                IWL_DEBUG_RADIO(mvm,
1024                                "WRDS SAR BIOS table invalid or unavailable. (%d)\n",
1025                                ret);
1026                /* if not available, don't fail and don't bother with EWRD */
1027                return 0;
1028        }
1029
1030        ret = iwl_mvm_sar_get_ewrd_table(mvm);
1031        /* if EWRD is not available, we can still use WRDS, so don't fail */
1032        if (ret < 0)
1033                IWL_DEBUG_RADIO(mvm,
1034                                "EWRD SAR BIOS table invalid or unavailable. (%d)\n",
1035                                ret);
1036
1037        /* choose profile 1 (WRDS) as default for both chains */
1038        ret = iwl_mvm_sar_select_profile(mvm, 1, 1);
1039
1040        /* if we don't have profile 0 from BIOS, just skip it */
1041        if (ret == -ENOENT)
1042                return 0;
1043
1044        return ret;
1045}
1046
1047static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
1048{
1049        int ret;
1050
1051        if (iwl_mvm_has_unified_ucode(mvm))
1052                return iwl_run_unified_mvm_ucode(mvm, false);
1053
1054        ret = iwl_run_init_mvm_ucode(mvm, false);
1055
1056        if (iwlmvm_mod_params.init_dbg)
1057                return 0;
1058
1059        if (ret) {
1060                IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1061                return ret;
1062        }
1063
1064        /*
1065         * Stop and start the transport without entering low power
1066         * mode. This will save the state of other components on the
1067         * device that are triggered by the INIT firwmare (MFUART).
1068         */
1069        _iwl_trans_stop_device(mvm->trans, false);
1070        ret = _iwl_trans_start_hw(mvm->trans, false);
1071        if (ret)
1072                return ret;
1073
1074        ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
1075        if (ret)
1076                return ret;
1077
1078        return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
1079}
1080
1081int iwl_mvm_up(struct iwl_mvm *mvm)
1082{
1083        int ret, i;
1084        struct ieee80211_channel *chan;
1085        struct cfg80211_chan_def chandef;
1086
1087        lockdep_assert_held(&mvm->mutex);
1088
1089        ret = iwl_trans_start_hw(mvm->trans);
1090        if (ret)
1091                return ret;
1092
1093        ret = iwl_mvm_load_rt_fw(mvm);
1094        if (ret) {
1095                IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
1096                goto error;
1097        }
1098
1099        iwl_get_shared_mem_conf(&mvm->fwrt);
1100
1101        ret = iwl_mvm_sf_update(mvm, NULL, false);
1102        if (ret)
1103                IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1104
1105        mvm->fwrt.dump.conf = FW_DBG_INVALID;
1106        /* if we have a destination, assume EARLY START */
1107        if (mvm->fw->dbg_dest_tlv)
1108                mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
1109        iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
1110
1111        ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1112        if (ret)
1113                goto error;
1114
1115        if (!iwl_mvm_has_unified_ucode(mvm)) {
1116                /* Send phy db control command and then phy db calibration */
1117                ret = iwl_send_phy_db_data(mvm->phy_db);
1118                if (ret)
1119                        goto error;
1120
1121                ret = iwl_send_phy_cfg_cmd(mvm);
1122                if (ret)
1123                        goto error;
1124        }
1125
1126        ret = iwl_mvm_send_bt_init_conf(mvm);
1127        if (ret)
1128                goto error;
1129
1130        /* Init RSS configuration */
1131        /* TODO - remove a000 disablement when we have RXQ config API */
1132        if (iwl_mvm_has_new_rx_api(mvm) &&
1133            mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_A000) {
1134                ret = iwl_send_rss_cfg_cmd(mvm);
1135                if (ret) {
1136                        IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
1137                                ret);
1138                        goto error;
1139                }
1140        }
1141
1142        /* init the fw <-> mac80211 STA mapping */
1143        for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1144                RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1145
1146        mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1147
1148        /* reset quota debouncing buffer - 0xff will yield invalid data */
1149        memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1150
1151        ret = iwl_mvm_send_dqa_cmd(mvm);
1152        if (ret)
1153                goto error;
1154
1155        /* Add auxiliary station for scanning */
1156        ret = iwl_mvm_add_aux_sta(mvm);
1157        if (ret)
1158                goto error;
1159
1160        /* Add all the PHY contexts */
1161        chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
1162        cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
1163        for (i = 0; i < NUM_PHY_CTX; i++) {
1164                /*
1165                 * The channel used here isn't relevant as it's
1166                 * going to be overwritten in the other flows.
1167                 * For now use the first channel we have.
1168                 */
1169                ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
1170                                           &chandef, 1, 1);
1171                if (ret)
1172                        goto error;
1173        }
1174
1175#ifdef CONFIG_THERMAL
1176        if (iwl_mvm_is_tt_in_fw(mvm)) {
1177                /* in order to give the responsibility of ct-kill and
1178                 * TX backoff to FW we need to send empty temperature reporting
1179                 * cmd during init time
1180                 */
1181                iwl_mvm_send_temp_report_ths_cmd(mvm);
1182        } else {
1183                /* Initialize tx backoffs to the minimal possible */
1184                iwl_mvm_tt_tx_backoff(mvm, 0);
1185        }
1186
1187        /* TODO: read the budget from BIOS / Platform NVM */
1188
1189        /*
1190         * In case there is no budget from BIOS / Platform NVM the default
1191         * budget should be 2000mW (cooling state 0).
1192         */
1193        if (iwl_mvm_is_ctdp_supported(mvm)) {
1194                ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
1195                                           mvm->cooling_dev.cur_state);
1196                if (ret)
1197                        goto error;
1198        }
1199#else
1200        /* Initialize tx backoffs to the minimal possible */
1201        iwl_mvm_tt_tx_backoff(mvm, 0);
1202#endif
1203
1204        WARN_ON(iwl_mvm_config_ltr(mvm));
1205
1206        ret = iwl_mvm_power_update_device(mvm);
1207        if (ret)
1208                goto error;
1209
1210        /*
1211         * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1212         * anyway, so don't init MCC.
1213         */
1214        if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1215                ret = iwl_mvm_init_mcc(mvm);
1216                if (ret)
1217                        goto error;
1218        }
1219
1220        if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1221                mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1222                ret = iwl_mvm_config_scan(mvm);
1223                if (ret)
1224                        goto error;
1225        }
1226
1227        /* allow FW/transport low power modes if not during restart */
1228        if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1229                iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1230
1231        ret = iwl_mvm_sar_init(mvm);
1232        if (ret)
1233                goto error;
1234
1235        ret = iwl_mvm_sar_geo_init(mvm);
1236        if (ret)
1237                goto error;
1238
1239        iwl_mvm_leds_sync(mvm);
1240
1241        IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1242        return 0;
1243 error:
1244        if (!iwlmvm_mod_params.init_dbg)
1245                iwl_mvm_stop_device(mvm);
1246        return ret;
1247}
1248
1249int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1250{
1251        int ret, i;
1252
1253        lockdep_assert_held(&mvm->mutex);
1254
1255        ret = iwl_trans_start_hw(mvm->trans);
1256        if (ret)
1257                return ret;
1258
1259        ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1260        if (ret) {
1261                IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1262                goto error;
1263        }
1264
1265        ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1266        if (ret)
1267                goto error;
1268
1269        /* Send phy db control command and then phy db calibration*/
1270        ret = iwl_send_phy_db_data(mvm->phy_db);
1271        if (ret)
1272                goto error;
1273
1274        ret = iwl_send_phy_cfg_cmd(mvm);
1275        if (ret)
1276                goto error;
1277
1278        /* init the fw <-> mac80211 STA mapping */
1279        for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1280                RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1281
1282        /* Add auxiliary station for scanning */
1283        ret = iwl_mvm_add_aux_sta(mvm);
1284        if (ret)
1285                goto error;
1286
1287        return 0;
1288 error:
1289        iwl_mvm_stop_device(mvm);
1290        return ret;
1291}
1292
1293void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
1294                                 struct iwl_rx_cmd_buffer *rxb)
1295{
1296        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1297        struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
1298        u32 flags = le32_to_cpu(card_state_notif->flags);
1299
1300        IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
1301                          (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1302                          (flags & SW_CARD_DISABLED) ? "Kill" : "On",
1303                          (flags & CT_KILL_CARD_DISABLED) ?
1304                          "Reached" : "Not reached");
1305}
1306
1307void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
1308                             struct iwl_rx_cmd_buffer *rxb)
1309{
1310        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1311        struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
1312
1313        IWL_DEBUG_INFO(mvm,
1314                       "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
1315                       le32_to_cpu(mfuart_notif->installed_ver),
1316                       le32_to_cpu(mfuart_notif->external_ver),
1317                       le32_to_cpu(mfuart_notif->status),
1318                       le32_to_cpu(mfuart_notif->duration));
1319
1320        if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
1321                IWL_DEBUG_INFO(mvm,
1322                               "MFUART: image size: 0x%08x\n",
1323                               le32_to_cpu(mfuart_notif->image_size));
1324}
1325