xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/coex.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2013-2014, 2018-2020, 2022-2023 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  */
6 #include <linux/ieee80211.h>
7 #include <linux/etherdevice.h>
8 #include <net/mac80211.h>
9 
10 #include "fw/api/coex.h"
11 #include "iwl-modparams.h"
12 #include "mvm.h"
13 #include "iwl-debug.h"
14 
15 /* 20MHz / 40MHz below / 40Mhz above*/
16 static const __le64 iwl_ci_mask[][3] = {
17 	/* dummy entry for channel 0 */
18 	{cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
19 	{
20 		cpu_to_le64(0x0000001FFFULL),
21 		cpu_to_le64(0x0ULL),
22 		cpu_to_le64(0x00007FFFFFULL),
23 	},
24 	{
25 		cpu_to_le64(0x000000FFFFULL),
26 		cpu_to_le64(0x0ULL),
27 		cpu_to_le64(0x0003FFFFFFULL),
28 	},
29 	{
30 		cpu_to_le64(0x000003FFFCULL),
31 		cpu_to_le64(0x0ULL),
32 		cpu_to_le64(0x000FFFFFFCULL),
33 	},
34 	{
35 		cpu_to_le64(0x00001FFFE0ULL),
36 		cpu_to_le64(0x0ULL),
37 		cpu_to_le64(0x007FFFFFE0ULL),
38 	},
39 	{
40 		cpu_to_le64(0x00007FFF80ULL),
41 		cpu_to_le64(0x00007FFFFFULL),
42 		cpu_to_le64(0x01FFFFFF80ULL),
43 	},
44 	{
45 		cpu_to_le64(0x0003FFFC00ULL),
46 		cpu_to_le64(0x0003FFFFFFULL),
47 		cpu_to_le64(0x0FFFFFFC00ULL),
48 	},
49 	{
50 		cpu_to_le64(0x000FFFF000ULL),
51 		cpu_to_le64(0x000FFFFFFCULL),
52 		cpu_to_le64(0x3FFFFFF000ULL),
53 	},
54 	{
55 		cpu_to_le64(0x007FFF8000ULL),
56 		cpu_to_le64(0x007FFFFFE0ULL),
57 		cpu_to_le64(0xFFFFFF8000ULL),
58 	},
59 	{
60 		cpu_to_le64(0x01FFFE0000ULL),
61 		cpu_to_le64(0x01FFFFFF80ULL),
62 		cpu_to_le64(0xFFFFFE0000ULL),
63 	},
64 	{
65 		cpu_to_le64(0x0FFFF00000ULL),
66 		cpu_to_le64(0x0FFFFFFC00ULL),
67 		cpu_to_le64(0x0ULL),
68 	},
69 	{
70 		cpu_to_le64(0x3FFFC00000ULL),
71 		cpu_to_le64(0x3FFFFFF000ULL),
72 		cpu_to_le64(0x0)
73 	},
74 	{
75 		cpu_to_le64(0xFFFE000000ULL),
76 		cpu_to_le64(0xFFFFFF8000ULL),
77 		cpu_to_le64(0x0)
78 	},
79 	{
80 		cpu_to_le64(0xFFF8000000ULL),
81 		cpu_to_le64(0xFFFFFE0000ULL),
82 		cpu_to_le64(0x0)
83 	},
84 	{
85 		cpu_to_le64(0xFE00000000ULL),
86 		cpu_to_le64(0x0ULL),
87 		cpu_to_le64(0x0ULL)
88 	},
89 };
90 
91 static enum iwl_bt_coex_lut_type
92 iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
93 {
94 	struct ieee80211_chanctx_conf *chanctx_conf;
95 	enum iwl_bt_coex_lut_type ret;
96 	u16 phy_ctx_id;
97 	u32 primary_ch_phy_id, secondary_ch_phy_id;
98 
99 	/*
100 	 * Checking that we hold mvm->mutex is a good idea, but the rate
101 	 * control can't acquire the mutex since it runs in Tx path.
102 	 * So this is racy in that case, but in the worst case, the AMPDU
103 	 * size limit will be wrong for a short time which is not a big
104 	 * issue.
105 	 */
106 
107 	rcu_read_lock();
108 
109 	chanctx_conf = rcu_dereference(vif->bss_conf.chanctx_conf);
110 
111 	if (!chanctx_conf ||
112 	     chanctx_conf->def.chan->band != NL80211_BAND_2GHZ) {
113 		rcu_read_unlock();
114 		return BT_COEX_INVALID_LUT;
115 	}
116 
117 	ret = BT_COEX_TX_DIS_LUT;
118 
119 	phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
120 	primary_ch_phy_id = le32_to_cpu(mvm->last_bt_ci_cmd.primary_ch_phy_id);
121 	secondary_ch_phy_id =
122 		le32_to_cpu(mvm->last_bt_ci_cmd.secondary_ch_phy_id);
123 
124 	if (primary_ch_phy_id == phy_ctx_id)
125 		ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
126 	else if (secondary_ch_phy_id == phy_ctx_id)
127 		ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
128 	/* else - default = TX TX disallowed */
129 
130 	rcu_read_unlock();
131 
132 	return ret;
133 }
134 
135 int iwl_mvm_send_bt_init_conf(struct iwl_mvm *mvm)
136 {
137 	struct iwl_bt_coex_cmd bt_cmd = {};
138 	u32 mode;
139 
140 	lockdep_assert_held(&mvm->mutex);
141 
142 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS)) {
143 		switch (mvm->bt_force_ant_mode) {
144 		case BT_FORCE_ANT_BT:
145 			mode = BT_COEX_BT;
146 			break;
147 		case BT_FORCE_ANT_WIFI:
148 			mode = BT_COEX_WIFI;
149 			break;
150 		default:
151 			WARN_ON(1);
152 			mode = 0;
153 		}
154 
155 		bt_cmd.mode = cpu_to_le32(mode);
156 		goto send_cmd;
157 	}
158 
159 	bt_cmd.mode = cpu_to_le32(BT_COEX_NW);
160 
161 	if (IWL_MVM_BT_COEX_SYNC2SCO)
162 		bt_cmd.enabled_modules |=
163 			cpu_to_le32(BT_COEX_SYNC2SCO_ENABLED);
164 
165 	if (iwl_mvm_is_mplut_supported(mvm))
166 		bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_MPLUT_ENABLED);
167 
168 	bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_HIGH_BAND_RET);
169 
170 send_cmd:
171 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
172 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
173 
174 	return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, 0, sizeof(bt_cmd), &bt_cmd);
175 }
176 
177 static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
178 				       bool enable)
179 {
180 	struct iwl_bt_coex_reduced_txp_update_cmd cmd = {};
181 	struct iwl_mvm_sta *mvmsta;
182 	u32 value;
183 
184 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
185 		return 0;
186 
187 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
188 	if (!mvmsta)
189 		return 0;
190 
191 	/* nothing to do */
192 	if (mvmsta->bt_reduced_txpower == enable)
193 		return 0;
194 
195 	value = mvmsta->deflink.sta_id;
196 
197 	if (enable)
198 		value |= BT_REDUCED_TX_POWER_BIT;
199 
200 	IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
201 		       enable ? "en" : "dis", sta_id);
202 
203 	cmd.reduced_txp = cpu_to_le32(value);
204 	mvmsta->bt_reduced_txpower = enable;
205 
206 	return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_UPDATE_REDUCED_TXP,
207 				    CMD_ASYNC, sizeof(cmd), &cmd);
208 }
209 
210 struct iwl_bt_iterator_data {
211 	struct iwl_bt_coex_profile_notif *notif;
212 	struct iwl_mvm *mvm;
213 	struct ieee80211_chanctx_conf *primary;
214 	struct ieee80211_chanctx_conf *secondary;
215 	bool primary_ll;
216 	u8 primary_load;
217 	u8 secondary_load;
218 };
219 
220 static inline
221 void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
222 				       struct ieee80211_vif *vif,
223 				       bool enable, int rssi)
224 {
225 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
226 
227 	mvmvif->bf_data.last_bt_coex_event = rssi;
228 	mvmvif->bf_data.bt_coex_max_thold =
229 		enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
230 	mvmvif->bf_data.bt_coex_min_thold =
231 		enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
232 }
233 
234 #define MVM_COEX_TCM_PERIOD (HZ * 10)
235 
236 static void iwl_mvm_bt_coex_tcm_based_ci(struct iwl_mvm *mvm,
237 					 struct iwl_bt_iterator_data *data)
238 {
239 	unsigned long now = jiffies;
240 
241 	if (!time_after(now, mvm->bt_coex_last_tcm_ts + MVM_COEX_TCM_PERIOD))
242 		return;
243 
244 	mvm->bt_coex_last_tcm_ts = now;
245 
246 	/* We assume here that we don't have more than 2 vifs on 2.4GHz */
247 
248 	/* if the primary is low latency, it will stay primary */
249 	if (data->primary_ll)
250 		return;
251 
252 	if (data->primary_load >= data->secondary_load)
253 		return;
254 
255 	swap(data->primary, data->secondary);
256 }
257 
258 static void iwl_mvm_bt_coex_enable_esr(struct iwl_mvm *mvm,
259 				       struct ieee80211_vif *vif, bool enable)
260 {
261 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
262 	int link_id;
263 
264 	lockdep_assert_held(&mvm->mutex);
265 
266 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
267 		return;
268 
269 	/* Done already */
270 	if (mvmvif->bt_coex_esr_disabled == !enable)
271 		return;
272 
273 	mvmvif->bt_coex_esr_disabled = !enable;
274 
275 	/* Nothing to do */
276 	if (mvmvif->esr_active == enable)
277 		return;
278 
279 	if (enable) {
280 		/* Try to re-enable eSR*/
281 		iwl_mvm_mld_select_links(mvm, vif, false);
282 		return;
283 	}
284 
285 	/*
286 	 * Find the primary link, as we want to switch to it and drop the
287 	 * secondary one.
288 	 */
289 	link_id = iwl_mvm_mld_get_primary_link(mvm, vif, vif->active_links);
290 	WARN_ON(link_id < 0);
291 
292 	ieee80211_set_active_links_async(vif,
293 					 vif->active_links & BIT(link_id));
294 }
295 
296 /*
297  * This function receives the LB link id and checks if eSR should be
298  * enabled or disabled (due to BT coex)
299  */
300 bool
301 iwl_mvm_bt_coex_calculate_esr_mode(struct iwl_mvm *mvm,
302 				   struct ieee80211_vif *vif,
303 				   int link_id, int primary_link)
304 {
305 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
306 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
307 	bool have_wifi_loss_rate =
308 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
309 					BT_PROFILE_NOTIFICATION, 0) > 4;
310 	s8 link_rssi = 0;
311 	u8 wifi_loss_rate;
312 
313 	lockdep_assert_held(&mvm->mutex);
314 
315 	if (mvm->last_bt_notif.wifi_loss_low_rssi == BT_OFF)
316 		return true;
317 
318 	 /* If LB link is the primary one we should always disable eSR */
319 	if (link_id == primary_link)
320 		return false;
321 
322 	/* The feature is not supported */
323 	if (!have_wifi_loss_rate)
324 		return true;
325 
326 	/*
327 	 * We might not have a link_info when checking whether we can
328 	 * (re)enable eSR - the LB link might not exist yet
329 	 */
330 	if (link_info)
331 		link_rssi = (s8)link_info->beacon_stats.avg_signal;
332 
333 	/*
334 	 * In case we don't know the RSSI - take the lower wifi loss,
335 	 * so we will more likely enter eSR, and if RSSI is low -
336 	 * we will get an update on this and exit eSR.
337 	 */
338 	if (!link_rssi)
339 		wifi_loss_rate = mvm->last_bt_notif.wifi_loss_mid_high_rssi;
340 
341 	else if (!mvmvif->bt_coex_esr_disabled)
342 		 /* RSSI needs to get really low to disable eSR... */
343 		wifi_loss_rate =
344 			link_rssi <= -IWL_MVM_BT_COEX_DISABLE_ESR_THRESH ?
345 				mvm->last_bt_notif.wifi_loss_low_rssi :
346 				mvm->last_bt_notif.wifi_loss_mid_high_rssi;
347 	else
348 		/* ...And really high before we enable it back */
349 		wifi_loss_rate =
350 			link_rssi <= -IWL_MVM_BT_COEX_ENABLE_ESR_THRESH ?
351 				mvm->last_bt_notif.wifi_loss_low_rssi :
352 				mvm->last_bt_notif.wifi_loss_mid_high_rssi;
353 
354 	return wifi_loss_rate <= IWL_MVM_BT_COEX_WIFI_LOSS_THRESH;
355 }
356 
357 void iwl_mvm_bt_coex_update_vif_esr(struct iwl_mvm *mvm,
358 				    struct ieee80211_vif *vif,
359 				    int link_id)
360 {
361 	unsigned long usable_links = ieee80211_vif_usable_links(vif);
362 	int primary_link = iwl_mvm_mld_get_primary_link(mvm, vif,
363 							usable_links);
364 	bool enable;
365 
366 	/* Not assoc, not MLD vif or only one usable link */
367 	if (primary_link < 0)
368 		return;
369 
370 	enable = iwl_mvm_bt_coex_calculate_esr_mode(mvm, vif, link_id,
371 						    primary_link);
372 
373 	iwl_mvm_bt_coex_enable_esr(mvm, vif, enable);
374 }
375 
376 static void iwl_mvm_bt_notif_per_link(struct iwl_mvm *mvm,
377 				      struct ieee80211_vif *vif,
378 				      struct iwl_bt_iterator_data *data,
379 				      unsigned int link_id)
380 {
381 	/* default smps_mode is AUTOMATIC - only used for client modes */
382 	enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
383 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
384 	u32 bt_activity_grading, min_ag_for_static_smps;
385 	struct ieee80211_chanctx_conf *chanctx_conf;
386 	struct iwl_mvm_vif_link_info *link_info;
387 	struct ieee80211_bss_conf *link_conf;
388 	int ave_rssi;
389 
390 	lockdep_assert_held(&mvm->mutex);
391 
392 	link_info = mvmvif->link[link_id];
393 	if (!link_info)
394 		return;
395 
396 	link_conf = rcu_dereference(vif->link_conf[link_id]);
397 	/* This can happen due to races: if we receive the notification
398 	 * and have the mutex held, while mac80211 is stuck on our mutex
399 	 * in the middle of removing the link.
400 	 */
401 	if (!link_conf)
402 		return;
403 
404 	chanctx_conf = rcu_dereference(link_conf->chanctx_conf);
405 
406 	/* If channel context is invalid or not on 2.4GHz .. */
407 	if ((!chanctx_conf ||
408 	     chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
409 		if (vif->type == NL80211_IFTYPE_STATION) {
410 			/* ... relax constraints and disable rssi events */
411 			iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
412 					    smps_mode, link_id);
413 			iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
414 						    false);
415 			/* FIXME: should this be per link? */
416 			iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
417 		}
418 		return;
419 	}
420 
421 	iwl_mvm_bt_coex_update_vif_esr(mvm, vif, link_id);
422 
423 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2))
424 		min_ag_for_static_smps = BT_VERY_HIGH_TRAFFIC;
425 	else
426 		min_ag_for_static_smps = BT_HIGH_TRAFFIC;
427 
428 	bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
429 	if (bt_activity_grading >= min_ag_for_static_smps)
430 		smps_mode = IEEE80211_SMPS_STATIC;
431 	else if (bt_activity_grading >= BT_LOW_TRAFFIC)
432 		smps_mode = IEEE80211_SMPS_DYNAMIC;
433 
434 	/* relax SMPS constraints for next association */
435 	if (!vif->cfg.assoc)
436 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
437 
438 	if (link_info->phy_ctxt &&
439 	    (mvm->last_bt_notif.rrc_status & BIT(link_info->phy_ctxt->id)))
440 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
441 
442 	IWL_DEBUG_COEX(data->mvm,
443 		       "mac %d link %d: bt_activity_grading %d smps_req %d\n",
444 		       mvmvif->id, link_info->fw_link_id,
445 		       bt_activity_grading, smps_mode);
446 
447 	if (vif->type == NL80211_IFTYPE_STATION)
448 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
449 				    smps_mode, link_id);
450 
451 	/* low latency is always primary */
452 	if (iwl_mvm_vif_low_latency(mvmvif)) {
453 		data->primary_ll = true;
454 
455 		data->secondary = data->primary;
456 		data->primary = chanctx_conf;
457 	}
458 
459 	if (vif->type == NL80211_IFTYPE_AP) {
460 		if (!mvmvif->ap_ibss_active)
461 			return;
462 
463 		if (chanctx_conf == data->primary)
464 			return;
465 
466 		if (!data->primary_ll) {
467 			/*
468 			 * downgrade the current primary no matter what its
469 			 * type is.
470 			 */
471 			data->secondary = data->primary;
472 			data->primary = chanctx_conf;
473 		} else {
474 			/* there is low latency vif - we will be secondary */
475 			data->secondary = chanctx_conf;
476 		}
477 
478 		/* FIXME: TCM load per interface? or need something per link? */
479 		if (data->primary == chanctx_conf)
480 			data->primary_load = mvm->tcm.result.load[mvmvif->id];
481 		else if (data->secondary == chanctx_conf)
482 			data->secondary_load = mvm->tcm.result.load[mvmvif->id];
483 		return;
484 	}
485 
486 	/*
487 	 * STA / P2P Client, try to be primary if first vif. If we are in low
488 	 * latency mode, we are already in primary and just don't do much
489 	 */
490 	if (!data->primary || data->primary == chanctx_conf)
491 		data->primary = chanctx_conf;
492 	else if (!data->secondary)
493 		/* if secondary is not NULL, it might be a GO */
494 		data->secondary = chanctx_conf;
495 
496 	/* FIXME: TCM load per interface? or need something per link? */
497 	if (data->primary == chanctx_conf)
498 		data->primary_load = mvm->tcm.result.load[mvmvif->id];
499 	else if (data->secondary == chanctx_conf)
500 		data->secondary_load = mvm->tcm.result.load[mvmvif->id];
501 	/*
502 	 * don't reduce the Tx power if one of these is true:
503 	 *  we are in LOOSE
504 	 *  BT is inactive
505 	 *  we are not associated
506 	 */
507 	if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
508 	    le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF ||
509 	    !vif->cfg.assoc) {
510 		iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id, false);
511 		/* FIXME: should this be per link? */
512 		iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
513 		return;
514 	}
515 
516 	/* try to get the avg rssi from fw */
517 	ave_rssi = mvmvif->bf_data.ave_beacon_signal;
518 
519 	/* if the RSSI isn't valid, fake it is very low */
520 	if (!ave_rssi)
521 		ave_rssi = -100;
522 	if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
523 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
524 						true))
525 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
526 	} else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
527 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
528 						false))
529 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
530 	}
531 
532 	/* Begin to monitor the RSSI: it may influence the reduced Tx power */
533 	iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
534 }
535 
536 /* must be called under rcu_read_lock */
537 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
538 				      struct ieee80211_vif *vif)
539 {
540 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
541 	struct iwl_bt_iterator_data *data = _data;
542 	struct iwl_mvm *mvm = data->mvm;
543 	unsigned int link_id;
544 
545 	lockdep_assert_held(&mvm->mutex);
546 
547 	switch (vif->type) {
548 	case NL80211_IFTYPE_STATION:
549 		break;
550 	case NL80211_IFTYPE_AP:
551 		if (!mvmvif->ap_ibss_active)
552 			return;
553 		break;
554 	default:
555 		return;
556 	}
557 
558 	/* When BT is off this will be 0 */
559 	if (data->notif->wifi_loss_low_rssi == BT_OFF)
560 		iwl_mvm_bt_coex_enable_esr(mvm, vif, true);
561 
562 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
563 		iwl_mvm_bt_notif_per_link(mvm, vif, data, link_id);
564 }
565 
566 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
567 {
568 	struct iwl_bt_iterator_data data = {
569 		.mvm = mvm,
570 		.notif = &mvm->last_bt_notif,
571 	};
572 	struct iwl_bt_coex_ci_cmd cmd = {};
573 	u8 ci_bw_idx;
574 
575 	/* Ignore updates if we are in force mode */
576 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
577 		return;
578 
579 	rcu_read_lock();
580 	ieee80211_iterate_active_interfaces_atomic(
581 					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
582 					iwl_mvm_bt_notif_iterator, &data);
583 
584 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
585 		rcu_read_unlock();
586 		return;
587 	}
588 
589 	iwl_mvm_bt_coex_tcm_based_ci(mvm, &data);
590 
591 	if (data.primary) {
592 		struct ieee80211_chanctx_conf *chan = data.primary;
593 		if (WARN_ON(!chan->def.chan)) {
594 			rcu_read_unlock();
595 			return;
596 		}
597 
598 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
599 			ci_bw_idx = 0;
600 		} else {
601 			if (chan->def.center_freq1 >
602 			    chan->def.chan->center_freq)
603 				ci_bw_idx = 2;
604 			else
605 				ci_bw_idx = 1;
606 		}
607 
608 		cmd.bt_primary_ci =
609 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
610 		cmd.primary_ch_phy_id =
611 			cpu_to_le32(*((u16 *)data.primary->drv_priv));
612 	}
613 
614 	if (data.secondary) {
615 		struct ieee80211_chanctx_conf *chan = data.secondary;
616 		if (WARN_ON(!data.secondary->def.chan)) {
617 			rcu_read_unlock();
618 			return;
619 		}
620 
621 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
622 			ci_bw_idx = 0;
623 		} else {
624 			if (chan->def.center_freq1 >
625 			    chan->def.chan->center_freq)
626 				ci_bw_idx = 2;
627 			else
628 				ci_bw_idx = 1;
629 		}
630 
631 		cmd.bt_secondary_ci =
632 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
633 		cmd.secondary_ch_phy_id =
634 			cpu_to_le32(*((u16 *)data.secondary->drv_priv));
635 	}
636 
637 	rcu_read_unlock();
638 
639 	/* Don't spam the fw with the same command over and over */
640 	if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
641 		if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
642 					 sizeof(cmd), &cmd))
643 			IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
644 		memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
645 	}
646 }
647 
648 void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
649 			      struct iwl_rx_cmd_buffer *rxb)
650 {
651 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
652 	struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
653 
654 	IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
655 	IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
656 	IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
657 		       le32_to_cpu(notif->primary_ch_lut));
658 	IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
659 		       le32_to_cpu(notif->secondary_ch_lut));
660 	IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
661 		       le32_to_cpu(notif->bt_activity_grading));
662 
663 	/* remember this notification for future use: rssi fluctuations */
664 	memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
665 
666 	iwl_mvm_bt_coex_notif_handle(mvm);
667 }
668 
669 void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
670 			   enum ieee80211_rssi_event_data rssi_event)
671 {
672 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
673 	int ret;
674 
675 	lockdep_assert_held(&mvm->mutex);
676 
677 	/* Ignore updates if we are in force mode */
678 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
679 		return;
680 
681 	/*
682 	 * Rssi update while not associated - can happen since the statistics
683 	 * are handled asynchronously
684 	 */
685 	if (mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA)
686 		return;
687 
688 	/* No BT - reports should be disabled */
689 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
690 		return;
691 
692 	IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
693 		       rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
694 
695 	/*
696 	 * Check if rssi is good enough for reduced Tx power, but not in loose
697 	 * scheme.
698 	 */
699 	if (rssi_event == RSSI_EVENT_LOW ||
700 	    iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
701 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
702 						  mvmvif->deflink.ap_sta_id,
703 						  false);
704 	else
705 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
706 						  mvmvif->deflink.ap_sta_id,
707 						  true);
708 
709 	if (ret)
710 		IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
711 }
712 
713 #define LINK_QUAL_AGG_TIME_LIMIT_DEF	(4000)
714 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT	(1200)
715 
716 u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
717 				struct ieee80211_sta *sta)
718 {
719 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
720 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
721 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
722 	enum iwl_bt_coex_lut_type lut_type;
723 
724 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
725 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
726 
727 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
728 	    BT_HIGH_TRAFFIC)
729 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
730 
731 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
732 
733 	if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
734 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
735 
736 	/* tight coex, high bt traffic, reduce AGG time limit */
737 	return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
738 }
739 
740 bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
741 				     struct ieee80211_sta *sta)
742 {
743 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
744 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
745 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
746 	enum iwl_bt_coex_lut_type lut_type;
747 
748 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
749 		return true;
750 
751 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
752 	    BT_HIGH_TRAFFIC)
753 		return true;
754 
755 	/*
756 	 * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
757 	 * since BT is already killed.
758 	 * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
759 	 * we Tx.
760 	 * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
761 	 */
762 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
763 	return lut_type != BT_COEX_LOOSE_LUT;
764 }
765 
766 bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
767 {
768 	if (ant & mvm->cfg->non_shared_ant)
769 		return true;
770 
771 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
772 		BT_HIGH_TRAFFIC;
773 }
774 
775 bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
776 {
777 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
778 }
779 
780 bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
781 				    enum nl80211_band band)
782 {
783 	u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
784 
785 	if (band != NL80211_BAND_2GHZ)
786 		return false;
787 
788 	return bt_activity >= BT_LOW_TRAFFIC;
789 }
790 
791 u8 iwl_mvm_bt_coex_get_single_ant_msk(struct iwl_mvm *mvm, u8 enabled_ants)
792 {
793 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2) &&
794 	    (mvm->cfg->non_shared_ant & enabled_ants))
795 		return mvm->cfg->non_shared_ant;
796 
797 	return first_antenna(enabled_ants);
798 }
799 
800 u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
801 			   struct ieee80211_tx_info *info, u8 ac)
802 {
803 	__le16 fc = hdr->frame_control;
804 	bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
805 
806 	if (info->band != NL80211_BAND_2GHZ)
807 		return 0;
808 
809 	if (unlikely(mvm->bt_tx_prio))
810 		return mvm->bt_tx_prio - 1;
811 
812 	if (likely(ieee80211_is_data(fc))) {
813 		if (likely(ieee80211_is_data_qos(fc))) {
814 			switch (ac) {
815 			case IEEE80211_AC_BE:
816 				return mplut_enabled ? 1 : 0;
817 			case IEEE80211_AC_VI:
818 				return mplut_enabled ? 2 : 3;
819 			case IEEE80211_AC_VO:
820 				return 3;
821 			default:
822 				return 0;
823 			}
824 		} else if (is_multicast_ether_addr(hdr->addr1)) {
825 			return 3;
826 		} else
827 			return 0;
828 	} else if (ieee80211_is_mgmt(fc)) {
829 		return ieee80211_is_disassoc(fc) ? 0 : 3;
830 	} else if (ieee80211_is_ctl(fc)) {
831 		/* ignore cfend and cfendack frames as we never send those */
832 		return 3;
833 	}
834 
835 	return 0;
836 }
837 
838 void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
839 {
840 	iwl_mvm_bt_coex_notif_handle(mvm);
841 }
842