Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/iwlwifi...
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / bt-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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63
64 #include <net/mac80211.h>
65
66 #include "fw-api-bt-coex.h"
67 #include "iwl-modparams.h"
68 #include "mvm.h"
69 #include "iwl-debug.h"
70
71 #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant)                  \
72         [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) |    \
73                    ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
74
75 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
76         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
77                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
78         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
79                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
80         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
81                        BT_COEX_PRIO_TBL_PRIO_LOW, 0),
82         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
83                        BT_COEX_PRIO_TBL_PRIO_LOW, 1),
84         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
85                        BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
86         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
87                        BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
88         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
89                        BT_COEX_PRIO_TBL_DISABLED, 0),
90         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
91                        BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
92         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
93                        BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
94         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
95                        BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
96         0, 0, 0, 0, 0, 0,
97 };
98
99 #undef EVENT_PRIO_ANT
100
101 #define BT_ENABLE_REDUCED_TXPOWER_THRESHOLD     (-62)
102 #define BT_DISABLE_REDUCED_TXPOWER_THRESHOLD    (-65)
103 #define BT_ANTENNA_COUPLING_THRESHOLD           (30)
104
105 int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
106 {
107         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
108                 return 0;
109
110         return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, CMD_SYNC,
111                                     sizeof(struct iwl_bt_coex_prio_tbl_cmd),
112                                     &iwl_bt_prio_tbl);
113 }
114
115 const u32 iwl_bt_ack_kill_msk[BT_KILL_MSK_MAX] = {
116         [BT_KILL_MSK_DEFAULT] = 0xffff0000,
117         [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
118         [BT_KILL_MSK_REDUCED_TXPOW] = 0,
119 };
120
121 const u32 iwl_bt_cts_kill_msk[BT_KILL_MSK_MAX] = {
122         [BT_KILL_MSK_DEFAULT] = 0xffff0000,
123         [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
124         [BT_KILL_MSK_REDUCED_TXPOW] = 0,
125 };
126
127 static const __le32 iwl_bt_prio_boost[BT_COEX_BOOST_SIZE] = {
128         cpu_to_le32(0xf0f0f0f0),
129         cpu_to_le32(0xc0c0c0c0),
130         cpu_to_le32(0xfcfcfcfc),
131         cpu_to_le32(0xff00ff00),
132 };
133
134 static const __le32 iwl_single_shared_ant[BT_COEX_MAX_LUT][BT_COEX_LUT_SIZE] = {
135         {
136                 cpu_to_le32(0x40000000),
137                 cpu_to_le32(0x00000000),
138                 cpu_to_le32(0x44000000),
139                 cpu_to_le32(0x00000000),
140                 cpu_to_le32(0x40000000),
141                 cpu_to_le32(0x00000000),
142                 cpu_to_le32(0x44000000),
143                 cpu_to_le32(0x00000000),
144                 cpu_to_le32(0xc0004000),
145                 cpu_to_le32(0xf0005000),
146                 cpu_to_le32(0xc0004000),
147                 cpu_to_le32(0xf0005000),
148         },
149         {
150                 cpu_to_le32(0x40000000),
151                 cpu_to_le32(0x00000000),
152                 cpu_to_le32(0x44000000),
153                 cpu_to_le32(0x00000000),
154                 cpu_to_le32(0x40000000),
155                 cpu_to_le32(0x00000000),
156                 cpu_to_le32(0x44000000),
157                 cpu_to_le32(0x00000000),
158                 cpu_to_le32(0xc0004000),
159                 cpu_to_le32(0xf0005000),
160                 cpu_to_le32(0xc0004000),
161                 cpu_to_le32(0xf0005000),
162         },
163         {
164                 cpu_to_le32(0x40000000),
165                 cpu_to_le32(0x00000000),
166                 cpu_to_le32(0x44000000),
167                 cpu_to_le32(0x00000000),
168                 cpu_to_le32(0x40000000),
169                 cpu_to_le32(0x00000000),
170                 cpu_to_le32(0x44000000),
171                 cpu_to_le32(0x00000000),
172                 cpu_to_le32(0xc0004000),
173                 cpu_to_le32(0xf0005000),
174                 cpu_to_le32(0xc0004000),
175                 cpu_to_le32(0xf0005000),
176         },
177 };
178
179 static const __le32 iwl_combined_lookup[BT_COEX_MAX_LUT][BT_COEX_LUT_SIZE] = {
180         {
181                 /* Tight */
182                 cpu_to_le32(0xaaaaaaaa),
183                 cpu_to_le32(0xaaaaaaaa),
184                 cpu_to_le32(0xaeaaaaaa),
185                 cpu_to_le32(0xaaaaaaaa),
186                 cpu_to_le32(0xcc00ff28),
187                 cpu_to_le32(0x0000aaaa),
188                 cpu_to_le32(0xcc00aaaa),
189                 cpu_to_le32(0x0000aaaa),
190                 cpu_to_le32(0xc0004000),
191                 cpu_to_le32(0x00000000),
192                 cpu_to_le32(0xf0005000),
193                 cpu_to_le32(0xf0005000),
194         },
195         {
196                 /* Loose */
197                 cpu_to_le32(0xaaaaaaaa),
198                 cpu_to_le32(0xaaaaaaaa),
199                 cpu_to_le32(0xaaaaaaaa),
200                 cpu_to_le32(0xaaaaaaaa),
201                 cpu_to_le32(0xcc00ff28),
202                 cpu_to_le32(0x0000aaaa),
203                 cpu_to_le32(0xcc00aaaa),
204                 cpu_to_le32(0x0000aaaa),
205                 cpu_to_le32(0x00000000),
206                 cpu_to_le32(0x00000000),
207                 cpu_to_le32(0xf0005000),
208                 cpu_to_le32(0xf0005000),
209         },
210         {
211                 /* Tx Tx disabled */
212                 cpu_to_le32(0xaaaaaaaa),
213                 cpu_to_le32(0xaaaaaaaa),
214                 cpu_to_le32(0xaaaaaaaa),
215                 cpu_to_le32(0xaaaaaaaa),
216                 cpu_to_le32(0xcc00ff28),
217                 cpu_to_le32(0x0000aaaa),
218                 cpu_to_le32(0xcc00aaaa),
219                 cpu_to_le32(0x0000aaaa),
220                 cpu_to_le32(0xC0004000),
221                 cpu_to_le32(0xC0004000),
222                 cpu_to_le32(0xF0005000),
223                 cpu_to_le32(0xF0005000),
224         },
225 };
226
227 /* 20MHz / 40MHz below / 40Mhz above*/
228 static const __le64 iwl_ci_mask[][3] = {
229         /* dummy entry for channel 0 */
230         {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
231         {
232                 cpu_to_le64(0x0000001FFFULL),
233                 cpu_to_le64(0x0ULL),
234                 cpu_to_le64(0x00007FFFFFULL),
235         },
236         {
237                 cpu_to_le64(0x000000FFFFULL),
238                 cpu_to_le64(0x0ULL),
239                 cpu_to_le64(0x0003FFFFFFULL),
240         },
241         {
242                 cpu_to_le64(0x000003FFFCULL),
243                 cpu_to_le64(0x0ULL),
244                 cpu_to_le64(0x000FFFFFFCULL),
245         },
246         {
247                 cpu_to_le64(0x00001FFFE0ULL),
248                 cpu_to_le64(0x0ULL),
249                 cpu_to_le64(0x007FFFFFE0ULL),
250         },
251         {
252                 cpu_to_le64(0x00007FFF80ULL),
253                 cpu_to_le64(0x00007FFFFFULL),
254                 cpu_to_le64(0x01FFFFFF80ULL),
255         },
256         {
257                 cpu_to_le64(0x0003FFFC00ULL),
258                 cpu_to_le64(0x0003FFFFFFULL),
259                 cpu_to_le64(0x0FFFFFFC00ULL),
260         },
261         {
262                 cpu_to_le64(0x000FFFF000ULL),
263                 cpu_to_le64(0x000FFFFFFCULL),
264                 cpu_to_le64(0x3FFFFFF000ULL),
265         },
266         {
267                 cpu_to_le64(0x007FFF8000ULL),
268                 cpu_to_le64(0x007FFFFFE0ULL),
269                 cpu_to_le64(0xFFFFFF8000ULL),
270         },
271         {
272                 cpu_to_le64(0x01FFFE0000ULL),
273                 cpu_to_le64(0x01FFFFFF80ULL),
274                 cpu_to_le64(0xFFFFFE0000ULL),
275         },
276         {
277                 cpu_to_le64(0x0FFFF00000ULL),
278                 cpu_to_le64(0x0FFFFFFC00ULL),
279                 cpu_to_le64(0x0ULL),
280         },
281         {
282                 cpu_to_le64(0x3FFFC00000ULL),
283                 cpu_to_le64(0x3FFFFFF000ULL),
284                 cpu_to_le64(0x0)
285         },
286         {
287                 cpu_to_le64(0xFFFE000000ULL),
288                 cpu_to_le64(0xFFFFFF8000ULL),
289                 cpu_to_le64(0x0)
290         },
291         {
292                 cpu_to_le64(0xFFF8000000ULL),
293                 cpu_to_le64(0xFFFFFE0000ULL),
294                 cpu_to_le64(0x0)
295         },
296         {
297                 cpu_to_le64(0xFFC0000000ULL),
298                 cpu_to_le64(0x0ULL),
299                 cpu_to_le64(0x0ULL)
300         },
301 };
302
303 static const __le32 iwl_bt_mprio_lut[BT_COEX_MULTI_PRIO_LUT_SIZE] = {
304         cpu_to_le32(0x22002200),
305         cpu_to_le32(0x33113311),
306 };
307
308 static enum iwl_bt_coex_lut_type
309 iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
310 {
311         struct ieee80211_chanctx_conf *chanctx_conf;
312         enum iwl_bt_coex_lut_type ret;
313         u16 phy_ctx_id;
314
315         /*
316          * Checking that we hold mvm->mutex is a good idea, but the rate
317          * control can't acquire the mutex since it runs in Tx path.
318          * So this is racy in that case, but in the worst case, the AMPDU
319          * size limit will be wrong for a short time which is not a big
320          * issue.
321          */
322
323         rcu_read_lock();
324
325         chanctx_conf = rcu_dereference(vif->chanctx_conf);
326
327         if (!chanctx_conf ||
328              chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
329                 rcu_read_unlock();
330                 return BT_COEX_LOOSE_LUT;
331         }
332
333         ret = BT_COEX_TX_DIS_LUT;
334
335         if (mvm->cfg->bt_shared_single_ant) {
336                 rcu_read_unlock();
337                 return ret;
338         }
339
340         phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
341
342         if (mvm->last_bt_ci_cmd.primary_ch_phy_id == phy_ctx_id)
343                 ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
344         else if (mvm->last_bt_ci_cmd.secondary_ch_phy_id == phy_ctx_id)
345                 ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
346         /* else - default = TX TX disallowed */
347
348         rcu_read_unlock();
349
350         return ret;
351 }
352
353 int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
354 {
355         struct iwl_bt_coex_cmd *bt_cmd;
356         struct iwl_host_cmd cmd = {
357                 .id = BT_CONFIG,
358                 .len = { sizeof(*bt_cmd), },
359                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
360                 .flags = CMD_SYNC,
361         };
362         int ret;
363         u32 flags;
364
365         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
366                 return 0;
367
368         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
369         if (!bt_cmd)
370                 return -ENOMEM;
371         cmd.data[0] = bt_cmd;
372
373         bt_cmd->max_kill = 5;
374         bt_cmd->bt4_antenna_isolation_thr = BT_ANTENNA_COUPLING_THRESHOLD,
375         bt_cmd->bt4_antenna_isolation = iwlwifi_mod_params.ant_coupling,
376         bt_cmd->bt4_tx_tx_delta_freq_thr = 15,
377         bt_cmd->bt4_tx_rx_max_freq0 = 15,
378
379         flags = iwlwifi_mod_params.bt_coex_active ?
380                         BT_COEX_NW : BT_COEX_DISABLE;
381         bt_cmd->flags = cpu_to_le32(flags);
382
383         bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE |
384                                             BT_VALID_BT_PRIO_BOOST |
385                                             BT_VALID_MAX_KILL |
386                                             BT_VALID_3W_TMRS |
387                                             BT_VALID_KILL_ACK |
388                                             BT_VALID_KILL_CTS |
389                                             BT_VALID_REDUCED_TX_POWER |
390                                             BT_VALID_LUT |
391                                             BT_VALID_WIFI_RX_SW_PRIO_BOOST |
392                                             BT_VALID_WIFI_TX_SW_PRIO_BOOST |
393                                             BT_VALID_CORUN_LUT_20 |
394                                             BT_VALID_CORUN_LUT_40 |
395                                             BT_VALID_ANT_ISOLATION |
396                                             BT_VALID_ANT_ISOLATION_THRS |
397                                             BT_VALID_TXTX_DELTA_FREQ_THRS |
398                                             BT_VALID_TXRX_MAX_FREQ_0 |
399                                             BT_VALID_SYNC_TO_SCO);
400
401         if (IWL_MVM_BT_COEX_SYNC2SCO)
402                 bt_cmd->flags |= cpu_to_le32(BT_COEX_SYNC2SCO);
403
404         if (mvm->cfg->bt_shared_single_ant)
405                 memcpy(&bt_cmd->decision_lut, iwl_single_shared_ant,
406                        sizeof(iwl_single_shared_ant));
407         else
408                 memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
409                        sizeof(iwl_combined_lookup));
410
411         memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
412                sizeof(iwl_bt_prio_boost));
413         memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
414                sizeof(iwl_bt_mprio_lut));
415         bt_cmd->kill_ack_msk =
416                 cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
417         bt_cmd->kill_cts_msk =
418                 cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
419
420         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
421         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
422
423         ret = iwl_mvm_send_cmd(mvm, &cmd);
424
425         kfree(bt_cmd);
426         return ret;
427 }
428
429 static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm,
430                                            bool reduced_tx_power)
431 {
432         enum iwl_bt_kill_msk bt_kill_msk;
433         struct iwl_bt_coex_cmd *bt_cmd;
434         struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
435         struct iwl_host_cmd cmd = {
436                 .id = BT_CONFIG,
437                 .data[0] = &bt_cmd,
438                 .len = { sizeof(*bt_cmd), },
439                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
440                 .flags = CMD_SYNC,
441         };
442         int ret = 0;
443
444         lockdep_assert_held(&mvm->mutex);
445
446         if (reduced_tx_power) {
447                 /* Reduced Tx power has precedence on the type of the profile */
448                 bt_kill_msk = BT_KILL_MSK_REDUCED_TXPOW;
449         } else {
450                 /* Low latency BT profile is active: give higher prio to BT */
451                 if (BT_MBOX_MSG(notif, 3, SCO_STATE)  ||
452                     BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
453                     BT_MBOX_MSG(notif, 3, SNIFF_STATE))
454                         bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
455                 else
456                         bt_kill_msk = BT_KILL_MSK_DEFAULT;
457         }
458
459         IWL_DEBUG_COEX(mvm,
460                        "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
461                        bt_kill_msk,
462                        BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
463                        BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
464                        BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
465
466         /* Don't send HCMD if there is no update */
467         if (bt_kill_msk == mvm->bt_kill_msk)
468                 return 0;
469
470         mvm->bt_kill_msk = bt_kill_msk;
471
472         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
473         if (!bt_cmd)
474                 return -ENOMEM;
475         cmd.data[0] = bt_cmd;
476         bt_cmd->flags = cpu_to_le32(BT_COEX_NW);
477
478         bt_cmd->kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
479         bt_cmd->kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
480         bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_ENABLE |
481                                              BT_VALID_KILL_ACK |
482                                              BT_VALID_KILL_CTS);
483
484         IWL_DEBUG_COEX(mvm, "ACK Kill msk = 0x%08x, CTS Kill msk = 0x%08x\n",
485                        iwl_bt_ack_kill_msk[bt_kill_msk],
486                        iwl_bt_cts_kill_msk[bt_kill_msk]);
487
488         ret = iwl_mvm_send_cmd(mvm, &cmd);
489
490         kfree(bt_cmd);
491         return ret;
492 }
493
494 int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id, bool enable)
495 {
496         struct iwl_bt_coex_cmd *bt_cmd;
497         /* Send ASYNC since this can be sent from an atomic context */
498         struct iwl_host_cmd cmd = {
499                 .id = BT_CONFIG,
500                 .len = { sizeof(*bt_cmd), },
501                 .dataflags = { IWL_HCMD_DFL_DUP, },
502                 .flags = CMD_ASYNC,
503         };
504         struct iwl_mvm_sta *mvmsta;
505         int ret;
506
507         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
508         if (!mvmsta)
509                 return 0;
510
511         /* nothing to do */
512         if (mvmsta->bt_reduced_txpower_dbg ||
513             mvmsta->bt_reduced_txpower == enable)
514                 return 0;
515
516         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
517         if (!bt_cmd)
518                 return -ENOMEM;
519         cmd.data[0] = bt_cmd;
520         bt_cmd->flags = cpu_to_le32(BT_COEX_NW);
521
522         bt_cmd->valid_bit_msk =
523                 cpu_to_le32(BT_VALID_ENABLE | BT_VALID_REDUCED_TX_POWER);
524         bt_cmd->bt_reduced_tx_power = sta_id;
525
526         if (enable)
527                 bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
528
529         IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
530                        enable ? "en" : "dis", sta_id);
531
532         mvmsta->bt_reduced_txpower = enable;
533
534         ret = iwl_mvm_send_cmd(mvm, &cmd);
535
536         kfree(bt_cmd);
537         return ret;
538 }
539
540 struct iwl_bt_iterator_data {
541         struct iwl_bt_coex_profile_notif *notif;
542         struct iwl_mvm *mvm;
543         u32 num_bss_ifaces;
544         bool reduced_tx_power;
545         struct ieee80211_chanctx_conf *primary;
546         struct ieee80211_chanctx_conf *secondary;
547         bool primary_ll;
548 };
549
550 static inline
551 void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
552                                        struct ieee80211_vif *vif,
553                                        bool enable, int rssi)
554 {
555         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
556
557         mvmvif->bf_data.last_bt_coex_event = rssi;
558         mvmvif->bf_data.bt_coex_max_thold =
559                 enable ? BT_ENABLE_REDUCED_TXPOWER_THRESHOLD : 0;
560         mvmvif->bf_data.bt_coex_min_thold =
561                 enable ? BT_DISABLE_REDUCED_TXPOWER_THRESHOLD : 0;
562 }
563
564 /* must be called under rcu_read_lock */
565 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
566                                       struct ieee80211_vif *vif)
567 {
568         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
569         struct iwl_bt_iterator_data *data = _data;
570         struct iwl_mvm *mvm = data->mvm;
571         struct ieee80211_chanctx_conf *chanctx_conf;
572         enum ieee80211_smps_mode smps_mode;
573         u32 bt_activity_grading;
574         int ave_rssi;
575
576         lockdep_assert_held(&mvm->mutex);
577
578         switch (vif->type) {
579         case NL80211_IFTYPE_STATION:
580                 /* default smps_mode for BSS / P2P client is AUTOMATIC */
581                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
582                 data->num_bss_ifaces++;
583
584                 /*
585                  * Count unassoc BSSes, relax SMSP constraints
586                  * and disable reduced Tx Power
587                  */
588                 if (!vif->bss_conf.assoc) {
589                         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
590                                             smps_mode);
591                         if (iwl_mvm_bt_coex_reduced_txp(mvm,
592                                                         mvmvif->ap_sta_id,
593                                                         false))
594                                 IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
595                         return;
596                 }
597                 break;
598         case NL80211_IFTYPE_AP:
599                 /* default smps_mode for AP / GO is OFF */
600                 smps_mode = IEEE80211_SMPS_OFF;
601                 if (!mvmvif->ap_ibss_active) {
602                         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
603                                             smps_mode);
604                         return;
605                 }
606
607                 /* the Ack / Cts kill mask must be default if AP / GO */
608                 data->reduced_tx_power = false;
609                 break;
610         default:
611                 return;
612         }
613
614         chanctx_conf = rcu_dereference(vif->chanctx_conf);
615
616         /* If channel context is invalid or not on 2.4GHz .. */
617         if ((!chanctx_conf ||
618              chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
619                 /* ... relax constraints and disable rssi events */
620                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
621                                     smps_mode);
622                 if (vif->type == NL80211_IFTYPE_STATION)
623                         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
624                 return;
625         }
626
627         bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
628         if (bt_activity_grading >= BT_HIGH_TRAFFIC)
629                 smps_mode = IEEE80211_SMPS_STATIC;
630         else if (bt_activity_grading >= BT_LOW_TRAFFIC)
631                 smps_mode = vif->type == NL80211_IFTYPE_AP ?
632                                 IEEE80211_SMPS_OFF :
633                                 IEEE80211_SMPS_DYNAMIC;
634         IWL_DEBUG_COEX(data->mvm,
635                        "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
636                        mvmvif->id, data->notif->bt_status, bt_activity_grading,
637                        smps_mode);
638
639         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX, smps_mode);
640
641         /* low latency is always primary */
642         if (iwl_mvm_vif_low_latency(mvmvif)) {
643                 data->primary_ll = true;
644
645                 data->secondary = data->primary;
646                 data->primary = chanctx_conf;
647         }
648
649         if (vif->type == NL80211_IFTYPE_AP) {
650                 if (!mvmvif->ap_ibss_active)
651                         return;
652
653                 if (chanctx_conf == data->primary)
654                         return;
655
656                 if (!data->primary_ll) {
657                         /*
658                          * downgrade the current primary no matter what its
659                          * type is.
660                          */
661                         data->secondary = data->primary;
662                         data->primary = chanctx_conf;
663                 } else {
664                         /* there is low latency vif - we will be secondary */
665                         data->secondary = chanctx_conf;
666                 }
667                 return;
668         }
669
670         /*
671          * STA / P2P Client, try to be primary if first vif. If we are in low
672          * latency mode, we are already in primary and just don't do much
673          */
674         if (!data->primary || data->primary == chanctx_conf)
675                 data->primary = chanctx_conf;
676         else if (!data->secondary)
677                 /* if secondary is not NULL, it might be a GO */
678                 data->secondary = chanctx_conf;
679
680         /* don't reduce the Tx power if in loose scheme */
681         if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
682             mvm->cfg->bt_shared_single_ant) {
683                 data->reduced_tx_power = false;
684                 iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
685                 return;
686         }
687
688         /* reduced Txpower only if BT is on, so ...*/
689         if (!data->notif->bt_status) {
690                 /* ... cancel reduced Tx power ... */
691                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
692                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
693                 data->reduced_tx_power = false;
694
695                 /* ... and there is no need to get reports on RSSI any more. */
696                 iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
697                 return;
698         }
699
700         /* try to get the avg rssi from fw */
701         ave_rssi = mvmvif->bf_data.ave_beacon_signal;
702
703         /* if the RSSI isn't valid, fake it is very low */
704         if (!ave_rssi)
705                 ave_rssi = -100;
706         if (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) {
707                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
708                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
709
710                 /*
711                  * bt_kill_msk can be BT_KILL_MSK_REDUCED_TXPOW only if all the
712                  * BSS / P2P clients have rssi above threshold.
713                  * We set the bt_kill_msk to BT_KILL_MSK_REDUCED_TXPOW before
714                  * the iteration, if one interface's rssi isn't good enough,
715                  * bt_kill_msk will be set to default values.
716                  */
717         } else if (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) {
718                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
719                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
720
721                 /*
722                  * One interface hasn't rssi above threshold, bt_kill_msk must
723                  * be set to default values.
724                  */
725                 data->reduced_tx_power = false;
726         }
727
728         /* Begin to monitor the RSSI: it may influence the reduced Tx power */
729         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
730 }
731
732 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
733 {
734         struct iwl_bt_iterator_data data = {
735                 .mvm = mvm,
736                 .notif = &mvm->last_bt_notif,
737                 .reduced_tx_power = true,
738         };
739         struct iwl_bt_coex_ci_cmd cmd = {};
740         u8 ci_bw_idx;
741
742         rcu_read_lock();
743         ieee80211_iterate_active_interfaces_atomic(
744                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
745                                         iwl_mvm_bt_notif_iterator, &data);
746
747         if (data.primary) {
748                 struct ieee80211_chanctx_conf *chan = data.primary;
749                 if (WARN_ON(!chan->def.chan)) {
750                         rcu_read_unlock();
751                         return;
752                 }
753
754                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
755                         ci_bw_idx = 0;
756                         cmd.co_run_bw_primary = 0;
757                 } else {
758                         cmd.co_run_bw_primary = 1;
759                         if (chan->def.center_freq1 >
760                             chan->def.chan->center_freq)
761                                 ci_bw_idx = 2;
762                         else
763                                 ci_bw_idx = 1;
764                 }
765
766                 cmd.bt_primary_ci =
767                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
768                 cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
769         }
770
771         if (data.secondary) {
772                 struct ieee80211_chanctx_conf *chan = data.secondary;
773                 if (WARN_ON(!data.secondary->def.chan)) {
774                         rcu_read_unlock();
775                         return;
776                 }
777
778                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
779                         ci_bw_idx = 0;
780                         cmd.co_run_bw_secondary = 0;
781                 } else {
782                         cmd.co_run_bw_secondary = 1;
783                         if (chan->def.center_freq1 >
784                             chan->def.chan->center_freq)
785                                 ci_bw_idx = 2;
786                         else
787                                 ci_bw_idx = 1;
788                 }
789
790                 cmd.bt_secondary_ci =
791                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
792                 cmd.secondary_ch_phy_id = *((u16 *)data.secondary->drv_priv);
793         }
794
795         rcu_read_unlock();
796
797         /* Don't spam the fw with the same command over and over */
798         if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
799                 if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, CMD_SYNC,
800                                          sizeof(cmd), &cmd))
801                         IWL_ERR(mvm, "Failed to send BT_CI cmd");
802                 memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
803         }
804
805         /*
806          * If there are no BSS / P2P client interfaces, reduced Tx Power is
807          * irrelevant since it is based on the RSSI coming from the beacon.
808          * Use BT_KILL_MSK_DEFAULT in that case.
809          */
810         data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
811
812         if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
813                 IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
814 }
815
816 /* upon association, the fw will send in BT Coex notification */
817 int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
818                              struct iwl_rx_cmd_buffer *rxb,
819                              struct iwl_device_cmd *dev_cmd)
820 {
821         struct iwl_rx_packet *pkt = rxb_addr(rxb);
822         struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
823
824
825         IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
826         IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
827                        notif->bt_status ? "ON" : "OFF");
828         IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
829         IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
830         IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
831                        le32_to_cpu(notif->primary_ch_lut));
832         IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
833                        le32_to_cpu(notif->secondary_ch_lut));
834         IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
835                        le32_to_cpu(notif->bt_activity_grading));
836         IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
837                        notif->bt_agg_traffic_load);
838
839         /* remember this notification for future use: rssi fluctuations */
840         memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
841
842         iwl_mvm_bt_coex_notif_handle(mvm);
843
844         /*
845          * This is an async handler for a notification, returning anything other
846          * than 0 doesn't make sense even if HCMD failed.
847          */
848         return 0;
849 }
850
851 static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
852                                    struct ieee80211_vif *vif)
853 {
854         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
855         struct iwl_bt_iterator_data *data = _data;
856         struct iwl_mvm *mvm = data->mvm;
857
858         struct ieee80211_sta *sta;
859         struct iwl_mvm_sta *mvmsta;
860
861         struct ieee80211_chanctx_conf *chanctx_conf;
862
863         rcu_read_lock();
864         chanctx_conf = rcu_dereference(vif->chanctx_conf);
865         /* If channel context is invalid or not on 2.4GHz - don't count it */
866         if (!chanctx_conf ||
867             chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
868                 rcu_read_unlock();
869                 return;
870         }
871         rcu_read_unlock();
872
873         if (vif->type != NL80211_IFTYPE_STATION ||
874             mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
875                 return;
876
877         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
878                                         lockdep_is_held(&mvm->mutex));
879
880         /* This can happen if the station has been removed right now */
881         if (IS_ERR_OR_NULL(sta))
882                 return;
883
884         mvmsta = iwl_mvm_sta_from_mac80211(sta);
885
886         data->num_bss_ifaces++;
887
888         /*
889          * This interface doesn't support reduced Tx power (because of low
890          * RSSI probably), then set bt_kill_msk to default values.
891          */
892         if (!mvmsta->bt_reduced_txpower)
893                 data->reduced_tx_power = false;
894         /* else - possibly leave it to BT_KILL_MSK_REDUCED_TXPOW */
895 }
896
897 void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
898                            enum ieee80211_rssi_event rssi_event)
899 {
900         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
901         struct iwl_bt_iterator_data data = {
902                 .mvm = mvm,
903                 .reduced_tx_power = true,
904         };
905         int ret;
906
907         lockdep_assert_held(&mvm->mutex);
908
909         /*
910          * Rssi update while not associated - can happen since the statistics
911          * are handled asynchronously
912          */
913         if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
914                 return;
915
916         /* No BT - reports should be disabled */
917         if (!mvm->last_bt_notif.bt_status)
918                 return;
919
920         IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
921                        rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
922
923         /*
924          * Check if rssi is good enough for reduced Tx power, but not in loose
925          * scheme.
926          */
927         if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
928             iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
929                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
930                                                   false);
931         else
932                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
933
934         if (ret)
935                 IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
936
937         ieee80211_iterate_active_interfaces_atomic(
938                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
939                 iwl_mvm_bt_rssi_iterator, &data);
940
941         /*
942          * If there are no BSS / P2P client interfaces, reduced Tx Power is
943          * irrelevant since it is based on the RSSI coming from the beacon.
944          * Use BT_KILL_MSK_DEFAULT in that case.
945          */
946         data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
947
948         if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
949                 IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
950 }
951
952 #define LINK_QUAL_AGG_TIME_LIMIT_DEF    (4000)
953 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
954
955 u16 iwl_mvm_bt_coex_agg_time_limit(struct iwl_mvm *mvm,
956                                    struct ieee80211_sta *sta)
957 {
958         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
959         enum iwl_bt_coex_lut_type lut_type;
960
961         if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
962             BT_HIGH_TRAFFIC)
963                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
964
965         lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
966
967         if (lut_type == BT_COEX_LOOSE_LUT)
968                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
969
970         /* tight coex, high bt traffic, reduce AGG time limit */
971         return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
972 }
973
974 bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
975                                      struct ieee80211_sta *sta)
976 {
977         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
978
979         if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
980             BT_HIGH_TRAFFIC)
981                 return true;
982
983         /*
984          * In Tight, BT can't Rx while we Tx, so use both antennas since BT is
985          * already killed.
986          * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while we
987          * Tx.
988          */
989         return iwl_get_coex_type(mvm, mvmsta->vif) == BT_COEX_TIGHT_LUT;
990 }
991
992 void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
993 {
994         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
995                 return;
996
997         iwl_mvm_bt_coex_notif_handle(mvm);
998 }