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