bt-coex.c 24 KB

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