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