]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/intel/iwlwifi/mvm/tt.c
Merge remote-tracking branch 'usb-chipidea-next/ci-for-usb-next'
[karo-tx-linux.git] / drivers / net / wireless / intel / 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 <linuxwifi@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         struct iwl_ext_dts_measurement_cmd extcmd = {
180                 .control_mode = cpu_to_le32(DTS_AUTOMATIC),
181         };
182         u32 cmdid;
183
184         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
185                 cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
186                                    PHY_OPS_GROUP, 0);
187         else
188                 cmdid = CMD_DTS_MEASUREMENT_TRIGGER;
189
190         if (!fw_has_capa(&mvm->fw->ucode_capa,
191                          IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE))
192                 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd);
193
194         return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd);
195 }
196
197 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp)
198 {
199         struct iwl_notification_wait wait_temp_notif;
200         static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
201                                             DTS_MEASUREMENT_NOTIF_WIDE) };
202         int ret;
203
204         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
205                 temp_notif[0] = DTS_MEASUREMENT_NOTIFICATION;
206
207         lockdep_assert_held(&mvm->mutex);
208
209         iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
210                                    temp_notif, ARRAY_SIZE(temp_notif),
211                                    iwl_mvm_temp_notif_wait, temp);
212
213         ret = iwl_mvm_get_temp_cmd(mvm);
214         if (ret) {
215                 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
216                 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
217                 return ret;
218         }
219
220         ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
221                                     IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
222         if (ret)
223                 IWL_ERR(mvm, "Getting the temperature timed out\n");
224
225         return ret;
226 }
227
228 static void check_exit_ctkill(struct work_struct *work)
229 {
230         struct iwl_mvm_tt_mgmt *tt;
231         struct iwl_mvm *mvm;
232         u32 duration;
233         s32 temp;
234         int ret;
235
236         tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
237         mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
238
239         duration = tt->params.ct_kill_duration;
240
241         mutex_lock(&mvm->mutex);
242
243         if (__iwl_mvm_mac_start(mvm))
244                 goto reschedule;
245
246         /* make sure the device is available for direct read/writes */
247         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
248                 __iwl_mvm_mac_stop(mvm);
249                 goto reschedule;
250         }
251
252         ret = iwl_mvm_get_temp(mvm, &temp);
253
254         iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
255
256         __iwl_mvm_mac_stop(mvm);
257
258         if (ret)
259                 goto reschedule;
260
261         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
262
263         if (temp <= tt->params.ct_kill_exit) {
264                 mutex_unlock(&mvm->mutex);
265                 iwl_mvm_exit_ctkill(mvm);
266                 return;
267         }
268
269 reschedule:
270         mutex_unlock(&mvm->mutex);
271         schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
272                               round_jiffies(duration * HZ));
273 }
274
275 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
276                                      struct ieee80211_vif *vif)
277 {
278         struct iwl_mvm *mvm = _data;
279         enum ieee80211_smps_mode smps_mode;
280
281         lockdep_assert_held(&mvm->mutex);
282
283         if (mvm->thermal_throttle.dynamic_smps)
284                 smps_mode = IEEE80211_SMPS_DYNAMIC;
285         else
286                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
287
288         if (vif->type != NL80211_IFTYPE_STATION)
289                 return;
290
291         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
292 }
293
294 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
295 {
296         struct ieee80211_sta *sta;
297         struct iwl_mvm_sta *mvmsta;
298         int i, err;
299
300         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
301                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
302                                                 lockdep_is_held(&mvm->mutex));
303                 if (IS_ERR_OR_NULL(sta))
304                         continue;
305                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
306                 if (enable == mvmsta->tt_tx_protection)
307                         continue;
308                 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
309                 if (err) {
310                         IWL_ERR(mvm, "Failed to %s Tx protection\n",
311                                 enable ? "enable" : "disable");
312                 } else {
313                         IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
314                                        enable ? "Enable" : "Disable");
315                         mvmsta->tt_tx_protection = enable;
316                 }
317         }
318 }
319
320 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
321 {
322         struct iwl_host_cmd cmd = {
323                 .id = REPLY_THERMAL_MNG_BACKOFF,
324                 .len = { sizeof(u32), },
325                 .data = { &backoff, },
326         };
327
328         backoff = max(backoff, mvm->thermal_throttle.min_backoff);
329
330         if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
331                 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
332                                backoff);
333                 mvm->thermal_throttle.tx_backoff = backoff;
334         } else {
335                 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
336         }
337 }
338
339 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
340 {
341         struct iwl_tt_params *params = &mvm->thermal_throttle.params;
342         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
343         s32 temperature = mvm->temperature;
344         bool throttle_enable = false;
345         int i;
346         u32 tx_backoff;
347
348         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
349
350         if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
351                 iwl_mvm_enter_ctkill(mvm);
352                 return;
353         }
354
355         if (params->support_ct_kill &&
356             temperature <= params->ct_kill_exit) {
357                 iwl_mvm_exit_ctkill(mvm);
358                 return;
359         }
360
361         if (params->support_dynamic_smps) {
362                 if (!tt->dynamic_smps &&
363                     temperature >= params->dynamic_smps_entry) {
364                         IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
365                         tt->dynamic_smps = true;
366                         ieee80211_iterate_active_interfaces_atomic(
367                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
368                                         iwl_mvm_tt_smps_iterator, mvm);
369                         throttle_enable = true;
370                 } else if (tt->dynamic_smps &&
371                            temperature <= params->dynamic_smps_exit) {
372                         IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
373                         tt->dynamic_smps = false;
374                         ieee80211_iterate_active_interfaces_atomic(
375                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
376                                         iwl_mvm_tt_smps_iterator, mvm);
377                 }
378         }
379
380         if (params->support_tx_protection) {
381                 if (temperature >= params->tx_protection_entry) {
382                         iwl_mvm_tt_tx_protection(mvm, true);
383                         throttle_enable = true;
384                 } else if (temperature <= params->tx_protection_exit) {
385                         iwl_mvm_tt_tx_protection(mvm, false);
386                 }
387         }
388
389         if (params->support_tx_backoff) {
390                 tx_backoff = tt->min_backoff;
391                 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
392                         if (temperature < params->tx_backoff[i].temperature)
393                                 break;
394                         tx_backoff = max(tt->min_backoff,
395                                          params->tx_backoff[i].backoff);
396                 }
397                 if (tx_backoff != tt->min_backoff)
398                         throttle_enable = true;
399                 if (tt->tx_backoff != tx_backoff)
400                         iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
401         }
402
403         if (!tt->throttle && throttle_enable) {
404                 IWL_WARN(mvm,
405                          "Due to high temperature thermal throttling initiated\n");
406                 tt->throttle = true;
407         } else if (tt->throttle && !tt->dynamic_smps &&
408                    tt->tx_backoff == tt->min_backoff &&
409                    temperature <= params->tx_protection_exit) {
410                 IWL_WARN(mvm,
411                          "Temperature is back to normal thermal throttling stopped\n");
412                 tt->throttle = false;
413         }
414 }
415
416 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
417         .ct_kill_entry = 118,
418         .ct_kill_exit = 96,
419         .ct_kill_duration = 5,
420         .dynamic_smps_entry = 114,
421         .dynamic_smps_exit = 110,
422         .tx_protection_entry = 114,
423         .tx_protection_exit = 108,
424         .tx_backoff = {
425                 {.temperature = 112, .backoff = 200},
426                 {.temperature = 113, .backoff = 600},
427                 {.temperature = 114, .backoff = 1200},
428                 {.temperature = 115, .backoff = 2000},
429                 {.temperature = 116, .backoff = 4000},
430                 {.temperature = 117, .backoff = 10000},
431         },
432         .support_ct_kill = true,
433         .support_dynamic_smps = true,
434         .support_tx_protection = true,
435         .support_tx_backoff = true,
436 };
437
438 void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
439 {
440         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
441
442         IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
443
444         if (mvm->cfg->thermal_params)
445                 tt->params = *mvm->cfg->thermal_params;
446         else
447                 tt->params = iwl_mvm_default_tt_params;
448
449         tt->throttle = false;
450         tt->dynamic_smps = false;
451         tt->min_backoff = min_backoff;
452         INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
453 }
454
455 void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
456 {
457         cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
458         IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
459 }