xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/coex.c (revision ef9226cd56b718c79184a3466d32984a51cb449c)
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 iwl_mvm_vif_link_info *link_info,
223 				       bool enable, int rssi)
224 {
225 	link_info->bf_data.last_bt_coex_event = rssi;
226 	link_info->bf_data.bt_coex_max_thold =
227 		enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
228 	link_info->bf_data.bt_coex_min_thold =
229 		enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
230 }
231 
232 #define MVM_COEX_TCM_PERIOD (HZ * 10)
233 
234 static void iwl_mvm_bt_coex_tcm_based_ci(struct iwl_mvm *mvm,
235 					 struct iwl_bt_iterator_data *data)
236 {
237 	unsigned long now = jiffies;
238 
239 	if (!time_after(now, mvm->bt_coex_last_tcm_ts + MVM_COEX_TCM_PERIOD))
240 		return;
241 
242 	mvm->bt_coex_last_tcm_ts = now;
243 
244 	/* We assume here that we don't have more than 2 vifs on 2.4GHz */
245 
246 	/* if the primary is low latency, it will stay primary */
247 	if (data->primary_ll)
248 		return;
249 
250 	if (data->primary_load >= data->secondary_load)
251 		return;
252 
253 	swap(data->primary, data->secondary);
254 }
255 
256 static void iwl_mvm_bt_coex_enable_esr(struct iwl_mvm *mvm,
257 				       struct ieee80211_vif *vif, bool enable)
258 {
259 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
260 	int link_id;
261 
262 	lockdep_assert_held(&mvm->mutex);
263 
264 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
265 		return;
266 
267 	/* Done already */
268 	if (mvmvif->bt_coex_esr_disabled == !enable)
269 		return;
270 
271 	mvmvif->bt_coex_esr_disabled = !enable;
272 
273 	/* Nothing to do */
274 	if (mvmvif->esr_active == enable)
275 		return;
276 
277 	if (enable) {
278 		/* Try to re-enable eSR*/
279 		iwl_mvm_mld_select_links(mvm, vif, false);
280 		return;
281 	}
282 
283 	/*
284 	 * Find the primary link, as we want to switch to it and drop the
285 	 * secondary one.
286 	 */
287 	link_id = iwl_mvm_mld_get_primary_link(mvm, vif, vif->active_links);
288 	WARN_ON(link_id < 0);
289 
290 	ieee80211_set_active_links_async(vif,
291 					 vif->active_links & BIT(link_id));
292 }
293 
294 /*
295  * This function receives the LB link id and checks if eSR should be
296  * enabled or disabled (due to BT coex)
297  */
298 bool
299 iwl_mvm_bt_coex_calculate_esr_mode(struct iwl_mvm *mvm,
300 				   struct ieee80211_vif *vif,
301 				   int link_id, int primary_link)
302 {
303 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
304 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
305 	bool have_wifi_loss_rate =
306 		iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
307 					BT_PROFILE_NOTIFICATION, 0) > 4;
308 	s8 link_rssi = 0;
309 	u8 wifi_loss_rate;
310 
311 	lockdep_assert_held(&mvm->mutex);
312 
313 	if (mvm->last_bt_notif.wifi_loss_low_rssi == BT_OFF)
314 		return true;
315 
316 	 /* If LB link is the primary one we should always disable eSR */
317 	if (link_id == primary_link)
318 		return false;
319 
320 	/* The feature is not supported */
321 	if (!have_wifi_loss_rate)
322 		return true;
323 
324 	/*
325 	 * We might not have a link_info when checking whether we can
326 	 * (re)enable eSR - the LB link might not exist yet
327 	 */
328 	if (link_info)
329 		link_rssi = (s8)link_info->beacon_stats.avg_signal;
330 
331 	/*
332 	 * In case we don't know the RSSI - take the lower wifi loss,
333 	 * so we will more likely enter eSR, and if RSSI is low -
334 	 * we will get an update on this and exit eSR.
335 	 */
336 	if (!link_rssi)
337 		wifi_loss_rate = mvm->last_bt_notif.wifi_loss_mid_high_rssi;
338 
339 	else if (!mvmvif->bt_coex_esr_disabled)
340 		 /* RSSI needs to get really low to disable eSR... */
341 		wifi_loss_rate =
342 			link_rssi <= -IWL_MVM_BT_COEX_DISABLE_ESR_THRESH ?
343 				mvm->last_bt_notif.wifi_loss_low_rssi :
344 				mvm->last_bt_notif.wifi_loss_mid_high_rssi;
345 	else
346 		/* ...And really high before we enable it back */
347 		wifi_loss_rate =
348 			link_rssi <= -IWL_MVM_BT_COEX_ENABLE_ESR_THRESH ?
349 				mvm->last_bt_notif.wifi_loss_low_rssi :
350 				mvm->last_bt_notif.wifi_loss_mid_high_rssi;
351 
352 	return wifi_loss_rate <= IWL_MVM_BT_COEX_WIFI_LOSS_THRESH;
353 }
354 
355 void iwl_mvm_bt_coex_update_vif_esr(struct iwl_mvm *mvm,
356 				    struct ieee80211_vif *vif,
357 				    int link_id)
358 {
359 	unsigned long usable_links = ieee80211_vif_usable_links(vif);
360 	int primary_link = iwl_mvm_mld_get_primary_link(mvm, vif,
361 							usable_links);
362 	bool enable;
363 
364 	/* Not assoc, not MLD vif or only one usable link */
365 	if (primary_link < 0)
366 		return;
367 
368 	enable = iwl_mvm_bt_coex_calculate_esr_mode(mvm, vif, link_id,
369 						    primary_link);
370 
371 	iwl_mvm_bt_coex_enable_esr(mvm, vif, enable);
372 }
373 
374 static void iwl_mvm_bt_notif_per_link(struct iwl_mvm *mvm,
375 				      struct ieee80211_vif *vif,
376 				      struct iwl_bt_iterator_data *data,
377 				      unsigned int link_id)
378 {
379 	/* default smps_mode is AUTOMATIC - only used for client modes */
380 	enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
381 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
382 	u32 bt_activity_grading, min_ag_for_static_smps;
383 	struct ieee80211_chanctx_conf *chanctx_conf;
384 	struct iwl_mvm_vif_link_info *link_info;
385 	struct ieee80211_bss_conf *link_conf;
386 	int ave_rssi;
387 
388 	lockdep_assert_held(&mvm->mutex);
389 
390 	link_info = mvmvif->link[link_id];
391 	if (!link_info)
392 		return;
393 
394 	link_conf = rcu_dereference(vif->link_conf[link_id]);
395 	/* This can happen due to races: if we receive the notification
396 	 * and have the mutex held, while mac80211 is stuck on our mutex
397 	 * in the middle of removing the link.
398 	 */
399 	if (!link_conf)
400 		return;
401 
402 	chanctx_conf = rcu_dereference(link_conf->chanctx_conf);
403 
404 	/* If channel context is invalid or not on 2.4GHz .. */
405 	if ((!chanctx_conf ||
406 	     chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
407 		if (vif->type == NL80211_IFTYPE_STATION) {
408 			/* ... relax constraints and disable rssi events */
409 			iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
410 					    smps_mode, link_id);
411 			iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
412 						    false);
413 			iwl_mvm_bt_coex_enable_rssi_event(mvm, link_info, false,
414 							  0);
415 		}
416 		return;
417 	}
418 
419 	iwl_mvm_bt_coex_update_vif_esr(mvm, vif, link_id);
420 
421 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2))
422 		min_ag_for_static_smps = BT_VERY_HIGH_TRAFFIC;
423 	else
424 		min_ag_for_static_smps = BT_HIGH_TRAFFIC;
425 
426 	bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
427 	if (bt_activity_grading >= min_ag_for_static_smps)
428 		smps_mode = IEEE80211_SMPS_STATIC;
429 	else if (bt_activity_grading >= BT_LOW_TRAFFIC)
430 		smps_mode = IEEE80211_SMPS_DYNAMIC;
431 
432 	/* relax SMPS constraints for next association */
433 	if (!vif->cfg.assoc)
434 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
435 
436 	if (link_info->phy_ctxt &&
437 	    (mvm->last_bt_notif.rrc_status & BIT(link_info->phy_ctxt->id)))
438 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
439 
440 	IWL_DEBUG_COEX(data->mvm,
441 		       "mac %d link %d: bt_activity_grading %d smps_req %d\n",
442 		       mvmvif->id, link_info->fw_link_id,
443 		       bt_activity_grading, smps_mode);
444 
445 	if (vif->type == NL80211_IFTYPE_STATION)
446 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
447 				    smps_mode, link_id);
448 
449 	/* low latency is always primary */
450 	if (iwl_mvm_vif_low_latency(mvmvif)) {
451 		data->primary_ll = true;
452 
453 		data->secondary = data->primary;
454 		data->primary = chanctx_conf;
455 	}
456 
457 	if (vif->type == NL80211_IFTYPE_AP) {
458 		if (!mvmvif->ap_ibss_active)
459 			return;
460 
461 		if (chanctx_conf == data->primary)
462 			return;
463 
464 		if (!data->primary_ll) {
465 			/*
466 			 * downgrade the current primary no matter what its
467 			 * type is.
468 			 */
469 			data->secondary = data->primary;
470 			data->primary = chanctx_conf;
471 		} else {
472 			/* there is low latency vif - we will be secondary */
473 			data->secondary = chanctx_conf;
474 		}
475 
476 		/* FIXME: TCM load per interface? or need something per link? */
477 		if (data->primary == chanctx_conf)
478 			data->primary_load = mvm->tcm.result.load[mvmvif->id];
479 		else if (data->secondary == chanctx_conf)
480 			data->secondary_load = mvm->tcm.result.load[mvmvif->id];
481 		return;
482 	}
483 
484 	/*
485 	 * STA / P2P Client, try to be primary if first vif. If we are in low
486 	 * latency mode, we are already in primary and just don't do much
487 	 */
488 	if (!data->primary || data->primary == chanctx_conf)
489 		data->primary = chanctx_conf;
490 	else if (!data->secondary)
491 		/* if secondary is not NULL, it might be a GO */
492 		data->secondary = chanctx_conf;
493 
494 	/* FIXME: TCM load per interface? or need something per link? */
495 	if (data->primary == chanctx_conf)
496 		data->primary_load = mvm->tcm.result.load[mvmvif->id];
497 	else if (data->secondary == chanctx_conf)
498 		data->secondary_load = mvm->tcm.result.load[mvmvif->id];
499 	/*
500 	 * don't reduce the Tx power if one of these is true:
501 	 *  we are in LOOSE
502 	 *  BT is inactive
503 	 *  we are not associated
504 	 */
505 	if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
506 	    le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF ||
507 	    !vif->cfg.assoc) {
508 		iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id, false);
509 		iwl_mvm_bt_coex_enable_rssi_event(mvm, link_info, false, 0);
510 		return;
511 	}
512 
513 	/* try to get the avg rssi from fw */
514 	ave_rssi = link_info->bf_data.ave_beacon_signal;
515 
516 	/* if the RSSI isn't valid, fake it is very low */
517 	if (!ave_rssi)
518 		ave_rssi = -100;
519 	if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
520 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
521 						true))
522 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
523 	} else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
524 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
525 						false))
526 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
527 	}
528 
529 	/* Begin to monitor the RSSI: it may influence the reduced Tx power */
530 	iwl_mvm_bt_coex_enable_rssi_event(mvm, link_info, true, ave_rssi);
531 }
532 
533 /* must be called under rcu_read_lock */
534 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
535 				      struct ieee80211_vif *vif)
536 {
537 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
538 	struct iwl_bt_iterator_data *data = _data;
539 	struct iwl_mvm *mvm = data->mvm;
540 	unsigned int link_id;
541 
542 	lockdep_assert_held(&mvm->mutex);
543 
544 	switch (vif->type) {
545 	case NL80211_IFTYPE_STATION:
546 		break;
547 	case NL80211_IFTYPE_AP:
548 		if (!mvmvif->ap_ibss_active)
549 			return;
550 		break;
551 	default:
552 		return;
553 	}
554 
555 	/* When BT is off this will be 0 */
556 	if (data->notif->wifi_loss_low_rssi == BT_OFF)
557 		iwl_mvm_bt_coex_enable_esr(mvm, vif, true);
558 
559 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
560 		iwl_mvm_bt_notif_per_link(mvm, vif, data, link_id);
561 }
562 
563 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
564 {
565 	struct iwl_bt_iterator_data data = {
566 		.mvm = mvm,
567 		.notif = &mvm->last_bt_notif,
568 	};
569 	struct iwl_bt_coex_ci_cmd cmd = {};
570 	u8 ci_bw_idx;
571 
572 	/* Ignore updates if we are in force mode */
573 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
574 		return;
575 
576 	rcu_read_lock();
577 	ieee80211_iterate_active_interfaces_atomic(
578 					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
579 					iwl_mvm_bt_notif_iterator, &data);
580 
581 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
582 		rcu_read_unlock();
583 		return;
584 	}
585 
586 	iwl_mvm_bt_coex_tcm_based_ci(mvm, &data);
587 
588 	if (data.primary) {
589 		struct ieee80211_chanctx_conf *chan = data.primary;
590 		if (WARN_ON(!chan->def.chan)) {
591 			rcu_read_unlock();
592 			return;
593 		}
594 
595 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
596 			ci_bw_idx = 0;
597 		} else {
598 			if (chan->def.center_freq1 >
599 			    chan->def.chan->center_freq)
600 				ci_bw_idx = 2;
601 			else
602 				ci_bw_idx = 1;
603 		}
604 
605 		cmd.bt_primary_ci =
606 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
607 		cmd.primary_ch_phy_id =
608 			cpu_to_le32(*((u16 *)data.primary->drv_priv));
609 	}
610 
611 	if (data.secondary) {
612 		struct ieee80211_chanctx_conf *chan = data.secondary;
613 		if (WARN_ON(!data.secondary->def.chan)) {
614 			rcu_read_unlock();
615 			return;
616 		}
617 
618 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
619 			ci_bw_idx = 0;
620 		} else {
621 			if (chan->def.center_freq1 >
622 			    chan->def.chan->center_freq)
623 				ci_bw_idx = 2;
624 			else
625 				ci_bw_idx = 1;
626 		}
627 
628 		cmd.bt_secondary_ci =
629 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
630 		cmd.secondary_ch_phy_id =
631 			cpu_to_le32(*((u16 *)data.secondary->drv_priv));
632 	}
633 
634 	rcu_read_unlock();
635 
636 	/* Don't spam the fw with the same command over and over */
637 	if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
638 		if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
639 					 sizeof(cmd), &cmd))
640 			IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
641 		memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
642 	}
643 }
644 
645 void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
646 			      struct iwl_rx_cmd_buffer *rxb)
647 {
648 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
649 	struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
650 
651 	IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
652 	IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
653 	IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
654 		       le32_to_cpu(notif->primary_ch_lut));
655 	IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
656 		       le32_to_cpu(notif->secondary_ch_lut));
657 	IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
658 		       le32_to_cpu(notif->bt_activity_grading));
659 
660 	/* remember this notification for future use: rssi fluctuations */
661 	memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
662 
663 	iwl_mvm_bt_coex_notif_handle(mvm);
664 }
665 
666 void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
667 			   enum ieee80211_rssi_event_data rssi_event)
668 {
669 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
670 	int ret;
671 
672 	lockdep_assert_held(&mvm->mutex);
673 
674 	/* Ignore updates if we are in force mode */
675 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
676 		return;
677 
678 	/*
679 	 * Rssi update while not associated - can happen since the statistics
680 	 * are handled asynchronously
681 	 */
682 	if (mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA)
683 		return;
684 
685 	/* No BT - reports should be disabled */
686 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
687 		return;
688 
689 	IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
690 		       rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
691 
692 	/*
693 	 * Check if rssi is good enough for reduced Tx power, but not in loose
694 	 * scheme.
695 	 */
696 	if (rssi_event == RSSI_EVENT_LOW ||
697 	    iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
698 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
699 						  mvmvif->deflink.ap_sta_id,
700 						  false);
701 	else
702 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
703 						  mvmvif->deflink.ap_sta_id,
704 						  true);
705 
706 	if (ret)
707 		IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
708 }
709 
710 #define LINK_QUAL_AGG_TIME_LIMIT_DEF	(4000)
711 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT	(1200)
712 
713 u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
714 				struct ieee80211_sta *sta)
715 {
716 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
717 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
718 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
719 	enum iwl_bt_coex_lut_type lut_type;
720 
721 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
722 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
723 
724 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
725 	    BT_HIGH_TRAFFIC)
726 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
727 
728 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
729 
730 	if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
731 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
732 
733 	/* tight coex, high bt traffic, reduce AGG time limit */
734 	return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
735 }
736 
737 bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
738 				     struct ieee80211_sta *sta)
739 {
740 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
741 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
742 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
743 	enum iwl_bt_coex_lut_type lut_type;
744 
745 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
746 		return true;
747 
748 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
749 	    BT_HIGH_TRAFFIC)
750 		return true;
751 
752 	/*
753 	 * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
754 	 * since BT is already killed.
755 	 * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
756 	 * we Tx.
757 	 * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
758 	 */
759 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
760 	return lut_type != BT_COEX_LOOSE_LUT;
761 }
762 
763 bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
764 {
765 	if (ant & mvm->cfg->non_shared_ant)
766 		return true;
767 
768 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
769 		BT_HIGH_TRAFFIC;
770 }
771 
772 bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
773 {
774 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
775 }
776 
777 bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
778 				    enum nl80211_band band)
779 {
780 	u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
781 
782 	if (band != NL80211_BAND_2GHZ)
783 		return false;
784 
785 	return bt_activity >= BT_LOW_TRAFFIC;
786 }
787 
788 u8 iwl_mvm_bt_coex_get_single_ant_msk(struct iwl_mvm *mvm, u8 enabled_ants)
789 {
790 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2) &&
791 	    (mvm->cfg->non_shared_ant & enabled_ants))
792 		return mvm->cfg->non_shared_ant;
793 
794 	return first_antenna(enabled_ants);
795 }
796 
797 u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
798 			   struct ieee80211_tx_info *info, u8 ac)
799 {
800 	__le16 fc = hdr->frame_control;
801 	bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
802 
803 	if (info->band != NL80211_BAND_2GHZ)
804 		return 0;
805 
806 	if (unlikely(mvm->bt_tx_prio))
807 		return mvm->bt_tx_prio - 1;
808 
809 	if (likely(ieee80211_is_data(fc))) {
810 		if (likely(ieee80211_is_data_qos(fc))) {
811 			switch (ac) {
812 			case IEEE80211_AC_BE:
813 				return mplut_enabled ? 1 : 0;
814 			case IEEE80211_AC_VI:
815 				return mplut_enabled ? 2 : 3;
816 			case IEEE80211_AC_VO:
817 				return 3;
818 			default:
819 				return 0;
820 			}
821 		} else if (is_multicast_ether_addr(hdr->addr1)) {
822 			return 3;
823 		} else
824 			return 0;
825 	} else if (ieee80211_is_mgmt(fc)) {
826 		return ieee80211_is_disassoc(fc) ? 0 : 3;
827 	} else if (ieee80211_is_ctl(fc)) {
828 		/* ignore cfend and cfendack frames as we never send those */
829 		return 3;
830 	}
831 
832 	return 0;
833 }
834 
835 void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
836 {
837 	iwl_mvm_bt_coex_notif_handle(mvm);
838 }
839