Linux Audio

Check our new training course

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