Merge git://github.com/Jkirsher/net-next
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-agn-tt.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *****************************************************************************/
28
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34
35 #include <net/mac80211.h>
36
37 #include "iwl-eeprom.h"
38 #include "iwl-dev.h"
39 #include "iwl-core.h"
40 #include "iwl-io.h"
41 #include "iwl-commands.h"
42 #include "iwl-debug.h"
43 #include "iwl-agn-tt.h"
44
45 /* default Thermal Throttling transaction table
46  * Current state   |         Throttling Down               |  Throttling Up
47  *=============================================================================
48  *                 Condition Nxt State  Condition Nxt State Condition Nxt State
49  *-----------------------------------------------------------------------------
50  *     IWL_TI_0     T >= 114   CT_KILL  114>T>=105   TI_1      N/A      N/A
51  *     IWL_TI_1     T >= 114   CT_KILL  114>T>=110   TI_2     T<=95     TI_0
52  *     IWL_TI_2     T >= 114   CT_KILL                        T<=100    TI_1
53  *    IWL_CT_KILL      N/A       N/A       N/A        N/A     T<=95     TI_0
54  *=============================================================================
55  */
56 static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
57         {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
58         {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
59         {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
60 };
61 static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
62         {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
63         {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
64         {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
65 };
66 static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
67         {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
68         {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
69         {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
70 };
71 static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
72         {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
73         {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
74         {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
75 };
76
77 /* Advance Thermal Throttling default restriction table */
78 static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
79         {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
80         {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
81         {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
82         {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
83 };
84
85 bool iwl_tt_is_low_power_state(struct iwl_priv *priv)
86 {
87         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
88
89         if (tt->state >= IWL_TI_1)
90                 return true;
91         return false;
92 }
93
94 u8 iwl_tt_current_power_mode(struct iwl_priv *priv)
95 {
96         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
97
98         return tt->tt_power_mode;
99 }
100
101 bool iwl_ht_enabled(struct iwl_priv *priv)
102 {
103         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
104         struct iwl_tt_restriction *restriction;
105
106         if (!priv->thermal_throttle.advanced_tt)
107                 return true;
108         restriction = tt->restriction + tt->state;
109         return restriction->is_ht;
110 }
111
112 static bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
113 {
114         s32 temp = priv->temperature; /* degrees CELSIUS except specified */
115         bool within_margin = false;
116
117         if (priv->cfg->base_params->temperature_kelvin)
118                 temp = KELVIN_TO_CELSIUS(priv->temperature);
119
120         if (!priv->thermal_throttle.advanced_tt)
121                 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
122                                 CT_KILL_THRESHOLD_LEGACY) ? true : false;
123         else
124                 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
125                                 CT_KILL_THRESHOLD) ? true : false;
126         return within_margin;
127 }
128
129 bool iwl_check_for_ct_kill(struct iwl_priv *priv)
130 {
131         bool is_ct_kill = false;
132
133         if (iwl_within_ct_kill_margin(priv)) {
134                 iwl_tt_enter_ct_kill(priv);
135                 is_ct_kill = true;
136         }
137         return is_ct_kill;
138 }
139
140 enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
141 {
142         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
143         struct iwl_tt_restriction *restriction;
144
145         if (!priv->thermal_throttle.advanced_tt)
146                 return IWL_ANT_OK_MULTI;
147         restriction = tt->restriction + tt->state;
148         return restriction->tx_stream;
149 }
150
151 enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
152 {
153         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
154         struct iwl_tt_restriction *restriction;
155
156         if (!priv->thermal_throttle.advanced_tt)
157                 return IWL_ANT_OK_MULTI;
158         restriction = tt->restriction + tt->state;
159         return restriction->rx_stream;
160 }
161
162 #define CT_KILL_EXIT_DURATION (5)       /* 5 seconds duration */
163 #define CT_KILL_WAITING_DURATION (300)  /* 300ms duration */
164
165 /*
166  * toggle the bit to wake up uCode and check the temperature
167  * if the temperature is below CT, uCode will stay awake and send card
168  * state notification with CT_KILL bit clear to inform Thermal Throttling
169  * Management to change state. Otherwise, uCode will go back to sleep
170  * without doing anything, driver should continue the 5 seconds timer
171  * to wake up uCode for temperature check until temperature drop below CT
172  */
173 static void iwl_tt_check_exit_ct_kill(unsigned long data)
174 {
175         struct iwl_priv *priv = (struct iwl_priv *)data;
176         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
177         unsigned long flags;
178
179         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
180                 return;
181
182         if (tt->state == IWL_TI_CT_KILL) {
183                 if (priv->thermal_throttle.ct_kill_toggle) {
184                         iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
185                                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
186                         priv->thermal_throttle.ct_kill_toggle = false;
187                 } else {
188                         iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
189                                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
190                         priv->thermal_throttle.ct_kill_toggle = true;
191                 }
192                 iwl_read32(bus(priv), CSR_UCODE_DRV_GP1);
193                 spin_lock_irqsave(&bus(priv)->reg_lock, flags);
194                 if (!iwl_grab_nic_access(bus(priv)))
195                         iwl_release_nic_access(bus(priv));
196                 spin_unlock_irqrestore(&bus(priv)->reg_lock, flags);
197
198                 /* Reschedule the ct_kill timer to occur in
199                  * CT_KILL_EXIT_DURATION seconds to ensure we get a
200                  * thermal update */
201                 IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
202                 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
203                           jiffies + CT_KILL_EXIT_DURATION * HZ);
204         }
205 }
206
207 static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
208                            bool stop)
209 {
210         if (stop) {
211                 IWL_DEBUG_TEMP(priv, "Stop all queues\n");
212                 if (priv->shrd->mac80211_registered)
213                         ieee80211_stop_queues(priv->hw);
214                 IWL_DEBUG_TEMP(priv,
215                                 "Schedule 5 seconds CT_KILL Timer\n");
216                 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
217                           jiffies + CT_KILL_EXIT_DURATION * HZ);
218         } else {
219                 IWL_DEBUG_TEMP(priv, "Wake all queues\n");
220                 if (priv->shrd->mac80211_registered)
221                         ieee80211_wake_queues(priv->hw);
222         }
223 }
224
225 static void iwl_tt_ready_for_ct_kill(unsigned long data)
226 {
227         struct iwl_priv *priv = (struct iwl_priv *)data;
228         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
229
230         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
231                 return;
232
233         /* temperature timer expired, ready to go into CT_KILL state */
234         if (tt->state != IWL_TI_CT_KILL) {
235                 IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
236                                 "temperature timer expired\n");
237                 tt->state = IWL_TI_CT_KILL;
238                 set_bit(STATUS_CT_KILL, &priv->shrd->status);
239                 iwl_perform_ct_kill_task(priv, true);
240         }
241 }
242
243 static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
244 {
245         IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
246         /* make request to retrieve statistics information */
247         iwl_send_statistics_request(priv, CMD_SYNC, false);
248         /* Reschedule the ct_kill wait timer */
249         mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
250                  jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
251 }
252
253 #define IWL_MINIMAL_POWER_THRESHOLD             (CT_KILL_THRESHOLD_LEGACY)
254 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2     (100)
255 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1     (90)
256
257 /*
258  * Legacy thermal throttling
259  * 1) Avoid NIC destruction due to high temperatures
260  *      Chip will identify dangerously high temperatures that can
261  *      harm the device and will power down
262  * 2) Avoid the NIC power down due to high temperature
263  *      Throttle early enough to lower the power consumption before
264  *      drastic steps are needed
265  */
266 static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
267 {
268         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
269         enum iwl_tt_state old_state;
270
271 #ifdef CONFIG_IWLWIFI_DEBUG
272         if ((tt->tt_previous_temp) &&
273             (temp > tt->tt_previous_temp) &&
274             ((temp - tt->tt_previous_temp) >
275             IWL_TT_INCREASE_MARGIN)) {
276                 IWL_DEBUG_TEMP(priv,
277                         "Temperature increase %d degree Celsius\n",
278                         (temp - tt->tt_previous_temp));
279         }
280 #endif
281         old_state = tt->state;
282         /* in Celsius */
283         if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
284                 tt->state = IWL_TI_CT_KILL;
285         else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
286                 tt->state = IWL_TI_2;
287         else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
288                 tt->state = IWL_TI_1;
289         else
290                 tt->state = IWL_TI_0;
291
292 #ifdef CONFIG_IWLWIFI_DEBUG
293         tt->tt_previous_temp = temp;
294 #endif
295         /* stop ct_kill_waiting_tm timer */
296         del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
297         if (tt->state != old_state) {
298                 switch (tt->state) {
299                 case IWL_TI_0:
300                         /*
301                          * When the system is ready to go back to IWL_TI_0
302                          * we only have to call iwl_power_update_mode() to
303                          * do so.
304                          */
305                         break;
306                 case IWL_TI_1:
307                         tt->tt_power_mode = IWL_POWER_INDEX_3;
308                         break;
309                 case IWL_TI_2:
310                         tt->tt_power_mode = IWL_POWER_INDEX_4;
311                         break;
312                 default:
313                         tt->tt_power_mode = IWL_POWER_INDEX_5;
314                         break;
315                 }
316                 mutex_lock(&priv->shrd->mutex);
317                 if (old_state == IWL_TI_CT_KILL)
318                         clear_bit(STATUS_CT_KILL, &priv->shrd->status);
319                 if (tt->state != IWL_TI_CT_KILL &&
320                     iwl_power_update_mode(priv, true)) {
321                         /* TT state not updated
322                          * try again during next temperature read
323                          */
324                         if (old_state == IWL_TI_CT_KILL)
325                                 set_bit(STATUS_CT_KILL, &priv->shrd->status);
326                         tt->state = old_state;
327                         IWL_ERR(priv, "Cannot update power mode, "
328                                         "TT state not updated\n");
329                 } else {
330                         if (tt->state == IWL_TI_CT_KILL) {
331                                 if (force) {
332                                         set_bit(STATUS_CT_KILL,
333                                                 &priv->shrd->status);
334                                         iwl_perform_ct_kill_task(priv, true);
335                                 } else {
336                                         iwl_prepare_ct_kill_task(priv);
337                                         tt->state = old_state;
338                                 }
339                         } else if (old_state == IWL_TI_CT_KILL &&
340                                  tt->state != IWL_TI_CT_KILL)
341                                 iwl_perform_ct_kill_task(priv, false);
342                         IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
343                                         tt->state);
344                         IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
345                                         tt->tt_power_mode);
346                 }
347                 mutex_unlock(&priv->shrd->mutex);
348         }
349 }
350
351 /*
352  * Advance thermal throttling
353  * 1) Avoid NIC destruction due to high temperatures
354  *      Chip will identify dangerously high temperatures that can
355  *      harm the device and will power down
356  * 2) Avoid the NIC power down due to high temperature
357  *      Throttle early enough to lower the power consumption before
358  *      drastic steps are needed
359  *      Actions include relaxing the power down sleep thresholds and
360  *      decreasing the number of TX streams
361  * 3) Avoid throughput performance impact as much as possible
362  *
363  *=============================================================================
364  *                 Condition Nxt State  Condition Nxt State Condition Nxt State
365  *-----------------------------------------------------------------------------
366  *     IWL_TI_0     T >= 114   CT_KILL  114>T>=105   TI_1      N/A      N/A
367  *     IWL_TI_1     T >= 114   CT_KILL  114>T>=110   TI_2     T<=95     TI_0
368  *     IWL_TI_2     T >= 114   CT_KILL                        T<=100    TI_1
369  *    IWL_CT_KILL      N/A       N/A       N/A        N/A     T<=95     TI_0
370  *=============================================================================
371  */
372 static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
373 {
374         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
375         int i;
376         bool changed = false;
377         enum iwl_tt_state old_state;
378         struct iwl_tt_trans *transaction;
379
380         old_state = tt->state;
381         for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
382                 /* based on the current TT state,
383                  * find the curresponding transaction table
384                  * each table has (IWL_TI_STATE_MAX - 1) entries
385                  * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
386                  * will advance to the correct table.
387                  * then based on the current temperature
388                  * find the next state need to transaction to
389                  * go through all the possible (IWL_TI_STATE_MAX - 1) entries
390                  * in the current table to see if transaction is needed
391                  */
392                 transaction = tt->transaction +
393                         ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
394                 if (temp >= transaction->tt_low &&
395                     temp <= transaction->tt_high) {
396 #ifdef CONFIG_IWLWIFI_DEBUG
397                         if ((tt->tt_previous_temp) &&
398                             (temp > tt->tt_previous_temp) &&
399                             ((temp - tt->tt_previous_temp) >
400                             IWL_TT_INCREASE_MARGIN)) {
401                                 IWL_DEBUG_TEMP(priv,
402                                         "Temperature increase %d "
403                                         "degree Celsius\n",
404                                         (temp - tt->tt_previous_temp));
405                         }
406                         tt->tt_previous_temp = temp;
407 #endif
408                         if (old_state !=
409                             transaction->next_state) {
410                                 changed = true;
411                                 tt->state =
412                                         transaction->next_state;
413                         }
414                         break;
415                 }
416         }
417         /* stop ct_kill_waiting_tm timer */
418         del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
419         if (changed) {
420                 if (tt->state >= IWL_TI_1) {
421                         /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
422                         tt->tt_power_mode = IWL_POWER_INDEX_5;
423
424                         if (!iwl_ht_enabled(priv)) {
425                                 struct iwl_rxon_context *ctx;
426
427                                 for_each_context(priv, ctx) {
428                                         struct iwl_rxon_cmd *rxon;
429
430                                         rxon = &ctx->staging;
431
432                                         /* disable HT */
433                                         rxon->flags &= ~(
434                                                 RXON_FLG_CHANNEL_MODE_MSK |
435                                                 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
436                                                 RXON_FLG_HT40_PROT_MSK |
437                                                 RXON_FLG_HT_PROT_MSK);
438                                 }
439                         } else {
440                                 /* check HT capability and set
441                                  * according to the system HT capability
442                                  * in case get disabled before */
443                                 iwl_set_rxon_ht(priv, &priv->current_ht_config);
444                         }
445
446                 } else {
447                         /*
448                          * restore system power setting -- it will be
449                          * recalculated automatically.
450                          */
451
452                         /* check HT capability and set
453                          * according to the system HT capability
454                          * in case get disabled before */
455                         iwl_set_rxon_ht(priv, &priv->current_ht_config);
456                 }
457                 mutex_lock(&priv->shrd->mutex);
458                 if (old_state == IWL_TI_CT_KILL)
459                         clear_bit(STATUS_CT_KILL, &priv->shrd->status);
460                 if (tt->state != IWL_TI_CT_KILL &&
461                     iwl_power_update_mode(priv, true)) {
462                         /* TT state not updated
463                          * try again during next temperature read
464                          */
465                         IWL_ERR(priv, "Cannot update power mode, "
466                                         "TT state not updated\n");
467                         if (old_state == IWL_TI_CT_KILL)
468                                 set_bit(STATUS_CT_KILL, &priv->shrd->status);
469                         tt->state = old_state;
470                 } else {
471                         IWL_DEBUG_TEMP(priv,
472                                         "Thermal Throttling to new state: %u\n",
473                                         tt->state);
474                         if (old_state != IWL_TI_CT_KILL &&
475                             tt->state == IWL_TI_CT_KILL) {
476                                 if (force) {
477                                         IWL_DEBUG_TEMP(priv,
478                                                 "Enter IWL_TI_CT_KILL\n");
479                                         set_bit(STATUS_CT_KILL,
480                                                 &priv->shrd->status);
481                                         iwl_perform_ct_kill_task(priv, true);
482                                 } else {
483                                         iwl_prepare_ct_kill_task(priv);
484                                         tt->state = old_state;
485                                 }
486                         } else if (old_state == IWL_TI_CT_KILL &&
487                                   tt->state != IWL_TI_CT_KILL) {
488                                 IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
489                                 iwl_perform_ct_kill_task(priv, false);
490                         }
491                 }
492                 mutex_unlock(&priv->shrd->mutex);
493         }
494 }
495
496 /* Card State Notification indicated reach critical temperature
497  * if PSP not enable, no Thermal Throttling function will be performed
498  * just set the GP1 bit to acknowledge the event
499  * otherwise, go into IWL_TI_CT_KILL state
500  * since Card State Notification will not provide any temperature reading
501  * for Legacy mode
502  * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
503  * for advance mode
504  * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
505  */
506 static void iwl_bg_ct_enter(struct work_struct *work)
507 {
508         struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
509         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
510
511         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
512                 return;
513
514         if (!iwl_is_ready(priv->shrd))
515                 return;
516
517         if (tt->state != IWL_TI_CT_KILL) {
518                 IWL_ERR(priv, "Device reached critical temperature "
519                               "- ucode going to sleep!\n");
520                 if (!priv->thermal_throttle.advanced_tt)
521                         iwl_legacy_tt_handler(priv,
522                                               IWL_MINIMAL_POWER_THRESHOLD,
523                                               true);
524                 else
525                         iwl_advance_tt_handler(priv,
526                                                CT_KILL_THRESHOLD + 1, true);
527         }
528 }
529
530 /* Card State Notification indicated out of critical temperature
531  * since Card State Notification will not provide any temperature reading
532  * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
533  * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
534  */
535 static void iwl_bg_ct_exit(struct work_struct *work)
536 {
537         struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
538         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
539
540         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
541                 return;
542
543         if (!iwl_is_ready(priv->shrd))
544                 return;
545
546         /* stop ct_kill_exit_tm timer */
547         del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
548
549         if (tt->state == IWL_TI_CT_KILL) {
550                 IWL_ERR(priv,
551                         "Device temperature below critical"
552                         "- ucode awake!\n");
553                 /*
554                  * exit from CT_KILL state
555                  * reset the current temperature reading
556                  */
557                 priv->temperature = 0;
558                 if (!priv->thermal_throttle.advanced_tt)
559                         iwl_legacy_tt_handler(priv,
560                                       IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
561                                       true);
562                 else
563                         iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
564                                                true);
565         }
566 }
567
568 void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
569 {
570         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
571                 return;
572
573         IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
574         queue_work(priv->shrd->workqueue, &priv->ct_enter);
575 }
576
577 void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
578 {
579         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
580                 return;
581
582         IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
583         queue_work(priv->shrd->workqueue, &priv->ct_exit);
584 }
585
586 static void iwl_bg_tt_work(struct work_struct *work)
587 {
588         struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
589         s32 temp = priv->temperature; /* degrees CELSIUS except specified */
590
591         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
592                 return;
593
594         if (priv->cfg->base_params->temperature_kelvin)
595                 temp = KELVIN_TO_CELSIUS(priv->temperature);
596
597         if (!priv->thermal_throttle.advanced_tt)
598                 iwl_legacy_tt_handler(priv, temp, false);
599         else
600                 iwl_advance_tt_handler(priv, temp, false);
601 }
602
603 void iwl_tt_handler(struct iwl_priv *priv)
604 {
605         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
606                 return;
607
608         IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
609         queue_work(priv->shrd->workqueue, &priv->tt_work);
610 }
611
612 /* Thermal throttling initialization
613  * For advance thermal throttling:
614  *     Initialize Thermal Index and temperature threshold table
615  *     Initialize thermal throttling restriction table
616  */
617 void iwl_tt_initialize(struct iwl_priv *priv)
618 {
619         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
620         int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
621         struct iwl_tt_trans *transaction;
622
623         IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
624
625         memset(tt, 0, sizeof(struct iwl_tt_mgmt));
626
627         tt->state = IWL_TI_0;
628         init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
629         priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
630         priv->thermal_throttle.ct_kill_exit_tm.function =
631                 iwl_tt_check_exit_ct_kill;
632         init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
633         priv->thermal_throttle.ct_kill_waiting_tm.data =
634                 (unsigned long)priv;
635         priv->thermal_throttle.ct_kill_waiting_tm.function =
636                 iwl_tt_ready_for_ct_kill;
637         /* setup deferred ct kill work */
638         INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
639         INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
640         INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
641
642         if (priv->cfg->base_params->adv_thermal_throttle) {
643                 IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
644                 tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
645                                          IWL_TI_STATE_MAX, GFP_KERNEL);
646                 tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
647                         IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
648                         GFP_KERNEL);
649                 if (!tt->restriction || !tt->transaction) {
650                         IWL_ERR(priv, "Fallback to Legacy Throttling\n");
651                         priv->thermal_throttle.advanced_tt = false;
652                         kfree(tt->restriction);
653                         tt->restriction = NULL;
654                         kfree(tt->transaction);
655                         tt->transaction = NULL;
656                 } else {
657                         transaction = tt->transaction +
658                                 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
659                         memcpy(transaction, &tt_range_0[0], size);
660                         transaction = tt->transaction +
661                                 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
662                         memcpy(transaction, &tt_range_1[0], size);
663                         transaction = tt->transaction +
664                                 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
665                         memcpy(transaction, &tt_range_2[0], size);
666                         transaction = tt->transaction +
667                                 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
668                         memcpy(transaction, &tt_range_3[0], size);
669                         size = sizeof(struct iwl_tt_restriction) *
670                                 IWL_TI_STATE_MAX;
671                         memcpy(tt->restriction,
672                                 &restriction_range[0], size);
673                         priv->thermal_throttle.advanced_tt = true;
674                 }
675         } else {
676                 IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
677                 priv->thermal_throttle.advanced_tt = false;
678         }
679 }
680
681 /* cleanup thermal throttling management related memory and timer */
682 void iwl_tt_exit(struct iwl_priv *priv)
683 {
684         struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
685
686         /* stop ct_kill_exit_tm timer if activated */
687         del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
688         /* stop ct_kill_waiting_tm timer if activated */
689         del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
690         cancel_work_sync(&priv->tt_work);
691         cancel_work_sync(&priv->ct_enter);
692         cancel_work_sync(&priv->ct_exit);
693
694         if (priv->thermal_throttle.advanced_tt) {
695                 /* free advance thermal throttling memory */
696                 kfree(tt->restriction);
697                 tt->restriction = NULL;
698                 kfree(tt->transaction);
699                 tt->transaction = NULL;
700         }
701 }