Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / 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 - 2013 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <net/mac80211.h>
64
65 #include "iwl-trans.h"
66 #include "iwl-op-mode.h"
67 #include "iwl-fw.h"
68 #include "iwl-debug.h"
69 #include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
70 #include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
71 #include "iwl-eeprom-parse.h"
72
73 #include "mvm.h"
74 #include "iwl-phy-db.h"
75
76 #define MVM_UCODE_ALIVE_TIMEOUT HZ
77 #define MVM_UCODE_CALIB_TIMEOUT (2*HZ)
78
79 #define UCODE_VALID_OK  cpu_to_le32(0x1)
80
81 /* Default calibration values for WkP - set to INIT image w/o running */
82 static const u8 wkp_calib_values_rx_iq_skew[] = { 0x00, 0x00, 0x01, 0x00 };
83 static const u8 wkp_calib_values_tx_iq_skew[] = { 0x01, 0x00, 0x00, 0x00 };
84
85 struct iwl_calib_default_data {
86         u16 size;
87         void *data;
88 };
89
90 #define CALIB_SIZE_N_DATA(_buf) {.size = sizeof(_buf), .data = &_buf}
91
92 static const struct iwl_calib_default_data wkp_calib_default_data[12] = {
93         [9] = CALIB_SIZE_N_DATA(wkp_calib_values_tx_iq_skew),
94         [11] = CALIB_SIZE_N_DATA(wkp_calib_values_rx_iq_skew),
95 };
96
97 struct iwl_mvm_alive_data {
98         bool valid;
99         u32 scd_base_addr;
100 };
101
102 static inline const struct fw_img *
103 iwl_get_ucode_image(struct iwl_mvm *mvm, enum iwl_ucode_type ucode_type)
104 {
105         if (ucode_type >= IWL_UCODE_TYPE_MAX)
106                 return NULL;
107
108         return &mvm->fw->img[ucode_type];
109 }
110
111 static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
112 {
113         struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
114                 .valid = cpu_to_le32(valid_tx_ant),
115         };
116
117         IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
118         return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, CMD_SYNC,
119                                     sizeof(tx_ant_cmd), &tx_ant_cmd);
120 }
121
122 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
123                          struct iwl_rx_packet *pkt, void *data)
124 {
125         struct iwl_mvm *mvm =
126                 container_of(notif_wait, struct iwl_mvm, notif_wait);
127         struct iwl_mvm_alive_data *alive_data = data;
128         struct mvm_alive_resp *palive;
129
130         palive = (void *)pkt->data;
131
132         mvm->error_event_table = le32_to_cpu(palive->error_event_table_ptr);
133         mvm->log_event_table = le32_to_cpu(palive->log_event_table_ptr);
134         alive_data->scd_base_addr = le32_to_cpu(palive->scd_base_ptr);
135
136         alive_data->valid = le16_to_cpu(palive->status) == IWL_ALIVE_STATUS_OK;
137         IWL_DEBUG_FW(mvm,
138                      "Alive ucode status 0x%04x revision 0x%01X 0x%01X flags 0x%01X\n",
139                      le16_to_cpu(palive->status), palive->ver_type,
140                      palive->ver_subtype, palive->flags);
141
142         return true;
143 }
144
145 static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
146                                   struct iwl_rx_packet *pkt, void *data)
147 {
148         struct iwl_phy_db *phy_db = data;
149
150         if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
151                 WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
152                 return true;
153         }
154
155         WARN_ON(iwl_phy_db_set_section(phy_db, pkt, GFP_ATOMIC));
156
157         return false;
158 }
159
160 static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
161                                          enum iwl_ucode_type ucode_type)
162 {
163         struct iwl_notification_wait alive_wait;
164         struct iwl_mvm_alive_data alive_data;
165         const struct fw_img *fw;
166         int ret, i;
167         enum iwl_ucode_type old_type = mvm->cur_ucode;
168         static const u8 alive_cmd[] = { MVM_ALIVE };
169
170         mvm->cur_ucode = ucode_type;
171         fw = iwl_get_ucode_image(mvm, ucode_type);
172
173         mvm->ucode_loaded = false;
174
175         if (!fw)
176                 return -EINVAL;
177
178         iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
179                                    alive_cmd, ARRAY_SIZE(alive_cmd),
180                                    iwl_alive_fn, &alive_data);
181
182         ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
183         if (ret) {
184                 mvm->cur_ucode = old_type;
185                 iwl_remove_notification(&mvm->notif_wait, &alive_wait);
186                 return ret;
187         }
188
189         /*
190          * Some things may run in the background now, but we
191          * just wait for the ALIVE notification here.
192          */
193         ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
194                                     MVM_UCODE_ALIVE_TIMEOUT);
195         if (ret) {
196                 mvm->cur_ucode = old_type;
197                 return ret;
198         }
199
200         if (!alive_data.valid) {
201                 IWL_ERR(mvm, "Loaded ucode is not valid!\n");
202                 mvm->cur_ucode = old_type;
203                 return -EIO;
204         }
205
206         iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
207
208         /*
209          * Note: all the queues are enabled as part of the interface
210          * initialization, but in firmware restart scenarios they
211          * could be stopped, so wake them up. In firmware restart,
212          * mac80211 will have the queues stopped as well until the
213          * reconfiguration completes. During normal startup, they
214          * will be empty.
215          */
216
217         for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
218                 if (i < IWL_MVM_FIRST_AGG_QUEUE && i != IWL_MVM_CMD_QUEUE)
219                         mvm->queue_to_mac80211[i] = i;
220                 else
221                         mvm->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
222                 atomic_set(&mvm->queue_stop_count[i], 0);
223         }
224
225         mvm->transport_queue_stop = 0;
226
227         mvm->ucode_loaded = true;
228
229         return 0;
230 }
231
232 static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
233 {
234         struct iwl_phy_cfg_cmd phy_cfg_cmd;
235         enum iwl_ucode_type ucode_type = mvm->cur_ucode;
236
237         /* Set parameters */
238         phy_cfg_cmd.phy_cfg = cpu_to_le32(mvm->fw->phy_config);
239         phy_cfg_cmd.calib_control.event_trigger =
240                 mvm->fw->default_calib[ucode_type].event_trigger;
241         phy_cfg_cmd.calib_control.flow_trigger =
242                 mvm->fw->default_calib[ucode_type].flow_trigger;
243
244         IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
245                        phy_cfg_cmd.phy_cfg);
246
247         return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, CMD_SYNC,
248                                     sizeof(phy_cfg_cmd), &phy_cfg_cmd);
249 }
250
251 static int iwl_set_default_calibrations(struct iwl_mvm *mvm)
252 {
253         u8 cmd_raw[16]; /* holds the variable size commands */
254         struct iwl_set_calib_default_cmd *cmd =
255                 (struct iwl_set_calib_default_cmd *)cmd_raw;
256         int ret, i;
257
258         /* Setting default values for calibrations we don't run */
259         for (i = 0; i < ARRAY_SIZE(wkp_calib_default_data); i++) {
260                 u16 cmd_len;
261
262                 if (wkp_calib_default_data[i].size == 0)
263                         continue;
264
265                 memset(cmd_raw, 0, sizeof(cmd_raw));
266                 cmd_len = wkp_calib_default_data[i].size + sizeof(cmd);
267                 cmd->calib_index = cpu_to_le16(i);
268                 cmd->length = cpu_to_le16(wkp_calib_default_data[i].size);
269                 if (WARN_ONCE(cmd_len > sizeof(cmd_raw),
270                               "Need to enlarge cmd_raw to %d\n", cmd_len))
271                         break;
272                 memcpy(cmd->data, wkp_calib_default_data[i].data,
273                        wkp_calib_default_data[i].size);
274                 ret = iwl_mvm_send_cmd_pdu(mvm, SET_CALIB_DEFAULT_CMD, 0,
275                                            sizeof(*cmd) +
276                                            wkp_calib_default_data[i].size,
277                                            cmd);
278                 if (ret)
279                         return ret;
280         }
281
282         return 0;
283 }
284
285 int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
286 {
287         struct iwl_notification_wait calib_wait;
288         static const u8 init_complete[] = {
289                 INIT_COMPLETE_NOTIF,
290                 CALIB_RES_NOTIF_PHY_DB
291         };
292         int ret;
293
294         lockdep_assert_held(&mvm->mutex);
295
296         if (mvm->init_ucode_run)
297                 return 0;
298
299         iwl_init_notification_wait(&mvm->notif_wait,
300                                    &calib_wait,
301                                    init_complete,
302                                    ARRAY_SIZE(init_complete),
303                                    iwl_wait_phy_db_entry,
304                                    mvm->phy_db);
305
306         /* Will also start the device */
307         ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
308         if (ret) {
309                 IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
310                 goto error;
311         }
312
313         ret = iwl_send_bt_prio_tbl(mvm);
314         if (ret)
315                 goto error;
316
317         if (read_nvm) {
318                 /* Read nvm */
319                 ret = iwl_nvm_init(mvm);
320                 if (ret) {
321                         IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
322                         goto error;
323                 }
324         }
325
326         ret = iwl_nvm_check_version(mvm->nvm_data, mvm->trans);
327         WARN_ON(ret);
328
329         /* Send TX valid antennas before triggering calibrations */
330         ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
331         if (ret)
332                 goto error;
333
334         /* need to set default values */
335         ret = iwl_set_default_calibrations(mvm);
336         if (ret)
337                 goto error;
338
339         /*
340          * Send phy configurations command to init uCode
341          * to start the 16.0 uCode init image internal calibrations.
342          */
343         ret = iwl_send_phy_cfg_cmd(mvm);
344         if (ret) {
345                 IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
346                         ret);
347                 goto error;
348         }
349
350         /*
351          * Some things may run in the background now, but we
352          * just wait for the calibration complete notification.
353          */
354         ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
355                         MVM_UCODE_CALIB_TIMEOUT);
356         if (!ret)
357                 mvm->init_ucode_run = true;
358         goto out;
359
360 error:
361         iwl_remove_notification(&mvm->notif_wait, &calib_wait);
362 out:
363         if (!iwlmvm_mod_params.init_dbg) {
364                 iwl_trans_stop_device(mvm->trans);
365         } else if (!mvm->nvm_data) {
366                 /* we want to debug INIT and we have no NVM - fake */
367                 mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
368                                         sizeof(struct ieee80211_channel) +
369                                         sizeof(struct ieee80211_rate),
370                                         GFP_KERNEL);
371                 if (!mvm->nvm_data)
372                         return -ENOMEM;
373                 mvm->nvm_data->valid_rx_ant = 1;
374                 mvm->nvm_data->valid_tx_ant = 1;
375                 mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
376                 mvm->nvm_data->bands[0].n_channels = 1;
377                 mvm->nvm_data->bands[0].n_bitrates = 1;
378                 mvm->nvm_data->bands[0].bitrates =
379                         (void *)mvm->nvm_data->channels + 1;
380                 mvm->nvm_data->bands[0].bitrates->hw_value = 10;
381         }
382
383         return ret;
384 }
385
386 #define UCODE_CALIB_TIMEOUT     (2*HZ)
387
388 int iwl_mvm_up(struct iwl_mvm *mvm)
389 {
390         int ret, i;
391
392         lockdep_assert_held(&mvm->mutex);
393
394         ret = iwl_trans_start_hw(mvm->trans);
395         if (ret)
396                 return ret;
397
398         /* If we were in RFKILL during module loading, load init ucode now */
399         if (!mvm->init_ucode_run) {
400                 ret = iwl_run_init_mvm_ucode(mvm, false);
401                 if (ret && !iwlmvm_mod_params.init_dbg) {
402                         IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
403                         goto error;
404                 }
405         }
406
407         if (iwlmvm_mod_params.init_dbg)
408                 return 0;
409
410         ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
411         if (ret) {
412                 IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
413                 goto error;
414         }
415
416         ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
417         if (ret)
418                 goto error;
419
420         ret = iwl_send_bt_prio_tbl(mvm);
421         if (ret)
422                 goto error;
423
424         ret = iwl_send_bt_init_conf(mvm);
425         if (ret)
426                 goto error;
427
428         /* Send phy db control command and then phy db calibration*/
429         ret = iwl_send_phy_db_data(mvm->phy_db);
430         if (ret)
431                 goto error;
432
433         ret = iwl_send_phy_cfg_cmd(mvm);
434         if (ret)
435                 goto error;
436
437         /* init the fw <-> mac80211 STA mapping */
438         for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
439                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
440
441         /* Add auxiliary station for scanning */
442         ret = iwl_mvm_add_aux_sta(mvm);
443         if (ret)
444                 goto error;
445
446         IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
447
448         return 0;
449  error:
450         iwl_trans_stop_device(mvm->trans);
451         return ret;
452 }
453
454 int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
455 {
456         int ret, i;
457
458         lockdep_assert_held(&mvm->mutex);
459
460         ret = iwl_trans_start_hw(mvm->trans);
461         if (ret)
462                 return ret;
463
464         ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
465         if (ret) {
466                 IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
467                 goto error;
468         }
469
470         ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
471         if (ret)
472                 goto error;
473
474         /* Send phy db control command and then phy db calibration*/
475         ret = iwl_send_phy_db_data(mvm->phy_db);
476         if (ret)
477                 goto error;
478
479         ret = iwl_send_phy_cfg_cmd(mvm);
480         if (ret)
481                 goto error;
482
483         /* init the fw <-> mac80211 STA mapping */
484         for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
485                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
486
487         /* Add auxiliary station for scanning */
488         ret = iwl_mvm_add_aux_sta(mvm);
489         if (ret)
490                 goto error;
491
492         return 0;
493  error:
494         iwl_trans_stop_device(mvm->trans);
495         return ret;
496 }
497
498 int iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
499                                     struct iwl_rx_cmd_buffer *rxb,
500                                     struct iwl_device_cmd *cmd)
501 {
502         struct iwl_rx_packet *pkt = rxb_addr(rxb);
503         struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
504         u32 flags = le32_to_cpu(card_state_notif->flags);
505
506         IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
507                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
508                           (flags & SW_CARD_DISABLED) ? "Kill" : "On",
509                           (flags & CT_KILL_CARD_DISABLED) ?
510                           "Reached" : "Not reached");
511
512         return 0;
513 }
514
515 int iwl_mvm_rx_radio_ver(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
516                          struct iwl_device_cmd *cmd)
517 {
518         struct iwl_rx_packet *pkt = rxb_addr(rxb);
519         struct iwl_radio_version_notif *radio_version = (void *)pkt->data;
520
521         /* TODO: what to do with that? */
522         IWL_DEBUG_INFO(mvm,
523                        "Radio version: flavor: 0x%08x, step 0x%08x, dash 0x%08x\n",
524                        le32_to_cpu(radio_version->radio_flavor),
525                        le32_to_cpu(radio_version->radio_step),
526                        le32_to_cpu(radio_version->radio_dash));
527         return 0;
528 }