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