Merge tag 'qcom-soc-for-4.7-2' into net-next
[linux-2.6-block.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  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24  * USA
25  *
26  * The full GNU General Public License is included in this distribution
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
37  * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  *
44  *  * Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  *  * Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in
48  *    the documentation and/or other materials provided with the
49  *    distribution.
50  *  * Neither the name Intel Corporation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  *****************************************************************************/
67
68 #include <linux/sort.h>
69
70 #include "mvm.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         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
77         u32 duration = tt->params.ct_kill_duration;
78
79         if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
80                 return;
81
82         IWL_ERR(mvm, "Enter CT Kill\n");
83         iwl_mvm_set_hw_ctkill_state(mvm, true);
84
85         if (!iwl_mvm_is_tt_in_fw(mvm)) {
86                 tt->throttle = false;
87                 tt->dynamic_smps = false;
88         }
89
90         /* Don't schedule an exit work if we're in test mode, since
91          * the temperature will not change unless we manually set it
92          * again (or disable testing).
93          */
94         if (!mvm->temperature_test)
95                 schedule_delayed_work(&tt->ct_kill_exit,
96                                       round_jiffies_relative(duration * HZ));
97 }
98
99 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
100 {
101         if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
102                 return;
103
104         IWL_ERR(mvm, "Exit CT Kill\n");
105         iwl_mvm_set_hw_ctkill_state(mvm, false);
106 }
107
108 void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
109 {
110         /* ignore the notification if we are in test mode */
111         if (mvm->temperature_test)
112                 return;
113
114         if (mvm->temperature == temp)
115                 return;
116
117         mvm->temperature = temp;
118         iwl_mvm_tt_handler(mvm);
119 }
120
121 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
122                                     struct iwl_rx_packet *pkt)
123 {
124         struct iwl_dts_measurement_notif_v1 *notif_v1;
125         int len = iwl_rx_packet_payload_len(pkt);
126         int temp;
127
128         /* we can use notif_v1 only, because v2 only adds an additional
129          * parameter, which is not used in this function.
130         */
131         if (WARN_ON_ONCE(len < sizeof(*notif_v1))) {
132                 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
133                 return -EINVAL;
134         }
135
136         notif_v1 = (void *)pkt->data;
137
138         temp = le32_to_cpu(notif_v1->temp);
139
140         /* shouldn't be negative, but since it's s32, make sure it isn't */
141         if (WARN_ON_ONCE(temp < 0))
142                 temp = 0;
143
144         IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
145
146         return temp;
147 }
148
149 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
150                                     struct iwl_rx_packet *pkt, void *data)
151 {
152         struct iwl_mvm *mvm =
153                 container_of(notif_wait, struct iwl_mvm, notif_wait);
154         int *temp = data;
155         int ret;
156
157         ret = iwl_mvm_temp_notif_parse(mvm, pkt);
158         if (ret < 0)
159                 return true;
160
161         *temp = ret;
162
163         return true;
164 }
165
166 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
167 {
168         struct iwl_rx_packet *pkt = rxb_addr(rxb);
169         struct iwl_dts_measurement_notif_v2 *notif_v2;
170         int len = iwl_rx_packet_payload_len(pkt);
171         int temp;
172         u32 ths_crossed;
173
174         /* the notification is handled synchronously in ctkill, so skip here */
175         if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
176                 return;
177
178         temp = iwl_mvm_temp_notif_parse(mvm, pkt);
179
180         if (!iwl_mvm_is_tt_in_fw(mvm)) {
181                 if (temp >= 0)
182                         iwl_mvm_tt_temp_changed(mvm, temp);
183                 return;
184         }
185
186         if (WARN_ON_ONCE(len < sizeof(*notif_v2))) {
187                 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
188                 return;
189         }
190
191         notif_v2 = (void *)pkt->data;
192         ths_crossed = le32_to_cpu(notif_v2->threshold_idx);
193
194         /* 0xFF in ths_crossed means the notification is not related
195          * to a trip, so we can ignore it here.
196          */
197         if (ths_crossed == 0xFF)
198                 return;
199
200         IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n",
201                        temp, ths_crossed);
202
203 #ifdef CONFIG_THERMAL
204         if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS))
205                 return;
206
207         if (mvm->tz_device.tzone) {
208                 struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device;
209
210                 thermal_notify_framework(tz_dev->tzone,
211                                          tz_dev->fw_trips_index[ths_crossed]);
212         }
213 #endif /* CONFIG_THERMAL */
214 }
215
216 void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
217 {
218         struct iwl_rx_packet *pkt = rxb_addr(rxb);
219         struct ct_kill_notif *notif;
220         int len = iwl_rx_packet_payload_len(pkt);
221
222         if (WARN_ON_ONCE(len != sizeof(*notif))) {
223                 IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n");
224                 return;
225         }
226
227         notif = (struct ct_kill_notif *)pkt->data;
228         IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n",
229                        notif->temperature);
230
231         iwl_mvm_enter_ctkill(mvm);
232 }
233
234 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
235 {
236         struct iwl_dts_measurement_cmd cmd = {
237                 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
238         };
239         struct iwl_ext_dts_measurement_cmd extcmd = {
240                 .control_mode = cpu_to_le32(DTS_AUTOMATIC),
241         };
242         u32 cmdid;
243
244         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
245                 cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
246                                    PHY_OPS_GROUP, 0);
247         else
248                 cmdid = CMD_DTS_MEASUREMENT_TRIGGER;
249
250         if (!fw_has_capa(&mvm->fw->ucode_capa,
251                          IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE))
252                 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd);
253
254         return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd);
255 }
256
257 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp)
258 {
259         struct iwl_notification_wait wait_temp_notif;
260         static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
261                                             DTS_MEASUREMENT_NOTIF_WIDE) };
262         int ret;
263
264         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
265                 temp_notif[0] = DTS_MEASUREMENT_NOTIFICATION;
266
267         lockdep_assert_held(&mvm->mutex);
268
269         iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
270                                    temp_notif, ARRAY_SIZE(temp_notif),
271                                    iwl_mvm_temp_notif_wait, temp);
272
273         ret = iwl_mvm_get_temp_cmd(mvm);
274         if (ret) {
275                 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
276                 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
277                 return ret;
278         }
279
280         ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
281                                     IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
282         if (ret)
283                 IWL_ERR(mvm, "Getting the temperature timed out\n");
284
285         return ret;
286 }
287
288 static void check_exit_ctkill(struct work_struct *work)
289 {
290         struct iwl_mvm_tt_mgmt *tt;
291         struct iwl_mvm *mvm;
292         u32 duration;
293         s32 temp;
294         int ret;
295
296         tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
297         mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
298
299         if (iwl_mvm_is_tt_in_fw(mvm)) {
300                 iwl_mvm_exit_ctkill(mvm);
301
302                 return;
303         }
304
305         duration = tt->params.ct_kill_duration;
306
307         mutex_lock(&mvm->mutex);
308
309         if (__iwl_mvm_mac_start(mvm))
310                 goto reschedule;
311
312         /* make sure the device is available for direct read/writes */
313         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
314                 __iwl_mvm_mac_stop(mvm);
315                 goto reschedule;
316         }
317
318         ret = iwl_mvm_get_temp(mvm, &temp);
319
320         iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
321
322         __iwl_mvm_mac_stop(mvm);
323
324         if (ret)
325                 goto reschedule;
326
327         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
328
329         if (temp <= tt->params.ct_kill_exit) {
330                 mutex_unlock(&mvm->mutex);
331                 iwl_mvm_exit_ctkill(mvm);
332                 return;
333         }
334
335 reschedule:
336         mutex_unlock(&mvm->mutex);
337         schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
338                               round_jiffies(duration * HZ));
339 }
340
341 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
342                                      struct ieee80211_vif *vif)
343 {
344         struct iwl_mvm *mvm = _data;
345         enum ieee80211_smps_mode smps_mode;
346
347         lockdep_assert_held(&mvm->mutex);
348
349         if (mvm->thermal_throttle.dynamic_smps)
350                 smps_mode = IEEE80211_SMPS_DYNAMIC;
351         else
352                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
353
354         if (vif->type != NL80211_IFTYPE_STATION)
355                 return;
356
357         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
358 }
359
360 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
361 {
362         struct ieee80211_sta *sta;
363         struct iwl_mvm_sta *mvmsta;
364         int i, err;
365
366         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
367                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
368                                                 lockdep_is_held(&mvm->mutex));
369                 if (IS_ERR_OR_NULL(sta))
370                         continue;
371                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
372                 if (enable == mvmsta->tt_tx_protection)
373                         continue;
374                 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
375                 if (err) {
376                         IWL_ERR(mvm, "Failed to %s Tx protection\n",
377                                 enable ? "enable" : "disable");
378                 } else {
379                         IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
380                                        enable ? "Enable" : "Disable");
381                         mvmsta->tt_tx_protection = enable;
382                 }
383         }
384 }
385
386 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
387 {
388         struct iwl_host_cmd cmd = {
389                 .id = REPLY_THERMAL_MNG_BACKOFF,
390                 .len = { sizeof(u32), },
391                 .data = { &backoff, },
392         };
393
394         backoff = max(backoff, mvm->thermal_throttle.min_backoff);
395
396         if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
397                 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
398                                backoff);
399                 mvm->thermal_throttle.tx_backoff = backoff;
400         } else {
401                 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
402         }
403 }
404
405 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
406 {
407         struct iwl_tt_params *params = &mvm->thermal_throttle.params;
408         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
409         s32 temperature = mvm->temperature;
410         bool throttle_enable = false;
411         int i;
412         u32 tx_backoff;
413
414         IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
415
416         if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
417                 iwl_mvm_enter_ctkill(mvm);
418                 return;
419         }
420
421         if (params->support_ct_kill &&
422             temperature <= params->ct_kill_exit) {
423                 iwl_mvm_exit_ctkill(mvm);
424                 return;
425         }
426
427         if (params->support_dynamic_smps) {
428                 if (!tt->dynamic_smps &&
429                     temperature >= params->dynamic_smps_entry) {
430                         IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
431                         tt->dynamic_smps = true;
432                         ieee80211_iterate_active_interfaces_atomic(
433                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
434                                         iwl_mvm_tt_smps_iterator, mvm);
435                         throttle_enable = true;
436                 } else if (tt->dynamic_smps &&
437                            temperature <= params->dynamic_smps_exit) {
438                         IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
439                         tt->dynamic_smps = false;
440                         ieee80211_iterate_active_interfaces_atomic(
441                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
442                                         iwl_mvm_tt_smps_iterator, mvm);
443                 }
444         }
445
446         if (params->support_tx_protection) {
447                 if (temperature >= params->tx_protection_entry) {
448                         iwl_mvm_tt_tx_protection(mvm, true);
449                         throttle_enable = true;
450                 } else if (temperature <= params->tx_protection_exit) {
451                         iwl_mvm_tt_tx_protection(mvm, false);
452                 }
453         }
454
455         if (params->support_tx_backoff) {
456                 tx_backoff = tt->min_backoff;
457                 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
458                         if (temperature < params->tx_backoff[i].temperature)
459                                 break;
460                         tx_backoff = max(tt->min_backoff,
461                                          params->tx_backoff[i].backoff);
462                 }
463                 if (tx_backoff != tt->min_backoff)
464                         throttle_enable = true;
465                 if (tt->tx_backoff != tx_backoff)
466                         iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
467         }
468
469         if (!tt->throttle && throttle_enable) {
470                 IWL_WARN(mvm,
471                          "Due to high temperature thermal throttling initiated\n");
472                 tt->throttle = true;
473         } else if (tt->throttle && !tt->dynamic_smps &&
474                    tt->tx_backoff == tt->min_backoff &&
475                    temperature <= params->tx_protection_exit) {
476                 IWL_WARN(mvm,
477                          "Temperature is back to normal thermal throttling stopped\n");
478                 tt->throttle = false;
479         }
480 }
481
482 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
483         .ct_kill_entry = 118,
484         .ct_kill_exit = 96,
485         .ct_kill_duration = 5,
486         .dynamic_smps_entry = 114,
487         .dynamic_smps_exit = 110,
488         .tx_protection_entry = 114,
489         .tx_protection_exit = 108,
490         .tx_backoff = {
491                 {.temperature = 112, .backoff = 200},
492                 {.temperature = 113, .backoff = 600},
493                 {.temperature = 114, .backoff = 1200},
494                 {.temperature = 115, .backoff = 2000},
495                 {.temperature = 116, .backoff = 4000},
496                 {.temperature = 117, .backoff = 10000},
497         },
498         .support_ct_kill = true,
499         .support_dynamic_smps = true,
500         .support_tx_protection = true,
501         .support_tx_backoff = true,
502 };
503
504 /* budget in mWatt */
505 static const u32 iwl_mvm_cdev_budgets[] = {
506         2000,   /* cooling state 0 */
507         1800,   /* cooling state 1 */
508         1600,   /* cooling state 2 */
509         1400,   /* cooling state 3 */
510         1200,   /* cooling state 4 */
511         1000,   /* cooling state 5 */
512         900,    /* cooling state 6 */
513         800,    /* cooling state 7 */
514         700,    /* cooling state 8 */
515         650,    /* cooling state 9 */
516         600,    /* cooling state 10 */
517         550,    /* cooling state 11 */
518         500,    /* cooling state 12 */
519         450,    /* cooling state 13 */
520         400,    /* cooling state 14 */
521         350,    /* cooling state 15 */
522         300,    /* cooling state 16 */
523         250,    /* cooling state 17 */
524         200,    /* cooling state 18 */
525         150,    /* cooling state 19 */
526 };
527
528 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state)
529 {
530         struct iwl_mvm_ctdp_cmd cmd = {
531                 .operation = cpu_to_le32(op),
532                 .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]),
533                 .window_size = 0,
534         };
535         int ret;
536         u32 status;
537
538         lockdep_assert_held(&mvm->mutex);
539
540         ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP,
541                                                        CTDP_CONFIG_CMD),
542                                           sizeof(cmd), &cmd, &status);
543
544         if (ret) {
545                 IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret);
546                 return ret;
547         }
548
549         switch (op) {
550         case CTDP_CMD_OPERATION_START:
551 #ifdef CONFIG_THERMAL
552                 mvm->cooling_dev.cur_state = state;
553 #endif /* CONFIG_THERMAL */
554                 break;
555         case CTDP_CMD_OPERATION_REPORT:
556                 IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status);
557                 /* when the function is called with CTDP_CMD_OPERATION_REPORT
558                  * option the function should return the average budget value
559                  * that is received from the FW.
560                  * The budget can't be less or equal to 0, so it's possible
561                  * to distinguish between error values and budgets.
562                  */
563                 return status;
564         case CTDP_CMD_OPERATION_STOP:
565                 IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n");
566                 break;
567         }
568
569         return 0;
570 }
571
572 #ifdef CONFIG_THERMAL
573 static int compare_temps(const void *a, const void *b)
574 {
575         return ((s16)le16_to_cpu(*(__le16 *)a) -
576                 (s16)le16_to_cpu(*(__le16 *)b));
577 }
578
579 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm)
580 {
581         struct temp_report_ths_cmd cmd = {0};
582         int ret, i, j, idx = 0;
583
584         lockdep_assert_held(&mvm->mutex);
585
586         if (!mvm->tz_device.tzone)
587                 return -EINVAL;
588
589         /* The driver holds array of temperature trips that are unsorted
590          * and uncompressed, the FW should get it compressed and sorted
591          */
592
593         /* compress temp_trips to cmd array, remove uninitialized values*/
594         for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
595                 if (mvm->tz_device.temp_trips[i] != S16_MIN) {
596                         cmd.thresholds[idx++] =
597                                 cpu_to_le16(mvm->tz_device.temp_trips[i]);
598                 }
599         }
600         cmd.num_temps = cpu_to_le32(idx);
601
602         if (!idx)
603                 goto send;
604
605         /*sort cmd array*/
606         sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL);
607
608         /* we should save the indexes of trips because we sort
609          * and compress the orginal array
610          */
611         for (i = 0; i < idx; i++) {
612                 for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) {
613                         if (le16_to_cpu(cmd.thresholds[i]) ==
614                                 mvm->tz_device.temp_trips[j])
615                                 mvm->tz_device.fw_trips_index[i] = j;
616                 }
617         }
618
619 send:
620         ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
621                                                 TEMP_REPORTING_THRESHOLDS_CMD),
622                                    0, sizeof(cmd), &cmd);
623         if (ret)
624                 IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
625                         ret);
626
627         return ret;
628 }
629
630 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device,
631                                   int *temperature)
632 {
633         struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
634         int ret;
635         int temp;
636
637         mutex_lock(&mvm->mutex);
638
639         if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) {
640                 ret = -EIO;
641                 goto out;
642         }
643
644         ret = iwl_mvm_get_temp(mvm, &temp);
645         if (ret)
646                 goto out;
647
648         *temperature = temp * 1000;
649
650 out:
651         mutex_unlock(&mvm->mutex);
652         return ret;
653 }
654
655 static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device,
656                                        int trip, int *temp)
657 {
658         struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
659
660         if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
661                 return -EINVAL;
662
663         *temp = mvm->tz_device.temp_trips[trip] * 1000;
664
665         return 0;
666 }
667
668 static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device,
669                                        int trip, enum thermal_trip_type *type)
670 {
671         if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
672                 return -EINVAL;
673
674         *type = THERMAL_TRIP_PASSIVE;
675
676         return 0;
677 }
678
679 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device,
680                                        int trip, int temp)
681 {
682         struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
683         struct iwl_mvm_thermal_device *tzone;
684         int i, ret;
685         s16 temperature;
686
687         mutex_lock(&mvm->mutex);
688
689         if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) {
690                 ret = -EIO;
691                 goto out;
692         }
693
694         if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) {
695                 ret = -EINVAL;
696                 goto out;
697         }
698
699         if ((temp / 1000) > S16_MAX) {
700                 ret = -EINVAL;
701                 goto out;
702         }
703
704         temperature = (s16)(temp / 1000);
705         tzone = &mvm->tz_device;
706
707         if (!tzone) {
708                 ret = -EIO;
709                 goto out;
710         }
711
712         /* no updates*/
713         if (tzone->temp_trips[trip] == temperature) {
714                 ret = 0;
715                 goto out;
716         }
717
718         /* already existing temperature */
719         for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
720                 if (tzone->temp_trips[i] == temperature) {
721                         ret = -EINVAL;
722                         goto out;
723                 }
724         }
725
726         tzone->temp_trips[trip] = temperature;
727
728         ret = iwl_mvm_send_temp_report_ths_cmd(mvm);
729 out:
730         mutex_unlock(&mvm->mutex);
731         return ret;
732 }
733
734 static  struct thermal_zone_device_ops tzone_ops = {
735         .get_temp = iwl_mvm_tzone_get_temp,
736         .get_trip_temp = iwl_mvm_tzone_get_trip_temp,
737         .get_trip_type = iwl_mvm_tzone_get_trip_type,
738         .set_trip_temp = iwl_mvm_tzone_set_trip_temp,
739 };
740
741 /* make all trips writable */
742 #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1)
743
744 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
745 {
746         int i;
747         char name[] = "iwlwifi";
748
749         if (!iwl_mvm_is_tt_in_fw(mvm)) {
750                 mvm->tz_device.tzone = NULL;
751
752                 return;
753         }
754
755         BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
756
757         mvm->tz_device.tzone = thermal_zone_device_register(name,
758                                                         IWL_MAX_DTS_TRIPS,
759                                                         IWL_WRITABLE_TRIPS_MSK,
760                                                         mvm, &tzone_ops,
761                                                         NULL, 0, 0);
762         if (IS_ERR(mvm->tz_device.tzone)) {
763                 IWL_DEBUG_TEMP(mvm,
764                                "Failed to register to thermal zone (err = %ld)\n",
765                                PTR_ERR(mvm->tz_device.tzone));
766                 mvm->tz_device.tzone = NULL;
767                 return;
768         }
769
770         /* 0 is a valid temperature,
771          * so initialize the array with S16_MIN which invalid temperature
772          */
773         for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++)
774                 mvm->tz_device.temp_trips[i] = S16_MIN;
775 }
776
777 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev,
778                                        unsigned long *state)
779 {
780         *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1;
781
782         return 0;
783 }
784
785 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev,
786                                        unsigned long *state)
787 {
788         struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
789
790         *state = mvm->cooling_dev.cur_state;
791
792         return 0;
793 }
794
795 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev,
796                                        unsigned long new_state)
797 {
798         struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
799         int ret;
800
801         if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR))
802                 return -EIO;
803
804         mutex_lock(&mvm->mutex);
805
806         if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) {
807                 ret = -EINVAL;
808                 goto unlock;
809         }
810
811         ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
812                                    new_state);
813
814 unlock:
815         mutex_unlock(&mvm->mutex);
816         return ret;
817 }
818
819 static struct thermal_cooling_device_ops tcooling_ops = {
820         .get_max_state = iwl_mvm_tcool_get_max_state,
821         .get_cur_state = iwl_mvm_tcool_get_cur_state,
822         .set_cur_state = iwl_mvm_tcool_set_cur_state,
823 };
824
825 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm)
826 {
827         char name[] = "iwlwifi";
828
829         if (!iwl_mvm_is_ctdp_supported(mvm))
830                 return;
831
832         BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
833
834         mvm->cooling_dev.cdev =
835                 thermal_cooling_device_register(name,
836                                                 mvm,
837                                                 &tcooling_ops);
838
839         if (IS_ERR(mvm->cooling_dev.cdev)) {
840                 IWL_DEBUG_TEMP(mvm,
841                                "Failed to register to cooling device (err = %ld)\n",
842                                PTR_ERR(mvm->cooling_dev.cdev));
843                 mvm->cooling_dev.cdev = NULL;
844                 return;
845         }
846 }
847
848 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm)
849 {
850         if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone)
851                 return;
852
853         IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n");
854         thermal_zone_device_unregister(mvm->tz_device.tzone);
855         mvm->tz_device.tzone = NULL;
856 }
857
858 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm)
859 {
860         if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev)
861                 return;
862
863         IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n");
864         thermal_cooling_device_unregister(mvm->cooling_dev.cdev);
865         mvm->cooling_dev.cdev = NULL;
866 }
867 #endif /* CONFIG_THERMAL */
868
869 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff)
870 {
871         struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
872
873         IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
874
875         if (mvm->cfg->thermal_params)
876                 tt->params = *mvm->cfg->thermal_params;
877         else
878                 tt->params = iwl_mvm_default_tt_params;
879
880         tt->throttle = false;
881         tt->dynamic_smps = false;
882         tt->min_backoff = min_backoff;
883         INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
884
885 #ifdef CONFIG_THERMAL
886         iwl_mvm_cooling_device_register(mvm);
887         iwl_mvm_thermal_zone_register(mvm);
888 #endif
889 }
890
891 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm)
892 {
893         cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
894         IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
895
896 #ifdef CONFIG_THERMAL
897         iwl_mvm_cooling_device_unregister(mvm);
898         iwl_mvm_thermal_zone_unregister(mvm);
899 #endif
900 }