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