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