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