coex.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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 - 2014 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 - 2014 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 <linux/ieee80211.h>
  64. #include <linux/etherdevice.h>
  65. #include <net/mac80211.h>
  66. #include "fw-api-coex.h"
  67. #include "iwl-modparams.h"
  68. #include "mvm.h"
  69. #include "iwl-debug.h"
  70. #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant) \
  71. [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) | \
  72. ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
  73. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  74. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
  75. BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
  76. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
  77. BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
  78. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
  79. BT_COEX_PRIO_TBL_PRIO_LOW, 0),
  80. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
  81. BT_COEX_PRIO_TBL_PRIO_LOW, 1),
  82. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
  83. BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
  84. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
  85. BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
  86. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
  87. BT_COEX_PRIO_TBL_DISABLED, 0),
  88. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
  89. BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
  90. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
  91. BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
  92. EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
  93. BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
  94. 0, 0, 0, 0, 0, 0,
  95. };
  96. #undef EVENT_PRIO_ANT
  97. #define BT_ENABLE_REDUCED_TXPOWER_THRESHOLD (-62)
  98. #define BT_DISABLE_REDUCED_TXPOWER_THRESHOLD (-65)
  99. #define BT_ANTENNA_COUPLING_THRESHOLD (30)
  100. static int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
  101. {
  102. return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, 0,
  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), /* 50% */
  118. cpu_to_le32(0xc0c0c0c0), /* 25% */
  119. cpu_to_le32(0xfcfcfcfc), /* 75% */
  120. cpu_to_le32(0xfefefefe), /* 87.5% */
  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(0x00004000),
  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(0xeeaaaaaa),
  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(0xFFC0000000ULL),
  284. cpu_to_le64(0x0ULL),
  285. cpu_to_le64(0x0ULL)
  286. },
  287. };
  288. static const __le32 iwl_bt_mprio_lut[BT_COEX_MULTI_PRIO_LUT_SIZE] = {
  289. cpu_to_le32(0x28412201),
  290. cpu_to_le32(0x11118451),
  291. };
  292. struct corunning_block_luts {
  293. u8 range;
  294. __le32 lut20[BT_COEX_CORUN_LUT_SIZE];
  295. };
  296. /*
  297. * Ranges for the antenna coupling calibration / co-running block LUT:
  298. * LUT0: [ 0, 12[
  299. * LUT1: [12, 20[
  300. * LUT2: [20, 21[
  301. * LUT3: [21, 23[
  302. * LUT4: [23, 27[
  303. * LUT5: [27, 30[
  304. * LUT6: [30, 32[
  305. * LUT7: [32, 33[
  306. * LUT8: [33, - [
  307. */
  308. static const struct corunning_block_luts antenna_coupling_ranges[] = {
  309. {
  310. .range = 0,
  311. .lut20 = {
  312. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  313. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  314. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  315. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  316. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  317. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  318. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  319. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  320. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  321. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  322. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  323. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  324. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  325. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  326. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  327. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  328. },
  329. },
  330. {
  331. .range = 12,
  332. .lut20 = {
  333. cpu_to_le32(0x00000001), cpu_to_le32(0x00000000),
  334. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  335. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  336. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  337. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  338. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  339. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  340. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  341. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  342. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  343. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  344. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  345. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  346. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  347. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  348. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  349. },
  350. },
  351. {
  352. .range = 20,
  353. .lut20 = {
  354. cpu_to_le32(0x00000002), cpu_to_le32(0x00000000),
  355. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  356. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  357. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  358. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  359. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  360. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  361. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  362. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  363. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  364. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  365. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  366. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  367. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  368. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  369. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  370. },
  371. },
  372. {
  373. .range = 21,
  374. .lut20 = {
  375. cpu_to_le32(0x00000003), cpu_to_le32(0x00000000),
  376. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  377. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  378. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  379. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  380. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  381. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  382. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  383. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  384. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  385. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  386. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  387. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  388. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  389. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  390. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  391. },
  392. },
  393. {
  394. .range = 23,
  395. .lut20 = {
  396. cpu_to_le32(0x00000004), cpu_to_le32(0x00000000),
  397. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  398. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  399. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  400. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  401. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  402. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  403. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  404. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  405. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  406. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  407. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  408. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  409. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  410. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  411. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  412. },
  413. },
  414. {
  415. .range = 27,
  416. .lut20 = {
  417. cpu_to_le32(0x00000005), cpu_to_le32(0x00000000),
  418. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  419. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  420. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  421. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  422. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  423. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  424. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  425. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  426. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  427. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  428. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  429. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  430. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  431. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  432. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  433. },
  434. },
  435. {
  436. .range = 30,
  437. .lut20 = {
  438. cpu_to_le32(0x00000006), cpu_to_le32(0x00000000),
  439. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  440. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  441. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  442. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  443. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  444. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  445. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  446. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  447. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  448. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  449. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  450. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  451. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  452. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  453. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  454. },
  455. },
  456. {
  457. .range = 32,
  458. .lut20 = {
  459. cpu_to_le32(0x00000007), cpu_to_le32(0x00000000),
  460. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  461. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  462. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  463. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  464. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  465. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  466. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  467. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  468. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  469. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  470. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  471. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  472. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  473. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  474. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  475. },
  476. },
  477. {
  478. .range = 33,
  479. .lut20 = {
  480. cpu_to_le32(0x00000008), cpu_to_le32(0x00000000),
  481. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  482. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  483. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  484. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  485. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  486. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  487. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  488. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  489. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  490. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  491. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  492. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  493. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  494. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  495. cpu_to_le32(0x00000000), cpu_to_le32(0x00000000),
  496. },
  497. },
  498. };
  499. static enum iwl_bt_coex_lut_type
  500. iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
  501. {
  502. struct ieee80211_chanctx_conf *chanctx_conf;
  503. enum iwl_bt_coex_lut_type ret;
  504. u16 phy_ctx_id;
  505. /*
  506. * Checking that we hold mvm->mutex is a good idea, but the rate
  507. * control can't acquire the mutex since it runs in Tx path.
  508. * So this is racy in that case, but in the worst case, the AMPDU
  509. * size limit will be wrong for a short time which is not a big
  510. * issue.
  511. */
  512. rcu_read_lock();
  513. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  514. if (!chanctx_conf ||
  515. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
  516. rcu_read_unlock();
  517. return BT_COEX_LOOSE_LUT;
  518. }
  519. ret = BT_COEX_TX_DIS_LUT;
  520. if (mvm->cfg->bt_shared_single_ant) {
  521. rcu_read_unlock();
  522. return ret;
  523. }
  524. phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
  525. if (mvm->last_bt_ci_cmd.primary_ch_phy_id == phy_ctx_id)
  526. ret = le32_to_cpu(mvm->last_bt_notif.primary_ch_lut);
  527. else if (mvm->last_bt_ci_cmd.secondary_ch_phy_id == phy_ctx_id)
  528. ret = le32_to_cpu(mvm->last_bt_notif.secondary_ch_lut);
  529. /* else - default = TX TX disallowed */
  530. rcu_read_unlock();
  531. return ret;
  532. }
  533. int iwl_send_bt_init_conf(struct iwl_mvm *mvm)
  534. {
  535. struct iwl_bt_coex_cmd *bt_cmd;
  536. struct iwl_host_cmd cmd = {
  537. .id = BT_CONFIG,
  538. .len = { sizeof(*bt_cmd), },
  539. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  540. };
  541. int ret;
  542. u32 flags;
  543. ret = iwl_send_bt_prio_tbl(mvm);
  544. if (ret)
  545. return ret;
  546. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  547. if (!bt_cmd)
  548. return -ENOMEM;
  549. cmd.data[0] = bt_cmd;
  550. bt_cmd->max_kill = 5;
  551. bt_cmd->bt4_antenna_isolation_thr = BT_ANTENNA_COUPLING_THRESHOLD;
  552. bt_cmd->bt4_antenna_isolation = iwlwifi_mod_params.ant_coupling;
  553. bt_cmd->bt4_tx_tx_delta_freq_thr = 15;
  554. bt_cmd->bt4_tx_rx_max_freq0 = 15;
  555. bt_cmd->override_primary_lut = BT_COEX_INVALID_LUT;
  556. bt_cmd->override_secondary_lut = BT_COEX_INVALID_LUT;
  557. flags = iwlwifi_mod_params.bt_coex_active ?
  558. BT_COEX_NW : BT_COEX_DISABLE;
  559. bt_cmd->flags = cpu_to_le32(flags);
  560. bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE |
  561. BT_VALID_BT_PRIO_BOOST |
  562. BT_VALID_MAX_KILL |
  563. BT_VALID_3W_TMRS |
  564. BT_VALID_KILL_ACK |
  565. BT_VALID_KILL_CTS |
  566. BT_VALID_REDUCED_TX_POWER |
  567. BT_VALID_LUT |
  568. BT_VALID_WIFI_RX_SW_PRIO_BOOST |
  569. BT_VALID_WIFI_TX_SW_PRIO_BOOST |
  570. BT_VALID_ANT_ISOLATION |
  571. BT_VALID_ANT_ISOLATION_THRS |
  572. BT_VALID_TXTX_DELTA_FREQ_THRS |
  573. BT_VALID_TXRX_MAX_FREQ_0 |
  574. BT_VALID_SYNC_TO_SCO);
  575. if (IWL_MVM_BT_COEX_SYNC2SCO)
  576. bt_cmd->flags |= cpu_to_le32(BT_COEX_SYNC2SCO);
  577. if (IWL_MVM_BT_COEX_CORUNNING) {
  578. bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_CORUN_LUT_20 |
  579. BT_VALID_CORUN_LUT_40);
  580. bt_cmd->flags |= cpu_to_le32(BT_COEX_CORUNNING);
  581. }
  582. if (IWL_MVM_BT_COEX_MPLUT) {
  583. bt_cmd->flags |= cpu_to_le32(BT_COEX_MPLUT);
  584. bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_MULTI_PRIO_LUT);
  585. }
  586. if (mvm->cfg->bt_shared_single_ant)
  587. memcpy(&bt_cmd->decision_lut, iwl_single_shared_ant,
  588. sizeof(iwl_single_shared_ant));
  589. else
  590. memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
  591. sizeof(iwl_combined_lookup));
  592. /* Take first Co-running block LUT to get started */
  593. memcpy(bt_cmd->bt4_corun_lut20, antenna_coupling_ranges[0].lut20,
  594. sizeof(bt_cmd->bt4_corun_lut20));
  595. memcpy(bt_cmd->bt4_corun_lut40, antenna_coupling_ranges[0].lut20,
  596. sizeof(bt_cmd->bt4_corun_lut40));
  597. memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
  598. sizeof(iwl_bt_prio_boost));
  599. memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
  600. sizeof(iwl_bt_mprio_lut));
  601. bt_cmd->kill_ack_msk =
  602. cpu_to_le32(iwl_bt_ack_kill_msk[BT_KILL_MSK_DEFAULT]);
  603. bt_cmd->kill_cts_msk =
  604. cpu_to_le32(iwl_bt_cts_kill_msk[BT_KILL_MSK_DEFAULT]);
  605. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  606. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  607. ret = iwl_mvm_send_cmd(mvm, &cmd);
  608. kfree(bt_cmd);
  609. return ret;
  610. }
  611. static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm,
  612. bool reduced_tx_power)
  613. {
  614. enum iwl_bt_kill_msk bt_kill_msk;
  615. struct iwl_bt_coex_cmd *bt_cmd;
  616. struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
  617. struct iwl_host_cmd cmd = {
  618. .id = BT_CONFIG,
  619. .data[0] = &bt_cmd,
  620. .len = { sizeof(*bt_cmd), },
  621. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  622. };
  623. int ret = 0;
  624. lockdep_assert_held(&mvm->mutex);
  625. if (reduced_tx_power) {
  626. /* Reduced Tx power has precedence on the type of the profile */
  627. bt_kill_msk = BT_KILL_MSK_REDUCED_TXPOW;
  628. } else {
  629. /* Low latency BT profile is active: give higher prio to BT */
  630. if (BT_MBOX_MSG(notif, 3, SCO_STATE) ||
  631. BT_MBOX_MSG(notif, 3, A2DP_STATE) ||
  632. BT_MBOX_MSG(notif, 3, SNIFF_STATE))
  633. bt_kill_msk = BT_KILL_MSK_SCO_HID_A2DP;
  634. else
  635. bt_kill_msk = BT_KILL_MSK_DEFAULT;
  636. }
  637. IWL_DEBUG_COEX(mvm,
  638. "Update kill_msk: %d - SCO %sactive A2DP %sactive SNIFF %sactive\n",
  639. bt_kill_msk,
  640. BT_MBOX_MSG(notif, 3, SCO_STATE) ? "" : "in",
  641. BT_MBOX_MSG(notif, 3, A2DP_STATE) ? "" : "in",
  642. BT_MBOX_MSG(notif, 3, SNIFF_STATE) ? "" : "in");
  643. /* Don't send HCMD if there is no update */
  644. if (bt_kill_msk == mvm->bt_kill_msk)
  645. return 0;
  646. mvm->bt_kill_msk = bt_kill_msk;
  647. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  648. if (!bt_cmd)
  649. return -ENOMEM;
  650. cmd.data[0] = bt_cmd;
  651. bt_cmd->flags = cpu_to_le32(BT_COEX_NW);
  652. bt_cmd->kill_ack_msk = cpu_to_le32(iwl_bt_ack_kill_msk[bt_kill_msk]);
  653. bt_cmd->kill_cts_msk = cpu_to_le32(iwl_bt_cts_kill_msk[bt_kill_msk]);
  654. bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_ENABLE |
  655. BT_VALID_KILL_ACK |
  656. BT_VALID_KILL_CTS);
  657. IWL_DEBUG_COEX(mvm, "ACK Kill msk = 0x%08x, CTS Kill msk = 0x%08x\n",
  658. iwl_bt_ack_kill_msk[bt_kill_msk],
  659. iwl_bt_cts_kill_msk[bt_kill_msk]);
  660. ret = iwl_mvm_send_cmd(mvm, &cmd);
  661. kfree(bt_cmd);
  662. return ret;
  663. }
  664. static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
  665. bool enable)
  666. {
  667. struct iwl_bt_coex_cmd *bt_cmd;
  668. /* Send ASYNC since this can be sent from an atomic context */
  669. struct iwl_host_cmd cmd = {
  670. .id = BT_CONFIG,
  671. .len = { sizeof(*bt_cmd), },
  672. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  673. .flags = CMD_ASYNC,
  674. };
  675. struct iwl_mvm_sta *mvmsta;
  676. int ret;
  677. mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
  678. if (!mvmsta)
  679. return 0;
  680. /* nothing to do */
  681. if (mvmsta->bt_reduced_txpower == enable)
  682. return 0;
  683. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
  684. if (!bt_cmd)
  685. return -ENOMEM;
  686. cmd.data[0] = bt_cmd;
  687. bt_cmd->flags = cpu_to_le32(BT_COEX_NW);
  688. bt_cmd->valid_bit_msk =
  689. cpu_to_le32(BT_VALID_ENABLE | BT_VALID_REDUCED_TX_POWER);
  690. bt_cmd->bt_reduced_tx_power = sta_id;
  691. if (enable)
  692. bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
  693. IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
  694. enable ? "en" : "dis", sta_id);
  695. mvmsta->bt_reduced_txpower = enable;
  696. ret = iwl_mvm_send_cmd(mvm, &cmd);
  697. kfree(bt_cmd);
  698. return ret;
  699. }
  700. struct iwl_bt_iterator_data {
  701. struct iwl_bt_coex_profile_notif *notif;
  702. struct iwl_mvm *mvm;
  703. u32 num_bss_ifaces;
  704. bool reduced_tx_power;
  705. struct ieee80211_chanctx_conf *primary;
  706. struct ieee80211_chanctx_conf *secondary;
  707. bool primary_ll;
  708. };
  709. static inline
  710. void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
  711. struct ieee80211_vif *vif,
  712. bool enable, int rssi)
  713. {
  714. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  715. mvmvif->bf_data.last_bt_coex_event = rssi;
  716. mvmvif->bf_data.bt_coex_max_thold =
  717. enable ? BT_ENABLE_REDUCED_TXPOWER_THRESHOLD : 0;
  718. mvmvif->bf_data.bt_coex_min_thold =
  719. enable ? BT_DISABLE_REDUCED_TXPOWER_THRESHOLD : 0;
  720. }
  721. /* must be called under rcu_read_lock */
  722. static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
  723. struct ieee80211_vif *vif)
  724. {
  725. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  726. struct iwl_bt_iterator_data *data = _data;
  727. struct iwl_mvm *mvm = data->mvm;
  728. struct ieee80211_chanctx_conf *chanctx_conf;
  729. enum ieee80211_smps_mode smps_mode;
  730. u32 bt_activity_grading;
  731. int ave_rssi;
  732. lockdep_assert_held(&mvm->mutex);
  733. switch (vif->type) {
  734. case NL80211_IFTYPE_STATION:
  735. /* Count BSSes vifs */
  736. data->num_bss_ifaces++;
  737. /* default smps_mode for BSS / P2P client is AUTOMATIC */
  738. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  739. break;
  740. case NL80211_IFTYPE_AP:
  741. /* default smps_mode for AP / GO is OFF */
  742. smps_mode = IEEE80211_SMPS_OFF;
  743. if (!mvmvif->ap_ibss_active) {
  744. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  745. smps_mode);
  746. return;
  747. }
  748. /* the Ack / Cts kill mask must be default if AP / GO */
  749. data->reduced_tx_power = false;
  750. break;
  751. default:
  752. return;
  753. }
  754. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  755. /* If channel context is invalid or not on 2.4GHz .. */
  756. if ((!chanctx_conf ||
  757. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
  758. /* ... relax constraints and disable rssi events */
  759. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
  760. smps_mode);
  761. data->reduced_tx_power = false;
  762. if (vif->type == NL80211_IFTYPE_STATION) {
  763. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  764. false);
  765. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  766. }
  767. return;
  768. }
  769. bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
  770. if (bt_activity_grading >= BT_HIGH_TRAFFIC)
  771. smps_mode = IEEE80211_SMPS_STATIC;
  772. else if (bt_activity_grading >= BT_LOW_TRAFFIC)
  773. smps_mode = vif->type == NL80211_IFTYPE_AP ?
  774. IEEE80211_SMPS_OFF :
  775. IEEE80211_SMPS_DYNAMIC;
  776. /* relax SMPS contraints for next association */
  777. if (!vif->bss_conf.assoc)
  778. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  779. IWL_DEBUG_COEX(data->mvm,
  780. "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
  781. mvmvif->id, data->notif->bt_status, bt_activity_grading,
  782. smps_mode);
  783. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX, smps_mode);
  784. /* low latency is always primary */
  785. if (iwl_mvm_vif_low_latency(mvmvif)) {
  786. data->primary_ll = true;
  787. data->secondary = data->primary;
  788. data->primary = chanctx_conf;
  789. }
  790. if (vif->type == NL80211_IFTYPE_AP) {
  791. if (!mvmvif->ap_ibss_active)
  792. return;
  793. if (chanctx_conf == data->primary)
  794. return;
  795. if (!data->primary_ll) {
  796. /*
  797. * downgrade the current primary no matter what its
  798. * type is.
  799. */
  800. data->secondary = data->primary;
  801. data->primary = chanctx_conf;
  802. } else {
  803. /* there is low latency vif - we will be secondary */
  804. data->secondary = chanctx_conf;
  805. }
  806. return;
  807. }
  808. /*
  809. * STA / P2P Client, try to be primary if first vif. If we are in low
  810. * latency mode, we are already in primary and just don't do much
  811. */
  812. if (!data->primary || data->primary == chanctx_conf)
  813. data->primary = chanctx_conf;
  814. else if (!data->secondary)
  815. /* if secondary is not NULL, it might be a GO */
  816. data->secondary = chanctx_conf;
  817. /*
  818. * don't reduce the Tx power if one of these is true:
  819. * we are in LOOSE
  820. * single share antenna product
  821. * BT is active
  822. * we are associated
  823. */
  824. if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
  825. mvm->cfg->bt_shared_single_ant || !vif->bss_conf.assoc ||
  826. !data->notif->bt_status) {
  827. data->reduced_tx_power = false;
  828. iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false);
  829. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
  830. return;
  831. }
  832. /* try to get the avg rssi from fw */
  833. ave_rssi = mvmvif->bf_data.ave_beacon_signal;
  834. /* if the RSSI isn't valid, fake it is very low */
  835. if (!ave_rssi)
  836. ave_rssi = -100;
  837. if (ave_rssi > BT_ENABLE_REDUCED_TXPOWER_THRESHOLD) {
  838. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
  839. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  840. /*
  841. * bt_kill_msk can be BT_KILL_MSK_REDUCED_TXPOW only if all the
  842. * BSS / P2P clients have rssi above threshold.
  843. * We set the bt_kill_msk to BT_KILL_MSK_REDUCED_TXPOW before
  844. * the iteration, if one interface's rssi isn't good enough,
  845. * bt_kill_msk will be set to default values.
  846. */
  847. } else if (ave_rssi < BT_DISABLE_REDUCED_TXPOWER_THRESHOLD) {
  848. if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
  849. IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
  850. /*
  851. * One interface hasn't rssi above threshold, bt_kill_msk must
  852. * be set to default values.
  853. */
  854. data->reduced_tx_power = false;
  855. }
  856. /* Begin to monitor the RSSI: it may influence the reduced Tx power */
  857. iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
  858. }
  859. static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
  860. {
  861. struct iwl_bt_iterator_data data = {
  862. .mvm = mvm,
  863. .notif = &mvm->last_bt_notif,
  864. .reduced_tx_power = true,
  865. };
  866. struct iwl_bt_coex_ci_cmd cmd = {};
  867. u8 ci_bw_idx;
  868. rcu_read_lock();
  869. ieee80211_iterate_active_interfaces_atomic(
  870. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  871. iwl_mvm_bt_notif_iterator, &data);
  872. if (data.primary) {
  873. struct ieee80211_chanctx_conf *chan = data.primary;
  874. if (WARN_ON(!chan->def.chan)) {
  875. rcu_read_unlock();
  876. return;
  877. }
  878. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  879. ci_bw_idx = 0;
  880. cmd.co_run_bw_primary = 0;
  881. } else {
  882. cmd.co_run_bw_primary = 1;
  883. if (chan->def.center_freq1 >
  884. chan->def.chan->center_freq)
  885. ci_bw_idx = 2;
  886. else
  887. ci_bw_idx = 1;
  888. }
  889. cmd.bt_primary_ci =
  890. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  891. cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
  892. }
  893. if (data.secondary) {
  894. struct ieee80211_chanctx_conf *chan = data.secondary;
  895. if (WARN_ON(!data.secondary->def.chan)) {
  896. rcu_read_unlock();
  897. return;
  898. }
  899. if (chan->def.width < NL80211_CHAN_WIDTH_40) {
  900. ci_bw_idx = 0;
  901. cmd.co_run_bw_secondary = 0;
  902. } else {
  903. cmd.co_run_bw_secondary = 1;
  904. if (chan->def.center_freq1 >
  905. chan->def.chan->center_freq)
  906. ci_bw_idx = 2;
  907. else
  908. ci_bw_idx = 1;
  909. }
  910. cmd.bt_secondary_ci =
  911. iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
  912. cmd.secondary_ch_phy_id = *((u16 *)data.secondary->drv_priv);
  913. }
  914. rcu_read_unlock();
  915. /* Don't spam the fw with the same command over and over */
  916. if (memcmp(&cmd, &mvm->last_bt_ci_cmd, sizeof(cmd))) {
  917. if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
  918. sizeof(cmd), &cmd))
  919. IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
  920. memcpy(&mvm->last_bt_ci_cmd, &cmd, sizeof(cmd));
  921. }
  922. /*
  923. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  924. * irrelevant since it is based on the RSSI coming from the beacon.
  925. * Use BT_KILL_MSK_DEFAULT in that case.
  926. */
  927. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  928. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  929. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  930. }
  931. int iwl_mvm_rx_bt_coex_notif(struct iwl_mvm *mvm,
  932. struct iwl_rx_cmd_buffer *rxb,
  933. struct iwl_device_cmd *dev_cmd)
  934. {
  935. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  936. struct iwl_bt_coex_profile_notif *notif = (void *)pkt->data;
  937. IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
  938. IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
  939. notif->bt_status ? "ON" : "OFF");
  940. IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
  941. IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
  942. IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
  943. le32_to_cpu(notif->primary_ch_lut));
  944. IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
  945. le32_to_cpu(notif->secondary_ch_lut));
  946. IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
  947. le32_to_cpu(notif->bt_activity_grading));
  948. IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
  949. notif->bt_agg_traffic_load);
  950. /* remember this notification for future use: rssi fluctuations */
  951. memcpy(&mvm->last_bt_notif, notif, sizeof(mvm->last_bt_notif));
  952. iwl_mvm_bt_coex_notif_handle(mvm);
  953. /*
  954. * This is an async handler for a notification, returning anything other
  955. * than 0 doesn't make sense even if HCMD failed.
  956. */
  957. return 0;
  958. }
  959. static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
  960. struct ieee80211_vif *vif)
  961. {
  962. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  963. struct iwl_bt_iterator_data *data = _data;
  964. struct iwl_mvm *mvm = data->mvm;
  965. struct ieee80211_sta *sta;
  966. struct iwl_mvm_sta *mvmsta;
  967. struct ieee80211_chanctx_conf *chanctx_conf;
  968. rcu_read_lock();
  969. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  970. /* If channel context is invalid or not on 2.4GHz - don't count it */
  971. if (!chanctx_conf ||
  972. chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
  973. rcu_read_unlock();
  974. return;
  975. }
  976. rcu_read_unlock();
  977. if (vif->type != NL80211_IFTYPE_STATION ||
  978. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  979. return;
  980. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  981. lockdep_is_held(&mvm->mutex));
  982. /* This can happen if the station has been removed right now */
  983. if (IS_ERR_OR_NULL(sta))
  984. return;
  985. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  986. data->num_bss_ifaces++;
  987. /*
  988. * This interface doesn't support reduced Tx power (because of low
  989. * RSSI probably), then set bt_kill_msk to default values.
  990. */
  991. if (!mvmsta->bt_reduced_txpower)
  992. data->reduced_tx_power = false;
  993. /* else - possibly leave it to BT_KILL_MSK_REDUCED_TXPOW */
  994. }
  995. void iwl_mvm_bt_rssi_event(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  996. enum ieee80211_rssi_event rssi_event)
  997. {
  998. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  999. struct iwl_bt_iterator_data data = {
  1000. .mvm = mvm,
  1001. .reduced_tx_power = true,
  1002. };
  1003. int ret;
  1004. lockdep_assert_held(&mvm->mutex);
  1005. /*
  1006. * Rssi update while not associated - can happen since the statistics
  1007. * are handled asynchronously
  1008. */
  1009. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  1010. return;
  1011. /* No BT - reports should be disabled */
  1012. if (!mvm->last_bt_notif.bt_status)
  1013. return;
  1014. IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
  1015. rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
  1016. /*
  1017. * Check if rssi is good enough for reduced Tx power, but not in loose
  1018. * scheme.
  1019. */
  1020. if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
  1021. iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
  1022. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
  1023. false);
  1024. else
  1025. ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
  1026. if (ret)
  1027. IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
  1028. ieee80211_iterate_active_interfaces_atomic(
  1029. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1030. iwl_mvm_bt_rssi_iterator, &data);
  1031. /*
  1032. * If there are no BSS / P2P client interfaces, reduced Tx Power is
  1033. * irrelevant since it is based on the RSSI coming from the beacon.
  1034. * Use BT_KILL_MSK_DEFAULT in that case.
  1035. */
  1036. data.reduced_tx_power = data.reduced_tx_power && data.num_bss_ifaces;
  1037. if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm, data.reduced_tx_power))
  1038. IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
  1039. }
  1040. #define LINK_QUAL_AGG_TIME_LIMIT_DEF (4000)
  1041. #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
  1042. u16 iwl_mvm_coex_agg_time_limit(struct iwl_mvm *mvm,
  1043. struct ieee80211_sta *sta)
  1044. {
  1045. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1046. enum iwl_bt_coex_lut_type lut_type;
  1047. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  1048. BT_HIGH_TRAFFIC)
  1049. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  1050. lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
  1051. if (lut_type == BT_COEX_LOOSE_LUT)
  1052. return LINK_QUAL_AGG_TIME_LIMIT_DEF;
  1053. /* tight coex, high bt traffic, reduce AGG time limit */
  1054. return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
  1055. }
  1056. bool iwl_mvm_bt_coex_is_mimo_allowed(struct iwl_mvm *mvm,
  1057. struct ieee80211_sta *sta)
  1058. {
  1059. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1060. if (le32_to_cpu(mvm->last_bt_notif.bt_activity_grading) <
  1061. BT_HIGH_TRAFFIC)
  1062. return true;
  1063. /*
  1064. * In Tight, BT can't Rx while we Tx, so use both antennas since BT is
  1065. * already killed.
  1066. * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while we
  1067. * Tx.
  1068. */
  1069. return iwl_get_coex_type(mvm, mvmsta->vif) == BT_COEX_TIGHT_LUT;
  1070. }
  1071. bool iwl_mvm_bt_coex_is_tpc_allowed(struct iwl_mvm *mvm,
  1072. enum ieee80211_band band)
  1073. {
  1074. u32 bt_activity = le32_to_cpu(mvm->last_bt_notif.bt_activity_grading);
  1075. if (band != IEEE80211_BAND_2GHZ)
  1076. return false;
  1077. return bt_activity >= BT_LOW_TRAFFIC;
  1078. }
  1079. u8 iwl_mvm_bt_coex_tx_prio(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
  1080. struct ieee80211_tx_info *info, u8 ac)
  1081. {
  1082. __le16 fc = hdr->frame_control;
  1083. if (info->band != IEEE80211_BAND_2GHZ)
  1084. return 0;
  1085. if (unlikely(mvm->bt_tx_prio))
  1086. return mvm->bt_tx_prio - 1;
  1087. /* High prio packet (wrt. BT coex) if it is EAPOL, MCAST or MGMT */
  1088. if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO ||
  1089. is_multicast_ether_addr(hdr->addr1) ||
  1090. ieee80211_is_ctl(fc) || ieee80211_is_mgmt(fc) ||
  1091. ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc))
  1092. return 3;
  1093. switch (ac) {
  1094. case IEEE80211_AC_BE:
  1095. return 1;
  1096. case IEEE80211_AC_VO:
  1097. return 3;
  1098. case IEEE80211_AC_VI:
  1099. return 2;
  1100. default:
  1101. break;
  1102. }
  1103. return 0;
  1104. }
  1105. void iwl_mvm_bt_coex_vif_change(struct iwl_mvm *mvm)
  1106. {
  1107. iwl_mvm_bt_coex_notif_handle(mvm);
  1108. }
  1109. int iwl_mvm_rx_ant_coupling_notif(struct iwl_mvm *mvm,
  1110. struct iwl_rx_cmd_buffer *rxb,
  1111. struct iwl_device_cmd *dev_cmd)
  1112. {
  1113. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1114. u32 ant_isolation = le32_to_cpup((void *)pkt->data);
  1115. u8 __maybe_unused lower_bound, upper_bound;
  1116. int ret;
  1117. u8 lut;
  1118. struct iwl_bt_coex_cmd *bt_cmd;
  1119. struct iwl_host_cmd cmd = {
  1120. .id = BT_CONFIG,
  1121. .len = { sizeof(*bt_cmd), },
  1122. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1123. };
  1124. if (!IWL_MVM_BT_COEX_CORUNNING)
  1125. return 0;
  1126. lockdep_assert_held(&mvm->mutex);
  1127. if (ant_isolation == mvm->last_ant_isol)
  1128. return 0;
  1129. for (lut = 0; lut < ARRAY_SIZE(antenna_coupling_ranges) - 1; lut++)
  1130. if (ant_isolation < antenna_coupling_ranges[lut + 1].range)
  1131. break;
  1132. lower_bound = antenna_coupling_ranges[lut].range;
  1133. if (lut < ARRAY_SIZE(antenna_coupling_ranges) - 1)
  1134. upper_bound = antenna_coupling_ranges[lut + 1].range;
  1135. else
  1136. upper_bound = antenna_coupling_ranges[lut].range;
  1137. IWL_DEBUG_COEX(mvm, "Antenna isolation=%d in range [%d,%d[, lut=%d\n",
  1138. ant_isolation, lower_bound, upper_bound, lut);
  1139. mvm->last_ant_isol = ant_isolation;
  1140. if (mvm->last_corun_lut == lut)
  1141. return 0;
  1142. mvm->last_corun_lut = lut;
  1143. bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
  1144. if (!bt_cmd)
  1145. return 0;
  1146. cmd.data[0] = bt_cmd;
  1147. bt_cmd->flags = cpu_to_le32(BT_COEX_NW);
  1148. bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_ENABLE |
  1149. BT_VALID_CORUN_LUT_20 |
  1150. BT_VALID_CORUN_LUT_40);
  1151. /* For the moment, use the same LUT for 20GHz and 40GHz */
  1152. memcpy(bt_cmd->bt4_corun_lut20, antenna_coupling_ranges[lut].lut20,
  1153. sizeof(bt_cmd->bt4_corun_lut20));
  1154. memcpy(bt_cmd->bt4_corun_lut40, antenna_coupling_ranges[lut].lut20,
  1155. sizeof(bt_cmd->bt4_corun_lut40));
  1156. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1157. kfree(bt_cmd);
  1158. return ret;
  1159. }