coex.c 19 KB

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