GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / wireless / intel / iwlwifi / mvm / fw.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * 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
89 struct iwl_mvm_alive_data {
90         bool valid;
91         u32 scd_base_addr;
92 };
93
94 static 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
105 static 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 = BIT(IWL_RSS_HASH_TYPE_IPV4_TCP) |
111                              BIT(IWL_RSS_HASH_TYPE_IPV4_UDP) |
112                              BIT(IWL_RSS_HASH_TYPE_IPV4_PAYLOAD) |
113                              BIT(IWL_RSS_HASH_TYPE_IPV6_TCP) |
114                              BIT(IWL_RSS_HASH_TYPE_IPV6_UDP) |
115                              BIT(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
130 static 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
147 void 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
168 static 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
224 static 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
232 static 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
247 static 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
344 static 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
426 error:
427         iwl_remove_notification(&mvm->notif_wait, &init_wait);
428         return ret;
429 }
430
431 static 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
454 int 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_ONCE(mvm->nvm_data->nvm_version < mvm->trans->cfg->nvm_ver,
505                   "Too old NVM version (0x%0x, required = 0x%0x)",
506                   mvm->nvm_data->nvm_version, mvm->trans->cfg->nvm_ver);
507
508         /*
509          * abort after reading the nvm in case RF Kill is on, we will complete
510          * the init seq later when RF kill will switch to off
511          */
512         if (iwl_mvm_is_radio_hw_killed(mvm)) {
513                 IWL_DEBUG_RF_KILL(mvm,
514                                   "jump over all phy activities due to RF kill\n");
515                 goto remove_notif;
516         }
517
518         mvm->calibrating = true;
519
520         /* Send TX valid antennas before triggering calibrations */
521         ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
522         if (ret)
523                 goto remove_notif;
524
525         ret = iwl_send_phy_cfg_cmd(mvm);
526         if (ret) {
527                 IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
528                         ret);
529                 goto remove_notif;
530         }
531
532         /*
533          * Some things may run in the background now, but we
534          * just wait for the calibration complete notification.
535          */
536         ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
537                                     MVM_UCODE_CALIB_TIMEOUT);
538         if (!ret)
539                 goto out;
540
541         if (iwl_mvm_is_radio_hw_killed(mvm)) {
542                 IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
543                 ret = 0;
544         } else {
545                 IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
546                         ret);
547         }
548
549         goto out;
550
551 remove_notif:
552         iwl_remove_notification(&mvm->notif_wait, &calib_wait);
553 out:
554         mvm->calibrating = false;
555         if (iwlmvm_mod_params.init_dbg && !mvm->nvm_data) {
556                 /* we want to debug INIT and we have no NVM - fake */
557                 mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
558                                         sizeof(struct ieee80211_channel) +
559                                         sizeof(struct ieee80211_rate),
560                                         GFP_KERNEL);
561                 if (!mvm->nvm_data)
562                         return -ENOMEM;
563                 mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
564                 mvm->nvm_data->bands[0].n_channels = 1;
565                 mvm->nvm_data->bands[0].n_bitrates = 1;
566                 mvm->nvm_data->bands[0].bitrates =
567                         (void *)mvm->nvm_data->channels + 1;
568                 mvm->nvm_data->bands[0].bitrates->hw_value = 10;
569         }
570
571         return ret;
572 }
573
574 static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
575 {
576         struct iwl_ltr_config_cmd cmd = {
577                 .flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
578         };
579
580         if (!mvm->trans->ltr_enabled)
581                 return 0;
582
583         return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
584                                     sizeof(cmd), &cmd);
585 }
586
587 #ifdef CONFIG_ACPI
588 #define ACPI_WRDS_METHOD                "WRDS"
589 #define ACPI_EWRD_METHOD                "EWRD"
590 #define ACPI_WGDS_METHOD                "WGDS"
591 #define ACPI_WIFI_DOMAIN                (0x07)
592 #define ACPI_WRDS_WIFI_DATA_SIZE        (IWL_MVM_SAR_TABLE_SIZE + 2)
593 #define ACPI_EWRD_WIFI_DATA_SIZE        ((IWL_MVM_SAR_PROFILE_NUM - 1) * \
594                                          IWL_MVM_SAR_TABLE_SIZE + 3)
595 #define ACPI_WGDS_WIFI_DATA_SIZE        19
596 #define ACPI_WGDS_NUM_BANDS             2
597 #define ACPI_WGDS_TABLE_SIZE            3
598
599 static int iwl_mvm_sar_set_profile(struct iwl_mvm *mvm,
600                                    union acpi_object *table,
601                                    struct iwl_mvm_sar_profile *profile,
602                                    bool enabled)
603 {
604         int i;
605
606         profile->enabled = enabled;
607
608         for (i = 0; i < IWL_MVM_SAR_TABLE_SIZE; i++) {
609                 if ((table[i].type != ACPI_TYPE_INTEGER) ||
610                     (table[i].integer.value > U8_MAX))
611                         return -EINVAL;
612
613                 profile->table[i] = table[i].integer.value;
614         }
615
616         return 0;
617 }
618
619 static union acpi_object *iwl_mvm_sar_find_wifi_pkg(struct iwl_mvm *mvm,
620                                                     union acpi_object *data,
621                                                     int data_size)
622 {
623         union acpi_object *wifi_pkg = NULL;
624         int i;
625
626         /*
627          * We need at least two packages, one for the revision and one
628          * for the data itself.  Also check that the revision is valid
629          * (i.e. it is an integer set to 0).
630          */
631         if (data->type != ACPI_TYPE_PACKAGE ||
632             data->package.count < 2 ||
633             data->package.elements[0].type != ACPI_TYPE_INTEGER ||
634             data->package.elements[0].integer.value != 0) {
635                 IWL_DEBUG_RADIO(mvm, "Unsupported packages structure\n");
636                 return ERR_PTR(-EINVAL);
637         }
638
639         /* loop through all the packages to find the one for WiFi */
640         for (i = 1; i < data->package.count; i++) {
641                 union acpi_object *domain;
642
643                 wifi_pkg = &data->package.elements[i];
644
645                 /* Skip anything that is not a package with the right
646                  * amount of elements (i.e. domain_type,
647                  * enabled/disabled plus the actual data size.
648                  */
649                 if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
650                     wifi_pkg->package.count != data_size)
651                         continue;
652
653                 domain = &wifi_pkg->package.elements[0];
654                 if (domain->type == ACPI_TYPE_INTEGER &&
655                     domain->integer.value == ACPI_WIFI_DOMAIN)
656                         break;
657
658                 wifi_pkg = NULL;
659         }
660
661         if (!wifi_pkg)
662                 return ERR_PTR(-ENOENT);
663
664         return wifi_pkg;
665 }
666
667 static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
668 {
669         union acpi_object *wifi_pkg, *table;
670         acpi_handle root_handle;
671         acpi_handle handle;
672         struct acpi_buffer wrds = {ACPI_ALLOCATE_BUFFER, NULL};
673         acpi_status status;
674         bool enabled;
675         int ret;
676
677         root_handle = ACPI_HANDLE(mvm->dev);
678         if (!root_handle) {
679                 IWL_DEBUG_RADIO(mvm,
680                                 "Could not retrieve root port ACPI handle\n");
681                 return -ENOENT;
682         }
683
684         /* Get the method's handle */
685         status = acpi_get_handle(root_handle, (acpi_string)ACPI_WRDS_METHOD,
686                                  &handle);
687         if (ACPI_FAILURE(status)) {
688                 IWL_DEBUG_RADIO(mvm, "WRDS method not found\n");
689                 return -ENOENT;
690         }
691
692         /* Call WRDS with no arguments */
693         status = acpi_evaluate_object(handle, NULL, NULL, &wrds);
694         if (ACPI_FAILURE(status)) {
695                 IWL_DEBUG_RADIO(mvm, "WRDS invocation failed (0x%x)\n", status);
696                 return -ENOENT;
697         }
698
699         wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, wrds.pointer,
700                                              ACPI_WRDS_WIFI_DATA_SIZE);
701         if (IS_ERR(wifi_pkg)) {
702                 ret = PTR_ERR(wifi_pkg);
703                 goto out_free;
704         }
705
706         if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
707                 ret = -EINVAL;
708                 goto out_free;
709         }
710
711         enabled = !!(wifi_pkg->package.elements[1].integer.value);
712
713         /* position of the actual table */
714         table = &wifi_pkg->package.elements[2];
715
716         /* The profile from WRDS is officially profile 1, but goes
717          * into sar_profiles[0] (because we don't have a profile 0).
718          */
719         ret = iwl_mvm_sar_set_profile(mvm, table, &mvm->sar_profiles[0],
720                                       enabled);
721
722 out_free:
723         kfree(wrds.pointer);
724         return ret;
725 }
726
727 static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
728 {
729         union acpi_object *wifi_pkg;
730         acpi_handle root_handle;
731         acpi_handle handle;
732         struct acpi_buffer ewrd = {ACPI_ALLOCATE_BUFFER, NULL};
733         acpi_status status;
734         bool enabled;
735         int i, n_profiles, ret;
736
737         root_handle = ACPI_HANDLE(mvm->dev);
738         if (!root_handle) {
739                 IWL_DEBUG_RADIO(mvm,
740                                 "Could not retrieve root port ACPI handle\n");
741                 return -ENOENT;
742         }
743
744         /* Get the method's handle */
745         status = acpi_get_handle(root_handle, (acpi_string)ACPI_EWRD_METHOD,
746                                  &handle);
747         if (ACPI_FAILURE(status)) {
748                 IWL_DEBUG_RADIO(mvm, "EWRD method not found\n");
749                 return -ENOENT;
750         }
751
752         /* Call EWRD with no arguments */
753         status = acpi_evaluate_object(handle, NULL, NULL, &ewrd);
754         if (ACPI_FAILURE(status)) {
755                 IWL_DEBUG_RADIO(mvm, "EWRD invocation failed (0x%x)\n", status);
756                 return -ENOENT;
757         }
758
759         wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, ewrd.pointer,
760                                              ACPI_EWRD_WIFI_DATA_SIZE);
761         if (IS_ERR(wifi_pkg)) {
762                 ret = PTR_ERR(wifi_pkg);
763                 goto out_free;
764         }
765
766         if ((wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) ||
767             (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER)) {
768                 ret = -EINVAL;
769                 goto out_free;
770         }
771
772         enabled = !!(wifi_pkg->package.elements[1].integer.value);
773         n_profiles = wifi_pkg->package.elements[2].integer.value;
774
775         /* in case of BIOS bug */
776         if (n_profiles <= 0) {
777                 ret = -EINVAL;
778                 goto out_free;
779         }
780
781         for (i = 0; i < n_profiles; i++) {
782                 /* the tables start at element 3 */
783                 int pos = 3;
784
785                 /* The EWRD profiles officially go from 2 to 4, but we
786                  * save them in sar_profiles[1-3] (because we don't
787                  * have profile 0).  So in the array we start from 1.
788                  */
789                 ret = iwl_mvm_sar_set_profile(mvm,
790                                               &wifi_pkg->package.elements[pos],
791                                               &mvm->sar_profiles[i + 1],
792                                               enabled);
793                 if (ret < 0)
794                         break;
795
796                 /* go to the next table */
797                 pos += IWL_MVM_SAR_TABLE_SIZE;
798         }
799
800 out_free:
801         kfree(ewrd.pointer);
802         return ret;
803 }
804
805 static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
806 {
807         union acpi_object *wifi_pkg;
808         acpi_handle root_handle;
809         acpi_handle handle;
810         struct acpi_buffer wgds = {ACPI_ALLOCATE_BUFFER, NULL};
811         acpi_status status;
812         int i, j, ret;
813         int idx = 1;
814
815         root_handle = ACPI_HANDLE(mvm->dev);
816         if (!root_handle) {
817                 IWL_DEBUG_RADIO(mvm,
818                                 "Could not retrieve root port ACPI handle\n");
819                 return -ENOENT;
820         }
821
822         /* Get the method's handle */
823         status = acpi_get_handle(root_handle, (acpi_string)ACPI_WGDS_METHOD,
824                                  &handle);
825         if (ACPI_FAILURE(status)) {
826                 IWL_DEBUG_RADIO(mvm, "WGDS method not found\n");
827                 return -ENOENT;
828         }
829
830         /* Call WGDS with no arguments */
831         status = acpi_evaluate_object(handle, NULL, NULL, &wgds);
832         if (ACPI_FAILURE(status)) {
833                 IWL_DEBUG_RADIO(mvm, "WGDS invocation failed (0x%x)\n", status);
834                 return -ENOENT;
835         }
836
837         wifi_pkg = iwl_mvm_sar_find_wifi_pkg(mvm, wgds.pointer,
838                                              ACPI_WGDS_WIFI_DATA_SIZE);
839         if (IS_ERR(wifi_pkg)) {
840                 ret = PTR_ERR(wifi_pkg);
841                 goto out_free;
842         }
843
844         for (i = 0; i < IWL_NUM_GEO_PROFILES; i++) {
845                 for (j = 0; j < IWL_MVM_GEO_TABLE_SIZE; j++) {
846                         union acpi_object *entry;
847
848                         entry = &wifi_pkg->package.elements[idx++];
849                         if ((entry->type != ACPI_TYPE_INTEGER) ||
850                             (entry->integer.value > U8_MAX)) {
851                                 ret = -EINVAL;
852                                 goto out_free;
853                         }
854
855                         mvm->geo_profiles[i].values[j] = entry->integer.value;
856                 }
857         }
858         ret = 0;
859 out_free:
860         kfree(wgds.pointer);
861         return ret;
862 }
863
864 int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a, int prof_b)
865 {
866         struct iwl_dev_tx_power_cmd cmd = {
867                 .v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_CHAINS),
868         };
869         int i, j, idx;
870         int profs[IWL_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
871         int len = sizeof(cmd);
872
873         BUILD_BUG_ON(IWL_NUM_CHAIN_LIMITS < 2);
874         BUILD_BUG_ON(IWL_NUM_CHAIN_LIMITS * IWL_NUM_SUB_BANDS !=
875                      IWL_MVM_SAR_TABLE_SIZE);
876
877         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
878                 len = sizeof(cmd.v3);
879
880         for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
881                 struct iwl_mvm_sar_profile *prof;
882
883                 /* don't allow SAR to be disabled (profile 0 means disable) */
884                 if (profs[i] == 0)
885                         return -EPERM;
886
887                 /* we are off by one, so allow up to IWL_MVM_SAR_PROFILE_NUM */
888                 if (profs[i] > IWL_MVM_SAR_PROFILE_NUM)
889                         return -EINVAL;
890
891                 /* profiles go from 1 to 4, so decrement to access the array */
892                 prof = &mvm->sar_profiles[profs[i] - 1];
893
894                 /* if the profile is disabled, do nothing */
895                 if (!prof->enabled) {
896                         IWL_DEBUG_RADIO(mvm, "SAR profile %d is disabled.\n",
897                                         profs[i]);
898                         /* if one of the profiles is disabled, we fail all */
899                         return -ENOENT;
900                 }
901
902                 IWL_DEBUG_RADIO(mvm, "  Chain[%d]:\n", i);
903                 for (j = 0; j < IWL_NUM_SUB_BANDS; j++) {
904                         idx = (i * IWL_NUM_SUB_BANDS) + j;
905                         cmd.v3.per_chain_restriction[i][j] =
906                                 cpu_to_le16(prof->table[idx]);
907                         IWL_DEBUG_RADIO(mvm, "    Band[%d] = %d * .125dBm\n",
908                                         j, prof->table[idx]);
909                 }
910         }
911
912         IWL_DEBUG_RADIO(mvm, "Sending REDUCE_TX_POWER_CMD per chain\n");
913
914         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
915 }
916
917 static bool iwl_mvm_sar_geo_support(struct iwl_mvm *mvm)
918 {
919         /*
920          * The GEO_TX_POWER_LIMIT command is not supported on earlier
921          * firmware versions.  Unfortunately, we don't have a TLV API
922          * flag to rely on, so rely on the major version which is in
923          * the first byte of ucode_ver.  This was implemented
924          * initially on version 38 and then backported to 36, 29 and
925          * 17.
926          */
927         return IWL_UCODE_SERIAL(mvm->fw->ucode_ver) >= 38 ||
928                IWL_UCODE_SERIAL(mvm->fw->ucode_ver) == 36 ||
929                IWL_UCODE_SERIAL(mvm->fw->ucode_ver) == 29 ||
930                IWL_UCODE_SERIAL(mvm->fw->ucode_ver) == 17;
931 }
932
933 int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
934 {
935         struct iwl_geo_tx_power_profiles_resp *resp;
936         int ret;
937
938         struct iwl_geo_tx_power_profiles_cmd geo_cmd = {
939                 .ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_GET_CURRENT_TABLE),
940         };
941         struct iwl_host_cmd cmd = {
942                 .id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT),
943                 .len = { sizeof(geo_cmd), },
944                 .flags = CMD_WANT_SKB,
945                 .data = { &geo_cmd },
946         };
947
948         if (!iwl_mvm_sar_geo_support(mvm))
949                 return -EOPNOTSUPP;
950
951         ret = iwl_mvm_send_cmd(mvm, &cmd);
952         if (ret) {
953                 IWL_ERR(mvm, "Failed to get geographic profile info %d\n", ret);
954                 return ret;
955         }
956
957         resp = (void *)cmd.resp_pkt->data;
958         ret = le32_to_cpu(resp->profile_idx);
959         if (WARN_ON(ret > IWL_NUM_GEO_PROFILES)) {
960                 ret = -EIO;
961                 IWL_WARN(mvm, "Invalid geographic profile idx (%d)\n", ret);
962         }
963
964         iwl_free_resp(&cmd);
965         return ret;
966 }
967
968 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
969 {
970         struct iwl_geo_tx_power_profiles_cmd cmd = {
971                 .ops = cpu_to_le32(IWL_PER_CHAIN_OFFSET_SET_TABLES),
972         };
973         int ret, i, j;
974         u16 cmd_wide_id =  WIDE_ID(PHY_OPS_GROUP, GEO_TX_POWER_LIMIT);
975
976         if (!iwl_mvm_sar_geo_support(mvm))
977                 return 0;
978
979         ret = iwl_mvm_sar_get_wgds_table(mvm);
980         if (ret < 0) {
981                 IWL_DEBUG_RADIO(mvm,
982                                 "Geo SAR BIOS table invalid or unavailable. (%d)\n",
983                                 ret);
984                 /* we don't fail if the table is not available */
985                 return 0;
986         }
987
988         IWL_DEBUG_RADIO(mvm, "Sending GEO_TX_POWER_LIMIT\n");
989
990         BUILD_BUG_ON(IWL_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
991                      ACPI_WGDS_TABLE_SIZE + 1 !=  ACPI_WGDS_WIFI_DATA_SIZE);
992
993         for (i = 0; i < IWL_NUM_GEO_PROFILES; i++) {
994                 struct iwl_per_chain_offset *chain =
995                         (struct iwl_per_chain_offset *)&cmd.table[i];
996
997                 for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
998                         u8 *value;
999
1000                         value = &mvm->geo_profiles[i].values[j *
1001                                 IWL_GEO_PER_CHAIN_SIZE];
1002                         chain[j].max_tx_power = cpu_to_le16(value[0]);
1003                         chain[j].chain_a = value[1];
1004                         chain[j].chain_b = value[2];
1005                         IWL_DEBUG_RADIO(mvm,
1006                                         "SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
1007                                         i, j, value[1], value[2], value[0]);
1008                 }
1009         }
1010         return iwl_mvm_send_cmd_pdu(mvm, cmd_wide_id, 0, sizeof(cmd), &cmd);
1011 }
1012
1013 #else /* CONFIG_ACPI */
1014 static int iwl_mvm_sar_get_wrds_table(struct iwl_mvm *mvm)
1015 {
1016         return -ENOENT;
1017 }
1018
1019 static int iwl_mvm_sar_get_ewrd_table(struct iwl_mvm *mvm)
1020 {
1021         return -ENOENT;
1022 }
1023
1024 static int iwl_mvm_sar_get_wgds_table(struct iwl_mvm *mvm)
1025 {
1026         return -ENOENT;
1027 }
1028
1029 static int iwl_mvm_sar_geo_init(struct iwl_mvm *mvm)
1030 {
1031         return 0;
1032 }
1033
1034 int iwl_mvm_sar_select_profile(struct iwl_mvm *mvm, int prof_a,
1035                                int prof_b)
1036 {
1037         return -ENOENT;
1038 }
1039
1040 int iwl_mvm_get_sar_geo_profile(struct iwl_mvm *mvm)
1041 {
1042         return -ENOENT;
1043 }
1044 #endif /* CONFIG_ACPI */
1045
1046 static int iwl_mvm_sar_init(struct iwl_mvm *mvm)
1047 {
1048         int ret;
1049
1050         ret = iwl_mvm_sar_get_wrds_table(mvm);
1051         if (ret < 0) {
1052                 IWL_DEBUG_RADIO(mvm,
1053                                 "WRDS SAR BIOS table invalid or unavailable. (%d)\n",
1054                                 ret);
1055                 /*
1056                  * If not available, don't fail and don't bother with EWRD.
1057                  * Return 1 to tell that we can't use WGDS either.
1058                  */
1059                 return 1;
1060         }
1061
1062         ret = iwl_mvm_sar_get_ewrd_table(mvm);
1063         /* if EWRD is not available, we can still use WRDS, so don't fail */
1064         if (ret < 0)
1065                 IWL_DEBUG_RADIO(mvm,
1066                                 "EWRD SAR BIOS table invalid or unavailable. (%d)\n",
1067                                 ret);
1068
1069         /* choose profile 1 (WRDS) as default for both chains */
1070         ret = iwl_mvm_sar_select_profile(mvm, 1, 1);
1071
1072         /*
1073          * If we don't have profile 0 from BIOS, just skip it.  This
1074          * means that SAR Geo will not be enabled either, even if we
1075          * have other valid profiles.
1076          */
1077         if (ret == -ENOENT)
1078                 return 1;
1079
1080         return ret;
1081 }
1082
1083 static int iwl_mvm_load_rt_fw(struct iwl_mvm *mvm)
1084 {
1085         int ret;
1086
1087         if (iwl_mvm_has_unified_ucode(mvm))
1088                 return iwl_run_unified_mvm_ucode(mvm, false);
1089
1090         ret = iwl_run_init_mvm_ucode(mvm, false);
1091
1092         if (iwlmvm_mod_params.init_dbg)
1093                 return 0;
1094
1095         if (ret) {
1096                 IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
1097                 return ret;
1098         }
1099
1100         /*
1101          * Stop and start the transport without entering low power
1102          * mode. This will save the state of other components on the
1103          * device that are triggered by the INIT firwmare (MFUART).
1104          */
1105         _iwl_trans_stop_device(mvm->trans, false);
1106         ret = _iwl_trans_start_hw(mvm->trans, false);
1107         if (ret)
1108                 return ret;
1109
1110         ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
1111         if (ret)
1112                 return ret;
1113
1114         return iwl_init_paging(&mvm->fwrt, mvm->fwrt.cur_fw_img);
1115 }
1116
1117 int iwl_mvm_up(struct iwl_mvm *mvm)
1118 {
1119         int ret, i;
1120         struct ieee80211_channel *chan;
1121         struct cfg80211_chan_def chandef;
1122
1123         lockdep_assert_held(&mvm->mutex);
1124
1125         ret = iwl_trans_start_hw(mvm->trans);
1126         if (ret)
1127                 return ret;
1128
1129         ret = iwl_mvm_load_rt_fw(mvm);
1130         if (ret) {
1131                 IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
1132                 goto error;
1133         }
1134
1135         iwl_get_shared_mem_conf(&mvm->fwrt);
1136
1137         ret = iwl_mvm_sf_update(mvm, NULL, false);
1138         if (ret)
1139                 IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
1140
1141         mvm->fwrt.dump.conf = FW_DBG_INVALID;
1142         /* if we have a destination, assume EARLY START */
1143         if (mvm->fw->dbg_dest_tlv)
1144                 mvm->fwrt.dump.conf = FW_DBG_START_FROM_ALIVE;
1145         iwl_fw_start_dbg_conf(&mvm->fwrt, FW_DBG_START_FROM_ALIVE);
1146
1147         ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1148         if (ret)
1149                 goto error;
1150
1151         if (!iwl_mvm_has_unified_ucode(mvm)) {
1152                 /* Send phy db control command and then phy db calibration */
1153                 ret = iwl_send_phy_db_data(mvm->phy_db);
1154                 if (ret)
1155                         goto error;
1156
1157                 ret = iwl_send_phy_cfg_cmd(mvm);
1158                 if (ret)
1159                         goto error;
1160         }
1161
1162         ret = iwl_mvm_send_bt_init_conf(mvm);
1163         if (ret)
1164                 goto error;
1165
1166         /* Init RSS configuration */
1167         /* TODO - remove a000 disablement when we have RXQ config API */
1168         if (iwl_mvm_has_new_rx_api(mvm) &&
1169             mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_A000) {
1170                 ret = iwl_send_rss_cfg_cmd(mvm);
1171                 if (ret) {
1172                         IWL_ERR(mvm, "Failed to configure RSS queues: %d\n",
1173                                 ret);
1174                         goto error;
1175                 }
1176         }
1177
1178         /* init the fw <-> mac80211 STA mapping */
1179         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1180                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1181
1182         mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1183
1184         /* reset quota debouncing buffer - 0xff will yield invalid data */
1185         memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
1186
1187         ret = iwl_mvm_send_dqa_cmd(mvm);
1188         if (ret)
1189                 goto error;
1190
1191         /* Add auxiliary station for scanning */
1192         ret = iwl_mvm_add_aux_sta(mvm);
1193         if (ret)
1194                 goto error;
1195
1196         /* Add all the PHY contexts */
1197         chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
1198         cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
1199         for (i = 0; i < NUM_PHY_CTX; i++) {
1200                 /*
1201                  * The channel used here isn't relevant as it's
1202                  * going to be overwritten in the other flows.
1203                  * For now use the first channel we have.
1204                  */
1205                 ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
1206                                            &chandef, 1, 1);
1207                 if (ret)
1208                         goto error;
1209         }
1210
1211 #ifdef CONFIG_THERMAL
1212         if (iwl_mvm_is_tt_in_fw(mvm)) {
1213                 /* in order to give the responsibility of ct-kill and
1214                  * TX backoff to FW we need to send empty temperature reporting
1215                  * cmd during init time
1216                  */
1217                 iwl_mvm_send_temp_report_ths_cmd(mvm);
1218         } else {
1219                 /* Initialize tx backoffs to the minimal possible */
1220                 iwl_mvm_tt_tx_backoff(mvm, 0);
1221         }
1222
1223         /* TODO: read the budget from BIOS / Platform NVM */
1224
1225         /*
1226          * In case there is no budget from BIOS / Platform NVM the default
1227          * budget should be 2000mW (cooling state 0).
1228          */
1229         if (iwl_mvm_is_ctdp_supported(mvm)) {
1230                 ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
1231                                            mvm->cooling_dev.cur_state);
1232                 if (ret)
1233                         goto error;
1234         }
1235 #else
1236         /* Initialize tx backoffs to the minimal possible */
1237         iwl_mvm_tt_tx_backoff(mvm, 0);
1238 #endif
1239
1240         WARN_ON(iwl_mvm_config_ltr(mvm));
1241
1242         ret = iwl_mvm_power_update_device(mvm);
1243         if (ret)
1244                 goto error;
1245
1246         /*
1247          * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
1248          * anyway, so don't init MCC.
1249          */
1250         if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
1251                 ret = iwl_mvm_init_mcc(mvm);
1252                 if (ret)
1253                         goto error;
1254         }
1255
1256         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1257                 mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1258                 ret = iwl_mvm_config_scan(mvm);
1259                 if (ret)
1260                         goto error;
1261         }
1262
1263         /* allow FW/transport low power modes if not during restart */
1264         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1265                 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1266
1267         ret = iwl_mvm_sar_init(mvm);
1268         if (ret == 0) {
1269                 ret = iwl_mvm_sar_geo_init(mvm);
1270         } else if (ret > 0 && !iwl_mvm_sar_get_wgds_table(mvm)) {
1271                 /*
1272                  * If basic SAR is not available, we check for WGDS,
1273                  * which should *not* be available either.  If it is
1274                  * available, issue an error, because we can't use SAR
1275                  * Geo without basic SAR.
1276                  */
1277                 IWL_ERR(mvm, "BIOS contains WGDS but no WRDS\n");
1278         }
1279
1280         if (ret < 0)
1281                 goto error;
1282
1283         iwl_mvm_leds_sync(mvm);
1284
1285         IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
1286         return 0;
1287  error:
1288         if (!iwlmvm_mod_params.init_dbg)
1289                 iwl_mvm_stop_device(mvm);
1290         return ret;
1291 }
1292
1293 int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
1294 {
1295         int ret, i;
1296
1297         lockdep_assert_held(&mvm->mutex);
1298
1299         ret = iwl_trans_start_hw(mvm->trans);
1300         if (ret)
1301                 return ret;
1302
1303         ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
1304         if (ret) {
1305                 IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
1306                 goto error;
1307         }
1308
1309         ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
1310         if (ret)
1311                 goto error;
1312
1313         /* Send phy db control command and then phy db calibration*/
1314         ret = iwl_send_phy_db_data(mvm->phy_db);
1315         if (ret)
1316                 goto error;
1317
1318         ret = iwl_send_phy_cfg_cmd(mvm);
1319         if (ret)
1320                 goto error;
1321
1322         /* init the fw <-> mac80211 STA mapping */
1323         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++)
1324                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
1325
1326         /* Add auxiliary station for scanning */
1327         ret = iwl_mvm_add_aux_sta(mvm);
1328         if (ret)
1329                 goto error;
1330
1331         return 0;
1332  error:
1333         iwl_mvm_stop_device(mvm);
1334         return ret;
1335 }
1336
1337 void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
1338                                  struct iwl_rx_cmd_buffer *rxb)
1339 {
1340         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1341         struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
1342         u32 flags = le32_to_cpu(card_state_notif->flags);
1343
1344         IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
1345                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1346                           (flags & SW_CARD_DISABLED) ? "Kill" : "On",
1347                           (flags & CT_KILL_CARD_DISABLED) ?
1348                           "Reached" : "Not reached");
1349 }
1350
1351 void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
1352                              struct iwl_rx_cmd_buffer *rxb)
1353 {
1354         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1355         struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
1356
1357         IWL_DEBUG_INFO(mvm,
1358                        "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
1359                        le32_to_cpu(mfuart_notif->installed_ver),
1360                        le32_to_cpu(mfuart_notif->external_ver),
1361                        le32_to_cpu(mfuart_notif->status),
1362                        le32_to_cpu(mfuart_notif->duration));
1363
1364         if (iwl_rx_packet_payload_len(pkt) == sizeof(*mfuart_notif))
1365                 IWL_DEBUG_INFO(mvm,
1366                                "MFUART: image size: 0x%08x\n",
1367                                le32_to_cpu(mfuart_notif->image_size));
1368 }