]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/mvm/coex_legacy.c
Merge tag 'xtensa-for-next-20140815' into for_next
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / mvm / coex_legacy.c
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
64 #include <linux/ieee80211.h>
65 #include <linux/etherdevice.h>
66 #include <net/mac80211.h>
67
68 #include "fw-api-coex.h"
69 #include "iwl-modparams.h"
70 #include "mvm.h"
71 #include "iwl-debug.h"
72
73 #define EVENT_PRIO_ANT(_evt, _prio, _shrd_ant)                  \
74         [(_evt)] = (((_prio) << BT_COEX_PRIO_TBL_PRIO_POS) |    \
75                    ((_shrd_ant) << BT_COEX_PRIO_TBL_SHRD_ANT_POS))
76
77 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
78         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB1,
79                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 0),
80         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_INIT_CALIB2,
81                        BT_COEX_PRIO_TBL_PRIO_BYPASS, 1),
82         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW1,
83                        BT_COEX_PRIO_TBL_PRIO_LOW, 0),
84         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_LOW2,
85                        BT_COEX_PRIO_TBL_PRIO_LOW, 1),
86         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH1,
87                        BT_COEX_PRIO_TBL_PRIO_HIGH, 0),
88         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_PERIODIC_CALIB_HIGH2,
89                        BT_COEX_PRIO_TBL_PRIO_HIGH, 1),
90         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_DTIM,
91                        BT_COEX_PRIO_TBL_DISABLED, 0),
92         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN52,
93                        BT_COEX_PRIO_TBL_PRIO_COEX_OFF, 0),
94         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_SCAN24,
95                        BT_COEX_PRIO_TBL_PRIO_COEX_ON, 0),
96         EVENT_PRIO_ANT(BT_COEX_PRIO_TBL_EVT_IDLE,
97                        BT_COEX_PRIO_TBL_PRIO_COEX_IDLE, 0),
98         0, 0, 0, 0, 0, 0,
99 };
100
101 #undef EVENT_PRIO_ANT
102
103 #define BT_ANTENNA_COUPLING_THRESHOLD           (30)
104
105 static int iwl_send_bt_prio_tbl(struct iwl_mvm *mvm)
106 {
107         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
108                 return 0;
109
110         return iwl_mvm_send_cmd_pdu(mvm, BT_COEX_PRIO_TABLE, 0,
111                                     sizeof(struct iwl_bt_coex_prio_tbl_cmd),
112                                     &iwl_bt_prio_tbl);
113 }
114
115 static const __le32 iwl_bt_prio_boost[BT_COEX_BOOST_SIZE] = {
116         cpu_to_le32(0xf0f0f0f0), /* 50% */
117         cpu_to_le32(0xc0c0c0c0), /* 25% */
118         cpu_to_le32(0xfcfcfcfc), /* 75% */
119         cpu_to_le32(0xfefefefe), /* 87.5% */
120 };
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
167 static const __le32 iwl_combined_lookup[BT_COEX_MAX_LUT][BT_COEX_LUT_SIZE] = {
168         {
169                 /* Tight */
170                 cpu_to_le32(0xaaaaaaaa),
171                 cpu_to_le32(0xaaaaaaaa),
172                 cpu_to_le32(0xaeaaaaaa),
173                 cpu_to_le32(0xaaaaaaaa),
174                 cpu_to_le32(0xcc00ff28),
175                 cpu_to_le32(0x0000aaaa),
176                 cpu_to_le32(0xcc00aaaa),
177                 cpu_to_le32(0x0000aaaa),
178                 cpu_to_le32(0xc0004000),
179                 cpu_to_le32(0x00004000),
180                 cpu_to_le32(0xf0005000),
181                 cpu_to_le32(0xf0005000),
182         },
183         {
184                 /* Loose */
185                 cpu_to_le32(0xaaaaaaaa),
186                 cpu_to_le32(0xaaaaaaaa),
187                 cpu_to_le32(0xaaaaaaaa),
188                 cpu_to_le32(0xaaaaaaaa),
189                 cpu_to_le32(0xcc00ff28),
190                 cpu_to_le32(0x0000aaaa),
191                 cpu_to_le32(0xcc00aaaa),
192                 cpu_to_le32(0x0000aaaa),
193                 cpu_to_le32(0x00000000),
194                 cpu_to_le32(0x00000000),
195                 cpu_to_le32(0xf0005000),
196                 cpu_to_le32(0xf0005000),
197         },
198         {
199                 /* Tx Tx disabled */
200                 cpu_to_le32(0xaaaaaaaa),
201                 cpu_to_le32(0xaaaaaaaa),
202                 cpu_to_le32(0xeeaaaaaa),
203                 cpu_to_le32(0xaaaaaaaa),
204                 cpu_to_le32(0xcc00ff28),
205                 cpu_to_le32(0x0000aaaa),
206                 cpu_to_le32(0xcc00aaaa),
207                 cpu_to_le32(0x0000aaaa),
208                 cpu_to_le32(0xc0004000),
209                 cpu_to_le32(0xc0004000),
210                 cpu_to_le32(0xf0005000),
211                 cpu_to_le32(0xf0005000),
212         },
213 };
214
215 /* 20MHz / 40MHz below / 40Mhz above*/
216 static const __le64 iwl_ci_mask[][3] = {
217         /* dummy entry for channel 0 */
218         {cpu_to_le64(0), cpu_to_le64(0), cpu_to_le64(0)},
219         {
220                 cpu_to_le64(0x0000001FFFULL),
221                 cpu_to_le64(0x0ULL),
222                 cpu_to_le64(0x00007FFFFFULL),
223         },
224         {
225                 cpu_to_le64(0x000000FFFFULL),
226                 cpu_to_le64(0x0ULL),
227                 cpu_to_le64(0x0003FFFFFFULL),
228         },
229         {
230                 cpu_to_le64(0x000003FFFCULL),
231                 cpu_to_le64(0x0ULL),
232                 cpu_to_le64(0x000FFFFFFCULL),
233         },
234         {
235                 cpu_to_le64(0x00001FFFE0ULL),
236                 cpu_to_le64(0x0ULL),
237                 cpu_to_le64(0x007FFFFFE0ULL),
238         },
239         {
240                 cpu_to_le64(0x00007FFF80ULL),
241                 cpu_to_le64(0x00007FFFFFULL),
242                 cpu_to_le64(0x01FFFFFF80ULL),
243         },
244         {
245                 cpu_to_le64(0x0003FFFC00ULL),
246                 cpu_to_le64(0x0003FFFFFFULL),
247                 cpu_to_le64(0x0FFFFFFC00ULL),
248         },
249         {
250                 cpu_to_le64(0x000FFFF000ULL),
251                 cpu_to_le64(0x000FFFFFFCULL),
252                 cpu_to_le64(0x3FFFFFF000ULL),
253         },
254         {
255                 cpu_to_le64(0x007FFF8000ULL),
256                 cpu_to_le64(0x007FFFFFE0ULL),
257                 cpu_to_le64(0xFFFFFF8000ULL),
258         },
259         {
260                 cpu_to_le64(0x01FFFE0000ULL),
261                 cpu_to_le64(0x01FFFFFF80ULL),
262                 cpu_to_le64(0xFFFFFE0000ULL),
263         },
264         {
265                 cpu_to_le64(0x0FFFF00000ULL),
266                 cpu_to_le64(0x0FFFFFFC00ULL),
267                 cpu_to_le64(0x0ULL),
268         },
269         {
270                 cpu_to_le64(0x3FFFC00000ULL),
271                 cpu_to_le64(0x3FFFFFF000ULL),
272                 cpu_to_le64(0x0)
273         },
274         {
275                 cpu_to_le64(0xFFFE000000ULL),
276                 cpu_to_le64(0xFFFFFF8000ULL),
277                 cpu_to_le64(0x0)
278         },
279         {
280                 cpu_to_le64(0xFFF8000000ULL),
281                 cpu_to_le64(0xFFFFFE0000ULL),
282                 cpu_to_le64(0x0)
283         },
284         {
285                 cpu_to_le64(0xFFC0000000ULL),
286                 cpu_to_le64(0x0ULL),
287                 cpu_to_le64(0x0ULL)
288         },
289 };
290
291 static const __le32 iwl_bt_mprio_lut[BT_COEX_MULTI_PRIO_LUT_SIZE] = {
292         cpu_to_le32(0x28412201),
293         cpu_to_le32(0x11118451),
294 };
295
296 struct corunning_block_luts {
297         u8 range;
298         __le32 lut20[BT_COEX_CORUN_LUT_SIZE];
299 };
300
301 /*
302  * Ranges for the antenna coupling calibration / co-running block LUT:
303  *              LUT0: [ 0, 12[
304  *              LUT1: [12, 20[
305  *              LUT2: [20, 21[
306  *              LUT3: [21, 23[
307  *              LUT4: [23, 27[
308  *              LUT5: [27, 30[
309  *              LUT6: [30, 32[
310  *              LUT7: [32, 33[
311  *              LUT8: [33, - [
312  */
313 static const struct corunning_block_luts antenna_coupling_ranges[] = {
314         {
315                 .range = 0,
316                 .lut20 = {
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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
329                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
330                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
331                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
332                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
333                 },
334         },
335         {
336                 .range = 12,
337                 .lut20 = {
338                         cpu_to_le32(0x00000001),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
350                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
351                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
352                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
353                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
354                 },
355         },
356         {
357                 .range = 20,
358                 .lut20 = {
359                         cpu_to_le32(0x00000002),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
371                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
372                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
373                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
374                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
375                 },
376         },
377         {
378                 .range = 21,
379                 .lut20 = {
380                         cpu_to_le32(0x00000003),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
392                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
393                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
394                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
395                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
396                 },
397         },
398         {
399                 .range = 23,
400                 .lut20 = {
401                         cpu_to_le32(0x00000004),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
413                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
414                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
415                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
416                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
417                 },
418         },
419         {
420                 .range = 27,
421                 .lut20 = {
422                         cpu_to_le32(0x00000005),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
434                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
435                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
436                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
437                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
438                 },
439         },
440         {
441                 .range = 30,
442                 .lut20 = {
443                         cpu_to_le32(0x00000006),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
455                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
456                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
457                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
458                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
459                 },
460         },
461         {
462                 .range = 32,
463                 .lut20 = {
464                         cpu_to_le32(0x00000007),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
476                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
477                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
478                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
479                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
480                 },
481         },
482         {
483                 .range = 33,
484                 .lut20 = {
485                         cpu_to_le32(0x00000008),  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                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
497                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
498                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
499                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
500                         cpu_to_le32(0x00000000),  cpu_to_le32(0x00000000),
501                 },
502         },
503 };
504
505 static enum iwl_bt_coex_lut_type
506 iwl_get_coex_type(struct iwl_mvm *mvm, const struct ieee80211_vif *vif)
507 {
508         struct ieee80211_chanctx_conf *chanctx_conf;
509         enum iwl_bt_coex_lut_type ret;
510         u16 phy_ctx_id;
511
512         /*
513          * Checking that we hold mvm->mutex is a good idea, but the rate
514          * control can't acquire the mutex since it runs in Tx path.
515          * So this is racy in that case, but in the worst case, the AMPDU
516          * size limit will be wrong for a short time which is not a big
517          * issue.
518          */
519
520         rcu_read_lock();
521
522         chanctx_conf = rcu_dereference(vif->chanctx_conf);
523
524         if (!chanctx_conf ||
525             chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
526                 rcu_read_unlock();
527                 return BT_COEX_INVALID_LUT;
528         }
529
530         ret = BT_COEX_TX_DIS_LUT;
531
532         if (mvm->cfg->bt_shared_single_ant) {
533                 rcu_read_unlock();
534                 return ret;
535         }
536
537         phy_ctx_id = *((u16 *)chanctx_conf->drv_priv);
538
539         if (mvm->last_bt_ci_cmd_old.primary_ch_phy_id == phy_ctx_id)
540                 ret = le32_to_cpu(mvm->last_bt_notif_old.primary_ch_lut);
541         else if (mvm->last_bt_ci_cmd_old.secondary_ch_phy_id == phy_ctx_id)
542                 ret = le32_to_cpu(mvm->last_bt_notif_old.secondary_ch_lut);
543         /* else - default = TX TX disallowed */
544
545         rcu_read_unlock();
546
547         return ret;
548 }
549
550 int iwl_send_bt_init_conf_old(struct iwl_mvm *mvm)
551 {
552         struct iwl_bt_coex_cmd_old *bt_cmd;
553         struct iwl_host_cmd cmd = {
554                 .id = BT_CONFIG,
555                 .len = { sizeof(*bt_cmd), },
556                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
557         };
558         int ret;
559         u32 flags;
560
561         ret = iwl_send_bt_prio_tbl(mvm);
562         if (ret)
563                 return ret;
564
565         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
566         if (!bt_cmd)
567                 return -ENOMEM;
568         cmd.data[0] = bt_cmd;
569
570         lockdep_assert_held(&mvm->mutex);
571
572         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS)) {
573                 switch (mvm->bt_force_ant_mode) {
574                 case BT_FORCE_ANT_AUTO:
575                         flags = BT_COEX_AUTO_OLD;
576                         break;
577                 case BT_FORCE_ANT_BT:
578                         flags = BT_COEX_BT_OLD;
579                         break;
580                 case BT_FORCE_ANT_WIFI:
581                         flags = BT_COEX_WIFI_OLD;
582                         break;
583                 default:
584                         WARN_ON(1);
585                         flags = 0;
586                 }
587
588                 bt_cmd->flags = cpu_to_le32(flags);
589                 bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE);
590                 goto send_cmd;
591         }
592
593         bt_cmd->max_kill = 5;
594         bt_cmd->bt4_antenna_isolation_thr = BT_ANTENNA_COUPLING_THRESHOLD;
595         bt_cmd->bt4_antenna_isolation = iwlwifi_mod_params.ant_coupling;
596         bt_cmd->bt4_tx_tx_delta_freq_thr = 15;
597         bt_cmd->bt4_tx_rx_max_freq0 = 15;
598         bt_cmd->override_primary_lut = BT_COEX_INVALID_LUT;
599         bt_cmd->override_secondary_lut = BT_COEX_INVALID_LUT;
600
601         flags = iwlwifi_mod_params.bt_coex_active ?
602                         BT_COEX_NW_OLD : BT_COEX_DISABLE_OLD;
603         bt_cmd->flags = cpu_to_le32(flags);
604
605         bt_cmd->valid_bit_msk = cpu_to_le32(BT_VALID_ENABLE |
606                                             BT_VALID_BT_PRIO_BOOST |
607                                             BT_VALID_MAX_KILL |
608                                             BT_VALID_3W_TMRS |
609                                             BT_VALID_KILL_ACK |
610                                             BT_VALID_KILL_CTS |
611                                             BT_VALID_REDUCED_TX_POWER |
612                                             BT_VALID_LUT |
613                                             BT_VALID_WIFI_RX_SW_PRIO_BOOST |
614                                             BT_VALID_WIFI_TX_SW_PRIO_BOOST |
615                                             BT_VALID_ANT_ISOLATION |
616                                             BT_VALID_ANT_ISOLATION_THRS |
617                                             BT_VALID_TXTX_DELTA_FREQ_THRS |
618                                             BT_VALID_TXRX_MAX_FREQ_0 |
619                                             BT_VALID_SYNC_TO_SCO);
620
621         if (IWL_MVM_BT_COEX_SYNC2SCO)
622                 bt_cmd->flags |= cpu_to_le32(BT_COEX_SYNC2SCO);
623
624         if (IWL_MVM_BT_COEX_CORUNNING) {
625                 bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_CORUN_LUT_20 |
626                                                      BT_VALID_CORUN_LUT_40);
627                 bt_cmd->flags |= cpu_to_le32(BT_COEX_CORUNNING);
628         }
629
630         if (IWL_MVM_BT_COEX_MPLUT) {
631                 bt_cmd->flags |= cpu_to_le32(BT_COEX_MPLUT);
632                 bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_MULTI_PRIO_LUT);
633         }
634
635         if (mvm->cfg->bt_shared_single_ant)
636                 memcpy(&bt_cmd->decision_lut, iwl_single_shared_ant,
637                        sizeof(iwl_single_shared_ant));
638         else
639                 memcpy(&bt_cmd->decision_lut, iwl_combined_lookup,
640                        sizeof(iwl_combined_lookup));
641
642         /* Take first Co-running block LUT to get started */
643         memcpy(bt_cmd->bt4_corun_lut20, antenna_coupling_ranges[0].lut20,
644                sizeof(bt_cmd->bt4_corun_lut20));
645         memcpy(bt_cmd->bt4_corun_lut40, antenna_coupling_ranges[0].lut20,
646                sizeof(bt_cmd->bt4_corun_lut40));
647
648         memcpy(&bt_cmd->bt_prio_boost, iwl_bt_prio_boost,
649                sizeof(iwl_bt_prio_boost));
650         memcpy(&bt_cmd->bt4_multiprio_lut, iwl_bt_mprio_lut,
651                sizeof(iwl_bt_mprio_lut));
652
653 send_cmd:
654         memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
655         memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
656
657         ret = iwl_mvm_send_cmd(mvm, &cmd);
658
659         kfree(bt_cmd);
660         return ret;
661 }
662
663 static int iwl_mvm_bt_udpate_ctrl_kill_msk(struct iwl_mvm *mvm)
664 {
665         struct iwl_bt_coex_profile_notif_old *notif = &mvm->last_bt_notif_old;
666         u32 primary_lut = le32_to_cpu(notif->primary_ch_lut);
667         u32 ag = le32_to_cpu(notif->bt_activity_grading);
668         struct iwl_bt_coex_cmd_old *bt_cmd;
669         u8 ack_kill_msk, cts_kill_msk;
670         struct iwl_host_cmd cmd = {
671                 .id = BT_CONFIG,
672                 .data[0] = &bt_cmd,
673                 .len = { sizeof(*bt_cmd), },
674                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
675         };
676         int ret = 0;
677
678         lockdep_assert_held(&mvm->mutex);
679
680         ack_kill_msk = iwl_bt_ack_kill_msk[ag][primary_lut];
681         cts_kill_msk = iwl_bt_cts_kill_msk[ag][primary_lut];
682
683         if (mvm->bt_ack_kill_msk[0] == ack_kill_msk &&
684             mvm->bt_cts_kill_msk[0] == cts_kill_msk)
685                 return 0;
686
687         mvm->bt_ack_kill_msk[0] = ack_kill_msk;
688         mvm->bt_cts_kill_msk[0] = cts_kill_msk;
689
690         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
691         if (!bt_cmd)
692                 return -ENOMEM;
693         cmd.data[0] = bt_cmd;
694         bt_cmd->flags = cpu_to_le32(BT_COEX_NW_OLD);
695
696         bt_cmd->kill_ack_msk = cpu_to_le32(iwl_bt_ctl_kill_msk[ack_kill_msk]);
697         bt_cmd->kill_cts_msk = cpu_to_le32(iwl_bt_ctl_kill_msk[cts_kill_msk]);
698         bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_ENABLE |
699                                              BT_VALID_KILL_ACK |
700                                              BT_VALID_KILL_CTS);
701
702         ret = iwl_mvm_send_cmd(mvm, &cmd);
703
704         kfree(bt_cmd);
705         return ret;
706 }
707
708 static int iwl_mvm_bt_coex_reduced_txp(struct iwl_mvm *mvm, u8 sta_id,
709                                        bool enable)
710 {
711         struct iwl_bt_coex_cmd_old *bt_cmd;
712         /* Send ASYNC since this can be sent from an atomic context */
713         struct iwl_host_cmd cmd = {
714                 .id = BT_CONFIG,
715                 .len = { sizeof(*bt_cmd), },
716                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
717                 .flags = CMD_ASYNC,
718         };
719         struct iwl_mvm_sta *mvmsta;
720         int ret;
721
722         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
723         if (!mvmsta)
724                 return 0;
725
726         /* nothing to do */
727         if (mvmsta->bt_reduced_txpower == enable)
728                 return 0;
729
730         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_ATOMIC);
731         if (!bt_cmd)
732                 return -ENOMEM;
733         cmd.data[0] = bt_cmd;
734         bt_cmd->flags = cpu_to_le32(BT_COEX_NW_OLD);
735
736         bt_cmd->valid_bit_msk =
737                 cpu_to_le32(BT_VALID_ENABLE | BT_VALID_REDUCED_TX_POWER);
738         bt_cmd->bt_reduced_tx_power = sta_id;
739
740         if (enable)
741                 bt_cmd->bt_reduced_tx_power |= BT_REDUCED_TX_POWER_BIT;
742
743         IWL_DEBUG_COEX(mvm, "%sable reduced Tx Power for sta %d\n",
744                        enable ? "en" : "dis", sta_id);
745
746         mvmsta->bt_reduced_txpower = enable;
747
748         ret = iwl_mvm_send_cmd(mvm, &cmd);
749
750         kfree(bt_cmd);
751         return ret;
752 }
753
754 struct iwl_bt_iterator_data {
755         struct iwl_bt_coex_profile_notif_old *notif;
756         struct iwl_mvm *mvm;
757         struct ieee80211_chanctx_conf *primary;
758         struct ieee80211_chanctx_conf *secondary;
759         bool primary_ll;
760 };
761
762 static inline
763 void iwl_mvm_bt_coex_enable_rssi_event(struct iwl_mvm *mvm,
764                                        struct ieee80211_vif *vif,
765                                        bool enable, int rssi)
766 {
767         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
768
769         mvmvif->bf_data.last_bt_coex_event = rssi;
770         mvmvif->bf_data.bt_coex_max_thold =
771                 enable ? -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH : 0;
772         mvmvif->bf_data.bt_coex_min_thold =
773                 enable ? -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH : 0;
774 }
775
776 /* must be called under rcu_read_lock */
777 static void iwl_mvm_bt_notif_iterator(void *_data, u8 *mac,
778                                       struct ieee80211_vif *vif)
779 {
780         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
781         struct iwl_bt_iterator_data *data = _data;
782         struct iwl_mvm *mvm = data->mvm;
783         struct ieee80211_chanctx_conf *chanctx_conf;
784         enum ieee80211_smps_mode smps_mode;
785         u32 bt_activity_grading;
786         int ave_rssi;
787
788         lockdep_assert_held(&mvm->mutex);
789
790         switch (vif->type) {
791         case NL80211_IFTYPE_STATION:
792                 /* default smps_mode for BSS / P2P client is AUTOMATIC */
793                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
794                 break;
795         case NL80211_IFTYPE_AP:
796                 if (!mvmvif->ap_ibss_active)
797                         return;
798                 break;
799         default:
800                 return;
801         }
802
803         chanctx_conf = rcu_dereference(vif->chanctx_conf);
804
805         /* If channel context is invalid or not on 2.4GHz .. */
806         if ((!chanctx_conf ||
807              chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ)) {
808                 if (vif->type == NL80211_IFTYPE_STATION) {
809                         /* ... relax constraints and disable rssi events */
810                         iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
811                                             smps_mode);
812                         iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
813                                                     false);
814                         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
815                 }
816                 return;
817         }
818
819         bt_activity_grading = le32_to_cpu(data->notif->bt_activity_grading);
820         if (bt_activity_grading >= BT_HIGH_TRAFFIC)
821                 smps_mode = IEEE80211_SMPS_STATIC;
822         else if (bt_activity_grading >= BT_LOW_TRAFFIC)
823                 smps_mode = vif->type == NL80211_IFTYPE_AP ?
824                                 IEEE80211_SMPS_OFF :
825                                 IEEE80211_SMPS_DYNAMIC;
826
827         /* relax SMPS contraints for next association */
828         if (!vif->bss_conf.assoc)
829                 smps_mode = IEEE80211_SMPS_AUTOMATIC;
830
831         IWL_DEBUG_COEX(data->mvm,
832                        "mac %d: bt_status %d bt_activity_grading %d smps_req %d\n",
833                        mvmvif->id, data->notif->bt_status, bt_activity_grading,
834                        smps_mode);
835
836         if (vif->type == NL80211_IFTYPE_STATION)
837                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_BT_COEX,
838                                     smps_mode);
839
840         /* low latency is always primary */
841         if (iwl_mvm_vif_low_latency(mvmvif)) {
842                 data->primary_ll = true;
843
844                 data->secondary = data->primary;
845                 data->primary = chanctx_conf;
846         }
847
848         if (vif->type == NL80211_IFTYPE_AP) {
849                 if (!mvmvif->ap_ibss_active)
850                         return;
851
852                 if (chanctx_conf == data->primary)
853                         return;
854
855                 if (!data->primary_ll) {
856                         /*
857                          * downgrade the current primary no matter what its
858                          * type is.
859                          */
860                         data->secondary = data->primary;
861                         data->primary = chanctx_conf;
862                 } else {
863                         /* there is low latency vif - we will be secondary */
864                         data->secondary = chanctx_conf;
865                 }
866                 return;
867         }
868
869         /*
870          * STA / P2P Client, try to be primary if first vif. If we are in low
871          * latency mode, we are already in primary and just don't do much
872          */
873         if (!data->primary || data->primary == chanctx_conf)
874                 data->primary = chanctx_conf;
875         else if (!data->secondary)
876                 /* if secondary is not NULL, it might be a GO */
877                 data->secondary = chanctx_conf;
878
879         /*
880          * don't reduce the Tx power if one of these is true:
881          *  we are in LOOSE
882          *  single share antenna product
883          *  BT is active
884          *  we are associated
885          */
886         if (iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT ||
887             mvm->cfg->bt_shared_single_ant || !vif->bss_conf.assoc ||
888             !data->notif->bt_status) {
889                 iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false);
890                 iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, false, 0);
891                 return;
892         }
893
894         /* try to get the avg rssi from fw */
895         ave_rssi = mvmvif->bf_data.ave_beacon_signal;
896
897         /* if the RSSI isn't valid, fake it is very low */
898         if (!ave_rssi)
899                 ave_rssi = -100;
900         if (ave_rssi > -IWL_MVM_BT_COEX_EN_RED_TXP_THRESH) {
901                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true))
902                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
903         } else if (ave_rssi < -IWL_MVM_BT_COEX_DIS_RED_TXP_THRESH) {
904                 if (iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, false))
905                         IWL_ERR(mvm, "Couldn't send BT_CONFIG cmd\n");
906         }
907
908         /* Begin to monitor the RSSI: it may influence the reduced Tx power */
909         iwl_mvm_bt_coex_enable_rssi_event(mvm, vif, true, ave_rssi);
910 }
911
912 static void iwl_mvm_bt_coex_notif_handle(struct iwl_mvm *mvm)
913 {
914         struct iwl_bt_iterator_data data = {
915                 .mvm = mvm,
916                 .notif = &mvm->last_bt_notif_old,
917         };
918         struct iwl_bt_coex_ci_cmd_old cmd = {};
919         u8 ci_bw_idx;
920
921         /* Ignore updates if we are in force mode */
922         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
923                 return;
924
925         rcu_read_lock();
926         ieee80211_iterate_active_interfaces_atomic(
927                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
928                                         iwl_mvm_bt_notif_iterator, &data);
929
930         if (data.primary) {
931                 struct ieee80211_chanctx_conf *chan = data.primary;
932
933                 if (WARN_ON(!chan->def.chan)) {
934                         rcu_read_unlock();
935                         return;
936                 }
937
938                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
939                         ci_bw_idx = 0;
940                         cmd.co_run_bw_primary = 0;
941                 } else {
942                         cmd.co_run_bw_primary = 1;
943                         if (chan->def.center_freq1 >
944                             chan->def.chan->center_freq)
945                                 ci_bw_idx = 2;
946                         else
947                                 ci_bw_idx = 1;
948                 }
949
950                 cmd.bt_primary_ci =
951                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
952                 cmd.primary_ch_phy_id = *((u16 *)data.primary->drv_priv);
953         }
954
955         if (data.secondary) {
956                 struct ieee80211_chanctx_conf *chan = data.secondary;
957
958                 if (WARN_ON(!data.secondary->def.chan)) {
959                         rcu_read_unlock();
960                         return;
961                 }
962
963                 if (chan->def.width < NL80211_CHAN_WIDTH_40) {
964                         ci_bw_idx = 0;
965                         cmd.co_run_bw_secondary = 0;
966                 } else {
967                         cmd.co_run_bw_secondary = 1;
968                         if (chan->def.center_freq1 >
969                             chan->def.chan->center_freq)
970                                 ci_bw_idx = 2;
971                         else
972                                 ci_bw_idx = 1;
973                 }
974
975                 cmd.bt_secondary_ci =
976                         iwl_ci_mask[chan->def.chan->hw_value][ci_bw_idx];
977                 cmd.secondary_ch_phy_id = *((u16 *)data.secondary->drv_priv);
978         }
979
980         rcu_read_unlock();
981
982         /* Don't spam the fw with the same command over and over */
983         if (memcmp(&cmd, &mvm->last_bt_ci_cmd_old, sizeof(cmd))) {
984                 if (iwl_mvm_send_cmd_pdu(mvm, BT_COEX_CI, 0,
985                                          sizeof(cmd), &cmd))
986                         IWL_ERR(mvm, "Failed to send BT_CI cmd\n");
987                 memcpy(&mvm->last_bt_ci_cmd_old, &cmd, sizeof(cmd));
988         }
989
990         if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm))
991                 IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
992 }
993
994 int iwl_mvm_rx_bt_coex_notif_old(struct iwl_mvm *mvm,
995                                  struct iwl_rx_cmd_buffer *rxb,
996                                  struct iwl_device_cmd *dev_cmd)
997 {
998         struct iwl_rx_packet *pkt = rxb_addr(rxb);
999         struct iwl_bt_coex_profile_notif_old *notif = (void *)pkt->data;
1000
1001         IWL_DEBUG_COEX(mvm, "BT Coex Notification received\n");
1002         IWL_DEBUG_COEX(mvm, "\tBT status: %s\n",
1003                        notif->bt_status ? "ON" : "OFF");
1004         IWL_DEBUG_COEX(mvm, "\tBT open conn %d\n", notif->bt_open_conn);
1005         IWL_DEBUG_COEX(mvm, "\tBT ci compliance %d\n", notif->bt_ci_compliance);
1006         IWL_DEBUG_COEX(mvm, "\tBT primary_ch_lut %d\n",
1007                        le32_to_cpu(notif->primary_ch_lut));
1008         IWL_DEBUG_COEX(mvm, "\tBT secondary_ch_lut %d\n",
1009                        le32_to_cpu(notif->secondary_ch_lut));
1010         IWL_DEBUG_COEX(mvm, "\tBT activity grading %d\n",
1011                        le32_to_cpu(notif->bt_activity_grading));
1012         IWL_DEBUG_COEX(mvm, "\tBT agg traffic load %d\n",
1013                        notif->bt_agg_traffic_load);
1014
1015         /* remember this notification for future use: rssi fluctuations */
1016         memcpy(&mvm->last_bt_notif_old, notif, sizeof(mvm->last_bt_notif_old));
1017
1018         iwl_mvm_bt_coex_notif_handle(mvm);
1019
1020         /*
1021          * This is an async handler for a notification, returning anything other
1022          * than 0 doesn't make sense even if HCMD failed.
1023          */
1024         return 0;
1025 }
1026
1027 static void iwl_mvm_bt_rssi_iterator(void *_data, u8 *mac,
1028                                      struct ieee80211_vif *vif)
1029 {
1030         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1031         struct iwl_bt_iterator_data *data = _data;
1032         struct iwl_mvm *mvm = data->mvm;
1033
1034         struct ieee80211_sta *sta;
1035         struct iwl_mvm_sta *mvmsta;
1036
1037         struct ieee80211_chanctx_conf *chanctx_conf;
1038
1039         rcu_read_lock();
1040         chanctx_conf = rcu_dereference(vif->chanctx_conf);
1041         /* If channel context is invalid or not on 2.4GHz - don't count it */
1042         if (!chanctx_conf ||
1043             chanctx_conf->def.chan->band != IEEE80211_BAND_2GHZ) {
1044                 rcu_read_unlock();
1045                 return;
1046         }
1047         rcu_read_unlock();
1048
1049         if (vif->type != NL80211_IFTYPE_STATION ||
1050             mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
1051                 return;
1052
1053         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1054                                         lockdep_is_held(&mvm->mutex));
1055
1056         /* This can happen if the station has been removed right now */
1057         if (IS_ERR_OR_NULL(sta))
1058                 return;
1059
1060         mvmsta = iwl_mvm_sta_from_mac80211(sta);
1061 }
1062
1063 void iwl_mvm_bt_rssi_event_old(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1064                                enum ieee80211_rssi_event rssi_event)
1065 {
1066         struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1067         struct iwl_bt_iterator_data data = {
1068                 .mvm = mvm,
1069         };
1070         int ret;
1071
1072         lockdep_assert_held(&mvm->mutex);
1073
1074         /* Ignore updates if we are in force mode */
1075         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
1076                 return;
1077
1078         /*
1079          * Rssi update while not associated - can happen since the statistics
1080          * are handled asynchronously
1081          */
1082         if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
1083                 return;
1084
1085         /* No BT - reports should be disabled */
1086         if (!mvm->last_bt_notif_old.bt_status)
1087                 return;
1088
1089         IWL_DEBUG_COEX(mvm, "RSSI for %pM is now %s\n", vif->bss_conf.bssid,
1090                        rssi_event == RSSI_EVENT_HIGH ? "HIGH" : "LOW");
1091
1092         /*
1093          * Check if rssi is good enough for reduced Tx power, but not in loose
1094          * scheme.
1095          */
1096         if (rssi_event == RSSI_EVENT_LOW || mvm->cfg->bt_shared_single_ant ||
1097             iwl_get_coex_type(mvm, vif) == BT_COEX_LOOSE_LUT)
1098                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id,
1099                                                   false);
1100         else
1101                 ret = iwl_mvm_bt_coex_reduced_txp(mvm, mvmvif->ap_sta_id, true);
1102
1103         if (ret)
1104                 IWL_ERR(mvm, "couldn't send BT_CONFIG HCMD upon RSSI event\n");
1105
1106         ieee80211_iterate_active_interfaces_atomic(
1107                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1108                 iwl_mvm_bt_rssi_iterator, &data);
1109
1110         if (iwl_mvm_bt_udpate_ctrl_kill_msk(mvm))
1111                 IWL_ERR(mvm, "Failed to update the ctrl_kill_msk\n");
1112 }
1113
1114 #define LINK_QUAL_AGG_TIME_LIMIT_DEF    (4000)
1115 #define LINK_QUAL_AGG_TIME_LIMIT_BT_ACT (1200)
1116
1117 u16 iwl_mvm_coex_agg_time_limit_old(struct iwl_mvm *mvm,
1118                                     struct ieee80211_sta *sta)
1119 {
1120         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1121         enum iwl_bt_coex_lut_type lut_type;
1122
1123         if (le32_to_cpu(mvm->last_bt_notif_old.bt_activity_grading) <
1124             BT_HIGH_TRAFFIC)
1125                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
1126
1127         if (mvm->last_bt_notif_old.ttc_enabled)
1128                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
1129
1130         lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
1131
1132         if (lut_type == BT_COEX_LOOSE_LUT || lut_type == BT_COEX_INVALID_LUT)
1133                 return LINK_QUAL_AGG_TIME_LIMIT_DEF;
1134
1135         /* tight coex, high bt traffic, reduce AGG time limit */
1136         return LINK_QUAL_AGG_TIME_LIMIT_BT_ACT;
1137 }
1138
1139 bool iwl_mvm_bt_coex_is_mimo_allowed_old(struct iwl_mvm *mvm,
1140                                          struct ieee80211_sta *sta)
1141 {
1142         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1143         enum iwl_bt_coex_lut_type lut_type;
1144
1145         if (mvm->last_bt_notif_old.ttc_enabled)
1146                 return true;
1147
1148         if (le32_to_cpu(mvm->last_bt_notif_old.bt_activity_grading) <
1149             BT_HIGH_TRAFFIC)
1150                 return true;
1151
1152         /*
1153          * In Tight / TxTxDis, BT can't Rx while we Tx, so use both antennas
1154          * since BT is already killed.
1155          * In Loose, BT can Rx while we Tx, so forbid MIMO to let BT Rx while
1156          * we Tx.
1157          * When we are in 5GHz, we'll get BT_COEX_INVALID_LUT allowing MIMO.
1158          */
1159         lut_type = iwl_get_coex_type(mvm, mvmsta->vif);
1160         return lut_type != BT_COEX_LOOSE_LUT;
1161 }
1162
1163 bool iwl_mvm_bt_coex_is_shared_ant_avail_old(struct iwl_mvm *mvm)
1164 {
1165         u32 ag = le32_to_cpu(mvm->last_bt_notif_old.bt_activity_grading);
1166         return ag == BT_OFF;
1167 }
1168
1169 bool iwl_mvm_bt_coex_is_tpc_allowed_old(struct iwl_mvm *mvm,
1170                                         enum ieee80211_band band)
1171 {
1172         u32 bt_activity =
1173                 le32_to_cpu(mvm->last_bt_notif_old.bt_activity_grading);
1174
1175         if (band != IEEE80211_BAND_2GHZ)
1176                 return false;
1177
1178         return bt_activity >= BT_LOW_TRAFFIC;
1179 }
1180
1181 void iwl_mvm_bt_coex_vif_change_old(struct iwl_mvm *mvm)
1182 {
1183         iwl_mvm_bt_coex_notif_handle(mvm);
1184 }
1185
1186 int iwl_mvm_rx_ant_coupling_notif_old(struct iwl_mvm *mvm,
1187                                       struct iwl_rx_cmd_buffer *rxb,
1188                                       struct iwl_device_cmd *dev_cmd)
1189 {
1190         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1191         u32 ant_isolation = le32_to_cpup((void *)pkt->data);
1192         u8 __maybe_unused lower_bound, upper_bound;
1193         int ret;
1194         u8 lut;
1195
1196         struct iwl_bt_coex_cmd_old *bt_cmd;
1197         struct iwl_host_cmd cmd = {
1198                 .id = BT_CONFIG,
1199                 .len = { sizeof(*bt_cmd), },
1200                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1201         };
1202
1203         if (!IWL_MVM_BT_COEX_CORUNNING)
1204                 return 0;
1205
1206         lockdep_assert_held(&mvm->mutex);
1207
1208         /* Ignore updates if we are in force mode */
1209         if (unlikely(mvm->bt_force_ant_mode != BT_FORCE_ANT_DIS))
1210                 return 0;
1211
1212         if (ant_isolation ==  mvm->last_ant_isol)
1213                 return 0;
1214
1215         for (lut = 0; lut < ARRAY_SIZE(antenna_coupling_ranges) - 1; lut++)
1216                 if (ant_isolation < antenna_coupling_ranges[lut + 1].range)
1217                         break;
1218
1219         lower_bound = antenna_coupling_ranges[lut].range;
1220
1221         if (lut < ARRAY_SIZE(antenna_coupling_ranges) - 1)
1222                 upper_bound = antenna_coupling_ranges[lut + 1].range;
1223         else
1224                 upper_bound = antenna_coupling_ranges[lut].range;
1225
1226         IWL_DEBUG_COEX(mvm, "Antenna isolation=%d in range [%d,%d[, lut=%d\n",
1227                        ant_isolation, lower_bound, upper_bound, lut);
1228
1229         mvm->last_ant_isol = ant_isolation;
1230
1231         if (mvm->last_corun_lut == lut)
1232                 return 0;
1233
1234         mvm->last_corun_lut = lut;
1235
1236         bt_cmd = kzalloc(sizeof(*bt_cmd), GFP_KERNEL);
1237         if (!bt_cmd)
1238                 return 0;
1239         cmd.data[0] = bt_cmd;
1240
1241         bt_cmd->flags = cpu_to_le32(BT_COEX_NW_OLD);
1242         bt_cmd->valid_bit_msk |= cpu_to_le32(BT_VALID_ENABLE |
1243                                              BT_VALID_CORUN_LUT_20 |
1244                                              BT_VALID_CORUN_LUT_40);
1245
1246         /* For the moment, use the same LUT for 20GHz and 40GHz */
1247         memcpy(bt_cmd->bt4_corun_lut20, antenna_coupling_ranges[lut].lut20,
1248                sizeof(bt_cmd->bt4_corun_lut20));
1249
1250         memcpy(bt_cmd->bt4_corun_lut40, antenna_coupling_ranges[lut].lut20,
1251                sizeof(bt_cmd->bt4_corun_lut40));
1252
1253         ret = iwl_mvm_send_cmd(mvm, &cmd);
1254
1255         kfree(bt_cmd);
1256         return ret;
1257 }