1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2013-2014, 2018-2020, 2022 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 	if (mvm->cfg->bt_shared_single_ant) {
120 		rcu_read_unlock();
121 		return ret;
122 	}
123 
124 	phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
125 	primary_ch_phy_id = le32_to_cpu(mvm->last_bt_ci_cmd.primary_ch_phy_id);
126 	secondary_ch_phy_id =
127 		le32_to_cpu(mvm->last_bt_ci_cmd.secondary_ch_phy_id);
128 
129 	if (primary_ch_phy_id == phy_ctx_id)
130 		ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
131 	else if (secondary_ch_phy_id == phy_ctx_id)
132 		ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
133 	/* else - default = TX TX disallowed */
134 
135 	rcu_read_unlock();
136 
137 	return ret;
138 }
139 
iwl_mvm_send_bt_init_conf(struct iwl_mvm * mvm)140 int iwl_mvm_send_bt_init_conf(struct iwl_mvm *mvm)
141 {
142 	struct iwl_bt_coex_cmd bt_cmd = {};
143 	u32 mode;
144 
145 	lockdep_assert_held(&mvm->mutex);
146 
147 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS)) {
148 		switch (mvm->bt_force_ant_mode) {
149 		case BT_FORCE_ANT_BT:
150 			mode = BT_COEX_BT;
151 			break;
152 		case BT_FORCE_ANT_WIFI:
153 			mode = BT_COEX_WIFI;
154 			break;
155 		default:
156 			WARN_ON(1);
157 			mode = 0;
158 		}
159 
160 		bt_cmd.mode = cpu_to_le32(mode);
161 		goto send_cmd;
162 	}
163 
164 	bt_cmd.mode = cpu_to_le32(BT_COEX_NW);
165 
166 	if (IWL_MVM_BT_COEX_SYNC2SCO)
167 		bt_cmd.enabled_modules |=
168 			cpu_to_le32(BT_COEX_SYNC2SCO_ENABLED);
169 
170 	if (iwl_mvm_is_mplut_supported(mvm))
171 		bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_MPLUT_ENABLED);
172 
173 	bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_HIGH_BAND_RET);
174 
175 send_cmd:
176 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
177 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
178 
179 	return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, 0, sizeof(bt_cmd), &bt_cmd);
180 }
181 
iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm * mvm,u8 sta_id,bool enable)182 static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
183 				       bool enable)
184 {
185 	struct iwl_bt_coex_reduced_txp_update_cmd cmd = {};
186 	struct iwl_mvm_sta *mvmsta;
187 	u32 value;
188 
189 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
190 	if (!mvmsta)
191 		return 0;
192 
193 	/* nothing to do */
194 	if (mvmsta->bt_reduced_txpower == enable)
195 		return 0;
196 
197 	value = mvmsta->deflink.sta_id;
198 
199 	if (enable)
200 		value |= BT_REDUCED_TX_POWER_BIT;
201 
202 	IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
203 		       enable ? "en" : "dis", sta_id);
204 
205 	cmd.reduced_txp = cpu_to_le32(value);
206 	mvmsta->bt_reduced_txpower = enable;
207 
208 	return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_UPDATE_REDUCED_TXP,
209 				    CMD_ASYNC, sizeof(cmd), &cmd);
210 }
211 
212 struct iwl_bt_iterator_data {
213 	struct iwl_bt_coex_profile_notif *notif;
214 	struct iwl_mvm *mvm;
215 	struct ieee80211_chanctx_conf *primary;
216 	struct ieee80211_chanctx_conf *secondary;
217 	bool primary_ll;
218 	u8 primary_load;
219 	u8 secondary_load;
220 };
221 
222 static inline
iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool enable,int rssi)223 void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
224 				       struct ieee80211_vif *vif,
225 				       bool enable, int rssi)
226 {
227 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
228 
229 	mvmvif->bf_data.last_bt_coex_event = rssi;
230 	mvmvif->bf_data.bt_coex_max_thold =
231 		enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
232 	mvmvif->bf_data.bt_coex_min_thold =
233 		enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
234 }
235 
236 #define MVM_COEX_TCM_PERIOD (HZ * 10)
237 
iwl_mvm_bt_coex_tcm_based_ci(struct iwl_mvm * mvm,struct iwl_bt_iterator_data * data)238 static void iwl_mvm_bt_coex_tcm_based_ci(struct iwl_mvm *mvm,
239 					 struct iwl_bt_iterator_data *data)
240 {
241 	unsigned long now = jiffies;
242 
243 	if (!time_after(now, mvm->bt_coex_last_tcm_ts + MVM_COEX_TCM_PERIOD))
244 		return;
245 
246 	mvm->bt_coex_last_tcm_ts = now;
247 
248 	/* We assume here that we don't have more than 2 vifs on 2.4GHz */
249 
250 	/* if the primary is low latency, it will stay primary */
251 	if (data->primary_ll)
252 		return;
253 
254 	if (data->primary_load >= data->secondary_load)
255 		return;
256 
257 	swap(data->primary, data->secondary);
258 }
259 
iwl_mvm_bt_notif_per_link(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_bt_iterator_data * data,unsigned int link_id)260 static void iwl_mvm_bt_notif_per_link(struct iwl_mvm *mvm,
261 				      struct ieee80211_vif *vif,
262 				      struct iwl_bt_iterator_data *data,
263 				      unsigned int link_id)
264 {
265 	/* default smps_mode is AUTOMATIC - only used for client modes */
266 	enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
267 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
268 	u32 bt_activity_grading, min_ag_for_static_smps;
269 	struct ieee80211_chanctx_conf *chanctx_conf;
270 	struct iwl_mvm_vif_link_info *link_info;
271 	struct ieee80211_bss_conf *link_conf;
272 	int ave_rssi;
273 
274 	lockdep_assert_held(&mvm->mutex);
275 
276 	link_info = mvmvif->link[link_id];
277 	if (!link_info)
278 		return;
279 
280 	link_conf = rcu_dereference(vif->link_conf[link_id]);
281 	/* This can happen due to races: if we receive the notification
282 	 * and have the mutex held, while mac80211 is stuck on our mutex
283 	 * in the middle of removing the link.
284 	 */
285 	if (!link_conf)
286 		return;
287 
288 	chanctx_conf = rcu_dereference(link_conf->chanctx_conf);
289 
290 	/* If channel context is invalid or not on 2.4GHz .. */
291 	if ((!chanctx_conf ||
292 	     chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
293 		if (vif->type == NL80211_IFTYPE_STATION) {
294 			/* ... relax constraints and disable rssi events */
295 			iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
296 					    smps_mode, link_id);
297 			iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
298 						    false);
299 			/* FIXME: should this be per link? */
300 			iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
301 		}
302 		return;
303 	}
304 
305 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2))
306 		min_ag_for_static_smps = BT_VERY_HIGH_TRAFFIC;
307 	else
308 		min_ag_for_static_smps = BT_HIGH_TRAFFIC;
309 
310 	bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
311 	if (bt_activity_grading >= min_ag_for_static_smps)
312 		smps_mode = IEEE80211_SMPS_STATIC;
313 	else if (bt_activity_grading >= BT_LOW_TRAFFIC)
314 		smps_mode = IEEE80211_SMPS_DYNAMIC;
315 
316 	/* relax SMPS constraints for next association */
317 	if (!vif->cfg.assoc)
318 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
319 
320 	if (link_info->phy_ctxt &&
321 	    (mvm->last_bt_notif.rrc_status & BIT(link_info->phy_ctxt->id)))
322 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
323 
324 	IWL_DEBUG_COEX(data->mvm,
325 		       "mac %d link %d: bt_activity_grading %d smps_req %d\n",
326 		       mvmvif->id, link_info->fw_link_id,
327 		       bt_activity_grading, smps_mode);
328 
329 	if (vif->type == NL80211_IFTYPE_STATION)
330 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
331 				    smps_mode, link_id);
332 
333 	/* low latency is always primary */
334 	if (iwl_mvm_vif_low_latency(mvmvif)) {
335 		data->primary_ll = true;
336 
337 		data->secondary = data->primary;
338 		data->primary = chanctx_conf;
339 	}
340 
341 	if (vif->type == NL80211_IFTYPE_AP) {
342 		if (!mvmvif->ap_ibss_active)
343 			return;
344 
345 		if (chanctx_conf == data->primary)
346 			return;
347 
348 		if (!data->primary_ll) {
349 			/*
350 			 * downgrade the current primary no matter what its
351 			 * type is.
352 			 */
353 			data->secondary = data->primary;
354 			data->primary = chanctx_conf;
355 		} else {
356 			/* there is low latency vif - we will be secondary */
357 			data->secondary = chanctx_conf;
358 		}
359 
360 		/* FIXME: TCM load per interface? or need something per link? */
361 		if (data->primary == chanctx_conf)
362 			data->primary_load = mvm->tcm.result.load[mvmvif->id];
363 		else if (data->secondary == chanctx_conf)
364 			data->secondary_load = mvm->tcm.result.load[mvmvif->id];
365 		return;
366 	}
367 
368 	/*
369 	 * STA / P2P Client, try to be primary if first vif. If we are in low
370 	 * latency mode, we are already in primary and just don't do much
371 	 */
372 	if (!data->primary || data->primary == chanctx_conf)
373 		data->primary = chanctx_conf;
374 	else if (!data->secondary)
375 		/* if secondary is not NULL, it might be a GO */
376 		data->secondary = chanctx_conf;
377 
378 	/* FIXME: TCM load per interface? or need something per link? */
379 	if (data->primary == chanctx_conf)
380 		data->primary_load = mvm->tcm.result.load[mvmvif->id];
381 	else if (data->secondary == chanctx_conf)
382 		data->secondary_load = mvm->tcm.result.load[mvmvif->id];
383 	/*
384 	 * don't reduce the Tx power if one of these is true:
385 	 *  we are in LOOSE
386 	 *  single share antenna product
387 	 *  BT is inactive
388 	 *  we are not associated
389 	 */
390 	if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
391 	    mvm->cfg->bt_shared_single_ant || !vif->cfg.assoc ||
392 	    le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF) {
393 		iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id, false);
394 		/* FIXME: should this be per link? */
395 		iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
396 		return;
397 	}
398 
399 	/* try to get the avg rssi from fw */
400 	ave_rssi = mvmvif->bf_data.ave_beacon_signal;
401 
402 	/* if the RSSI isn't valid, fake it is very low */
403 	if (!ave_rssi)
404 		ave_rssi = -100;
405 	if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
406 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
407 						true))
408 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
409 	} else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
410 		if (iwl_mvm_bt_coex_reduced_txp(mvm, link_info->ap_sta_id,
411 						false))
412 			IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
413 	}
414 
415 	/* Begin to monitor the RSSI: it may influence the reduced Tx power */
416 	iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
417 }
418 
419 /* must be called under rcu_read_lock */
iwl_mvm_bt_notif_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)420 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
421 				      struct ieee80211_vif *vif)
422 {
423 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
424 	struct iwl_bt_iterator_data *data = _data;
425 	struct iwl_mvm *mvm = data->mvm;
426 	unsigned int link_id;
427 
428 	lockdep_assert_held(&mvm->mutex);
429 
430 	switch (vif->type) {
431 	case NL80211_IFTYPE_STATION:
432 		break;
433 	case NL80211_IFTYPE_AP:
434 		if (!mvmvif->ap_ibss_active)
435 			return;
436 		break;
437 	default:
438 		return;
439 	}
440 
441 	for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++)
442 		iwl_mvm_bt_notif_per_link(mvm, vif, data, link_id);
443 }
444 
iwl_mvm_bt_coex_notif_handle(struct iwl_mvm * mvm)445 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
446 {
447 	struct iwl_bt_iterator_data data = {
448 		.mvm = mvm,
449 		.notif = &mvm->last_bt_notif,
450 	};
451 	struct iwl_bt_coex_ci_cmd cmd = {};
452 	u8 ci_bw_idx;
453 
454 	/* Ignore updates if we are in force mode */
455 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
456 		return;
457 
458 	rcu_read_lock();
459 	ieee80211_iterate_active_interfaces_atomic(
460 					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
461 					iwl_mvm_bt_notif_iterator, &data);
462 
463 	iwl_mvm_bt_coex_tcm_based_ci(mvm, &data);
464 
465 	if (data.primary) {
466 		struct ieee80211_chanctx_conf *chan = data.primary;
467 		if (WARN_ON(!chan->def.chan)) {
468 			rcu_read_unlock();
469 			return;
470 		}
471 
472 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
473 			ci_bw_idx = 0;
474 		} else {
475 			if (chan->def.center_freq1 >
476 			    chan->def.chan->center_freq)
477 				ci_bw_idx = 2;
478 			else
479 				ci_bw_idx = 1;
480 		}
481 
482 		cmd.bt_primary_ci =
483 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
484 		cmd.primary_ch_phy_id =
485 			cpu_to_le32(*((u16 *)data.primary->drv_priv));
486 	}
487 
488 	if (data.secondary) {
489 		struct ieee80211_chanctx_conf *chan = data.secondary;
490 		if (WARN_ON(!data.secondary->def.chan)) {
491 			rcu_read_unlock();
492 			return;
493 		}
494 
495 		if (chan->def.width < NL80211_CHAN_WIDTH_40) {
496 			ci_bw_idx = 0;
497 		} else {
498 			if (chan->def.center_freq1 >
499 			    chan->def.chan->center_freq)
500 				ci_bw_idx = 2;
501 			else
502 				ci_bw_idx = 1;
503 		}
504 
505 		cmd.bt_secondary_ci =
506 			iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
507 		cmd.secondary_ch_phy_id =
508 			cpu_to_le32(*((u16 *)data.secondary->drv_priv));
509 	}
510 
511 	rcu_read_unlock();
512 
513 	/* Don't spam the fw with the same command over and over */
514 	if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
515 		if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
516 					 sizeof(cmd), &cmd))
517 			IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
518 		memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
519 	}
520 }
521 
iwl_mvm_rx_bt_coex_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)522 void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
523 			      struct iwl_rx_cmd_buffer *rxb)
524 {
525 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
526 	struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
527 
528 	IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
529 	IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
530 	IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
531 		       le32_to_cpu(notif->primary_ch_lut));
532 	IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
533 		       le32_to_cpu(notif->secondary_ch_lut));
534 	IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
535 		       le32_to_cpu(notif->bt_activity_grading));
536 
537 	/* remember this notification for future use: rssi fluctuations */
538 	memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
539 
540 	iwl_mvm_bt_coex_notif_handle(mvm);
541 }
542 
iwl_mvm_bt_rssi_event(struct iwl_mvm * mvm,struct ieee80211_vif * vif,enum ieee80211_rssi_event_data rssi_event)543 void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
544 			   enum ieee80211_rssi_event_data rssi_event)
545 {
546 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
547 	int ret;
548 
549 	lockdep_assert_held(&mvm->mutex);
550 
551 	/* Ignore updates if we are in force mode */
552 	if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
553 		return;
554 
555 	/*
556 	 * Rssi update while not associated - can happen since the statistics
557 	 * are handled asynchronously
558 	 */
559 	if (mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA)
560 		return;
561 
562 	/* No BT - reports should be disabled */
563 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
564 		return;
565 
566 	IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
567 		       rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
568 
569 	/*
570 	 * Check if rssi is good enough for reduced Tx power, but not in loose
571 	 * scheme.
572 	 */
573 	if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
574 	    iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
575 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
576 						  mvmvif->deflink.ap_sta_id,
577 						  false);
578 	else
579 		ret = iwl_mvm_bt_coex_reduced_txp(mvm,
580 						  mvmvif->deflink.ap_sta_id,
581 						  true);
582 
583 	if (ret)
584 		IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
585 }
586 
587 #define LINK_QUAL_AGG_TIME_LIMIT_DEF	(4000)
588 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT	(1200)
589 
iwl_mvm_coex_agg_time_limit(struct iwl_mvm * mvm,struct ieee80211_sta * sta)590 u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
591 				struct ieee80211_sta *sta)
592 {
593 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
594 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
595 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
596 	enum iwl_bt_coex_lut_type lut_type;
597 
598 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
599 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
600 
601 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
602 	    BT_HIGH_TRAFFIC)
603 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
604 
605 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
606 
607 	if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
608 		return LINK_QUAL_AGG_TIME_LIMIT_DEF;
609 
610 	/* tight coex, high bt traffic, reduce AGG time limit */
611 	return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
612 }
613 
iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm * mvm,struct ieee80211_sta * sta)614 bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
615 				     struct ieee80211_sta *sta)
616 {
617 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
618 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
619 	struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->deflink.phy_ctxt;
620 	enum iwl_bt_coex_lut_type lut_type;
621 
622 	if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
623 		return true;
624 
625 	if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
626 	    BT_HIGH_TRAFFIC)
627 		return true;
628 
629 	/*
630 	 * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
631 	 * since BT is already killed.
632 	 * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
633 	 * we Tx.
634 	 * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
635 	 */
636 	lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
637 	return lut_type != BT_COEX_LOOSE_LUT;
638 }
639 
iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm * mvm,u8 ant)640 bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
641 {
642 	/* there is no other antenna, shared antenna is always available */
643 	if (mvm->cfg->bt_shared_single_ant)
644 		return true;
645 
646 	if (ant & mvm->cfg->non_shared_ant)
647 		return true;
648 
649 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
650 		BT_HIGH_TRAFFIC;
651 }
652 
iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm * mvm)653 bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
654 {
655 	/* there is no other antenna, shared antenna is always available */
656 	if (mvm->cfg->bt_shared_single_ant)
657 		return true;
658 
659 	return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
660 }
661 
iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm * mvm,enum nl80211_band band)662 bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
663 				    enum nl80211_band band)
664 {
665 	u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
666 
667 	if (band != NL80211_BAND_2GHZ)
668 		return false;
669 
670 	return bt_activity >= BT_LOW_TRAFFIC;
671 }
672 
iwl_mvm_bt_coex_get_single_ant_msk(struct iwl_mvm * mvm,u8 enabled_ants)673 u8 iwl_mvm_bt_coex_get_single_ant_msk(struct iwl_mvm *mvm, u8 enabled_ants)
674 {
675 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2) &&
676 	    (mvm->cfg->non_shared_ant & enabled_ants))
677 		return mvm->cfg->non_shared_ant;
678 
679 	return first_antenna(enabled_ants);
680 }
681 
iwl_mvm_bt_coex_tx_prio(struct iwl_mvm * mvm,struct ieee80211_hdr * hdr,struct ieee80211_tx_info * info,u8 ac)682 u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
683 			   struct ieee80211_tx_info *info, u8 ac)
684 {
685 	__le16 fc = hdr->frame_control;
686 	bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
687 
688 	if (info->band != NL80211_BAND_2GHZ)
689 		return 0;
690 
691 	if (unlikely(mvm->bt_tx_prio))
692 		return mvm->bt_tx_prio - 1;
693 
694 	if (likely(ieee80211_is_data(fc))) {
695 		if (likely(ieee80211_is_data_qos(fc))) {
696 			switch (ac) {
697 			case IEEE80211_AC_BE:
698 				return mplut_enabled ? 1 : 0;
699 			case IEEE80211_AC_VI:
700 				return mplut_enabled ? 2 : 3;
701 			case IEEE80211_AC_VO:
702 				return 3;
703 			default:
704 				return 0;
705 			}
706 		} else if (is_multicast_ether_addr(hdr->addr1)) {
707 			return 3;
708 		} else
709 			return 0;
710 	} else if (ieee80211_is_mgmt(fc)) {
711 		return ieee80211_is_disassoc(fc) ? 0 : 3;
712 	} else if (ieee80211_is_ctl(fc)) {
713 		/* ignore cfend and cfendack frames as we never send those */
714 		return 3;
715 	}
716 
717 	return 0;
718 }
719 
iwl_mvm_bt_coex_vif_change(struct iwl_mvm * mvm)720 void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
721 {
722 	iwl_mvm_bt_coex_notif_handle(mvm);
723 }
724