bt-coex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 IWL_BT_LOAD_FORCE_SISO_THRESHOLD (3)
  96. #define BT_ENABLE_REDUCED_TXPOWER_THRESHOLD (-62)
  97. #define BT_DISABLE_REDUCED_TXPOWER_THRESHOLD (-65)
  98. #define BT_ANTENNA_COUPLING_THRESHOLD (30)
  99. static inline bool is_loose_coex(void)
  100. {
  101. return iwlwifi_mod_params.ant_coupling >
  102. BT_ANTENNA_COUPLING_THRESHOLD;
  103. }
  104. int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
  105. {
  106. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  107. return 0;
  108. return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, CMD_SYNC,
  109. sizeof(struct iwl_bt_coex_prio_tbl_cmd),
  110. &iwl_bt_prio_tbl);
  111. }
  112. enum iwl_bt_kill_msk {
  113. BT_KILL_MSK_DEFAULT,
  114. BT_KILL_MSK_SCO_HID_A2DP,
  115. BT_KILL_MSK_REDUCED_TXPOW,
  116. BT_KILL_MSK_MAX,
  117. };
  118. static const u32 iwl_bt_ack_kill_msk[BT_KILL_MSK_MAX] = {
  119. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  120. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  121. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  122. };
  123. static const u32 iwl_bt_cts_kill_msk[BT_KILL_MSK_MAX] = {
  124. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  125. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  126. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  127. };
  128. static const __le32 iwl_bt_prio_boost[BT_COEX_BOOST_SIZE] = {
  129. cpu_to_le32(0xf0f0f0f0),
  130. cpu_to_le32(0xc0c0c0c0),
  131. cpu_to_le32(0xfcfcfcfc),
  132. cpu_to_le32(0xff00ff00),
  133. };
  134. static const __le32 iwl_combined_lookup[BT_COEX_MAX_LUT][BT_COEX_LUT_SIZE] = {
  135. {
  136. /* Tight */
  137. cpu_to_le32(0xaaaaaaaa),
  138. cpu_to_le32(0xaaaaaaaa),
  139. cpu_to_le32(0xaeaaaaaa),
  140. cpu_to_le32(0xaaaaaaaa),
  141. cpu_to_le32(0xcc00ff28),
  142. cpu_to_le32(0x0000aaaa),
  143. cpu_to_le32(0xcc00aaaa),
  144. cpu_to_le32(0x0000aaaa),
  145. cpu_to_le32(0xc0004000),
  146. cpu_to_le32(0x00000000),
  147. cpu_to_le32(0xf0005000),
  148. cpu_to_le32(0xf0005000),
  149. },
  150. {
  151. /* Loose */
  152. cpu_to_le32(0xaaaaaaaa),
  153. cpu_to_le32(0xaaaaaaaa),
  154. cpu_to_le32(0xaaaaaaaa),
  155. cpu_to_le32(0xaaaaaaaa),
  156. cpu_to_le32(0xcc00ff28),
  157. cpu_to_le32(0x0000aaaa),
  158. cpu_to_le32(0xcc00aaaa),
  159. cpu_to_le32(0x0000aaaa),
  160. cpu_to_le32(0x00000000),
  161. cpu_to_le32(0x00000000),
  162. cpu_to_le32(0xf0005000),
  163. cpu_to_le32(0xf0005000),
  164. },
  165. {
  166. /* Tx Tx disabled */
  167. cpu_to_le32(0xaaaaaaaa),
  168. cpu_to_le32(0xaaaaaaaa),
  169. cpu_to_le32(0xaaaaaaaa),
  170. cpu_to_le32(0xaaaaaaaa),
  171. cpu_to_le32(0xcc00ff28),
  172. cpu_to_le32(0x0000aaaa),
  173. cpu_to_le32(0xcc00aaaa),
  174. cpu_to_le32(0x0000aaaa),
  175. cpu_to_le32(0xC0004000),
  176. cpu_to_le32(0xC0004000),
  177. cpu_to_le32(0xF0005000),
  178. cpu_to_le32(0xF0005000),
  179. },
  180. };
  181. /* 20MHz / 40MHz below / 40Mhz above*/
  182. static const __le64 iwl_ci_mask[][3] = {
  183. /* dummy entry for channel 0 */
  184. {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
  185. {
  186. cpu_to_le64(0x0000001FFFULL),
  187. cpu_to_le64(0x0ULL),
  188. cpu_to_le64(0x00007FFFFFULL),
  189. },
  190. {
  191. cpu_to_le64(0x000000FFFFULL),
  192. cpu_to_le64(0x0ULL),
  193. cpu_to_le64(0x0003FFFFFFULL),
  194. },
  195. {
  196. cpu_to_le64(0x000003FFFCULL),
  197. cpu_to_le64(0x0ULL),
  198. cpu_to_le64(0x000FFFFFFCULL),
  199. },
  200. {
  201. cpu_to_le64(0x00001FFFE0ULL),
  202. cpu_to_le64(0x0ULL),
  203. cpu_to_le64(0x007FFFFFE0ULL),
  204. },
  205. {
  206. cpu_to_le64(0x00007FFF80ULL),
  207. cpu_to_le64(0x00007FFFFFULL),
  208. cpu_to_le64(0x01FFFFFF80ULL),
  209. },
  210. {
  211. cpu_to_le64(0x0003FFFC00ULL),
  212. cpu_to_le64(0x0003FFFFFFULL),
  213. cpu_to_le64(0x0FFFFFFC00ULL),
  214. },
  215. {
  216. cpu_to_le64(0x000FFFF000ULL),
  217. cpu_to_le64(0x000FFFFFFCULL),
  218. cpu_to_le64(0x3FFFFFF000ULL),
  219. },
  220. {
  221. cpu_to_le64(0x007FFF8000ULL),
  222. cpu_to_le64(0x007FFFFFE0ULL),
  223. cpu_to_le64(0xFFFFFF8000ULL),
  224. },
  225. {
  226. cpu_to_le64(0x01FFFE0000ULL),
  227. cpu_to_le64(0x01FFFFFF80ULL),
  228. cpu_to_le64(0xFFFFFE0000ULL),
  229. },
  230. {
  231. cpu_to_le64(0x0FFFF00000ULL),
  232. cpu_to_le64(0x0FFFFFFC00ULL),
  233. cpu_to_le64(0x0ULL),
  234. },
  235. {
  236. cpu_to_le64(0x3FFFC00000ULL),
  237. cpu_to_le64(0x3FFFFFF000ULL),
  238. cpu_to_le64(0x0)
  239. },
  240. {
  241. cpu_to_le64(0xFFFE000000ULL),
  242. cpu_to_le64(0xFFFFFF8000ULL),
  243. cpu_to_le64(0x0)
  244. },
  245. {
  246. cpu_to_le64(0xFFF8000000ULL),
  247. cpu_to_le64(0xFFFFFE0000ULL),
  248. cpu_to_le64(0x0)
  249. },
  250. {
  251. cpu_to_le64(0xFE00000000ULL),
  252. cpu_to_le64(0x0ULL),
  253. cpu_to_le64(0x0)
  254. },
  255. };
  256. static const __le32 iwl_bt_mprio_lut[BT_COEX_MULTI_PRIO_LUT_SIZE] = {
  257. cpu_to_le32(0x22002200),
  258. cpu_to_le32(0x33113311),
  259. };
  260. int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
  261. {
  262. struct iwl_bt_coex_cmd *bt_cmd;
  263. struct iwl_host_cmd cmd = {
  264. .id = BT_CONFIG,
  265. .len = { sizeof(*bt_cmd), },
  266. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  267. .flags = CMD_SYNC,
  268. };
  269. int ret;
  270. u32 flags;
  271. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  272. return 0;
  273. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  274. if (!bt_cmd)
  275. return -ENOMEM;
  276. cmd.data[0] = bt_cmd;
  277. bt_cmd->max_kill = 5;
  278. bt_cmd->bt4_antenna_isolation_thr = BT_ANTENNA_COUPLING_THRESHOLD,
  279. bt_cmd->bt4_antenna_isolation = iwlwifi_mod_params.ant_coupling,
  280. bt_cmd->bt4_tx_tx_delta_freq_thr = 15,
  281. bt_cmd->bt4_tx_rx_max_freq0 = 15,
  282. flags = iwlwifi_mod_params.bt_coex_active ?
  283. BT_COEX_NW : BT_COEX_DISABLE;
  284. flags |= BT_CH_PRIMARY_EN | BT_CH_SECONDARY_EN | BT_SYNC_2_BT_DISABLE;
  285. bt_cmd->flags = cpu_to_le32(flags);
  286. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE |
  287. BT_VALID_BT_PRIO_BOOST |
  288. BT_VALID_MAX_KILL |
  289. BT_VALID_3W_TMRS |
  290. BT_VALID_KILL_ACK |
  291. BT_VALID_KILL_CTS |
  292. BT_VALID_REDUCED_TX_POWER |
  293. BT_VALID_LUT |
  294. BT_VALID_WIFI_RX_SW_PRIO_BOOST |
  295. BT_VALID_WIFI_TX_SW_PRIO_BOOST |
  296. BT_VALID_MULTI_PRIO_LUT |
  297. BT_VALID_CORUN_LUT_20 |
  298. BT_VALID_CORUN_LUT_40 |
  299. BT_VALID_ANT_ISOLATION |
  300. BT_VALID_ANT_ISOLATION_THRS |
  301. BT_VALID_TXTX_DELTA_FREQ_THRS |
  302. BT_VALID_TXRX_MAX_FREQ_0);
  303. memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
  304. sizeof(iwl_combined_lookup));
  305. memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
  306. sizeof(iwl_bt_prio_boost));
  307. memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
  308. sizeof(iwl_bt_mprio_lut));
  309. bt_cmd->kill_ack_msk =
  310. cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
  311. bt_cmd->kill_cts_msk =
  312. cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
  313. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  314. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  315. ret = iwl_mvm_send_cmd(mvm, &cmd);
  316. kfree(bt_cmd);
  317. return ret;
  318. }
  319. static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm,
  320. bool reduced_tx_power)
  321. {
  322. enum iwl_bt_kill_msk bt_kill_msk;
  323. struct iwl_bt_coex_cmd *bt_cmd;
  324. struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
  325. struct iwl_host_cmd cmd = {
  326. .id = BT_CONFIG,
  327. .data[0] = &bt_cmd,
  328. .len = { sizeof(*bt_cmd), },
  329. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  330. .flags = CMD_SYNC,
  331. };
  332. int ret = 0;
  333. lockdep_assert_held(&mvm->mutex);
  334. if (reduced_tx_power) {
  335. /* Reduced Tx power has precedence on the type of the profile */
  336. bt_kill_msk = BT_KILL_MSK_REDUCED_TXPOW;
  337. } else {
  338. /* Low latency BT profile is active: give higher prio to BT */
  339. if (BT_MBOX_MSG(notif, 3, SCO_STATE) ||
  340. BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
  341. BT_MBOX_MSG(notif, 3, SNIFF_STATE))
  342. bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
  343. else
  344. bt_kill_msk = BT_KILL_MSK_DEFAULT;
  345. }
  346. IWL_DEBUG_COEX(mvm,
  347. "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
  348. bt_kill_msk,
  349. BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
  350. BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
  351. BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
  352. /* Don't send HCMD if there is no update */
  353. if (bt_kill_msk == mvm->bt_kill_msk)
  354. return 0;
  355. mvm->bt_kill_msk = bt_kill_msk;
  356. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  357. if (!bt_cmd)
  358. return -ENOMEM;
  359. cmd.data[0] = bt_cmd;
  360. bt_cmd->kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
  361. bt_cmd->kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
  362. bt_cmd->valid_bit_msk =
  363. cpu_to_le32(BT_VALID_KILL_ACK | BT_VALID_KILL_CTS);
  364. IWL_DEBUG_COEX(mvm, "ACK Kill msk = 0x%08x, CTS Kill msk = 0x%08x\n",
  365. iwl_bt_ack_kill_msk[bt_kill_msk],
  366. iwl_bt_cts_kill_msk[bt_kill_msk]);
  367. ret = iwl_mvm_send_cmd(mvm, &cmd);
  368. kfree(bt_cmd);
  369. return ret;
  370. }
  371. static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
  372. bool enable)
  373. {
  374. struct iwl_bt_coex_cmd *bt_cmd;
  375. /* Send ASYNC since this can be sent from an atomic context */
  376. struct iwl_host_cmd cmd = {
  377. .id = BT_CONFIG,
  378. .len = { sizeof(*bt_cmd), },
  379. .dataflags = { IWL_HCMD_DFL_DUP, },
  380. .flags = CMD_ASYNC,
  381. };
  382. struct ieee80211_sta *sta;
  383. struct iwl_mvm_sta *mvmsta;
  384. int ret;
  385. /* This can happen if the station has been removed right now */
  386. if (sta_id == IWL_MVM_STATION_COUNT)
  387. return 0;
  388. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  389. lockdep_is_held(&mvm->mutex));
  390. mvmsta = (void *)sta->drv_priv;
  391. /* nothing to do */
  392. if (mvmsta->bt_reduced_txpower == enable)
  393. return 0;
  394. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
  395. if (!bt_cmd)
  396. return -ENOMEM;
  397. cmd.data[0] = bt_cmd;
  398. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_REDUCED_TX_POWER),
  399. bt_cmd->bt_reduced_tx_power = sta_id;
  400. if (enable)
  401. bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
  402. IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
  403. enable ? "en" : "dis", sta_id);
  404. mvmsta->bt_reduced_txpower = enable;
  405. ret = iwl_mvm_send_cmd(mvm, &cmd);
  406. kfree(bt_cmd);
  407. return ret;
  408. }
  409. struct iwl_bt_iterator_data {
  410. struct iwl_bt_coex_profile_notif *notif;
  411. struct iwl_mvm *mvm;
  412. u32 num_bss_ifaces;
  413. bool reduced_tx_power;
  414. struct ieee80211_chanctx_conf *primary;
  415. struct ieee80211_chanctx_conf *secondary;
  416. };
  417. /* must be called under rcu_read_lock */
  418. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  419. struct ieee80211_vif *vif)
  420. {
  421. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  422. struct iwl_bt_iterator_data *data = _data;
  423. struct iwl_mvm *mvm = data->mvm;
  424. struct ieee80211_chanctx_conf *chanctx_conf;
  425. enum ieee80211_smps_mode smps_mode;
  426. int ave_rssi;
  427. lockdep_assert_held(&mvm->mutex);
  428. if (vif->type != NL80211_IFTYPE_STATION &&
  429. vif->type != NL80211_IFTYPE_AP)
  430. return;
  431. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  432. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  433. /* If channel context is invalid or not on 2.4GHz .. */
  434. if ((!chanctx_conf ||
  435. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
  436. /* ... and it is an associated STATION, relax constraints */
  437. if (vif->type == NL80211_IFTYPE_STATION && vif->bss_conf.assoc)
  438. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  439. smps_mode);
  440. return;
  441. }
  442. /* SoftAP / GO will always be primary */
  443. if (vif->type == NL80211_IFTYPE_AP) {
  444. if (!mvmvif->ap_active)
  445. return;
  446. /* the Ack / Cts kill mask must be default if AP / GO */
  447. data->reduced_tx_power = false;
  448. if (chanctx_conf == data->primary)
  449. return;
  450. /* downgrade the current primary no matter what its type is */
  451. data->secondary = data->primary;
  452. data->primary = chanctx_conf;
  453. return;
  454. }
  455. /* we are now a STA / P2P Client, and take associated ones only */
  456. if (!vif->bss_conf.assoc)
  457. return;
  458. /* STA / P2P Client, try to be primary if first vif */
  459. if (!data->primary || data->primary == chanctx_conf)
  460. data->primary = chanctx_conf;
  461. else if (!data->secondary)
  462. /* if secondary is not NULL, it might be a GO */
  463. data->secondary = chanctx_conf;
  464. if (data->notif->bt_status)
  465. smps_mode = IEEE80211_SMPS_DYNAMIC;
  466. if (le32_to_cpu(data->notif->bt_activity_grading) >=
  467. IWL_BT_LOAD_FORCE_SISO_THRESHOLD)
  468. smps_mode = IEEE80211_SMPS_STATIC;
  469. IWL_DEBUG_COEX(data->mvm,
  470. "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
  471. mvmvif->id, data->notif->bt_status,
  472. data->notif->bt_activity_grading, smps_mode);
  473. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX, smps_mode);
  474. /* don't reduce the Tx power if in loose scheme */
  475. if (is_loose_coex())
  476. return;
  477. data->num_bss_ifaces++;
  478. /* reduced Txpower only if there are open BT connections, so ...*/
  479. if (!BT_MBOX_MSG(data->notif, 3, OPEN_CON_2)) {
  480. /* ... cancel reduced Tx power ... */
  481. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  482. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  483. data->reduced_tx_power = false;
  484. /* ... and there is no need to get reports on RSSI any more. */
  485. mvmvif->bf_data.last_bt_coex_event = 0;
  486. mvmvif->bf_data.bt_coex_max_thold = 0;
  487. mvmvif->bf_data.bt_coex_min_thold = 0;
  488. return;
  489. }
  490. /* try to get the avg rssi from fw */
  491. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  492. /* if the RSSI isn't valid, fake it is very low */
  493. if (!ave_rssi)
  494. ave_rssi = -100;
  495. if (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) {
  496. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  497. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  498. /*
  499. * bt_kill_msk can be BT_KILL_MSK_REDUCED_TXPOW only if all the
  500. * BSS / P2P clients have rssi above threshold.
  501. * We set the bt_kill_msk to BT_KILL_MSK_REDUCED_TXPOW before
  502. * the iteration, if one interface's rssi isn't good enough,
  503. * bt_kill_msk will be set to default values.
  504. */
  505. } else if (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) {
  506. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  507. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  508. /*
  509. * One interface hasn't rssi above threshold, bt_kill_msk must
  510. * be set to default values.
  511. */
  512. data->reduced_tx_power = false;
  513. }
  514. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  515. /* reset previous bt coex event tracking */
  516. mvmvif->bf_data.last_bt_coex_event = 0;
  517. mvmvif->bf_data.bt_coex_max_thold =
  518. BT_ENABLE_REDUCED_TXPOWER_THRESHOLD;
  519. mvmvif->bf_data.bt_coex_min_thold =
  520. BT_DISABLE_REDUCED_TXPOWER_THRESHOLD;
  521. }
  522. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  523. {
  524. struct iwl_bt_iterator_data data = {
  525. .mvm = mvm,
  526. .notif = &mvm->last_bt_notif,
  527. .reduced_tx_power = true,
  528. };
  529. struct iwl_bt_coex_ci_cmd cmd = {};
  530. u8 ci_bw_idx;
  531. rcu_read_lock();
  532. ieee80211_iterate_active_interfaces_atomic(
  533. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  534. iwl_mvm_bt_notif_iterator, &data);
  535. if (data.primary) {
  536. struct ieee80211_chanctx_conf *chan = data.primary;
  537. if (WARN_ON(!chan->def.chan)) {
  538. rcu_read_unlock();
  539. return;
  540. }
  541. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  542. ci_bw_idx = 0;
  543. cmd.co_run_bw_primary = 0;
  544. } else {
  545. cmd.co_run_bw_primary = 1;
  546. if (chan->def.center_freq1 >
  547. chan->def.chan->center_freq)
  548. ci_bw_idx = 2;
  549. else
  550. ci_bw_idx = 1;
  551. }
  552. cmd.bt_primary_ci =
  553. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  554. cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  555. }
  556. if (data.secondary) {
  557. struct ieee80211_chanctx_conf *chan = data.secondary;
  558. if (WARN_ON(!data.secondary->def.chan)) {
  559. rcu_read_unlock();
  560. return;
  561. }
  562. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  563. ci_bw_idx = 0;
  564. cmd.co_run_bw_secondary = 0;
  565. } else {
  566. cmd.co_run_bw_secondary = 1;
  567. if (chan->def.center_freq1 >
  568. chan->def.chan->center_freq)
  569. ci_bw_idx = 2;
  570. else
  571. ci_bw_idx = 1;
  572. }
  573. cmd.bt_secondary_ci =
  574. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  575. cmd.secondary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  576. }
  577. rcu_read_unlock();
  578. /* Don't spam the fw with the same command over and over */
  579. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  580. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, CMD_SYNC,
  581. sizeof(cmd), &cmd))
  582. IWL_ERR(mvm, "Failed to send BT_CI cmd");
  583. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  584. }
  585. /*
  586. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  587. * irrelevant since it is based on the RSSI coming from the beacon.
  588. * Use BT_KILL_MSK_DEFAULT in that case.
  589. */
  590. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  591. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  592. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  593. }
  594. /* upon association, the fw will send in BT Coex notification */
  595. int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  596. struct iwl_rx_cmd_buffer *rxb,
  597. struct iwl_device_cmd *dev_cmd)
  598. {
  599. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  600. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  601. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  602. IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
  603. notif->bt_status ? "ON" : "OFF");
  604. IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
  605. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  606. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  607. le32_to_cpu(notif->primary_ch_lut));
  608. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  609. le32_to_cpu(notif->secondary_ch_lut));
  610. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  611. le32_to_cpu(notif->bt_activity_grading));
  612. IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
  613. notif->bt_agg_traffic_load);
  614. /* remember this notification for future use: rssi fluctuations */
  615. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  616. iwl_mvm_bt_coex_notif_handle(mvm);
  617. /*
  618. * This is an async handler for a notification, returning anything other
  619. * than 0 doesn't make sense even if HCMD failed.
  620. */
  621. return 0;
  622. }
  623. static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
  624. struct ieee80211_vif *vif)
  625. {
  626. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  627. struct iwl_bt_iterator_data *data = _data;
  628. struct iwl_mvm *mvm = data->mvm;
  629. struct ieee80211_sta *sta;
  630. struct iwl_mvm_sta *mvmsta;
  631. if (vif->type != NL80211_IFTYPE_STATION ||
  632. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  633. return;
  634. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  635. lockdep_is_held(&mvm->mutex));
  636. mvmsta = (void *)sta->drv_priv;
  637. data->num_bss_ifaces++;
  638. /*
  639. * This interface doesn't support reduced Tx power (because of low
  640. * RSSI probably), then set bt_kill_msk to default values.
  641. */
  642. if (!mvmsta->bt_reduced_txpower)
  643. data->reduced_tx_power = false;
  644. /* else - possibly leave it to BT_KILL_MSK_REDUCED_TXPOW */
  645. }
  646. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  647. enum ieee80211_rssi_event rssi_event)
  648. {
  649. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  650. struct iwl_bt_iterator_data data = {
  651. .mvm = mvm,
  652. .reduced_tx_power = true,
  653. };
  654. int ret;
  655. lockdep_assert_held(&mvm->mutex);
  656. /* Rssi update while not associated ?! */
  657. if (WARN_ON_ONCE(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT))
  658. return;
  659. /* No open connection - reports should be disabled */
  660. if (!BT_MBOX_MSG(&mvm->last_bt_notif, 3, OPEN_CON_2))
  661. return;
  662. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  663. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  664. /*
  665. * Check if rssi is good enough for reduced Tx power, but not in loose
  666. * scheme.
  667. */
  668. if (rssi_event == RSSI_EVENT_LOW || is_loose_coex())
  669. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  670. false);
  671. else
  672. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  673. if (ret)
  674. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  675. ieee80211_iterate_active_interfaces_atomic(
  676. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  677. iwl_mvm_bt_rssi_iterator, &data);
  678. /*
  679. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  680. * irrelevant since it is based on the RSSI coming from the beacon.
  681. * Use BT_KILL_MSK_DEFAULT in that case.
  682. */
  683. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  684. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  685. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  686. }
  687. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  688. {
  689. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  690. return;
  691. iwl_mvm_bt_coex_notif_handle(mvm);
  692. }