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