coex.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/ieee80211.h>
  68. #include <linux/etherdevice.h>
  69. #include <net/mac80211.h>
  70. #include "fw/api/coex.h"
  71. #include "iwl-modparams.h"
  72. #include "mvm.h"
  73. #include "iwl-debug.h"
  74. /* 20MHz / 40MHz below / 40Mhz above*/
  75. static const __le64 iwl_ci_mask[][3] = {
  76. /* dummy entry for channel 0 */
  77. {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
  78. {
  79. cpu_to_le64(0x0000001FFFULL),
  80. cpu_to_le64(0x0ULL),
  81. cpu_to_le64(0x00007FFFFFULL),
  82. },
  83. {
  84. cpu_to_le64(0x000000FFFFULL),
  85. cpu_to_le64(0x0ULL),
  86. cpu_to_le64(0x0003FFFFFFULL),
  87. },
  88. {
  89. cpu_to_le64(0x000003FFFCULL),
  90. cpu_to_le64(0x0ULL),
  91. cpu_to_le64(0x000FFFFFFCULL),
  92. },
  93. {
  94. cpu_to_le64(0x00001FFFE0ULL),
  95. cpu_to_le64(0x0ULL),
  96. cpu_to_le64(0x007FFFFFE0ULL),
  97. },
  98. {
  99. cpu_to_le64(0x00007FFF80ULL),
  100. cpu_to_le64(0x00007FFFFFULL),
  101. cpu_to_le64(0x01FFFFFF80ULL),
  102. },
  103. {
  104. cpu_to_le64(0x0003FFFC00ULL),
  105. cpu_to_le64(0x0003FFFFFFULL),
  106. cpu_to_le64(0x0FFFFFFC00ULL),
  107. },
  108. {
  109. cpu_to_le64(0x000FFFF000ULL),
  110. cpu_to_le64(0x000FFFFFFCULL),
  111. cpu_to_le64(0x3FFFFFF000ULL),
  112. },
  113. {
  114. cpu_to_le64(0x007FFF8000ULL),
  115. cpu_to_le64(0x007FFFFFE0ULL),
  116. cpu_to_le64(0xFFFFFF8000ULL),
  117. },
  118. {
  119. cpu_to_le64(0x01FFFE0000ULL),
  120. cpu_to_le64(0x01FFFFFF80ULL),
  121. cpu_to_le64(0xFFFFFE0000ULL),
  122. },
  123. {
  124. cpu_to_le64(0x0FFFF00000ULL),
  125. cpu_to_le64(0x0FFFFFFC00ULL),
  126. cpu_to_le64(0x0ULL),
  127. },
  128. {
  129. cpu_to_le64(0x3FFFC00000ULL),
  130. cpu_to_le64(0x3FFFFFF000ULL),
  131. cpu_to_le64(0x0)
  132. },
  133. {
  134. cpu_to_le64(0xFFFE000000ULL),
  135. cpu_to_le64(0xFFFFFF8000ULL),
  136. cpu_to_le64(0x0)
  137. },
  138. {
  139. cpu_to_le64(0xFFF8000000ULL),
  140. cpu_to_le64(0xFFFFFE0000ULL),
  141. cpu_to_le64(0x0)
  142. },
  143. {
  144. cpu_to_le64(0xFE00000000ULL),
  145. cpu_to_le64(0x0ULL),
  146. cpu_to_le64(0x0ULL)
  147. },
  148. };
  149. static enum iwl_bt_coex_lut_type
  150. iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
  151. {
  152. struct ieee80211_chanctx_conf *chanctx_conf;
  153. enum iwl_bt_coex_lut_type ret;
  154. u16 phy_ctx_id;
  155. u32 primary_ch_phy_id, secondary_ch_phy_id;
  156. /*
  157. * Checking that we hold mvm->mutex is a good idea, but the rate
  158. * control can't acquire the mutex since it runs in Tx path.
  159. * So this is racy in that case, but in the worst case, the AMPDU
  160. * size limit will be wrong for a short time which is not a big
  161. * issue.
  162. */
  163. rcu_read_lock();
  164. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  165. if (!chanctx_conf ||
  166. chanctx_conf->def.chan->band != NL80211_BAND_2GHZ) {
  167. rcu_read_unlock();
  168. return BT_COEX_INVALID_LUT;
  169. }
  170. ret = BT_COEX_TX_DIS_LUT;
  171. if (mvm->cfg->bt_shared_single_ant) {
  172. rcu_read_unlock();
  173. return ret;
  174. }
  175. phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
  176. primary_ch_phy_id = le32_to_cpu(mvm->last_bt_ci_cmd.primary_ch_phy_id);
  177. secondary_ch_phy_id =
  178. le32_to_cpu(mvm->last_bt_ci_cmd.secondary_ch_phy_id);
  179. if (primary_ch_phy_id == phy_ctx_id)
  180. ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
  181. else if (secondary_ch_phy_id == phy_ctx_id)
  182. ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
  183. /* else - default = TX TX disallowed */
  184. rcu_read_unlock();
  185. return ret;
  186. }
  187. int iwl_mvm_send_bt_init_conf(struct iwl_mvm *mvm)
  188. {
  189. struct iwl_bt_coex_cmd bt_cmd = {};
  190. u32 mode;
  191. lockdep_assert_held(&mvm->mutex);
  192. if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS)) {
  193. switch (mvm->bt_force_ant_mode) {
  194. case BT_FORCE_ANT_BT:
  195. mode = BT_COEX_BT;
  196. break;
  197. case BT_FORCE_ANT_WIFI:
  198. mode = BT_COEX_WIFI;
  199. break;
  200. default:
  201. WARN_ON(1);
  202. mode = 0;
  203. }
  204. bt_cmd.mode = cpu_to_le32(mode);
  205. goto send_cmd;
  206. }
  207. mode = iwlwifi_mod_params.bt_coex_active ? BT_COEX_NW : BT_COEX_DISABLE;
  208. bt_cmd.mode = cpu_to_le32(mode);
  209. if (IWL_MVM_BT_COEX_SYNC2SCO)
  210. bt_cmd.enabled_modules |=
  211. cpu_to_le32(BT_COEX_SYNC2SCO_ENABLED);
  212. if (iwl_mvm_is_mplut_supported(mvm))
  213. bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_MPLUT_ENABLED);
  214. bt_cmd.enabled_modules |= cpu_to_le32(BT_COEX_HIGH_BAND_RET);
  215. send_cmd:
  216. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  217. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  218. return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, 0, sizeof(bt_cmd), &bt_cmd);
  219. }
  220. static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
  221. bool enable)
  222. {
  223. struct iwl_bt_coex_reduced_txp_update_cmd cmd = {};
  224. struct iwl_mvm_sta *mvmsta;
  225. u32 value;
  226. int ret;
  227. mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
  228. if (!mvmsta)
  229. return 0;
  230. /* nothing to do */
  231. if (mvmsta->bt_reduced_txpower == enable)
  232. return 0;
  233. value = mvmsta->sta_id;
  234. if (enable)
  235. value |= BT_REDUCED_TX_POWER_BIT;
  236. IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
  237. enable ? "en" : "dis", sta_id);
  238. cmd.reduced_txp = cpu_to_le32(value);
  239. mvmsta->bt_reduced_txpower = enable;
  240. ret = iwl_mvm_send_cmd_pdu(mvm, BT_COEX_UPDATE_REDUCED_TXP, CMD_ASYNC,
  241. sizeof(cmd), &cmd);
  242. return ret;
  243. }
  244. struct iwl_bt_iterator_data {
  245. struct iwl_bt_coex_profile_notif *notif;
  246. struct iwl_mvm *mvm;
  247. struct ieee80211_chanctx_conf *primary;
  248. struct ieee80211_chanctx_conf *secondary;
  249. bool primary_ll;
  250. };
  251. static inline
  252. void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
  253. struct ieee80211_vif *vif,
  254. bool enable, int rssi)
  255. {
  256. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  257. mvmvif->bf_data.last_bt_coex_event = rssi;
  258. mvmvif->bf_data.bt_coex_max_thold =
  259. enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
  260. mvmvif->bf_data.bt_coex_min_thold =
  261. enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
  262. }
  263. /* must be called under rcu_read_lock */
  264. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  265. struct ieee80211_vif *vif)
  266. {
  267. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  268. struct iwl_bt_iterator_data *data = _data;
  269. struct iwl_mvm *mvm = data->mvm;
  270. struct ieee80211_chanctx_conf *chanctx_conf;
  271. /* default smps_mode is AUTOMATIC - only used for client modes */
  272. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
  273. u32 bt_activity_grading;
  274. int ave_rssi;
  275. lockdep_assert_held(&mvm->mutex);
  276. switch (vif->type) {
  277. case NL80211_IFTYPE_STATION:
  278. break;
  279. case NL80211_IFTYPE_AP:
  280. if (!mvmvif->ap_ibss_active)
  281. return;
  282. break;
  283. default:
  284. return;
  285. }
  286. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  287. /* If channel context is invalid or not on 2.4GHz .. */
  288. if ((!chanctx_conf ||
  289. chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
  290. if (vif->type == NL80211_IFTYPE_STATION) {
  291. /* ... relax constraints and disable rssi events */
  292. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  293. smps_mode);
  294. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  295. false);
  296. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  297. }
  298. return;
  299. }
  300. bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
  301. if (bt_activity_grading >= BT_HIGH_TRAFFIC)
  302. smps_mode = IEEE80211_SMPS_STATIC;
  303. else if (bt_activity_grading >= BT_LOW_TRAFFIC)
  304. smps_mode = IEEE80211_SMPS_DYNAMIC;
  305. /* relax SMPS constraints for next association */
  306. if (!vif->bss_conf.assoc)
  307. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  308. if (mvmvif->phy_ctxt &&
  309. (mvm->last_bt_notif.rrc_status & BIT(mvmvif->phy_ctxt->id)))
  310. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  311. IWL_DEBUG_COEX(data->mvm,
  312. "mac %d: bt_activity_grading %d smps_req %d\n",
  313. mvmvif->id, bt_activity_grading, smps_mode);
  314. if (vif->type == NL80211_IFTYPE_STATION)
  315. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  316. smps_mode);
  317. /* low latency is always primary */
  318. if (iwl_mvm_vif_low_latency(mvmvif)) {
  319. data->primary_ll = true;
  320. data->secondary = data->primary;
  321. data->primary = chanctx_conf;
  322. }
  323. if (vif->type == NL80211_IFTYPE_AP) {
  324. if (!mvmvif->ap_ibss_active)
  325. return;
  326. if (chanctx_conf == data->primary)
  327. return;
  328. if (!data->primary_ll) {
  329. /*
  330. * downgrade the current primary no matter what its
  331. * type is.
  332. */
  333. data->secondary = data->primary;
  334. data->primary = chanctx_conf;
  335. } else {
  336. /* there is low latency vif - we will be secondary */
  337. data->secondary = chanctx_conf;
  338. }
  339. return;
  340. }
  341. /*
  342. * STA / P2P Client, try to be primary if first vif. If we are in low
  343. * latency mode, we are already in primary and just don't do much
  344. */
  345. if (!data->primary || data->primary == chanctx_conf)
  346. data->primary = chanctx_conf;
  347. else if (!data->secondary)
  348. /* if secondary is not NULL, it might be a GO */
  349. data->secondary = chanctx_conf;
  350. /*
  351. * don't reduce the Tx power if one of these is true:
  352. * we are in LOOSE
  353. * single share antenna product
  354. * BT is inactive
  355. * we are not associated
  356. */
  357. if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
  358. mvm->cfg->bt_shared_single_ant || !vif->bss_conf.assoc ||
  359. le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF) {
  360. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false);
  361. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  362. return;
  363. }
  364. /* try to get the avg rssi from fw */
  365. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  366. /* if the RSSI isn't valid, fake it is very low */
  367. if (!ave_rssi)
  368. ave_rssi = -100;
  369. if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
  370. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  371. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  372. } else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
  373. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  374. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  375. }
  376. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  377. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
  378. }
  379. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  380. {
  381. struct iwl_bt_iterator_data data = {
  382. .mvm = mvm,
  383. .notif = &mvm->last_bt_notif,
  384. };
  385. struct iwl_bt_coex_ci_cmd cmd = {};
  386. u8 ci_bw_idx;
  387. /* Ignore updates if we are in force mode */
  388. if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
  389. return;
  390. rcu_read_lock();
  391. ieee80211_iterate_active_interfaces_atomic(
  392. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  393. iwl_mvm_bt_notif_iterator, &data);
  394. if (data.primary) {
  395. struct ieee80211_chanctx_conf *chan = data.primary;
  396. if (WARN_ON(!chan->def.chan)) {
  397. rcu_read_unlock();
  398. return;
  399. }
  400. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  401. ci_bw_idx = 0;
  402. } else {
  403. if (chan->def.center_freq1 >
  404. chan->def.chan->center_freq)
  405. ci_bw_idx = 2;
  406. else
  407. ci_bw_idx = 1;
  408. }
  409. cmd.bt_primary_ci =
  410. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  411. cmd.primary_ch_phy_id =
  412. cpu_to_le32(*((u16 *)data.primary->drv_priv));
  413. }
  414. if (data.secondary) {
  415. struct ieee80211_chanctx_conf *chan = data.secondary;
  416. if (WARN_ON(!data.secondary->def.chan)) {
  417. rcu_read_unlock();
  418. return;
  419. }
  420. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  421. ci_bw_idx = 0;
  422. } else {
  423. if (chan->def.center_freq1 >
  424. chan->def.chan->center_freq)
  425. ci_bw_idx = 2;
  426. else
  427. ci_bw_idx = 1;
  428. }
  429. cmd.bt_secondary_ci =
  430. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  431. cmd.secondary_ch_phy_id =
  432. cpu_to_le32(*((u16 *)data.secondary->drv_priv));
  433. }
  434. rcu_read_unlock();
  435. /* Don't spam the fw with the same command over and over */
  436. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  437. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
  438. sizeof(cmd), &cmd))
  439. IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
  440. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  441. }
  442. }
  443. void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  444. struct iwl_rx_cmd_buffer *rxb)
  445. {
  446. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  447. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  448. if (!iwl_mvm_has_new_ats_coex_api(mvm)) {
  449. struct iwl_bt_coex_profile_notif_v4 *v4 = (void *)pkt->data;
  450. mvm->last_bt_notif.mbox_msg[0] = v4->mbox_msg[0];
  451. mvm->last_bt_notif.mbox_msg[1] = v4->mbox_msg[1];
  452. mvm->last_bt_notif.mbox_msg[2] = v4->mbox_msg[2];
  453. mvm->last_bt_notif.mbox_msg[3] = v4->mbox_msg[3];
  454. mvm->last_bt_notif.msg_idx = v4->msg_idx;
  455. mvm->last_bt_notif.bt_ci_compliance = v4->bt_ci_compliance;
  456. mvm->last_bt_notif.primary_ch_lut = v4->primary_ch_lut;
  457. mvm->last_bt_notif.secondary_ch_lut = v4->secondary_ch_lut;
  458. mvm->last_bt_notif.bt_activity_grading =
  459. v4->bt_activity_grading;
  460. mvm->last_bt_notif.ttc_status = v4->ttc_status;
  461. mvm->last_bt_notif.rrc_status = v4->rrc_status;
  462. } else {
  463. /* save this notification for future use: rssi fluctuations */
  464. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  465. }
  466. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  467. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n",
  468. mvm->last_bt_notif.bt_ci_compliance);
  469. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  470. le32_to_cpu(mvm->last_bt_notif.primary_ch_lut));
  471. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  472. le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut));
  473. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  474. le32_to_cpu(mvm->last_bt_notif.bt_activity_grading));
  475. iwl_mvm_bt_coex_notif_handle(mvm);
  476. }
  477. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  478. enum ieee80211_rssi_event_data rssi_event)
  479. {
  480. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  481. int ret;
  482. lockdep_assert_held(&mvm->mutex);
  483. /* Ignore updates if we are in force mode */
  484. if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
  485. return;
  486. /*
  487. * Rssi update while not associated - can happen since the statistics
  488. * are handled asynchronously
  489. */
  490. if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA)
  491. return;
  492. /* No BT - reports should be disabled */
  493. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
  494. return;
  495. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  496. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  497. /*
  498. * Check if rssi is good enough for reduced Tx power, but not in loose
  499. * scheme.
  500. */
  501. if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
  502. iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  503. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  504. false);
  505. else
  506. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  507. if (ret)
  508. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  509. }
  510. #define LINK_QUAL_AGG_TIME_LIMIT_DEF (4000)
  511. #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
  512. u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
  513. struct ieee80211_sta *sta)
  514. {
  515. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  516. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  517. struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
  518. enum iwl_bt_coex_lut_type lut_type;
  519. if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
  520. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  521. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  522. BT_HIGH_TRAFFIC)
  523. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  524. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  525. if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
  526. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  527. /* tight coex, high bt traffic, reduce AGG time limit */
  528. return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
  529. }
  530. bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
  531. struct ieee80211_sta *sta)
  532. {
  533. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  534. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  535. struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
  536. enum iwl_bt_coex_lut_type lut_type;
  537. if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
  538. return true;
  539. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  540. BT_HIGH_TRAFFIC)
  541. return true;
  542. /*
  543. * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
  544. * since BT is already killed.
  545. * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
  546. * we Tx.
  547. * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
  548. */
  549. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  550. return lut_type != BT_COEX_LOOSE_LUT;
  551. }
  552. bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
  553. {
  554. /* there is no other antenna, shared antenna is always available */
  555. if (mvm->cfg->bt_shared_single_ant)
  556. return true;
  557. if (ant & mvm->cfg->non_shared_ant)
  558. return true;
  559. return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  560. BT_HIGH_TRAFFIC;
  561. }
  562. bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
  563. {
  564. /* there is no other antenna, shared antenna is always available */
  565. if (mvm->cfg->bt_shared_single_ant)
  566. return true;
  567. return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
  568. }
  569. bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
  570. enum nl80211_band band)
  571. {
  572. u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
  573. if (band != NL80211_BAND_2GHZ)
  574. return false;
  575. return bt_activity >= BT_LOW_TRAFFIC;
  576. }
  577. u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
  578. struct ieee80211_tx_info *info, u8 ac)
  579. {
  580. __le16 fc = hdr->frame_control;
  581. bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
  582. if (info->band != NL80211_BAND_2GHZ)
  583. return 0;
  584. if (unlikely(mvm->bt_tx_prio))
  585. return mvm->bt_tx_prio - 1;
  586. if (likely(ieee80211_is_data(fc))) {
  587. if (likely(ieee80211_is_data_qos(fc))) {
  588. switch (ac) {
  589. case IEEE80211_AC_BE:
  590. return mplut_enabled ? 1 : 0;
  591. case IEEE80211_AC_VI:
  592. return mplut_enabled ? 2 : 3;
  593. case IEEE80211_AC_VO:
  594. return 3;
  595. default:
  596. return 0;
  597. }
  598. } else if (is_multicast_ether_addr(hdr->addr1)) {
  599. return 3;
  600. } else
  601. return 0;
  602. } else if (ieee80211_is_mgmt(fc)) {
  603. return ieee80211_is_disassoc(fc) ? 0 : 3;
  604. } else if (ieee80211_is_ctl(fc)) {
  605. /* ignore cfend and cfendack frames as we never send those */
  606. return 3;
  607. }
  608. return 0;
  609. }
  610. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
  611. {
  612. iwl_mvm_bt_coex_notif_handle(mvm);
  613. }