bt-coex.c 27 KB

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