Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.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 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 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 "fw-api-bt-coex.h"
65 #include "iwl-modparams.h"
66 #include "mvm.h"
67 #include "iwl-debug.h"
68
69 #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant)                  \
70         [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) |    \
71                    ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
72
73 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
74         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
75                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
76         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
77                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
78         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
79                        BT_COEX_PRIO_TBL_PRIO_LOW, 0),
80         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
81                        BT_COEX_PRIO_TBL_PRIO_LOW, 1),
82         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
83                        BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
84         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
85                        BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
86         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
87                        BT_COEX_PRIO_TBL_DISABLED, 0),
88         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
89                        BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
90         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
91                        BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
92         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
93                        BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
94         0, 0, 0, 0, 0, 0,
95 };
96
97 #undef EVENT_PRIO_ANT
98
99 int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
100 {
101         return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, CMD_SYNC,
102                                     sizeof(struct iwl_bt_coex_prio_tbl_cmd),
103                                     &iwl_bt_prio_tbl);
104 }
105
106 static int iwl_send_bt_env(struct iwl_mvm *mvm, u8 action, u8 type)
107 {
108         struct iwl_bt_coex_prot_env_cmd env_cmd;
109         int ret;
110
111         env_cmd.action = action;
112         env_cmd.type = type;
113         ret = iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PROT_ENV, CMD_SYNC,
114                                    sizeof(env_cmd), &env_cmd);
115         if (ret)
116                 IWL_ERR(mvm, "failed to send BT env command\n");
117         return ret;
118 }
119
120 enum iwl_bt_kill_msk {
121         BT_KILL_MSK_DEFAULT,
122         BT_KILL_MSK_SCO_HID_A2DP,
123         BT_KILL_MSK_REDUCED_TXPOW,
124         BT_KILL_MSK_MAX,
125 };
126
127 static const u32 iwl_bt_ack_kill_msk[BT_KILL_MSK_MAX] = {
128         [BT_KILL_MSK_DEFAULT] = 0xffff0000,
129         [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
130         [BT_KILL_MSK_REDUCED_TXPOW] = 0,
131 };
132
133 static const u32 iwl_bt_cts_kill_msk[BT_KILL_MSK_MAX] = {
134         [BT_KILL_MSK_DEFAULT] = 0xffff0000,
135         [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
136         [BT_KILL_MSK_REDUCED_TXPOW] = 0,
137 };
138
139 #define IWL_BT_DEFAULT_BOOST (0xf0f0f0f0)
140
141 /* Tight Coex */
142 static const __le32 iwl_tight_lookup[BT_COEX_LUT_SIZE] = {
143         cpu_to_le32(0xaaaaaaaa),
144         cpu_to_le32(0xaaaaaaaa),
145         cpu_to_le32(0xaeaaaaaa),
146         cpu_to_le32(0xaaaaaaaa),
147         cpu_to_le32(0xcc00ff28),
148         cpu_to_le32(0x0000aaaa),
149         cpu_to_le32(0xcc00aaaa),
150         cpu_to_le32(0x0000aaaa),
151         cpu_to_le32(0xc0004000),
152         cpu_to_le32(0x00000000),
153         cpu_to_le32(0xf0005000),
154         cpu_to_le32(0xf0005000),
155 };
156
157 /* Loose Coex */
158 static const __le32 iwl_loose_lookup[BT_COEX_LUT_SIZE] = {
159         cpu_to_le32(0xaaaaaaaa),
160         cpu_to_le32(0xaaaaaaaa),
161         cpu_to_le32(0xaeaaaaaa),
162         cpu_to_le32(0xaaaaaaaa),
163         cpu_to_le32(0xcc00ff28),
164         cpu_to_le32(0x0000aaaa),
165         cpu_to_le32(0xcc00aaaa),
166         cpu_to_le32(0x0000aaaa),
167         cpu_to_le32(0x00000000),
168         cpu_to_le32(0x00000000),
169         cpu_to_le32(0xf0005000),
170         cpu_to_le32(0xf0005000),
171 };
172
173 /* Full concurrency */
174 static const __le32 iwl_concurrent_lookup[BT_COEX_LUT_SIZE] = {
175         cpu_to_le32(0xaaaaaaaa),
176         cpu_to_le32(0xaaaaaaaa),
177         cpu_to_le32(0xaaaaaaaa),
178         cpu_to_le32(0xaaaaaaaa),
179         cpu_to_le32(0xaaaaaaaa),
180         cpu_to_le32(0xaaaaaaaa),
181         cpu_to_le32(0xaaaaaaaa),
182         cpu_to_le32(0xaaaaaaaa),
183         cpu_to_le32(0x00000000),
184         cpu_to_le32(0x00000000),
185         cpu_to_le32(0x00000000),
186         cpu_to_le32(0x00000000),
187 };
188
189 /* BT Antenna Coupling Threshold (dB) */
190 #define IWL_BT_ANTENNA_COUPLING_THRESHOLD       (35)
191 #define IWL_BT_LOAD_FORCE_SISO_THRESHOLD        (3)
192
193
194 int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
195 {
196         struct iwl_bt_coex_cmd cmd = {
197                 .max_kill = 5,
198                 .bt3_time_t7_value = 1,
199                 .bt3_prio_sample_time = 2,
200                 .bt3_timer_t2_value = 0xc,
201         };
202         int ret;
203
204         cmd.flags = iwlwifi_mod_params.bt_coex_active ?
205                         BT_COEX_NW : BT_COEX_DISABLE;
206         cmd.flags |= iwlwifi_mod_params.bt_ch_announce ? BT_CH_PRIMARY_EN : 0;
207         cmd.flags |= BT_SYNC_2_BT_DISABLE;
208
209         cmd.valid_bit_msk = cpu_to_le16(BT_VALID_ENABLE |
210                                         BT_VALID_BT_PRIO_BOOST |
211                                         BT_VALID_MAX_KILL |
212                                         BT_VALID_3W_TMRS |
213                                         BT_VALID_KILL_ACK |
214                                         BT_VALID_KILL_CTS |
215                                         BT_VALID_REDUCED_TX_POWER |
216                                         BT_VALID_LUT);
217
218         if (iwlwifi_mod_params.ant_coupling > IWL_BT_ANTENNA_COUPLING_THRESHOLD)
219                 memcpy(&cmd.decision_lut, iwl_loose_lookup,
220                        sizeof(iwl_tight_lookup));
221         else
222                 memcpy(&cmd.decision_lut, iwl_tight_lookup,
223                        sizeof(iwl_tight_lookup));
224
225         cmd.bt_prio_boost = cpu_to_le32(IWL_BT_DEFAULT_BOOST);
226         cmd.kill_ack_msk =
227                 cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
228         cmd.kill_cts_msk =
229                 cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
230
231         /* go to CALIB state in internal BT-Coex state machine */
232         ret = iwl_send_bt_env(mvm, BT_COEX_ENV_OPEN,
233                               BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
234         if (ret)
235                 return ret;
236
237         ret  = iwl_send_bt_env(mvm, BT_COEX_ENV_CLOSE,
238                                BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
239         if (ret)
240                 return ret;
241
242         return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, CMD_SYNC,
243                                     sizeof(cmd), &cmd);
244 }
245
246 struct iwl_bt_notif_iterator_data {
247         struct iwl_mvm *mvm;
248         struct iwl_bt_coex_profile_notif *notif;
249 };
250
251 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
252                                       struct ieee80211_vif *vif)
253 {
254         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
255         struct iwl_bt_notif_iterator_data *data = _data;
256         struct ieee80211_chanctx_conf *chanctx_conf;
257         enum ieee80211_smps_mode smps_mode;
258         enum ieee80211_band band;
259
260         if (vif->type != NL80211_IFTYPE_STATION)
261                 return;
262
263         rcu_read_lock();
264         chanctx_conf = rcu_dereference(vif->chanctx_conf);
265         if (chanctx_conf && chanctx_conf->def.chan)
266                 band = chanctx_conf->def.chan->band;
267         else
268                 band = -1;
269         rcu_read_unlock();
270
271         if (band != IEEE80211_BAND_2GHZ)
272                 return;
273
274         smps_mode = IEEE80211_SMPS_AUTOMATIC;
275
276         if (data->notif->bt_status)
277                 smps_mode = IEEE80211_SMPS_DYNAMIC;
278
279         if (data->notif->bt_traffic_load >= IWL_BT_LOAD_FORCE_SISO_THRESHOLD)
280                 smps_mode = IEEE80211_SMPS_STATIC;
281
282         IWL_DEBUG_COEX(data->mvm,
283                        "mac %d: bt_status %d traffic_load %d smps_req %d\n",
284                        mvmvif->id,  data->notif->bt_status,
285                        data->notif->bt_traffic_load, smps_mode);
286
287         ieee80211_request_smps(vif, smps_mode);
288 }
289
290 int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
291                              struct iwl_rx_cmd_buffer *rxb,
292                              struct iwl_device_cmd *dev_cmd)
293 {
294         struct iwl_rx_packet *pkt = rxb_addr(rxb);
295         struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
296         struct iwl_bt_notif_iterator_data data = {
297                 .mvm = mvm,
298                 .notif = notif,
299         };
300         struct iwl_bt_coex_cmd cmd = {};
301         enum iwl_bt_kill_msk bt_kill_msk;
302
303         IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
304         IWL_DEBUG_COEX(mvm, "\tBT %salive\n", notif->bt_status ? "" : "not ");
305         IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
306         IWL_DEBUG_COEX(mvm, "\tBT traffic load %d\n", notif->bt_traffic_load);
307         IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
308                        notif->bt_agg_traffic_load);
309         IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
310
311         /* remember this notification for future use: rssi fluctuations */
312         memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
313
314         ieee80211_iterate_active_interfaces_atomic(
315                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
316                                         iwl_mvm_bt_notif_iterator, &data);
317
318         /* Low latency BT profile is active: give higher prio to BT */
319         if (BT_MBOX_MSG(notif, 3, SCO_STATE)  ||
320             BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
321             BT_MBOX_MSG(notif, 3, SNIFF_STATE))
322                 bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
323         else
324                 bt_kill_msk = BT_KILL_MSK_DEFAULT;
325
326         /* Don't send HCMD if there is no update */
327         if (bt_kill_msk == mvm->bt_kill_msk)
328                 return 0;
329
330         IWL_DEBUG_COEX(mvm,
331                        "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
332                        bt_kill_msk,
333                        BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
334                        BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
335                        BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
336
337         mvm->bt_kill_msk = bt_kill_msk;
338         cmd.kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
339         cmd.kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
340
341         cmd.valid_bit_msk = cpu_to_le16(BT_VALID_KILL_ACK | BT_VALID_KILL_CTS);
342
343         if (iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, CMD_SYNC, sizeof(cmd), &cmd))
344                 IWL_ERR(mvm, "Failed to sent BT Coex CMD\n");
345
346         /* This handler is ASYNC */
347         return 0;
348 }