GNU Linux-libre 4.14.295-gnu1
[releases.git] / drivers / net / wireless / intel / iwlwifi / mvm / coex.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 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2017        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) 2013 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * Copyright(c) 2017        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/ieee80211.h>
69 #include <linux/etherdevice.h>
70 #include <net/mac80211.h>
71
72 #include "fw/api/coex.h"
73 #include "iwl-modparams.h"
74 #include "mvm.h"
75 #include "iwl-debug.h"
76
77 /* 20MHz / 40MHz below / 40Mhz above*/
78 static const __le64 iwl_ci_mask[][3] = {
79         /* dummy entry for channel 0 */
80         {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
81         {
82                 cpu_to_le64(0x0000001FFFULL),
83                 cpu_to_le64(0x0ULL),
84                 cpu_to_le64(0x00007FFFFFULL),
85         },
86         {
87                 cpu_to_le64(0x000000FFFFULL),
88                 cpu_to_le64(0x0ULL),
89                 cpu_to_le64(0x0003FFFFFFULL),
90         },
91         {
92                 cpu_to_le64(0x000003FFFCULL),
93                 cpu_to_le64(0x0ULL),
94                 cpu_to_le64(0x000FFFFFFCULL),
95         },
96         {
97                 cpu_to_le64(0x00001FFFE0ULL),
98                 cpu_to_le64(0x0ULL),
99                 cpu_to_le64(0x007FFFFFE0ULL),
100         },
101         {
102                 cpu_to_le64(0x00007FFF80ULL),
103                 cpu_to_le64(0x00007FFFFFULL),
104                 cpu_to_le64(0x01FFFFFF80ULL),
105         },
106         {
107                 cpu_to_le64(0x0003FFFC00ULL),
108                 cpu_to_le64(0x0003FFFFFFULL),
109                 cpu_to_le64(0x0FFFFFFC00ULL),
110         },
111         {
112                 cpu_to_le64(0x000FFFF000ULL),
113                 cpu_to_le64(0x000FFFFFFCULL),
114                 cpu_to_le64(0x3FFFFFF000ULL),
115         },
116         {
117                 cpu_to_le64(0x007FFF8000ULL),
118                 cpu_to_le64(0x007FFFFFE0ULL),
119                 cpu_to_le64(0xFFFFFF8000ULL),
120         },
121         {
122                 cpu_to_le64(0x01FFFE0000ULL),
123                 cpu_to_le64(0x01FFFFFF80ULL),
124                 cpu_to_le64(0xFFFFFE0000ULL),
125         },
126         {
127                 cpu_to_le64(0x0FFFF00000ULL),
128                 cpu_to_le64(0x0FFFFFFC00ULL),
129                 cpu_to_le64(0x0ULL),
130         },
131         {
132                 cpu_to_le64(0x3FFFC00000ULL),
133                 cpu_to_le64(0x3FFFFFF000ULL),
134                 cpu_to_le64(0x0)
135         },
136         {
137                 cpu_to_le64(0xFFFE000000ULL),
138                 cpu_to_le64(0xFFFFFF8000ULL),
139                 cpu_to_le64(0x0)
140         },
141         {
142                 cpu_to_le64(0xFFF8000000ULL),
143                 cpu_to_le64(0xFFFFFE0000ULL),
144                 cpu_to_le64(0x0)
145         },
146         {
147                 cpu_to_le64(0xFE00000000ULL),
148                 cpu_to_le64(0x0ULL),
149                 cpu_to_le64(0x0ULL)
150         },
151 };
152
153 static enum iwl_bt_coex_lut_type
154 iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
155 {
156         struct ieee80211_chanctx_conf *chanctx_conf;
157         enum iwl_bt_coex_lut_type ret;
158         u16 phy_ctx_id;
159         u32 primary_ch_phy_id, secondary_ch_phy_id;
160
161         /*
162          * Checking that we hold mvm->mutex is a good idea, but the rate
163          * control can't acquire the mutex since it runs in Tx path.
164          * So this is racy in that case, but in the worst case, the AMPDU
165          * size limit will be wrong for a short time which is not a big
166          * issue.
167          */
168
169         rcu_read_lock();
170
171         chanctx_conf = rcu_dereference(vif->chanctx_conf);
172
173         if (!chanctx_conf ||
174              chanctx_conf->def.chan->band != NL80211_BAND_2GHZ) {
175                 rcu_read_unlock();
176                 return BT_COEX_INVALID_LUT;
177         }
178
179         ret = BT_COEX_TX_DIS_LUT;
180
181         if (mvm->cfg->bt_shared_single_ant) {
182                 rcu_read_unlock();
183                 return ret;
184         }
185
186         phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
187         primary_ch_phy_id = le32_to_cpu(mvm->last_bt_ci_cmd.primary_ch_phy_id);
188         secondary_ch_phy_id =
189                 le32_to_cpu(mvm->last_bt_ci_cmd.secondary_ch_phy_id);
190
191         if (primary_ch_phy_id == phy_ctx_id)
192                 ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
193         else if (secondary_ch_phy_id == phy_ctx_id)
194                 ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
195         /* else - default = TX TX disallowed */
196
197         rcu_read_unlock();
198
199         return ret;
200 }
201
202 int iwl_mvm_send_bt_init_conf(struct iwl_mvm *mvm)
203 {
204         struct iwl_bt_coex_cmd bt_cmd = {};
205         u32 mode;
206
207         lockdep_assert_held(&mvm->mutex);
208
209         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS)) {
210                 switch (mvm->bt_force_ant_mode) {
211                 case BT_FORCE_ANT_BT:
212                         mode = BT_COEX_BT;
213                         break;
214                 case BT_FORCE_ANT_WIFI:
215                         mode = BT_COEX_WIFI;
216                         break;
217                 default:
218                         WARN_ON(1);
219                         mode = 0;
220                 }
221
222                 bt_cmd.mode = cpu_to_le32(mode);
223                 goto send_cmd;
224         }
225
226         mode = iwlwifi_mod_params.bt_coex_active ? BT_COEX_NW : BT_COEX_DISABLE;
227         bt_cmd.mode = cpu_to_le32(mode);
228
229         if (IWL_MVM_BT_COEX_SYNC2SCO)
230                 bt_cmd.enabled_modules |=
231                         cpu_to_le32(BT_COEX_SYNC2SCO_ENABLED);
232
233         if (iwl_mvm_is_mplut_supported(mvm))
234                 bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_MPLUT_ENABLED);
235
236         bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_HIGH_BAND_RET);
237
238 send_cmd:
239         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
240         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
241
242         return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, 0, sizeof(bt_cmd), &bt_cmd);
243 }
244
245 static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
246                                        bool enable)
247 {
248         struct iwl_bt_coex_reduced_txp_update_cmd cmd = {};
249         struct iwl_mvm_sta *mvmsta;
250         u32 value;
251         int ret;
252
253         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
254         if (!mvmsta)
255                 return 0;
256
257         /* nothing to do */
258         if (mvmsta->bt_reduced_txpower == enable)
259                 return 0;
260
261         value = mvmsta->sta_id;
262
263         if (enable)
264                 value |= BT_REDUCED_TX_POWER_BIT;
265
266         IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
267                        enable ? "en" : "dis", sta_id);
268
269         cmd.reduced_txp = cpu_to_le32(value);
270         mvmsta->bt_reduced_txpower = enable;
271
272         ret = iwl_mvm_send_cmd_pdu(mvm, BT_COEX_UPDATE_REDUCED_TXP, CMD_ASYNC,
273                                    sizeof(cmd), &cmd);
274
275         return ret;
276 }
277
278 struct iwl_bt_iterator_data {
279         struct iwl_bt_coex_profile_notif *notif;
280         struct iwl_mvm *mvm;
281         struct ieee80211_chanctx_conf *primary;
282         struct ieee80211_chanctx_conf *secondary;
283         bool primary_ll;
284 };
285
286 static inline
287 void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
288                                        struct ieee80211_vif *vif,
289                                        bool enable, int rssi)
290 {
291         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
292
293         mvmvif->bf_data.last_bt_coex_event = rssi;
294         mvmvif->bf_data.bt_coex_max_thold =
295                 enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
296         mvmvif->bf_data.bt_coex_min_thold =
297                 enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
298 }
299
300 /* must be called under rcu_read_lock */
301 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
302                                       struct ieee80211_vif *vif)
303 {
304         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
305         struct iwl_bt_iterator_data *data = _data;
306         struct iwl_mvm *mvm = data->mvm;
307         struct ieee80211_chanctx_conf *chanctx_conf;
308         /* default smps_mode is AUTOMATIC - only used for client modes */
309         enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
310         u32 bt_activity_grading;
311         int ave_rssi;
312
313         lockdep_assert_held(&mvm->mutex);
314
315         switch (vif->type) {
316         case NL80211_IFTYPE_STATION:
317                 break;
318         case NL80211_IFTYPE_AP:
319                 if (!mvmvif->ap_ibss_active)
320                         return;
321                 break;
322         default:
323                 return;
324         }
325
326         chanctx_conf = rcu_dereference(vif->chanctx_conf);
327
328         /* If channel context is invalid or not on 2.4GHz .. */
329         if ((!chanctx_conf ||
330              chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
331                 if (vif->type == NL80211_IFTYPE_STATION) {
332                         /* ... relax constraints and disable rssi events */
333                         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
334                                             smps_mode);
335                         iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
336                                                     false);
337                         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
338                 }
339                 return;
340         }
341
342         bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
343         if (bt_activity_grading >= BT_HIGH_TRAFFIC)
344                 smps_mode = IEEE80211_SMPS_STATIC;
345         else if (bt_activity_grading >= BT_LOW_TRAFFIC)
346                 smps_mode = IEEE80211_SMPS_DYNAMIC;
347
348         /* relax SMPS constraints for next association */
349         if (!vif->bss_conf.assoc)
350                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
351
352         if (mvmvif->phy_ctxt &&
353             (mvm->last_bt_notif.rrc_status & BIT(mvmvif->phy_ctxt->id)))
354                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
355
356         IWL_DEBUG_COEX(data->mvm,
357                        "mac %d: bt_activity_grading %d smps_req %d\n",
358                        mvmvif->id, bt_activity_grading, smps_mode);
359
360         if (vif->type == NL80211_IFTYPE_STATION)
361                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
362                                     smps_mode);
363
364         /* low latency is always primary */
365         if (iwl_mvm_vif_low_latency(mvmvif)) {
366                 data->primary_ll = true;
367
368                 data->secondary = data->primary;
369                 data->primary = chanctx_conf;
370         }
371
372         if (vif->type == NL80211_IFTYPE_AP) {
373                 if (!mvmvif->ap_ibss_active)
374                         return;
375
376                 if (chanctx_conf == data->primary)
377                         return;
378
379                 if (!data->primary_ll) {
380                         /*
381                          * downgrade the current primary no matter what its
382                          * type is.
383                          */
384                         data->secondary = data->primary;
385                         data->primary = chanctx_conf;
386                 } else {
387                         /* there is low latency vif - we will be secondary */
388                         data->secondary = chanctx_conf;
389                 }
390                 return;
391         }
392
393         /*
394          * STA / P2P Client, try to be primary if first vif. If we are in low
395          * latency mode, we are already in primary and just don't do much
396          */
397         if (!data->primary || data->primary == chanctx_conf)
398                 data->primary = chanctx_conf;
399         else if (!data->secondary)
400                 /* if secondary is not NULL, it might be a GO */
401                 data->secondary = chanctx_conf;
402
403         /*
404          * don't reduce the Tx power if one of these is true:
405          *  we are in LOOSE
406          *  single share antenna product
407          *  BT is inactive
408          *  we are not associated
409          */
410         if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
411             mvm->cfg->bt_shared_single_ant || !vif->bss_conf.assoc ||
412             le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF) {
413                 iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false);
414                 iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
415                 return;
416         }
417
418         /* try to get the avg rssi from fw */
419         ave_rssi = mvmvif->bf_data.ave_beacon_signal;
420
421         /* if the RSSI isn't valid, fake it is very low */
422         if (!ave_rssi)
423                 ave_rssi = -100;
424         if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
425                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
426                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
427         } else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
428                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
429                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
430         }
431
432         /* Begin to monitor the RSSI: it may influence the reduced Tx power */
433         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
434 }
435
436 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
437 {
438         struct iwl_bt_iterator_data data = {
439                 .mvm = mvm,
440                 .notif = &mvm->last_bt_notif,
441         };
442         struct iwl_bt_coex_ci_cmd cmd = {};
443         u8 ci_bw_idx;
444
445         /* Ignore updates if we are in force mode */
446         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
447                 return;
448
449         rcu_read_lock();
450         ieee80211_iterate_active_interfaces_atomic(
451                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
452                                         iwl_mvm_bt_notif_iterator, &data);
453
454         if (data.primary) {
455                 struct ieee80211_chanctx_conf *chan = data.primary;
456                 if (WARN_ON(!chan->def.chan)) {
457                         rcu_read_unlock();
458                         return;
459                 }
460
461                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
462                         ci_bw_idx = 0;
463                 } else {
464                         if (chan->def.center_freq1 >
465                             chan->def.chan->center_freq)
466                                 ci_bw_idx = 2;
467                         else
468                                 ci_bw_idx = 1;
469                 }
470
471                 cmd.bt_primary_ci =
472                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
473                 cmd.primary_ch_phy_id =
474                         cpu_to_le32(*((u16 *)data.primary->drv_priv));
475         }
476
477         if (data.secondary) {
478                 struct ieee80211_chanctx_conf *chan = data.secondary;
479                 if (WARN_ON(!data.secondary->def.chan)) {
480                         rcu_read_unlock();
481                         return;
482                 }
483
484                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
485                         ci_bw_idx = 0;
486                 } else {
487                         if (chan->def.center_freq1 >
488                             chan->def.chan->center_freq)
489                                 ci_bw_idx = 2;
490                         else
491                                 ci_bw_idx = 1;
492                 }
493
494                 cmd.bt_secondary_ci =
495                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
496                 cmd.secondary_ch_phy_id =
497                         cpu_to_le32(*((u16 *)data.secondary->drv_priv));
498         }
499
500         rcu_read_unlock();
501
502         /* Don't spam the fw with the same command over and over */
503         if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
504                 if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
505                                          sizeof(cmd), &cmd))
506                         IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
507                 memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
508         }
509 }
510
511 void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
512                               struct iwl_rx_cmd_buffer *rxb)
513 {
514         struct iwl_rx_packet *pkt = rxb_addr(rxb);
515         struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
516
517         if (!iwl_mvm_has_new_ats_coex_api(mvm)) {
518                 struct iwl_bt_coex_profile_notif_v4 *v4 = (void *)pkt->data;
519
520                 mvm->last_bt_notif.mbox_msg[0] = v4->mbox_msg[0];
521                 mvm->last_bt_notif.mbox_msg[1] = v4->mbox_msg[1];
522                 mvm->last_bt_notif.mbox_msg[2] = v4->mbox_msg[2];
523                 mvm->last_bt_notif.mbox_msg[3] = v4->mbox_msg[3];
524                 mvm->last_bt_notif.msg_idx = v4->msg_idx;
525                 mvm->last_bt_notif.bt_ci_compliance = v4->bt_ci_compliance;
526                 mvm->last_bt_notif.primary_ch_lut = v4->primary_ch_lut;
527                 mvm->last_bt_notif.secondary_ch_lut = v4->secondary_ch_lut;
528                 mvm->last_bt_notif.bt_activity_grading =
529                         v4->bt_activity_grading;
530                 mvm->last_bt_notif.ttc_status = v4->ttc_status;
531                 mvm->last_bt_notif.rrc_status = v4->rrc_status;
532         } else {
533                 /* save this notification for future use: rssi fluctuations */
534                 memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
535         }
536
537         IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
538         IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n",
539                        mvm->last_bt_notif.bt_ci_compliance);
540         IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
541                        le32_to_cpu(mvm->last_bt_notif.primary_ch_lut));
542         IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
543                        le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut));
544         IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
545                        le32_to_cpu(mvm->last_bt_notif.bt_activity_grading));
546
547
548         iwl_mvm_bt_coex_notif_handle(mvm);
549 }
550
551 void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
552                            enum ieee80211_rssi_event_data rssi_event)
553 {
554         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
555         int ret;
556
557         lockdep_assert_held(&mvm->mutex);
558
559         /* Ignore updates if we are in force mode */
560         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
561                 return;
562
563         /*
564          * Rssi update while not associated - can happen since the statistics
565          * are handled asynchronously
566          */
567         if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA)
568                 return;
569
570         /* No BT - reports should be disabled */
571         if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
572                 return;
573
574         IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
575                        rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
576
577         /*
578          * Check if rssi is good enough for reduced Tx power, but not in loose
579          * scheme.
580          */
581         if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
582             iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
583                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
584                                                   false);
585         else
586                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
587
588         if (ret)
589                 IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
590 }
591
592 #define LINK_QUAL_AGG_TIME_LIMIT_DEF    (4000)
593 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
594
595 u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
596                                 struct ieee80211_sta *sta)
597 {
598         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
599         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
600         struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
601         enum iwl_bt_coex_lut_type lut_type;
602
603         if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
604                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
605
606         if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
607             BT_HIGH_TRAFFIC)
608                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
609
610         lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
611
612         if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
613                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
614
615         /* tight coex, high bt traffic, reduce AGG time limit */
616         return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
617 }
618
619 bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
620                                      struct ieee80211_sta *sta)
621 {
622         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
623         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
624         struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
625         enum iwl_bt_coex_lut_type lut_type;
626
627         if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
628                 return true;
629
630         if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
631             BT_HIGH_TRAFFIC)
632                 return true;
633
634         /*
635          * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
636          * since BT is already killed.
637          * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
638          * we Tx.
639          * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
640          */
641         lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
642         return lut_type != BT_COEX_LOOSE_LUT;
643 }
644
645 bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
646 {
647         /* there is no other antenna, shared antenna is always available */
648         if (mvm->cfg->bt_shared_single_ant)
649                 return true;
650
651         if (ant & mvm->cfg->non_shared_ant)
652                 return true;
653
654         return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
655                 BT_HIGH_TRAFFIC;
656 }
657
658 bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
659 {
660         /* there is no other antenna, shared antenna is always available */
661         if (mvm->cfg->bt_shared_single_ant)
662                 return true;
663
664         return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
665 }
666
667 bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
668                                     enum nl80211_band band)
669 {
670         u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
671
672         if (band != NL80211_BAND_2GHZ)
673                 return false;
674
675         return bt_activity >= BT_LOW_TRAFFIC;
676 }
677
678 u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
679                            struct ieee80211_tx_info *info, u8 ac)
680 {
681         __le16 fc = hdr->frame_control;
682         bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
683
684         if (info->band != NL80211_BAND_2GHZ)
685                 return 0;
686
687         if (unlikely(mvm->bt_tx_prio))
688                 return mvm->bt_tx_prio - 1;
689
690         if (likely(ieee80211_is_data(fc))) {
691                 if (likely(ieee80211_is_data_qos(fc))) {
692                         switch (ac) {
693                         case IEEE80211_AC_BE:
694                                 return mplut_enabled ? 1 : 0;
695                         case IEEE80211_AC_VI:
696                                 return mplut_enabled ? 2 : 3;
697                         case IEEE80211_AC_VO:
698                                 return 3;
699                         default:
700                                 return 0;
701                         }
702                 } else if (is_multicast_ether_addr(hdr->addr1)) {
703                         return 3;
704                 } else
705                         return 0;
706         } else if (ieee80211_is_mgmt(fc)) {
707                 return ieee80211_is_disassoc(fc) ? 0 : 3;
708         } else if (ieee80211_is_ctl(fc)) {
709                 /* ignore cfend and cfendack frames as we never send those */
710                 return 3;
711         }
712
713         return 0;
714 }
715
716 void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
717 {
718         iwl_mvm_bt_coex_notif_handle(mvm);
719 }