Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / tt.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) 2013 - 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
66 #include "mvm.h"
67
68 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
69
70 static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
71 {
72         u32 duration = mvm->thermal_throttle.params->ct_kill_duration;
73
74         if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
75                 return;
76
77         IWL_ERR(mvm, "Enter CT Kill\n");
78         iwl_mvm_set_hw_ctkill_state(mvm, true);
79
80         /* Don't schedule an exit work if we're in test mode, since
81          * the temperature will not change unless we manually set it
82          * again (or disable testing).
83          */
84         if (!mvm->temperature_test)
85                 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
86                                       round_jiffies_relative(duration * HZ));
87 }
88
89 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
90 {
91         if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
92                 return;
93
94         IWL_ERR(mvm, "Exit CT Kill\n");
95         iwl_mvm_set_hw_ctkill_state(mvm, false);
96 }
97
98 static bool iwl_mvm_temp_notif(struct iwl_notif_wait_data *notif_wait,
99                                struct iwl_rx_packet *pkt, void *data)
100 {
101         struct iwl_mvm *mvm =
102                 container_of(notif_wait, struct iwl_mvm, notif_wait);
103         int *temp = data;
104         struct iwl_dts_measurement_notif *notif;
105         int len = iwl_rx_packet_payload_len(pkt);
106
107         if (WARN_ON_ONCE(len != sizeof(*notif))) {
108                 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
109                 return true;
110         }
111
112         notif = (void *)pkt->data;
113
114         *temp = le32_to_cpu(notif->temp);
115
116         /* shouldn't be negative, but since it's s32, make sure it isn't */
117         if (WARN_ON_ONCE(*temp < 0))
118                 *temp = 0;
119
120         IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", *temp);
121         return true;
122 }
123
124 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
125 {
126         struct iwl_dts_measurement_cmd cmd = {
127                 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
128         };
129
130         return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
131                                     sizeof(cmd), &cmd);
132 }
133
134 int iwl_mvm_get_temp(struct iwl_mvm *mvm)
135 {
136         struct iwl_notification_wait wait_temp_notif;
137         static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
138         int ret, temp;
139
140         lockdep_assert_held(&mvm->mutex);
141
142         iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
143                                    temp_notif, ARRAY_SIZE(temp_notif),
144                                    iwl_mvm_temp_notif, &temp);
145
146         ret = iwl_mvm_get_temp_cmd(mvm);
147         if (ret) {
148                 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
149                 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
150                 return ret;
151         }
152
153         ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
154                                     IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
155         if (ret) {
156                 IWL_ERR(mvm, "Getting the temperature timed out\n");
157                 return ret;
158         }
159
160         return temp;
161 }
162
163 static void check_exit_ctkill(struct work_struct *work)
164 {
165         struct iwl_mvm_tt_mgmt *tt;
166         struct iwl_mvm *mvm;
167         u32 duration;
168         s32 temp;
169
170         tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
171         mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
172
173         duration = tt->params->ct_kill_duration;
174
175         mutex_lock(&mvm->mutex);
176
177         if (__iwl_mvm_mac_start(mvm))
178                 goto reschedule;
179
180         /* make sure the device is available for direct read/writes */
181         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
182                 __iwl_mvm_mac_stop(mvm);
183                 goto reschedule;
184         }
185
186         temp = iwl_mvm_get_temp(mvm);
187
188         iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
189
190         __iwl_mvm_mac_stop(mvm);
191
192         if (temp < 0)
193                 goto reschedule;
194
195         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
196
197         if (temp <= tt->params->ct_kill_exit) {
198                 mutex_unlock(&mvm->mutex);
199                 iwl_mvm_exit_ctkill(mvm);
200                 return;
201         }
202
203 reschedule:
204         mutex_unlock(&mvm->mutex);
205         schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
206                               round_jiffies(duration * HZ));
207 }
208
209 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
210                                      struct ieee80211_vif *vif)
211 {
212         struct iwl_mvm *mvm = _data;
213         enum ieee80211_smps_mode smps_mode;
214
215         lockdep_assert_held(&mvm->mutex);
216
217         if (mvm->thermal_throttle.dynamic_smps)
218                 smps_mode = IEEE80211_SMPS_DYNAMIC;
219         else
220                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
221
222         if (vif->type != NL80211_IFTYPE_STATION)
223                 return;
224
225         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
226 }
227
228 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
229 {
230         struct ieee80211_sta *sta;
231         struct iwl_mvm_sta *mvmsta;
232         int i, err;
233
234         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
235                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
236                                                 lockdep_is_held(&mvm->mutex));
237                 if (IS_ERR_OR_NULL(sta))
238                         continue;
239                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
240                 if (enable == mvmsta->tt_tx_protection)
241                         continue;
242                 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
243                 if (err) {
244                         IWL_ERR(mvm, "Failed to %s Tx protection\n",
245                                 enable ? "enable" : "disable");
246                 } else {
247                         IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
248                                        enable ? "Enable" : "Disable");
249                         mvmsta->tt_tx_protection = enable;
250                 }
251         }
252 }
253
254 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
255 {
256         struct iwl_host_cmd cmd = {
257                 .id = REPLY_THERMAL_MNG_BACKOFF,
258                 .len = { sizeof(u32), },
259                 .data = { &backoff, },
260         };
261
262         backoff = max(backoff, mvm->thermal_throttle.min_backoff);
263
264         if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
265                 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
266                                backoff);
267                 mvm->thermal_throttle.tx_backoff = backoff;
268         } else {
269                 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
270         }
271 }
272
273 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
274 {
275         const struct iwl_tt_params *params = mvm->thermal_throttle.params;
276         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
277         s32 temperature = mvm->temperature;
278         bool throttle_enable = false;
279         int i;
280         u32 tx_backoff;
281
282         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
283
284         if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
285                 iwl_mvm_enter_ctkill(mvm);
286                 return;
287         }
288
289         if (params->support_ct_kill &&
290             temperature <= tt->params->ct_kill_exit) {
291                 iwl_mvm_exit_ctkill(mvm);
292                 return;
293         }
294
295         if (params->support_dynamic_smps) {
296                 if (!tt->dynamic_smps &&
297                     temperature >= params->dynamic_smps_entry) {
298                         IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
299                         tt->dynamic_smps = true;
300                         ieee80211_iterate_active_interfaces_atomic(
301                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
302                                         iwl_mvm_tt_smps_iterator, mvm);
303                         throttle_enable = true;
304                 } else if (tt->dynamic_smps &&
305                            temperature <= params->dynamic_smps_exit) {
306                         IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
307                         tt->dynamic_smps = false;
308                         ieee80211_iterate_active_interfaces_atomic(
309                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
310                                         iwl_mvm_tt_smps_iterator, mvm);
311                 }
312         }
313
314         if (params->support_tx_protection) {
315                 if (temperature >= params->tx_protection_entry) {
316                         iwl_mvm_tt_tx_protection(mvm, true);
317                         throttle_enable = true;
318                 } else if (temperature <= params->tx_protection_exit) {
319                         iwl_mvm_tt_tx_protection(mvm, false);
320                 }
321         }
322
323         if (params->support_tx_backoff) {
324                 tx_backoff = tt->min_backoff;
325                 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
326                         if (temperature < params->tx_backoff[i].temperature)
327                                 break;
328                         tx_backoff = max(tt->min_backoff,
329                                          params->tx_backoff[i].backoff);
330                 }
331                 if (tx_backoff != tt->min_backoff)
332                         throttle_enable = true;
333                 if (tt->tx_backoff != tx_backoff)
334                         iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
335         }
336
337         if (!tt->throttle && throttle_enable) {
338                 IWL_WARN(mvm,
339                          "Due to high temperature thermal throttling initiated\n");
340                 tt->throttle = true;
341         } else if (tt->throttle && !tt->dynamic_smps &&
342                    tt->tx_backoff == tt->min_backoff &&
343                    temperature <= params->tx_protection_exit) {
344                 IWL_WARN(mvm,
345                          "Temperature is back to normal thermal throttling stopped\n");
346                 tt->throttle = false;
347         }
348 }
349
350 static const struct iwl_tt_params iwl7000_tt_params = {
351         .ct_kill_entry = 118,
352         .ct_kill_exit = 96,
353         .ct_kill_duration = 5,
354         .dynamic_smps_entry = 114,
355         .dynamic_smps_exit = 110,
356         .tx_protection_entry = 114,
357         .tx_protection_exit = 108,
358         .tx_backoff = {
359                 {.temperature = 112, .backoff = 200},
360                 {.temperature = 113, .backoff = 600},
361                 {.temperature = 114, .backoff = 1200},
362                 {.temperature = 115, .backoff = 2000},
363                 {.temperature = 116, .backoff = 4000},
364                 {.temperature = 117, .backoff = 10000},
365         },
366         .support_ct_kill = true,
367         .support_dynamic_smps = true,
368         .support_tx_protection = true,
369         .support_tx_backoff = true,
370 };
371
372 static const struct iwl_tt_params iwl7000_high_temp_tt_params = {
373         .ct_kill_entry = 118,
374         .ct_kill_exit = 96,
375         .ct_kill_duration = 5,
376         .dynamic_smps_entry = 114,
377         .dynamic_smps_exit = 110,
378         .tx_protection_entry = 114,
379         .tx_protection_exit = 108,
380         .tx_backoff = {
381                 {.temperature = 112, .backoff = 300},
382                 {.temperature = 113, .backoff = 800},
383                 {.temperature = 114, .backoff = 1500},
384                 {.temperature = 115, .backoff = 3000},
385                 {.temperature = 116, .backoff = 5000},
386                 {.temperature = 117, .backoff = 10000},
387         },
388         .support_ct_kill = true,
389         .support_dynamic_smps = true,
390         .support_tx_protection = true,
391         .support_tx_backoff = true,
392 };
393
394 void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
395 {
396         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
397
398         IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
399
400         if (mvm->cfg->high_temp)
401                 tt->params = &iwl7000_high_temp_tt_params;
402         else
403                 tt->params = &iwl7000_tt_params;
404
405         tt->throttle = false;
406         tt->min_backoff = min_backoff;
407         INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
408 }
409
410 void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
411 {
412         cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
413         IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
414 }