bt-coex.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_MULTI_PRIO_LUT |
  363. BT_VALID_CORUN_LUT_20 |
  364. BT_VALID_CORUN_LUT_40 |
  365. BT_VALID_ANT_ISOLATION |
  366. BT_VALID_ANT_ISOLATION_THRS |
  367. BT_VALID_TXTX_DELTA_FREQ_THRS |
  368. BT_VALID_TXRX_MAX_FREQ_0);
  369. if (mvm->cfg->bt_shared_single_ant)
  370. memcpy(&bt_cmd->decision_lut, iwl_single_shared_ant,
  371. sizeof(iwl_single_shared_ant));
  372. else
  373. memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
  374. sizeof(iwl_combined_lookup));
  375. memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
  376. sizeof(iwl_bt_prio_boost));
  377. memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
  378. sizeof(iwl_bt_mprio_lut));
  379. bt_cmd->kill_ack_msk =
  380. cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
  381. bt_cmd->kill_cts_msk =
  382. cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
  383. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  384. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  385. ret = iwl_mvm_send_cmd(mvm, &cmd);
  386. kfree(bt_cmd);
  387. return ret;
  388. }
  389. static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm,
  390. bool reduced_tx_power)
  391. {
  392. enum iwl_bt_kill_msk bt_kill_msk;
  393. struct iwl_bt_coex_cmd *bt_cmd;
  394. struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
  395. struct iwl_host_cmd cmd = {
  396. .id = BT_CONFIG,
  397. .data[0] = &bt_cmd,
  398. .len = { sizeof(*bt_cmd), },
  399. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  400. .flags = CMD_SYNC,
  401. };
  402. int ret = 0;
  403. lockdep_assert_held(&mvm->mutex);
  404. if (reduced_tx_power) {
  405. /* Reduced Tx power has precedence on the type of the profile */
  406. bt_kill_msk = BT_KILL_MSK_REDUCED_TXPOW;
  407. } else {
  408. /* Low latency BT profile is active: give higher prio to BT */
  409. if (BT_MBOX_MSG(notif, 3, SCO_STATE) ||
  410. BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
  411. BT_MBOX_MSG(notif, 3, SNIFF_STATE))
  412. bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
  413. else
  414. bt_kill_msk = BT_KILL_MSK_DEFAULT;
  415. }
  416. IWL_DEBUG_COEX(mvm,
  417. "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
  418. bt_kill_msk,
  419. BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
  420. BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
  421. BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
  422. /* Don't send HCMD if there is no update */
  423. if (bt_kill_msk == mvm->bt_kill_msk)
  424. return 0;
  425. mvm->bt_kill_msk = bt_kill_msk;
  426. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  427. if (!bt_cmd)
  428. return -ENOMEM;
  429. cmd.data[0] = bt_cmd;
  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 =
  433. cpu_to_le32(BT_VALID_KILL_ACK | BT_VALID_KILL_CTS);
  434. IWL_DEBUG_COEX(mvm, "ACK Kill msk = 0x%08x, CTS Kill msk = 0x%08x\n",
  435. iwl_bt_ack_kill_msk[bt_kill_msk],
  436. iwl_bt_cts_kill_msk[bt_kill_msk]);
  437. ret = iwl_mvm_send_cmd(mvm, &cmd);
  438. kfree(bt_cmd);
  439. return ret;
  440. }
  441. static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
  442. bool enable)
  443. {
  444. struct iwl_bt_coex_cmd *bt_cmd;
  445. /* Send ASYNC since this can be sent from an atomic context */
  446. struct iwl_host_cmd cmd = {
  447. .id = BT_CONFIG,
  448. .len = { sizeof(*bt_cmd), },
  449. .dataflags = { IWL_HCMD_DFL_DUP, },
  450. .flags = CMD_ASYNC,
  451. };
  452. struct ieee80211_sta *sta;
  453. struct iwl_mvm_sta *mvmsta;
  454. int ret;
  455. /* This can happen if the station has been removed right now */
  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. mvmsta = (void *)sta->drv_priv;
  461. /* nothing to do */
  462. if (mvmsta->bt_reduced_txpower == enable)
  463. return 0;
  464. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
  465. if (!bt_cmd)
  466. return -ENOMEM;
  467. cmd.data[0] = bt_cmd;
  468. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_REDUCED_TX_POWER),
  469. bt_cmd->bt_reduced_tx_power = sta_id;
  470. if (enable)
  471. bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
  472. IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
  473. enable ? "en" : "dis", sta_id);
  474. mvmsta->bt_reduced_txpower = enable;
  475. ret = iwl_mvm_send_cmd(mvm, &cmd);
  476. kfree(bt_cmd);
  477. return ret;
  478. }
  479. struct iwl_bt_iterator_data {
  480. struct iwl_bt_coex_profile_notif *notif;
  481. struct iwl_mvm *mvm;
  482. u32 num_bss_ifaces;
  483. bool reduced_tx_power;
  484. struct ieee80211_chanctx_conf *primary;
  485. struct ieee80211_chanctx_conf *secondary;
  486. };
  487. static inline
  488. void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
  489. struct ieee80211_vif *vif,
  490. bool enable, int rssi)
  491. {
  492. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  493. mvmvif->bf_data.last_bt_coex_event = rssi;
  494. mvmvif->bf_data.bt_coex_max_thold =
  495. enable ? BT_ENABLE_REDUCED_TXPOWER_THRESHOLD : 0;
  496. mvmvif->bf_data.bt_coex_min_thold =
  497. enable ? BT_DISABLE_REDUCED_TXPOWER_THRESHOLD : 0;
  498. }
  499. /* must be called under rcu_read_lock */
  500. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  501. struct ieee80211_vif *vif)
  502. {
  503. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  504. struct iwl_bt_iterator_data *data = _data;
  505. struct iwl_mvm *mvm = data->mvm;
  506. struct ieee80211_chanctx_conf *chanctx_conf;
  507. enum ieee80211_smps_mode smps_mode;
  508. int ave_rssi;
  509. lockdep_assert_held(&mvm->mutex);
  510. if (vif->type != NL80211_IFTYPE_STATION &&
  511. vif->type != NL80211_IFTYPE_AP)
  512. return;
  513. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  514. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  515. /* If channel context is invalid or not on 2.4GHz .. */
  516. if ((!chanctx_conf ||
  517. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
  518. /* ... and it is an associated STATION, relax constraints */
  519. if (vif->type == NL80211_IFTYPE_STATION && vif->bss_conf.assoc)
  520. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  521. smps_mode);
  522. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  523. return;
  524. }
  525. /* SoftAP / GO will always be primary */
  526. if (vif->type == NL80211_IFTYPE_AP) {
  527. if (!mvmvif->ap_active)
  528. return;
  529. /* the Ack / Cts kill mask must be default if AP / GO */
  530. data->reduced_tx_power = false;
  531. if (chanctx_conf == data->primary)
  532. return;
  533. /* downgrade the current primary no matter what its type is */
  534. data->secondary = data->primary;
  535. data->primary = chanctx_conf;
  536. return;
  537. }
  538. data->num_bss_ifaces++;
  539. /* we are now a STA / P2P Client, and take associated ones only */
  540. if (!vif->bss_conf.assoc)
  541. return;
  542. /* STA / P2P Client, try to be primary if first vif */
  543. if (!data->primary || data->primary == chanctx_conf)
  544. data->primary = chanctx_conf;
  545. else if (!data->secondary)
  546. /* if secondary is not NULL, it might be a GO */
  547. data->secondary = chanctx_conf;
  548. if (data->notif->bt_status)
  549. smps_mode = IEEE80211_SMPS_DYNAMIC;
  550. if (le32_to_cpu(data->notif->bt_activity_grading) >= BT_LOW_TRAFFIC)
  551. smps_mode = IEEE80211_SMPS_STATIC;
  552. IWL_DEBUG_COEX(data->mvm,
  553. "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
  554. mvmvif->id, data->notif->bt_status,
  555. data->notif->bt_activity_grading, smps_mode);
  556. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX, smps_mode);
  557. /* don't reduce the Tx power if in loose scheme */
  558. if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
  559. mvm->cfg->bt_shared_single_ant) {
  560. data->reduced_tx_power = false;
  561. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  562. return;
  563. }
  564. /* reduced Txpower only if BT is on, so ...*/
  565. if (le32_to_cpu(data->notif->bt_activity_grading) == BT_OFF) {
  566. /* ... cancel reduced Tx power ... */
  567. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  568. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  569. data->reduced_tx_power = false;
  570. /* ... and there is no need to get reports on RSSI any more. */
  571. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  572. return;
  573. }
  574. /* try to get the avg rssi from fw */
  575. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  576. /* if the RSSI isn't valid, fake it is very low */
  577. if (!ave_rssi)
  578. ave_rssi = -100;
  579. if (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) {
  580. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  581. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  582. /*
  583. * bt_kill_msk can be BT_KILL_MSK_REDUCED_TXPOW only if all the
  584. * BSS / P2P clients have rssi above threshold.
  585. * We set the bt_kill_msk to BT_KILL_MSK_REDUCED_TXPOW before
  586. * the iteration, if one interface's rssi isn't good enough,
  587. * bt_kill_msk will be set to default values.
  588. */
  589. } else if (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) {
  590. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  591. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  592. /*
  593. * One interface hasn't rssi above threshold, bt_kill_msk must
  594. * be set to default values.
  595. */
  596. data->reduced_tx_power = false;
  597. }
  598. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  599. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
  600. }
  601. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  602. {
  603. struct iwl_bt_iterator_data data = {
  604. .mvm = mvm,
  605. .notif = &mvm->last_bt_notif,
  606. .reduced_tx_power = true,
  607. };
  608. struct iwl_bt_coex_ci_cmd cmd = {};
  609. u8 ci_bw_idx;
  610. rcu_read_lock();
  611. ieee80211_iterate_active_interfaces_atomic(
  612. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  613. iwl_mvm_bt_notif_iterator, &data);
  614. if (data.primary) {
  615. struct ieee80211_chanctx_conf *chan = data.primary;
  616. if (WARN_ON(!chan->def.chan)) {
  617. rcu_read_unlock();
  618. return;
  619. }
  620. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  621. ci_bw_idx = 0;
  622. cmd.co_run_bw_primary = 0;
  623. } else {
  624. cmd.co_run_bw_primary = 1;
  625. if (chan->def.center_freq1 >
  626. chan->def.chan->center_freq)
  627. ci_bw_idx = 2;
  628. else
  629. ci_bw_idx = 1;
  630. }
  631. cmd.bt_primary_ci =
  632. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  633. cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  634. }
  635. if (data.secondary) {
  636. struct ieee80211_chanctx_conf *chan = data.secondary;
  637. if (WARN_ON(!data.secondary->def.chan)) {
  638. rcu_read_unlock();
  639. return;
  640. }
  641. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  642. ci_bw_idx = 0;
  643. cmd.co_run_bw_secondary = 0;
  644. } else {
  645. cmd.co_run_bw_secondary = 1;
  646. if (chan->def.center_freq1 >
  647. chan->def.chan->center_freq)
  648. ci_bw_idx = 2;
  649. else
  650. ci_bw_idx = 1;
  651. }
  652. cmd.bt_secondary_ci =
  653. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  654. cmd.secondary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  655. }
  656. rcu_read_unlock();
  657. /* Don't spam the fw with the same command over and over */
  658. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  659. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, CMD_SYNC,
  660. sizeof(cmd), &cmd))
  661. IWL_ERR(mvm, "Failed to send BT_CI cmd");
  662. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  663. }
  664. /*
  665. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  666. * irrelevant since it is based on the RSSI coming from the beacon.
  667. * Use BT_KILL_MSK_DEFAULT in that case.
  668. */
  669. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  670. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  671. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  672. }
  673. /* upon association, the fw will send in BT Coex notification */
  674. int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  675. struct iwl_rx_cmd_buffer *rxb,
  676. struct iwl_device_cmd *dev_cmd)
  677. {
  678. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  679. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  680. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  681. IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
  682. notif->bt_status ? "ON" : "OFF");
  683. IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
  684. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  685. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  686. le32_to_cpu(notif->primary_ch_lut));
  687. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  688. le32_to_cpu(notif->secondary_ch_lut));
  689. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  690. le32_to_cpu(notif->bt_activity_grading));
  691. IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
  692. notif->bt_agg_traffic_load);
  693. /* remember this notification for future use: rssi fluctuations */
  694. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  695. iwl_mvm_bt_coex_notif_handle(mvm);
  696. /*
  697. * This is an async handler for a notification, returning anything other
  698. * than 0 doesn't make sense even if HCMD failed.
  699. */
  700. return 0;
  701. }
  702. static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
  703. struct ieee80211_vif *vif)
  704. {
  705. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  706. struct iwl_bt_iterator_data *data = _data;
  707. struct iwl_mvm *mvm = data->mvm;
  708. struct ieee80211_sta *sta;
  709. struct iwl_mvm_sta *mvmsta;
  710. struct ieee80211_chanctx_conf *chanctx_conf;
  711. rcu_read_lock();
  712. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  713. /* If channel context is invalid or not on 2.4GHz - don't count it */
  714. if (!chanctx_conf ||
  715. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
  716. rcu_read_unlock();
  717. return;
  718. }
  719. rcu_read_unlock();
  720. if (vif->type != NL80211_IFTYPE_STATION ||
  721. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  722. return;
  723. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  724. lockdep_is_held(&mvm->mutex));
  725. mvmsta = (void *)sta->drv_priv;
  726. data->num_bss_ifaces++;
  727. /*
  728. * This interface doesn't support reduced Tx power (because of low
  729. * RSSI probably), then set bt_kill_msk to default values.
  730. */
  731. if (!mvmsta->bt_reduced_txpower)
  732. data->reduced_tx_power = false;
  733. /* else - possibly leave it to BT_KILL_MSK_REDUCED_TXPOW */
  734. }
  735. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  736. enum ieee80211_rssi_event rssi_event)
  737. {
  738. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  739. struct iwl_bt_iterator_data data = {
  740. .mvm = mvm,
  741. .reduced_tx_power = true,
  742. };
  743. int ret;
  744. lockdep_assert_held(&mvm->mutex);
  745. /* Rssi update while not associated ?! */
  746. if (WARN_ON_ONCE(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT))
  747. return;
  748. /* No BT - reports should be disabled */
  749. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) == BT_OFF)
  750. return;
  751. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  752. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  753. /*
  754. * Check if rssi is good enough for reduced Tx power, but not in loose
  755. * scheme.
  756. */
  757. if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
  758. iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  759. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  760. false);
  761. else
  762. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  763. if (ret)
  764. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  765. ieee80211_iterate_active_interfaces_atomic(
  766. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  767. iwl_mvm_bt_rssi_iterator, &data);
  768. /*
  769. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  770. * irrelevant since it is based on the RSSI coming from the beacon.
  771. * Use BT_KILL_MSK_DEFAULT in that case.
  772. */
  773. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  774. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  775. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  776. }
  777. #define LINK_QUAL_AGG_TIME_LIMIT_DEF (4000)
  778. #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
  779. u16 iwl_mvm_bt_coex_agg_time_limit(struct iwl_mvm *mvm,
  780. struct ieee80211_sta *sta)
  781. {
  782. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  783. enum iwl_bt_coex_lut_type lut_type;
  784. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  785. BT_LOW_TRAFFIC)
  786. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  787. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  788. if (lut_type == BT_COEX_LOOSE_LUT)
  789. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  790. /* tight coex, high bt traffic, reduce AGG time limit */
  791. return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
  792. }
  793. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  794. {
  795. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWBT_COEX))
  796. return;
  797. iwl_mvm_bt_coex_notif_handle(mvm);
  798. }