coex.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. u8 primary_load;
  249. u8 secondary_load;
  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. #define MVM_COEX_TCM_PERIOD (HZ * 10)
  264. static void iwl_mvm_bt_coex_tcm_based_ci(struct iwl_mvm *mvm,
  265. struct iwl_bt_iterator_data *data)
  266. {
  267. unsigned long now = jiffies;
  268. if (!time_after(now, mvm->bt_coex_last_tcm_ts + MVM_COEX_TCM_PERIOD))
  269. return;
  270. mvm->bt_coex_last_tcm_ts = now;
  271. /* We assume here that we don't have more than 2 vifs on 2.4GHz */
  272. /* if the primary is low latency, it will stay primary */
  273. if (data->primary_ll)
  274. return;
  275. if (data->primary_load >= data->secondary_load)
  276. return;
  277. swap(data->primary, data->secondary);
  278. }
  279. /* must be called under rcu_read_lock */
  280. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  281. struct ieee80211_vif *vif)
  282. {
  283. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  284. struct iwl_bt_iterator_data *data = _data;
  285. struct iwl_mvm *mvm = data->mvm;
  286. struct ieee80211_chanctx_conf *chanctx_conf;
  287. /* default smps_mode is AUTOMATIC - only used for client modes */
  288. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
  289. u32 bt_activity_grading;
  290. int ave_rssi;
  291. lockdep_assert_held(&mvm->mutex);
  292. switch (vif->type) {
  293. case NL80211_IFTYPE_STATION:
  294. break;
  295. case NL80211_IFTYPE_AP:
  296. if (!mvmvif->ap_ibss_active)
  297. return;
  298. break;
  299. default:
  300. return;
  301. }
  302. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  303. /* If channel context is invalid or not on 2.4GHz .. */
  304. if ((!chanctx_conf ||
  305. chanctx_conf->def.chan->band != NL80211_BAND_2GHZ)) {
  306. if (vif->type == NL80211_IFTYPE_STATION) {
  307. /* ... relax constraints and disable rssi events */
  308. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  309. smps_mode);
  310. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  311. false);
  312. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  313. }
  314. return;
  315. }
  316. bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
  317. if (bt_activity_grading >= BT_HIGH_TRAFFIC)
  318. smps_mode = IEEE80211_SMPS_STATIC;
  319. else if (bt_activity_grading >= BT_LOW_TRAFFIC)
  320. smps_mode = IEEE80211_SMPS_DYNAMIC;
  321. /* relax SMPS constraints for next association */
  322. if (!vif->bss_conf.assoc)
  323. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  324. if (mvmvif->phy_ctxt &&
  325. (mvm->last_bt_notif.rrc_status & BIT(mvmvif->phy_ctxt->id)))
  326. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  327. IWL_DEBUG_COEX(data->mvm,
  328. "mac %d: bt_activity_grading %d smps_req %d\n",
  329. mvmvif->id, bt_activity_grading, smps_mode);
  330. if (vif->type == NL80211_IFTYPE_STATION)
  331. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  332. smps_mode);
  333. /* low latency is always primary */
  334. if (iwl_mvm_vif_low_latency(mvmvif)) {
  335. data->primary_ll = true;
  336. data->secondary = data->primary;
  337. data->primary = chanctx_conf;
  338. }
  339. if (vif->type == NL80211_IFTYPE_AP) {
  340. if (!mvmvif->ap_ibss_active)
  341. return;
  342. if (chanctx_conf == data->primary)
  343. return;
  344. if (!data->primary_ll) {
  345. /*
  346. * downgrade the current primary no matter what its
  347. * type is.
  348. */
  349. data->secondary = data->primary;
  350. data->primary = chanctx_conf;
  351. } else {
  352. /* there is low latency vif - we will be secondary */
  353. data->secondary = chanctx_conf;
  354. }
  355. if (data->primary == chanctx_conf)
  356. data->primary_load = mvm->tcm.result.load[mvmvif->id];
  357. else if (data->secondary == chanctx_conf)
  358. data->secondary_load = mvm->tcm.result.load[mvmvif->id];
  359. return;
  360. }
  361. /*
  362. * STA / P2P Client, try to be primary if first vif. If we are in low
  363. * latency mode, we are already in primary and just don't do much
  364. */
  365. if (!data->primary || data->primary == chanctx_conf)
  366. data->primary = chanctx_conf;
  367. else if (!data->secondary)
  368. /* if secondary is not NULL, it might be a GO */
  369. data->secondary = chanctx_conf;
  370. if (data->primary == chanctx_conf)
  371. data->primary_load = mvm->tcm.result.load[mvmvif->id];
  372. else if (data->secondary == chanctx_conf)
  373. data->secondary_load = mvm->tcm.result.load[mvmvif->id];
  374. /*
  375. * don't reduce the Tx power if one of these is true:
  376. * we are in LOOSE
  377. * single share antenna product
  378. * BT is inactive
  379. * we are not associated
  380. */
  381. if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
  382. mvm->cfg->bt_shared_single_ant || !vif->bss_conf.assoc ||
  383. le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF) {
  384. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false);
  385. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  386. return;
  387. }
  388. /* try to get the avg rssi from fw */
  389. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  390. /* if the RSSI isn't valid, fake it is very low */
  391. if (!ave_rssi)
  392. ave_rssi = -100;
  393. if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
  394. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  395. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  396. } else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
  397. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  398. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  399. }
  400. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  401. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
  402. }
  403. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  404. {
  405. struct iwl_bt_iterator_data data = {
  406. .mvm = mvm,
  407. .notif = &mvm->last_bt_notif,
  408. };
  409. struct iwl_bt_coex_ci_cmd cmd = {};
  410. u8 ci_bw_idx;
  411. /* Ignore updates if we are in force mode */
  412. if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
  413. return;
  414. rcu_read_lock();
  415. ieee80211_iterate_active_interfaces_atomic(
  416. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  417. iwl_mvm_bt_notif_iterator, &data);
  418. iwl_mvm_bt_coex_tcm_based_ci(mvm, &data);
  419. if (data.primary) {
  420. struct ieee80211_chanctx_conf *chan = data.primary;
  421. if (WARN_ON(!chan->def.chan)) {
  422. rcu_read_unlock();
  423. return;
  424. }
  425. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  426. ci_bw_idx = 0;
  427. } else {
  428. if (chan->def.center_freq1 >
  429. chan->def.chan->center_freq)
  430. ci_bw_idx = 2;
  431. else
  432. ci_bw_idx = 1;
  433. }
  434. cmd.bt_primary_ci =
  435. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  436. cmd.primary_ch_phy_id =
  437. cpu_to_le32(*((u16 *)data.primary->drv_priv));
  438. }
  439. if (data.secondary) {
  440. struct ieee80211_chanctx_conf *chan = data.secondary;
  441. if (WARN_ON(!data.secondary->def.chan)) {
  442. rcu_read_unlock();
  443. return;
  444. }
  445. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  446. ci_bw_idx = 0;
  447. } else {
  448. if (chan->def.center_freq1 >
  449. chan->def.chan->center_freq)
  450. ci_bw_idx = 2;
  451. else
  452. ci_bw_idx = 1;
  453. }
  454. cmd.bt_secondary_ci =
  455. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  456. cmd.secondary_ch_phy_id =
  457. cpu_to_le32(*((u16 *)data.secondary->drv_priv));
  458. }
  459. rcu_read_unlock();
  460. /* Don't spam the fw with the same command over and over */
  461. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  462. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
  463. sizeof(cmd), &cmd))
  464. IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
  465. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  466. }
  467. }
  468. void iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  469. struct iwl_rx_cmd_buffer *rxb)
  470. {
  471. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  472. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  473. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  474. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  475. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  476. le32_to_cpu(notif->primary_ch_lut));
  477. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  478. le32_to_cpu(notif->secondary_ch_lut));
  479. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  480. le32_to_cpu(notif->bt_activity_grading));
  481. /* remember this notification for future use: rssi fluctuations */
  482. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  483. iwl_mvm_bt_coex_notif_handle(mvm);
  484. }
  485. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  486. enum ieee80211_rssi_event_data rssi_event)
  487. {
  488. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  489. int ret;
  490. lockdep_assert_held(&mvm->mutex);
  491. /* Ignore updates if we are in force mode */
  492. if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
  493. return;
  494. /*
  495. * Rssi update while not associated - can happen since the statistics
  496. * are handled asynchronously
  497. */
  498. if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA)
  499. return;
  500. /* No BT - reports should be disabled */
  501. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
  502. return;
  503. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  504. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  505. /*
  506. * Check if rssi is good enough for reduced Tx power, but not in loose
  507. * scheme.
  508. */
  509. if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
  510. iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  511. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  512. false);
  513. else
  514. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  515. if (ret)
  516. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  517. }
  518. #define LINK_QUAL_AGG_TIME_LIMIT_DEF (4000)
  519. #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
  520. u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
  521. struct ieee80211_sta *sta)
  522. {
  523. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  524. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  525. struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
  526. enum iwl_bt_coex_lut_type lut_type;
  527. if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
  528. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  529. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  530. BT_HIGH_TRAFFIC)
  531. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  532. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  533. if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
  534. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  535. /* tight coex, high bt traffic, reduce AGG time limit */
  536. return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
  537. }
  538. bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
  539. struct ieee80211_sta *sta)
  540. {
  541. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  542. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  543. struct iwl_mvm_phy_ctxt *phy_ctxt = mvmvif->phy_ctxt;
  544. enum iwl_bt_coex_lut_type lut_type;
  545. if (mvm->last_bt_notif.ttc_status & BIT(phy_ctxt->id))
  546. return true;
  547. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  548. BT_HIGH_TRAFFIC)
  549. return true;
  550. /*
  551. * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
  552. * since BT is already killed.
  553. * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
  554. * we Tx.
  555. * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
  556. */
  557. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  558. return lut_type != BT_COEX_LOOSE_LUT;
  559. }
  560. bool iwl_mvm_bt_coex_is_ant_avail(struct iwl_mvm *mvm, u8 ant)
  561. {
  562. /* there is no other antenna, shared antenna is always available */
  563. if (mvm->cfg->bt_shared_single_ant)
  564. return true;
  565. if (ant & mvm->cfg->non_shared_ant)
  566. return true;
  567. return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  568. BT_HIGH_TRAFFIC;
  569. }
  570. bool iwl_mvm_bt_coex_is_shared_ant_avail(struct iwl_mvm *mvm)
  571. {
  572. /* there is no other antenna, shared antenna is always available */
  573. if (mvm->cfg->bt_shared_single_ant)
  574. return true;
  575. return le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) < BT_HIGH_TRAFFIC;
  576. }
  577. bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
  578. enum nl80211_band band)
  579. {
  580. u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
  581. if (band != NL80211_BAND_2GHZ)
  582. return false;
  583. return bt_activity >= BT_LOW_TRAFFIC;
  584. }
  585. u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
  586. struct ieee80211_tx_info *info, u8 ac)
  587. {
  588. __le16 fc = hdr->frame_control;
  589. bool mplut_enabled = iwl_mvm_is_mplut_supported(mvm);
  590. if (info->band != NL80211_BAND_2GHZ)
  591. return 0;
  592. if (unlikely(mvm->bt_tx_prio))
  593. return mvm->bt_tx_prio - 1;
  594. if (likely(ieee80211_is_data(fc))) {
  595. if (likely(ieee80211_is_data_qos(fc))) {
  596. switch (ac) {
  597. case IEEE80211_AC_BE:
  598. return mplut_enabled ? 1 : 0;
  599. case IEEE80211_AC_VI:
  600. return mplut_enabled ? 2 : 3;
  601. case IEEE80211_AC_VO:
  602. return 3;
  603. default:
  604. return 0;
  605. }
  606. } else if (is_multicast_ether_addr(hdr->addr1)) {
  607. return 3;
  608. } else
  609. return 0;
  610. } else if (ieee80211_is_mgmt(fc)) {
  611. return ieee80211_is_disassoc(fc) ? 0 : 3;
  612. } else if (ieee80211_is_ctl(fc)) {
  613. /* ignore cfend and cfendack frames as we never send those */
  614. return 3;
  615. }
  616. return 0;
  617. }
  618. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
  619. {
  620. iwl_mvm_bt_coex_notif_handle(mvm);
  621. }