]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/mvm/tt.c
Merge branch 'for-linus-4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[karo-tx-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  * Copyright(c) 2015 Intel Deutschland GmbH
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  *
43  *  * Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  *  * Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in
47  *    the documentation and/or other materials provided with the
48  *    distribution.
49  *  * Neither the name Intel Corporation nor the names of its
50  *    contributors may be used to endorse or promote products derived
51  *    from this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
63  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  *****************************************************************************/
66
67 #include "mvm.h"
68
69 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
70
71 static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
72 {
73         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
74         u32 duration = tt->params.ct_kill_duration;
75
76         if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
77                 return;
78
79         IWL_ERR(mvm, "Enter CT Kill\n");
80         iwl_mvm_set_hw_ctkill_state(mvm, true);
81
82         tt->throttle = false;
83         tt->dynamic_smps = false;
84
85         /* Don't schedule an exit work if we're in test mode, since
86          * the temperature will not change unless we manually set it
87          * again (or disable testing).
88          */
89         if (!mvm->temperature_test)
90                 schedule_delayed_work(&tt->ct_kill_exit,
91                                       round_jiffies_relative(duration * HZ));
92 }
93
94 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
95 {
96         if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
97                 return;
98
99         IWL_ERR(mvm, "Exit CT Kill\n");
100         iwl_mvm_set_hw_ctkill_state(mvm, false);
101 }
102
103 void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
104 {
105         /* ignore the notification if we are in test mode */
106         if (mvm->temperature_test)
107                 return;
108
109         if (mvm->temperature == temp)
110                 return;
111
112         mvm->temperature = temp;
113         iwl_mvm_tt_handler(mvm);
114 }
115
116 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
117                                     struct iwl_rx_packet *pkt)
118 {
119         struct iwl_dts_measurement_notif *notif;
120         int len = iwl_rx_packet_payload_len(pkt);
121         int temp;
122
123         if (WARN_ON_ONCE(len != sizeof(*notif))) {
124                 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
125                 return -EINVAL;
126         }
127
128         notif = (void *)pkt->data;
129
130         temp = le32_to_cpu(notif->temp);
131
132         /* shouldn't be negative, but since it's s32, make sure it isn't */
133         if (WARN_ON_ONCE(temp < 0))
134                 temp = 0;
135
136         IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
137
138         return temp;
139 }
140
141 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
142                                     struct iwl_rx_packet *pkt, void *data)
143 {
144         struct iwl_mvm *mvm =
145                 container_of(notif_wait, struct iwl_mvm, notif_wait);
146         int *temp = data;
147         int ret;
148
149         ret = iwl_mvm_temp_notif_parse(mvm, pkt);
150         if (ret < 0)
151                 return true;
152
153         *temp = ret;
154
155         return true;
156 }
157
158 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
159 {
160         struct iwl_rx_packet *pkt = rxb_addr(rxb);
161         int temp;
162
163         /* the notification is handled synchronously in ctkill, so skip here */
164         if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
165                 return;
166
167         temp = iwl_mvm_temp_notif_parse(mvm, pkt);
168         if (temp < 0)
169                 return;
170
171         iwl_mvm_tt_temp_changed(mvm, temp);
172 }
173
174 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
175 {
176         struct iwl_dts_measurement_cmd cmd = {
177                 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
178         };
179
180         return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
181                                     sizeof(cmd), &cmd);
182 }
183
184 int iwl_mvm_get_temp(struct iwl_mvm *mvm)
185 {
186         struct iwl_notification_wait wait_temp_notif;
187         static const u16 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
188         int ret, temp;
189
190         lockdep_assert_held(&mvm->mutex);
191
192         iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
193                                    temp_notif, ARRAY_SIZE(temp_notif),
194                                    iwl_mvm_temp_notif_wait, &temp);
195
196         ret = iwl_mvm_get_temp_cmd(mvm);
197         if (ret) {
198                 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
199                 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
200                 return ret;
201         }
202
203         ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
204                                     IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
205         if (ret) {
206                 IWL_ERR(mvm, "Getting the temperature timed out\n");
207                 return ret;
208         }
209
210         return temp;
211 }
212
213 static void check_exit_ctkill(struct work_struct *work)
214 {
215         struct iwl_mvm_tt_mgmt *tt;
216         struct iwl_mvm *mvm;
217         u32 duration;
218         s32 temp;
219
220         tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
221         mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
222
223         duration = tt->params.ct_kill_duration;
224
225         mutex_lock(&mvm->mutex);
226
227         if (__iwl_mvm_mac_start(mvm))
228                 goto reschedule;
229
230         /* make sure the device is available for direct read/writes */
231         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
232                 __iwl_mvm_mac_stop(mvm);
233                 goto reschedule;
234         }
235
236         temp = iwl_mvm_get_temp(mvm);
237
238         iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
239
240         __iwl_mvm_mac_stop(mvm);
241
242         if (temp < 0)
243                 goto reschedule;
244
245         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
246
247         if (temp <= tt->params.ct_kill_exit) {
248                 mutex_unlock(&mvm->mutex);
249                 iwl_mvm_exit_ctkill(mvm);
250                 return;
251         }
252
253 reschedule:
254         mutex_unlock(&mvm->mutex);
255         schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
256                               round_jiffies(duration * HZ));
257 }
258
259 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
260                                      struct ieee80211_vif *vif)
261 {
262         struct iwl_mvm *mvm = _data;
263         enum ieee80211_smps_mode smps_mode;
264
265         lockdep_assert_held(&mvm->mutex);
266
267         if (mvm->thermal_throttle.dynamic_smps)
268                 smps_mode = IEEE80211_SMPS_DYNAMIC;
269         else
270                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
271
272         if (vif->type != NL80211_IFTYPE_STATION)
273                 return;
274
275         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
276 }
277
278 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
279 {
280         struct ieee80211_sta *sta;
281         struct iwl_mvm_sta *mvmsta;
282         int i, err;
283
284         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
285                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
286                                                 lockdep_is_held(&mvm->mutex));
287                 if (IS_ERR_OR_NULL(sta))
288                         continue;
289                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
290                 if (enable == mvmsta->tt_tx_protection)
291                         continue;
292                 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
293                 if (err) {
294                         IWL_ERR(mvm, "Failed to %s Tx protection\n",
295                                 enable ? "enable" : "disable");
296                 } else {
297                         IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
298                                        enable ? "Enable" : "Disable");
299                         mvmsta->tt_tx_protection = enable;
300                 }
301         }
302 }
303
304 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
305 {
306         struct iwl_host_cmd cmd = {
307                 .id = REPLY_THERMAL_MNG_BACKOFF,
308                 .len = { sizeof(u32), },
309                 .data = { &backoff, },
310         };
311
312         backoff = max(backoff, mvm->thermal_throttle.min_backoff);
313
314         if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
315                 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
316                                backoff);
317                 mvm->thermal_throttle.tx_backoff = backoff;
318         } else {
319                 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
320         }
321 }
322
323 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
324 {
325         struct iwl_tt_params *params = &mvm->thermal_throttle.params;
326         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
327         s32 temperature = mvm->temperature;
328         bool throttle_enable = false;
329         int i;
330         u32 tx_backoff;
331
332         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
333
334         if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
335                 iwl_mvm_enter_ctkill(mvm);
336                 return;
337         }
338
339         if (params->support_ct_kill &&
340             temperature <= params->ct_kill_exit) {
341                 iwl_mvm_exit_ctkill(mvm);
342                 return;
343         }
344
345         if (params->support_dynamic_smps) {
346                 if (!tt->dynamic_smps &&
347                     temperature >= params->dynamic_smps_entry) {
348                         IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
349                         tt->dynamic_smps = true;
350                         ieee80211_iterate_active_interfaces_atomic(
351                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
352                                         iwl_mvm_tt_smps_iterator, mvm);
353                         throttle_enable = true;
354                 } else if (tt->dynamic_smps &&
355                            temperature <= params->dynamic_smps_exit) {
356                         IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
357                         tt->dynamic_smps = false;
358                         ieee80211_iterate_active_interfaces_atomic(
359                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
360                                         iwl_mvm_tt_smps_iterator, mvm);
361                 }
362         }
363
364         if (params->support_tx_protection) {
365                 if (temperature >= params->tx_protection_entry) {
366                         iwl_mvm_tt_tx_protection(mvm, true);
367                         throttle_enable = true;
368                 } else if (temperature <= params->tx_protection_exit) {
369                         iwl_mvm_tt_tx_protection(mvm, false);
370                 }
371         }
372
373         if (params->support_tx_backoff) {
374                 tx_backoff = tt->min_backoff;
375                 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
376                         if (temperature < params->tx_backoff[i].temperature)
377                                 break;
378                         tx_backoff = max(tt->min_backoff,
379                                          params->tx_backoff[i].backoff);
380                 }
381                 if (tx_backoff != tt->min_backoff)
382                         throttle_enable = true;
383                 if (tt->tx_backoff != tx_backoff)
384                         iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
385         }
386
387         if (!tt->throttle && throttle_enable) {
388                 IWL_WARN(mvm,
389                          "Due to high temperature thermal throttling initiated\n");
390                 tt->throttle = true;
391         } else if (tt->throttle && !tt->dynamic_smps &&
392                    tt->tx_backoff == tt->min_backoff &&
393                    temperature <= params->tx_protection_exit) {
394                 IWL_WARN(mvm,
395                          "Temperature is back to normal thermal throttling stopped\n");
396                 tt->throttle = false;
397         }
398 }
399
400 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
401         .ct_kill_entry = 118,
402         .ct_kill_exit = 96,
403         .ct_kill_duration = 5,
404         .dynamic_smps_entry = 114,
405         .dynamic_smps_exit = 110,
406         .tx_protection_entry = 114,
407         .tx_protection_exit = 108,
408         .tx_backoff = {
409                 {.temperature = 112, .backoff = 200},
410                 {.temperature = 113, .backoff = 600},
411                 {.temperature = 114, .backoff = 1200},
412                 {.temperature = 115, .backoff = 2000},
413                 {.temperature = 116, .backoff = 4000},
414                 {.temperature = 117, .backoff = 10000},
415         },
416         .support_ct_kill = true,
417         .support_dynamic_smps = true,
418         .support_tx_protection = true,
419         .support_tx_backoff = true,
420 };
421
422 void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
423 {
424         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
425
426         IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
427
428         if (mvm->cfg->thermal_params)
429                 tt->params = *mvm->cfg->thermal_params;
430         else
431                 tt->params = iwl_mvm_default_tt_params;
432
433         tt->throttle = false;
434         tt->dynamic_smps = false;
435         tt->min_backoff = min_backoff;
436         INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
437 }
438
439 void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
440 {
441         cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
442         IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
443 }