blob: b4eac59c82546c11e34b39f6ebe376efdc146778 [file] [log] [blame]
developer0f312e82022-11-01 12:31:52 +08001// SPDX-License-Identifier: ISC
2/* Copyright (C) 2020 MediaTek Inc. */
3
4#include <linux/etherdevice.h>
5#include <linux/platform_device.h>
6#include <linux/pci.h>
7#include <linux/module.h>
8#include <net/ipv6.h>
9#include "mt7921.h"
10#include "mcu.h"
11
12static void
13mt7921_gen_ppe_thresh(u8 *he_ppet, int nss)
14{
15 u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
16 static const u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
17
18 he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
19 FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
20 ru_bit_mask);
21
22 ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
23 nss * hweight8(ru_bit_mask) * 2;
24 ppet_size = DIV_ROUND_UP(ppet_bits, 8);
25
26 for (i = 0; i < ppet_size - 1; i++)
27 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
28
29 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
30 (0xff >> (8 - (ppet_bits - 1) % 8));
31}
32
33static int
34mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
35 struct ieee80211_sband_iftype_data *data)
36{
37 int i, idx = 0;
38 int nss = hweight8(phy->mt76->chainmask);
39 u16 mcs_map = 0;
40
41 for (i = 0; i < 8; i++) {
42 if (i < nss)
43 mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
44 else
45 mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
46 }
47
48 for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
49 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
50 struct ieee80211_he_cap_elem *he_cap_elem =
51 &he_cap->he_cap_elem;
52 struct ieee80211_he_mcs_nss_supp *he_mcs =
53 &he_cap->he_mcs_nss_supp;
54
55 switch (i) {
56 case NL80211_IFTYPE_STATION:
57 case NL80211_IFTYPE_AP:
58 break;
59 default:
60 continue;
61 }
62
63 data[idx].types_mask = BIT(i);
64 he_cap->has_he = true;
65
66 he_cap_elem->mac_cap_info[0] =
67 IEEE80211_HE_MAC_CAP0_HTC_HE;
68 he_cap_elem->mac_cap_info[3] =
69 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
70 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
71 he_cap_elem->mac_cap_info[4] =
72 IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
73
74 if (band == NL80211_BAND_2GHZ)
75 he_cap_elem->phy_cap_info[0] =
76 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
77 else
78 he_cap_elem->phy_cap_info[0] =
79 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
80
81 he_cap_elem->phy_cap_info[1] =
82 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
83 he_cap_elem->phy_cap_info[2] =
84 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
85 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
86 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
87 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
88 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
89
90 switch (i) {
91 case NL80211_IFTYPE_AP:
92 he_cap_elem->mac_cap_info[2] |=
93 IEEE80211_HE_MAC_CAP2_BSR;
94 he_cap_elem->mac_cap_info[4] |=
95 IEEE80211_HE_MAC_CAP4_BQR;
96 he_cap_elem->mac_cap_info[5] |=
97 IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
98 he_cap_elem->phy_cap_info[3] |=
99 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
100 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
101 he_cap_elem->phy_cap_info[6] |=
102 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
103 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
104 he_cap_elem->phy_cap_info[9] |=
105 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
106 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
107 break;
108 case NL80211_IFTYPE_STATION:
109 he_cap_elem->mac_cap_info[1] |=
110 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
111
112 if (band == NL80211_BAND_2GHZ)
113 he_cap_elem->phy_cap_info[0] |=
114 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
115 else
116 he_cap_elem->phy_cap_info[0] |=
117 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
118
119 he_cap_elem->phy_cap_info[1] |=
120 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
121 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
122 he_cap_elem->phy_cap_info[3] |=
123 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
124 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
125 he_cap_elem->phy_cap_info[4] |=
126 IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
127 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
128 he_cap_elem->phy_cap_info[5] |=
129 IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
130 IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
131 he_cap_elem->phy_cap_info[6] |=
132 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
133 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
134 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
135 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
136 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
137 he_cap_elem->phy_cap_info[7] |=
138 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
139 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
140 he_cap_elem->phy_cap_info[8] |=
141 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
142 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
143 he_cap_elem->phy_cap_info[9] |=
144 IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
145 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
146 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
147 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
148 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
149 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
150
151 if (is_mt7922(phy->mt76->dev)) {
152 he_cap_elem->phy_cap_info[0] |=
153 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
154 he_cap_elem->phy_cap_info[8] |=
155 IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
156 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
157 }
158 break;
159 }
160
161 he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
162 he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
163 if (is_mt7922(phy->mt76->dev)) {
164 he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
165 he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
166 }
167
168 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
169 if (he_cap_elem->phy_cap_info[6] &
170 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
171 mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
172 } else {
173 he_cap_elem->phy_cap_info[9] |=
174 IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
175 }
176
177 if (band == NL80211_BAND_6GHZ) {
178 struct ieee80211_supported_band *sband =
179 &phy->mt76->sband_5g.sband;
180 struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
181 struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
182 u32 exp;
183 u16 cap;
184
185 cap = u16_encode_bits(ht_cap->ampdu_density,
186 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
187 exp = u32_get_bits(vht_cap->cap,
188 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
189 cap |= u16_encode_bits(exp,
190 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
191 exp = u32_get_bits(vht_cap->cap,
192 IEEE80211_VHT_CAP_MAX_MPDU_MASK);
193 cap |= u16_encode_bits(exp,
194 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
195 if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
196 cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
197 if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
198 cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
199
200 data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
201 }
202 idx++;
203 }
204
205 return idx;
206}
207
208void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
209{
210 struct ieee80211_sband_iftype_data *data;
211 struct ieee80211_supported_band *band;
212 int n;
213
214 if (phy->mt76->cap.has_2ghz) {
215 data = phy->iftype[NL80211_BAND_2GHZ];
216 n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
217
218 band = &phy->mt76->sband_2g.sband;
219 band->iftype_data = data;
220 band->n_iftype_data = n;
221 }
222
223 if (phy->mt76->cap.has_5ghz) {
224 data = phy->iftype[NL80211_BAND_5GHZ];
225 n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
226
227 band = &phy->mt76->sband_5g.sband;
228 band->iftype_data = data;
229 band->n_iftype_data = n;
230
231 if (phy->mt76->cap.has_6ghz) {
232 data = phy->iftype[NL80211_BAND_6GHZ];
233 n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
234
235 band = &phy->mt76->sband_6g.sband;
236 band->iftype_data = data;
237 band->n_iftype_data = n;
238 }
239 }
240}
241
242int __mt7921_start(struct mt7921_phy *phy)
243{
244 struct mt76_phy *mphy = phy->mt76;
245 int err;
246
247 err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
248 if (err)
249 return err;
250
251 err = mt76_connac_mcu_set_channel_domain(mphy);
252 if (err)
253 return err;
254
255 err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
256 if (err)
257 return err;
258
259 err = mt7921_set_tx_sar_pwr(mphy->hw, NULL);
260 if (err)
261 return err;
262
263 mt7921_mac_reset_counters(phy);
264 set_bit(MT76_STATE_RUNNING, &mphy->state);
265
266 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
267 MT7921_WATCHDOG_TIME);
268
269 return 0;
270}
271EXPORT_SYMBOL_GPL(__mt7921_start);
272
273static int mt7921_start(struct ieee80211_hw *hw)
274{
275 struct mt7921_phy *phy = mt7921_hw_phy(hw);
276 int err;
277
278 mt7921_mutex_acquire(phy->dev);
279 err = __mt7921_start(phy);
280 mt7921_mutex_release(phy->dev);
281
282 return err;
283}
284
285void mt7921_stop(struct ieee80211_hw *hw)
286{
287 struct mt7921_dev *dev = mt7921_hw_dev(hw);
288 struct mt7921_phy *phy = mt7921_hw_phy(hw);
289
290 cancel_delayed_work_sync(&phy->mt76->mac_work);
291
292 cancel_delayed_work_sync(&dev->pm.ps_work);
293 cancel_work_sync(&dev->pm.wake_work);
294 cancel_work_sync(&dev->reset_work);
295 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
296
297 mt7921_mutex_acquire(dev);
298 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
299 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
300 mt7921_mutex_release(dev);
301}
302EXPORT_SYMBOL_GPL(mt7921_stop);
303
304static int mt7921_add_interface(struct ieee80211_hw *hw,
305 struct ieee80211_vif *vif)
306{
307 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
308 struct mt7921_dev *dev = mt7921_hw_dev(hw);
309 struct mt7921_phy *phy = mt7921_hw_phy(hw);
310 struct mt76_txq *mtxq;
311 int idx, ret = 0;
312
313 mt7921_mutex_acquire(dev);
314
315 mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
316 if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
317 ret = -ENOSPC;
318 goto out;
319 }
320
321 mvif->mt76.omac_idx = mvif->mt76.idx;
322 mvif->phy = phy;
323 mvif->mt76.band_idx = 0;
324 mvif->mt76.wmm_idx = mvif->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
325
326 ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
327 true);
328 if (ret)
329 goto out;
330
331 dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
332 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
333
334 idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
335
336 INIT_LIST_HEAD(&mvif->sta.poll_list);
337 mvif->sta.wcid.idx = idx;
338 mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
339 mvif->sta.wcid.hw_key_idx = -1;
340 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
341 mt76_packet_id_init(&mvif->sta.wcid);
342
343 mt7921_mac_wtbl_update(dev, idx,
344 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
345
346 ewma_rssi_init(&mvif->rssi);
347
348 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
349 if (vif->txq) {
350 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
351 mtxq->wcid = idx;
352 }
353
354 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
355out:
356 mt7921_mutex_release(dev);
357
358 return ret;
359}
360
361static void mt7921_remove_interface(struct ieee80211_hw *hw,
362 struct ieee80211_vif *vif)
363{
364 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
365 struct mt7921_sta *msta = &mvif->sta;
366 struct mt7921_dev *dev = mt7921_hw_dev(hw);
367 struct mt7921_phy *phy = mt7921_hw_phy(hw);
368 int idx = msta->wcid.idx;
369
370 mt7921_mutex_acquire(dev);
371 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
372 mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
373
374 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
375
376 dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
377 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
378 mt7921_mutex_release(dev);
379
380 spin_lock_bh(&dev->sta_poll_lock);
381 if (!list_empty(&msta->poll_list))
382 list_del_init(&msta->poll_list);
383 spin_unlock_bh(&dev->sta_poll_lock);
384
385 mt76_packet_id_flush(&dev->mt76, &msta->wcid);
386}
387
388static int mt7921_set_channel(struct mt7921_phy *phy)
389{
390 struct mt7921_dev *dev = phy->dev;
391 int ret;
392
393 cancel_delayed_work_sync(&phy->mt76->mac_work);
394
395 mt7921_mutex_acquire(dev);
396 set_bit(MT76_RESET, &phy->mt76->state);
397
398 mt76_set_channel(phy->mt76);
399
400 ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
401 if (ret)
402 goto out;
403
404 mt7921_mac_set_timing(phy);
405
406 mt7921_mac_reset_counters(phy);
407 phy->noise = 0;
408
409out:
410 clear_bit(MT76_RESET, &phy->mt76->state);
411 mt7921_mutex_release(dev);
412
413 mt76_worker_schedule(&dev->mt76.tx_worker);
414 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
415 MT7921_WATCHDOG_TIME);
416
417 return ret;
418}
419
420static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
421 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
422 struct ieee80211_key_conf *key)
423{
424 struct mt7921_dev *dev = mt7921_hw_dev(hw);
425 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
426 struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
427 &mvif->sta;
428 struct mt76_wcid *wcid = &msta->wcid;
429 u8 *wcid_keyidx = &wcid->hw_key_idx;
430 int idx = key->keyidx, err = 0;
431
432 /* The hardware does not support per-STA RX GTK, fallback
433 * to software mode for these.
434 */
435 if ((vif->type == NL80211_IFTYPE_ADHOC ||
436 vif->type == NL80211_IFTYPE_MESH_POINT) &&
437 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
438 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
439 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
440 return -EOPNOTSUPP;
441
442 /* fall back to sw encryption for unsupported ciphers */
443 switch (key->cipher) {
444 case WLAN_CIPHER_SUITE_AES_CMAC:
445 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
446 wcid_keyidx = &wcid->hw_key_idx2;
447 break;
448 case WLAN_CIPHER_SUITE_WEP40:
449 case WLAN_CIPHER_SUITE_WEP104:
450 if (!mvif->wep_sta)
451 return -EOPNOTSUPP;
452 break;
453 case WLAN_CIPHER_SUITE_TKIP:
454 case WLAN_CIPHER_SUITE_CCMP:
455 case WLAN_CIPHER_SUITE_CCMP_256:
456 case WLAN_CIPHER_SUITE_GCMP:
457 case WLAN_CIPHER_SUITE_GCMP_256:
458 case WLAN_CIPHER_SUITE_SMS4:
459 break;
460 default:
461 return -EOPNOTSUPP;
462 }
463
464 mt7921_mutex_acquire(dev);
465
466 if (cmd == SET_KEY)
467 *wcid_keyidx = idx;
468 else if (idx == *wcid_keyidx)
469 *wcid_keyidx = -1;
470 else
471 goto out;
472
473 mt76_wcid_key_setup(&dev->mt76, wcid,
474 cmd == SET_KEY ? key : NULL);
475
476 err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
477 key, MCU_UNI_CMD(STA_REC_UPDATE),
478 &msta->wcid, cmd);
479 if (err)
480 goto out;
481
482 if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
483 key->cipher == WLAN_CIPHER_SUITE_WEP40)
484 err = mt76_connac_mcu_add_key(&dev->mt76, vif,
485 &mvif->wep_sta->bip,
486 key, MCU_UNI_CMD(STA_REC_UPDATE),
487 &mvif->wep_sta->wcid, cmd);
488out:
489 mt7921_mutex_release(dev);
490
491 return err;
492}
493
494static void
495mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
496{
497 struct mt7921_dev *dev = priv;
498 struct ieee80211_hw *hw = mt76_hw(dev);
499 bool pm_enable = dev->pm.enable;
500 int err;
501
502 err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable);
503 if (err < 0)
504 return;
505
506 if (pm_enable) {
507 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
508 ieee80211_hw_set(hw, CONNECTION_MONITOR);
509 } else {
510 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
511 __clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
512 }
513}
514
515static void
516mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
517{
518 struct mt7921_dev *dev = priv;
519 struct ieee80211_hw *hw = mt76_hw(dev);
520 struct mt76_connac_pm *pm = &dev->pm;
521 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
522
523 mt7921_mcu_set_sniffer(dev, vif, monitor);
524 pm->enable = pm->enable_user && !monitor;
525 pm->ds_enable = pm->ds_enable_user && !monitor;
526
527 mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
528
529 if (monitor)
530 mt7921_mcu_set_beacon_filter(dev, vif, false);
531}
532
533void mt7921_set_runtime_pm(struct mt7921_dev *dev)
534{
535 struct ieee80211_hw *hw = mt76_hw(dev);
536 struct mt76_connac_pm *pm = &dev->pm;
537 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
538
539 pm->enable = pm->enable_user && !monitor;
540 ieee80211_iterate_active_interfaces(hw,
541 IEEE80211_IFACE_ITER_RESUME_ALL,
542 mt7921_pm_interface_iter, dev);
543 pm->ds_enable = pm->ds_enable_user && !monitor;
544 mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
545}
546
547static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
548{
549 struct mt7921_dev *dev = mt7921_hw_dev(hw);
550 struct mt7921_phy *phy = mt7921_hw_phy(hw);
551 int ret = 0;
552
553 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
554 ieee80211_stop_queues(hw);
555 ret = mt7921_set_channel(phy);
556 if (ret)
557 return ret;
558 ieee80211_wake_queues(hw);
559 }
560
561 mt7921_mutex_acquire(dev);
562
563 if (changed & IEEE80211_CONF_CHANGE_POWER) {
564 ret = mt7921_set_tx_sar_pwr(hw, NULL);
565 if (ret)
566 goto out;
567 }
568
569 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
570 ieee80211_iterate_active_interfaces(hw,
571 IEEE80211_IFACE_ITER_RESUME_ALL,
572 mt7921_sniffer_interface_iter, dev);
573 dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
574 }
575
576out:
577 mt7921_mutex_release(dev);
578
579 return ret;
580}
581
582static int
583mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
584 const struct ieee80211_tx_queue_params *params)
585{
586 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
587
588 /* no need to update right away, we'll get BSS_CHANGED_QOS */
589 queue = mt76_connac_lmac_mapping(queue);
590 mvif->queue_params[queue] = *params;
591
592 return 0;
593}
594
595static void mt7921_configure_filter(struct ieee80211_hw *hw,
596 unsigned int changed_flags,
597 unsigned int *total_flags,
598 u64 multicast)
599{
600 struct mt7921_dev *dev = mt7921_hw_dev(hw);
601 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
602 MT_WF_RFCR1_DROP_BF_POLL |
603 MT_WF_RFCR1_DROP_BA |
604 MT_WF_RFCR1_DROP_CFEND |
605 MT_WF_RFCR1_DROP_CFACK;
606 u32 flags = 0;
607
608#define MT76_FILTER(_flag, _hw) do { \
609 flags |= *total_flags & FIF_##_flag; \
610 dev->mt76.rxfilter &= ~(_hw); \
611 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
612 } while (0)
613
614 mt7921_mutex_acquire(dev);
615
616 dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
617 MT_WF_RFCR_DROP_OTHER_BEACON |
618 MT_WF_RFCR_DROP_FRAME_REPORT |
619 MT_WF_RFCR_DROP_PROBEREQ |
620 MT_WF_RFCR_DROP_MCAST_FILTERED |
621 MT_WF_RFCR_DROP_MCAST |
622 MT_WF_RFCR_DROP_BCAST |
623 MT_WF_RFCR_DROP_DUPLICATE |
624 MT_WF_RFCR_DROP_A2_BSSID |
625 MT_WF_RFCR_DROP_UNWANTED_CTL |
626 MT_WF_RFCR_DROP_STBC_MULTI);
627
628 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
629 MT_WF_RFCR_DROP_A3_MAC |
630 MT_WF_RFCR_DROP_A3_BSSID);
631
632 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
633
634 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
635 MT_WF_RFCR_DROP_RTS |
636 MT_WF_RFCR_DROP_CTL_RSV |
637 MT_WF_RFCR_DROP_NDPA);
638
639 *total_flags = flags;
640 mt76_wr(dev, MT_WF_RFCR(0), dev->mt76.rxfilter);
641
642 if (*total_flags & FIF_CONTROL)
643 mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
644 else
645 mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
646
647 mt7921_mutex_release(dev);
648}
649
650static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
651 struct ieee80211_vif *vif,
652 struct ieee80211_bss_conf *info,
653 u32 changed)
654{
655 struct mt7921_phy *phy = mt7921_hw_phy(hw);
656 struct mt7921_dev *dev = mt7921_hw_dev(hw);
657
658 mt7921_mutex_acquire(dev);
659
660 if (changed & BSS_CHANGED_ERP_SLOT) {
661 int slottime = info->use_short_slot ? 9 : 20;
662
663 if (slottime != phy->slottime) {
664 phy->slottime = slottime;
665 mt7921_mac_set_timing(phy);
666 }
667 }
668
669 if (changed & (BSS_CHANGED_BEACON |
670 BSS_CHANGED_BEACON_ENABLED))
671 mt7921_mcu_uni_add_beacon_offload(dev, hw, vif,
672 info->enable_beacon);
673
674 /* ensure that enable txcmd_mode after bss_info */
675 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
676 mt7921_mcu_set_tx(dev, vif);
677
678 if (changed & BSS_CHANGED_PS)
679 mt7921_mcu_uni_bss_ps(dev, vif);
680
681 if (changed & BSS_CHANGED_ASSOC) {
682 mt7921_mcu_sta_update(dev, NULL, vif, true,
683 MT76_STA_INFO_STATE_ASSOC);
684 mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
685 }
686
687 if (changed & BSS_CHANGED_ARP_FILTER) {
688 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
689
690 mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
691 info);
692 }
693
694 mt7921_mutex_release(dev);
695}
696
697int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
698 struct ieee80211_sta *sta)
699{
700 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
701 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
702 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
703 int ret, idx;
704
705 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
706 if (idx < 0)
707 return -ENOSPC;
708
709 INIT_LIST_HEAD(&msta->poll_list);
710 msta->vif = mvif;
711 msta->wcid.sta = 1;
712 msta->wcid.idx = idx;
713 msta->wcid.phy_idx = mvif->mt76.band_idx;
714 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
715 msta->last_txs = jiffies;
716
717 ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
718 if (ret)
719 return ret;
720
721 if (vif->type == NL80211_IFTYPE_STATION)
722 mvif->wep_sta = msta;
723
724 mt7921_mac_wtbl_update(dev, idx,
725 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
726
727 ret = mt7921_mcu_sta_update(dev, sta, vif, true,
728 MT76_STA_INFO_STATE_NONE);
729 if (ret)
730 return ret;
731
732 mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
733
734 return 0;
735}
736EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
737
738void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
739 struct ieee80211_sta *sta)
740{
741 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
742 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
743 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
744
745 mt7921_mutex_acquire(dev);
746
747 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
748 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
749 true);
750
751 mt7921_mac_wtbl_update(dev, msta->wcid.idx,
752 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
753 memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
754
755 mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
756
757 mt7921_mutex_release(dev);
758}
759EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
760
761void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
762 struct ieee80211_sta *sta)
763{
764 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
765 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
766
767 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
768 mt76_connac_pm_wake(&dev->mphy, &dev->pm);
769
770 mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
771 mt7921_mac_wtbl_update(dev, msta->wcid.idx,
772 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
773
774 if (vif->type == NL80211_IFTYPE_STATION) {
775 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
776
777 mvif->wep_sta = NULL;
778 ewma_rssi_init(&mvif->rssi);
779 if (!sta->tdls)
780 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
781 &mvif->sta.wcid, false);
782 }
783
784 spin_lock_bh(&dev->sta_poll_lock);
785 if (!list_empty(&msta->poll_list))
786 list_del_init(&msta->poll_list);
787 spin_unlock_bh(&dev->sta_poll_lock);
788
789 mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
790}
791EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
792
793void mt7921_tx_worker(struct mt76_worker *w)
794{
795 struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
796 mt76.tx_worker);
797
798 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
799 queue_work(dev->mt76.wq, &dev->pm.wake_work);
800 return;
801 }
802
803 mt76_txq_schedule_all(&dev->mphy);
804 mt76_connac_pm_unref(&dev->mphy, &dev->pm);
805}
806
807static void mt7921_tx(struct ieee80211_hw *hw,
808 struct ieee80211_tx_control *control,
809 struct sk_buff *skb)
810{
811 struct mt7921_dev *dev = mt7921_hw_dev(hw);
812 struct mt76_phy *mphy = hw->priv;
813 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
814 struct ieee80211_vif *vif = info->control.vif;
815 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
816 int qid;
817
818 if (control->sta) {
819 struct mt7921_sta *sta;
820
821 sta = (struct mt7921_sta *)control->sta->drv_priv;
822 wcid = &sta->wcid;
823 }
824
825 if (vif && !control->sta) {
826 struct mt7921_vif *mvif;
827
828 mvif = (struct mt7921_vif *)vif->drv_priv;
829 wcid = &mvif->sta.wcid;
830 }
831
832 if (mt76_connac_pm_ref(mphy, &dev->pm)) {
833 mt76_tx(mphy, control->sta, wcid, skb);
834 mt76_connac_pm_unref(mphy, &dev->pm);
835 return;
836 }
837
838 qid = skb_get_queue_mapping(skb);
839 if (qid >= MT_TXQ_PSD) {
840 qid = IEEE80211_AC_BE;
841 skb_set_queue_mapping(skb, qid);
842 }
843
844 mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
845}
846
847static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
848{
849 struct mt7921_dev *dev = mt7921_hw_dev(hw);
850
851 mt7921_mutex_acquire(dev);
852 mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
853 mt7921_mutex_release(dev);
854
855 return 0;
856}
857
858static int
859mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
860 struct ieee80211_ampdu_params *params)
861{
862 enum ieee80211_ampdu_mlme_action action = params->action;
863 struct mt7921_dev *dev = mt7921_hw_dev(hw);
864 struct ieee80211_sta *sta = params->sta;
865 struct ieee80211_txq *txq = sta->txq[params->tid];
866 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
867 u16 tid = params->tid;
868 u16 ssn = params->ssn;
869 struct mt76_txq *mtxq;
870 int ret = 0;
871
872 if (!txq)
873 return -EINVAL;
874
875 mtxq = (struct mt76_txq *)txq->drv_priv;
876
877 mt7921_mutex_acquire(dev);
878 switch (action) {
879 case IEEE80211_AMPDU_RX_START:
880 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
881 params->buf_size);
882 mt7921_mcu_uni_rx_ba(dev, params, true);
883 break;
884 case IEEE80211_AMPDU_RX_STOP:
885 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
886 mt7921_mcu_uni_rx_ba(dev, params, false);
887 break;
888 case IEEE80211_AMPDU_TX_OPERATIONAL:
889 mtxq->aggr = true;
890 mtxq->send_bar = false;
891 mt7921_mcu_uni_tx_ba(dev, params, true);
892 break;
893 case IEEE80211_AMPDU_TX_STOP_FLUSH:
894 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
895 mtxq->aggr = false;
896 clear_bit(tid, &msta->ampdu_state);
897 mt7921_mcu_uni_tx_ba(dev, params, false);
898 break;
899 case IEEE80211_AMPDU_TX_START:
900 set_bit(tid, &msta->ampdu_state);
901 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
902 break;
903 case IEEE80211_AMPDU_TX_STOP_CONT:
904 mtxq->aggr = false;
905 clear_bit(tid, &msta->ampdu_state);
906 mt7921_mcu_uni_tx_ba(dev, params, false);
907 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
908 break;
909 }
910 mt7921_mutex_release(dev);
911
912 return ret;
913}
914
915static int mt7921_sta_state(struct ieee80211_hw *hw,
916 struct ieee80211_vif *vif,
917 struct ieee80211_sta *sta,
918 enum ieee80211_sta_state old_state,
919 enum ieee80211_sta_state new_state)
920{
921 struct mt7921_dev *dev = mt7921_hw_dev(hw);
922
923 if (dev->pm.ds_enable) {
924 mt7921_mutex_acquire(dev);
925 mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
926 mt7921_mutex_release(dev);
927 }
928
929 return mt76_sta_state(hw, vif, sta, old_state, new_state);
930}
931
932static int
933mt7921_get_stats(struct ieee80211_hw *hw,
934 struct ieee80211_low_level_stats *stats)
935{
936 struct mt7921_phy *phy = mt7921_hw_phy(hw);
937 struct mib_stats *mib = &phy->mib;
938
939 mt7921_mutex_acquire(phy->dev);
940
941 stats->dot11RTSSuccessCount = mib->rts_cnt;
942 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
943 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
944 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
945
946 mt7921_mutex_release(phy->dev);
947
948 return 0;
949}
950
951static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
952 /* tx counters */
953 "tx_ampdu_cnt",
954 "tx_mpdu_attempts",
955 "tx_mpdu_success",
956 "tx_pkt_ebf_cnt",
957 "tx_pkt_ibf_cnt",
958 "tx_ampdu_len:0-1",
959 "tx_ampdu_len:2-10",
960 "tx_ampdu_len:11-19",
961 "tx_ampdu_len:20-28",
962 "tx_ampdu_len:29-37",
963 "tx_ampdu_len:38-46",
964 "tx_ampdu_len:47-55",
965 "tx_ampdu_len:56-79",
966 "tx_ampdu_len:80-103",
967 "tx_ampdu_len:104-127",
968 "tx_ampdu_len:128-151",
969 "tx_ampdu_len:152-175",
970 "tx_ampdu_len:176-199",
971 "tx_ampdu_len:200-223",
972 "tx_ampdu_len:224-247",
973 "ba_miss_count",
974 "tx_beamformer_ppdu_iBF",
975 "tx_beamformer_ppdu_eBF",
976 "tx_beamformer_rx_feedback_all",
977 "tx_beamformer_rx_feedback_he",
978 "tx_beamformer_rx_feedback_vht",
979 "tx_beamformer_rx_feedback_ht",
980 "tx_msdu_pack_1",
981 "tx_msdu_pack_2",
982 "tx_msdu_pack_3",
983 "tx_msdu_pack_4",
984 "tx_msdu_pack_5",
985 "tx_msdu_pack_6",
986 "tx_msdu_pack_7",
987 "tx_msdu_pack_8",
988 /* rx counters */
989 "rx_mpdu_cnt",
990 "rx_ampdu_cnt",
991 "rx_ampdu_bytes_cnt",
992 "rx_ba_cnt",
993 /* per vif counters */
994 "v_tx_mode_cck",
995 "v_tx_mode_ofdm",
996 "v_tx_mode_ht",
997 "v_tx_mode_ht_gf",
998 "v_tx_mode_vht",
999 "v_tx_mode_he_su",
1000 "v_tx_mode_he_ext_su",
1001 "v_tx_mode_he_tb",
1002 "v_tx_mode_he_mu",
1003 "v_tx_bw_20",
1004 "v_tx_bw_40",
1005 "v_tx_bw_80",
1006 "v_tx_bw_160",
1007 "v_tx_mcs_0",
1008 "v_tx_mcs_1",
1009 "v_tx_mcs_2",
1010 "v_tx_mcs_3",
1011 "v_tx_mcs_4",
1012 "v_tx_mcs_5",
1013 "v_tx_mcs_6",
1014 "v_tx_mcs_7",
1015 "v_tx_mcs_8",
1016 "v_tx_mcs_9",
1017 "v_tx_mcs_10",
1018 "v_tx_mcs_11",
1019};
1020
1021static void
1022mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1023 u32 sset, u8 *data)
1024{
1025 if (sset != ETH_SS_STATS)
1026 return;
1027
1028 memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
1029}
1030
1031static int
1032mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1033 int sset)
1034{
1035 return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0;
1036}
1037
1038static void
1039mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1040{
1041 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1042 struct mt76_ethtool_worker_info *wi = wi_data;
1043
1044 if (msta->vif->mt76.idx != wi->idx)
1045 return;
1046
1047 mt76_ethtool_worker(wi, &msta->wcid.stats);
1048}
1049
1050static
1051void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1052 struct ethtool_stats *stats, u64 *data)
1053{
1054 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1055 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1056 struct mt7921_dev *dev = phy->dev;
1057 struct mib_stats *mib = &phy->mib;
1058 struct mt76_ethtool_worker_info wi = {
1059 .data = data,
1060 .idx = mvif->mt76.idx,
1061 };
1062 int i, ei = 0;
1063
1064 mt7921_mutex_acquire(dev);
1065
1066 mt7921_mac_update_mib_stats(phy);
1067
1068 data[ei++] = mib->tx_ampdu_cnt;
1069 data[ei++] = mib->tx_mpdu_attempts_cnt;
1070 data[ei++] = mib->tx_mpdu_success_cnt;
1071 data[ei++] = mib->tx_pkt_ebf_cnt;
1072 data[ei++] = mib->tx_pkt_ibf_cnt;
1073
1074 /* Tx ampdu stat */
1075 for (i = 0; i < 15; i++)
1076 data[ei++] = dev->mt76.aggr_stats[i];
1077
1078 data[ei++] = phy->mib.ba_miss_cnt;
1079
1080 /* Tx Beamformer monitor */
1081 data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1082 data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1083
1084 /* Tx Beamformer Rx feedback monitor */
1085 data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1086 data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1087 data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1088 data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1089
1090 /* Tx amsdu info (pack-count histogram) */
1091 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1092 data[ei++] = mib->tx_amsdu[i];
1093
1094 /* rx counters */
1095 data[ei++] = mib->rx_mpdu_cnt;
1096 data[ei++] = mib->rx_ampdu_cnt;
1097 data[ei++] = mib->rx_ampdu_bytes_cnt;
1098 data[ei++] = mib->rx_ba_cnt;
1099
1100 /* Add values for all stations owned by this vif */
1101 wi.initial_stat_idx = ei;
1102 ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1103
1104 mt7921_mutex_release(dev);
1105
1106 if (!wi.sta_count)
1107 return;
1108
1109 ei += wi.worker_stat_count;
1110 if (ei != ARRAY_SIZE(mt7921_gstrings_stats))
1111 dev_err(dev->mt76.dev, "ei: %d SSTATS_LEN: %zu",
1112 ei, ARRAY_SIZE(mt7921_gstrings_stats));
1113}
1114
1115static u64
1116mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1117{
1118 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1119 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1120 u8 omac_idx = mvif->mt76.omac_idx;
1121 union {
1122 u64 t64;
1123 u32 t32[2];
1124 } tsf;
1125 u16 n;
1126
1127 mt7921_mutex_acquire(dev);
1128
1129 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1130 /* TSF software read */
1131 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1132 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1133 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1134
1135 mt7921_mutex_release(dev);
1136
1137 return tsf.t64;
1138}
1139
1140static void
1141mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1142 u64 timestamp)
1143{
1144 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1145 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1146 u8 omac_idx = mvif->mt76.omac_idx;
1147 union {
1148 u64 t64;
1149 u32 t32[2];
1150 } tsf = { .t64 = timestamp, };
1151 u16 n;
1152
1153 mt7921_mutex_acquire(dev);
1154
1155 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1156 mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1157 mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1158 /* TSF software overwrite */
1159 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1160
1161 mt7921_mutex_release(dev);
1162}
1163
1164static void
1165mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1166{
1167 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1168 struct mt7921_dev *dev = phy->dev;
1169
1170 mt7921_mutex_acquire(dev);
1171 phy->coverage_class = max_t(s16, coverage_class, 0);
1172 mt7921_mac_set_timing(phy);
1173 mt7921_mutex_release(dev);
1174}
1175
1176void mt7921_scan_work(struct work_struct *work)
1177{
1178 struct mt7921_phy *phy;
1179
1180 phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1181 scan_work.work);
1182
1183 while (true) {
1184 struct mt76_connac2_mcu_rxd *rxd;
1185 struct sk_buff *skb;
1186
1187 spin_lock_bh(&phy->dev->mt76.lock);
1188 skb = __skb_dequeue(&phy->scan_event_list);
1189 spin_unlock_bh(&phy->dev->mt76.lock);
1190
1191 if (!skb)
1192 break;
1193
1194 rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1195 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1196 ieee80211_sched_scan_results(phy->mt76->hw);
1197 } else if (test_and_clear_bit(MT76_HW_SCANNING,
1198 &phy->mt76->state)) {
1199 struct cfg80211_scan_info info = {
1200 .aborted = false,
1201 };
1202
1203 ieee80211_scan_completed(phy->mt76->hw, &info);
1204 }
1205 dev_kfree_skb(skb);
1206 }
1207}
1208
1209static int
1210mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1211 struct ieee80211_scan_request *req)
1212{
1213 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1214 struct mt76_phy *mphy = hw->priv;
1215 int err;
1216
1217 mt7921_mutex_acquire(dev);
1218 err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1219 mt7921_mutex_release(dev);
1220
1221 return err;
1222}
1223
1224static void
1225mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1226{
1227 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1228 struct mt76_phy *mphy = hw->priv;
1229
1230 mt7921_mutex_acquire(dev);
1231 mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1232 mt7921_mutex_release(dev);
1233}
1234
1235static int
1236mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1237 struct cfg80211_sched_scan_request *req,
1238 struct ieee80211_scan_ies *ies)
1239{
1240 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1241 struct mt76_phy *mphy = hw->priv;
1242 int err;
1243
1244 mt7921_mutex_acquire(dev);
1245
1246 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1247 if (err < 0)
1248 goto out;
1249
1250 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1251out:
1252 mt7921_mutex_release(dev);
1253
1254 return err;
1255}
1256
1257static int
1258mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1259{
1260 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1261 struct mt76_phy *mphy = hw->priv;
1262 int err;
1263
1264 mt7921_mutex_acquire(dev);
1265 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1266 mt7921_mutex_release(dev);
1267
1268 return err;
1269}
1270
1271static int
1272mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1273{
1274 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1275 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1276 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1277
1278 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1279 return -EINVAL;
1280
1281 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1282 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1283
1284 mt7921_mutex_acquire(dev);
1285
1286 phy->mt76->antenna_mask = tx_ant;
1287 phy->mt76->chainmask = tx_ant;
1288
1289 mt76_set_stream_caps(phy->mt76, true);
1290 mt7921_set_stream_he_caps(phy);
1291
1292 mt7921_mutex_release(dev);
1293
1294 return 0;
1295}
1296
1297static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1298 struct ieee80211_vif *vif,
1299 struct ieee80211_sta *sta,
1300 struct station_info *sinfo)
1301{
1302 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1303 struct rate_info *txrate = &msta->wcid.rate;
1304
1305 if (!txrate->legacy && !txrate->flags)
1306 return;
1307
1308 if (txrate->legacy) {
1309 sinfo->txrate.legacy = txrate->legacy;
1310 } else {
1311 sinfo->txrate.mcs = txrate->mcs;
1312 sinfo->txrate.nss = txrate->nss;
1313 sinfo->txrate.bw = txrate->bw;
1314 sinfo->txrate.he_gi = txrate->he_gi;
1315 sinfo->txrate.he_dcm = txrate->he_dcm;
1316 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1317 }
1318 sinfo->txrate.flags = txrate->flags;
1319 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1320}
1321
1322#ifdef CONFIG_PM
1323static int mt7921_suspend(struct ieee80211_hw *hw,
1324 struct cfg80211_wowlan *wowlan)
1325{
1326 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1327 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1328
1329 cancel_delayed_work_sync(&phy->scan_work);
1330 cancel_delayed_work_sync(&phy->mt76->mac_work);
1331
1332 cancel_delayed_work_sync(&dev->pm.ps_work);
1333 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1334
1335 mt7921_mutex_acquire(dev);
1336
1337 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1338 ieee80211_iterate_active_interfaces(hw,
1339 IEEE80211_IFACE_ITER_RESUME_ALL,
1340 mt7921_mcu_set_suspend_iter,
1341 &dev->mphy);
1342
1343 mt7921_mutex_release(dev);
1344
1345 return 0;
1346}
1347
1348static int mt7921_resume(struct ieee80211_hw *hw)
1349{
1350 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1351 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1352
1353 mt7921_mutex_acquire(dev);
1354
1355 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1356 ieee80211_iterate_active_interfaces(hw,
1357 IEEE80211_IFACE_ITER_RESUME_ALL,
1358 mt76_connac_mcu_set_suspend_iter,
1359 &dev->mphy);
1360
1361 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1362 MT7921_WATCHDOG_TIME);
1363
1364 mt7921_mutex_release(dev);
1365
1366 return 0;
1367}
1368
1369static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1370{
1371 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1372 struct mt76_dev *mdev = &dev->mt76;
1373
1374 device_set_wakeup_enable(mdev->dev, enabled);
1375}
1376
1377static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1378 struct ieee80211_vif *vif,
1379 struct cfg80211_gtk_rekey_data *data)
1380{
1381 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1382
1383 mt7921_mutex_acquire(dev);
1384 mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1385 mt7921_mutex_release(dev);
1386}
1387#endif /* CONFIG_PM */
1388
1389static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1390 u32 queues, bool drop)
1391{
1392 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1393
1394 wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1395 HZ / 2);
1396}
1397
1398static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1399 struct ieee80211_vif *vif,
1400 struct ieee80211_sta *sta,
1401 bool enabled)
1402{
1403 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1404 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1405
1406 mt7921_mutex_acquire(dev);
1407
1408 if (enabled)
1409 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1410 else
1411 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1412
1413 mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1414 MCU_UNI_CMD(STA_REC_UPDATE));
1415
1416 mt7921_mutex_release(dev);
1417}
1418
1419#if IS_ENABLED(CONFIG_IPV6)
1420static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1421 struct ieee80211_vif *vif,
1422 struct inet6_dev *idev)
1423{
1424 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1425 struct mt7921_dev *dev = mvif->phy->dev;
1426 struct inet6_ifaddr *ifa;
1427 struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1428 struct sk_buff *skb;
1429 u8 i, idx = 0;
1430
1431 struct {
1432 struct {
1433 u8 bss_idx;
1434 u8 pad[3];
1435 } __packed hdr;
1436 struct mt76_connac_arpns_tlv arpns;
1437 } req_hdr = {
1438 .hdr = {
1439 .bss_idx = mvif->mt76.idx,
1440 },
1441 .arpns = {
1442 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1443 .mode = 2, /* update */
1444 .option = 1, /* update only */
1445 },
1446 };
1447
1448 read_lock_bh(&idev->lock);
1449 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1450 if (ifa->flags & IFA_F_TENTATIVE)
1451 continue;
1452 ns_addrs[idx] = ifa->addr;
1453 if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1454 break;
1455 }
1456 read_unlock_bh(&idev->lock);
1457
1458 if (!idx)
1459 return;
1460
1461 req_hdr.arpns.ips_num = idx;
1462 req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1463 + idx * sizeof(struct in6_addr));
1464 skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1465 sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1466 sizeof(req_hdr), GFP_ATOMIC);
1467 if (!skb)
1468 return;
1469
1470 for (i = 0; i < idx; i++)
1471 skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1472
1473 skb_queue_tail(&dev->ipv6_ns_list, skb);
1474
1475 ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1476}
1477#endif
1478
1479int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1480 const struct cfg80211_sar_specs *sar)
1481{
1482 struct mt76_phy *mphy = hw->priv;
1483 int err;
1484
1485 if (sar) {
1486 err = mt76_init_sar_power(hw, sar);
1487 if (err)
1488 return err;
1489 }
1490
1491 mt7921_init_acpi_sar_power(mt7921_hw_phy(hw), !sar);
1492
1493 err = mt76_connac_mcu_set_rate_txpower(mphy);
1494
1495 return err;
1496}
1497
1498static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1499 const struct cfg80211_sar_specs *sar)
1500{
1501 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1502 int err;
1503
1504 mt7921_mutex_acquire(dev);
1505 err = mt7921_set_tx_sar_pwr(hw, sar);
1506 mt7921_mutex_release(dev);
1507
1508 return err;
1509}
1510
1511static void
1512mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1513 struct ieee80211_vif *vif,
1514 struct cfg80211_chan_def *chandef)
1515{
1516 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1517
1518 mt7921_mutex_acquire(dev);
1519 mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1520 mt7921_mutex_release(dev);
1521}
1522
1523static int
1524mt7921_start_ap(struct ieee80211_hw *hw,
1525 struct ieee80211_vif *vif)
1526{
1527 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1528 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1529 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1530 int err;
1531
1532 mt7921_mutex_acquire(dev);
1533
1534 err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid,
1535 true);
1536 if (err)
1537 goto out;
1538
1539 err = mt7921_mcu_set_bss_pm(dev, vif, true);
1540 if (err)
1541 goto out;
1542
1543 err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1544 MT76_STA_INFO_STATE_NONE);
1545out:
1546 mt7921_mutex_release(dev);
1547
1548 return err;
1549}
1550
1551static void
1552mt7921_stop_ap(struct ieee80211_hw *hw,
1553 struct ieee80211_vif *vif)
1554{
1555 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1556 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1557 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1558 int err;
1559
1560 mt7921_mutex_acquire(dev);
1561
1562 err = mt7921_mcu_set_bss_pm(dev, vif, false);
1563 if (err)
1564 goto out;
1565
1566 mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false);
1567
1568out:
1569 mt7921_mutex_release(dev);
1570}
1571
1572const struct ieee80211_ops mt7921_ops = {
1573 .tx = mt7921_tx,
1574 .start = mt7921_start,
1575 .stop = mt7921_stop,
1576 .add_interface = mt7921_add_interface,
1577 .remove_interface = mt7921_remove_interface,
1578 .config = mt7921_config,
1579 .conf_tx = mt7921_conf_tx,
1580 .configure_filter = mt7921_configure_filter,
1581 .bss_info_changed = mt7921_bss_info_changed,
1582 .start_ap = mt7921_start_ap,
1583 .stop_ap = mt7921_stop_ap,
1584 .sta_state = mt7921_sta_state,
1585 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1586 .set_key = mt7921_set_key,
1587 .sta_set_decap_offload = mt7921_sta_set_decap_offload,
1588#if IS_ENABLED(CONFIG_IPV6)
1589 .ipv6_addr_change = mt7921_ipv6_addr_change,
1590#endif /* CONFIG_IPV6 */
1591 .ampdu_action = mt7921_ampdu_action,
1592 .set_rts_threshold = mt7921_set_rts_threshold,
1593 .wake_tx_queue = mt76_wake_tx_queue,
1594 .release_buffered_frames = mt76_release_buffered_frames,
1595 .channel_switch_beacon = mt7921_channel_switch_beacon,
1596 .get_txpower = mt76_get_txpower,
1597 .get_stats = mt7921_get_stats,
1598 .get_et_sset_count = mt7921_get_et_sset_count,
1599 .get_et_strings = mt7921_get_et_strings,
1600 .get_et_stats = mt7921_get_et_stats,
1601 .get_tsf = mt7921_get_tsf,
1602 .set_tsf = mt7921_set_tsf,
1603 .get_survey = mt76_get_survey,
1604 .get_antenna = mt76_get_antenna,
1605 .set_antenna = mt7921_set_antenna,
1606 .set_coverage_class = mt7921_set_coverage_class,
1607 .hw_scan = mt7921_hw_scan,
1608 .cancel_hw_scan = mt7921_cancel_hw_scan,
1609 .sta_statistics = mt7921_sta_statistics,
1610 .sched_scan_start = mt7921_start_sched_scan,
1611 .sched_scan_stop = mt7921_stop_sched_scan,
1612 CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1613 CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1614#ifdef CONFIG_PM
1615 .suspend = mt7921_suspend,
1616 .resume = mt7921_resume,
1617 .set_wakeup = mt7921_set_wakeup,
1618 .set_rekey_data = mt7921_set_rekey_data,
1619#endif /* CONFIG_PM */
1620 .flush = mt7921_flush,
1621 .set_sar_specs = mt7921_set_sar_specs,
1622};
1623EXPORT_SYMBOL_GPL(mt7921_ops);
1624
1625MODULE_LICENSE("Dual BSD/GPL");
1626MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");