bt-coex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "fw-api-bt-coex.h"
  64. #include "iwl-modparams.h"
  65. #include "mvm.h"
  66. #include "iwl-debug.h"
  67. #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant) \
  68. [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) | \
  69. ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
  70. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  71. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
  72. BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
  73. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
  74. BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
  75. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
  76. BT_COEX_PRIO_TBL_PRIO_LOW, 0),
  77. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
  78. BT_COEX_PRIO_TBL_PRIO_LOW, 1),
  79. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
  80. BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
  81. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
  82. BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
  83. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
  84. BT_COEX_PRIO_TBL_DISABLED, 0),
  85. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
  86. BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
  87. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
  88. BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
  89. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
  90. BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
  91. 0, 0, 0, 0, 0, 0,
  92. };
  93. #undef EVENT_PRIO_ANT
  94. int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
  95. {
  96. return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, CMD_SYNC,
  97. sizeof(struct iwl_bt_coex_prio_tbl_cmd),
  98. &iwl_bt_prio_tbl);
  99. }
  100. static int iwl_send_bt_env(struct iwl_mvm *mvm, u8 action, u8 type)
  101. {
  102. struct iwl_bt_coex_prot_env_cmd env_cmd;
  103. int ret;
  104. env_cmd.action = action;
  105. env_cmd.type = type;
  106. ret = iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PROT_ENV, CMD_SYNC,
  107. sizeof(env_cmd), &env_cmd);
  108. if (ret)
  109. IWL_ERR(mvm, "failed to send BT env command\n");
  110. return ret;
  111. }
  112. enum iwl_bt_kill_msk {
  113. BT_KILL_MSK_DEFAULT,
  114. BT_KILL_MSK_SCO_HID_A2DP,
  115. BT_KILL_MSK_REDUCED_TXPOW,
  116. BT_KILL_MSK_MAX,
  117. };
  118. static const u32 iwl_bt_ack_kill_msk[BT_KILL_MSK_MAX] = {
  119. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  120. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  121. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  122. };
  123. static const u32 iwl_bt_cts_kill_msk[BT_KILL_MSK_MAX] = {
  124. [BT_KILL_MSK_DEFAULT] = 0xffff0000,
  125. [BT_KILL_MSK_SCO_HID_A2DP] = 0xffffffff,
  126. [BT_KILL_MSK_REDUCED_TXPOW] = 0,
  127. };
  128. #define IWL_BT_DEFAULT_BOOST (0xf0f0f0f0)
  129. /* Tight Coex */
  130. static const __le32 iwl_tight_lookup[BT_COEX_LUT_SIZE] = {
  131. cpu_to_le32(0xaaaaaaaa),
  132. cpu_to_le32(0xaaaaaaaa),
  133. cpu_to_le32(0xaeaaaaaa),
  134. cpu_to_le32(0xaaaaaaaa),
  135. cpu_to_le32(0xcc00ff28),
  136. cpu_to_le32(0x0000aaaa),
  137. cpu_to_le32(0xcc00aaaa),
  138. cpu_to_le32(0x0000aaaa),
  139. cpu_to_le32(0xc0004000),
  140. cpu_to_le32(0x00000000),
  141. cpu_to_le32(0xf0005000),
  142. cpu_to_le32(0xf0005000),
  143. };
  144. /* Loose Coex */
  145. static const __le32 iwl_loose_lookup[BT_COEX_LUT_SIZE] = {
  146. cpu_to_le32(0xaaaaaaaa),
  147. cpu_to_le32(0xaaaaaaaa),
  148. cpu_to_le32(0xaeaaaaaa),
  149. cpu_to_le32(0xaaaaaaaa),
  150. cpu_to_le32(0xcc00ff28),
  151. cpu_to_le32(0x0000aaaa),
  152. cpu_to_le32(0xcc00aaaa),
  153. cpu_to_le32(0x0000aaaa),
  154. cpu_to_le32(0x00000000),
  155. cpu_to_le32(0x00000000),
  156. cpu_to_le32(0xf0005000),
  157. cpu_to_le32(0xf0005000),
  158. };
  159. /* Full concurrency */
  160. static const __le32 iwl_concurrent_lookup[BT_COEX_LUT_SIZE] = {
  161. cpu_to_le32(0xaaaaaaaa),
  162. cpu_to_le32(0xaaaaaaaa),
  163. cpu_to_le32(0xaaaaaaaa),
  164. cpu_to_le32(0xaaaaaaaa),
  165. cpu_to_le32(0xaaaaaaaa),
  166. cpu_to_le32(0xaaaaaaaa),
  167. cpu_to_le32(0xaaaaaaaa),
  168. cpu_to_le32(0xaaaaaaaa),
  169. cpu_to_le32(0x00000000),
  170. cpu_to_le32(0x00000000),
  171. cpu_to_le32(0x00000000),
  172. cpu_to_le32(0x00000000),
  173. };
  174. /* BT Antenna Coupling Threshold (dB) */
  175. #define IWL_BT_ANTENNA_COUPLING_THRESHOLD (35)
  176. #define IWL_BT_LOAD_FORCE_SISO_THRESHOLD (3)
  177. int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
  178. {
  179. struct iwl_bt_coex_cmd cmd = {
  180. .max_kill = 5,
  181. .bt3_time_t7_value = 1,
  182. .bt3_prio_sample_time = 2,
  183. .bt3_timer_t2_value = 0xc,
  184. };
  185. int ret;
  186. cmd.flags = iwlwifi_mod_params.bt_coex_active ?
  187. BT_COEX_NW : BT_COEX_DISABLE;
  188. cmd.flags |= iwlwifi_mod_params.bt_ch_announce ? BT_CH_PRIMARY_EN : 0;
  189. cmd.flags |= BT_SYNC_2_BT_DISABLE;
  190. cmd.valid_bit_msk = cpu_to_le16(BT_VALID_ENABLE |
  191. BT_VALID_BT_PRIO_BOOST |
  192. BT_VALID_MAX_KILL |
  193. BT_VALID_3W_TMRS |
  194. BT_VALID_KILL_ACK |
  195. BT_VALID_KILL_CTS |
  196. BT_VALID_REDUCED_TX_POWER |
  197. BT_VALID_LUT);
  198. if (iwlwifi_mod_params.ant_coupling > IWL_BT_ANTENNA_COUPLING_THRESHOLD)
  199. memcpy(&cmd.decision_lut, iwl_loose_lookup,
  200. sizeof(iwl_tight_lookup));
  201. else
  202. memcpy(&cmd.decision_lut, iwl_tight_lookup,
  203. sizeof(iwl_tight_lookup));
  204. cmd.bt_prio_boost = cpu_to_le32(IWL_BT_DEFAULT_BOOST);
  205. cmd.kill_ack_msk =
  206. cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
  207. cmd.kill_cts_msk =
  208. cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
  209. /* go to CALIB state in internal BT-Coex state machine */
  210. ret = iwl_send_bt_env(mvm, BT_COEX_ENV_OPEN,
  211. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  212. if (ret)
  213. return ret;
  214. ret = iwl_send_bt_env(mvm, BT_COEX_ENV_CLOSE,
  215. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  216. if (ret)
  217. return ret;
  218. return iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, CMD_SYNC,
  219. sizeof(cmd), &cmd);
  220. }
  221. struct iwl_bt_notif_iterator_data {
  222. struct iwl_mvm *mvm;
  223. struct iwl_bt_coex_profile_notif *notif;
  224. };
  225. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  226. struct ieee80211_vif *vif)
  227. {
  228. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  229. struct iwl_bt_notif_iterator_data *data = _data;
  230. struct ieee80211_chanctx_conf *chanctx_conf;
  231. enum ieee80211_smps_mode smps_mode;
  232. enum ieee80211_band band;
  233. if (vif->type != NL80211_IFTYPE_STATION)
  234. return;
  235. rcu_read_lock();
  236. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  237. if (chanctx_conf && chanctx_conf->def.chan)
  238. band = chanctx_conf->def.chan->band;
  239. else
  240. band = -1;
  241. rcu_read_unlock();
  242. if (band != IEEE80211_BAND_2GHZ)
  243. return;
  244. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  245. if (data->notif->bt_status)
  246. smps_mode = IEEE80211_SMPS_DYNAMIC;
  247. if (data->notif->bt_traffic_load >= IWL_BT_LOAD_FORCE_SISO_THRESHOLD)
  248. smps_mode = IEEE80211_SMPS_STATIC;
  249. IWL_DEBUG_COEX(data->mvm,
  250. "mac %d: bt_status %d traffic_load %d smps_req %d\n",
  251. mvmvif->id, data->notif->bt_status,
  252. data->notif->bt_traffic_load, smps_mode);
  253. ieee80211_request_smps(vif, smps_mode);
  254. }
  255. int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  256. struct iwl_rx_cmd_buffer *rxb,
  257. struct iwl_device_cmd *dev_cmd)
  258. {
  259. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  260. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  261. struct iwl_bt_notif_iterator_data data = {
  262. .mvm = mvm,
  263. .notif = notif,
  264. };
  265. struct iwl_bt_coex_cmd cmd = {};
  266. enum iwl_bt_kill_msk bt_kill_msk;
  267. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  268. IWL_DEBUG_COEX(mvm, "\tBT %salive\n", notif->bt_status ? "" : "not ");
  269. IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
  270. IWL_DEBUG_COEX(mvm, "\tBT traffic load %d\n", notif->bt_traffic_load);
  271. IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
  272. notif->bt_agg_traffic_load);
  273. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  274. /* remember this notification for future use: rssi fluctuations */
  275. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  276. ieee80211_iterate_active_interfaces_atomic(
  277. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  278. iwl_mvm_bt_notif_iterator, &data);
  279. /* Low latency BT profile is active: give higher prio to BT */
  280. if (BT_MBOX_MSG(notif, 3, SCO_STATE) ||
  281. BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
  282. BT_MBOX_MSG(notif, 3, SNIFF_STATE))
  283. bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
  284. else
  285. bt_kill_msk = BT_KILL_MSK_DEFAULT;
  286. /* Don't send HCMD if there is no update */
  287. if (bt_kill_msk == mvm->bt_kill_msk)
  288. return 0;
  289. IWL_DEBUG_COEX(mvm,
  290. "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
  291. bt_kill_msk,
  292. BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
  293. BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
  294. BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
  295. mvm->bt_kill_msk = bt_kill_msk;
  296. cmd.kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
  297. cmd.kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
  298. cmd.valid_bit_msk = cpu_to_le16(BT_VALID_KILL_ACK | BT_VALID_KILL_CTS);
  299. if (iwl_mvm_send_cmd_pdu(mvm, BT_CONFIG, CMD_SYNC, sizeof(cmd), &cmd))
  300. IWL_ERR(mvm, "Failed to sent BT Coex CMD\n");
  301. /* This handler is ASYNC */
  302. return 0;
  303. }