ath11k: fix FCS_ERR flag in radio tap header
[linux-2.6-block.git] / drivers / net / wireless / ath / ath11k / mac.c
CommitLineData
d5c65159
KV
1// SPDX-License-Identifier: BSD-3-Clause-Clear
2/*
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 */
5
6#include <net/mac80211.h>
7#include <linux/etherdevice.h>
8#include "mac.h"
9#include "core.h"
10#include "debug.h"
11#include "wmi.h"
12#include "hw.h"
13#include "dp_tx.h"
14#include "dp_rx.h"
15#include "testmode.h"
16#include "peer.h"
568f0603 17#include "debugfs_sta.h"
d5c65159
KV
18
19#define CHAN2G(_channel, _freq, _flags) { \
20 .band = NL80211_BAND_2GHZ, \
21 .hw_value = (_channel), \
22 .center_freq = (_freq), \
23 .flags = (_flags), \
24 .max_antenna_gain = 0, \
25 .max_power = 30, \
26}
27
28#define CHAN5G(_channel, _freq, _flags) { \
29 .band = NL80211_BAND_5GHZ, \
30 .hw_value = (_channel), \
31 .center_freq = (_freq), \
32 .flags = (_flags), \
33 .max_antenna_gain = 0, \
34 .max_power = 30, \
35}
36
c5625aba
PKC
37#define CHAN6G(_channel, _freq, _flags) { \
38 .band = NL80211_BAND_6GHZ, \
39 .hw_value = (_channel), \
40 .center_freq = (_freq), \
41 .flags = (_flags), \
42 .max_antenna_gain = 0, \
43 .max_power = 30, \
44}
45
d5c65159
KV
46static const struct ieee80211_channel ath11k_2ghz_channels[] = {
47 CHAN2G(1, 2412, 0),
48 CHAN2G(2, 2417, 0),
49 CHAN2G(3, 2422, 0),
50 CHAN2G(4, 2427, 0),
51 CHAN2G(5, 2432, 0),
52 CHAN2G(6, 2437, 0),
53 CHAN2G(7, 2442, 0),
54 CHAN2G(8, 2447, 0),
55 CHAN2G(9, 2452, 0),
56 CHAN2G(10, 2457, 0),
57 CHAN2G(11, 2462, 0),
58 CHAN2G(12, 2467, 0),
59 CHAN2G(13, 2472, 0),
60 CHAN2G(14, 2484, 0),
61};
62
63static const struct ieee80211_channel ath11k_5ghz_channels[] = {
64 CHAN5G(36, 5180, 0),
65 CHAN5G(40, 5200, 0),
66 CHAN5G(44, 5220, 0),
67 CHAN5G(48, 5240, 0),
68 CHAN5G(52, 5260, 0),
69 CHAN5G(56, 5280, 0),
70 CHAN5G(60, 5300, 0),
71 CHAN5G(64, 5320, 0),
72 CHAN5G(100, 5500, 0),
73 CHAN5G(104, 5520, 0),
74 CHAN5G(108, 5540, 0),
75 CHAN5G(112, 5560, 0),
76 CHAN5G(116, 5580, 0),
77 CHAN5G(120, 5600, 0),
78 CHAN5G(124, 5620, 0),
79 CHAN5G(128, 5640, 0),
80 CHAN5G(132, 5660, 0),
81 CHAN5G(136, 5680, 0),
82 CHAN5G(140, 5700, 0),
83 CHAN5G(144, 5720, 0),
84 CHAN5G(149, 5745, 0),
85 CHAN5G(153, 5765, 0),
86 CHAN5G(157, 5785, 0),
87 CHAN5G(161, 5805, 0),
88 CHAN5G(165, 5825, 0),
89 CHAN5G(169, 5845, 0),
90 CHAN5G(173, 5865, 0),
91};
92
c5625aba
PKC
93static const struct ieee80211_channel ath11k_6ghz_channels[] = {
94 CHAN6G(1, 5955, 0),
95 CHAN6G(5, 5975, 0),
96 CHAN6G(9, 5995, 0),
97 CHAN6G(13, 6015, 0),
98 CHAN6G(17, 6035, 0),
99 CHAN6G(21, 6055, 0),
100 CHAN6G(25, 6075, 0),
101 CHAN6G(29, 6095, 0),
102 CHAN6G(33, 6115, 0),
103 CHAN6G(37, 6135, 0),
104 CHAN6G(41, 6155, 0),
105 CHAN6G(45, 6175, 0),
106 CHAN6G(49, 6195, 0),
107 CHAN6G(53, 6215, 0),
108 CHAN6G(57, 6235, 0),
109 CHAN6G(61, 6255, 0),
110 CHAN6G(65, 6275, 0),
111 CHAN6G(69, 6295, 0),
112 CHAN6G(73, 6315, 0),
113 CHAN6G(77, 6335, 0),
114 CHAN6G(81, 6355, 0),
115 CHAN6G(85, 6375, 0),
116 CHAN6G(89, 6395, 0),
117 CHAN6G(93, 6415, 0),
118 CHAN6G(97, 6435, 0),
119 CHAN6G(101, 6455, 0),
120 CHAN6G(105, 6475, 0),
121 CHAN6G(109, 6495, 0),
122 CHAN6G(113, 6515, 0),
123 CHAN6G(117, 6535, 0),
124 CHAN6G(121, 6555, 0),
125 CHAN6G(125, 6575, 0),
126 CHAN6G(129, 6595, 0),
127 CHAN6G(133, 6615, 0),
128 CHAN6G(137, 6635, 0),
129 CHAN6G(141, 6655, 0),
130 CHAN6G(145, 6675, 0),
131 CHAN6G(149, 6695, 0),
132 CHAN6G(153, 6715, 0),
133 CHAN6G(157, 6735, 0),
134 CHAN6G(161, 6755, 0),
135 CHAN6G(165, 6775, 0),
136 CHAN6G(169, 6795, 0),
137 CHAN6G(173, 6815, 0),
138 CHAN6G(177, 6835, 0),
139 CHAN6G(181, 6855, 0),
140 CHAN6G(185, 6875, 0),
141 CHAN6G(189, 6895, 0),
142 CHAN6G(193, 6915, 0),
143 CHAN6G(197, 6935, 0),
144 CHAN6G(201, 6955, 0),
145 CHAN6G(205, 6975, 0),
146 CHAN6G(209, 6995, 0),
147 CHAN6G(213, 7015, 0),
148 CHAN6G(217, 7035, 0),
149 CHAN6G(221, 7055, 0),
150 CHAN6G(225, 7075, 0),
151 CHAN6G(229, 7095, 0),
152 CHAN6G(233, 7115, 0),
4a9550f5
PKC
153
154 /* new addition in IEEE Std 802.11ax-2021 */
155 CHAN6G(2, 5935, 0),
c5625aba
PKC
156};
157
d5c65159
KV
158static struct ieee80211_rate ath11k_legacy_rates[] = {
159 { .bitrate = 10,
160 .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
161 { .bitrate = 20,
162 .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
163 .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
164 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
165 { .bitrate = 55,
166 .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
167 .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
168 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
169 { .bitrate = 110,
170 .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
171 .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
172 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
173
174 { .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
175 { .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
176 { .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
177 { .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
178 { .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
179 { .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
180 { .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
181 { .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
182};
183
184static const int
185ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
186 [NL80211_BAND_2GHZ] = {
187 [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
188 [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
189 [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
190 [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
191 [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
192 [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
193 [NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
194 [NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
195 },
196 [NL80211_BAND_5GHZ] = {
197 [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
198 [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
199 [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
200 [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
201 [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
202 [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
203 [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
204 [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
205 },
22eeadcd
PKC
206 [NL80211_BAND_6GHZ] = {
207 [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
208 [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
209 [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
210 [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
211 [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
212 [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
213 [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
214 [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
215 },
216
d5c65159
KV
217};
218
219const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
220 .rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
221 HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
222 HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
223 .pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
224 .pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
225 .pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
226 .pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
227 HTT_RX_FP_CTRL_FILTER_FLASG3
228};
229
230#define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
231#define ath11k_g_rates ath11k_legacy_rates
232#define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
233#define ath11k_a_rates (ath11k_legacy_rates + 4)
234#define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
235
236#define ATH11K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
237
238static const u32 ath11k_smps_map[] = {
239 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
240 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
241 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
242 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
243};
244
e7495035
CH
245static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
246 struct ieee80211_vif *vif);
247
39e81c6a
T
248u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
249{
250 u8 ret = 0;
251
252 switch (bw) {
253 case ATH11K_BW_20:
254 ret = RATE_INFO_BW_20;
255 break;
256 case ATH11K_BW_40:
257 ret = RATE_INFO_BW_40;
258 break;
259 case ATH11K_BW_80:
260 ret = RATE_INFO_BW_80;
261 break;
262 case ATH11K_BW_160:
263 ret = RATE_INFO_BW_160;
264 break;
265 }
266
267 return ret;
268}
269
92bacd1c
VN
270enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
271{
272 switch (bw) {
273 case RATE_INFO_BW_20:
274 return ATH11K_BW_20;
275 case RATE_INFO_BW_40:
276 return ATH11K_BW_40;
277 case RATE_INFO_BW_80:
278 return ATH11K_BW_80;
279 case RATE_INFO_BW_160:
280 return ATH11K_BW_160;
281 default:
282 return ATH11K_BW_20;
283 }
284}
285
d5c65159
KV
286int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
287 u16 *rate)
288{
289 /* As default, it is OFDM rates */
290 int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
291 int max_rates_idx = ath11k_g_rates_size;
292
293 if (preamble == WMI_RATE_PREAMBLE_CCK) {
294 hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
295 i = 0;
296 max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
297 }
298
299 while (i < max_rates_idx) {
300 if (hw_rc == ath11k_legacy_rates[i].hw_value) {
301 *rateidx = i;
302 *rate = ath11k_legacy_rates[i].bitrate;
303 return 0;
304 }
305 i++;
306 }
307
308 return -EINVAL;
309}
310
311static int get_num_chains(u32 mask)
312{
313 int num_chains = 0;
314
315 while (mask) {
316 if (mask & BIT(0))
317 num_chains++;
318 mask >>= 1;
319 }
320
321 return num_chains;
322}
323
324u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
325 u32 bitrate)
326{
327 int i;
328
329 for (i = 0; i < sband->n_bitrates; i++)
330 if (sband->bitrates[i].bitrate == bitrate)
331 return i;
332
333 return 0;
334}
335
336static u32
337ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
338{
339 int nss;
340
341 for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
342 if (ht_mcs_mask[nss])
343 return nss + 1;
344
345 return 1;
346}
347
348static u32
349ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
350{
351 int nss;
352
353 for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
354 if (vht_mcs_mask[nss])
355 return nss + 1;
356
357 return 1;
358}
359
61fe43e7
MH
360static u32
361ath11k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
362{
363 int nss;
364
365 for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
366 if (he_mcs_mask[nss])
367 return nss + 1;
368
369 return 1;
370}
371
d5c65159
KV
372static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
373{
374/* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
375 * 0 for no restriction
376 * 1 for 1/4 us
377 * 2 for 1/2 us
378 * 3 for 1 us
379 * 4 for 2 us
380 * 5 for 4 us
381 * 6 for 8 us
382 * 7 for 16 us
383 */
384 switch (mpdudensity) {
385 case 0:
386 return 0;
387 case 1:
388 case 2:
389 case 3:
390 /* Our lower layer calculations limit our precision to
391 * 1 microsecond
392 */
393 return 1;
394 case 4:
395 return 2;
396 case 5:
397 return 4;
398 case 6:
399 return 8;
400 case 7:
401 return 16;
402 default:
403 return 0;
404 }
405}
406
407static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
408 struct cfg80211_chan_def *def)
409{
410 struct ieee80211_chanctx_conf *conf;
411
412 rcu_read_lock();
413 conf = rcu_dereference(vif->chanctx_conf);
414 if (!conf) {
415 rcu_read_unlock();
416 return -ENOENT;
417 }
418
419 *def = conf->def;
420 rcu_read_unlock();
421
422 return 0;
423}
424
425static bool ath11k_mac_bitrate_is_cck(int bitrate)
426{
427 switch (bitrate) {
428 case 10:
429 case 20:
430 case 55:
431 case 110:
432 return true;
433 }
434
435 return false;
436}
437
438u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
439 u8 hw_rate, bool cck)
440{
441 const struct ieee80211_rate *rate;
442 int i;
443
444 for (i = 0; i < sband->n_bitrates; i++) {
445 rate = &sband->bitrates[i];
446
447 if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
448 continue;
449
450 if (rate->hw_value == hw_rate)
451 return i;
452 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
453 rate->hw_value_short == hw_rate)
454 return i;
455 }
456
457 return 0;
458}
459
460static u8 ath11k_mac_bitrate_to_rate(int bitrate)
461{
462 return DIV_ROUND_UP(bitrate, 5) |
463 (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
464}
465
466static void ath11k_get_arvif_iter(void *data, u8 *mac,
467 struct ieee80211_vif *vif)
468{
469 struct ath11k_vif_iter *arvif_iter = data;
470 struct ath11k_vif *arvif = (void *)vif->drv_priv;
471
472 if (arvif->vdev_id == arvif_iter->vdev_id)
473 arvif_iter->arvif = arvif;
474}
475
476struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
477{
478 struct ath11k_vif_iter arvif_iter;
479 u32 flags;
480
481 memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
482 arvif_iter.vdev_id = vdev_id;
483
484 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
485 ieee80211_iterate_active_interfaces_atomic(ar->hw,
486 flags,
487 ath11k_get_arvif_iter,
488 &arvif_iter);
79c080db
KP
489 if (!arvif_iter.arvif) {
490 ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
d5c65159 491 return NULL;
79c080db 492 }
d5c65159
KV
493
494 return arvif_iter.arvif;
495}
496
497struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
498 u32 vdev_id)
499{
500 int i;
501 struct ath11k_pdev *pdev;
502 struct ath11k_vif *arvif;
503
504 for (i = 0; i < ab->num_radios; i++) {
505 pdev = rcu_dereference(ab->pdevs_active[i]);
79feedfe
KP
506 if (pdev && pdev->ar &&
507 (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
d5c65159
KV
508 arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
509 if (arvif)
510 return arvif;
511 }
512 }
513
514 return NULL;
515}
516
517struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
518{
519 int i;
520 struct ath11k_pdev *pdev;
d5c65159
KV
521
522 for (i = 0; i < ab->num_radios; i++) {
523 pdev = rcu_dereference(ab->pdevs_active[i]);
524 if (pdev && pdev->ar) {
79c080db
KP
525 if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
526 return pdev->ar;
d5c65159
KV
527 }
528 }
529
530 return NULL;
531}
532
533struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
534{
535 int i;
536 struct ath11k_pdev *pdev;
537
5f859bc0
CH
538 if (ab->hw_params.single_pdev_only) {
539 pdev = rcu_dereference(ab->pdevs_active[0]);
540 return pdev ? pdev->ar : NULL;
541 }
542
d5c65159
KV
543 if (WARN_ON(pdev_id > ab->num_radios))
544 return NULL;
545
546 for (i = 0; i < ab->num_radios; i++) {
547 pdev = rcu_dereference(ab->pdevs_active[i]);
548
549 if (pdev && pdev->pdev_id == pdev_id)
550 return (pdev->ar ? pdev->ar : NULL);
551 }
552
553 return NULL;
554}
555
d5c65159
KV
556static void ath11k_pdev_caps_update(struct ath11k *ar)
557{
558 struct ath11k_base *ab = ar->ab;
559
560 ar->max_tx_power = ab->target_caps.hw_max_tx_power;
561
562 /* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
563 * But since the received value in svcrdy is same as hw_max_tx_power,
564 * we can set ar->min_tx_power to 0 currently until
565 * this is fixed in firmware
566 */
567 ar->min_tx_power = 0;
568
569 ar->txpower_limit_2g = ar->max_tx_power;
570 ar->txpower_limit_5g = ar->max_tx_power;
571 ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
572}
573
574static int ath11k_mac_txpower_recalc(struct ath11k *ar)
575{
576 struct ath11k_pdev *pdev = ar->pdev;
577 struct ath11k_vif *arvif;
578 int ret, txpower = -1;
579 u32 param;
580
581 lockdep_assert_held(&ar->conf_mutex);
582
583 list_for_each_entry(arvif, &ar->arvifs, list) {
584 if (arvif->txpower <= 0)
585 continue;
586
587 if (txpower == -1)
588 txpower = arvif->txpower;
589 else
590 txpower = min(txpower, arvif->txpower);
591 }
592
593 if (txpower == -1)
594 return 0;
595
596 /* txpwr is set as 2 units per dBm in FW*/
597 txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
598 ar->max_tx_power) * 2;
599
600 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
601 txpower / 2);
602
603 if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
604 ar->txpower_limit_2g != txpower) {
605 param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
606 ret = ath11k_wmi_pdev_set_param(ar, param,
607 txpower, ar->pdev->pdev_id);
608 if (ret)
609 goto fail;
610 ar->txpower_limit_2g = txpower;
611 }
612
613 if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
614 ar->txpower_limit_5g != txpower) {
615 param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
616 ret = ath11k_wmi_pdev_set_param(ar, param,
617 txpower, ar->pdev->pdev_id);
618 if (ret)
619 goto fail;
620 ar->txpower_limit_5g = txpower;
621 }
622
623 return 0;
624
625fail:
626 ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
627 txpower / 2, param, ret);
628 return ret;
629}
630
631static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
632{
633 struct ath11k *ar = arvif->ar;
634 u32 vdev_param, rts_cts = 0;
635 int ret;
636
637 lockdep_assert_held(&ar->conf_mutex);
638
639 vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
640
641 /* Enable RTS/CTS protection for sw retries (when legacy stations
642 * are in BSS) or by default only for second rate series.
643 * TODO: Check if we need to enable CTS 2 Self in any case
644 */
645 rts_cts = WMI_USE_RTS_CTS;
646
647 if (arvif->num_legacy_stations > 0)
648 rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
649 else
650 rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
651
652 /* Need not send duplicate param value to firmware */
653 if (arvif->rtscts_prot_mode == rts_cts)
654 return 0;
655
656 arvif->rtscts_prot_mode = rts_cts;
657
658 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
659 arvif->vdev_id, rts_cts);
660
661 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
662 vdev_param, rts_cts);
663 if (ret)
664 ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
665 arvif->vdev_id, ret);
666
667 return ret;
668}
669
670static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
671{
672 struct ath11k *ar = arvif->ar;
673 u32 param;
674 int ret;
675
676 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
677 ATH11K_KICKOUT_THRESHOLD,
678 ar->pdev->pdev_id);
679 if (ret) {
680 ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
681 arvif->vdev_id, ret);
682 return ret;
683 }
684
685 param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
686 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
687 ATH11K_KEEPALIVE_MIN_IDLE);
688 if (ret) {
689 ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
690 arvif->vdev_id, ret);
691 return ret;
692 }
693
694 param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
695 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
696 ATH11K_KEEPALIVE_MAX_IDLE);
697 if (ret) {
698 ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
699 arvif->vdev_id, ret);
700 return ret;
701 }
702
703 param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
704 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
705 ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
706 if (ret) {
707 ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
708 arvif->vdev_id, ret);
709 return ret;
710 }
711
712 return 0;
713}
714
715void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
716{
717 struct ath11k_peer *peer, *tmp;
718 struct ath11k_base *ab = ar->ab;
719
720 lockdep_assert_held(&ar->conf_mutex);
721
722 spin_lock_bh(&ab->base_lock);
723 list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
724 ath11k_peer_rx_tid_cleanup(ar, peer);
725 list_del(&peer->list);
726 kfree(peer);
727 }
728 spin_unlock_bh(&ab->base_lock);
729
730 ar->num_peers = 0;
731 ar->num_stations = 0;
732}
733
d37b4862
SM
734static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
735{
736 lockdep_assert_held(&ar->conf_mutex);
737
738 if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
739 return -ESHUTDOWN;
740
741 if (!wait_for_completion_timeout(&ar->vdev_setup_done,
742 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
743 return -ETIMEDOUT;
744
745 return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
746}
747
64e06b78
SM
748static void
749ath11k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
750 struct ieee80211_chanctx_conf *conf,
751 void *data)
d5c65159 752{
64e06b78
SM
753 struct cfg80211_chan_def **def = data;
754
755 *def = &conf->def;
756}
757
758static int ath11k_mac_monitor_vdev_start(struct ath11k *ar, int vdev_id,
759 struct cfg80211_chan_def *chandef)
760{
761 struct ieee80211_channel *channel;
762 struct wmi_vdev_start_req_arg arg = {};
763 int ret;
764
765 lockdep_assert_held(&ar->conf_mutex);
766
767 channel = chandef->chan;
768
769 arg.vdev_id = vdev_id;
770 arg.channel.freq = channel->center_freq;
771 arg.channel.band_center_freq1 = chandef->center_freq1;
772 arg.channel.band_center_freq2 = chandef->center_freq2;
773
774 arg.channel.mode = ath11k_phymodes[chandef->chan->band][chandef->width];
775 arg.channel.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
776
777 arg.channel.min_power = 0;
778 arg.channel.max_power = channel->max_power * 2;
779 arg.channel.max_reg_power = channel->max_reg_power * 2;
780 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
781
782 arg.pref_tx_streams = ar->num_tx_chains;
783 arg.pref_rx_streams = ar->num_rx_chains;
784
785 arg.channel.passive = !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
786
787 reinit_completion(&ar->vdev_setup_done);
788 reinit_completion(&ar->vdev_delete_done);
789
790 ret = ath11k_wmi_vdev_start(ar, &arg, false);
791 if (ret) {
792 ath11k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
793 vdev_id, ret);
794 return ret;
795 }
796
797 ret = ath11k_mac_vdev_setup_sync(ar);
798 if (ret) {
799 ath11k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
800 vdev_id, ret);
801 return ret;
802 }
803
804 ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
805 if (ret) {
806 ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
807 vdev_id, ret);
808 goto vdev_stop;
809 }
810
811 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
812 vdev_id);
813
814 return 0;
815
816vdev_stop:
817 reinit_completion(&ar->vdev_setup_done);
818
819 ret = ath11k_wmi_vdev_stop(ar, vdev_id);
820 if (ret) {
821 ath11k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
822 vdev_id, ret);
823 return ret;
824 }
825
826 ret = ath11k_mac_vdev_setup_sync(ar);
827 if (ret) {
828 ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i stop: %d\n",
829 vdev_id, ret);
830 return ret;
831 }
832
833 return -EIO;
834}
835
836static int ath11k_mac_monitor_vdev_stop(struct ath11k *ar)
837{
838 int ret;
839
840 lockdep_assert_held(&ar->conf_mutex);
841
842 reinit_completion(&ar->vdev_setup_done);
843
844 ret = ath11k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
845 if (ret) {
846 ath11k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
847 ar->monitor_vdev_id, ret);
848 return ret;
849 }
850
851 ret = ath11k_mac_vdev_setup_sync(ar);
852 if (ret) {
853 ath11k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
854 ar->monitor_vdev_id, ret);
855 return ret;
856 }
857
858 ret = ath11k_wmi_vdev_down(ar, ar->monitor_vdev_id);
859 if (ret) {
860 ath11k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
861 ar->monitor_vdev_id, ret);
862 return ret;
863 }
864
865 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i stopped\n",
866 ar->monitor_vdev_id);
d5c65159 867
7fd85517 868 return 0;
d5c65159
KV
869}
870
64e06b78
SM
871static int ath11k_mac_monitor_vdev_create(struct ath11k *ar)
872{
873 struct ath11k_pdev *pdev = ar->pdev;
874 struct vdev_create_params param = {};
875 int bit, ret;
876 u8 tmp_addr[6] = {0};
877 u16 nss;
878
879 lockdep_assert_held(&ar->conf_mutex);
880
881 if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
882 return 0;
883
884 if (ar->ab->free_vdev_map == 0) {
885 ath11k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
886 return -ENOMEM;
887 }
888
889 bit = __ffs64(ar->ab->free_vdev_map);
890
891 ar->monitor_vdev_id = bit;
892
893 param.if_id = ar->monitor_vdev_id;
894 param.type = WMI_VDEV_TYPE_MONITOR;
895 param.subtype = WMI_VDEV_SUBTYPE_NONE;
896 param.pdev_id = pdev->pdev_id;
897
898 if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
899 param.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
900 param.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
901 }
902 if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
903 param.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
904 param.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
905 }
906
907 ret = ath11k_wmi_vdev_create(ar, tmp_addr, &param);
908 if (ret) {
909 ath11k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
910 ar->monitor_vdev_id, ret);
911 ar->monitor_vdev_id = -1;
912 return ret;
913 }
914
915 nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
916 ret = ath11k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
917 WMI_VDEV_PARAM_NSS, nss);
918 if (ret) {
919 ath11k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
920 ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
921 goto err_vdev_del;
922 }
923
924 ret = ath11k_mac_txpower_recalc(ar);
925 if (ret) {
926 ath11k_warn(ar->ab, "failed to recalc txpower for monitor vdev %d: %d\n",
927 ar->monitor_vdev_id, ret);
928 goto err_vdev_del;
929 }
930
931 ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
932 ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
933 ar->num_created_vdevs++;
934 set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
935
936 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d created\n",
937 ar->monitor_vdev_id);
938
939 return 0;
940
941err_vdev_del:
942 ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
943 ar->monitor_vdev_id = -1;
944 return ret;
945}
946
947static int ath11k_mac_monitor_vdev_delete(struct ath11k *ar)
948{
949 int ret;
950 unsigned long time_left;
951
952 lockdep_assert_held(&ar->conf_mutex);
953
954 if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
955 return 0;
956
957 reinit_completion(&ar->vdev_delete_done);
958
959 ret = ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
960 if (ret) {
961 ath11k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
962 ar->monitor_vdev_id, ret);
963 return ret;
964 }
965
966 time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
967 ATH11K_VDEV_DELETE_TIMEOUT_HZ);
968 if (time_left == 0) {
969 ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
970 } else {
971 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d deleted\n",
972 ar->monitor_vdev_id);
973
974 ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
975 ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
976 ar->num_created_vdevs--;
977 ar->monitor_vdev_id = -1;
978 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
979 }
980
981 return ret;
982}
983
984static int ath11k_mac_monitor_start(struct ath11k *ar)
985{
986 struct cfg80211_chan_def *chandef = NULL;
987 int ret;
988
989 lockdep_assert_held(&ar->conf_mutex);
990
991 if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
992 return 0;
993
994 ieee80211_iter_chan_contexts_atomic(ar->hw,
995 ath11k_mac_get_any_chandef_iter,
996 &chandef);
997 if (!chandef)
998 return 0;
999
1000 ret = ath11k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1001 if (ret) {
1002 ath11k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1003 ath11k_mac_monitor_vdev_delete(ar);
1004 return ret;
1005 }
1006
1007 set_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1008
1009 ar->num_started_vdevs++;
1010 ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1011 if (ret) {
1012 ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during start: %d",
1013 ret);
1014 return ret;
1015 }
1016
1017 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor started\n");
1018
1019 return 0;
1020}
1021
1022static int ath11k_mac_monitor_stop(struct ath11k *ar)
1023{
1024 int ret;
1025
1026 lockdep_assert_held(&ar->conf_mutex);
1027
1028 if (!test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1029 return 0;
1030
1031 ret = ath11k_mac_monitor_vdev_stop(ar);
1032 if (ret) {
1033 ath11k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1034 return ret;
1035 }
1036
1037 clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1038 ar->num_started_vdevs--;
1039
1040 ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1041 if (ret) {
1042 ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during stop: %d",
1043 ret);
1044 return ret;
1045 }
1046
1047 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1048
1049 return 0;
1050}
1051
b2beffa7
CH
1052static int ath11k_mac_vif_setup_ps(struct ath11k_vif *arvif)
1053{
1054 struct ath11k *ar = arvif->ar;
1055 struct ieee80211_vif *vif = arvif->vif;
1056 struct ieee80211_conf *conf = &ar->hw->conf;
1057 enum wmi_sta_powersave_param param;
1058 enum wmi_sta_ps_mode psmode;
1059 int ret;
1060 int timeout;
1061 bool enable_ps;
1062
1063 lockdep_assert_held(&arvif->ar->conf_mutex);
1064
1065 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1066 return 0;
1067
1068 enable_ps = arvif->ps;
1069
1070 if (!arvif->is_started) {
1071 /* mac80211 can update vif powersave state while disconnected.
1072 * Firmware doesn't behave nicely and consumes more power than
1073 * necessary if PS is disabled on a non-started vdev. Hence
1074 * force-enable PS for non-running vdevs.
1075 */
1076 psmode = WMI_STA_PS_MODE_ENABLED;
1077 } else if (enable_ps) {
1078 psmode = WMI_STA_PS_MODE_ENABLED;
1079 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1080
1081 timeout = conf->dynamic_ps_timeout;
1082 if (timeout == 0) {
1083 /* firmware doesn't like 0 */
1084 timeout = ieee80211_tu_to_usec(vif->bss_conf.beacon_int) / 1000;
1085 }
1086
1087 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1088 timeout);
1089 if (ret) {
1090 ath11k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
1091 arvif->vdev_id, ret);
1092 return ret;
1093 }
1094 } else {
1095 psmode = WMI_STA_PS_MODE_DISABLED;
1096 }
1097
1098 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d psmode %s\n",
1099 arvif->vdev_id, psmode ? "enable" : "disable");
1100
1101 ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
1102 if (ret) {
1103 ath11k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
1104 psmode, arvif->vdev_id, ret);
1105 return ret;
1106 }
1107
1108 return 0;
1109}
1110
1111static int ath11k_mac_config_ps(struct ath11k *ar)
1112{
1113 struct ath11k_vif *arvif;
1114 int ret = 0;
1115
1116 lockdep_assert_held(&ar->conf_mutex);
1117
1118 list_for_each_entry(arvif, &ar->arvifs, list) {
1119 ret = ath11k_mac_vif_setup_ps(arvif);
1120 if (ret) {
1121 ath11k_warn(ar->ab, "failed to setup powersave: %d\n", ret);
1122 break;
1123 }
1124 }
1125
1126 return ret;
1127}
1128
64e06b78
SM
1129static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1130{
1131 struct ath11k *ar = hw->priv;
1132 struct ieee80211_conf *conf = &hw->conf;
1133 int ret = 0;
1134
1135 mutex_lock(&ar->conf_mutex);
1136
1137 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1138 if (conf->flags & IEEE80211_CONF_MONITOR) {
1139 set_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1140
1141 if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1142 &ar->monitor_flags))
1143 goto out;
1144
1145 ret = ath11k_mac_monitor_vdev_create(ar);
1146 if (ret) {
1147 ath11k_warn(ar->ab, "failed to create monitor vdev: %d",
1148 ret);
1149 goto out;
1150 }
1151
1152 ret = ath11k_mac_monitor_start(ar);
1153 if (ret) {
1154 ath11k_warn(ar->ab, "failed to start monitor: %d",
1155 ret);
1156 goto err_mon_del;
1157 }
1158 } else {
1159 clear_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1160
1161 if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1162 &ar->monitor_flags))
1163 goto out;
1164
1165 ret = ath11k_mac_monitor_stop(ar);
1166 if (ret) {
1167 ath11k_warn(ar->ab, "failed to stop monitor: %d",
1168 ret);
1169 goto out;
1170 }
1171
1172 ret = ath11k_mac_monitor_vdev_delete(ar);
1173 if (ret) {
1174 ath11k_warn(ar->ab, "failed to delete monitor vdev: %d",
1175 ret);
1176 goto out;
1177 }
1178 }
1179 }
1180
1181out:
1182 mutex_unlock(&ar->conf_mutex);
1183 return ret;
1184
1185err_mon_del:
1186 ath11k_mac_monitor_vdev_delete(ar);
1187 mutex_unlock(&ar->conf_mutex);
1188 return ret;
1189}
1190
d5c65159
KV
1191static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
1192{
1193 struct ath11k *ar = arvif->ar;
1194 struct ath11k_base *ab = ar->ab;
1195 struct ieee80211_hw *hw = ar->hw;
1196 struct ieee80211_vif *vif = arvif->vif;
1197 struct ieee80211_mutable_offsets offs = {};
1198 struct sk_buff *bcn;
01e34233
VN
1199 struct ieee80211_mgmt *mgmt;
1200 u8 *ies;
d5c65159
KV
1201 int ret;
1202
1203 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1204 return 0;
1205
1206 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1207 if (!bcn) {
1208 ath11k_warn(ab, "failed to get beacon template from mac80211\n");
1209 return -EPERM;
1210 }
1211
01e34233
VN
1212 ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1213 ies += sizeof(mgmt->u.beacon);
1214
1215 if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1216 arvif->rsnie_present = true;
1217
1218 if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1219 WLAN_OUI_TYPE_MICROSOFT_WPA,
1220 ies, (skb_tail_pointer(bcn) - ies)))
1221 arvif->wpaie_present = true;
1222
d5c65159
KV
1223 ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
1224
1225 kfree_skb(bcn);
1226
1227 if (ret)
1228 ath11k_warn(ab, "failed to submit beacon template command: %d\n",
1229 ret);
1230
1231 return ret;
1232}
1233
1234static void ath11k_control_beaconing(struct ath11k_vif *arvif,
1235 struct ieee80211_bss_conf *info)
1236{
1237 struct ath11k *ar = arvif->ar;
1238 int ret = 0;
1239
1240 lockdep_assert_held(&arvif->ar->conf_mutex);
1241
1242 if (!info->enable_beacon) {
1243 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1244 if (ret)
1245 ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1246 arvif->vdev_id, ret);
1247
1248 arvif->is_up = false;
1249 return;
1250 }
1251
1252 /* Install the beacon template to the FW */
1253 ret = ath11k_mac_setup_bcn_tmpl(arvif);
1254 if (ret) {
1255 ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1256 ret);
1257 return;
1258 }
1259
1260 arvif->tx_seq_no = 0x1000;
1261
1262 arvif->aid = 0;
1263
1264 ether_addr_copy(arvif->bssid, info->bssid);
1265
1266 ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1267 arvif->bssid);
1268 if (ret) {
1269 ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1270 arvif->vdev_id, ret);
1271 return;
1272 }
1273
1274 arvif->is_up = true;
1275
1276 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1277}
1278
26f69792
LS
1279static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
1280 struct ieee80211_vif *vif)
1281{
1282 struct sk_buff *skb = data;
1283 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1284 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1285
1286 if (vif->type != NL80211_IFTYPE_STATION)
1287 return;
1288
1289 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1290 return;
1291
1292 cancel_delayed_work(&arvif->connection_loss_work);
1293}
1294
1295void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
1296{
1297 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1298 IEEE80211_IFACE_ITER_NORMAL,
1299 ath11k_mac_handle_beacon_iter,
1300 skb);
1301}
1302
1303static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1304 struct ieee80211_vif *vif)
1305{
1306 u32 *vdev_id = data;
1307 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1308 struct ath11k *ar = arvif->ar;
1309 struct ieee80211_hw *hw = ar->hw;
1310
1311 if (arvif->vdev_id != *vdev_id)
1312 return;
1313
1314 if (!arvif->is_up)
1315 return;
1316
1317 ieee80211_beacon_loss(vif);
1318
1319 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1320 * (done by mac80211) succeeds but beacons do not resume then it
1321 * doesn't make sense to continue operation. Queue connection loss work
1322 * which can be cancelled when beacon is received.
1323 */
1324 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1325 ATH11K_CONNECTION_LOSS_HZ);
1326}
1327
1328void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
1329{
1330 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1331 IEEE80211_IFACE_ITER_NORMAL,
1332 ath11k_mac_handle_beacon_miss_iter,
1333 &vdev_id);
1334}
1335
1336static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1337{
1338 struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
1339 connection_loss_work.work);
1340 struct ieee80211_vif *vif = arvif->vif;
1341
1342 if (!arvif->is_up)
1343 return;
1344
1345 ieee80211_connection_loss(vif);
1346}
1347
d5c65159
KV
1348static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
1349 struct ieee80211_vif *vif,
1350 struct ieee80211_sta *sta,
1351 struct peer_assoc_params *arg)
1352{
1353 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1354 u32 aid;
1355
1356 lockdep_assert_held(&ar->conf_mutex);
1357
1358 if (vif->type == NL80211_IFTYPE_STATION)
1359 aid = vif->bss_conf.aid;
1360 else
1361 aid = sta->aid;
1362
1363 ether_addr_copy(arg->peer_mac, sta->addr);
1364 arg->vdev_id = arvif->vdev_id;
1365 arg->peer_associd = aid;
1366 arg->auth_flag = true;
1367 /* TODO: STA WAR in ath10k for listen interval required? */
1368 arg->peer_listen_intval = ar->hw->conf.listen_interval;
1369 arg->peer_nss = 1;
1370 arg->peer_caps = vif->bss_conf.assoc_capability;
1371}
1372
1373static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
1374 struct ieee80211_vif *vif,
1375 struct ieee80211_sta *sta,
1376 struct peer_assoc_params *arg)
1377{
1378 struct ieee80211_bss_conf *info = &vif->bss_conf;
1379 struct cfg80211_chan_def def;
1380 struct cfg80211_bss *bss;
01e34233 1381 struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
d5c65159
KV
1382 const u8 *rsnie = NULL;
1383 const u8 *wpaie = NULL;
1384
1385 lockdep_assert_held(&ar->conf_mutex);
1386
1387 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1388 return;
1389
1390 bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1391 IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
01e34233
VN
1392
1393 if (arvif->rsnie_present || arvif->wpaie_present) {
1394 arg->need_ptk_4_way = true;
1395 if (arvif->wpaie_present)
1396 arg->need_gtk_2_way = true;
1397 } else if (bss) {
d5c65159
KV
1398 const struct cfg80211_bss_ies *ies;
1399
1400 rcu_read_lock();
1401 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1402
1403 ies = rcu_dereference(bss->ies);
1404
1405 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1406 WLAN_OUI_TYPE_MICROSOFT_WPA,
1407 ies->data,
1408 ies->len);
1409 rcu_read_unlock();
1410 cfg80211_put_bss(ar->hw->wiphy, bss);
1411 }
1412
1413 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1414 if (rsnie || wpaie) {
1415 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1416 "%s: rsn ie found\n", __func__);
1417 arg->need_ptk_4_way = true;
1418 }
1419
1420 if (wpaie) {
1421 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1422 "%s: wpa ie found\n", __func__);
1423 arg->need_gtk_2_way = true;
1424 }
1425
1426 if (sta->mfp) {
1427 /* TODO: Need to check if FW supports PMF? */
1428 arg->is_pmf_enabled = true;
1429 }
1430
1431 /* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1432}
1433
1434static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
1435 struct ieee80211_vif *vif,
1436 struct ieee80211_sta *sta,
1437 struct peer_assoc_params *arg)
1438{
1439 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1440 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1441 struct cfg80211_chan_def def;
1442 const struct ieee80211_supported_band *sband;
1443 const struct ieee80211_rate *rates;
1444 enum nl80211_band band;
1445 u32 ratemask;
1446 u8 rate;
1447 int i;
1448
1449 lockdep_assert_held(&ar->conf_mutex);
1450
1451 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1452 return;
1453
1454 band = def.chan->band;
1455 sband = ar->hw->wiphy->bands[band];
1456 ratemask = sta->supp_rates[band];
1457 ratemask &= arvif->bitrate_mask.control[band].legacy;
1458 rates = sband->bitrates;
1459
1460 rateset->num_rates = 0;
1461
1462 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1463 if (!(ratemask & 1))
1464 continue;
1465
1466 rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1467 rateset->rates[rateset->num_rates] = rate;
1468 rateset->num_rates++;
1469 }
1470}
1471
1472static bool
1473ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
1474{
1475 int nss;
1476
1477 for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1478 if (ht_mcs_mask[nss])
1479 return false;
1480
1481 return true;
1482}
1483
1484static bool
eb19efed 1485ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[])
d5c65159
KV
1486{
1487 int nss;
1488
1489 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1490 if (vht_mcs_mask[nss])
1491 return false;
1492
1493 return true;
1494}
1495
1496static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1497 struct ieee80211_vif *vif,
1498 struct ieee80211_sta *sta,
1499 struct peer_assoc_params *arg)
1500{
1501 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1502 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1503 struct cfg80211_chan_def def;
1504 enum nl80211_band band;
1505 const u8 *ht_mcs_mask;
1506 int i, n;
1507 u8 max_nss;
1508 u32 stbc;
1509
1510 lockdep_assert_held(&ar->conf_mutex);
1511
1512 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1513 return;
1514
1515 if (!ht_cap->ht_supported)
1516 return;
1517
1518 band = def.chan->band;
1519 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1520
1521 if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1522 return;
1523
1524 arg->ht_flag = true;
1525
1526 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1527 ht_cap->ampdu_factor)) - 1;
1528
1529 arg->peer_mpdu_density =
1530 ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1531
1532 arg->peer_ht_caps = ht_cap->cap;
1533 arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1534
1535 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1536 arg->ldpc_flag = true;
1537
1538 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1539 arg->bw_40 = true;
1540 arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1541 }
1542
61fe43e7
MH
1543 /* As firmware handles this two flags (IEEE80211_HT_CAP_SGI_20
1544 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, we reset
1545 * both flags if guard interval is Default GI
1546 */
1547 if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_DEFAULT_GI)
1548 arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 |
1549 IEEE80211_HT_CAP_SGI_40);
1550
d5c65159
KV
1551 if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1552 if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1553 IEEE80211_HT_CAP_SGI_40))
1554 arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1555 }
1556
1557 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1558 arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1559 arg->stbc_flag = true;
1560 }
1561
1562 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1563 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1564 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1565 stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1566 arg->peer_rate_caps |= stbc;
1567 arg->stbc_flag = true;
1568 }
1569
1570 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1571 arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1572 else if (ht_cap->mcs.rx_mask[1])
1573 arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1574
1575 for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1576 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1577 (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1578 max_nss = (i / 8) + 1;
1579 arg->peer_ht_rates.rates[n++] = i;
1580 }
1581
1582 /* This is a workaround for HT-enabled STAs which break the spec
1583 * and have no HT capabilities RX mask (no HT RX MCS map).
1584 *
1585 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1586 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1587 *
1588 * Firmware asserts if such situation occurs.
1589 */
1590 if (n == 0) {
1591 arg->peer_ht_rates.num_rates = 8;
1592 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1593 arg->peer_ht_rates.rates[i] = i;
1594 } else {
1595 arg->peer_ht_rates.num_rates = n;
1596 arg->peer_nss = min(sta->rx_nss, max_nss);
1597 }
1598
1599 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1600 arg->peer_mac,
1601 arg->peer_ht_rates.num_rates,
1602 arg->peer_nss);
1603}
1604
1605static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1606{
1607 switch ((mcs_map >> (2 * nss)) & 0x3) {
1608 case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1609 case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1610 case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1611 }
1612 return 0;
1613}
1614
1615static u16
1616ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1617 const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1618{
1619 int idx_limit;
1620 int nss;
1621 u16 mcs_map;
1622 u16 mcs;
1623
1624 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1625 mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1626 vht_mcs_limit[nss];
1627
1628 if (mcs_map)
1629 idx_limit = fls(mcs_map) - 1;
1630 else
1631 idx_limit = -1;
1632
1633 switch (idx_limit) {
0b294aeb
GS
1634 case 0:
1635 case 1:
1636 case 2:
1637 case 3:
1638 case 4:
1639 case 5:
1640 case 6:
d5c65159
KV
1641 case 7:
1642 mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1643 break;
1644 case 8:
1645 mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1646 break;
1647 case 9:
1648 mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1649 break;
1650 default:
1651 WARN_ON(1);
0b294aeb 1652 fallthrough;
d5c65159
KV
1653 case -1:
1654 mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1655 break;
1656 }
1657
1658 tx_mcs_set &= ~(0x3 << (nss * 2));
1659 tx_mcs_set |= mcs << (nss * 2);
1660 }
1661
1662 return tx_mcs_set;
1663}
1664
f552d6fd
P
1665static u8 ath11k_get_nss_160mhz(struct ath11k *ar,
1666 u8 max_nss)
1667{
1668 u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
1669 u8 max_sup_nss = 0;
1670
1671 switch (nss_ratio_info) {
1672 case WMI_NSS_RATIO_1BY2_NSS:
1673 max_sup_nss = max_nss >> 1;
1674 break;
1675 case WMI_NSS_RATIO_3BY4_NSS:
1676 ath11k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
1677 break;
1678 case WMI_NSS_RATIO_1_NSS:
1679 max_sup_nss = max_nss;
1680 break;
1681 case WMI_NSS_RATIO_2_NSS:
1682 ath11k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
1683 break;
1684 default:
1685 ath11k_warn(ar->ab, "invalid nss ratio received from firmware: %d\n",
1686 nss_ratio_info);
1687 break;
1688 }
1689
1690 return max_sup_nss;
1691}
1692
d5c65159
KV
1693static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1694 struct ieee80211_vif *vif,
1695 struct ieee80211_sta *sta,
1696 struct peer_assoc_params *arg)
1697{
1698 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1699 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1700 struct cfg80211_chan_def def;
1701 enum nl80211_band band;
61fe43e7 1702 u16 *vht_mcs_mask;
d5c65159
KV
1703 u8 ampdu_factor;
1704 u8 max_nss, vht_mcs;
61fe43e7
MH
1705 int i, vht_nss, nss_idx;
1706 bool user_rate_valid = true;
f552d6fd 1707 u32 rx_nss, tx_nss, nss_160;
d5c65159
KV
1708
1709 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1710 return;
1711
1712 if (!vht_cap->vht_supported)
1713 return;
1714
1715 band = def.chan->band;
1716 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1717
1718 if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1719 return;
1720
1721 arg->vht_flag = true;
1722
1723 /* TODO: similar flags required? */
1724 arg->vht_capable = true;
1725
1726 if (def.chan->band == NL80211_BAND_2GHZ)
1727 arg->vht_ng_flag = true;
1728
1729 arg->peer_vht_caps = vht_cap->cap;
1730
1731 ampdu_factor = (vht_cap->cap &
1732 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1733 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1734
1735 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1736 * zero in VHT IE. Using it would result in degraded throughput.
1737 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1738 * it if VHT max_mpdu is smaller.
1739 */
1740 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1741 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1742 ampdu_factor)) - 1);
1743
1744 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1745 arg->bw_80 = true;
1746
1747 if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1748 arg->bw_160 = true;
1749
61fe43e7
MH
1750 vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
1751
1752 if (vht_nss > sta->rx_nss) {
1753 user_rate_valid = false;
1754 for (nss_idx = sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
1755 if (vht_mcs_mask[nss_idx]) {
1756 user_rate_valid = true;
1757 break;
1758 }
1759 }
1760 }
1761
1762 if (!user_rate_valid) {
1763 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting vht range mcs value to peer supported nss %d for peer %pM\n",
1764 sta->rx_nss, sta->addr);
1765 vht_mcs_mask[sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
1766 }
1767
d5c65159
KV
1768 /* Calculate peer NSS capability from VHT capabilities if STA
1769 * supports VHT.
1770 */
1771 for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1772 vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1773 (2 * i) & 3;
1774
1775 if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1776 vht_mcs_mask[i])
1777 max_nss = i + 1;
1778 }
1779 arg->peer_nss = min(sta->rx_nss, max_nss);
1780 arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1781 arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1782 arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1783 arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1784 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1785
1786 /* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1787 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1788 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1789 */
1790 arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1791 arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1792
40c766d4
RS
1793 if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1794 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1795 arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1796
d5c65159
KV
1797 /* TODO: Check */
1798 arg->tx_max_mcs_nss = 0xFF;
1799
f552d6fd
P
1800 if (arg->peer_phymode == MODE_11AC_VHT160 ||
1801 arg->peer_phymode == MODE_11AC_VHT80_80) {
1802 tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
1803 rx_nss = min(arg->peer_nss, tx_nss);
1804 arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
1805
1806 if (!rx_nss) {
1807 ath11k_warn(ar->ab, "invalid max_nss\n");
1808 return;
1809 }
1810
1811 if (arg->peer_phymode == MODE_11AC_VHT160)
1812 nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
1813 else
1814 nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
d5c65159 1815
f552d6fd
P
1816 arg->peer_bw_rxnss_override |= nss_160;
1817 }
1818
1819 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1820 "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
1821 sta->addr, arg->peer_max_mpdu, arg->peer_flags,
1822 arg->peer_bw_rxnss_override);
d5c65159
KV
1823}
1824
61fe43e7
MH
1825static int ath11k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
1826{
1827 switch ((mcs_map >> (2 * nss)) & 0x3) {
1828 case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
1829 case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
1830 case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
1831 }
1832 return 0;
1833}
1834
1835static u16 ath11k_peer_assoc_h_he_limit(u16 tx_mcs_set,
1836 const u16 he_mcs_limit[NL80211_HE_NSS_MAX])
1837{
1838 int idx_limit;
1839 int nss;
1840 u16 mcs_map;
1841 u16 mcs;
1842
1843 for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
1844 mcs_map = ath11k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
1845 he_mcs_limit[nss];
1846
1847 if (mcs_map)
1848 idx_limit = fls(mcs_map) - 1;
1849 else
1850 idx_limit = -1;
1851
1852 switch (idx_limit) {
1853 case 0 ... 7:
1854 mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
1855 break;
1856 case 8:
1857 case 9:
1858 mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
1859 break;
1860 case 10:
1861 case 11:
1862 mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
1863 break;
1864 default:
1865 WARN_ON(1);
1866 fallthrough;
1867 case -1:
1868 mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
1869 break;
1870 }
1871
1872 tx_mcs_set &= ~(0x3 << (nss * 2));
1873 tx_mcs_set |= mcs << (nss * 2);
1874 }
1875
1876 return tx_mcs_set;
1877}
1878
1879static bool
1880ath11k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
1881{
1882 int nss;
1883
1884 for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
1885 if (he_mcs_mask[nss])
1886 return false;
1887
1888 return true;
1889}
1890
d5c65159
KV
1891static void ath11k_peer_assoc_h_he(struct ath11k *ar,
1892 struct ieee80211_vif *vif,
1893 struct ieee80211_sta *sta,
1894 struct peer_assoc_params *arg)
1895{
61fe43e7
MH
1896 struct ath11k_vif *arvif = (void *)vif->drv_priv;
1897 struct cfg80211_chan_def def;
9f056ed8 1898 const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
af6d39db 1899 u8 ampdu_factor;
61fe43e7
MH
1900 enum nl80211_band band;
1901 u16 *he_mcs_mask;
1902 u8 max_nss, he_mcs;
1903 u16 he_tx_mcs = 0, v = 0;
1904 int i, he_nss, nss_idx;
1905 bool user_rate_valid = true;
f552d6fd 1906 u32 rx_nss, tx_nss, nss_160;
61fe43e7
MH
1907
1908 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1909 return;
9f056ed8
JC
1910
1911 if (!he_cap->has_he)
1912 return;
1913
61fe43e7
MH
1914 band = def.chan->band;
1915 he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
1916
1917 if (ath11k_peer_assoc_h_he_masked(he_mcs_mask))
1918 return;
1919
9f056ed8
JC
1920 arg->he_flag = true;
1921
c8bcd82a
KC
1922 memcpy_and_pad(&arg->peer_he_cap_macinfo,
1923 sizeof(arg->peer_he_cap_macinfo),
1924 he_cap->he_cap_elem.mac_cap_info,
1925 sizeof(he_cap->he_cap_elem.mac_cap_info),
1926 0);
1927 memcpy_and_pad(&arg->peer_he_cap_phyinfo,
1928 sizeof(arg->peer_he_cap_phyinfo),
1929 he_cap->he_cap_elem.phy_cap_info,
1930 sizeof(he_cap->he_cap_elem.phy_cap_info),
1931 0);
60689de4 1932 arg->peer_he_ops = vif->bss_conf.he_oper.params;
9f056ed8
JC
1933
1934 /* the top most byte is used to indicate BSS color info */
1935 arg->peer_he_ops &= 0xffffff;
1936
af6d39db
TC
1937 /* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
1938 * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
1939 * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
1940 *
1941 * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
1942 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1943 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1944 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1945 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1946 * length.
1947 */
1f851b8d
JB
1948 ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
1949 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
af6d39db
TC
1950
1951 if (ampdu_factor) {
1952 if (sta->vht_cap.vht_supported)
1953 arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1954 ampdu_factor)) - 1;
1955 else if (sta->ht_cap.ht_supported)
1956 arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1957 ampdu_factor)) - 1;
1958 }
1959
9f056ed8
JC
1960 if (he_cap->he_cap_elem.phy_cap_info[6] &
1961 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1962 int bit = 7;
1963 int nss, ru;
1964
1965 arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1966 IEEE80211_PPE_THRES_NSS_MASK;
1967 arg->peer_ppet.ru_bit_mask =
1968 (he_cap->ppe_thres[0] &
1969 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1970 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1971
1972 for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1973 for (ru = 0; ru < 4; ru++) {
1974 u32 val = 0;
1975 int i;
1976
1977 if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1978 continue;
1979 for (i = 0; i < 6; i++) {
1980 val >>= 1;
1981 val |= ((he_cap->ppe_thres[bit / 8] >>
1982 (bit % 8)) & 0x1) << 5;
1983 bit++;
1984 }
1985 arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1986 val << (ru * 6);
1987 }
1988 }
1989 }
1990
6d293d44
JC
1991 if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1992 arg->twt_responder = true;
1993 if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1994 arg->twt_requester = true;
1995
61fe43e7
MH
1996 he_nss = ath11k_mac_max_he_nss(he_mcs_mask);
1997
1998 if (he_nss > sta->rx_nss) {
1999 user_rate_valid = false;
2000 for (nss_idx = sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2001 if (he_mcs_mask[nss_idx]) {
2002 user_rate_valid = true;
2003 break;
2004 }
2005 }
2006 }
2007
2008 if (!user_rate_valid) {
2009 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting he range mcs value to peer supported nss %d for peer %pM\n",
2010 sta->rx_nss, sta->addr);
2011 he_mcs_mask[sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2012 }
2013
9f056ed8
JC
2014 switch (sta->bandwidth) {
2015 case IEEE80211_STA_RX_BW_160:
2016 if (he_cap->he_cap_elem.phy_cap_info[0] &
2017 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
2018 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
61fe43e7 2019 v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
9f056ed8
JC
2020 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2021
2022 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
2023 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2024
2025 arg->peer_he_mcs_count++;
61fe43e7 2026 he_tx_mcs = v;
9f056ed8
JC
2027 }
2028 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2029 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2030
2031 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
61fe43e7 2032 v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
9f056ed8
JC
2033 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2034
2035 arg->peer_he_mcs_count++;
61fe43e7
MH
2036 if (!he_tx_mcs)
2037 he_tx_mcs = v;
0b294aeb 2038 fallthrough;
9f056ed8
JC
2039
2040 default:
2041 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2042 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2043
2044 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
61fe43e7 2045 v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
9f056ed8
JC
2046 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2047
2048 arg->peer_he_mcs_count++;
61fe43e7
MH
2049 if (!he_tx_mcs)
2050 he_tx_mcs = v;
9f056ed8
JC
2051 break;
2052 }
61fe43e7
MH
2053
2054 /* Calculate peer NSS capability from HE capabilities if STA
2055 * supports HE.
2056 */
2057 for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2058 he_mcs = he_tx_mcs >> (2 * i) & 3;
2059
2060 /* In case of fixed rates, MCS Range in he_tx_mcs might have
2061 * unsupported range, with he_mcs_mask set, so check either of them
2062 * to find nss.
2063 */
2064 if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2065 he_mcs_mask[i])
2066 max_nss = i + 1;
2067 }
2068 arg->peer_nss = min(sta->rx_nss, max_nss);
2069
f552d6fd
P
2070 if (arg->peer_phymode == MODE_11AX_HE160 ||
2071 arg->peer_phymode == MODE_11AX_HE80_80) {
2072 tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2073 rx_nss = min(arg->peer_nss, tx_nss);
2074 arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2075
2076 if (!rx_nss) {
2077 ath11k_warn(ar->ab, "invalid max_nss\n");
2078 return;
2079 }
2080
2081 if (arg->peer_phymode == MODE_11AX_HE160)
2082 nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2083 else
2084 nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2085
2086 arg->peer_bw_rxnss_override |= nss_160;
2087 }
2088
61fe43e7 2089 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
f552d6fd
P
2090 "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2091 sta->addr, arg->peer_nss,
2092 arg->peer_he_mcs_count,
2093 arg->peer_bw_rxnss_override);
d5c65159
KV
2094}
2095
c3a7d7eb
PKC
2096static void ath11k_peer_assoc_h_he_6ghz(struct ath11k *ar,
2097 struct ieee80211_vif *vif,
2098 struct ieee80211_sta *sta,
2099 struct peer_assoc_params *arg)
2100{
2101 const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
2102 struct cfg80211_chan_def def;
2103 enum nl80211_band band;
2104 u8 ampdu_factor;
2105
2106 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2107 return;
2108
2109 band = def.chan->band;
2110
2111 if (!arg->he_flag || band != NL80211_BAND_6GHZ || !sta->he_6ghz_capa.capa)
2112 return;
2113
2114 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2115 arg->bw_80 = true;
2116
2117 if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
2118 arg->bw_160 = true;
2119
2120 arg->peer_he_caps_6ghz = le16_to_cpu(sta->he_6ghz_capa.capa);
2121 arg->peer_mpdu_density =
2122 ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
2123 arg->peer_he_caps_6ghz));
2124
2125 /* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2126 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2127 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2128 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2129 * Band Capabilities element in the 6 GHz band.
2130 *
2131 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2132 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2133 */
2134 ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
2135 he_cap->he_cap_elem.mac_cap_info[3]) +
2136 FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
2137 arg->peer_he_caps_6ghz);
2138
2139 arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2140 ampdu_factor)) - 1;
2141}
2142
d5c65159
KV
2143static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
2144 struct peer_assoc_params *arg)
2145{
2146 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2147 int smps;
2148
6f4d7030 2149 if (!ht_cap->ht_supported && !sta->he_6ghz_capa.capa)
d5c65159
KV
2150 return;
2151
6f4d7030
PKC
2152 if (ht_cap->ht_supported) {
2153 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2154 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2155 } else {
16bdce2a
KV
2156 smps = le16_get_bits(sta->he_6ghz_capa.capa,
2157 IEEE80211_HE_6GHZ_CAP_SM_PS);
6f4d7030 2158 }
d5c65159
KV
2159
2160 switch (smps) {
2161 case WLAN_HT_CAP_SM_PS_STATIC:
2162 arg->static_mimops_flag = true;
2163 break;
2164 case WLAN_HT_CAP_SM_PS_DYNAMIC:
2165 arg->dynamic_mimops_flag = true;
2166 break;
2167 case WLAN_HT_CAP_SM_PS_DISABLED:
2168 arg->spatial_mux_flag = true;
2169 break;
2170 default:
2171 break;
2172 }
2173}
2174
2175static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
2176 struct ieee80211_vif *vif,
2177 struct ieee80211_sta *sta,
2178 struct peer_assoc_params *arg)
2179{
2180 struct ath11k_vif *arvif = (void *)vif->drv_priv;
2181
2182 switch (arvif->vdev_type) {
2183 case WMI_VDEV_TYPE_AP:
2184 if (sta->wme) {
2185 /* TODO: Check WME vs QoS */
2186 arg->is_wme_set = true;
2187 arg->qos_flag = true;
2188 }
2189
2190 if (sta->wme && sta->uapsd_queues) {
2191 /* TODO: Check WME vs QoS */
2192 arg->is_wme_set = true;
2193 arg->apsd_flag = true;
2194 arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2195 }
2196 break;
2197 case WMI_VDEV_TYPE_STA:
2198 if (sta->wme) {
2199 arg->is_wme_set = true;
2200 arg->qos_flag = true;
2201 }
2202 break;
2203 default:
2204 break;
2205 }
2206
2207 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
2208 sta->addr, arg->qos_flag);
2209}
2210
2211static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
2212 struct ath11k_vif *arvif,
2213 struct ieee80211_sta *sta)
2214{
2215 struct ap_ps_params params;
2216 u32 max_sp;
2217 u32 uapsd;
2218 int ret;
2219
2220 lockdep_assert_held(&ar->conf_mutex);
2221
2222 params.vdev_id = arvif->vdev_id;
2223
2224 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2225 sta->uapsd_queues, sta->max_sp);
2226
2227 uapsd = 0;
2228 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2229 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2230 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2231 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2232 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2233 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2234 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2235 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2236 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2237 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2238 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2239 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2240
2241 max_sp = 0;
2242 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2243 max_sp = sta->max_sp;
2244
2245 params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2246 params.value = uapsd;
2247 ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2248 if (ret)
2249 goto err;
2250
2251 params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2252 params.value = max_sp;
2253 ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2254 if (ret)
2255 goto err;
2256
2257 /* TODO revisit during testing */
2258 params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2259 params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2260 ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2261 if (ret)
2262 goto err;
2263
2264 params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2265 params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2266 ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2267 if (ret)
2268 goto err;
2269
2270 return 0;
2271
2272err:
2273 ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2274 params.param, arvif->vdev_id, ret);
2275 return ret;
2276}
2277
2278static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2279{
2280 return sta->supp_rates[NL80211_BAND_2GHZ] >>
2281 ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2282}
2283
2284static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
2285 struct ieee80211_sta *sta)
2286{
2287 if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2288 switch (sta->vht_cap.cap &
2289 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2290 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2291 return MODE_11AC_VHT160;
2292 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2293 return MODE_11AC_VHT80_80;
2294 default:
2295 /* not sure if this is a valid case? */
2296 return MODE_11AC_VHT160;
2297 }
2298 }
2299
2300 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2301 return MODE_11AC_VHT80;
2302
2303 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2304 return MODE_11AC_VHT40;
2305
2306 if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2307 return MODE_11AC_VHT20;
2308
2309 return MODE_UNKNOWN;
2310}
2311
9f056ed8
JC
2312static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
2313 struct ieee80211_sta *sta)
2314{
2315 if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2316 if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
2317 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2318 return MODE_11AX_HE160;
2319 else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
2320 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
2321 return MODE_11AX_HE80_80;
2322 /* not sure if this is a valid case? */
2323 return MODE_11AX_HE160;
2324 }
2325
2326 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2327 return MODE_11AX_HE80;
2328
2329 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2330 return MODE_11AX_HE40;
2331
2332 if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2333 return MODE_11AX_HE20;
2334
2335 return MODE_UNKNOWN;
2336}
2337
d5c65159
KV
2338static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
2339 struct ieee80211_vif *vif,
2340 struct ieee80211_sta *sta,
2341 struct peer_assoc_params *arg)
2342{
2343 struct ath11k_vif *arvif = (void *)vif->drv_priv;
2344 struct cfg80211_chan_def def;
2345 enum nl80211_band band;
2346 const u8 *ht_mcs_mask;
2347 const u16 *vht_mcs_mask;
61fe43e7 2348 const u16 *he_mcs_mask;
d5c65159
KV
2349 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2350
2351 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2352 return;
2353
2354 band = def.chan->band;
2355 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2356 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
61fe43e7 2357 he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
d5c65159
KV
2358
2359 switch (band) {
2360 case NL80211_BAND_2GHZ:
61fe43e7
MH
2361 if (sta->he_cap.has_he &&
2362 !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
9f056ed8
JC
2363 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2364 phymode = MODE_11AX_HE80_2G;
2365 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2366 phymode = MODE_11AX_HE40_2G;
2367 else
2368 phymode = MODE_11AX_HE20_2G;
2369 } else if (sta->vht_cap.vht_supported &&
d5c65159
KV
2370 !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2371 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2372 phymode = MODE_11AC_VHT40;
2373 else
2374 phymode = MODE_11AC_VHT20;
2375 } else if (sta->ht_cap.ht_supported &&
2376 !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2377 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2378 phymode = MODE_11NG_HT40;
2379 else
2380 phymode = MODE_11NG_HT20;
2381 } else if (ath11k_mac_sta_has_ofdm_only(sta)) {
2382 phymode = MODE_11G;
2383 } else {
2384 phymode = MODE_11B;
2385 }
d5c65159
KV
2386 break;
2387 case NL80211_BAND_5GHZ:
22eeadcd 2388 case NL80211_BAND_6GHZ:
9f056ed8 2389 /* Check HE first */
61fe43e7
MH
2390 if (sta->he_cap.has_he &&
2391 !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
9f056ed8
JC
2392 phymode = ath11k_mac_get_phymode_he(ar, sta);
2393 } else if (sta->vht_cap.vht_supported &&
d5c65159
KV
2394 !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2395 phymode = ath11k_mac_get_phymode_vht(ar, sta);
2396 } else if (sta->ht_cap.ht_supported &&
2397 !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2398 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2399 phymode = MODE_11NA_HT40;
2400 else
2401 phymode = MODE_11NA_HT20;
2402 } else {
2403 phymode = MODE_11A;
2404 }
d5c65159
KV
2405 break;
2406 default:
2407 break;
2408 }
2409
2410 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
2411 sta->addr, ath11k_wmi_phymode_str(phymode));
2412
2413 arg->peer_phymode = phymode;
2414 WARN_ON(phymode == MODE_UNKNOWN);
2415}
2416
2417static void ath11k_peer_assoc_prepare(struct ath11k *ar,
2418 struct ieee80211_vif *vif,
2419 struct ieee80211_sta *sta,
2420 struct peer_assoc_params *arg,
2421 bool reassoc)
2422{
2423 lockdep_assert_held(&ar->conf_mutex);
2424
2425 memset(arg, 0, sizeof(*arg));
2426
2427 reinit_completion(&ar->peer_assoc_done);
2428
2429 arg->peer_new_assoc = !reassoc;
2430 ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
2431 ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
2432 ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
f552d6fd 2433 ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
d5c65159
KV
2434 ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
2435 ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
2436 ath11k_peer_assoc_h_he(ar, vif, sta, arg);
c3a7d7eb 2437 ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
d5c65159 2438 ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
d5c65159
KV
2439 ath11k_peer_assoc_h_smps(sta, arg);
2440
2441 /* TODO: amsdu_disable req? */
2442}
2443
2444static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
2445 const u8 *addr,
6f4d7030
PKC
2446 const struct ieee80211_sta_ht_cap *ht_cap,
2447 u16 he_6ghz_capa)
d5c65159
KV
2448{
2449 int smps;
2450
6f4d7030 2451 if (!ht_cap->ht_supported && !he_6ghz_capa)
d5c65159
KV
2452 return 0;
2453
6f4d7030
PKC
2454 if (ht_cap->ht_supported) {
2455 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2456 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2457 } else {
2458 smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
2459 }
d5c65159
KV
2460
2461 if (smps >= ARRAY_SIZE(ath11k_smps_map))
2462 return -EINVAL;
2463
2464 return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2465 WMI_PEER_MIMO_PS_STATE,
2466 ath11k_smps_map[smps]);
2467}
2468
2469static void ath11k_bss_assoc(struct ieee80211_hw *hw,
2470 struct ieee80211_vif *vif,
2471 struct ieee80211_bss_conf *bss_conf)
2472{
2473 struct ath11k *ar = hw->priv;
2474 struct ath11k_vif *arvif = (void *)vif->drv_priv;
2475 struct peer_assoc_params peer_arg;
2476 struct ieee80211_sta *ap_sta;
85f36923
KP
2477 struct ath11k_peer *peer;
2478 bool is_auth = false;
d5c65159
KV
2479 int ret;
2480
2481 lockdep_assert_held(&ar->conf_mutex);
2482
2483 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2484 arvif->vdev_id, arvif->bssid, arvif->aid);
2485
2486 rcu_read_lock();
2487
2488 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2489 if (!ap_sta) {
2490 ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2491 bss_conf->bssid, arvif->vdev_id);
2492 rcu_read_unlock();
2493 return;
2494 }
2495
2496 ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2497
2498 rcu_read_unlock();
2499
c802b6d7 2500 peer_arg.is_assoc = true;
d5c65159
KV
2501 ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2502 if (ret) {
2503 ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2504 bss_conf->bssid, arvif->vdev_id, ret);
2505 return;
2506 }
2507
2508 if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2509 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2510 bss_conf->bssid, arvif->vdev_id);
2511 return;
2512 }
2513
2514 ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
6f4d7030
PKC
2515 &ap_sta->ht_cap,
2516 le16_to_cpu(ap_sta->he_6ghz_capa.capa));
d5c65159
KV
2517 if (ret) {
2518 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2519 arvif->vdev_id, ret);
2520 return;
2521 }
2522
2523 WARN_ON(arvif->is_up);
2524
2525 arvif->aid = bss_conf->aid;
2526 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2527
2528 ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2529 if (ret) {
2530 ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
2531 arvif->vdev_id, ret);
2532 return;
2533 }
2534
2535 arvif->is_up = true;
2536
2537 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2538 "mac vdev %d up (associated) bssid %pM aid %d\n",
2539 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2540
85f36923
KP
2541 spin_lock_bh(&ar->ab->base_lock);
2542
2543 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
2544 if (peer && peer->is_authorized)
2545 is_auth = true;
2546
2547 spin_unlock_bh(&ar->ab->base_lock);
2548
2549 if (is_auth) {
2550 ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
2551 arvif->vdev_id,
2552 WMI_PEER_AUTHORIZE,
2553 1);
2554 if (ret)
2555 ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
2556 }
3f8be640
JC
2557
2558 ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2559 &bss_conf->he_obss_pd);
2560 if (ret)
2561 ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
2562 arvif->vdev_id, ret);
d5c65159
KV
2563}
2564
2565static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
2566 struct ieee80211_vif *vif)
2567{
2568 struct ath11k *ar = hw->priv;
2569 struct ath11k_vif *arvif = (void *)vif->drv_priv;
2570 int ret;
2571
2572 lockdep_assert_held(&ar->conf_mutex);
2573
2574 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2575 arvif->vdev_id, arvif->bssid);
2576
2577 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
2578 if (ret)
2579 ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
2580 arvif->vdev_id, ret);
2581
2582 arvif->is_up = false;
2583
26f69792 2584 cancel_delayed_work_sync(&arvif->connection_loss_work);
d5c65159
KV
2585}
2586
2587static u32 ath11k_mac_get_rate_hw_value(int bitrate)
2588{
2589 u32 preamble;
2590 u16 hw_value;
2591 int rate;
2592 size_t i;
2593
2594 if (ath11k_mac_bitrate_is_cck(bitrate))
2595 preamble = WMI_RATE_PREAMBLE_CCK;
2596 else
2597 preamble = WMI_RATE_PREAMBLE_OFDM;
2598
2599 for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
2600 if (ath11k_legacy_rates[i].bitrate != bitrate)
2601 continue;
2602
2603 hw_value = ath11k_legacy_rates[i].hw_value;
2604 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2605
2606 return rate;
2607 }
2608
2609 return -EINVAL;
2610}
2611
2612static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
2613 struct ieee80211_vif *vif,
2614 struct cfg80211_chan_def *def)
2615{
2616 struct ath11k_vif *arvif = (void *)vif->drv_priv;
2617 const struct ieee80211_supported_band *sband;
2618 u8 basic_rate_idx;
2619 int hw_rate_code;
2620 u32 vdev_param;
2621 u16 bitrate;
2622 int ret;
2623
2624 lockdep_assert_held(&ar->conf_mutex);
2625
2626 sband = ar->hw->wiphy->bands[def->chan->band];
2627 basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
2628 bitrate = sband->bitrates[basic_rate_idx].bitrate;
2629
2630 hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
2631 if (hw_rate_code < 0) {
2632 ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
2633 return;
2634 }
2635
2636 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
2637 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2638 hw_rate_code);
2639 if (ret)
2640 ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
2641
2642 vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
2643 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2644 hw_rate_code);
2645 if (ret)
2646 ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
2647}
2648
047679e3
AD
2649static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
2650 struct ieee80211_bss_conf *info)
2651{
2652 struct ath11k *ar = arvif->ar;
2653 struct sk_buff *tmpl;
2654 int ret;
2655 u32 interval;
2656 bool unsol_bcast_probe_resp_enabled = false;
2657
2658 if (info->fils_discovery.max_interval) {
2659 interval = info->fils_discovery.max_interval;
2660
2661 tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
2662 if (tmpl)
2663 ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
2664 tmpl);
2665 } else if (info->unsol_bcast_probe_resp_interval) {
2666 unsol_bcast_probe_resp_enabled = 1;
2667 interval = info->unsol_bcast_probe_resp_interval;
2668
2669 tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
2670 arvif->vif);
2671 if (tmpl)
2672 ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
2673 tmpl);
2674 } else { /* Disable */
2675 return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
2676 }
2677
2678 if (!tmpl) {
2679 ath11k_warn(ar->ab,
2680 "mac vdev %i failed to retrieve %s template\n",
2681 arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
2682 "unsolicited broadcast probe response" :
2683 "FILS discovery"));
2684 return -EPERM;
2685 }
2686 kfree_skb(tmpl);
2687
2688 if (!ret)
2689 ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
2690 unsol_bcast_probe_resp_enabled);
2691
2692 return ret;
2693}
2694
b56b08ae
RM
2695static int ath11k_mac_config_obss_pd(struct ath11k *ar,
2696 struct ieee80211_he_obss_pd *he_obss_pd)
2697{
2698 u32 bitmap[2], param_id, param_val, pdev_id;
2699 int ret;
2700 s8 non_srg_th = 0, srg_th = 0;
2701
2702 pdev_id = ar->pdev->pdev_id;
2703
2704 /* Set and enable SRG/non-SRG OBSS PD Threshold */
2705 param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
689a5e6f 2706 if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
b56b08ae
RM
2707 ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
2708 if (ret)
2709 ath11k_warn(ar->ab,
2710 "failed to set obss_pd_threshold for pdev: %u\n",
2711 pdev_id);
2712 return ret;
2713 }
2714
2715 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2716 "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
2717 he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
2718 he_obss_pd->max_offset);
2719
2720 param_val = 0;
2721
2722 if (he_obss_pd->sr_ctrl &
2723 IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
2724 non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
2725 } else {
2726 if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
2727 non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
2728 he_obss_pd->non_srg_max_offset);
2729 else
2730 non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
2731
2732 param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
2733 }
2734
2735 if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
2736 srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
2737 param_val |= ATH11K_OBSS_PD_SRG_EN;
2738 }
2739
2740 if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
2741 ar->ab->wmi_ab.svc_map)) {
2742 param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
2743 param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
2744 } else {
2745 non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
2746 /* SRG not supported and threshold in dB */
2747 param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
2748 ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
2749 }
2750
2751 param_val |= (non_srg_th & GENMASK(7, 0));
2752 ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2753 if (ret) {
2754 ath11k_warn(ar->ab,
2755 "failed to set obss_pd_threshold for pdev: %u\n",
2756 pdev_id);
2757 return ret;
2758 }
2759
2760 /* Enable OBSS PD for all access category */
2761 param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
2762 param_val = 0xf;
2763 ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2764 if (ret) {
2765 ath11k_warn(ar->ab,
2766 "failed to set obss_pd_per_ac for pdev: %u\n",
2767 pdev_id);
2768 return ret;
2769 }
2770
2771 /* Set SR Prohibit */
2772 param_id = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
2773 param_val = !!(he_obss_pd->sr_ctrl &
2774 IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
2775 ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2776 if (ret) {
2777 ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
2778 pdev_id);
2779 return ret;
2780 }
2781
2782 if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
2783 ar->ab->wmi_ab.svc_map))
2784 return 0;
2785
2786 /* Set SRG BSS Color Bitmap */
2787 memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
2788 ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
2789 if (ret) {
2790 ath11k_warn(ar->ab,
2791 "failed to set bss_color_bitmap for pdev: %u\n",
2792 pdev_id);
2793 return ret;
2794 }
2795
2796 /* Set SRG Partial BSSID Bitmap */
2797 memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
2798 ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
2799 if (ret) {
2800 ath11k_warn(ar->ab,
2801 "failed to set partial_bssid_bitmap for pdev: %u\n",
2802 pdev_id);
2803 return ret;
2804 }
2805
2806 memset(bitmap, 0xff, sizeof(bitmap));
2807
2808 /* Enable all BSS Colors for SRG */
2809 ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
2810 if (ret) {
2811 ath11k_warn(ar->ab,
2812 "failed to set srg_color_en_bitmap pdev: %u\n",
2813 pdev_id);
2814 return ret;
2815 }
2816
2817 /* Enable all patial BSSID mask for SRG */
2818 ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
2819 if (ret) {
2820 ath11k_warn(ar->ab,
2821 "failed to set srg_bssid_en_bitmap pdev: %u\n",
2822 pdev_id);
2823 return ret;
2824 }
2825
2826 /* Enable all BSS Colors for non-SRG */
2827 ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
2828 if (ret) {
2829 ath11k_warn(ar->ab,
2830 "failed to set non_srg_color_en_bitmap pdev: %u\n",
2831 pdev_id);
2832 return ret;
2833 }
2834
2835 /* Enable all patial BSSID mask for non-SRG */
2836 ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
2837 if (ret) {
2838 ath11k_warn(ar->ab,
2839 "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
2840 pdev_id);
2841 return ret;
2842 }
2843
2844 return 0;
2845}
2846
d5c65159
KV
2847static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
2848 struct ieee80211_vif *vif,
2849 struct ieee80211_bss_conf *info,
2850 u32 changed)
2851{
2852 struct ath11k *ar = hw->priv;
2853 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2854 struct cfg80211_chan_def def;
2855 u32 param_id, param_value;
2856 enum nl80211_band band;
2857 u32 vdev_param;
2858 int mcast_rate;
2859 u32 preamble;
2860 u16 hw_value;
2861 u16 bitrate;
2862 int ret = 0;
2863 u8 rateidx;
2864 u32 rate;
2865
2866 mutex_lock(&ar->conf_mutex);
2867
2868 if (changed & BSS_CHANGED_BEACON_INT) {
2869 arvif->beacon_interval = info->beacon_int;
2870
2871 param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
2872 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2873 param_id,
2874 arvif->beacon_interval);
2875 if (ret)
2876 ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
2877 arvif->vdev_id);
2878 else
2879 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2880 "Beacon interval: %d set for VDEV: %d\n",
2881 arvif->beacon_interval, arvif->vdev_id);
2882 }
2883
2884 if (changed & BSS_CHANGED_BEACON) {
2885 param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
2886 param_value = WMI_BEACON_STAGGERED_MODE;
2887 ret = ath11k_wmi_pdev_set_param(ar, param_id,
2888 param_value, ar->pdev->pdev_id);
2889 if (ret)
2890 ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
2891 arvif->vdev_id);
2892 else
2893 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2894 "Set staggered beacon mode for VDEV: %d\n",
2895 arvif->vdev_id);
2896
2897 ret = ath11k_mac_setup_bcn_tmpl(arvif);
2898 if (ret)
2899 ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
2900 ret);
2901 }
2902
2903 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
2904 arvif->dtim_period = info->dtim_period;
2905
2906 param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
2907 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2908 param_id,
2909 arvif->dtim_period);
2910
2911 if (ret)
2912 ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
2913 arvif->vdev_id, ret);
2914 else
2915 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2916 "DTIM period: %d set for VDEV: %d\n",
2917 arvif->dtim_period, arvif->vdev_id);
2918 }
2919
2920 if (changed & BSS_CHANGED_SSID &&
2921 vif->type == NL80211_IFTYPE_AP) {
2922 arvif->u.ap.ssid_len = info->ssid_len;
2923 if (info->ssid_len)
2924 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2925 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2926 }
2927
2928 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
2929 ether_addr_copy(arvif->bssid, info->bssid);
2930
bd888150 2931 if (changed & BSS_CHANGED_BEACON_ENABLED) {
d5c65159
KV
2932 ath11k_control_beaconing(arvif, info);
2933
bd888150
PKC
2934 if (arvif->is_up && vif->bss_conf.he_support &&
2935 vif->bss_conf.he_oper.params) {
743b9065
PKC
2936 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2937 WMI_VDEV_PARAM_BA_MODE,
2938 WMI_BA_MODE_BUFFER_SIZE_256);
2939 if (ret)
2940 ath11k_warn(ar->ab,
2941 "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
2942 arvif->vdev_id);
2943
bd888150
PKC
2944 param_id = WMI_VDEV_PARAM_HEOPS_0_31;
2945 param_value = vif->bss_conf.he_oper.params;
2946 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2947 param_id, param_value);
2948 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2949 "he oper param: %x set for VDEV: %d\n",
2950 param_value, arvif->vdev_id);
2951
2952 if (ret)
2953 ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
2954 param_value, arvif->vdev_id, ret);
2955 }
2956 }
2957
d5c65159
KV
2958 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2959 u32 cts_prot;
2960
2961 cts_prot = !!(info->use_cts_prot);
2962 param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
2963
2964 if (arvif->is_started) {
2965 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2966 param_id, cts_prot);
2967 if (ret)
2968 ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
2969 arvif->vdev_id);
2970 else
2971 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
2972 cts_prot, arvif->vdev_id);
2973 } else {
2974 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
2975 }
2976 }
2977
2978 if (changed & BSS_CHANGED_ERP_SLOT) {
2979 u32 slottime;
2980
2981 if (info->use_short_slot)
2982 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2983
2984 else
2985 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2986
2987 param_id = WMI_VDEV_PARAM_SLOT_TIME;
2988 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2989 param_id, slottime);
2990 if (ret)
2991 ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
2992 arvif->vdev_id);
2993 else
2994 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2995 "Set slottime: %d for VDEV: %d\n",
2996 slottime, arvif->vdev_id);
2997 }
2998
2999 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3000 u32 preamble;
3001
3002 if (info->use_short_preamble)
3003 preamble = WMI_VDEV_PREAMBLE_SHORT;
3004 else
3005 preamble = WMI_VDEV_PREAMBLE_LONG;
3006
3007 param_id = WMI_VDEV_PARAM_PREAMBLE;
3008 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3009 param_id, preamble);
3010 if (ret)
3011 ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
3012 arvif->vdev_id);
3013 else
3014 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3015 "Set preamble: %d for VDEV: %d\n",
3016 preamble, arvif->vdev_id);
3017 }
3018
3019 if (changed & BSS_CHANGED_ASSOC) {
3020 if (info->assoc)
3021 ath11k_bss_assoc(hw, vif, info);
3022 else
3023 ath11k_bss_disassoc(hw, vif);
3024 }
3025
3026 if (changed & BSS_CHANGED_TXPOWER) {
3027 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3028 arvif->vdev_id, info->txpower);
3029
3030 arvif->txpower = info->txpower;
3031 ath11k_mac_txpower_recalc(ar);
3032 }
3033
b2beffa7
CH
3034 if (changed & BSS_CHANGED_PS &&
3035 ar->ab->hw_params.supports_sta_ps) {
3036 arvif->ps = vif->bss_conf.ps;
3037
3038 ret = ath11k_mac_config_ps(ar);
3039 if (ret)
3040 ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
3041 arvif->vdev_id, ret);
3042 }
3043
d5c65159
KV
3044 if (changed & BSS_CHANGED_MCAST_RATE &&
3045 !ath11k_mac_vif_chan(arvif->vif, &def)) {
3046 band = def.chan->band;
3047 mcast_rate = vif->bss_conf.mcast_rate[band];
3048
3049 if (mcast_rate > 0)
3050 rateidx = mcast_rate - 1;
3051 else
3052 rateidx = ffs(vif->bss_conf.basic_rates) - 1;
3053
3054 if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
3055 rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
3056
3057 bitrate = ath11k_legacy_rates[rateidx].bitrate;
3058 hw_value = ath11k_legacy_rates[rateidx].hw_value;
3059
3060 if (ath11k_mac_bitrate_is_cck(bitrate))
3061 preamble = WMI_RATE_PREAMBLE_CCK;
3062 else
3063 preamble = WMI_RATE_PREAMBLE_OFDM;
3064
3065 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3066
3067 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3068 "mac vdev %d mcast_rate %x\n",
3069 arvif->vdev_id, rate);
3070
3071 vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
3072 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3073 vdev_param, rate);
3074 if (ret)
3075 ath11k_warn(ar->ab,
3076 "failed to set mcast rate on vdev %i: %d\n",
3077 arvif->vdev_id, ret);
3078
3079 vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
3080 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3081 vdev_param, rate);
3082 if (ret)
3083 ath11k_warn(ar->ab,
3084 "failed to set bcast rate on vdev %i: %d\n",
3085 arvif->vdev_id, ret);
3086 }
3087
3088 if (changed & BSS_CHANGED_BASIC_RATES &&
3089 !ath11k_mac_vif_chan(arvif->vif, &def))
3090 ath11k_recalculate_mgmt_rate(ar, vif, &def);
3091
6d293d44
JC
3092 if (changed & BSS_CHANGED_TWT) {
3093 if (info->twt_requester || info->twt_responder)
485add35 3094 ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
6d293d44 3095 else
485add35 3096 ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
6d293d44
JC
3097 }
3098
3f8be640 3099 if (changed & BSS_CHANGED_HE_OBSS_PD)
b56b08ae 3100 ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
3f8be640 3101
beb2f772
JC
3102 if (changed & BSS_CHANGED_HE_BSS_COLOR) {
3103 if (vif->type == NL80211_IFTYPE_AP) {
3104 ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3105 ar, arvif->vdev_id, info->he_bss_color.color,
3106 ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
75e6b594 3107 info->he_bss_color.enabled);
beb2f772
JC
3108 if (ret)
3109 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3110 arvif->vdev_id, ret);
3111 } else if (vif->type == NL80211_IFTYPE_STATION) {
3112 ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
3113 arvif->vdev_id,
3114 1);
3115 if (ret)
3116 ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
3117 arvif->vdev_id, ret);
3118 ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3119 ar, arvif->vdev_id, 0,
3120 ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
3121 if (ret)
3122 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3123 arvif->vdev_id, ret);
3124 }
3125 }
3126
047679e3
AD
3127 if (changed & BSS_CHANGED_FILS_DISCOVERY ||
3128 changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
3129 ath11k_mac_fils_discovery(arvif, info);
3130
d5c65159
KV
3131 mutex_unlock(&ar->conf_mutex);
3132}
3133
3134void __ath11k_mac_scan_finish(struct ath11k *ar)
3135{
3136 lockdep_assert_held(&ar->data_lock);
3137
3138 switch (ar->scan.state) {
3139 case ATH11K_SCAN_IDLE:
3140 break;
3141 case ATH11K_SCAN_RUNNING:
3142 case ATH11K_SCAN_ABORTING:
62db14ea
WG
3143 if (ar->scan.is_roc && ar->scan.roc_notify)
3144 ieee80211_remain_on_channel_expired(ar->hw);
3145 fallthrough;
3146 case ATH11K_SCAN_STARTING:
d5c65159
KV
3147 if (!ar->scan.is_roc) {
3148 struct cfg80211_scan_info info = {
62db14ea
WG
3149 .aborted = ((ar->scan.state ==
3150 ATH11K_SCAN_ABORTING) ||
3151 (ar->scan.state ==
3152 ATH11K_SCAN_STARTING)),
d5c65159
KV
3153 };
3154
3155 ieee80211_scan_completed(ar->hw, &info);
d5c65159 3156 }
62db14ea 3157
d5c65159
KV
3158 ar->scan.state = ATH11K_SCAN_IDLE;
3159 ar->scan_channel = NULL;
3160 ar->scan.roc_freq = 0;
3161 cancel_delayed_work(&ar->scan.timeout);
3162 complete(&ar->scan.completed);
3163 break;
3164 }
3165}
3166
3167void ath11k_mac_scan_finish(struct ath11k *ar)
3168{
3169 spin_lock_bh(&ar->data_lock);
3170 __ath11k_mac_scan_finish(ar);
3171 spin_unlock_bh(&ar->data_lock);
3172}
3173
3174static int ath11k_scan_stop(struct ath11k *ar)
3175{
3176 struct scan_cancel_param arg = {
3177 .req_type = WLAN_SCAN_CANCEL_SINGLE,
3178 .scan_id = ATH11K_SCAN_ID,
3179 };
3180 int ret;
3181
3182 lockdep_assert_held(&ar->conf_mutex);
3183
3184 /* TODO: Fill other STOP Params */
3185 arg.pdev_id = ar->pdev->pdev_id;
3186
3187 ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
3188 if (ret) {
3189 ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
3190 goto out;
3191 }
3192
3193 ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3194 if (ret == 0) {
3195 ath11k_warn(ar->ab,
3196 "failed to receive scan abort comple: timed out\n");
3197 ret = -ETIMEDOUT;
3198 } else if (ret > 0) {
3199 ret = 0;
3200 }
3201
3202out:
3203 /* Scan state should be updated upon scan completion but in case
3204 * firmware fails to deliver the event (for whatever reason) it is
3205 * desired to clean up scan state anyway. Firmware may have just
3206 * dropped the scan completion event delivery due to transport pipe
3207 * being overflown with data and/or it can recover on its own before
3208 * next scan request is submitted.
3209 */
3210 spin_lock_bh(&ar->data_lock);
3211 if (ar->scan.state != ATH11K_SCAN_IDLE)
3212 __ath11k_mac_scan_finish(ar);
3213 spin_unlock_bh(&ar->data_lock);
3214
3215 return ret;
3216}
3217
3218static void ath11k_scan_abort(struct ath11k *ar)
3219{
3220 int ret;
3221
3222 lockdep_assert_held(&ar->conf_mutex);
3223
3224 spin_lock_bh(&ar->data_lock);
3225
3226 switch (ar->scan.state) {
3227 case ATH11K_SCAN_IDLE:
3228 /* This can happen if timeout worker kicked in and called
3229 * abortion while scan completion was being processed.
3230 */
3231 break;
3232 case ATH11K_SCAN_STARTING:
3233 case ATH11K_SCAN_ABORTING:
3234 ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
3235 ar->scan.state);
3236 break;
3237 case ATH11K_SCAN_RUNNING:
3238 ar->scan.state = ATH11K_SCAN_ABORTING;
3239 spin_unlock_bh(&ar->data_lock);
3240
3241 ret = ath11k_scan_stop(ar);
3242 if (ret)
3243 ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
3244
3245 spin_lock_bh(&ar->data_lock);
3246 break;
3247 }
3248
3249 spin_unlock_bh(&ar->data_lock);
3250}
3251
3252static void ath11k_scan_timeout_work(struct work_struct *work)
3253{
3254 struct ath11k *ar = container_of(work, struct ath11k,
3255 scan.timeout.work);
3256
3257 mutex_lock(&ar->conf_mutex);
3258 ath11k_scan_abort(ar);
3259 mutex_unlock(&ar->conf_mutex);
3260}
3261
3262static int ath11k_start_scan(struct ath11k *ar,
3263 struct scan_req_params *arg)
3264{
3265 int ret;
3266
3267 lockdep_assert_held(&ar->conf_mutex);
3268
9d11b7bf
KP
3269 if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
3270 ath11k_spectral_reset_buffer(ar);
3271
d5c65159
KV
3272 ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
3273 if (ret)
3274 return ret;
3275
3276 ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
3277 if (ret == 0) {
3278 ret = ath11k_scan_stop(ar);
3279 if (ret)
3280 ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
3281
3282 return -ETIMEDOUT;
3283 }
3284
3285 /* If we failed to start the scan, return error code at
3286 * this point. This is probably due to some issue in the
3287 * firmware, but no need to wedge the driver due to that...
3288 */
3289 spin_lock_bh(&ar->data_lock);
3290 if (ar->scan.state == ATH11K_SCAN_IDLE) {
3291 spin_unlock_bh(&ar->data_lock);
3292 return -EINVAL;
3293 }
3294 spin_unlock_bh(&ar->data_lock);
3295
3296 return 0;
3297}
3298
3299static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
3300 struct ieee80211_vif *vif,
3301 struct ieee80211_scan_request *hw_req)
3302{
3303 struct ath11k *ar = hw->priv;
3304 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3305 struct cfg80211_scan_request *req = &hw_req->req;
3306 struct scan_req_params arg;
3307 int ret = 0;
3308 int i;
3309
3310 mutex_lock(&ar->conf_mutex);
3311
3312 spin_lock_bh(&ar->data_lock);
3313 switch (ar->scan.state) {
3314 case ATH11K_SCAN_IDLE:
3315 reinit_completion(&ar->scan.started);
3316 reinit_completion(&ar->scan.completed);
3317 ar->scan.state = ATH11K_SCAN_STARTING;
3318 ar->scan.is_roc = false;
3319 ar->scan.vdev_id = arvif->vdev_id;
3320 ret = 0;
3321 break;
3322 case ATH11K_SCAN_STARTING:
3323 case ATH11K_SCAN_RUNNING:
3324 case ATH11K_SCAN_ABORTING:
3325 ret = -EBUSY;
3326 break;
3327 }
3328 spin_unlock_bh(&ar->data_lock);
3329
3330 if (ret)
3331 goto exit;
3332
3333 memset(&arg, 0, sizeof(arg));
3334 ath11k_wmi_start_scan_init(ar, &arg);
3335 arg.vdev_id = arvif->vdev_id;
3336 arg.scan_id = ATH11K_SCAN_ID;
3337
3338 if (req->ie_len) {
3339 arg.extraie.len = req->ie_len;
3340 arg.extraie.ptr = kzalloc(req->ie_len, GFP_KERNEL);
3341 memcpy(arg.extraie.ptr, req->ie, req->ie_len);
3342 }
3343
3344 if (req->n_ssids) {
3345 arg.num_ssids = req->n_ssids;
3346 for (i = 0; i < arg.num_ssids; i++) {
3347 arg.ssid[i].length = req->ssids[i].ssid_len;
3348 memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
3349 req->ssids[i].ssid_len);
3350 }
3351 } else {
3352 arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
3353 }
3354
3355 if (req->n_channels) {
3356 arg.num_chan = req->n_channels;
3357 for (i = 0; i < arg.num_chan; i++)
3358 arg.chan_list[i] = req->channels[i]->center_freq;
3359 }
3360
3361 ret = ath11k_start_scan(ar, &arg);
3362 if (ret) {
3363 ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
3364 spin_lock_bh(&ar->data_lock);
3365 ar->scan.state = ATH11K_SCAN_IDLE;
3366 spin_unlock_bh(&ar->data_lock);
3367 }
3368
3369 /* Add a 200ms margin to account for event/command processing */
3370 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3371 msecs_to_jiffies(arg.max_scan_time +
3372 ATH11K_MAC_SCAN_TIMEOUT_MSECS));
3373
3374exit:
3375 if (req->ie_len)
3376 kfree(arg.extraie.ptr);
3377
3378 mutex_unlock(&ar->conf_mutex);
3379 return ret;
3380}
3381
3382static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
3383 struct ieee80211_vif *vif)
3384{
3385 struct ath11k *ar = hw->priv;
3386
3387 mutex_lock(&ar->conf_mutex);
3388 ath11k_scan_abort(ar);
3389 mutex_unlock(&ar->conf_mutex);
3390
3391 cancel_delayed_work_sync(&ar->scan.timeout);
3392}
3393
3394static int ath11k_install_key(struct ath11k_vif *arvif,
3395 struct ieee80211_key_conf *key,
3396 enum set_key_cmd cmd,
3397 const u8 *macaddr, u32 flags)
3398{
3399 int ret;
3400 struct ath11k *ar = arvif->ar;
3401 struct wmi_vdev_install_key_arg arg = {
3402 .vdev_id = arvif->vdev_id,
3403 .key_idx = key->keyidx,
3404 .key_len = key->keylen,
3405 .key_data = key->key,
3406 .key_flags = flags,
3407 .macaddr = macaddr,
3408 };
3409
3410 lockdep_assert_held(&arvif->ar->conf_mutex);
3411
3412 reinit_completion(&ar->install_key_done);
3413
aa2092a9
VN
3414 if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3415 return 0;
3416
d5c65159
KV
3417 if (cmd == DISABLE_KEY) {
3418 /* TODO: Check if FW expects value other than NONE for del */
3419 /* arg.key_cipher = WMI_CIPHER_NONE; */
3420 arg.key_len = 0;
3421 arg.key_data = NULL;
3422 goto install;
3423 }
3424
3425 switch (key->cipher) {
3426 case WLAN_CIPHER_SUITE_CCMP:
3427 arg.key_cipher = WMI_CIPHER_AES_CCM;
3428 /* TODO: Re-check if flag is valid */
3429 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
3430 break;
3431 case WLAN_CIPHER_SUITE_TKIP:
3432 arg.key_cipher = WMI_CIPHER_TKIP;
3433 arg.key_txmic_len = 8;
3434 arg.key_rxmic_len = 8;
3435 break;
3436 case WLAN_CIPHER_SUITE_CCMP_256:
3437 arg.key_cipher = WMI_CIPHER_AES_CCM;
3438 break;
3439 case WLAN_CIPHER_SUITE_GCMP:
3440 case WLAN_CIPHER_SUITE_GCMP_256:
3441 arg.key_cipher = WMI_CIPHER_AES_GCM;
3442 break;
3443 default:
3444 ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
3445 return -EOPNOTSUPP;
3446 }
3447
aa2092a9
VN
3448 if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
3449 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
3450 IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
3451
d5c65159
KV
3452install:
3453 ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
aa2092a9 3454
d5c65159
KV
3455 if (ret)
3456 return ret;
3457
3458 if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
3459 return -ETIMEDOUT;
3460
3461 return ar->install_key_status ? -EINVAL : 0;
3462}
3463
3464static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
3465 const u8 *addr)
3466{
3467 struct ath11k *ar = arvif->ar;
3468 struct ath11k_base *ab = ar->ab;
3469 struct ath11k_peer *peer;
3470 int first_errno = 0;
3471 int ret;
3472 int i;
3473 u32 flags = 0;
3474
3475 lockdep_assert_held(&ar->conf_mutex);
3476
3477 spin_lock_bh(&ab->base_lock);
3478 peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
3479 spin_unlock_bh(&ab->base_lock);
3480
3481 if (!peer)
3482 return -ENOENT;
3483
3484 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
3485 if (!peer->keys[i])
3486 continue;
3487
3488 /* key flags are not required to delete the key */
3489 ret = ath11k_install_key(arvif, peer->keys[i],
3490 DISABLE_KEY, addr, flags);
3491 if (ret < 0 && first_errno == 0)
3492 first_errno = ret;
3493
3494 if (ret < 0)
3495 ath11k_warn(ab, "failed to remove peer key %d: %d\n",
3496 i, ret);
3497
3498 spin_lock_bh(&ab->base_lock);
3499 peer->keys[i] = NULL;
3500 spin_unlock_bh(&ab->base_lock);
3501 }
3502
3503 return first_errno;
3504}
3505
3506static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3507 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3508 struct ieee80211_key_conf *key)
3509{
3510 struct ath11k *ar = hw->priv;
3511 struct ath11k_base *ab = ar->ab;
3512 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3513 struct ath11k_peer *peer;
1441b2f2 3514 struct ath11k_sta *arsta;
d5c65159
KV
3515 const u8 *peer_addr;
3516 int ret = 0;
3517 u32 flags = 0;
3518
3519 /* BIP needs to be done in software */
3520 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3521 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3522 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
3523 key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
3524 return 1;
3525
aa2092a9
VN
3526 if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3527 return 1;
3528
d5c65159
KV
3529 if (key->keyidx > WMI_MAX_KEY_INDEX)
3530 return -ENOSPC;
3531
3532 mutex_lock(&ar->conf_mutex);
3533
3534 if (sta)
3535 peer_addr = sta->addr;
3536 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3537 peer_addr = vif->bss_conf.bssid;
3538 else
3539 peer_addr = vif->addr;
3540
3541 key->hw_key_idx = key->keyidx;
3542
3543 /* the peer should not disappear in mid-way (unless FW goes awry) since
3544 * we already hold conf_mutex. we just make sure its there now.
3545 */
3546 spin_lock_bh(&ab->base_lock);
3547 peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
c3944a56
S
3548
3549 /* flush the fragments cache during key (re)install to
3550 * ensure all frags in the new frag list belong to the same key.
3551 */
3552 if (peer && cmd == SET_KEY)
3553 ath11k_peer_frags_flush(ar, peer);
d5c65159
KV
3554 spin_unlock_bh(&ab->base_lock);
3555
3556 if (!peer) {
3557 if (cmd == SET_KEY) {
3558 ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
3559 peer_addr);
3560 ret = -EOPNOTSUPP;
3561 goto exit;
3562 } else {
3563 /* if the peer doesn't exist there is no key to disable
3564 * anymore
3565 */
3566 goto exit;
3567 }
3568 }
3569
3570 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3571 flags |= WMI_KEY_PAIRWISE;
3572 else
3573 flags |= WMI_KEY_GROUP;
3574
3575 ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
3576 if (ret) {
3577 ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
3578 goto exit;
3579 }
3580
1441b2f2
MP
3581 ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
3582 if (ret) {
3583 ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
3584 goto exit;
3585 }
3586
d5c65159
KV
3587 spin_lock_bh(&ab->base_lock);
3588 peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
243874c6 3589 if (peer && cmd == SET_KEY) {
d5c65159 3590 peer->keys[key->keyidx] = key;
acc79d98 3591 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
243874c6 3592 peer->ucast_keyidx = key->keyidx;
acc79d98
S
3593 peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
3594 } else {
243874c6 3595 peer->mcast_keyidx = key->keyidx;
acc79d98
S
3596 peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
3597 }
243874c6 3598 } else if (peer && cmd == DISABLE_KEY) {
d5c65159 3599 peer->keys[key->keyidx] = NULL;
243874c6
MP
3600 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3601 peer->ucast_keyidx = 0;
3602 else
3603 peer->mcast_keyidx = 0;
3604 } else if (!peer)
d5c65159
KV
3605 /* impossible unless FW goes crazy */
3606 ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
1441b2f2
MP
3607
3608 if (sta) {
3609 arsta = (struct ath11k_sta *)sta->drv_priv;
3610
3611 switch (key->cipher) {
3612 case WLAN_CIPHER_SUITE_TKIP:
3613 case WLAN_CIPHER_SUITE_CCMP:
3614 case WLAN_CIPHER_SUITE_CCMP_256:
3615 case WLAN_CIPHER_SUITE_GCMP:
3616 case WLAN_CIPHER_SUITE_GCMP_256:
3617 if (cmd == SET_KEY)
3618 arsta->pn_type = HAL_PN_TYPE_WPA;
3619 else
3620 arsta->pn_type = HAL_PN_TYPE_NONE;
3621 break;
3622 default:
3623 arsta->pn_type = HAL_PN_TYPE_NONE;
3624 break;
3625 }
3626 }
acc79d98 3627
d5c65159
KV
3628 spin_unlock_bh(&ab->base_lock);
3629
3630exit:
3631 mutex_unlock(&ar->conf_mutex);
3632 return ret;
3633}
3634
3635static int
3636ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
3637 enum nl80211_band band,
3638 const struct cfg80211_bitrate_mask *mask)
3639{
3640 int num_rates = 0;
3641 int i;
3642
3643 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
3644 num_rates += hweight16(mask->control[band].vht_mcs[i]);
3645
3646 return num_rates;
3647}
3648
61fe43e7
MH
3649static int
3650ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
3651 enum nl80211_band band,
3652 const struct cfg80211_bitrate_mask *mask)
3653{
3654 int num_rates = 0;
3655 int i;
3656
3657 for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
3658 num_rates += hweight16(mask->control[band].he_mcs[i]);
3659
3660 return num_rates;
3661}
3662
d5c65159
KV
3663static int
3664ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
3665 struct ieee80211_sta *sta,
3666 const struct cfg80211_bitrate_mask *mask,
3667 enum nl80211_band band)
3668{
3669 struct ath11k *ar = arvif->ar;
3670 u8 vht_rate, nss;
3671 u32 rate_code;
3672 int ret, i;
3673
3674 lockdep_assert_held(&ar->conf_mutex);
3675
3676 nss = 0;
3677
3678 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
3679 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
3680 nss = i + 1;
3681 vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
3682 }
3683 }
3684
3685 if (!nss) {
3686 ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
3687 sta->addr);
3688 return -EINVAL;
3689 }
3690
61fe43e7
MH
3691 /* Avoid updating invalid nss as fixed rate*/
3692 if (nss > sta->rx_nss)
3693 return -EINVAL;
3694
d5c65159
KV
3695 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3696 "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
3697 sta->addr);
3698
3699 rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
3700 WMI_RATE_PREAMBLE_VHT);
3701 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
3702 arvif->vdev_id,
3703 WMI_PEER_PARAM_FIXED_RATE,
3704 rate_code);
3705 if (ret)
3706 ath11k_warn(ar->ab,
3707 "failed to update STA %pM Fixed Rate %d: %d\n",
3708 sta->addr, rate_code, ret);
3709
3710 return ret;
3711}
3712
61fe43e7
MH
3713static int
3714ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
3715 struct ieee80211_sta *sta,
3716 const struct cfg80211_bitrate_mask *mask,
3717 enum nl80211_band band)
3718{
3719 struct ath11k *ar = arvif->ar;
3720 u8 he_rate, nss;
3721 u32 rate_code;
3722 int ret, i;
3723
3724 lockdep_assert_held(&ar->conf_mutex);
3725
3726 nss = 0;
3727
3728 for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
3729 if (hweight16(mask->control[band].he_mcs[i]) == 1) {
3730 nss = i + 1;
3731 he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
3732 }
3733 }
3734
3735 if (!nss) {
3736 ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
3737 sta->addr);
3738 return -EINVAL;
3739 }
3740
3741 /* Avoid updating invalid nss as fixed rate */
3742 if (nss > sta->rx_nss)
3743 return -EINVAL;
3744
3745 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3746 "mac setting fixed he rate for peer %pM, device will not switch to any other selected rates",
3747 sta->addr);
3748
3749 rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
3750 WMI_RATE_PREAMBLE_HE);
3751
3752 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
3753 arvif->vdev_id,
3754 WMI_PEER_PARAM_FIXED_RATE,
3755 rate_code);
3756 if (ret)
3757 ath11k_warn(ar->ab,
3758 "failed to update sta %pM fixed rate %d: %d\n",
3759 sta->addr, rate_code, ret);
3760
3761 return ret;
3762}
3763
d5c65159
KV
3764static int ath11k_station_assoc(struct ath11k *ar,
3765 struct ieee80211_vif *vif,
3766 struct ieee80211_sta *sta,
3767 bool reassoc)
3768{
3769 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3770 struct peer_assoc_params peer_arg;
3771 int ret = 0;
3772 struct cfg80211_chan_def def;
3773 enum nl80211_band band;
3774 struct cfg80211_bitrate_mask *mask;
61fe43e7 3775 u8 num_vht_rates, num_he_rates;
d5c65159
KV
3776
3777 lockdep_assert_held(&ar->conf_mutex);
3778
3779 if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
3780 return -EPERM;
3781
3782 band = def.chan->band;
3783 mask = &arvif->bitrate_mask;
3784
3785 ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
3786
c802b6d7 3787 peer_arg.is_assoc = true;
d5c65159
KV
3788 ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3789 if (ret) {
3790 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3791 sta->addr, arvif->vdev_id, ret);
3792 return ret;
3793 }
3794
3795 if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3796 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3797 sta->addr, arvif->vdev_id);
3798 return -ETIMEDOUT;
3799 }
3800
3801 num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
61fe43e7 3802 num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
d5c65159 3803
61fe43e7
MH
3804 /* If single VHT/HE rate is configured (by set_bitrate_mask()),
3805 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
d5c65159
KV
3806 * fixed param.
3807 * Note that all other rates and NSS will be disabled for this peer.
3808 */
3809 if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
3810 ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3811 band);
3812 if (ret)
3813 return ret;
61fe43e7
MH
3814 } else if (sta->he_cap.has_he && num_he_rates == 1) {
3815 ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
3816 band);
3817 if (ret)
3818 return ret;
d5c65159
KV
3819 }
3820
3821 /* Re-assoc is run only to update supported rates for given station. It
3822 * doesn't make much sense to reconfigure the peer completely.
3823 */
3824 if (reassoc)
3825 return 0;
3826
3827 ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
6f4d7030 3828 &sta->ht_cap, le16_to_cpu(sta->he_6ghz_capa.capa));
d5c65159
KV
3829 if (ret) {
3830 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3831 arvif->vdev_id, ret);
3832 return ret;
3833 }
3834
3835 if (!sta->wme) {
3836 arvif->num_legacy_stations++;
3837 ret = ath11k_recalc_rtscts_prot(arvif);
3838 if (ret)
3839 return ret;
3840 }
3841
3842 if (sta->wme && sta->uapsd_queues) {
3843 ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
3844 if (ret) {
3845 ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
3846 sta->addr, arvif->vdev_id, ret);
3847 return ret;
3848 }
3849 }
3850
3851 return 0;
3852}
3853
3854static int ath11k_station_disassoc(struct ath11k *ar,
3855 struct ieee80211_vif *vif,
3856 struct ieee80211_sta *sta)
3857{
3858 struct ath11k_vif *arvif = (void *)vif->drv_priv;
3859 int ret = 0;
3860
3861 lockdep_assert_held(&ar->conf_mutex);
3862
3863 if (!sta->wme) {
3864 arvif->num_legacy_stations--;
3865 ret = ath11k_recalc_rtscts_prot(arvif);
3866 if (ret)
3867 return ret;
3868 }
3869
3870 ret = ath11k_clear_peer_keys(arvif, sta->addr);
3871 if (ret) {
3872 ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
3873 arvif->vdev_id, ret);
3874 return ret;
3875 }
3876 return 0;
3877}
3878
3879static void ath11k_sta_rc_update_wk(struct work_struct *wk)
3880{
3881 struct ath11k *ar;
3882 struct ath11k_vif *arvif;
3883 struct ath11k_sta *arsta;
3884 struct ieee80211_sta *sta;
3885 struct cfg80211_chan_def def;
3886 enum nl80211_band band;
3887 const u8 *ht_mcs_mask;
3888 const u16 *vht_mcs_mask;
61fe43e7 3889 const u16 *he_mcs_mask;
d5c65159 3890 u32 changed, bw, nss, smps;
61fe43e7 3891 int err, num_vht_rates, num_he_rates;
d5c65159
KV
3892 const struct cfg80211_bitrate_mask *mask;
3893 struct peer_assoc_params peer_arg;
3894
3895 arsta = container_of(wk, struct ath11k_sta, update_wk);
3896 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3897 arvif = arsta->arvif;
3898 ar = arvif->ar;
3899
3900 if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
3901 return;
3902
3903 band = def.chan->band;
3904 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3905 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
61fe43e7 3906 he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
d5c65159
KV
3907
3908 spin_lock_bh(&ar->data_lock);
3909
3910 changed = arsta->changed;
3911 arsta->changed = 0;
3912
3913 bw = arsta->bw;
3914 nss = arsta->nss;
3915 smps = arsta->smps;
3916
3917 spin_unlock_bh(&ar->data_lock);
3918
3919 mutex_lock(&ar->conf_mutex);
3920
3921 nss = max_t(u32, 1, nss);
61fe43e7
MH
3922 nss = min(nss, max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
3923 ath11k_mac_max_vht_nss(vht_mcs_mask)),
3924 ath11k_mac_max_he_nss(he_mcs_mask)));
d5c65159
KV
3925
3926 if (changed & IEEE80211_RC_BW_CHANGED) {
f187fe8e
VN
3927 /* Send peer assoc command before set peer bandwidth param to
3928 * avoid the mismatch between the peer phymode and the peer
3929 * bandwidth.
3930 */
3931 ath11k_peer_assoc_prepare(ar, arvif->vif, sta, &peer_arg, true);
3932
3933 peer_arg.is_assoc = false;
3934 err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3935 if (err) {
3936 ath11k_warn(ar->ab, "failed to send peer assoc for STA %pM vdev %i: %d\n",
3937 sta->addr, arvif->vdev_id, err);
3938 } else if (wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3939 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3940 WMI_PEER_CHWIDTH, bw);
3941 if (err)
3942 ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
3943 sta->addr, bw, err);
3944 } else {
3945 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3946 sta->addr, arvif->vdev_id);
3947 }
d5c65159
KV
3948 }
3949
3950 if (changed & IEEE80211_RC_NSS_CHANGED) {
3951 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
3952 sta->addr, nss);
3953
3954 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3955 WMI_PEER_NSS, nss);
3956 if (err)
3957 ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3958 sta->addr, nss, err);
3959 }
3960
3961 if (changed & IEEE80211_RC_SMPS_CHANGED) {
3962 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
3963 sta->addr, smps);
3964
3965 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3966 WMI_PEER_MIMO_PS_STATE, smps);
3967 if (err)
3968 ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3969 sta->addr, smps, err);
3970 }
3971
3972 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3973 mask = &arvif->bitrate_mask;
3974 num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
3975 mask);
61fe43e7
MH
3976 num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
3977 mask);
d5c65159
KV
3978
3979 /* Peer_assoc_prepare will reject vht rates in
3980 * bitrate_mask if its not available in range format and
3981 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3982 * setting(eg. MCS 4,5,6) per peer is not supported here.
3983 * But, Single rate in VHT mask can be set as per-peer
3984 * fixed rate. But even if any HT rates are configured in
3985 * the bitrate mask, device will not switch to those rates
3986 * when per-peer Fixed rate is set.
3987 * TODO: Check RATEMASK_CMDID to support auto rates selection
3988 * across HT/VHT and for multiple VHT MCS support.
3989 */
3990 if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
3991 ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3992 band);
61fe43e7
MH
3993 } else if (sta->he_cap.has_he && num_he_rates == 1) {
3994 ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
3995 band);
d5c65159 3996 } else {
61fe43e7 3997 /* If the peer is non-VHT/HE or no fixed VHT/HE rate
d5c65159 3998 * is provided in the new bitrate mask we set the
61fe43e7
MH
3999 * other rates using peer_assoc command. Also clear
4000 * the peer fixed rate settings as it has higher proprity
4001 * than peer assoc
d5c65159 4002 */
61fe43e7
MH
4003 err = ath11k_wmi_set_peer_param(ar, sta->addr,
4004 arvif->vdev_id,
4005 WMI_PEER_PARAM_FIXED_RATE,
4006 WMI_FIXED_RATE_NONE);
4007 if (err)
4008 ath11k_warn(ar->ab,
4009 "failed to disable peer fixed rate for sta %pM: %d\n",
4010 sta->addr, err);
4011
d5c65159
KV
4012 ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4013 &peer_arg, true);
4014
c802b6d7 4015 peer_arg.is_assoc = false;
d5c65159
KV
4016 err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4017 if (err)
4018 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4019 sta->addr, arvif->vdev_id, err);
4020
4021 if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4022 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4023 sta->addr, arvif->vdev_id);
4024 }
4025 }
4026
4027 mutex_unlock(&ar->conf_mutex);
4028}
4029
34c67dc3
SM
4030static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4031{
4032 struct ath11k *ar;
4033 struct ath11k_vif *arvif;
4034 struct ath11k_sta *arsta;
4035 struct ieee80211_sta *sta;
4036 int ret = 0;
4037
4038 arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4039 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4040 arvif = arsta->arvif;
4041 ar = arvif->ar;
4042
4043 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4044 "setting USE_4ADDR for peer %pM\n", sta->addr);
4045
4046 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4047 arvif->vdev_id,
4048 WMI_PEER_USE_4ADDR, 1);
4049
4050 if (ret)
4051 ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4052 sta->addr, ret);
4053}
4054
d5c65159
KV
4055static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4056 struct ieee80211_sta *sta)
4057{
4058 struct ath11k *ar = arvif->ar;
4059
4060 lockdep_assert_held(&ar->conf_mutex);
4061
4062 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4063 return 0;
4064
4065 if (ar->num_stations >= ar->max_num_stations)
4066 return -ENOBUFS;
4067
4068 ar->num_stations++;
4069
4070 return 0;
4071}
4072
4073static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4074 struct ieee80211_sta *sta)
4075{
4076 struct ath11k *ar = arvif->ar;
4077
4078 lockdep_assert_held(&ar->conf_mutex);
4079
4080 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4081 return;
4082
4083 ar->num_stations--;
4084}
4085
5e971287
KP
4086static int ath11k_mac_station_add(struct ath11k *ar,
4087 struct ieee80211_vif *vif,
4088 struct ieee80211_sta *sta)
4089{
4090 struct ath11k_base *ab = ar->ab;
4091 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4092 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4093 struct peer_create_params peer_param;
4094 int ret;
4095
4096 lockdep_assert_held(&ar->conf_mutex);
4097
4098 ret = ath11k_mac_inc_num_stations(arvif, sta);
4099 if (ret) {
4100 ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4101 ar->max_num_stations);
4102 goto exit;
4103 }
4104
4105 arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4106 if (!arsta->rx_stats) {
4107 ret = -ENOMEM;
4108 goto dec_num_station;
4109 }
4110
4111 peer_param.vdev_id = arvif->vdev_id;
4112 peer_param.peer_addr = sta->addr;
4113 peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4114
4115 ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4116 if (ret) {
4117 ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4118 sta->addr, arvif->vdev_id);
4119 goto free_rx_stats;
4120 }
4121
4122 ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4123 sta->addr, arvif->vdev_id);
4124
cb4e57db 4125 if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
5e971287
KP
4126 arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4127 if (!arsta->tx_stats) {
4128 ret = -ENOMEM;
4129 goto free_peer;
4130 }
4131 }
4132
4133 if (ieee80211_vif_is_mesh(vif)) {
34c67dc3
SM
4134 ath11k_dbg(ab, ATH11K_DBG_MAC,
4135 "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
5e971287
KP
4136 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4137 arvif->vdev_id,
4138 WMI_PEER_USE_4ADDR, 1);
4139 if (ret) {
34c67dc3 4140 ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
5e971287
KP
4141 sta->addr, ret);
4142 goto free_tx_stats;
4143 }
4144 }
4145
4146 ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4147 if (ret) {
4148 ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4149 sta->addr, arvif->vdev_id, ret);
4150 goto free_tx_stats;
4151 }
4152
b0919924 4153 if (ab->hw_params.vdev_start_delay &&
aa44b2f3 4154 !arvif->is_started &&
b0919924 4155 arvif->vdev_type != WMI_VDEV_TYPE_AP) {
e7495035
CH
4156 ret = ath11k_start_vdev_delay(ar->hw, vif);
4157 if (ret) {
4158 ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4159 goto free_tx_stats;
4160 }
4161 }
4162
5e971287
KP
4163 return 0;
4164
4165free_tx_stats:
4166 kfree(arsta->tx_stats);
4167 arsta->tx_stats = NULL;
4168free_peer:
4169 ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4170free_rx_stats:
4171 kfree(arsta->rx_stats);
4172 arsta->rx_stats = NULL;
4173dec_num_station:
4174 ath11k_mac_dec_num_stations(arvif, sta);
4175exit:
4176 return ret;
4177}
4178
d5c65159
KV
4179static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
4180 struct ieee80211_vif *vif,
4181 struct ieee80211_sta *sta,
4182 enum ieee80211_sta_state old_state,
4183 enum ieee80211_sta_state new_state)
4184{
4185 struct ath11k *ar = hw->priv;
4186 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4187 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
58595c98 4188 struct ath11k_peer *peer;
d5c65159
KV
4189 int ret = 0;
4190
4191 /* cancel must be done outside the mutex to avoid deadlock */
4192 if ((old_state == IEEE80211_STA_NONE &&
34c67dc3 4193 new_state == IEEE80211_STA_NOTEXIST)) {
d5c65159 4194 cancel_work_sync(&arsta->update_wk);
34c67dc3
SM
4195 cancel_work_sync(&arsta->set_4addr_wk);
4196 }
d5c65159
KV
4197
4198 mutex_lock(&ar->conf_mutex);
4199
4200 if (old_state == IEEE80211_STA_NOTEXIST &&
4201 new_state == IEEE80211_STA_NONE) {
4202 memset(arsta, 0, sizeof(*arsta));
4203 arsta->arvif = arvif;
4204 INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
34c67dc3 4205 INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
d5c65159 4206
5e971287
KP
4207 ret = ath11k_mac_station_add(ar, vif, sta);
4208 if (ret)
4209 ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
d5c65159 4210 sta->addr, arvif->vdev_id);
d5c65159
KV
4211 } else if ((old_state == IEEE80211_STA_NONE &&
4212 new_state == IEEE80211_STA_NOTEXIST)) {
4213 ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
4214
b4a0f541
WG
4215 if (ar->ab->hw_params.vdev_start_delay &&
4216 vif->type == NL80211_IFTYPE_STATION)
4217 goto free;
4218
d5c65159
KV
4219 ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4220 if (ret)
4221 ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
4222 sta->addr, arvif->vdev_id);
4223 else
5e971287
KP
4224 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
4225 sta->addr, arvif->vdev_id);
d5c65159
KV
4226
4227 ath11k_mac_dec_num_stations(arvif, sta);
58595c98
VP
4228 spin_lock_bh(&ar->ab->base_lock);
4229 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4230 if (peer && peer->sta == sta) {
4231 ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
4232 vif->addr, arvif->vdev_id);
4233 peer->sta = NULL;
4234 list_del(&peer->list);
4235 kfree(peer);
4236 ar->num_peers--;
4237 }
4238 spin_unlock_bh(&ar->ab->base_lock);
d5c65159 4239
b4a0f541 4240free:
d5c65159
KV
4241 kfree(arsta->tx_stats);
4242 arsta->tx_stats = NULL;
4243
4244 kfree(arsta->rx_stats);
4245 arsta->rx_stats = NULL;
4246 } else if (old_state == IEEE80211_STA_AUTH &&
4247 new_state == IEEE80211_STA_ASSOC &&
4248 (vif->type == NL80211_IFTYPE_AP ||
4249 vif->type == NL80211_IFTYPE_MESH_POINT ||
4250 vif->type == NL80211_IFTYPE_ADHOC)) {
4251 ret = ath11k_station_assoc(ar, vif, sta, false);
4252 if (ret)
4253 ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
4254 sta->addr);
85f36923
KP
4255 } else if (old_state == IEEE80211_STA_ASSOC &&
4256 new_state == IEEE80211_STA_AUTHORIZED) {
4257 spin_lock_bh(&ar->ab->base_lock);
4258
4259 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4260 if (peer)
4261 peer->is_authorized = true;
4262
4263 spin_unlock_bh(&ar->ab->base_lock);
4264
4265 if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
4266 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4267 arvif->vdev_id,
4268 WMI_PEER_AUTHORIZE,
4269 1);
4270 if (ret)
4271 ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
4272 sta->addr, arvif->vdev_id, ret);
4273 }
4274 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
4275 new_state == IEEE80211_STA_ASSOC) {
4276 spin_lock_bh(&ar->ab->base_lock);
4277
4278 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4279 if (peer)
4280 peer->is_authorized = false;
4281
4282 spin_unlock_bh(&ar->ab->base_lock);
d5c65159
KV
4283 } else if (old_state == IEEE80211_STA_ASSOC &&
4284 new_state == IEEE80211_STA_AUTH &&
4285 (vif->type == NL80211_IFTYPE_AP ||
4286 vif->type == NL80211_IFTYPE_MESH_POINT ||
4287 vif->type == NL80211_IFTYPE_ADHOC)) {
4288 ret = ath11k_station_disassoc(ar, vif, sta);
4289 if (ret)
4290 ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
4291 sta->addr);
d5c65159
KV
4292 }
4293
d5c65159
KV
4294 mutex_unlock(&ar->conf_mutex);
4295 return ret;
4296}
4297
64f1d7e9
MK
4298static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
4299 struct ieee80211_vif *vif,
4300 struct ieee80211_sta *sta)
4301{
4302 struct ath11k *ar = hw->priv;
4303 struct ath11k_vif *arvif = (void *)vif->drv_priv;
4304 int ret = 0;
4305 s16 txpwr;
4306
4307 if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
4308 txpwr = 0;
4309 } else {
4310 txpwr = sta->txpwr.power;
4311 if (!txpwr)
4312 return -EINVAL;
4313 }
4314
4315 if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
4316 return -EINVAL;
4317
4318 mutex_lock(&ar->conf_mutex);
4319
4320 ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4321 WMI_PEER_USE_FIXED_PWR, txpwr);
4322 if (ret) {
4323 ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
4324 ret);
4325 goto out;
4326 }
4327
4328out:
4329 mutex_unlock(&ar->conf_mutex);
4330 return ret;
4331}
4332
34c67dc3
SM
4333static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
4334 struct ieee80211_vif *vif,
4335 struct ieee80211_sta *sta, bool enabled)
4336{
4337 struct ath11k *ar = hw->priv;
4338 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4339
4340 if (enabled && !arsta->use_4addr_set) {
4341 ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
4342 arsta->use_4addr_set = true;
4343 }
4344}
4345
d5c65159
KV
4346static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
4347 struct ieee80211_vif *vif,
4348 struct ieee80211_sta *sta,
4349 u32 changed)
4350{
4351 struct ath11k *ar = hw->priv;
4352 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4353 struct ath11k_vif *arvif = (void *)vif->drv_priv;
4354 struct ath11k_peer *peer;
4355 u32 bw, smps;
4356
4357 spin_lock_bh(&ar->ab->base_lock);
4358
4359 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4360 if (!peer) {
4361 spin_unlock_bh(&ar->ab->base_lock);
4362 ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
4363 sta->addr, arvif->vdev_id);
4364 return;
4365 }
4366
4367 spin_unlock_bh(&ar->ab->base_lock);
4368
4369 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4370 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4371 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4372 sta->smps_mode);
4373
4374 spin_lock_bh(&ar->data_lock);
4375
4376 if (changed & IEEE80211_RC_BW_CHANGED) {
4377 bw = WMI_PEER_CHWIDTH_20MHZ;
4378
4379 switch (sta->bandwidth) {
4380 case IEEE80211_STA_RX_BW_20:
4381 bw = WMI_PEER_CHWIDTH_20MHZ;
4382 break;
4383 case IEEE80211_STA_RX_BW_40:
4384 bw = WMI_PEER_CHWIDTH_40MHZ;
4385 break;
4386 case IEEE80211_STA_RX_BW_80:
4387 bw = WMI_PEER_CHWIDTH_80MHZ;
4388 break;
4389 case IEEE80211_STA_RX_BW_160:
4390 bw = WMI_PEER_CHWIDTH_160MHZ;
4391 break;
4392 default:
4393 ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
4394 sta->bandwidth, sta->addr);
4395 bw = WMI_PEER_CHWIDTH_20MHZ;
4396 break;
4397 }
4398
4399 arsta->bw = bw;
4400 }
4401
4402 if (changed & IEEE80211_RC_NSS_CHANGED)
4403 arsta->nss = sta->rx_nss;
4404
4405 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4406 smps = WMI_PEER_SMPS_PS_NONE;
4407
4408 switch (sta->smps_mode) {
4409 case IEEE80211_SMPS_AUTOMATIC:
4410 case IEEE80211_SMPS_OFF:
4411 smps = WMI_PEER_SMPS_PS_NONE;
4412 break;
4413 case IEEE80211_SMPS_STATIC:
4414 smps = WMI_PEER_SMPS_STATIC;
4415 break;
4416 case IEEE80211_SMPS_DYNAMIC:
4417 smps = WMI_PEER_SMPS_DYNAMIC;
4418 break;
4419 default:
4420 ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
4421 sta->smps_mode, sta->addr);
4422 smps = WMI_PEER_SMPS_PS_NONE;
4423 break;
4424 }
4425
4426 arsta->smps = smps;
4427 }
4428
4429 arsta->changed |= changed;
4430
4431 spin_unlock_bh(&ar->data_lock);
4432
4433 ieee80211_queue_work(hw, &arsta->update_wk);
4434}
4435
4436static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
4437 u16 ac, bool enable)
4438{
4439 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4440 u32 value = 0;
4441 int ret = 0;
4442
4443 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4444 return 0;
4445
4446 switch (ac) {
4447 case IEEE80211_AC_VO:
4448 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4449 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4450 break;
4451 case IEEE80211_AC_VI:
4452 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4453 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4454 break;
4455 case IEEE80211_AC_BE:
4456 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4457 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4458 break;
4459 case IEEE80211_AC_BK:
4460 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4461 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4462 break;
4463 }
4464
4465 if (enable)
4466 arvif->u.sta.uapsd |= value;
4467 else
4468 arvif->u.sta.uapsd &= ~value;
4469
4470 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4471 WMI_STA_PS_PARAM_UAPSD,
4472 arvif->u.sta.uapsd);
4473 if (ret) {
4474 ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
4475 goto exit;
4476 }
4477
4478 if (arvif->u.sta.uapsd)
4479 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4480 else
4481 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4482
4483 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4484 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4485 value);
4486 if (ret)
4487 ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
4488
4489exit:
4490 return ret;
4491}
4492
4493static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
4494 struct ieee80211_vif *vif, u16 ac,
4495 const struct ieee80211_tx_queue_params *params)
4496{
4497 struct ath11k *ar = hw->priv;
4498 struct ath11k_vif *arvif = (void *)vif->drv_priv;
4499 struct wmi_wmm_params_arg *p = NULL;
4500 int ret;
4501
4502 mutex_lock(&ar->conf_mutex);
4503
4504 switch (ac) {
4505 case IEEE80211_AC_VO:
4506 p = &arvif->wmm_params.ac_vo;
4507 break;
4508 case IEEE80211_AC_VI:
4509 p = &arvif->wmm_params.ac_vi;
4510 break;
4511 case IEEE80211_AC_BE:
4512 p = &arvif->wmm_params.ac_be;
4513 break;
4514 case IEEE80211_AC_BK:
4515 p = &arvif->wmm_params.ac_bk;
4516 break;
4517 }
4518
4519 if (WARN_ON(!p)) {
4520 ret = -EINVAL;
4521 goto exit;
4522 }
4523
4524 p->cwmin = params->cw_min;
4525 p->cwmax = params->cw_max;
4526 p->aifs = params->aifs;
f425078b 4527 p->txop = params->txop;
d5c65159
KV
4528
4529 ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
4530 &arvif->wmm_params);
4531 if (ret) {
4532 ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
4533 goto exit;
4534 }
4535
4536 ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4537
4538 if (ret)
4539 ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
4540
4541exit:
4542 mutex_unlock(&ar->conf_mutex);
4543 return ret;
4544}
4545
4546static struct ieee80211_sta_ht_cap
4547ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
4548{
4549 int i;
4550 struct ieee80211_sta_ht_cap ht_cap = {0};
4551 u32 ar_vht_cap = ar->pdev->cap.vht_cap;
4552
4553 if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
4554 return ht_cap;
4555
4556 ht_cap.ht_supported = 1;
4557 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
c000e56e 4558 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
d5c65159
KV
4559 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4560 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4561 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4562
4563 if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
4564 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4565
4566 if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
4567 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4568
4569 if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
4570 u32 smps;
4571
4572 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4573 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4574
4575 ht_cap.cap |= smps;
4576 }
4577
4578 if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
4579 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4580
4581 if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
4582 u32 stbc;
4583
4584 stbc = ar_ht_cap;
4585 stbc &= WMI_HT_CAP_RX_STBC;
4586 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4587 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4588 stbc &= IEEE80211_HT_CAP_RX_STBC;
4589
4590 ht_cap.cap |= stbc;
4591 }
4592
4593 if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
4594 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4595
4596 if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
4597 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4598
4599 if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4600 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4601
4602 for (i = 0; i < ar->num_rx_chains; i++) {
4603 if (rate_cap_rx_chainmask & BIT(i))
4604 ht_cap.mcs.rx_mask[i] = 0xFF;
4605 }
4606
4607 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4608
4609 return ht_cap;
4610}
4611
4612static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
4613{
4614 u32 value = 0;
4615 struct ath11k *ar = arvif->ar;
4616 int nsts;
4617 int sound_dim;
4618 u32 vht_cap = ar->pdev->cap.vht_cap;
4619 u32 vdev_param = WMI_VDEV_PARAM_TXBF;
4620
4621 if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
4622 nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4623 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4624 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4625 }
4626
4627 if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
4628 sound_dim = vht_cap &
4629 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4630 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4631 if (sound_dim > (ar->num_tx_chains - 1))
4632 sound_dim = ar->num_tx_chains - 1;
4633 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4634 }
4635
4636 if (!value)
4637 return 0;
4638
4639 if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
4640 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4641
4642 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
4643 arvif->vdev_type == WMI_VDEV_TYPE_AP)
4644 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
4645 }
4646
4647 /* TODO: SUBFEE not validated in HK, disable here until validated? */
4648
4649 if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
4650 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4651
4652 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
4653 arvif->vdev_type == WMI_VDEV_TYPE_STA)
4654 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
4655 }
4656
4657 return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4658 vdev_param, value);
4659}
4660
4661static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
4662{
4663 bool subfer, subfee;
4664 int sound_dim = 0;
4665
4666 subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
4667 subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
4668
4669 if (ar->num_tx_chains < 2) {
4670 *vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
4671 subfer = false;
4672 }
4673
4674 /* If SU Beaformer is not set, then disable MU Beamformer Capability */
4675 if (!subfer)
4676 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
4677
4678 /* If SU Beaformee is not set, then disable MU Beamformee Capability */
4679 if (!subfee)
4680 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
4681
4682 sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4683 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4684 *vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4685
4686 /* TODO: Need to check invalid STS and Sound_dim values set by FW? */
4687
4688 /* Enable Sounding Dimension Field only if SU BF is enabled */
4689 if (subfer) {
4690 if (sound_dim > (ar->num_tx_chains - 1))
4691 sound_dim = ar->num_tx_chains - 1;
4692
4693 sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4694 sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4695 *vht_cap |= sound_dim;
4696 }
4697
4698 /* Use the STS advertised by FW unless SU Beamformee is not supported*/
4699 if (!subfee)
4700 *vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
4701}
4702
4703static struct ieee80211_sta_vht_cap
4704ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
4705 u32 rate_cap_rx_chainmask)
4706{
4707 struct ieee80211_sta_vht_cap vht_cap = {0};
4708 u16 txmcs_map, rxmcs_map;
4709 int i;
4710
4711 vht_cap.vht_supported = 1;
4712 vht_cap.cap = ar->pdev->cap.vht_cap;
4713
78406044
WG
4714 if (ar->pdev->cap.nss_ratio_enabled)
4715 vht_cap.vht_mcs.tx_highest |=
4716 cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
4717
d5c65159
KV
4718 ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
4719
d5c65159
KV
4720 rxmcs_map = 0;
4721 txmcs_map = 0;
4722 for (i = 0; i < 8; i++) {
4723 if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
4724 txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4725 else
4726 txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4727
4728 if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
4729 rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4730 else
4731 rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4732 }
4733
4734 if (rate_cap_tx_chainmask <= 1)
4735 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4736
4737 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
4738 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
4739
4740 return vht_cap;
4741}
4742
4743static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
4744 struct ath11k_pdev_cap *cap,
4745 u32 *ht_cap_info)
4746{
4747 struct ieee80211_supported_band *band;
4748 u32 rate_cap_tx_chainmask;
4749 u32 rate_cap_rx_chainmask;
4750 u32 ht_cap;
4751
4752 rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
4753 rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
4754
4755 if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4756 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4757 ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
4758 if (ht_cap_info)
4759 *ht_cap_info = ht_cap;
4760 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
4761 rate_cap_rx_chainmask);
4762 }
4763
54f40f55
WG
4764 if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4765 (ar->ab->hw_params.single_pdev_only ||
4766 !ar->supports_6ghz)) {
d5c65159
KV
4767 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4768 ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
4769 if (ht_cap_info)
4770 *ht_cap_info = ht_cap;
4771 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
4772 rate_cap_rx_chainmask);
4773 band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
4774 rate_cap_rx_chainmask);
4775 }
4776}
4777
4778static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
4779{
4780 /* TODO: Check the request chainmask against the supported
4781 * chainmask table which is advertised in extented_service_ready event
4782 */
4783
4784 return 0;
4785}
4786
9f056ed8
JC
4787static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
4788 u8 *he_ppet)
4789{
4790 int nss, ru;
4791 u8 bit = 7;
4792
4793 he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
4794 he_ppet[0] |= (fw_ppet->ru_bit_mask <<
4795 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
4796 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
4797 for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4798 for (ru = 0; ru < 4; ru++) {
4799 u8 val;
4800 int i;
4801
4802 if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4803 continue;
4804 val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
4805 0x3f;
4806 val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
4807 for (i = 5; i >= 0; i--) {
4808 he_ppet[bit / 8] |=
4809 ((val >> i) & 0x1) << ((bit % 8));
4810 bit++;
4811 }
4812 }
4813 }
4814}
4815
13591a1c
SE
4816static void
4817ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
4818{
4819 u8 m;
4820
4821 m = IEEE80211_HE_MAC_CAP0_TWT_RES |
4822 IEEE80211_HE_MAC_CAP0_TWT_REQ;
4823 he_cap_elem->mac_cap_info[0] &= ~m;
4824
4825 m = IEEE80211_HE_MAC_CAP2_TRS |
4826 IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4827 IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4828 he_cap_elem->mac_cap_info[2] &= ~m;
4829
4830 m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
4831 IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4832 IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4833 he_cap_elem->mac_cap_info[3] &= ~m;
4834
4835 m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
4836 IEEE80211_HE_MAC_CAP4_BQR;
4837 he_cap_elem->mac_cap_info[4] &= ~m;
4838
76cf4221 4839 m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
13591a1c
SE
4840 IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
4841 IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
4842 IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
4843 he_cap_elem->mac_cap_info[5] &= ~m;
4844
4845 m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4846 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
4847 he_cap_elem->phy_cap_info[2] &= ~m;
4848
76cf4221 4849 m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
13591a1c
SE
4850 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
4851 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
4852 he_cap_elem->phy_cap_info[3] &= ~m;
4853
4854 m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
4855 he_cap_elem->phy_cap_info[4] &= ~m;
4856
4857 m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
4858 he_cap_elem->phy_cap_info[5] &= ~m;
4859
4860 m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
76cf4221 4861 IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
13591a1c
SE
4862 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
4863 IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
4864 he_cap_elem->phy_cap_info[6] &= ~m;
4865
76cf4221
JB
4866 m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
4867 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
13591a1c
SE
4868 IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
4869 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
4870 he_cap_elem->phy_cap_info[7] &= ~m;
4871
4872 m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
4873 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
4874 IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
4875 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
4876 he_cap_elem->phy_cap_info[8] &= ~m;
4877
4878 m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
4879 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
4880 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
4881 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
4882 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
4883 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
4884 he_cap_elem->phy_cap_info[9] &= ~m;
4885}
4886
f28b7b78
RM
4887static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
4888 struct ath11k_band_cap *bcap)
4889{
4890 u8 val;
4891
4892 bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
4893 if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
4894 bcap->he_6ghz_capa |=
4895 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
4896 WLAN_HT_CAP_SM_PS_DYNAMIC);
4897 else
4898 bcap->he_6ghz_capa |=
4899 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
4900 WLAN_HT_CAP_SM_PS_DISABLED);
4901 val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
4902 pcap->vht_cap);
4903 bcap->he_6ghz_capa |=
4904 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
4905 val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
4906 bcap->he_6ghz_capa |=
4907 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
4908 if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
4909 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
4910 if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
4911 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
4912
4913 return cpu_to_le16(bcap->he_6ghz_capa);
4914}
4915
9f056ed8
JC
4916static int ath11k_mac_copy_he_cap(struct ath11k *ar,
4917 struct ath11k_pdev_cap *cap,
4918 struct ieee80211_sband_iftype_data *data,
4919 int band)
4920{
4921 int i, idx = 0;
4922
4923 for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
4924 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
4925 struct ath11k_band_cap *band_cap = &cap->band[band];
4926 struct ieee80211_he_cap_elem *he_cap_elem =
4927 &he_cap->he_cap_elem;
4928
4929 switch (i) {
4930 case NL80211_IFTYPE_STATION:
4931 case NL80211_IFTYPE_AP:
13591a1c 4932 case NL80211_IFTYPE_MESH_POINT:
9f056ed8
JC
4933 break;
4934
4935 default:
4936 continue;
4937 }
4938
4939 data[idx].types_mask = BIT(i);
4940 he_cap->has_he = true;
4941 memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
4942 sizeof(he_cap_elem->mac_cap_info));
4943 memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
4944 sizeof(he_cap_elem->phy_cap_info));
4945
9529cba9 4946 he_cap_elem->mac_cap_info[1] &=
9f056ed8 4947 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
9f056ed8
JC
4948
4949 he_cap_elem->phy_cap_info[5] &=
4950 ~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
9f056ed8
JC
4951 he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
4952
4953 switch (i) {
4954 case NL80211_IFTYPE_AP:
37b76986
MS
4955 he_cap_elem->phy_cap_info[3] &=
4956 ~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
9f056ed8
JC
4957 he_cap_elem->phy_cap_info[9] |=
4958 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4959 break;
4960 case NL80211_IFTYPE_STATION:
4961 he_cap_elem->mac_cap_info[0] &=
4962 ~IEEE80211_HE_MAC_CAP0_TWT_RES;
4963 he_cap_elem->mac_cap_info[0] |=
4964 IEEE80211_HE_MAC_CAP0_TWT_REQ;
4965 he_cap_elem->phy_cap_info[9] |=
4966 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4967 break;
13591a1c
SE
4968 case NL80211_IFTYPE_MESH_POINT:
4969 ath11k_mac_filter_he_cap_mesh(he_cap_elem);
4970 break;
9f056ed8
JC
4971 }
4972
4973 he_cap->he_mcs_nss_supp.rx_mcs_80 =
4974 cpu_to_le16(band_cap->he_mcs & 0xffff);
4975 he_cap->he_mcs_nss_supp.tx_mcs_80 =
4976 cpu_to_le16(band_cap->he_mcs & 0xffff);
4977 he_cap->he_mcs_nss_supp.rx_mcs_160 =
4978 cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4979 he_cap->he_mcs_nss_supp.tx_mcs_160 =
4980 cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4981 he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
4982 cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4983 he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
4984 cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4985
4986 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4987 if (he_cap_elem->phy_cap_info[6] &
4988 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4989 ath11k_gen_ppe_thresh(&band_cap->he_ppet,
4990 he_cap->ppe_thres);
f28b7b78
RM
4991
4992 if (band == NL80211_BAND_6GHZ) {
4993 data[idx].he_6ghz_capa.capa =
4994 ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
4995 }
9f056ed8
JC
4996 idx++;
4997 }
4998
4999 return idx;
5000}
5001
5002static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5003 struct ath11k_pdev_cap *cap)
5004{
3b451683
CIK
5005 struct ieee80211_supported_band *band;
5006 int count;
9f056ed8
JC
5007
5008 if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5009 count = ath11k_mac_copy_he_cap(ar, cap,
5010 ar->mac.iftype[NL80211_BAND_2GHZ],
5011 NL80211_BAND_2GHZ);
5012 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5013 band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
3b451683 5014 band->n_iftype_data = count;
9f056ed8
JC
5015 }
5016
5017 if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5018 count = ath11k_mac_copy_he_cap(ar, cap,
5019 ar->mac.iftype[NL80211_BAND_5GHZ],
5020 NL80211_BAND_5GHZ);
5021 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5022 band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
3b451683 5023 band->n_iftype_data = count;
9f056ed8 5024 }
22eeadcd
PKC
5025
5026 if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5027 ar->supports_6ghz) {
5028 count = ath11k_mac_copy_he_cap(ar, cap,
5029 ar->mac.iftype[NL80211_BAND_6GHZ],
5030 NL80211_BAND_6GHZ);
5031 band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5032 band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
5033 band->n_iftype_data = count;
5034 }
9f056ed8
JC
5035}
5036
d5c65159
KV
5037static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5038{
5039 int ret;
5040
5041 lockdep_assert_held(&ar->conf_mutex);
5042
5043 if (ath11k_check_chain_mask(ar, tx_ant, true))
5044 return -EINVAL;
5045
5046 if (ath11k_check_chain_mask(ar, rx_ant, false))
5047 return -EINVAL;
5048
5049 ar->cfg_tx_chainmask = tx_ant;
5050 ar->cfg_rx_chainmask = rx_ant;
5051
5052 if (ar->state != ATH11K_STATE_ON &&
5053 ar->state != ATH11K_STATE_RESTARTED)
5054 return 0;
5055
5056 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5057 tx_ant, ar->pdev->pdev_id);
5058 if (ret) {
5059 ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5060 ret, tx_ant);
5061 return ret;
5062 }
5063
a3c5195a
S
5064 ar->num_tx_chains = get_num_chains(tx_ant);
5065
d5c65159
KV
5066 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5067 rx_ant, ar->pdev->pdev_id);
5068 if (ret) {
5069 ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5070 ret, rx_ant);
5071 return ret;
5072 }
5073
a3c5195a
S
5074 ar->num_rx_chains = get_num_chains(rx_ant);
5075
9f056ed8 5076 /* Reload HT/VHT/HE capability */
d5c65159 5077 ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9f056ed8 5078 ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
d5c65159
KV
5079
5080 return 0;
5081}
5082
5083int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
5084{
e7f33e0c 5085 struct sk_buff *msdu = skb;
52b776fa 5086 struct ieee80211_tx_info *info;
d5c65159
KV
5087 struct ath11k *ar = ctx;
5088 struct ath11k_base *ab = ar->ab;
d5c65159
KV
5089
5090 spin_lock_bh(&ar->txmgmt_idr_lock);
5091 idr_remove(&ar->txmgmt_idr, buf_id);
5092 spin_unlock_bh(&ar->txmgmt_idr_lock);
5093 dma_unmap_single(ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
5094 DMA_TO_DEVICE);
5095
5096 info = IEEE80211_SKB_CB(msdu);
5097 memset(&info->status, 0, sizeof(info->status));
5098
5099 ieee80211_free_txskb(ar->hw, msdu);
5100
5101 return 0;
5102}
5103
5104static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
5105{
5106 struct ieee80211_vif *vif = ctx;
5107 struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
5108 struct sk_buff *msdu = skb;
5109 struct ath11k *ar = skb_cb->ar;
5110 struct ath11k_base *ab = ar->ab;
5111
5112 if (skb_cb->vif == vif) {
5113 spin_lock_bh(&ar->txmgmt_idr_lock);
5114 idr_remove(&ar->txmgmt_idr, buf_id);
5115 spin_unlock_bh(&ar->txmgmt_idr_lock);
5116 dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
5117 DMA_TO_DEVICE);
5118 }
5119
5120 return 0;
5121}
5122
5123static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
5124 struct sk_buff *skb)
5125{
5126 struct ath11k_base *ab = ar->ab;
5127 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
e7f33e0c 5128 struct ieee80211_tx_info *info;
d5c65159
KV
5129 dma_addr_t paddr;
5130 int buf_id;
5131 int ret;
5132
5133 spin_lock_bh(&ar->txmgmt_idr_lock);
5134 buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
5135 ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
5136 spin_unlock_bh(&ar->txmgmt_idr_lock);
5137 if (buf_id < 0)
5138 return -ENOSPC;
5139
e7f33e0c 5140 info = IEEE80211_SKB_CB(skb);
cc20ff2c 5141 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
e7f33e0c
JC
5142 if ((ieee80211_is_action(hdr->frame_control) ||
5143 ieee80211_is_deauth(hdr->frame_control) ||
5144 ieee80211_is_disassoc(hdr->frame_control)) &&
5145 ieee80211_has_protected(hdr->frame_control)) {
5146 skb_put(skb, IEEE80211_CCMP_MIC_LEN);
5147 }
d5c65159
KV
5148 }
5149
5150 paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
5151 if (dma_mapping_error(ab->dev, paddr)) {
5152 ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
5153 ret = -EIO;
5154 goto err_free_idr;
5155 }
5156
5157 ATH11K_SKB_CB(skb)->paddr = paddr;
5158
5159 ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
5160 if (ret) {
5161 ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
5162 goto err_unmap_buf;
5163 }
5164
5165 return 0;
5166
5167err_unmap_buf:
5168 dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
5169 skb->len, DMA_TO_DEVICE);
5170err_free_idr:
5171 spin_lock_bh(&ar->txmgmt_idr_lock);
5172 idr_remove(&ar->txmgmt_idr, buf_id);
5173 spin_unlock_bh(&ar->txmgmt_idr_lock);
5174
5175 return ret;
5176}
5177
5178static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
5179{
5180 struct sk_buff *skb;
5181
5182 while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
5183 ieee80211_free_txskb(ar->hw, skb);
5184}
5185
5186static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
5187{
5188 struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
f4d291b4 5189 struct ath11k_skb_cb *skb_cb;
d5c65159
KV
5190 struct ath11k_vif *arvif;
5191 struct sk_buff *skb;
5192 int ret;
5193
5194 while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
f4d291b4
SE
5195 skb_cb = ATH11K_SKB_CB(skb);
5196 if (!skb_cb->vif) {
5197 ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
d5c65159 5198 ieee80211_free_txskb(ar->hw, skb);
66307ca0
SM
5199 continue;
5200 }
5201
f4d291b4 5202 arvif = ath11k_vif_to_arvif(skb_cb->vif);
66307ca0
SM
5203 if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
5204 arvif->is_started) {
5205 ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
5206 if (ret) {
5207 ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
5208 arvif->vdev_id, ret);
5209 ieee80211_free_txskb(ar->hw, skb);
5210 } else {
5211 atomic_inc(&ar->num_pending_mgmt_tx);
5212 }
d5c65159 5213 } else {
66307ca0 5214 ath11k_warn(ar->ab,
f4d291b4
SE
5215 "dropping mgmt frame for vdev %d, is_started %d\n",
5216 arvif->vdev_id,
66307ca0
SM
5217 arvif->is_started);
5218 ieee80211_free_txskb(ar->hw, skb);
d5c65159
KV
5219 }
5220 }
5221}
5222
5223static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
5224 bool is_prb_rsp)
5225{
5226 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
5227
5228 if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
5229 return -ESHUTDOWN;
5230
5231 /* Drop probe response packets when the pending management tx
5232 * count has reached a certain threshold, so as to prioritize
5233 * other mgmt packets like auth and assoc to be sent on time
5234 * for establishing successful connections.
5235 */
5236 if (is_prb_rsp &&
5237 atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
5238 ath11k_warn(ar->ab,
5239 "dropping probe response as pending queue is almost full\n");
5240 return -ENOSPC;
5241 }
5242
3808a180 5243 if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
d5c65159
KV
5244 ath11k_warn(ar->ab, "mgmt tx queue is full\n");
5245 return -ENOSPC;
5246 }
5247
5248 skb_queue_tail(q, skb);
5249 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
5250
5251 return 0;
5252}
5253
5254static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
5255 struct ieee80211_tx_control *control,
5256 struct sk_buff *skb)
5257{
e7f33e0c 5258 struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
d5c65159
KV
5259 struct ath11k *ar = hw->priv;
5260 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5261 struct ieee80211_vif *vif = info->control.vif;
5262 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5263 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
f4d291b4 5264 struct ieee80211_key_conf *key = info->control.hw_key;
e20cfa3b 5265 struct ath11k_sta *arsta = NULL;
f4d291b4 5266 u32 info_flags = info->flags;
d5c65159
KV
5267 bool is_prb_rsp;
5268 int ret;
5269
5da7acfe 5270 memset(skb_cb, 0, sizeof(*skb_cb));
f4d291b4
SE
5271 skb_cb->vif = vif;
5272
5273 if (key) {
5274 skb_cb->cipher = key->cipher;
5275 skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
5276 }
5277
5278 if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
e7f33e0c
JC
5279 skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
5280 } else if (ieee80211_is_mgmt(hdr->frame_control)) {
d5c65159
KV
5281 is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
5282 ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
5283 if (ret) {
5284 ath11k_warn(ar->ab, "failed to queue management frame %d\n",
5285 ret);
5286 ieee80211_free_txskb(ar->hw, skb);
5287 }
5288 return;
5289 }
5290
e20cfa3b
KP
5291 if (control->sta)
5292 arsta = (struct ath11k_sta *)control->sta->drv_priv;
5293
5294 ret = ath11k_dp_tx(ar, arvif, arsta, skb);
bcef57ea 5295 if (unlikely(ret)) {
d5c65159
KV
5296 ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
5297 ieee80211_free_txskb(ar->hw, skb);
5298 }
5299}
5300
5301void ath11k_mac_drain_tx(struct ath11k *ar)
5302{
5303 /* make sure rcu-protected mac80211 tx path itself is drained */
5304 synchronize_net();
5305
5306 cancel_work_sync(&ar->wmi_mgmt_tx_work);
5307 ath11k_mgmt_over_wmi_tx_purge(ar);
5308}
5309
5310static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
5311{
5312 struct htt_rx_ring_tlv_filter tlv_filter = {0};
4152e420
CH
5313 struct ath11k_base *ab = ar->ab;
5314 int i, ret = 0;
d5c65159
KV
5315 u32 ring_id;
5316
ec48d28b 5317 if (enable) {
d5c65159 5318 tlv_filter = ath11k_mac_mon_status_filter_default;
11af6de4
MK
5319 if (ath11k_debugfs_rx_filter(ar))
5320 tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
ec48d28b 5321 }
d5c65159 5322
4152e420
CH
5323 for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
5324 ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
5325 ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
5326 ar->dp.mac_id + i,
5327 HAL_RXDMA_MONITOR_STATUS,
5328 DP_RX_BUFFER_SIZE,
5329 &tlv_filter);
5330 }
d5c65159 5331
840c36fa
CH
5332 if (enable && !ar->ab->hw_params.rxdma1_enable)
5333 mod_timer(&ar->ab->mon_reap_timer, jiffies +
5334 msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
5335
4152e420 5336 return ret;
d5c65159
KV
5337}
5338
5339static int ath11k_mac_op_start(struct ieee80211_hw *hw)
5340{
5341 struct ath11k *ar = hw->priv;
5342 struct ath11k_base *ab = ar->ab;
5343 struct ath11k_pdev *pdev = ar->pdev;
5344 int ret;
5345
5346 ath11k_mac_drain_tx(ar);
5347 mutex_lock(&ar->conf_mutex);
5348
5349 switch (ar->state) {
5350 case ATH11K_STATE_OFF:
5351 ar->state = ATH11K_STATE_ON;
5352 break;
5353 case ATH11K_STATE_RESTARTING:
5354 ar->state = ATH11K_STATE_RESTARTED;
5355 break;
5356 case ATH11K_STATE_RESTARTED:
5357 case ATH11K_STATE_WEDGED:
5358 case ATH11K_STATE_ON:
5359 WARN_ON(1);
5360 ret = -EINVAL;
5361 goto err;
5362 }
5363
5364 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
5365 1, pdev->pdev_id);
5366
5367 if (ret) {
5368 ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
5369 goto err;
5370 }
5371
5372 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
5373 pdev->pdev_id);
5374 if (ret) {
5375 ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
5376 goto err;
5377 }
5378
5379 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
5380 0, pdev->pdev_id);
5381 if (ret) {
5382 ath11k_err(ab, "failed to set ac override for ARP: %d\n",
5383 ret);
5384 goto err;
5385 }
5386
5387 ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
5388 if (ret) {
5389 ath11k_err(ab, "failed to offload radar detection: %d\n",
5390 ret);
5391 goto err;
5392 }
5393
5394 ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
5395 HTT_PPDU_STATS_TAG_DEFAULT);
5396 if (ret) {
5397 ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
5398 goto err;
5399 }
5400
5401 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
5402 1, pdev->pdev_id);
5403
5404 if (ret) {
5405 ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
5406 goto err;
5407 }
5408
5409 __ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5410
5411 /* TODO: Do we need to enable ANI? */
5412
5413 ath11k_reg_update_chan_list(ar);
5414
5415 ar->num_started_vdevs = 0;
5416 ar->num_created_vdevs = 0;
5417 ar->num_peers = 0;
79c080db 5418 ar->allocated_vdev_map = 0;
d5c65159
KV
5419
5420 /* Configure monitor status ring with default rx_filter to get rx status
5421 * such as rssi, rx_duration.
5422 */
5423 ret = ath11k_mac_config_mon_status_default(ar, true);
5424 if (ret) {
5425 ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
5426 ret);
5427 goto err;
5428 }
5429
26c79927
S
5430 /* Configure the hash seed for hash based reo dest ring selection */
5431 ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
5432
c83c500b
CH
5433 /* allow device to enter IMPS */
5434 if (ab->hw_params.idle_ps) {
5435 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
5436 1, pdev->pdev_id);
5437 if (ret) {
5438 ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
5439 goto err;
5440 }
5441 }
c202e2eb
DC
5442
5443 mutex_unlock(&ar->conf_mutex);
5444
5445 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
5446 &ab->pdevs[ar->pdev_idx]);
5447
d5c65159
KV
5448 return 0;
5449
5450err:
5451 ar->state = ATH11K_STATE_OFF;
5452 mutex_unlock(&ar->conf_mutex);
5453
5454 return ret;
5455}
5456
5457static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
5458{
5459 struct ath11k *ar = hw->priv;
5460 struct htt_ppdu_stats_info *ppdu_stats, *tmp;
5461 int ret;
5462
5463 ath11k_mac_drain_tx(ar);
5464
5465 mutex_lock(&ar->conf_mutex);
5466 ret = ath11k_mac_config_mon_status_default(ar, false);
5467 if (ret)
5468 ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
5469 ret);
5470
5471 clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5472 ar->state = ATH11K_STATE_OFF;
5473 mutex_unlock(&ar->conf_mutex);
5474
5475 cancel_delayed_work_sync(&ar->scan.timeout);
5476 cancel_work_sync(&ar->regd_update_work);
5477
5478 spin_lock_bh(&ar->data_lock);
5479 list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
5480 list_del(&ppdu_stats->list);
5481 kfree(ppdu_stats);
5482 }
5483 spin_unlock_bh(&ar->data_lock);
5484
5485 rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
5486
5487 synchronize_rcu();
5488
5489 atomic_set(&ar->num_pending_mgmt_tx, 0);
5490}
5491
5492static void
5493ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
5494 struct vdev_create_params *params)
5495{
5496 struct ath11k *ar = arvif->ar;
5497 struct ath11k_pdev *pdev = ar->pdev;
5498
5499 params->if_id = arvif->vdev_id;
5500 params->type = arvif->vdev_type;
5501 params->subtype = arvif->vdev_subtype;
5502 params->pdev_id = pdev->pdev_id;
5503
5504 if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
5505 params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
5506 params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
5507 }
5508 if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
5509 params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
5510 params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
5511 }
22eeadcd
PKC
5512 if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
5513 ar->supports_6ghz) {
5514 params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
5515 params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
5516 }
d5c65159
KV
5517}
5518
5519static u32
5520ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
5521{
5522 struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
5523 struct ath11k_band_cap *cap_band = NULL;
5524 u32 *hecap_phy_ptr = NULL;
5525 u32 hemode = 0;
5526
5527 if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
5528 cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
5529 else
5530 cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
5531
5532 hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
5533
5534 hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
fcaf49d0
JC
5535 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
5536 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
d5c65159
KV
5537
5538 /* TODO WDS and other modes */
5539 if (viftype == NL80211_IFTYPE_AP) {
5540 hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
fcaf49d0
JC
5541 HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
5542 FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
5543 FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
d5c65159
KV
5544 } else {
5545 hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
5546 }
5547
5548 return hemode;
5549}
5550
5551static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
5552 struct ath11k_vif *arvif)
5553{
5554 u32 param_id, param_value;
5555 struct ath11k_base *ab = ar->ab;
5556 int ret = 0;
5557
5558 param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
5559 param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
5560 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5561 param_id, param_value);
5562 if (ret) {
5563 ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
5564 arvif->vdev_id, ret, param_value);
5565 return ret;
5566 }
5567 param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
5568 param_value =
5569 FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
5570 FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
5571 HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
5572 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5573 param_id, param_value);
5574 if (ret) {
5575 ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
5576 arvif->vdev_id, ret);
5577 return ret;
5578 }
5579 return ret;
5580}
5581
6aea26ce 5582static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
cd19836a 5583 struct ieee80211_vif *vif)
6aea26ce
FF
5584{
5585 struct ath11k *ar = hw->priv;
5586 struct ath11k_base *ab = ar->ab;
5587 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5588 u32 param_id, param_value;
5589 int ret;
5590
5591 param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
5592 if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
5593 (vif->type != NL80211_IFTYPE_STATION &&
5594 vif->type != NL80211_IFTYPE_AP))
2167fa60
S
5595 vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
5596 IEEE80211_OFFLOAD_DECAP_ENABLED);
6aea26ce
FF
5597
5598 if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
5599 param_value = ATH11K_HW_TXRX_ETHERNET;
5600 else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
5601 param_value = ATH11K_HW_TXRX_RAW;
5602 else
5603 param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
5604
5605 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5606 param_id, param_value);
5607 if (ret) {
5608 ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
5609 arvif->vdev_id, ret);
5610 vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
5611 }
2167fa60
S
5612
5613 param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
5614 if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
5615 param_value = ATH11K_HW_TXRX_ETHERNET;
5616 else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
5617 param_value = ATH11K_HW_TXRX_RAW;
5618 else
5619 param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
5620
5621 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5622 param_id, param_value);
5623 if (ret) {
5624 ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
5625 arvif->vdev_id, ret);
5626 vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
5627 }
6aea26ce
FF
5628}
5629
d5c65159
KV
5630static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
5631 struct ieee80211_vif *vif)
5632{
5633 struct ath11k *ar = hw->priv;
5634 struct ath11k_base *ab = ar->ab;
5635 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5636 struct vdev_create_params vdev_param = {0};
5637 struct peer_create_params peer_param;
5638 u32 param_id, param_value;
5639 u16 nss;
5640 int i;
4ea03443 5641 int ret, fbret;
d5c65159
KV
5642 int bit;
5643
5644 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5645
5646 mutex_lock(&ar->conf_mutex);
5647
5648 if (vif->type == NL80211_IFTYPE_AP &&
5649 ar->num_peers > (ar->max_num_peers - 1)) {
5650 ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
5651 ret = -ENOBUFS;
5652 goto err;
5653 }
5654
5655 if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
689a5e6f
SM
5656 ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
5657 ar->num_created_vdevs, TARGET_NUM_VDEVS);
d5c65159
KV
5658 ret = -EBUSY;
5659 goto err;
5660 }
5661
5662 memset(arvif, 0, sizeof(*arvif));
5663
5664 arvif->ar = ar;
5665 arvif->vif = vif;
5666
5667 INIT_LIST_HEAD(&arvif->list);
26f69792
LS
5668 INIT_DELAYED_WORK(&arvif->connection_loss_work,
5669 ath11k_mac_vif_sta_connection_loss_work);
d5c65159
KV
5670
5671 for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5672 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
61fe43e7 5673 arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
d5c65159
KV
5674 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5675 sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5676 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5677 sizeof(arvif->bitrate_mask.control[i].vht_mcs));
61fe43e7
MH
5678 memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
5679 sizeof(arvif->bitrate_mask.control[i].he_mcs));
d5c65159
KV
5680 }
5681
5682 bit = __ffs64(ab->free_vdev_map);
5683
5684 arvif->vdev_id = bit;
5685 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5686
5687 switch (vif->type) {
5688 case NL80211_IFTYPE_UNSPECIFIED:
5689 case NL80211_IFTYPE_STATION:
5690 arvif->vdev_type = WMI_VDEV_TYPE_STA;
5691 break;
5692 case NL80211_IFTYPE_MESH_POINT:
5693 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
0b294aeb 5694 fallthrough;
d5c65159
KV
5695 case NL80211_IFTYPE_AP:
5696 arvif->vdev_type = WMI_VDEV_TYPE_AP;
5697 break;
5698 case NL80211_IFTYPE_MONITOR:
5699 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
689a5e6f 5700 ar->monitor_vdev_id = bit;
d5c65159
KV
5701 break;
5702 default:
5703 WARN_ON(1);
5704 break;
5705 }
5706
5707 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
5708 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5709 ab->free_vdev_map);
5710
5711 vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
5712 for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5713 vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
5714
5715 ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
5716
5717 ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
5718 if (ret) {
5719 ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
5720 arvif->vdev_id, ret);
5721 goto err;
5722 }
5723
5724 ar->num_created_vdevs++;
657680cc
SM
5725 ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
5726 vif->addr, arvif->vdev_id);
79c080db 5727 ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
d5c65159 5728 ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
79c080db 5729
d5c65159
KV
5730 spin_lock_bh(&ar->data_lock);
5731 list_add(&arvif->list, &ar->arvifs);
5732 spin_unlock_bh(&ar->data_lock);
5733
6aea26ce 5734 ath11k_mac_op_update_vif_offload(hw, vif);
d5c65159
KV
5735
5736 nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
5737 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5738 WMI_VDEV_PARAM_NSS, nss);
5739 if (ret) {
5740 ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
5741 arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
5742 goto err_vdev_del;
5743 }
5744
5745 switch (arvif->vdev_type) {
5746 case WMI_VDEV_TYPE_AP:
5747 peer_param.vdev_id = arvif->vdev_id;
5748 peer_param.peer_addr = vif->addr;
5749 peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
5750 ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
5751 if (ret) {
5752 ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
5753 arvif->vdev_id, ret);
5754 goto err_vdev_del;
5755 }
5756
5757 ret = ath11k_mac_set_kickout(arvif);
5758 if (ret) {
5759 ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
5760 arvif->vdev_id, ret);
5761 goto err_peer_del;
5762 }
5763 break;
5764 case WMI_VDEV_TYPE_STA:
5765 param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5766 param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5767 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5768 param_id, param_value);
5769 if (ret) {
5770 ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
5771 arvif->vdev_id, ret);
5772 goto err_peer_del;
5773 }
5774
5775 param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
5776 param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
5777 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5778 param_id, param_value);
5779 if (ret) {
5780 ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
5781 arvif->vdev_id, ret);
5782 goto err_peer_del;
5783 }
5784
5785 param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
5786 param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
5787 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5788 param_id, param_value);
5789 if (ret) {
5790 ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
5791 arvif->vdev_id, ret);
5792 goto err_peer_del;
5793 }
6bfebd4b 5794
af3d8964
KV
5795 ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
5796 WMI_STA_PS_MODE_DISABLED);
6bfebd4b
JC
5797 if (ret) {
5798 ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
5799 arvif->vdev_id, ret);
5800 goto err_peer_del;
5801 }
d5c65159 5802 break;
689a5e6f
SM
5803 case WMI_VDEV_TYPE_MONITOR:
5804 set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
5805 break;
d5c65159
KV
5806 default:
5807 break;
5808 }
5809
5810 arvif->txpower = vif->bss_conf.txpower;
5811 ret = ath11k_mac_txpower_recalc(ar);
5812 if (ret)
5813 goto err_peer_del;
5814
5815 param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5816 param_value = ar->hw->wiphy->rts_threshold;
5817 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5818 param_id, param_value);
5819 if (ret) {
5820 ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
5821 arvif->vdev_id, ret);
5822 }
5823
d5c65159
KV
5824 ath11k_dp_vdev_tx_attach(ar, arvif);
5825
689a5e6f
SM
5826 if (vif->type != NL80211_IFTYPE_MONITOR &&
5827 test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
5828 ret = ath11k_mac_monitor_vdev_create(ar);
5829 if (ret) {
5830 ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
5831 ret);
5832 goto err_peer_del;
5833 }
5834 }
5835
d5c65159
KV
5836 mutex_unlock(&ar->conf_mutex);
5837
5838 return 0;
5839
5840err_peer_del:
5841 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
690ace20
RS
5842 reinit_completion(&ar->peer_delete_done);
5843
4ea03443
KP
5844 fbret = ath11k_wmi_send_peer_delete_cmd(ar, vif->addr,
5845 arvif->vdev_id);
5846 if (fbret) {
690ace20
RS
5847 ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
5848 arvif->vdev_id, vif->addr);
59ec8e2f 5849 goto err;
690ace20
RS
5850 }
5851
4ea03443
KP
5852 fbret = ath11k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5853 vif->addr);
5854 if (fbret)
59ec8e2f 5855 goto err;
690ace20 5856
d5c65159 5857 ar->num_peers--;
d5c65159
KV
5858 }
5859
5860err_vdev_del:
5861 ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
5862 ar->num_created_vdevs--;
79c080db 5863 ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
d5c65159
KV
5864 ab->free_vdev_map |= 1LL << arvif->vdev_id;
5865 spin_lock_bh(&ar->data_lock);
5866 list_del(&arvif->list);
5867 spin_unlock_bh(&ar->data_lock);
5868
5869err:
5870 mutex_unlock(&ar->conf_mutex);
5871
5872 return ret;
5873}
5874
5875static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
5876{
5877 struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
5878 struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
5879
5880 if (skb_cb->vif == vif)
5881 skb_cb->vif = NULL;
5882
5883 return 0;
5884}
5885
5886static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
5887 struct ieee80211_vif *vif)
5888{
5889 struct ath11k *ar = hw->priv;
5890 struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5891 struct ath11k_base *ab = ar->ab;
3cbbdfbe 5892 unsigned long time_left;
d5c65159
KV
5893 int ret;
5894 int i;
5895
26f69792
LS
5896 cancel_delayed_work_sync(&arvif->connection_loss_work);
5897
d5c65159
KV
5898 mutex_lock(&ar->conf_mutex);
5899
5900 ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
5901 arvif->vdev_id);
5902
d5c65159
KV
5903 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5904 ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
5905 if (ret)
5906 ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
5907 arvif->vdev_id, ret);
5908 }
5909
3cbbdfbe
RS
5910 reinit_completion(&ar->vdev_delete_done);
5911
d5c65159 5912 ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
3cbbdfbe 5913 if (ret) {
d5c65159
KV
5914 ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
5915 arvif->vdev_id, ret);
3cbbdfbe
RS
5916 goto err_vdev_del;
5917 }
5918
5919 time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5920 ATH11K_VDEV_DELETE_TIMEOUT_HZ);
5921 if (time_left == 0) {
5922 ath11k_warn(ab, "Timeout in receiving vdev delete response\n");
5923 goto err_vdev_del;
5924 }
d5c65159 5925
3cbbdfbe
RS
5926 ab->free_vdev_map |= 1LL << (arvif->vdev_id);
5927 ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
d5c65159 5928 ar->num_created_vdevs--;
3cbbdfbe 5929
657680cc
SM
5930 ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
5931 vif->addr, arvif->vdev_id);
3cbbdfbe 5932
689a5e6f
SM
5933 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5934 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
5935 ar->monitor_vdev_id = -1;
5936 } else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
5937 !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
5938 ret = ath11k_mac_monitor_vdev_delete(ar);
5939 if (ret)
5940 /* continue even if there's an error */
5941 ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
5942 ret);
5943 }
5944
3cbbdfbe
RS
5945err_vdev_del:
5946 spin_lock_bh(&ar->data_lock);
5947 list_del(&arvif->list);
5948 spin_unlock_bh(&ar->data_lock);
d5c65159
KV
5949
5950 ath11k_peer_cleanup(ar, arvif->vdev_id);
5951
5952 idr_for_each(&ar->txmgmt_idr,
5953 ath11k_mac_vif_txmgmt_idr_remove, vif);
5954
31582373 5955 for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
d5c65159
KV
5956 spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
5957 idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
5958 ath11k_mac_vif_unref, vif);
5959 spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
5960 }
5961
5962 /* Recalc txpower for remaining vdev */
5963 ath11k_mac_txpower_recalc(ar);
d5c65159
KV
5964
5965 /* TODO: recal traffic pause state based on the available vdevs */
5966
5967 mutex_unlock(&ar->conf_mutex);
5968}
5969
5970/* FIXME: Has to be verified. */
5971#define SUPPORTED_FILTERS \
5972 (FIF_ALLMULTI | \
5973 FIF_CONTROL | \
5974 FIF_PSPOLL | \
5975 FIF_OTHER_BSS | \
5976 FIF_BCN_PRBRESP_PROMISC | \
5977 FIF_PROBE_REQ | \
5978 FIF_FCSFAIL)
5979
5980static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
5981 unsigned int changed_flags,
5982 unsigned int *total_flags,
5983 u64 multicast)
5984{
5985 struct ath11k *ar = hw->priv;
d5c65159
KV
5986
5987 mutex_lock(&ar->conf_mutex);
5988
d5c65159
KV
5989 *total_flags &= SUPPORTED_FILTERS;
5990 ar->filter_flags = *total_flags;
5991
d5c65159
KV
5992 mutex_unlock(&ar->conf_mutex);
5993}
5994
5995static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
5996{
5997 struct ath11k *ar = hw->priv;
5998
5999 mutex_lock(&ar->conf_mutex);
6000
6001 *tx_ant = ar->cfg_tx_chainmask;
6002 *rx_ant = ar->cfg_rx_chainmask;
6003
6004 mutex_unlock(&ar->conf_mutex);
6005
6006 return 0;
6007}
6008
6009static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
6010{
6011 struct ath11k *ar = hw->priv;
6012 int ret;
6013
6014 mutex_lock(&ar->conf_mutex);
6015 ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
6016 mutex_unlock(&ar->conf_mutex);
6017
6018 return ret;
6019}
6020
6021static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
6022 struct ieee80211_vif *vif,
6023 struct ieee80211_ampdu_params *params)
6024{
6025 struct ath11k *ar = hw->priv;
6026 int ret = -EINVAL;
6027
6028 mutex_lock(&ar->conf_mutex);
6029
6030 switch (params->action) {
6031 case IEEE80211_AMPDU_RX_START:
6032 ret = ath11k_dp_rx_ampdu_start(ar, params);
6033 break;
6034 case IEEE80211_AMPDU_RX_STOP:
6035 ret = ath11k_dp_rx_ampdu_stop(ar, params);
6036 break;
6037 case IEEE80211_AMPDU_TX_START:
6038 case IEEE80211_AMPDU_TX_STOP_CONT:
6039 case IEEE80211_AMPDU_TX_STOP_FLUSH:
6040 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6041 case IEEE80211_AMPDU_TX_OPERATIONAL:
6042 /* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
6043 * Tx aggregation requests.
6044 */
6045 ret = -EOPNOTSUPP;
6046 break;
6047 }
6048
6049 mutex_unlock(&ar->conf_mutex);
6050
6051 return ret;
6052}
6053
6054static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
6055 struct ieee80211_chanctx_conf *ctx)
6056{
6057 struct ath11k *ar = hw->priv;
6058 struct ath11k_base *ab = ar->ab;
6059
6060 ath11k_dbg(ab, ATH11K_DBG_MAC,
bb2d2dfd 6061 "mac chanctx add freq %u width %d ptr %pK\n",
d5c65159
KV
6062 ctx->def.chan->center_freq, ctx->def.width, ctx);
6063
6064 mutex_lock(&ar->conf_mutex);
6065
6066 spin_lock_bh(&ar->data_lock);
6067 /* TODO: In case of multiple channel context, populate rx_channel from
6068 * Rx PPDU desc information.
6069 */
6070 ar->rx_channel = ctx->def.chan;
6071 spin_unlock_bh(&ar->data_lock);
6072
6073 mutex_unlock(&ar->conf_mutex);
6074
6075 return 0;
6076}
6077
6078static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
6079 struct ieee80211_chanctx_conf *ctx)
6080{
6081 struct ath11k *ar = hw->priv;
6082 struct ath11k_base *ab = ar->ab;
6083
6084 ath11k_dbg(ab, ATH11K_DBG_MAC,
bb2d2dfd 6085 "mac chanctx remove freq %u width %d ptr %pK\n",
d5c65159
KV
6086 ctx->def.chan->center_freq, ctx->def.width, ctx);
6087
6088 mutex_lock(&ar->conf_mutex);
6089
6090 spin_lock_bh(&ar->data_lock);
6091 /* TODO: In case of there is one more channel context left, populate
6092 * rx_channel with the channel of that remaining channel context.
6093 */
6094 ar->rx_channel = NULL;
6095 spin_unlock_bh(&ar->data_lock);
6096
6097 mutex_unlock(&ar->conf_mutex);
6098}
6099
d5c65159
KV
6100static int
6101ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
6102 const struct cfg80211_chan_def *chandef,
6103 bool restart)
6104{
6105 struct ath11k *ar = arvif->ar;
6106 struct ath11k_base *ab = ar->ab;
6107 struct wmi_vdev_start_req_arg arg = {};
6108 int he_support = arvif->vif->bss_conf.he_support;
6109 int ret = 0;
6110
6111 lockdep_assert_held(&ar->conf_mutex);
6112
6113 reinit_completion(&ar->vdev_setup_done);
6114
6115 arg.vdev_id = arvif->vdev_id;
6116 arg.dtim_period = arvif->dtim_period;
6117 arg.bcn_intval = arvif->beacon_interval;
6118
6119 arg.channel.freq = chandef->chan->center_freq;
6120 arg.channel.band_center_freq1 = chandef->center_freq1;
6121 arg.channel.band_center_freq2 = chandef->center_freq2;
6122 arg.channel.mode =
6123 ath11k_phymodes[chandef->chan->band][chandef->width];
6124
6125 arg.channel.min_power = 0;
6126 arg.channel.max_power = chandef->chan->max_power * 2;
6127 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
6128 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
6129
6130 arg.pref_tx_streams = ar->num_tx_chains;
6131 arg.pref_rx_streams = ar->num_rx_chains;
6132
6133 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6134 arg.ssid = arvif->u.ap.ssid;
6135 arg.ssid_len = arvif->u.ap.ssid_len;
6136 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
6137
6138 /* For now allow DFS for AP mode */
6139 arg.channel.chan_radar =
6140 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6141
788f805e
LS
6142 arg.channel.freq2_radar =
6143 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6144
d5c65159
KV
6145 arg.channel.passive = arg.channel.chan_radar;
6146
6147 spin_lock_bh(&ab->base_lock);
6148 arg.regdomain = ar->ab->dfs_region;
6149 spin_unlock_bh(&ab->base_lock);
6150
d5c65159
KV
6151 if (he_support) {
6152 ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
6153 if (ret) {
6154 ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
6155 arg.vdev_id);
6156 return ret;
6157 }
6158 }
6159 }
6160
6161 arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
6162
6163 ath11k_dbg(ab, ATH11K_DBG_MAC,
6164 "mac vdev %d start center_freq %d phymode %s\n",
6165 arg.vdev_id, arg.channel.freq,
6166 ath11k_wmi_phymode_str(arg.channel.mode));
6167
6168 ret = ath11k_wmi_vdev_start(ar, &arg, restart);
6169 if (ret) {
6170 ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
6171 restart ? "restart" : "start", arg.vdev_id);
6172 return ret;
6173 }
6174
6175 ret = ath11k_mac_vdev_setup_sync(ar);
6176 if (ret) {
6177 ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
6178 arg.vdev_id, restart ? "restart" : "start", ret);
6179 return ret;
6180 }
6181
689a5e6f
SM
6182 if (!restart)
6183 ar->num_started_vdevs++;
6184
657680cc
SM
6185 ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM started, vdev_id %d\n",
6186 arvif->vif->addr, arvif->vdev_id);
d5c65159
KV
6187
6188 /* Enable CAC Flag in the driver by checking the channel DFS cac time,
6189 * i.e dfs_cac_ms value which will be valid only for radar channels
6190 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
6191 * done before channel usage. This flags is used to drop rx packets.
6192 * during CAC.
6193 */
6194 /* TODO Set the flag for other interface types as required */
6195 if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
6196 chandef->chan->dfs_cac_ms &&
6197 chandef->chan->dfs_state == NL80211_DFS_USABLE) {
6198 set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6199 ath11k_dbg(ab, ATH11K_DBG_MAC,
6200 "CAC Started in chan_freq %d for vdev %d\n",
6201 arg.channel.freq, arg.vdev_id);
6202 }
6203
5815719d
BP
6204 ret = ath11k_mac_set_txbf_conf(arvif);
6205 if (ret)
6206 ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
6207 arvif->vdev_id, ret);
6208
d5c65159
KV
6209 return 0;
6210}
6211
6212static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
6213{
6214 struct ath11k *ar = arvif->ar;
6215 int ret;
6216
6217 lockdep_assert_held(&ar->conf_mutex);
6218
6219 reinit_completion(&ar->vdev_setup_done);
6220
d5c65159
KV
6221 ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
6222 if (ret) {
6223 ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
6224 arvif->vdev_id, ret);
6225 goto err;
6226 }
6227
6228 ret = ath11k_mac_vdev_setup_sync(ar);
6229 if (ret) {
6230 ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
6231 arvif->vdev_id, ret);
6232 goto err;
6233 }
6234
6235 WARN_ON(ar->num_started_vdevs == 0);
6236
6237 ar->num_started_vdevs--;
657680cc
SM
6238 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
6239 arvif->vif->addr, arvif->vdev_id);
d5c65159
KV
6240
6241 if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
6242 clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6243 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
6244 arvif->vdev_id);
6245 }
6246
6247 return 0;
6248err:
d5c65159
KV
6249 return ret;
6250}
6251
6252static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
6253 const struct cfg80211_chan_def *chandef)
6254{
6255 return ath11k_mac_vdev_start_restart(arvif, chandef, false);
6256}
6257
6258static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
6259 const struct cfg80211_chan_def *chandef)
6260{
6261 return ath11k_mac_vdev_start_restart(arvif, chandef, true);
6262}
6263
6264struct ath11k_mac_change_chanctx_arg {
6265 struct ieee80211_chanctx_conf *ctx;
6266 struct ieee80211_vif_chanctx_switch *vifs;
6267 int n_vifs;
6268 int next_vif;
6269};
6270
6271static void
6272ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
6273 struct ieee80211_vif *vif)
6274{
6275 struct ath11k_mac_change_chanctx_arg *arg = data;
6276
6277 if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
6278 return;
6279
6280 arg->n_vifs++;
6281}
6282
6283static void
6284ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
6285 struct ieee80211_vif *vif)
6286{
6287 struct ath11k_mac_change_chanctx_arg *arg = data;
6288 struct ieee80211_chanctx_conf *ctx;
6289
6290 ctx = rcu_access_pointer(vif->chanctx_conf);
6291 if (ctx != arg->ctx)
6292 return;
6293
6294 if (WARN_ON(arg->next_vif == arg->n_vifs))
6295 return;
6296
6297 arg->vifs[arg->next_vif].vif = vif;
6298 arg->vifs[arg->next_vif].old_ctx = ctx;
6299 arg->vifs[arg->next_vif].new_ctx = ctx;
6300 arg->next_vif++;
6301}
6302
6303static void
6304ath11k_mac_update_vif_chan(struct ath11k *ar,
6305 struct ieee80211_vif_chanctx_switch *vifs,
6306 int n_vifs)
6307{
6308 struct ath11k_base *ab = ar->ab;
6309 struct ath11k_vif *arvif;
6310 int ret;
6311 int i;
689a5e6f 6312 bool monitor_vif = false;
d5c65159
KV
6313
6314 lockdep_assert_held(&ar->conf_mutex);
6315
6316 for (i = 0; i < n_vifs; i++) {
6317 arvif = (void *)vifs[i].vif->drv_priv;
6318
689a5e6f
SM
6319 if (vifs[i].vif->type == NL80211_IFTYPE_MONITOR)
6320 monitor_vif = true;
6321
d5c65159 6322 ath11k_dbg(ab, ATH11K_DBG_MAC,
bb2d2dfd 6323 "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
d5c65159
KV
6324 arvif->vdev_id,
6325 vifs[i].old_ctx->def.chan->center_freq,
6326 vifs[i].new_ctx->def.chan->center_freq,
6327 vifs[i].old_ctx->def.width,
6328 vifs[i].new_ctx->def.width);
6329
6330 if (WARN_ON(!arvif->is_started))
6331 continue;
6332
6333 if (WARN_ON(!arvif->is_up))
6334 continue;
6335
6336 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
6337 if (ret) {
6338 ath11k_warn(ab, "failed to down vdev %d: %d\n",
6339 arvif->vdev_id, ret);
6340 continue;
6341 }
689a5e6f
SM
6342
6343 ar->num_started_vdevs--;
d5c65159
KV
6344 }
6345
6346 /* All relevant vdevs are downed and associated channel resources
6347 * should be available for the channel switch now.
6348 */
6349
6350 /* TODO: Update ar->rx_channel */
6351
6352 for (i = 0; i < n_vifs; i++) {
6353 arvif = (void *)vifs[i].vif->drv_priv;
6354
6355 if (WARN_ON(!arvif->is_started))
6356 continue;
6357
6358 if (WARN_ON(!arvif->is_up))
6359 continue;
6360
d5c65159
KV
6361 ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
6362 if (ret) {
6363 ath11k_warn(ab, "failed to restart vdev %d: %d\n",
6364 arvif->vdev_id, ret);
6365 continue;
6366 }
6367
979ebc54
SM
6368 ret = ath11k_mac_setup_bcn_tmpl(arvif);
6369 if (ret)
6370 ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
6371 ret);
6372
d5c65159
KV
6373 ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6374 arvif->bssid);
6375 if (ret) {
6376 ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
6377 arvif->vdev_id, ret);
6378 continue;
6379 }
6380 }
689a5e6f
SM
6381
6382 /* Restart the internal monitor vdev on new channel */
6383 if (!monitor_vif &&
6384 test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
6385 ret = ath11k_mac_monitor_stop(ar);
6386 if (ret) {
6387 ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
6388 ret);
6389 return;
6390 }
6391
6392 ret = ath11k_mac_monitor_start(ar);
6393 if (ret) {
6394 ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
6395 ret);
6396 return;
6397 }
6398 }
d5c65159
KV
6399}
6400
6401static void
6402ath11k_mac_update_active_vif_chan(struct ath11k *ar,
6403 struct ieee80211_chanctx_conf *ctx)
6404{
6405 struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
6406
6407 lockdep_assert_held(&ar->conf_mutex);
6408
6409 ieee80211_iterate_active_interfaces_atomic(ar->hw,
6410 IEEE80211_IFACE_ITER_NORMAL,
6411 ath11k_mac_change_chanctx_cnt_iter,
6412 &arg);
6413 if (arg.n_vifs == 0)
6414 return;
6415
6416 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
6417 if (!arg.vifs)
6418 return;
6419
6420 ieee80211_iterate_active_interfaces_atomic(ar->hw,
6421 IEEE80211_IFACE_ITER_NORMAL,
6422 ath11k_mac_change_chanctx_fill_iter,
6423 &arg);
6424
6425 ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
6426
6427 kfree(arg.vifs);
6428}
6429
6430static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
6431 struct ieee80211_chanctx_conf *ctx,
6432 u32 changed)
6433{
6434 struct ath11k *ar = hw->priv;
6435 struct ath11k_base *ab = ar->ab;
6436
6437 mutex_lock(&ar->conf_mutex);
6438
6439 ath11k_dbg(ab, ATH11K_DBG_MAC,
bb2d2dfd 6440 "mac chanctx change freq %u width %d ptr %pK changed %x\n",
d5c65159
KV
6441 ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
6442
6443 /* This shouldn't really happen because channel switching should use
6444 * switch_vif_chanctx().
6445 */
6446 if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
6447 goto unlock;
6448
6449 if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
6450 ath11k_mac_update_active_vif_chan(ar, ctx);
6451
6452 /* TODO: Recalc radar detection */
6453
6454unlock:
6455 mutex_unlock(&ar->conf_mutex);
6456}
6457
e7495035
CH
6458static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
6459 struct ieee80211_vif *vif)
6460{
6461 struct ath11k *ar = hw->priv;
6462 struct ath11k_base *ab = ar->ab;
6463 struct ath11k_vif *arvif = (void *)vif->drv_priv;
6464 int ret;
6465
6466 if (WARN_ON(arvif->is_started))
6467 return -EBUSY;
6468
6469 ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
6470 if (ret) {
6471 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6472 arvif->vdev_id, vif->addr,
6473 arvif->chanctx.def.chan->center_freq, ret);
6474 return ret;
6475 }
6476
6477 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
689a5e6f 6478 ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr);
e7495035
CH
6479 if (ret) {
6480 ath11k_warn(ab, "failed put monitor up: %d\n", ret);
6481 return ret;
6482 }
6483 }
6484
6485 arvif->is_started = true;
6486
6487 /* TODO: Setup ps and cts/rts protection */
6488 return 0;
6489}
6490
d5c65159
KV
6491static int
6492ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
6493 struct ieee80211_vif *vif,
6494 struct ieee80211_chanctx_conf *ctx)
6495{
6496 struct ath11k *ar = hw->priv;
6497 struct ath11k_base *ab = ar->ab;
6498 struct ath11k_vif *arvif = (void *)vif->drv_priv;
6499 int ret;
701e48a4 6500 struct peer_create_params param;
d5c65159
KV
6501
6502 mutex_lock(&ar->conf_mutex);
6503
6504 ath11k_dbg(ab, ATH11K_DBG_MAC,
6505 "mac chanctx assign ptr %pK vdev_id %i\n",
6506 ctx, arvif->vdev_id);
6507
e7495035 6508 /* for QCA6390 bss peer must be created before vdev_start */
b0919924 6509 if (ab->hw_params.vdev_start_delay &&
701e48a4 6510 arvif->vdev_type != WMI_VDEV_TYPE_AP &&
aa44b2f3
CH
6511 arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6512 !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
e7495035 6513 memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
c134d1f8
AD
6514 ret = 0;
6515 goto out;
e7495035
CH
6516 }
6517
d5c65159 6518 if (WARN_ON(arvif->is_started)) {
c134d1f8
AD
6519 ret = -EBUSY;
6520 goto out;
d5c65159
KV
6521 }
6522
aa44b2f3 6523 if (ab->hw_params.vdev_start_delay &&
77d7e871
KV
6524 arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6525 arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
701e48a4
CH
6526 param.vdev_id = arvif->vdev_id;
6527 param.peer_type = WMI_PEER_TYPE_DEFAULT;
6528 param.peer_addr = ar->mac_addr;
c134d1f8 6529
701e48a4 6530 ret = ath11k_peer_create(ar, arvif, NULL, &param);
c134d1f8
AD
6531 if (ret) {
6532 ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
6533 ret);
6534 goto out;
6535 }
701e48a4
CH
6536 }
6537
689a5e6f
SM
6538 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6539 ret = ath11k_mac_monitor_start(ar);
6540 if (ret) {
6541 ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
6542 ret);
6543 goto out;
6544 }
6545
6546 arvif->is_started = true;
6547 goto out;
6548 }
6549
d5c65159
KV
6550 ret = ath11k_mac_vdev_start(arvif, &ctx->def);
6551 if (ret) {
6552 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6553 arvif->vdev_id, vif->addr,
6554 ctx->def.chan->center_freq, ret);
c134d1f8 6555 goto out;
d5c65159 6556 }
d5c65159
KV
6557
6558 arvif->is_started = true;
6559
689a5e6f
SM
6560 if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6561 test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
6562 ret = ath11k_mac_monitor_start(ar);
6563 if (ret) {
6564 ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
6565 ret);
6566 goto out;
6567 }
6568 }
6569
d5c65159
KV
6570 /* TODO: Setup ps and cts/rts protection */
6571
c134d1f8 6572 ret = 0;
d5c65159 6573
c134d1f8 6574out:
d5c65159
KV
6575 mutex_unlock(&ar->conf_mutex);
6576
6577 return ret;
6578}
6579
6580static void
6581ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
6582 struct ieee80211_vif *vif,
6583 struct ieee80211_chanctx_conf *ctx)
6584{
6585 struct ath11k *ar = hw->priv;
6586 struct ath11k_base *ab = ar->ab;
6587 struct ath11k_vif *arvif = (void *)vif->drv_priv;
6588 int ret;
6589
6590 mutex_lock(&ar->conf_mutex);
6591
6592 ath11k_dbg(ab, ATH11K_DBG_MAC,
6593 "mac chanctx unassign ptr %pK vdev_id %i\n",
6594 ctx, arvif->vdev_id);
6595
6596 WARN_ON(!arvif->is_started);
6597
701e48a4
CH
6598 if (ab->hw_params.vdev_start_delay &&
6599 arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
6600 ath11k_peer_find_by_addr(ab, ar->mac_addr))
6601 ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
6602
689a5e6f
SM
6603 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6604 ret = ath11k_mac_monitor_stop(ar);
6605 if (ret) {
6606 ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
6607 ret);
6608 mutex_unlock(&ar->conf_mutex);
6609 return;
6610 }
6611
6612 arvif->is_started = false;
6613 mutex_unlock(&ar->conf_mutex);
6614 return;
6615 }
6616
d5c65159
KV
6617 ret = ath11k_mac_vdev_stop(arvif);
6618 if (ret)
6619 ath11k_warn(ab, "failed to stop vdev %i: %d\n",
6620 arvif->vdev_id, ret);
6621
6622 arvif->is_started = false;
6623
b4a0f541
WG
6624 if (ab->hw_params.vdev_start_delay &&
6625 arvif->vdev_type == WMI_VDEV_TYPE_STA) {
6626 ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
6627 if (ret)
6628 ath11k_warn(ar->ab,
6629 "failed to delete peer %pM for vdev %d: %d\n",
6630 arvif->bssid, arvif->vdev_id, ret);
6631 else
6632 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6633 "mac removed peer %pM vdev %d after vdev stop\n",
6634 arvif->bssid, arvif->vdev_id);
6635 }
6636
701e48a4
CH
6637 if (ab->hw_params.vdev_start_delay &&
6638 arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
6639 ath11k_wmi_vdev_down(ar, arvif->vdev_id);
6640
689a5e6f
SM
6641 if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6642 ar->num_started_vdevs == 1 &&
6643 test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
6644 ret = ath11k_mac_monitor_stop(ar);
6645 if (ret)
6646 /* continue even if there's an error */
6647 ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
6648 ret);
6649 }
6650
d5c65159
KV
6651 mutex_unlock(&ar->conf_mutex);
6652}
6653
6654static int
6655ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
6656 struct ieee80211_vif_chanctx_switch *vifs,
6657 int n_vifs,
6658 enum ieee80211_chanctx_switch_mode mode)
6659{
6660 struct ath11k *ar = hw->priv;
6661
6662 mutex_lock(&ar->conf_mutex);
6663
6664 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6665 "mac chanctx switch n_vifs %d mode %d\n",
6666 n_vifs, mode);
6667 ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
6668
6669 mutex_unlock(&ar->conf_mutex);
6670
6671 return 0;
6672}
6673
6674static int
6675ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
6676{
6677 struct ath11k_vif *arvif;
6678 int ret = 0;
6679
6680 mutex_lock(&ar->conf_mutex);
6681 list_for_each_entry(arvif, &ar->arvifs, list) {
6682 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
6683 param, arvif->vdev_id, value);
6684
6685 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6686 param, value);
6687 if (ret) {
6688 ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
6689 param, arvif->vdev_id, ret);
6690 break;
6691 }
6692 }
6693 mutex_unlock(&ar->conf_mutex);
6694 return ret;
6695}
6696
6697/* mac80211 stores device specific RTS/Fragmentation threshold value,
6698 * this is set interface specific to firmware from ath11k driver
6699 */
6700static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6701{
6702 struct ath11k *ar = hw->priv;
6703 int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6704
6705 return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
6706}
6707
6708static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6709{
6710 /* Even though there's a WMI vdev param for fragmentation threshold no
6711 * known firmware actually implements it. Moreover it is not possible to
6712 * rely frame fragmentation to mac80211 because firmware clears the
6713 * "more fragments" bit in frame control making it impossible for remote
6714 * devices to reassemble frames.
6715 *
6716 * Hence implement a dummy callback just to say fragmentation isn't
6717 * supported. This effectively prevents mac80211 from doing frame
6718 * fragmentation in software.
6719 */
6720 return -EOPNOTSUPP;
6721}
6722
6723static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6724 u32 queues, bool drop)
6725{
6726 struct ath11k *ar = hw->priv;
6727 long time_left;
6728
6729 if (drop)
6730 return;
6731
6732 time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
6733 (atomic_read(&ar->dp.num_tx_pending) == 0),
6734 ATH11K_FLUSH_TIMEOUT);
6735 if (time_left == 0)
6736 ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
6737}
6738
6739static int
6740ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
6741 enum nl80211_band band,
6742 const struct cfg80211_bitrate_mask *mask)
6743{
6744 int num_rates = 0;
6745 int i;
6746
6747 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6748 num_rates += hweight16(mask->control[band].ht_mcs[i]);
6749
6750 return num_rates;
6751}
6752
6753static bool
6754ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
6755 enum nl80211_band band,
6756 const struct cfg80211_bitrate_mask *mask)
6757{
6758 int num_rates = 0;
6759
6760 num_rates = hweight32(mask->control[band].legacy);
6761
6762 if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
6763 return false;
6764
6765 if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
6766 return false;
6767
61fe43e7
MH
6768 if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
6769 return false;
6770
d5c65159
KV
6771 return num_rates == 1;
6772}
6773
61fe43e7
MH
6774static __le16
6775ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
6776{
6777 if (he_cap->he_cap_elem.phy_cap_info[0] &
6778 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
6779 return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
6780
6781 if (he_cap->he_cap_elem.phy_cap_info[0] &
6782 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
6783 return he_cap->he_mcs_nss_supp.tx_mcs_160;
6784
6785 return he_cap->he_mcs_nss_supp.tx_mcs_80;
6786}
6787
d5c65159
KV
6788static bool
6789ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
6790 enum nl80211_band band,
6791 const struct cfg80211_bitrate_mask *mask,
6792 int *nss)
6793{
6794 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6795 u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
61fe43e7 6796 u16 he_mcs_map = 0;
d5c65159
KV
6797 u8 ht_nss_mask = 0;
6798 u8 vht_nss_mask = 0;
61fe43e7 6799 u8 he_nss_mask = 0;
d5c65159
KV
6800 int i;
6801
6802 /* No need to consider legacy here. Basic rates are always present
6803 * in bitrate mask
6804 */
6805
6806 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6807 if (mask->control[band].ht_mcs[i] == 0)
6808 continue;
6809 else if (mask->control[band].ht_mcs[i] ==
6810 sband->ht_cap.mcs.rx_mask[i])
6811 ht_nss_mask |= BIT(i);
6812 else
6813 return false;
6814 }
6815
6816 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6817 if (mask->control[band].vht_mcs[i] == 0)
6818 continue;
6819 else if (mask->control[band].vht_mcs[i] ==
6820 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6821 vht_nss_mask |= BIT(i);
6822 else
6823 return false;
6824 }
6825
61fe43e7
MH
6826 he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(&sband->iftype_data->he_cap));
6827
6828 for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6829 if (mask->control[band].he_mcs[i] == 0)
6830 continue;
6831
6832 if (mask->control[band].he_mcs[i] ==
6833 ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
6834 he_nss_mask |= BIT(i);
6835 else
6836 return false;
6837 }
6838
6839 if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
d5c65159
KV
6840 return false;
6841
6842 if (ht_nss_mask == 0)
6843 return false;
6844
6845 if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6846 return false;
6847
6848 *nss = fls(ht_nss_mask);
6849
6850 return true;
6851}
6852
6853static int
6854ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
6855 enum nl80211_band band,
6856 const struct cfg80211_bitrate_mask *mask,
6857 u32 *rate, u8 *nss)
6858{
6859 int rate_idx;
6860 u16 bitrate;
6861 u8 preamble;
6862 u8 hw_rate;
6863
6864 if (hweight32(mask->control[band].legacy) != 1)
6865 return -EINVAL;
6866
6867 rate_idx = ffs(mask->control[band].legacy) - 1;
6868
22eeadcd 6869 if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
d5c65159
KV
6870 rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
6871
6872 hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
6873 bitrate = ath11k_legacy_rates[rate_idx].bitrate;
6874
6875 if (ath11k_mac_bitrate_is_cck(bitrate))
6876 preamble = WMI_RATE_PREAMBLE_CCK;
6877 else
6878 preamble = WMI_RATE_PREAMBLE_OFDM;
6879
6880 *nss = 1;
6881 *rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
6882
6883 return 0;
6884}
6885
61fe43e7
MH
6886static int
6887ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
d5c65159
KV
6888{
6889 struct ath11k *ar = arvif->ar;
d5c65159
KV
6890 int ret;
6891
61fe43e7
MH
6892 /* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
6893 if (he_gi && he_gi != 0xFF)
6894 he_gi += 1;
d5c65159 6895
61fe43e7
MH
6896 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6897 WMI_VDEV_PARAM_SGI, he_gi);
6898 if (ret) {
6899 ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
6900 he_gi, ret);
6901 return ret;
6902 }
6903 /* start from 1 */
6904 if (he_ltf != 0xFF)
6905 he_ltf += 1;
d5c65159 6906
d5c65159 6907 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
61fe43e7 6908 WMI_VDEV_PARAM_HE_LTF, he_ltf);
d5c65159 6909 if (ret) {
61fe43e7
MH
6910 ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
6911 he_ltf, ret);
d5c65159
KV
6912 return ret;
6913 }
6914
61fe43e7
MH
6915 return 0;
6916}
6917
6918static int
6919ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
6920{
6921 struct ath11k *ar = arvif->ar;
6922 int ret;
6923 u32 he_ar_gi_ltf;
6924
6925 if (he_gi != 0xFF) {
6926 switch (he_gi) {
6927 case NL80211_RATE_INFO_HE_GI_0_8:
6928 he_gi = WMI_AUTORATE_800NS_GI;
6929 break;
6930 case NL80211_RATE_INFO_HE_GI_1_6:
6931 he_gi = WMI_AUTORATE_1600NS_GI;
6932 break;
6933 case NL80211_RATE_INFO_HE_GI_3_2:
6934 he_gi = WMI_AUTORATE_3200NS_GI;
6935 break;
6936 default:
6937 ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
6938 return -EINVAL;
6939 }
6940 }
6941
6942 if (he_ltf != 0xFF) {
6943 switch (he_ltf) {
6944 case NL80211_RATE_INFO_HE_1XLTF:
6945 he_ltf = WMI_HE_AUTORATE_LTF_1X;
6946 break;
6947 case NL80211_RATE_INFO_HE_2XLTF:
6948 he_ltf = WMI_HE_AUTORATE_LTF_2X;
6949 break;
6950 case NL80211_RATE_INFO_HE_4XLTF:
6951 he_ltf = WMI_HE_AUTORATE_LTF_4X;
6952 break;
6953 default:
6954 ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
6955 return -EINVAL;
6956 }
6957 }
6958
6959 he_ar_gi_ltf = he_gi | he_ltf;
d5c65159 6960 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
61fe43e7
MH
6961 WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
6962 he_ar_gi_ltf);
d5c65159 6963 if (ret) {
61fe43e7
MH
6964 ath11k_warn(ar->ab,
6965 "failed to set he autorate gi %u ltf %u: %d\n",
6966 he_gi, he_ltf, ret);
d5c65159
KV
6967 return ret;
6968 }
6969
61fe43e7
MH
6970 return 0;
6971}
6972
6973static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
6974 u32 rate, u8 nss, u8 sgi, u8 ldpc,
6975 u8 he_gi, u8 he_ltf, bool he_fixed_rate)
6976{
6977 struct ath11k *ar = arvif->ar;
6978 u32 vdev_param;
6979 int ret;
6980
6981 lockdep_assert_held(&ar->conf_mutex);
6982
6983 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6984 "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n",
6985 arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
6986 he_ltf, he_fixed_rate);
6987
6988 if (!arvif->vif->bss_conf.he_support) {
6989 vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
6990 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6991 vdev_param, rate);
6992 if (ret) {
6993 ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
6994 rate, ret);
6995 return ret;
6996 }
6997 }
6998
6999 vdev_param = WMI_VDEV_PARAM_NSS;
d5c65159 7000 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
61fe43e7 7001 vdev_param, nss);
d5c65159 7002 if (ret) {
61fe43e7
MH
7003 ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
7004 nss, ret);
d5c65159
KV
7005 return ret;
7006 }
7007
7008 vdev_param = WMI_VDEV_PARAM_LDPC;
7009 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7010 vdev_param, ldpc);
7011 if (ret) {
7012 ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
7013 ldpc, ret);
7014 return ret;
7015 }
7016
61fe43e7
MH
7017 if (arvif->vif->bss_conf.he_support) {
7018 if (he_fixed_rate) {
7019 ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
7020 he_ltf);
7021 if (ret) {
7022 ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
7023 ret);
7024 return ret;
7025 }
7026 } else {
7027 ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
7028 he_ltf);
7029 if (ret) {
7030 ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
7031 ret);
7032 return ret;
7033 }
7034 }
7035 } else {
7036 vdev_param = WMI_VDEV_PARAM_SGI;
7037 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7038 vdev_param, sgi);
7039 if (ret) {
7040 ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
7041 sgi, ret);
7042 return ret;
7043 }
7044 }
7045
d5c65159
KV
7046 return 0;
7047}
7048
7049static bool
7050ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
7051 enum nl80211_band band,
7052 const struct cfg80211_bitrate_mask *mask)
7053{
7054 int i;
7055 u16 vht_mcs;
7056
7057 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7058 vht_mcs = mask->control[band].vht_mcs[i];
7059
7060 switch (vht_mcs) {
7061 case 0:
7062 case BIT(8) - 1:
7063 case BIT(9) - 1:
7064 case BIT(10) - 1:
7065 break;
7066 default:
7067 return false;
7068 }
7069 }
7070
7071 return true;
7072}
7073
61fe43e7
MH
7074static bool
7075ath11k_mac_he_mcs_range_present(struct ath11k *ar,
7076 enum nl80211_band band,
7077 const struct cfg80211_bitrate_mask *mask)
7078{
7079 int i;
7080 u16 he_mcs;
7081
7082 for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
7083 he_mcs = mask->control[band].he_mcs[i];
7084
7085 switch (he_mcs) {
7086 case 0:
7087 case BIT(8) - 1:
7088 case BIT(10) - 1:
7089 case BIT(12) - 1:
7090 break;
7091 default:
7092 return false;
7093 }
7094 }
7095
7096 return true;
7097}
7098
d5c65159
KV
7099static void ath11k_mac_set_bitrate_mask_iter(void *data,
7100 struct ieee80211_sta *sta)
7101{
7102 struct ath11k_vif *arvif = data;
7103 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
7104 struct ath11k *ar = arvif->ar;
7105
7106 spin_lock_bh(&ar->data_lock);
7107 arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
7108 spin_unlock_bh(&ar->data_lock);
7109
7110 ieee80211_queue_work(ar->hw, &arsta->update_wk);
7111}
7112
7113static void ath11k_mac_disable_peer_fixed_rate(void *data,
7114 struct ieee80211_sta *sta)
7115{
7116 struct ath11k_vif *arvif = data;
7117 struct ath11k *ar = arvif->ar;
7118 int ret;
7119
7120 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
7121 arvif->vdev_id,
7122 WMI_PEER_PARAM_FIXED_RATE,
7123 WMI_FIXED_RATE_NONE);
7124 if (ret)
7125 ath11k_warn(ar->ab,
7126 "failed to disable peer fixed rate for STA %pM ret %d\n",
7127 sta->addr, ret);
7128}
7129
61fe43e7
MH
7130static bool
7131ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
7132 const struct cfg80211_bitrate_mask *mask)
7133{
7134 bool he_fixed_rate = false, vht_fixed_rate = false;
7135 struct ath11k_peer *peer, *tmp;
7136 const u16 *vht_mcs_mask, *he_mcs_mask;
7137 u8 vht_nss, he_nss;
7138 bool ret = true;
7139
7140 vht_mcs_mask = mask->control[band].vht_mcs;
7141 he_mcs_mask = mask->control[band].he_mcs;
7142
7143 if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
7144 vht_fixed_rate = true;
7145
7146 if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
7147 he_fixed_rate = true;
7148
7149 if (!vht_fixed_rate && !he_fixed_rate)
7150 return true;
7151
7152 vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
7153 he_nss = ath11k_mac_max_he_nss(he_mcs_mask);
7154
7155 rcu_read_lock();
7156 spin_lock_bh(&ar->ab->base_lock);
7157 list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
7158 if (peer->sta) {
7159 if (vht_fixed_rate && (!peer->sta->vht_cap.vht_supported ||
7160 peer->sta->rx_nss < vht_nss)) {
7161 ret = false;
7162 goto out;
7163 }
7164 if (he_fixed_rate && (!peer->sta->he_cap.has_he ||
7165 peer->sta->rx_nss < he_nss)) {
7166 ret = false;
7167 goto out;
7168 }
7169 }
7170 }
7171
7172out:
7173 spin_unlock_bh(&ar->ab->base_lock);
7174 rcu_read_unlock();
7175 return ret;
7176}
7177
d5c65159
KV
7178static int
7179ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
7180 struct ieee80211_vif *vif,
7181 const struct cfg80211_bitrate_mask *mask)
7182{
7183 struct ath11k_vif *arvif = (void *)vif->drv_priv;
7184 struct cfg80211_chan_def def;
7185 struct ath11k *ar = arvif->ar;
7186 enum nl80211_band band;
7187 const u8 *ht_mcs_mask;
7188 const u16 *vht_mcs_mask;
61fe43e7
MH
7189 const u16 *he_mcs_mask;
7190 u8 he_ltf = 0;
7191 u8 he_gi = 0;
d5c65159
KV
7192 u32 rate;
7193 u8 nss;
7194 u8 sgi;
7195 u8 ldpc;
7196 int single_nss;
7197 int ret;
7198 int num_rates;
61fe43e7 7199 bool he_fixed_rate = false;
d5c65159
KV
7200
7201 if (ath11k_mac_vif_chan(vif, &def))
7202 return -EPERM;
7203
7204 band = def.chan->band;
7205 ht_mcs_mask = mask->control[band].ht_mcs;
7206 vht_mcs_mask = mask->control[band].vht_mcs;
61fe43e7 7207 he_mcs_mask = mask->control[band].he_mcs;
d5c65159
KV
7208 ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
7209
7210 sgi = mask->control[band].gi;
7211 if (sgi == NL80211_TXRATE_FORCE_LGI)
7212 return -EINVAL;
7213
61fe43e7
MH
7214 he_gi = mask->control[band].he_gi;
7215 he_ltf = mask->control[band].he_ltf;
7216
d5c65159
KV
7217 /* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
7218 * requires passing atleast one of used basic rates along with them.
7219 * Fixed rate setting across different preambles(legacy, HT, VHT) is
7220 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
7221 * suitable for setting single HT/VHT rates.
7222 * But, there could be a single basic rate passed from userspace which
7223 * can be done through the FIXED_RATE param.
7224 */
7225 if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
7226 ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
7227 &nss);
7228 if (ret) {
7229 ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
7230 arvif->vdev_id, ret);
7231 return ret;
7232 }
7233 ieee80211_iterate_stations_atomic(ar->hw,
7234 ath11k_mac_disable_peer_fixed_rate,
7235 arvif);
7236 } else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
7237 &single_nss)) {
7238 rate = WMI_FIXED_RATE_NONE;
7239 nss = single_nss;
61fe43e7
MH
7240 mutex_lock(&ar->conf_mutex);
7241 arvif->bitrate_mask = *mask;
7242 ieee80211_iterate_stations_atomic(ar->hw,
7243 ath11k_mac_set_bitrate_mask_iter,
7244 arvif);
7245 mutex_unlock(&ar->conf_mutex);
d5c65159
KV
7246 } else {
7247 rate = WMI_FIXED_RATE_NONE;
61fe43e7
MH
7248
7249 if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
7250 ath11k_warn(ar->ab,
567ec33a 7251 "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
d5c65159 7252 nss = min_t(u32, ar->num_tx_chains,
61fe43e7
MH
7253 max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
7254 ath11k_mac_max_vht_nss(vht_mcs_mask)),
7255 ath11k_mac_max_he_nss(he_mcs_mask)));
d5c65159
KV
7256
7257 /* If multiple rates across different preambles are given
7258 * we can reconfigure this info with all peers using PEER_ASSOC
7259 * command with the below exception cases.
7260 * - Single VHT Rate : peer_assoc command accommodates only MCS
7261 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
7262 * mandates passing basic rates along with HT/VHT rates, FW
7263 * doesn't allow switching from VHT to Legacy. Hence instead of
7264 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
7265 * we could set this VHT rate as peer fixed rate param, which
7266 * will override FIXED rate and FW rate control algorithm.
7267 * If single VHT rate is passed along with HT rates, we select
7268 * the VHT rate as fixed rate for vht peers.
7269 * - Multiple VHT Rates : When Multiple VHT rates are given,this
7270 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
7271 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
7272 * RATEMASK_CMDID can cover all use cases of setting rates
7273 * across multiple preambles and rates within same type.
7274 * But requires more validation of the command at this point.
7275 */
7276
7277 num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
7278 mask);
7279
7280 if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
7281 num_rates > 1) {
7282 /* TODO: Handle multiple VHT MCS values setting using
7283 * RATEMASK CMD
7284 */
7285 ath11k_warn(ar->ab,
61fe43e7
MH
7286 "setting %d mcs values in bitrate mask not supported\n",
7287 num_rates);
d5c65159
KV
7288 return -EINVAL;
7289 }
7290
61fe43e7
MH
7291 num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
7292 mask);
7293 if (num_rates == 1)
7294 he_fixed_rate = true;
7295
7296 if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
7297 num_rates > 1) {
7298 ath11k_warn(ar->ab,
7299 "Setting more than one HE MCS Value in bitrate mask not supported\n");
7300 return -EINVAL;
7301 }
7302
7303 mutex_lock(&ar->conf_mutex);
d5c65159
KV
7304 ieee80211_iterate_stations_atomic(ar->hw,
7305 ath11k_mac_disable_peer_fixed_rate,
7306 arvif);
7307
d5c65159
KV
7308 arvif->bitrate_mask = *mask;
7309 ieee80211_iterate_stations_atomic(ar->hw,
7310 ath11k_mac_set_bitrate_mask_iter,
7311 arvif);
7312
7313 mutex_unlock(&ar->conf_mutex);
7314 }
7315
7316 mutex_lock(&ar->conf_mutex);
7317
61fe43e7
MH
7318 ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
7319 he_ltf, he_fixed_rate);
d5c65159 7320 if (ret) {
61fe43e7 7321 ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
d5c65159
KV
7322 arvif->vdev_id, ret);
7323 }
7324
7325 mutex_unlock(&ar->conf_mutex);
7326
7327 return ret;
7328}
7329
7330static void
7331ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
7332 enum ieee80211_reconfig_type reconfig_type)
7333{
7334 struct ath11k *ar = hw->priv;
7335
7336 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
7337 return;
7338
7339 mutex_lock(&ar->conf_mutex);
7340
7341 if (ar->state == ATH11K_STATE_RESTARTED) {
7342 ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
7343 ar->pdev->pdev_id);
7344 ar->state = ATH11K_STATE_ON;
7345 ieee80211_wake_queues(ar->hw);
7346 }
7347
7348 mutex_unlock(&ar->conf_mutex);
7349}
7350
7351static void
7352ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
7353 struct ieee80211_channel *channel)
7354{
7355 int ret;
7356 enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
7357
7358 lockdep_assert_held(&ar->conf_mutex);
7359
6bc9d6f7 7360 if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
d5c65159
KV
7361 ar->rx_channel != channel)
7362 return;
7363
7364 if (ar->scan.state != ATH11K_SCAN_IDLE) {
7365 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7366 "ignoring bss chan info req while scanning..\n");
7367 return;
7368 }
7369
7370 reinit_completion(&ar->bss_survey_done);
7371
7372 ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
7373 if (ret) {
7374 ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
7375 return;
7376 }
7377
7378 ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
7379 if (ret == 0)
7380 ath11k_warn(ar->ab, "bss channel survey timed out\n");
7381}
7382
7383static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
7384 struct survey_info *survey)
7385{
7386 struct ath11k *ar = hw->priv;
7387 struct ieee80211_supported_band *sband;
7388 struct survey_info *ar_survey;
7389 int ret = 0;
7390
7391 if (idx >= ATH11K_NUM_CHANS)
7392 return -ENOENT;
7393
7394 ar_survey = &ar->survey[idx];
7395
7396 mutex_lock(&ar->conf_mutex);
7397
7398 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7399 if (sband && idx >= sband->n_channels) {
7400 idx -= sband->n_channels;
7401 sband = NULL;
7402 }
7403
7404 if (!sband)
7405 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
b6b142f6
PKC
7406 if (sband && idx >= sband->n_channels) {
7407 idx -= sband->n_channels;
7408 sband = NULL;
7409 }
d5c65159 7410
b6b142f6
PKC
7411 if (!sband)
7412 sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
d5c65159
KV
7413 if (!sband || idx >= sband->n_channels) {
7414 ret = -ENOENT;
7415 goto exit;
7416 }
7417
7418 ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
7419
7420 spin_lock_bh(&ar->data_lock);
7421 memcpy(survey, ar_survey, sizeof(*survey));
7422 spin_unlock_bh(&ar->data_lock);
7423
7424 survey->channel = &sband->channels[idx];
7425
7426 if (ar->rx_channel == survey->channel)
7427 survey->filled |= SURVEY_INFO_IN_USE;
7428
7429exit:
7430 mutex_unlock(&ar->conf_mutex);
7431 return ret;
7432}
7433
7434static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
7435 struct ieee80211_vif *vif,
7436 struct ieee80211_sta *sta,
7437 struct station_info *sinfo)
7438{
7439 struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
7440
7441 sinfo->rx_duration = arsta->rx_duration;
7442 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
7443
a9e945ea
VN
7444 sinfo->tx_duration = arsta->tx_duration;
7445 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
7446
1d795645
WG
7447 if (arsta->txrate.legacy || arsta->txrate.nss) {
7448 if (arsta->txrate.legacy) {
7449 sinfo->txrate.legacy = arsta->txrate.legacy;
7450 } else {
7451 sinfo->txrate.mcs = arsta->txrate.mcs;
7452 sinfo->txrate.nss = arsta->txrate.nss;
7453 sinfo->txrate.bw = arsta->txrate.bw;
7454 sinfo->txrate.he_gi = arsta->txrate.he_gi;
7455 sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
7456 sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
7457 }
7458 sinfo->txrate.flags = arsta->txrate.flags;
7459 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
7460 }
d5c65159
KV
7461
7462 /* TODO: Use real NF instead of default one. */
7463 sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
f277eb05 7464 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
d5c65159
KV
7465}
7466
7467static const struct ieee80211_ops ath11k_ops = {
7468 .tx = ath11k_mac_op_tx,
7469 .start = ath11k_mac_op_start,
7470 .stop = ath11k_mac_op_stop,
7471 .reconfig_complete = ath11k_mac_op_reconfig_complete,
7472 .add_interface = ath11k_mac_op_add_interface,
7473 .remove_interface = ath11k_mac_op_remove_interface,
6aea26ce 7474 .update_vif_offload = ath11k_mac_op_update_vif_offload,
d5c65159
KV
7475 .config = ath11k_mac_op_config,
7476 .bss_info_changed = ath11k_mac_op_bss_info_changed,
7477 .configure_filter = ath11k_mac_op_configure_filter,
7478 .hw_scan = ath11k_mac_op_hw_scan,
7479 .cancel_hw_scan = ath11k_mac_op_cancel_hw_scan,
7480 .set_key = ath11k_mac_op_set_key,
7481 .sta_state = ath11k_mac_op_sta_state,
34c67dc3 7482 .sta_set_4addr = ath11k_mac_op_sta_set_4addr,
64f1d7e9 7483 .sta_set_txpwr = ath11k_mac_op_sta_set_txpwr,
d5c65159
KV
7484 .sta_rc_update = ath11k_mac_op_sta_rc_update,
7485 .conf_tx = ath11k_mac_op_conf_tx,
7486 .set_antenna = ath11k_mac_op_set_antenna,
7487 .get_antenna = ath11k_mac_op_get_antenna,
7488 .ampdu_action = ath11k_mac_op_ampdu_action,
7489 .add_chanctx = ath11k_mac_op_add_chanctx,
7490 .remove_chanctx = ath11k_mac_op_remove_chanctx,
7491 .change_chanctx = ath11k_mac_op_change_chanctx,
7492 .assign_vif_chanctx = ath11k_mac_op_assign_vif_chanctx,
7493 .unassign_vif_chanctx = ath11k_mac_op_unassign_vif_chanctx,
7494 .switch_vif_chanctx = ath11k_mac_op_switch_vif_chanctx,
7495 .set_rts_threshold = ath11k_mac_op_set_rts_threshold,
7496 .set_frag_threshold = ath11k_mac_op_set_frag_threshold,
7497 .set_bitrate_mask = ath11k_mac_op_set_bitrate_mask,
7498 .get_survey = ath11k_mac_op_get_survey,
7499 .flush = ath11k_mac_op_flush,
7500 .sta_statistics = ath11k_mac_op_sta_statistics,
7501 CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
a45ceea5 7502#ifdef CONFIG_ATH11K_DEBUGFS
568f0603 7503 .sta_add_debugfs = ath11k_debugfs_sta_op_add,
d5c65159
KV
7504#endif
7505};
7506
d5c65159
KV
7507static void ath11k_mac_update_ch_list(struct ath11k *ar,
7508 struct ieee80211_supported_band *band,
7509 u32 freq_low, u32 freq_high)
7510{
7511 int i;
7512
7513 if (!(freq_low && freq_high))
7514 return;
7515
7516 for (i = 0; i < band->n_channels; i++) {
7517 if (band->channels[i].center_freq < freq_low ||
7518 band->channels[i].center_freq > freq_high)
7519 band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
7520 }
7521}
7522
5f859bc0
CH
7523static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
7524{
7525 struct ath11k_pdev *pdev = ar->pdev;
7526 struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
7527
7528 if (band == WMI_HOST_WLAN_2G_CAP)
7529 return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
7530
7531 if (band == WMI_HOST_WLAN_5G_CAP)
7532 return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
7533
7534 ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
7535
7536 return 0;
7537}
7538
d5c65159
KV
7539static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
7540 u32 supported_bands)
7541{
7542 struct ieee80211_supported_band *band;
74bba5e5 7543 struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
d5c65159 7544 void *channels;
5f859bc0 7545 u32 phy_id;
d5c65159
KV
7546
7547 BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
22eeadcd
PKC
7548 ARRAY_SIZE(ath11k_5ghz_channels) +
7549 ARRAY_SIZE(ath11k_6ghz_channels)) !=
d5c65159
KV
7550 ATH11K_NUM_CHANS);
7551
7552 reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
74bba5e5 7553 temp_reg_cap = reg_cap;
d5c65159
KV
7554
7555 if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
7556 channels = kmemdup(ath11k_2ghz_channels,
7557 sizeof(ath11k_2ghz_channels),
7558 GFP_KERNEL);
7559 if (!channels)
7560 return -ENOMEM;
7561
7562 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
22eeadcd 7563 band->band = NL80211_BAND_2GHZ;
d5c65159
KV
7564 band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
7565 band->channels = channels;
7566 band->n_bitrates = ath11k_g_rates_size;
7567 band->bitrates = ath11k_g_rates;
7568 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
5f859bc0
CH
7569
7570 if (ar->ab->hw_params.single_pdev_only) {
7571 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
74bba5e5 7572 temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
5f859bc0 7573 }
d5c65159 7574 ath11k_mac_update_ch_list(ar, band,
74bba5e5
WG
7575 temp_reg_cap->low_2ghz_chan,
7576 temp_reg_cap->high_2ghz_chan);
d5c65159
KV
7577 }
7578
7579 if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
22eeadcd
PKC
7580 if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
7581 channels = kmemdup(ath11k_6ghz_channels,
7582 sizeof(ath11k_6ghz_channels), GFP_KERNEL);
7583 if (!channels) {
7584 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7585 return -ENOMEM;
7586 }
7587
7588 ar->supports_6ghz = true;
7589 band = &ar->mac.sbands[NL80211_BAND_6GHZ];
7590 band->band = NL80211_BAND_6GHZ;
7591 band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
7592 band->channels = channels;
7593 band->n_bitrates = ath11k_a_rates_size;
7594 band->bitrates = ath11k_a_rates;
7595 ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
74bba5e5
WG
7596
7597 if (ar->ab->hw_params.single_pdev_only) {
7598 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
7599 temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
7600 }
7601
22eeadcd 7602 ath11k_mac_update_ch_list(ar, band,
74bba5e5
WG
7603 temp_reg_cap->low_5ghz_chan,
7604 temp_reg_cap->high_5ghz_chan);
d5c65159
KV
7605 }
7606
22eeadcd
PKC
7607 if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
7608 channels = kmemdup(ath11k_5ghz_channels,
7609 sizeof(ath11k_5ghz_channels),
7610 GFP_KERNEL);
7611 if (!channels) {
7612 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7613 kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7614 return -ENOMEM;
7615 }
7616
7617 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7618 band->band = NL80211_BAND_5GHZ;
7619 band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
7620 band->channels = channels;
7621 band->n_bitrates = ath11k_a_rates_size;
7622 band->bitrates = ath11k_a_rates;
7623 ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
5f859bc0
CH
7624
7625 if (ar->ab->hw_params.single_pdev_only) {
7626 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
74bba5e5 7627 temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
5f859bc0
CH
7628 }
7629
22eeadcd 7630 ath11k_mac_update_ch_list(ar, band,
74bba5e5
WG
7631 temp_reg_cap->low_5ghz_chan,
7632 temp_reg_cap->high_5ghz_chan);
22eeadcd 7633 }
d5c65159
KV
7634 }
7635
7636 return 0;
7637}
7638
2626c269
KV
7639static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
7640{
7641 struct ath11k_base *ab = ar->ab;
7642 struct ieee80211_iface_combination *combinations;
7643 struct ieee80211_iface_limit *limits;
7644 int n_limits;
7645
7646 combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
7647 if (!combinations)
7648 return -ENOMEM;
7649
7650 n_limits = 2;
7651
7652 limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
8431350e
AD
7653 if (!limits) {
7654 kfree(combinations);
2626c269 7655 return -ENOMEM;
8431350e 7656 }
2626c269
KV
7657
7658 limits[0].max = 1;
7659 limits[0].types |= BIT(NL80211_IFTYPE_STATION);
7660
7661 limits[1].max = 16;
7662 limits[1].types |= BIT(NL80211_IFTYPE_AP);
7663
7664 if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
7665 ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
7666 limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
7667
7668 combinations[0].limits = limits;
7669 combinations[0].n_limits = n_limits;
7670 combinations[0].max_interfaces = 16;
7671 combinations[0].num_different_channels = 1;
7672 combinations[0].beacon_int_infra_match = true;
7673 combinations[0].beacon_int_min_gcd = 100;
7674 combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7675 BIT(NL80211_CHAN_WIDTH_20) |
7676 BIT(NL80211_CHAN_WIDTH_40) |
f552d6fd
P
7677 BIT(NL80211_CHAN_WIDTH_80) |
7678 BIT(NL80211_CHAN_WIDTH_80P80) |
7679 BIT(NL80211_CHAN_WIDTH_160);
2626c269
KV
7680
7681 ar->hw->wiphy->iface_combinations = combinations;
7682 ar->hw->wiphy->n_iface_combinations = 1;
7683
7684 return 0;
7685}
7686
d5c65159
KV
7687static const u8 ath11k_if_types_ext_capa[] = {
7688 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7689 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7690};
7691
7692static const u8 ath11k_if_types_ext_capa_sta[] = {
7693 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7694 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7695 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
7696};
7697
7698static const u8 ath11k_if_types_ext_capa_ap[] = {
7699 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7700 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7701 [9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
7702};
7703
7704static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
7705 {
7706 .extended_capabilities = ath11k_if_types_ext_capa,
7707 .extended_capabilities_mask = ath11k_if_types_ext_capa,
7708 .extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
7709 }, {
7710 .iftype = NL80211_IFTYPE_STATION,
7711 .extended_capabilities = ath11k_if_types_ext_capa_sta,
7712 .extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
7713 .extended_capabilities_len =
7714 sizeof(ath11k_if_types_ext_capa_sta),
7715 }, {
7716 .iftype = NL80211_IFTYPE_AP,
7717 .extended_capabilities = ath11k_if_types_ext_capa_ap,
7718 .extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
7719 .extended_capabilities_len =
7720 sizeof(ath11k_if_types_ext_capa_ap),
7721 },
7722};
7723
0366f426
VT
7724static void __ath11k_mac_unregister(struct ath11k *ar)
7725{
7726 cancel_work_sync(&ar->regd_update_work);
7727
7728 ieee80211_unregister_hw(ar->hw);
7729
7730 idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
7731 idr_destroy(&ar->txmgmt_idr);
7732
7733 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7734 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
22eeadcd 7735 kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
0366f426 7736
2626c269
KV
7737 kfree(ar->hw->wiphy->iface_combinations[0].limits);
7738 kfree(ar->hw->wiphy->iface_combinations);
7739
0366f426
VT
7740 SET_IEEE80211_DEV(ar->hw, NULL);
7741}
7742
7743void ath11k_mac_unregister(struct ath11k_base *ab)
7744{
7745 struct ath11k *ar;
7746 struct ath11k_pdev *pdev;
7747 int i;
7748
7749 for (i = 0; i < ab->num_radios; i++) {
7750 pdev = &ab->pdevs[i];
7751 ar = pdev->ar;
7752 if (!ar)
7753 continue;
7754
7755 __ath11k_mac_unregister(ar);
7756 }
7757}
7758
7759static int __ath11k_mac_register(struct ath11k *ar)
d5c65159
KV
7760{
7761 struct ath11k_base *ab = ar->ab;
7762 struct ath11k_pdev_cap *cap = &ar->pdev->cap;
7763 static const u32 cipher_suites[] = {
7764 WLAN_CIPHER_SUITE_TKIP,
7765 WLAN_CIPHER_SUITE_CCMP,
7766 WLAN_CIPHER_SUITE_AES_CMAC,
7767 WLAN_CIPHER_SUITE_BIP_CMAC_256,
7768 WLAN_CIPHER_SUITE_BIP_GMAC_128,
7769 WLAN_CIPHER_SUITE_BIP_GMAC_256,
7770 WLAN_CIPHER_SUITE_GCMP,
7771 WLAN_CIPHER_SUITE_GCMP_256,
7772 WLAN_CIPHER_SUITE_CCMP_256,
7773 };
7774 int ret;
7775 u32 ht_cap = 0;
7776
7777 ath11k_pdev_caps_update(ar);
7778
7779 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7780
7781 SET_IEEE80211_DEV(ar->hw, ab->dev);
7782
7783 ret = ath11k_mac_setup_channels_rates(ar,
7784 cap->supported_bands);
7785 if (ret)
7e8453e3 7786 goto err;
d5c65159
KV
7787
7788 ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
9f056ed8 7789 ath11k_mac_setup_he_cap(ar, cap);
d5c65159 7790
2626c269
KV
7791 ret = ath11k_mac_setup_iface_combinations(ar);
7792 if (ret) {
7793 ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
7794 goto err_free_channels;
7795 }
7796
d5c65159
KV
7797 ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
7798 ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
7799
2626c269 7800 ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
d5c65159
KV
7801
7802 ieee80211_hw_set(ar->hw, SIGNAL_DBM);
7803 ieee80211_hw_set(ar->hw, SUPPORTS_PS);
7804 ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
7805 ieee80211_hw_set(ar->hw, MFP_CAPABLE);
7806 ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
7807 ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
7808 ieee80211_hw_set(ar->hw, AP_LINK_PS);
7809 ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
d5c65159
KV
7810 ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
7811 ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
7812 ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
7813 ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
7814 ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
7815 ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
7816 ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
2167fa60
S
7817
7818 if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
7819 ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
7820 ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
7821 }
f552d6fd
P
7822
7823 if (cap->nss_ratio_enabled)
7824 ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
7825
c3a7d7eb 7826 if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
d5c65159
KV
7827 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
7828 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
7829 ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
7830 ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
26c79927 7831 ieee80211_hw_set(ar->hw, USES_RSS);
d5c65159
KV
7832 }
7833
7834 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
7835 ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
7836
7837 /* TODO: Check if HT capability advertised from firmware is different
7838 * for each band for a dual band capable radio. It will be tricky to
7839 * handle it when the ht capability different for each band.
7840 */
82c434c1
WG
7841 if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
7842 (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
d5c65159
KV
7843 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
7844
7845 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
7846 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
7847
7848 ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
7849
7850 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
7851 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
7852 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
7853
7854 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
7855 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
7856 NL80211_FEATURE_AP_SCAN;
7857
7858 ar->max_num_stations = TARGET_NUM_STATIONS;
7859 ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
7860
7861 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
7862
7863 ar->hw->queues = ATH11K_HW_MAX_QUEUES;
107560d8 7864 ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
d5c65159
KV
7865 ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
7866 ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
7867
7868 ar->hw->vif_data_size = sizeof(struct ath11k_vif);
7869 ar->hw->sta_data_size = sizeof(struct ath11k_sta);
7870
d5c65159 7871 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
64f1d7e9 7872 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
d5c65159
KV
7873
7874 ar->hw->wiphy->cipher_suites = cipher_suites;
7875 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
7876
7877 ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
7878 ar->hw->wiphy->num_iftype_ext_capab =
7879 ARRAY_SIZE(ath11k_iftypes_ext_capa);
7880
047679e3
AD
7881 if (ar->supports_6ghz) {
7882 wiphy_ext_feature_set(ar->hw->wiphy,
7883 NL80211_EXT_FEATURE_FILS_DISCOVERY);
7884 wiphy_ext_feature_set(ar->hw->wiphy,
7885 NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
7886 }
7887
d5c65159
KV
7888 ath11k_reg_init(ar);
7889
aa2092a9
VN
7890 if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
7891 ar->hw->netdev_features = NETIF_F_HW_CSUM;
7892 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
7893 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
7894 }
d5c65159
KV
7895
7896 ret = ieee80211_register_hw(ar->hw);
7897 if (ret) {
7898 ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
2626c269 7899 goto err_free_if_combs;
d5c65159
KV
7900 }
7901
3f6e6c32
KV
7902 if (!ab->hw_params.supports_monitor)
7903 /* There's a race between calling ieee80211_register_hw()
7904 * and here where the monitor mode is enabled for a little
7905 * while. But that time is so short and in practise it make
7906 * a difference in real life.
7907 */
7908 ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
7909
d5c65159 7910 /* Apply the regd received during initialization */
1db2b0d0 7911 ret = ath11k_regd_update(ar);
d5c65159
KV
7912 if (ret) {
7913 ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
0d969683 7914 goto err_unregister_hw;
d5c65159
KV
7915 }
7916
cb4e57db 7917 ret = ath11k_debugfs_register(ar);
d5c65159
KV
7918 if (ret) {
7919 ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
0d969683 7920 goto err_unregister_hw;
d5c65159
KV
7921 }
7922
7923 return 0;
7924
0d969683
WG
7925err_unregister_hw:
7926 ieee80211_unregister_hw(ar->hw);
7927
2626c269
KV
7928err_free_if_combs:
7929 kfree(ar->hw->wiphy->iface_combinations[0].limits);
7930 kfree(ar->hw->wiphy->iface_combinations);
7931
7932err_free_channels:
d5c65159
KV
7933 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7934 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
7e8453e3 7935 kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
d5c65159 7936
7e8453e3 7937err:
d5c65159
KV
7938 SET_IEEE80211_DEV(ar->hw, NULL);
7939 return ret;
7940}
7941
0366f426 7942int ath11k_mac_register(struct ath11k_base *ab)
d5c65159
KV
7943{
7944 struct ath11k *ar;
7945 struct ath11k_pdev *pdev;
7946 int i;
0366f426 7947 int ret;
d5c65159 7948
6fc3b94e
MK
7949 if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
7950 return 0;
7951
3c79cb4d
SM
7952 /* Initialize channel counters frequency value in hertz */
7953 ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
7954 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
7955
d5c65159
KV
7956 for (i = 0; i < ab->num_radios; i++) {
7957 pdev = &ab->pdevs[i];
7958 ar = pdev->ar;
0366f426
VT
7959 if (ab->pdevs_macaddr_valid) {
7960 ether_addr_copy(ar->mac_addr, pdev->mac_addr);
7961 } else {
7962 ether_addr_copy(ar->mac_addr, ab->mac_addr);
7963 ar->mac_addr[4] += i;
7964 }
d5c65159 7965
3c79cb4d
SM
7966 idr_init(&ar->txmgmt_idr);
7967 spin_lock_init(&ar->txmgmt_idr_lock);
7968
0366f426
VT
7969 ret = __ath11k_mac_register(ar);
7970 if (ret)
7971 goto err_cleanup;
0366f426 7972 }
d5c65159 7973
0366f426 7974 return 0;
d5c65159 7975
0366f426
VT
7976err_cleanup:
7977 for (i = i - 1; i >= 0; i--) {
7978 pdev = &ab->pdevs[i];
7979 ar = pdev->ar;
7980 __ath11k_mac_unregister(ar);
d5c65159 7981 }
0366f426
VT
7982
7983 return ret;
d5c65159
KV
7984}
7985
0366f426 7986int ath11k_mac_allocate(struct ath11k_base *ab)
d5c65159
KV
7987{
7988 struct ieee80211_hw *hw;
7989 struct ath11k *ar;
7990 struct ath11k_pdev *pdev;
7991 int ret;
7992 int i;
7993
7994 if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
7995 return 0;
7996
7997 for (i = 0; i < ab->num_radios; i++) {
7998 pdev = &ab->pdevs[i];
7999 hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
8000 if (!hw) {
8001 ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
8002 ret = -ENOMEM;
0366f426 8003 goto err_free_mac;
d5c65159
KV
8004 }
8005
8006 ar = hw->priv;
8007 ar->hw = hw;
8008 ar->ab = ab;
8009 ar->pdev = pdev;
8010 ar->pdev_idx = i;
d547ca4c 8011 ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
d5c65159 8012
6bc9d6f7 8013 ar->wmi = &ab->wmi_ab.wmi[i];
d5c65159
KV
8014 /* FIXME wmi[0] is already initialized during attach,
8015 * Should we do this again?
8016 */
8017 ath11k_wmi_pdev_attach(ab, i);
8018
8019 ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
8020 ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
8021 ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
8022 ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
8023
d5c65159
KV
8024 pdev->ar = ar;
8025 spin_lock_init(&ar->data_lock);
8026 INIT_LIST_HEAD(&ar->arvifs);
8027 INIT_LIST_HEAD(&ar->ppdu_stats_info);
8028 mutex_init(&ar->conf_mutex);
8029 init_completion(&ar->vdev_setup_done);
3cbbdfbe 8030 init_completion(&ar->vdev_delete_done);
d5c65159 8031 init_completion(&ar->peer_assoc_done);
690ace20 8032 init_completion(&ar->peer_delete_done);
d5c65159
KV
8033 init_completion(&ar->install_key_done);
8034 init_completion(&ar->bss_survey_done);
8035 init_completion(&ar->scan.started);
8036 init_completion(&ar->scan.completed);
a41d1034
PKC
8037 init_completion(&ar->thermal.wmi_sync);
8038
d5c65159
KV
8039 INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
8040 INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
8041
8042 INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
8043 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
64e06b78 8044
64e06b78
SM
8045 clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
8046
8047 ar->monitor_vdev_id = -1;
8048 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
d5c65159
KV
8049 }
8050
d5c65159
KV
8051 return 0;
8052
0366f426 8053err_free_mac:
d5c65159
KV
8054 ath11k_mac_destroy(ab);
8055
8056 return ret;
8057}
8058
8059void ath11k_mac_destroy(struct ath11k_base *ab)
8060{
8061 struct ath11k *ar;
8062 struct ath11k_pdev *pdev;
8063 int i;
8064
8065 for (i = 0; i < ab->num_radios; i++) {
8066 pdev = &ab->pdevs[i];
8067 ar = pdev->ar;
8068 if (!ar)
8069 continue;
8070
8071 ieee80211_free_hw(ar->hw);
8072 pdev->ar = NULL;
8073 }
8074}