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