bt-coex.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <net/mac80211.h>
  64. #include "fw-api-bt-coex.h"
  65. #include "iwl-modparams.h"
  66. #include "mvm.h"
  67. #include "iwl-debug.h"
  68. #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant) \
  69. [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) | \
  70. ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
  71. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  72. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
  73. BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
  74. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
  75. BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
  76. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
  77. BT_COEX_PRIO_TBL_PRIO_LOW, 0),
  78. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
  79. BT_COEX_PRIO_TBL_PRIO_LOW, 1),
  80. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
  81. BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
  82. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
  83. BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
  84. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
  85. BT_COEX_PRIO_TBL_DISABLED, 0),
  86. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
  87. BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
  88. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
  89. BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
  90. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
  91. BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
  92. 0, 0, 0, 0, 0, 0,
  93. };
  94. #undef EVENT_PRIO_ANT
  95. #define BT_ENABLE_REDUCED_TXPOWER_THRESHOLD (-62)
  96. #define BT_DISABLE_REDUCED_TXPOWER_THRESHOLD (-65)
  97. #define BT_ANTENNA_COUPLING_THRESHOLD (30)
  98. int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
  99. {
  100. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  101. return 0;
  102. return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, CMD_SYNC,
  103. sizeof(struct iwl_bt_coex_prio_tbl_cmd),
  104. &iwl_bt_prio_tbl);
  105. }
  106. enum iwl_bt_kill_msk {
  107. BT_KILL_MSK_DEFAULT,
  108. BT_KILL_MSK_SCO_HID_A2DP,
  109. BT_KILL_MSK_REDUCED_TXPOW,
  110. BT_KILL_MSK_MAX,
  111. };
  112. static const u32 iwl_bt_ack_kill_msk[BT_KILL_MSK_MAX] = {
  113. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  114. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  115. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  116. };
  117. static const u32 iwl_bt_cts_kill_msk[BT_KILL_MSK_MAX] = {
  118. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  119. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  120. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  121. };
  122. static const __le32 iwl_bt_prio_boost[BT_COEX_BOOST_SIZE] = {
  123. cpu_to_le32(0xf0f0f0f0),
  124. cpu_to_le32(0xc0c0c0c0),
  125. cpu_to_le32(0xfcfcfcfc),
  126. cpu_to_le32(0xff00ff00),
  127. };
  128. static const __le32 iwl_combined_lookup[BT_COEX_MAX_LUT][BT_COEX_LUT_SIZE] = {
  129. {
  130. /* Tight */
  131. cpu_to_le32(0xaaaaaaaa),
  132. cpu_to_le32(0xaaaaaaaa),
  133. cpu_to_le32(0xaeaaaaaa),
  134. cpu_to_le32(0xaaaaaaaa),
  135. cpu_to_le32(0xcc00ff28),
  136. cpu_to_le32(0x0000aaaa),
  137. cpu_to_le32(0xcc00aaaa),
  138. cpu_to_le32(0x0000aaaa),
  139. cpu_to_le32(0xc0004000),
  140. cpu_to_le32(0x00000000),
  141. cpu_to_le32(0xf0005000),
  142. cpu_to_le32(0xf0005000),
  143. },
  144. {
  145. /* Loose */
  146. cpu_to_le32(0xaaaaaaaa),
  147. cpu_to_le32(0xaaaaaaaa),
  148. cpu_to_le32(0xaaaaaaaa),
  149. cpu_to_le32(0xaaaaaaaa),
  150. cpu_to_le32(0xcc00ff28),
  151. cpu_to_le32(0x0000aaaa),
  152. cpu_to_le32(0xcc00aaaa),
  153. cpu_to_le32(0x0000aaaa),
  154. cpu_to_le32(0x00000000),
  155. cpu_to_le32(0x00000000),
  156. cpu_to_le32(0xf0005000),
  157. cpu_to_le32(0xf0005000),
  158. },
  159. {
  160. /* Tx Tx disabled */
  161. cpu_to_le32(0xaaaaaaaa),
  162. cpu_to_le32(0xaaaaaaaa),
  163. cpu_to_le32(0xaaaaaaaa),
  164. cpu_to_le32(0xaaaaaaaa),
  165. cpu_to_le32(0xcc00ff28),
  166. cpu_to_le32(0x0000aaaa),
  167. cpu_to_le32(0xcc00aaaa),
  168. cpu_to_le32(0x0000aaaa),
  169. cpu_to_le32(0xC0004000),
  170. cpu_to_le32(0xC0004000),
  171. cpu_to_le32(0xF0005000),
  172. cpu_to_le32(0xF0005000),
  173. },
  174. };
  175. /* 20MHz / 40MHz below / 40Mhz above*/
  176. static const __le64 iwl_ci_mask[][3] = {
  177. /* dummy entry for channel 0 */
  178. {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
  179. {
  180. cpu_to_le64(0x0000001FFFULL),
  181. cpu_to_le64(0x0ULL),
  182. cpu_to_le64(0x00007FFFFFULL),
  183. },
  184. {
  185. cpu_to_le64(0x000000FFFFULL),
  186. cpu_to_le64(0x0ULL),
  187. cpu_to_le64(0x0003FFFFFFULL),
  188. },
  189. {
  190. cpu_to_le64(0x000003FFFCULL),
  191. cpu_to_le64(0x0ULL),
  192. cpu_to_le64(0x000FFFFFFCULL),
  193. },
  194. {
  195. cpu_to_le64(0x00001FFFE0ULL),
  196. cpu_to_le64(0x0ULL),
  197. cpu_to_le64(0x007FFFFFE0ULL),
  198. },
  199. {
  200. cpu_to_le64(0x00007FFF80ULL),
  201. cpu_to_le64(0x00007FFFFFULL),
  202. cpu_to_le64(0x01FFFFFF80ULL),
  203. },
  204. {
  205. cpu_to_le64(0x0003FFFC00ULL),
  206. cpu_to_le64(0x0003FFFFFFULL),
  207. cpu_to_le64(0x0FFFFFFC00ULL),
  208. },
  209. {
  210. cpu_to_le64(0x000FFFF000ULL),
  211. cpu_to_le64(0x000FFFFFFCULL),
  212. cpu_to_le64(0x3FFFFFF000ULL),
  213. },
  214. {
  215. cpu_to_le64(0x007FFF8000ULL),
  216. cpu_to_le64(0x007FFFFFE0ULL),
  217. cpu_to_le64(0xFFFFFF8000ULL),
  218. },
  219. {
  220. cpu_to_le64(0x01FFFE0000ULL),
  221. cpu_to_le64(0x01FFFFFF80ULL),
  222. cpu_to_le64(0xFFFFFE0000ULL),
  223. },
  224. {
  225. cpu_to_le64(0x0FFFF00000ULL),
  226. cpu_to_le64(0x0FFFFFFC00ULL),
  227. cpu_to_le64(0x0ULL),
  228. },
  229. {
  230. cpu_to_le64(0x3FFFC00000ULL),
  231. cpu_to_le64(0x3FFFFFF000ULL),
  232. cpu_to_le64(0x0)
  233. },
  234. {
  235. cpu_to_le64(0xFFFE000000ULL),
  236. cpu_to_le64(0xFFFFFF8000ULL),
  237. cpu_to_le64(0x0)
  238. },
  239. {
  240. cpu_to_le64(0xFFF8000000ULL),
  241. cpu_to_le64(0xFFFFFE0000ULL),
  242. cpu_to_le64(0x0)
  243. },
  244. {
  245. cpu_to_le64(0xFE00000000ULL),
  246. cpu_to_le64(0x0ULL),
  247. cpu_to_le64(0x0)
  248. },
  249. };
  250. static const __le32 iwl_bt_mprio_lut[BT_COEX_MULTI_PRIO_LUT_SIZE] = {
  251. cpu_to_le32(0x22002200),
  252. cpu_to_le32(0x33113311),
  253. };
  254. static enum iwl_bt_coex_lut_type
  255. iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
  256. {
  257. struct ieee80211_chanctx_conf *chanctx_conf;
  258. enum iwl_bt_coex_lut_type ret;
  259. u16 phy_ctx_id;
  260. lockdep_assert_held(&mvm->mutex);
  261. rcu_read_lock();
  262. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  263. if (!chanctx_conf ||
  264. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
  265. rcu_read_unlock();
  266. return BT_COEX_LOOSE_LUT;
  267. }
  268. ret = BT_COEX_TX_DIS_LUT;
  269. phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
  270. if (mvm->last_bt_ci_cmd.primary_ch_phy_id == phy_ctx_id)
  271. ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
  272. else if (mvm->last_bt_ci_cmd.secondary_ch_phy_id == phy_ctx_id)
  273. ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
  274. /* else - default = TX TX disallowed */
  275. rcu_read_unlock();
  276. return ret;
  277. }
  278. int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
  279. {
  280. struct iwl_bt_coex_cmd *bt_cmd;
  281. struct iwl_host_cmd cmd = {
  282. .id = BT_CONFIG,
  283. .len = { sizeof(*bt_cmd), },
  284. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  285. .flags = CMD_SYNC,
  286. };
  287. int ret;
  288. u32 flags;
  289. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  290. return 0;
  291. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  292. if (!bt_cmd)
  293. return -ENOMEM;
  294. cmd.data[0] = bt_cmd;
  295. bt_cmd->max_kill = 5;
  296. bt_cmd->bt4_antenna_isolation_thr = BT_ANTENNA_COUPLING_THRESHOLD,
  297. bt_cmd->bt4_antenna_isolation = iwlwifi_mod_params.ant_coupling,
  298. bt_cmd->bt4_tx_tx_delta_freq_thr = 15,
  299. bt_cmd->bt4_tx_rx_max_freq0 = 15,
  300. flags = iwlwifi_mod_params.bt_coex_active ?
  301. BT_COEX_NW : BT_COEX_DISABLE;
  302. flags |= BT_CH_PRIMARY_EN | BT_CH_SECONDARY_EN | BT_SYNC_2_BT_DISABLE;
  303. bt_cmd->flags = cpu_to_le32(flags);
  304. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE |
  305. BT_VALID_BT_PRIO_BOOST |
  306. BT_VALID_MAX_KILL |
  307. BT_VALID_3W_TMRS |
  308. BT_VALID_KILL_ACK |
  309. BT_VALID_KILL_CTS |
  310. BT_VALID_REDUCED_TX_POWER |
  311. BT_VALID_LUT |
  312. BT_VALID_WIFI_RX_SW_PRIO_BOOST |
  313. BT_VALID_WIFI_TX_SW_PRIO_BOOST |
  314. BT_VALID_MULTI_PRIO_LUT |
  315. BT_VALID_CORUN_LUT_20 |
  316. BT_VALID_CORUN_LUT_40 |
  317. BT_VALID_ANT_ISOLATION |
  318. BT_VALID_ANT_ISOLATION_THRS |
  319. BT_VALID_TXTX_DELTA_FREQ_THRS |
  320. BT_VALID_TXRX_MAX_FREQ_0);
  321. memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
  322. sizeof(iwl_combined_lookup));
  323. memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
  324. sizeof(iwl_bt_prio_boost));
  325. memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
  326. sizeof(iwl_bt_mprio_lut));
  327. bt_cmd->kill_ack_msk =
  328. cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
  329. bt_cmd->kill_cts_msk =
  330. cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
  331. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  332. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  333. ret = iwl_mvm_send_cmd(mvm, &cmd);
  334. kfree(bt_cmd);
  335. return ret;
  336. }
  337. static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm,
  338. bool reduced_tx_power)
  339. {
  340. enum iwl_bt_kill_msk bt_kill_msk;
  341. struct iwl_bt_coex_cmd *bt_cmd;
  342. struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
  343. struct iwl_host_cmd cmd = {
  344. .id = BT_CONFIG,
  345. .data[0] = &bt_cmd,
  346. .len = { sizeof(*bt_cmd), },
  347. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  348. .flags = CMD_SYNC,
  349. };
  350. int ret = 0;
  351. lockdep_assert_held(&mvm->mutex);
  352. if (reduced_tx_power) {
  353. /* Reduced Tx power has precedence on the type of the profile */
  354. bt_kill_msk = BT_KILL_MSK_REDUCED_TXPOW;
  355. } else {
  356. /* Low latency BT profile is active: give higher prio to BT */
  357. if (BT_MBOX_MSG(notif, 3, SCO_STATE) ||
  358. BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
  359. BT_MBOX_MSG(notif, 3, SNIFF_STATE))
  360. bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
  361. else
  362. bt_kill_msk = BT_KILL_MSK_DEFAULT;
  363. }
  364. IWL_DEBUG_COEX(mvm,
  365. "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
  366. bt_kill_msk,
  367. BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
  368. BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
  369. BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
  370. /* Don't send HCMD if there is no update */
  371. if (bt_kill_msk == mvm->bt_kill_msk)
  372. return 0;
  373. mvm->bt_kill_msk = bt_kill_msk;
  374. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  375. if (!bt_cmd)
  376. return -ENOMEM;
  377. cmd.data[0] = bt_cmd;
  378. bt_cmd->kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
  379. bt_cmd->kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
  380. bt_cmd->valid_bit_msk =
  381. cpu_to_le32(BT_VALID_KILL_ACK | BT_VALID_KILL_CTS);
  382. IWL_DEBUG_COEX(mvm, "ACK Kill msk = 0x%08x, CTS Kill msk = 0x%08x\n",
  383. iwl_bt_ack_kill_msk[bt_kill_msk],
  384. iwl_bt_cts_kill_msk[bt_kill_msk]);
  385. ret = iwl_mvm_send_cmd(mvm, &cmd);
  386. kfree(bt_cmd);
  387. return ret;
  388. }
  389. static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
  390. bool enable)
  391. {
  392. struct iwl_bt_coex_cmd *bt_cmd;
  393. /* Send ASYNC since this can be sent from an atomic context */
  394. struct iwl_host_cmd cmd = {
  395. .id = BT_CONFIG,
  396. .len = { sizeof(*bt_cmd), },
  397. .dataflags = { IWL_HCMD_DFL_DUP, },
  398. .flags = CMD_ASYNC,
  399. };
  400. struct ieee80211_sta *sta;
  401. struct iwl_mvm_sta *mvmsta;
  402. int ret;
  403. /* This can happen if the station has been removed right now */
  404. if (sta_id == IWL_MVM_STATION_COUNT)
  405. return 0;
  406. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  407. lockdep_is_held(&mvm->mutex));
  408. mvmsta = (void *)sta->drv_priv;
  409. /* nothing to do */
  410. if (mvmsta->bt_reduced_txpower == enable)
  411. return 0;
  412. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
  413. if (!bt_cmd)
  414. return -ENOMEM;
  415. cmd.data[0] = bt_cmd;
  416. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_REDUCED_TX_POWER),
  417. bt_cmd->bt_reduced_tx_power = sta_id;
  418. if (enable)
  419. bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
  420. IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
  421. enable ? "en" : "dis", sta_id);
  422. mvmsta->bt_reduced_txpower = enable;
  423. ret = iwl_mvm_send_cmd(mvm, &cmd);
  424. kfree(bt_cmd);
  425. return ret;
  426. }
  427. struct iwl_bt_iterator_data {
  428. struct iwl_bt_coex_profile_notif *notif;
  429. struct iwl_mvm *mvm;
  430. u32 num_bss_ifaces;
  431. bool reduced_tx_power;
  432. struct ieee80211_chanctx_conf *primary;
  433. struct ieee80211_chanctx_conf *secondary;
  434. };
  435. /* must be called under rcu_read_lock */
  436. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  437. struct ieee80211_vif *vif)
  438. {
  439. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  440. struct iwl_bt_iterator_data *data = _data;
  441. struct iwl_mvm *mvm = data->mvm;
  442. struct ieee80211_chanctx_conf *chanctx_conf;
  443. enum ieee80211_smps_mode smps_mode;
  444. int ave_rssi;
  445. lockdep_assert_held(&mvm->mutex);
  446. if (vif->type != NL80211_IFTYPE_STATION &&
  447. vif->type != NL80211_IFTYPE_AP)
  448. return;
  449. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  450. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  451. /* If channel context is invalid or not on 2.4GHz .. */
  452. if ((!chanctx_conf ||
  453. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
  454. /* ... and it is an associated STATION, relax constraints */
  455. if (vif->type == NL80211_IFTYPE_STATION && vif->bss_conf.assoc)
  456. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  457. smps_mode);
  458. return;
  459. }
  460. /* SoftAP / GO will always be primary */
  461. if (vif->type == NL80211_IFTYPE_AP) {
  462. if (!mvmvif->ap_active)
  463. return;
  464. /* the Ack / Cts kill mask must be default if AP / GO */
  465. data->reduced_tx_power = false;
  466. if (chanctx_conf == data->primary)
  467. return;
  468. /* downgrade the current primary no matter what its type is */
  469. data->secondary = data->primary;
  470. data->primary = chanctx_conf;
  471. return;
  472. }
  473. /* we are now a STA / P2P Client, and take associated ones only */
  474. if (!vif->bss_conf.assoc)
  475. return;
  476. /* STA / P2P Client, try to be primary if first vif */
  477. if (!data->primary || data->primary == chanctx_conf)
  478. data->primary = chanctx_conf;
  479. else if (!data->secondary)
  480. /* if secondary is not NULL, it might be a GO */
  481. data->secondary = chanctx_conf;
  482. if (data->notif->bt_status)
  483. smps_mode = IEEE80211_SMPS_DYNAMIC;
  484. if (le32_to_cpu(data->notif->bt_activity_grading) >= BT_LOW_TRAFFIC)
  485. smps_mode = IEEE80211_SMPS_STATIC;
  486. IWL_DEBUG_COEX(data->mvm,
  487. "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
  488. mvmvif->id, data->notif->bt_status,
  489. data->notif->bt_activity_grading, smps_mode);
  490. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX, smps_mode);
  491. /* don't reduce the Tx power if in loose scheme */
  492. if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  493. return;
  494. data->num_bss_ifaces++;
  495. /* reduced Txpower only if BT is on, so ...*/
  496. if (le32_to_cpu(data->notif->bt_activity_grading) == BT_OFF) {
  497. /* ... cancel reduced Tx power ... */
  498. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  499. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  500. data->reduced_tx_power = false;
  501. /* ... and there is no need to get reports on RSSI any more. */
  502. mvmvif->bf_data.last_bt_coex_event = 0;
  503. mvmvif->bf_data.bt_coex_max_thold = 0;
  504. mvmvif->bf_data.bt_coex_min_thold = 0;
  505. return;
  506. }
  507. /* try to get the avg rssi from fw */
  508. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  509. /* if the RSSI isn't valid, fake it is very low */
  510. if (!ave_rssi)
  511. ave_rssi = -100;
  512. if (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) {
  513. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  514. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  515. /*
  516. * bt_kill_msk can be BT_KILL_MSK_REDUCED_TXPOW only if all the
  517. * BSS / P2P clients have rssi above threshold.
  518. * We set the bt_kill_msk to BT_KILL_MSK_REDUCED_TXPOW before
  519. * the iteration, if one interface's rssi isn't good enough,
  520. * bt_kill_msk will be set to default values.
  521. */
  522. } else if (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) {
  523. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  524. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  525. /*
  526. * One interface hasn't rssi above threshold, bt_kill_msk must
  527. * be set to default values.
  528. */
  529. data->reduced_tx_power = false;
  530. }
  531. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  532. /* reset previous bt coex event tracking */
  533. mvmvif->bf_data.last_bt_coex_event = 0;
  534. mvmvif->bf_data.bt_coex_max_thold =
  535. BT_ENABLE_REDUCED_TXPOWER_THRESHOLD;
  536. mvmvif->bf_data.bt_coex_min_thold =
  537. BT_DISABLE_REDUCED_TXPOWER_THRESHOLD;
  538. }
  539. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  540. {
  541. struct iwl_bt_iterator_data data = {
  542. .mvm = mvm,
  543. .notif = &mvm->last_bt_notif,
  544. .reduced_tx_power = true,
  545. };
  546. struct iwl_bt_coex_ci_cmd cmd = {};
  547. u8 ci_bw_idx;
  548. rcu_read_lock();
  549. ieee80211_iterate_active_interfaces_atomic(
  550. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  551. iwl_mvm_bt_notif_iterator, &data);
  552. if (data.primary) {
  553. struct ieee80211_chanctx_conf *chan = data.primary;
  554. if (WARN_ON(!chan->def.chan)) {
  555. rcu_read_unlock();
  556. return;
  557. }
  558. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  559. ci_bw_idx = 0;
  560. cmd.co_run_bw_primary = 0;
  561. } else {
  562. cmd.co_run_bw_primary = 1;
  563. if (chan->def.center_freq1 >
  564. chan->def.chan->center_freq)
  565. ci_bw_idx = 2;
  566. else
  567. ci_bw_idx = 1;
  568. }
  569. cmd.bt_primary_ci =
  570. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  571. cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  572. }
  573. if (data.secondary) {
  574. struct ieee80211_chanctx_conf *chan = data.secondary;
  575. if (WARN_ON(!data.secondary->def.chan)) {
  576. rcu_read_unlock();
  577. return;
  578. }
  579. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  580. ci_bw_idx = 0;
  581. cmd.co_run_bw_secondary = 0;
  582. } else {
  583. cmd.co_run_bw_secondary = 1;
  584. if (chan->def.center_freq1 >
  585. chan->def.chan->center_freq)
  586. ci_bw_idx = 2;
  587. else
  588. ci_bw_idx = 1;
  589. }
  590. cmd.bt_secondary_ci =
  591. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  592. cmd.secondary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  593. }
  594. rcu_read_unlock();
  595. /* Don't spam the fw with the same command over and over */
  596. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  597. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, CMD_SYNC,
  598. sizeof(cmd), &cmd))
  599. IWL_ERR(mvm, "Failed to send BT_CI cmd");
  600. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  601. }
  602. /*
  603. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  604. * irrelevant since it is based on the RSSI coming from the beacon.
  605. * Use BT_KILL_MSK_DEFAULT in that case.
  606. */
  607. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  608. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  609. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  610. }
  611. /* upon association, the fw will send in BT Coex notification */
  612. int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  613. struct iwl_rx_cmd_buffer *rxb,
  614. struct iwl_device_cmd *dev_cmd)
  615. {
  616. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  617. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  618. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  619. IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
  620. notif->bt_status ? "ON" : "OFF");
  621. IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
  622. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  623. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  624. le32_to_cpu(notif->primary_ch_lut));
  625. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  626. le32_to_cpu(notif->secondary_ch_lut));
  627. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  628. le32_to_cpu(notif->bt_activity_grading));
  629. IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
  630. notif->bt_agg_traffic_load);
  631. /* remember this notification for future use: rssi fluctuations */
  632. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  633. iwl_mvm_bt_coex_notif_handle(mvm);
  634. /*
  635. * This is an async handler for a notification, returning anything other
  636. * than 0 doesn't make sense even if HCMD failed.
  637. */
  638. return 0;
  639. }
  640. static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
  641. struct ieee80211_vif *vif)
  642. {
  643. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  644. struct iwl_bt_iterator_data *data = _data;
  645. struct iwl_mvm *mvm = data->mvm;
  646. struct ieee80211_sta *sta;
  647. struct iwl_mvm_sta *mvmsta;
  648. if (vif->type != NL80211_IFTYPE_STATION ||
  649. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  650. return;
  651. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  652. lockdep_is_held(&mvm->mutex));
  653. mvmsta = (void *)sta->drv_priv;
  654. data->num_bss_ifaces++;
  655. /*
  656. * This interface doesn't support reduced Tx power (because of low
  657. * RSSI probably), then set bt_kill_msk to default values.
  658. */
  659. if (!mvmsta->bt_reduced_txpower)
  660. data->reduced_tx_power = false;
  661. /* else - possibly leave it to BT_KILL_MSK_REDUCED_TXPOW */
  662. }
  663. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  664. enum ieee80211_rssi_event rssi_event)
  665. {
  666. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  667. struct iwl_bt_iterator_data data = {
  668. .mvm = mvm,
  669. .reduced_tx_power = true,
  670. };
  671. int ret;
  672. lockdep_assert_held(&mvm->mutex);
  673. /* Rssi update while not associated ?! */
  674. if (WARN_ON_ONCE(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT))
  675. return;
  676. /* No BT - reports should be disabled */
  677. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
  678. return;
  679. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  680. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  681. /*
  682. * Check if rssi is good enough for reduced Tx power, but not in loose
  683. * scheme.
  684. */
  685. if (rssi_event == RSSI_EVENT_LOW ||
  686. iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  687. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  688. false);
  689. else
  690. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  691. if (ret)
  692. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  693. ieee80211_iterate_active_interfaces_atomic(
  694. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  695. iwl_mvm_bt_rssi_iterator, &data);
  696. /*
  697. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  698. * irrelevant since it is based on the RSSI coming from the beacon.
  699. * Use BT_KILL_MSK_DEFAULT in that case.
  700. */
  701. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  702. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  703. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  704. }
  705. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  706. {
  707. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  708. return;
  709. iwl_mvm_bt_coex_notif_handle(mvm);
  710. }