1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2022 MediaTek Inc.
4 */
5
6 #include "mt7996.h"
7 #include "mcu.h"
8 #include "mac.h"
9
mt7996_dev_running(struct mt7996_dev * dev)10 static bool mt7996_dev_running(struct mt7996_dev *dev)
11 {
12 struct mt7996_phy *phy;
13
14 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
15 return true;
16
17 phy = mt7996_phy2(dev);
18 if (phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
19 return true;
20
21 phy = mt7996_phy3(dev);
22
23 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
24 }
25
mt7996_run(struct ieee80211_hw * hw)26 int mt7996_run(struct ieee80211_hw *hw)
27 {
28 struct mt7996_dev *dev = mt7996_hw_dev(hw);
29 struct mt7996_phy *phy = mt7996_hw_phy(hw);
30 bool running;
31 int ret;
32
33 running = mt7996_dev_running(dev);
34 if (!running) {
35 ret = mt7996_mcu_set_hdr_trans(dev, true);
36 if (ret)
37 goto out;
38 }
39
40 mt7996_mac_enable_nf(dev, phy->mt76->band_idx);
41
42 ret = mt7996_mcu_set_rts_thresh(phy, 0x92b);
43 if (ret)
44 goto out;
45
46 ret = mt7996_mcu_set_radio_en(phy, true);
47 if (ret)
48 goto out;
49
50 ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_RX_PATH);
51 if (ret)
52 goto out;
53
54 ret = mt7996_mcu_set_thermal_throttling(phy, MT7996_THERMAL_THROTTLE_MAX);
55 if (ret)
56 goto out;
57
58 ret = mt7996_mcu_set_thermal_protect(phy, true);
59 if (ret)
60 goto out;
61
62 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
63
64 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
65 MT7996_WATCHDOG_TIME);
66
67 if (!running)
68 mt7996_mac_reset_counters(phy);
69
70 out:
71 return ret;
72 }
73
mt7996_start(struct ieee80211_hw * hw)74 static int mt7996_start(struct ieee80211_hw *hw)
75 {
76 struct mt7996_dev *dev = mt7996_hw_dev(hw);
77 int ret;
78
79 flush_work(&dev->init_work);
80
81 mutex_lock(&dev->mt76.mutex);
82 ret = mt7996_run(hw);
83 mutex_unlock(&dev->mt76.mutex);
84
85 return ret;
86 }
87
mt7996_stop(struct ieee80211_hw * hw)88 static void mt7996_stop(struct ieee80211_hw *hw)
89 {
90 struct mt7996_dev *dev = mt7996_hw_dev(hw);
91 struct mt7996_phy *phy = mt7996_hw_phy(hw);
92
93 cancel_delayed_work_sync(&phy->mt76->mac_work);
94
95 mutex_lock(&dev->mt76.mutex);
96
97 mt7996_mcu_set_radio_en(phy, false);
98
99 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
100
101 mutex_unlock(&dev->mt76.mutex);
102 }
103
get_free_idx(u32 mask,u8 start,u8 end)104 static inline int get_free_idx(u32 mask, u8 start, u8 end)
105 {
106 return ffs(~mask & GENMASK(end, start));
107 }
108
get_omac_idx(enum nl80211_iftype type,u64 mask)109 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
110 {
111 int i;
112
113 switch (type) {
114 case NL80211_IFTYPE_MESH_POINT:
115 case NL80211_IFTYPE_ADHOC:
116 case NL80211_IFTYPE_STATION:
117 /* prefer hw bssid slot 1-3 */
118 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
119 if (i)
120 return i - 1;
121
122 if (type != NL80211_IFTYPE_STATION)
123 break;
124
125 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
126 if (i)
127 return i - 1;
128
129 if (~mask & BIT(HW_BSSID_0))
130 return HW_BSSID_0;
131
132 break;
133 case NL80211_IFTYPE_MONITOR:
134 case NL80211_IFTYPE_AP:
135 /* ap uses hw bssid 0 and ext bssid */
136 if (~mask & BIT(HW_BSSID_0))
137 return HW_BSSID_0;
138
139 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
140 if (i)
141 return i - 1;
142
143 break;
144 default:
145 WARN_ON(1);
146 break;
147 }
148
149 return -1;
150 }
151
mt7996_init_bitrate_mask(struct ieee80211_vif * vif)152 static void mt7996_init_bitrate_mask(struct ieee80211_vif *vif)
153 {
154 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
155 int i;
156
157 for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
158 mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
159 mvif->bitrate_mask.control[i].he_gi = 0xff;
160 mvif->bitrate_mask.control[i].he_ltf = 0xff;
161 mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
162 memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
163 sizeof(mvif->bitrate_mask.control[i].ht_mcs));
164 memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
165 sizeof(mvif->bitrate_mask.control[i].vht_mcs));
166 memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
167 sizeof(mvif->bitrate_mask.control[i].he_mcs));
168 }
169 }
170
mt7996_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)171 static int mt7996_add_interface(struct ieee80211_hw *hw,
172 struct ieee80211_vif *vif)
173 {
174 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
175 struct mt7996_dev *dev = mt7996_hw_dev(hw);
176 struct mt7996_phy *phy = mt7996_hw_phy(hw);
177 struct mt76_txq *mtxq;
178 u8 band_idx = phy->mt76->band_idx;
179 int idx, ret = 0;
180
181 mutex_lock(&dev->mt76.mutex);
182
183 if (vif->type == NL80211_IFTYPE_MONITOR &&
184 is_zero_ether_addr(vif->addr))
185 phy->monitor_vif = vif;
186
187 mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
188 if (mvif->mt76.idx >= mt7996_max_interface_num(dev)) {
189 ret = -ENOSPC;
190 goto out;
191 }
192
193 idx = get_omac_idx(vif->type, phy->omac_mask);
194 if (idx < 0) {
195 ret = -ENOSPC;
196 goto out;
197 }
198 mvif->mt76.omac_idx = idx;
199 mvif->phy = phy;
200 mvif->mt76.band_idx = band_idx;
201 mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
202
203 ret = mt7996_mcu_add_dev_info(phy, vif, true);
204 if (ret)
205 goto out;
206
207 dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
208 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
209
210 idx = MT7996_WTBL_RESERVED - mvif->mt76.idx;
211
212 INIT_LIST_HEAD(&mvif->sta.rc_list);
213 INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
214 mvif->sta.wcid.idx = idx;
215 mvif->sta.wcid.phy_idx = band_idx;
216 mvif->sta.wcid.hw_key_idx = -1;
217 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
218 mt76_wcid_init(&mvif->sta.wcid);
219
220 mt7996_mac_wtbl_update(dev, idx,
221 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
222
223 if (vif->txq) {
224 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
225 mtxq->wcid = idx;
226 }
227
228 if (vif->type != NL80211_IFTYPE_AP &&
229 (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
230 vif->offload_flags = 0;
231 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
232
233 if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
234 mvif->mt76.basic_rates_idx = MT7996_BASIC_RATES_TBL + 4;
235 else
236 mvif->mt76.basic_rates_idx = MT7996_BASIC_RATES_TBL;
237
238 mt7996_init_bitrate_mask(vif);
239
240 mt7996_mcu_add_bss_info(phy, vif, true);
241 mt7996_mcu_add_sta(dev, vif, NULL, true);
242 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
243
244 out:
245 mutex_unlock(&dev->mt76.mutex);
246
247 return ret;
248 }
249
mt7996_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)250 static void mt7996_remove_interface(struct ieee80211_hw *hw,
251 struct ieee80211_vif *vif)
252 {
253 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
254 struct mt7996_sta *msta = &mvif->sta;
255 struct mt7996_dev *dev = mt7996_hw_dev(hw);
256 struct mt7996_phy *phy = mt7996_hw_phy(hw);
257 int idx = msta->wcid.idx;
258
259 mt7996_mcu_add_sta(dev, vif, NULL, false);
260 mt7996_mcu_add_bss_info(phy, vif, false);
261
262 if (vif == phy->monitor_vif)
263 phy->monitor_vif = NULL;
264
265 mt7996_mcu_add_dev_info(phy, vif, false);
266
267 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
268
269 mutex_lock(&dev->mt76.mutex);
270 dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
271 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
272 mutex_unlock(&dev->mt76.mutex);
273
274 spin_lock_bh(&dev->mt76.sta_poll_lock);
275 if (!list_empty(&msta->wcid.poll_list))
276 list_del_init(&msta->wcid.poll_list);
277 spin_unlock_bh(&dev->mt76.sta_poll_lock);
278
279 mt76_wcid_cleanup(&dev->mt76, &msta->wcid);
280 }
281
mt7996_set_channel(struct mt7996_phy * phy)282 int mt7996_set_channel(struct mt7996_phy *phy)
283 {
284 struct mt7996_dev *dev = phy->dev;
285 int ret;
286
287 cancel_delayed_work_sync(&phy->mt76->mac_work);
288
289 mutex_lock(&dev->mt76.mutex);
290 set_bit(MT76_RESET, &phy->mt76->state);
291
292 mt76_set_channel(phy->mt76);
293
294 ret = mt7996_mcu_set_chan_info(phy, UNI_CHANNEL_SWITCH);
295 if (ret)
296 goto out;
297
298 ret = mt7996_dfs_init_radar_detector(phy);
299 mt7996_mac_cca_stats_reset(phy);
300
301 mt7996_mac_reset_counters(phy);
302 phy->noise = 0;
303
304 out:
305 clear_bit(MT76_RESET, &phy->mt76->state);
306 mutex_unlock(&dev->mt76.mutex);
307
308 mt76_txq_schedule_all(phy->mt76);
309
310 ieee80211_queue_delayed_work(phy->mt76->hw,
311 &phy->mt76->mac_work,
312 MT7996_WATCHDOG_TIME);
313
314 return ret;
315 }
316
mt7996_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)317 static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
318 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
319 struct ieee80211_key_conf *key)
320 {
321 struct mt7996_dev *dev = mt7996_hw_dev(hw);
322 struct mt7996_phy *phy = mt7996_hw_phy(hw);
323 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
324 struct mt7996_sta *msta = sta ? (struct mt7996_sta *)sta->drv_priv :
325 &mvif->sta;
326 struct mt76_wcid *wcid = &msta->wcid;
327 u8 *wcid_keyidx = &wcid->hw_key_idx;
328 int idx = key->keyidx;
329 int err = 0;
330
331 /* The hardware does not support per-STA RX GTK, fallback
332 * to software mode for these.
333 */
334 if ((vif->type == NL80211_IFTYPE_ADHOC ||
335 vif->type == NL80211_IFTYPE_MESH_POINT) &&
336 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
337 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
338 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
339 return -EOPNOTSUPP;
340
341 /* fall back to sw encryption for unsupported ciphers */
342 switch (key->cipher) {
343 case WLAN_CIPHER_SUITE_AES_CMAC:
344 wcid_keyidx = &wcid->hw_key_idx2;
345 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
346 break;
347 case WLAN_CIPHER_SUITE_TKIP:
348 case WLAN_CIPHER_SUITE_CCMP:
349 case WLAN_CIPHER_SUITE_CCMP_256:
350 case WLAN_CIPHER_SUITE_GCMP:
351 case WLAN_CIPHER_SUITE_GCMP_256:
352 case WLAN_CIPHER_SUITE_SMS4:
353 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
354 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
355 break;
356 case WLAN_CIPHER_SUITE_WEP40:
357 case WLAN_CIPHER_SUITE_WEP104:
358 default:
359 return -EOPNOTSUPP;
360 }
361
362 mutex_lock(&dev->mt76.mutex);
363
364 if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
365 mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
366 mt7996_mcu_add_bss_info(phy, vif, true);
367 }
368
369 if (cmd == SET_KEY) {
370 *wcid_keyidx = idx;
371 } else {
372 if (idx == *wcid_keyidx)
373 *wcid_keyidx = -1;
374 goto out;
375 }
376
377 mt76_wcid_key_setup(&dev->mt76, wcid, key);
378
379 if (key->keyidx == 6 || key->keyidx == 7)
380 err = mt7996_mcu_bcn_prot_enable(dev, vif, key);
381 else
382 err = mt7996_mcu_add_key(&dev->mt76, vif, key,
383 MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
384 &msta->wcid, cmd);
385 out:
386 mutex_unlock(&dev->mt76.mutex);
387
388 return err;
389 }
390
mt7996_config(struct ieee80211_hw * hw,u32 changed)391 static int mt7996_config(struct ieee80211_hw *hw, u32 changed)
392 {
393 struct mt7996_dev *dev = mt7996_hw_dev(hw);
394 struct mt7996_phy *phy = mt7996_hw_phy(hw);
395 int ret;
396
397 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
398 ieee80211_stop_queues(hw);
399 ret = mt7996_set_channel(phy);
400 if (ret)
401 return ret;
402 ieee80211_wake_queues(hw);
403 }
404
405 if (changed & (IEEE80211_CONF_CHANGE_POWER |
406 IEEE80211_CONF_CHANGE_CHANNEL)) {
407 ret = mt7996_mcu_set_txpower_sku(phy);
408 if (ret)
409 return ret;
410 }
411
412 mutex_lock(&dev->mt76.mutex);
413
414 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
415 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
416
417 if (!enabled)
418 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
419 else
420 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
421
422 mt76_rmw_field(dev, MT_DMA_DCR0(phy->mt76->band_idx),
423 MT_DMA_DCR0_RXD_G5_EN, enabled);
424 mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
425 }
426
427 mutex_unlock(&dev->mt76.mutex);
428
429 return 0;
430 }
431
432 static int
mt7996_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)433 mt7996_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
434 unsigned int link_id, u16 queue,
435 const struct ieee80211_tx_queue_params *params)
436 {
437 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
438 const u8 mq_to_aci[] = {
439 [IEEE80211_AC_VO] = 3,
440 [IEEE80211_AC_VI] = 2,
441 [IEEE80211_AC_BE] = 0,
442 [IEEE80211_AC_BK] = 1,
443 };
444
445 /* firmware uses access class index */
446 mvif->queue_params[mq_to_aci[queue]] = *params;
447 /* no need to update right away, we'll get BSS_CHANGED_QOS */
448
449 return 0;
450 }
451
mt7996_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)452 static void mt7996_configure_filter(struct ieee80211_hw *hw,
453 unsigned int changed_flags,
454 unsigned int *total_flags,
455 u64 multicast)
456 {
457 struct mt7996_dev *dev = mt7996_hw_dev(hw);
458 struct mt7996_phy *phy = mt7996_hw_phy(hw);
459 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
460 MT_WF_RFCR1_DROP_BF_POLL |
461 MT_WF_RFCR1_DROP_BA |
462 MT_WF_RFCR1_DROP_CFEND |
463 MT_WF_RFCR1_DROP_CFACK;
464 u32 flags = 0;
465
466 #define MT76_FILTER(_flag, _hw) do { \
467 flags |= *total_flags & FIF_##_flag; \
468 phy->rxfilter &= ~(_hw); \
469 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
470 } while (0)
471
472 mutex_lock(&dev->mt76.mutex);
473
474 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
475 MT_WF_RFCR_DROP_OTHER_BEACON |
476 MT_WF_RFCR_DROP_FRAME_REPORT |
477 MT_WF_RFCR_DROP_PROBEREQ |
478 MT_WF_RFCR_DROP_MCAST_FILTERED |
479 MT_WF_RFCR_DROP_MCAST |
480 MT_WF_RFCR_DROP_BCAST |
481 MT_WF_RFCR_DROP_DUPLICATE |
482 MT_WF_RFCR_DROP_A2_BSSID |
483 MT_WF_RFCR_DROP_UNWANTED_CTL |
484 MT_WF_RFCR_DROP_STBC_MULTI);
485
486 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
487 MT_WF_RFCR_DROP_A3_MAC |
488 MT_WF_RFCR_DROP_A3_BSSID);
489
490 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
491
492 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
493 MT_WF_RFCR_DROP_RTS |
494 MT_WF_RFCR_DROP_CTL_RSV |
495 MT_WF_RFCR_DROP_NDPA);
496
497 *total_flags = flags;
498 mt76_wr(dev, MT_WF_RFCR(phy->mt76->band_idx), phy->rxfilter);
499
500 if (*total_flags & FIF_CONTROL)
501 mt76_clear(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
502 else
503 mt76_set(dev, MT_WF_RFCR1(phy->mt76->band_idx), ctl_flags);
504
505 mutex_unlock(&dev->mt76.mutex);
506 }
507
508 static void
mt7996_update_bss_color(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_he_bss_color * bss_color)509 mt7996_update_bss_color(struct ieee80211_hw *hw,
510 struct ieee80211_vif *vif,
511 struct cfg80211_he_bss_color *bss_color)
512 {
513 struct mt7996_dev *dev = mt7996_hw_dev(hw);
514
515 switch (vif->type) {
516 case NL80211_IFTYPE_AP: {
517 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
518
519 if (mvif->mt76.omac_idx > HW_BSSID_MAX)
520 return;
521 fallthrough;
522 }
523 case NL80211_IFTYPE_STATION:
524 mt7996_mcu_update_bss_color(dev, vif, bss_color);
525 break;
526 default:
527 break;
528 }
529 }
530
531 static u8
mt7996_get_rates_table(struct ieee80211_hw * hw,struct ieee80211_vif * vif,bool beacon,bool mcast)532 mt7996_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
533 bool beacon, bool mcast)
534 {
535 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
536 struct mt76_phy *mphy = hw->priv;
537 u16 rate;
538 u8 i, idx;
539
540 rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast);
541
542 if (beacon) {
543 struct mt7996_phy *phy = mphy->priv;
544
545 /* odd index for driver, even index for firmware */
546 idx = MT7996_BEACON_RATES_TBL + 2 * phy->mt76->band_idx;
547 if (phy->beacon_rate != rate)
548 mt7996_mcu_set_fixed_rate_table(phy, idx, rate, beacon);
549
550 return idx;
551 }
552
553 idx = FIELD_GET(MT_TX_RATE_IDX, rate);
554 for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
555 if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
556 return MT7996_BASIC_RATES_TBL + 2 * i;
557
558 return mvif->basic_rates_idx;
559 }
560
561 static void
mt7996_update_mu_group(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info)562 mt7996_update_mu_group(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
563 struct ieee80211_bss_conf *info)
564 {
565 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
566 struct mt7996_dev *dev = mt7996_hw_dev(hw);
567 u8 band = mvif->mt76.band_idx;
568 u32 *mu;
569
570 mu = (u32 *)info->mu_group.membership;
571 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD0(band), mu[0]);
572 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_VLD1(band), mu[1]);
573
574 mu = (u32 *)info->mu_group.position;
575 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS0(band), mu[0]);
576 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS1(band), mu[1]);
577 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS2(band), mu[2]);
578 mt76_wr(dev, MT_WF_PHYRX_BAND_GID_TAB_POS3(band), mu[3]);
579 }
580
mt7996_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)581 static void mt7996_bss_info_changed(struct ieee80211_hw *hw,
582 struct ieee80211_vif *vif,
583 struct ieee80211_bss_conf *info,
584 u64 changed)
585 {
586 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
587 struct mt7996_phy *phy = mt7996_hw_phy(hw);
588 struct mt7996_dev *dev = mt7996_hw_dev(hw);
589
590 mutex_lock(&dev->mt76.mutex);
591
592 /* station mode uses BSSID to map the wlan entry to a peer,
593 * and then peer references bss_info_rfch to set bandwidth cap.
594 */
595 if ((changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid)) ||
596 (changed & BSS_CHANGED_ASSOC && vif->cfg.assoc) ||
597 (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon)) {
598 mt7996_mcu_add_bss_info(phy, vif, true);
599 mt7996_mcu_add_sta(dev, vif, NULL, true);
600 }
601
602 if (changed & BSS_CHANGED_ERP_CTS_PROT)
603 mt7996_mac_enable_rtscts(dev, vif, info->use_cts_prot);
604
605 if (changed & BSS_CHANGED_ERP_SLOT) {
606 int slottime = info->use_short_slot ? 9 : 20;
607
608 if (slottime != phy->slottime) {
609 phy->slottime = slottime;
610 mt7996_mcu_set_timing(phy, vif);
611 }
612 }
613
614 if (changed & BSS_CHANGED_MCAST_RATE)
615 mvif->mcast_rates_idx =
616 mt7996_get_rates_table(hw, vif, false, true);
617
618 if (changed & BSS_CHANGED_BASIC_RATES)
619 mvif->basic_rates_idx =
620 mt7996_get_rates_table(hw, vif, false, false);
621
622 /* ensure that enable txcmd_mode after bss_info */
623 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
624 mt7996_mcu_set_tx(dev, vif);
625
626 if (changed & BSS_CHANGED_HE_OBSS_PD)
627 mt7996_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
628
629 if (changed & BSS_CHANGED_HE_BSS_COLOR)
630 mt7996_update_bss_color(hw, vif, &info->he_bss_color);
631
632 if (changed & (BSS_CHANGED_BEACON |
633 BSS_CHANGED_BEACON_ENABLED)) {
634 mvif->beacon_rates_idx =
635 mt7996_get_rates_table(hw, vif, true, false);
636
637 mt7996_mcu_add_beacon(hw, vif, info->enable_beacon);
638 }
639
640 if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
641 BSS_CHANGED_FILS_DISCOVERY))
642 mt7996_mcu_beacon_inband_discov(dev, vif, changed);
643
644 if (changed & BSS_CHANGED_MU_GROUPS)
645 mt7996_update_mu_group(hw, vif, info);
646
647 mutex_unlock(&dev->mt76.mutex);
648 }
649
650 static void
mt7996_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)651 mt7996_channel_switch_beacon(struct ieee80211_hw *hw,
652 struct ieee80211_vif *vif,
653 struct cfg80211_chan_def *chandef)
654 {
655 struct mt7996_dev *dev = mt7996_hw_dev(hw);
656
657 mutex_lock(&dev->mt76.mutex);
658 mt7996_mcu_add_beacon(hw, vif, true);
659 mutex_unlock(&dev->mt76.mutex);
660 }
661
mt7996_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)662 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
663 struct ieee80211_sta *sta)
664 {
665 struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
666 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
667 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
668 u8 band_idx = mvif->phy->mt76->band_idx;
669 int ret, idx;
670
671 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7996_WTBL_STA);
672 if (idx < 0)
673 return -ENOSPC;
674
675 INIT_LIST_HEAD(&msta->rc_list);
676 INIT_LIST_HEAD(&msta->wcid.poll_list);
677 msta->vif = mvif;
678 msta->wcid.sta = 1;
679 msta->wcid.idx = idx;
680 msta->wcid.phy_idx = band_idx;
681 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
682
683 ewma_avg_signal_init(&msta->avg_ack_signal);
684
685 mt7996_mac_wtbl_update(dev, idx,
686 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
687
688 ret = mt7996_mcu_add_sta(dev, vif, sta, true);
689 if (ret)
690 return ret;
691
692 return mt7996_mcu_add_rate_ctrl(dev, vif, sta, false);
693 }
694
mt7996_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)695 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
696 struct ieee80211_sta *sta)
697 {
698 struct mt7996_dev *dev = container_of(mdev, struct mt7996_dev, mt76);
699 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
700 int i;
701
702 mt7996_mcu_add_sta(dev, vif, sta, false);
703
704 mt7996_mac_wtbl_update(dev, msta->wcid.idx,
705 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
706
707 for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
708 mt7996_mac_twt_teardown_flow(dev, msta, i);
709
710 spin_lock_bh(&mdev->sta_poll_lock);
711 if (!list_empty(&msta->wcid.poll_list))
712 list_del_init(&msta->wcid.poll_list);
713 if (!list_empty(&msta->rc_list))
714 list_del_init(&msta->rc_list);
715 spin_unlock_bh(&mdev->sta_poll_lock);
716 }
717
mt7996_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)718 static void mt7996_tx(struct ieee80211_hw *hw,
719 struct ieee80211_tx_control *control,
720 struct sk_buff *skb)
721 {
722 struct mt7996_dev *dev = mt7996_hw_dev(hw);
723 struct mt76_phy *mphy = hw->priv;
724 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
725 struct ieee80211_vif *vif = info->control.vif;
726 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
727
728 if (control->sta) {
729 struct mt7996_sta *sta;
730
731 sta = (struct mt7996_sta *)control->sta->drv_priv;
732 wcid = &sta->wcid;
733 }
734
735 if (vif && !control->sta) {
736 struct mt7996_vif *mvif;
737
738 mvif = (struct mt7996_vif *)vif->drv_priv;
739 wcid = &mvif->sta.wcid;
740 }
741
742 mt76_tx(mphy, control->sta, wcid, skb);
743 }
744
mt7996_set_rts_threshold(struct ieee80211_hw * hw,u32 val)745 static int mt7996_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
746 {
747 struct mt7996_phy *phy = mt7996_hw_phy(hw);
748 int ret;
749
750 mutex_lock(&phy->dev->mt76.mutex);
751 ret = mt7996_mcu_set_rts_thresh(phy, val);
752 mutex_unlock(&phy->dev->mt76.mutex);
753
754 return ret;
755 }
756
757 static int
mt7996_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)758 mt7996_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
759 struct ieee80211_ampdu_params *params)
760 {
761 enum ieee80211_ampdu_mlme_action action = params->action;
762 struct mt7996_dev *dev = mt7996_hw_dev(hw);
763 struct ieee80211_sta *sta = params->sta;
764 struct ieee80211_txq *txq = sta->txq[params->tid];
765 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
766 u16 tid = params->tid;
767 u16 ssn = params->ssn;
768 struct mt76_txq *mtxq;
769 int ret = 0;
770
771 if (!txq)
772 return -EINVAL;
773
774 mtxq = (struct mt76_txq *)txq->drv_priv;
775
776 mutex_lock(&dev->mt76.mutex);
777 switch (action) {
778 case IEEE80211_AMPDU_RX_START:
779 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
780 params->buf_size);
781 ret = mt7996_mcu_add_rx_ba(dev, params, true);
782 break;
783 case IEEE80211_AMPDU_RX_STOP:
784 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
785 ret = mt7996_mcu_add_rx_ba(dev, params, false);
786 break;
787 case IEEE80211_AMPDU_TX_OPERATIONAL:
788 mtxq->aggr = true;
789 mtxq->send_bar = false;
790 ret = mt7996_mcu_add_tx_ba(dev, params, true);
791 break;
792 case IEEE80211_AMPDU_TX_STOP_FLUSH:
793 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
794 mtxq->aggr = false;
795 clear_bit(tid, &msta->wcid.ampdu_state);
796 ret = mt7996_mcu_add_tx_ba(dev, params, false);
797 break;
798 case IEEE80211_AMPDU_TX_START:
799 set_bit(tid, &msta->wcid.ampdu_state);
800 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
801 break;
802 case IEEE80211_AMPDU_TX_STOP_CONT:
803 mtxq->aggr = false;
804 clear_bit(tid, &msta->wcid.ampdu_state);
805 ret = mt7996_mcu_add_tx_ba(dev, params, false);
806 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
807 break;
808 }
809 mutex_unlock(&dev->mt76.mutex);
810
811 return ret;
812 }
813
814 static int
mt7996_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)815 mt7996_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
816 struct ieee80211_sta *sta)
817 {
818 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
819 IEEE80211_STA_NONE);
820 }
821
822 static int
mt7996_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)823 mt7996_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
824 struct ieee80211_sta *sta)
825 {
826 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
827 IEEE80211_STA_NOTEXIST);
828 }
829
830 static int
mt7996_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)831 mt7996_get_stats(struct ieee80211_hw *hw,
832 struct ieee80211_low_level_stats *stats)
833 {
834 struct mt7996_phy *phy = mt7996_hw_phy(hw);
835 struct mt7996_dev *dev = mt7996_hw_dev(hw);
836 struct mt76_mib_stats *mib = &phy->mib;
837
838 mutex_lock(&dev->mt76.mutex);
839
840 stats->dot11RTSSuccessCount = mib->rts_cnt;
841 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
842 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
843 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
844
845 mutex_unlock(&dev->mt76.mutex);
846
847 return 0;
848 }
849
__mt7996_get_tsf(struct ieee80211_hw * hw,struct mt7996_vif * mvif)850 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif)
851 {
852 struct mt7996_dev *dev = mt7996_hw_dev(hw);
853 struct mt7996_phy *phy = mt7996_hw_phy(hw);
854 union {
855 u64 t64;
856 u32 t32[2];
857 } tsf;
858 u16 n;
859
860 lockdep_assert_held(&dev->mt76.mutex);
861
862 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
863 : mvif->mt76.omac_idx;
864 /* TSF software read */
865 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
866 MT_LPON_TCR_SW_READ);
867 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
868 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
869
870 return tsf.t64;
871 }
872
873 static u64
mt7996_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)874 mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
875 {
876 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
877 struct mt7996_dev *dev = mt7996_hw_dev(hw);
878 u64 ret;
879
880 mutex_lock(&dev->mt76.mutex);
881 ret = __mt7996_get_tsf(hw, mvif);
882 mutex_unlock(&dev->mt76.mutex);
883
884 return ret;
885 }
886
887 static void
mt7996_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)888 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
889 u64 timestamp)
890 {
891 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
892 struct mt7996_dev *dev = mt7996_hw_dev(hw);
893 struct mt7996_phy *phy = mt7996_hw_phy(hw);
894 union {
895 u64 t64;
896 u32 t32[2];
897 } tsf = { .t64 = timestamp, };
898 u16 n;
899
900 mutex_lock(&dev->mt76.mutex);
901
902 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
903 : mvif->mt76.omac_idx;
904 mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
905 mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
906 /* TSF software overwrite */
907 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
908 MT_LPON_TCR_SW_WRITE);
909
910 mutex_unlock(&dev->mt76.mutex);
911 }
912
913 static void
mt7996_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)914 mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
915 s64 timestamp)
916 {
917 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
918 struct mt7996_dev *dev = mt7996_hw_dev(hw);
919 struct mt7996_phy *phy = mt7996_hw_phy(hw);
920 union {
921 u64 t64;
922 u32 t32[2];
923 } tsf = { .t64 = timestamp, };
924 u16 n;
925
926 mutex_lock(&dev->mt76.mutex);
927
928 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
929 : mvif->mt76.omac_idx;
930 mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
931 mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
932 /* TSF software adjust*/
933 mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
934 MT_LPON_TCR_SW_ADJUST);
935
936 mutex_unlock(&dev->mt76.mutex);
937 }
938
939 static void
mt7996_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)940 mt7996_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
941 {
942 struct mt7996_phy *phy = mt7996_hw_phy(hw);
943 struct mt7996_dev *dev = phy->dev;
944
945 mutex_lock(&dev->mt76.mutex);
946 phy->coverage_class = max_t(s16, coverage_class, 0);
947 mt7996_mac_set_coverage_class(phy);
948 mutex_unlock(&dev->mt76.mutex);
949 }
950
951 static int
mt7996_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)952 mt7996_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
953 {
954 struct mt7996_dev *dev = mt7996_hw_dev(hw);
955 struct mt7996_phy *phy = mt7996_hw_phy(hw);
956 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
957 u8 band_idx = phy->mt76->band_idx, shift = dev->chainshift[band_idx];
958
959 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
960 return -EINVAL;
961
962 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
963 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
964
965 mutex_lock(&dev->mt76.mutex);
966
967 phy->mt76->antenna_mask = tx_ant;
968
969 /* restore to the origin chainmask which might have auxiliary path */
970 if (hweight8(tx_ant) == max_nss && band_idx < MT_BAND2)
971 phy->mt76->chainmask = ((dev->chainmask >> shift) &
972 (BIT(dev->chainshift[band_idx + 1] - shift) - 1)) << shift;
973 else if (hweight8(tx_ant) == max_nss)
974 phy->mt76->chainmask = (dev->chainmask >> shift) << shift;
975 else
976 phy->mt76->chainmask = tx_ant << shift;
977
978 mt76_set_stream_caps(phy->mt76, true);
979 mt7996_set_stream_vht_txbf_caps(phy);
980 mt7996_set_stream_he_eht_caps(phy);
981 mt7996_mcu_set_txpower_sku(phy);
982
983 mutex_unlock(&dev->mt76.mutex);
984
985 return 0;
986 }
987
mt7996_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)988 static void mt7996_sta_statistics(struct ieee80211_hw *hw,
989 struct ieee80211_vif *vif,
990 struct ieee80211_sta *sta,
991 struct station_info *sinfo)
992 {
993 struct mt7996_phy *phy = mt7996_hw_phy(hw);
994 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
995 struct rate_info *txrate = &msta->wcid.rate;
996
997 if (txrate->legacy || txrate->flags) {
998 if (txrate->legacy) {
999 sinfo->txrate.legacy = txrate->legacy;
1000 } else {
1001 sinfo->txrate.mcs = txrate->mcs;
1002 sinfo->txrate.nss = txrate->nss;
1003 sinfo->txrate.bw = txrate->bw;
1004 sinfo->txrate.he_gi = txrate->he_gi;
1005 sinfo->txrate.he_dcm = txrate->he_dcm;
1006 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1007 sinfo->txrate.eht_gi = txrate->eht_gi;
1008 }
1009 sinfo->txrate.flags = txrate->flags;
1010 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1011 }
1012 sinfo->txrate.flags = txrate->flags;
1013 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1014
1015 sinfo->tx_failed = msta->wcid.stats.tx_failed;
1016 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1017
1018 sinfo->tx_retries = msta->wcid.stats.tx_retries;
1019 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1020
1021 sinfo->ack_signal = (s8)msta->ack_signal;
1022 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1023
1024 sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1025 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1026
1027 if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1028 sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1029 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1030
1031 sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1032 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1033
1034 sinfo->tx_packets = msta->wcid.stats.tx_packets;
1035 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1036
1037 sinfo->rx_packets = msta->wcid.stats.rx_packets;
1038 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1039 }
1040 }
1041
mt7996_sta_rc_work(void * data,struct ieee80211_sta * sta)1042 static void mt7996_sta_rc_work(void *data, struct ieee80211_sta *sta)
1043 {
1044 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1045 struct mt7996_dev *dev = msta->vif->phy->dev;
1046 u32 *changed = data;
1047
1048 spin_lock_bh(&dev->mt76.sta_poll_lock);
1049 msta->changed |= *changed;
1050 if (list_empty(&msta->rc_list))
1051 list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1052 spin_unlock_bh(&dev->mt76.sta_poll_lock);
1053 }
1054
mt7996_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)1055 static void mt7996_sta_rc_update(struct ieee80211_hw *hw,
1056 struct ieee80211_vif *vif,
1057 struct ieee80211_sta *sta,
1058 u32 changed)
1059 {
1060 struct mt7996_phy *phy = mt7996_hw_phy(hw);
1061 struct mt7996_dev *dev = phy->dev;
1062
1063 mt7996_sta_rc_work(&changed, sta);
1064 ieee80211_queue_work(hw, &dev->rc_work);
1065 }
1066
1067 static int
mt7996_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1068 mt7996_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1069 const struct cfg80211_bitrate_mask *mask)
1070 {
1071 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1072 struct mt7996_phy *phy = mt7996_hw_phy(hw);
1073 struct mt7996_dev *dev = phy->dev;
1074 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1075
1076 mvif->bitrate_mask = *mask;
1077
1078 /* if multiple rates across different preambles are given we can
1079 * reconfigure this info with all peers using sta_rec command with
1080 * the below exception cases.
1081 * - single rate : if a rate is passed along with different preambles,
1082 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1083 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1084 * then multiple MCS setting (MCS 4,5,6) is not supported.
1085 */
1086 ieee80211_iterate_stations_atomic(hw, mt7996_sta_rc_work, &changed);
1087 ieee80211_queue_work(hw, &dev->rc_work);
1088
1089 return 0;
1090 }
1091
mt7996_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1092 static void mt7996_sta_set_4addr(struct ieee80211_hw *hw,
1093 struct ieee80211_vif *vif,
1094 struct ieee80211_sta *sta,
1095 bool enabled)
1096 {
1097 struct mt7996_dev *dev = mt7996_hw_dev(hw);
1098 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1099
1100 if (enabled)
1101 set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1102 else
1103 clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1104
1105 mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1106 }
1107
mt7996_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1108 static void mt7996_sta_set_decap_offload(struct ieee80211_hw *hw,
1109 struct ieee80211_vif *vif,
1110 struct ieee80211_sta *sta,
1111 bool enabled)
1112 {
1113 struct mt7996_dev *dev = mt7996_hw_dev(hw);
1114 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1115
1116 if (enabled)
1117 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1118 else
1119 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1120
1121 mt7996_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1122 }
1123
1124 static const char mt7996_gstrings_stats[][ETH_GSTRING_LEN] = {
1125 "tx_ampdu_cnt",
1126 "tx_stop_q_empty_cnt",
1127 "tx_mpdu_attempts",
1128 "tx_mpdu_success",
1129 "tx_rwp_fail_cnt",
1130 "tx_rwp_need_cnt",
1131 "tx_pkt_ebf_cnt",
1132 "tx_pkt_ibf_cnt",
1133 "tx_ampdu_len:0-1",
1134 "tx_ampdu_len:2-10",
1135 "tx_ampdu_len:11-19",
1136 "tx_ampdu_len:20-28",
1137 "tx_ampdu_len:29-37",
1138 "tx_ampdu_len:38-46",
1139 "tx_ampdu_len:47-55",
1140 "tx_ampdu_len:56-79",
1141 "tx_ampdu_len:80-103",
1142 "tx_ampdu_len:104-127",
1143 "tx_ampdu_len:128-151",
1144 "tx_ampdu_len:152-175",
1145 "tx_ampdu_len:176-199",
1146 "tx_ampdu_len:200-223",
1147 "tx_ampdu_len:224-247",
1148 "ba_miss_count",
1149 "tx_beamformer_ppdu_iBF",
1150 "tx_beamformer_ppdu_eBF",
1151 "tx_beamformer_rx_feedback_all",
1152 "tx_beamformer_rx_feedback_he",
1153 "tx_beamformer_rx_feedback_vht",
1154 "tx_beamformer_rx_feedback_ht",
1155 "tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1156 "tx_beamformer_rx_feedback_nc",
1157 "tx_beamformer_rx_feedback_nr",
1158 "tx_beamformee_ok_feedback_pkts",
1159 "tx_beamformee_feedback_trig",
1160 "tx_mu_beamforming",
1161 "tx_mu_mpdu",
1162 "tx_mu_successful_mpdu",
1163 "tx_su_successful_mpdu",
1164 "tx_msdu_pack_1",
1165 "tx_msdu_pack_2",
1166 "tx_msdu_pack_3",
1167 "tx_msdu_pack_4",
1168 "tx_msdu_pack_5",
1169 "tx_msdu_pack_6",
1170 "tx_msdu_pack_7",
1171 "tx_msdu_pack_8",
1172
1173 /* rx counters */
1174 "rx_fifo_full_cnt",
1175 "rx_mpdu_cnt",
1176 "channel_idle_cnt",
1177 "rx_vector_mismatch_cnt",
1178 "rx_delimiter_fail_cnt",
1179 "rx_len_mismatch_cnt",
1180 "rx_ampdu_cnt",
1181 "rx_ampdu_bytes_cnt",
1182 "rx_ampdu_valid_subframe_cnt",
1183 "rx_ampdu_valid_subframe_b_cnt",
1184 "rx_pfdrop_cnt",
1185 "rx_vec_queue_overflow_drop_cnt",
1186 "rx_ba_cnt",
1187
1188 /* per vif counters */
1189 "v_tx_mode_cck",
1190 "v_tx_mode_ofdm",
1191 "v_tx_mode_ht",
1192 "v_tx_mode_ht_gf",
1193 "v_tx_mode_vht",
1194 "v_tx_mode_he_su",
1195 "v_tx_mode_he_ext_su",
1196 "v_tx_mode_he_tb",
1197 "v_tx_mode_he_mu",
1198 "v_tx_mode_eht_su",
1199 "v_tx_mode_eht_trig",
1200 "v_tx_mode_eht_mu",
1201 "v_tx_bw_20",
1202 "v_tx_bw_40",
1203 "v_tx_bw_80",
1204 "v_tx_bw_160",
1205 "v_tx_bw_320",
1206 "v_tx_mcs_0",
1207 "v_tx_mcs_1",
1208 "v_tx_mcs_2",
1209 "v_tx_mcs_3",
1210 "v_tx_mcs_4",
1211 "v_tx_mcs_5",
1212 "v_tx_mcs_6",
1213 "v_tx_mcs_7",
1214 "v_tx_mcs_8",
1215 "v_tx_mcs_9",
1216 "v_tx_mcs_10",
1217 "v_tx_mcs_11",
1218 "v_tx_mcs_12",
1219 "v_tx_mcs_13",
1220 "v_tx_nss_1",
1221 "v_tx_nss_2",
1222 "v_tx_nss_3",
1223 "v_tx_nss_4",
1224 };
1225
1226 #define MT7996_SSTATS_LEN ARRAY_SIZE(mt7996_gstrings_stats)
1227
1228 /* Ethtool related API */
1229 static
mt7996_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1230 void mt7996_get_et_strings(struct ieee80211_hw *hw,
1231 struct ieee80211_vif *vif,
1232 u32 sset, u8 *data)
1233 {
1234 if (sset == ETH_SS_STATS)
1235 memcpy(data, mt7996_gstrings_stats,
1236 sizeof(mt7996_gstrings_stats));
1237 }
1238
1239 static
mt7996_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1240 int mt7996_get_et_sset_count(struct ieee80211_hw *hw,
1241 struct ieee80211_vif *vif, int sset)
1242 {
1243 if (sset == ETH_SS_STATS)
1244 return MT7996_SSTATS_LEN;
1245
1246 return 0;
1247 }
1248
mt7996_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1249 static void mt7996_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1250 {
1251 struct mt76_ethtool_worker_info *wi = wi_data;
1252 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1253
1254 if (msta->vif->mt76.idx != wi->idx)
1255 return;
1256
1257 mt76_ethtool_worker(wi, &msta->wcid.stats, true);
1258 }
1259
1260 static
mt7996_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1261 void mt7996_get_et_stats(struct ieee80211_hw *hw,
1262 struct ieee80211_vif *vif,
1263 struct ethtool_stats *stats, u64 *data)
1264 {
1265 struct mt7996_dev *dev = mt7996_hw_dev(hw);
1266 struct mt7996_phy *phy = mt7996_hw_phy(hw);
1267 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1268 struct mt76_mib_stats *mib = &phy->mib;
1269 struct mt76_ethtool_worker_info wi = {
1270 .data = data,
1271 .idx = mvif->mt76.idx,
1272 };
1273 /* See mt7996_ampdu_stat_read_phy, etc */
1274 int i, ei = 0;
1275
1276 mutex_lock(&dev->mt76.mutex);
1277
1278 mt7996_mac_update_stats(phy);
1279
1280 data[ei++] = mib->tx_ampdu_cnt;
1281 data[ei++] = mib->tx_stop_q_empty_cnt;
1282 data[ei++] = mib->tx_mpdu_attempts_cnt;
1283 data[ei++] = mib->tx_mpdu_success_cnt;
1284 data[ei++] = mib->tx_rwp_fail_cnt;
1285 data[ei++] = mib->tx_rwp_need_cnt;
1286 data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1287 data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1288
1289 /* Tx ampdu stat */
1290 for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1291 data[ei++] = phy->mt76->aggr_stats[i];
1292 data[ei++] = phy->mib.ba_miss_cnt;
1293
1294 /* Tx Beamformer monitor */
1295 data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1296 data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1297
1298 /* Tx Beamformer Rx feedback monitor */
1299 data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1300 data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1301 data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1302 data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1303
1304 data[ei++] = mib->tx_bf_rx_fb_bw;
1305 data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1306 data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1307
1308 /* Tx Beamformee Rx NDPA & Tx feedback report */
1309 data[ei++] = mib->tx_bf_fb_cpl_cnt;
1310 data[ei++] = mib->tx_bf_fb_trig_cnt;
1311
1312 /* Tx SU & MU counters */
1313 data[ei++] = mib->tx_mu_bf_cnt;
1314 data[ei++] = mib->tx_mu_mpdu_cnt;
1315 data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1316 data[ei++] = mib->tx_su_acked_mpdu_cnt;
1317
1318 /* Tx amsdu info (pack-count histogram) */
1319 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1320 data[ei++] = mib->tx_amsdu[i];
1321
1322 /* rx counters */
1323 data[ei++] = mib->rx_fifo_full_cnt;
1324 data[ei++] = mib->rx_mpdu_cnt;
1325 data[ei++] = mib->channel_idle_cnt;
1326 data[ei++] = mib->rx_vector_mismatch_cnt;
1327 data[ei++] = mib->rx_delimiter_fail_cnt;
1328 data[ei++] = mib->rx_len_mismatch_cnt;
1329 data[ei++] = mib->rx_ampdu_cnt;
1330 data[ei++] = mib->rx_ampdu_bytes_cnt;
1331 data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1332 data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1333 data[ei++] = mib->rx_pfdrop_cnt;
1334 data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1335 data[ei++] = mib->rx_ba_cnt;
1336
1337 /* Add values for all stations owned by this vif */
1338 wi.initial_stat_idx = ei;
1339 ieee80211_iterate_stations_atomic(hw, mt7996_ethtool_worker, &wi);
1340
1341 mutex_unlock(&dev->mt76.mutex);
1342
1343 if (wi.sta_count == 0)
1344 return;
1345
1346 ei += wi.worker_stat_count;
1347 if (ei != MT7996_SSTATS_LEN)
1348 dev_err(dev->mt76.dev, "ei: %d MT7996_SSTATS_LEN: %d",
1349 ei, (int)MT7996_SSTATS_LEN);
1350 }
1351
1352 static void
mt7996_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1353 mt7996_twt_teardown_request(struct ieee80211_hw *hw,
1354 struct ieee80211_sta *sta,
1355 u8 flowid)
1356 {
1357 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1358 struct mt7996_dev *dev = mt7996_hw_dev(hw);
1359
1360 mutex_lock(&dev->mt76.mutex);
1361 mt7996_mac_twt_teardown_flow(dev, msta, flowid);
1362 mutex_unlock(&dev->mt76.mutex);
1363 }
1364
1365 static int
mt7996_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1366 mt7996_set_radar_background(struct ieee80211_hw *hw,
1367 struct cfg80211_chan_def *chandef)
1368 {
1369 struct mt7996_phy *phy = mt7996_hw_phy(hw);
1370 struct mt7996_dev *dev = phy->dev;
1371 int ret = -EINVAL;
1372 bool running;
1373
1374 mutex_lock(&dev->mt76.mutex);
1375
1376 if (dev->mt76.region == NL80211_DFS_UNSET)
1377 goto out;
1378
1379 if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1380 /* rdd2 is already locked */
1381 ret = -EBUSY;
1382 goto out;
1383 }
1384
1385 /* rdd2 already configured on a radar channel */
1386 running = dev->rdd2_phy &&
1387 cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1388 !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1389
1390 if (!chandef || running ||
1391 !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1392 ret = mt7996_mcu_rdd_background_enable(phy, NULL);
1393 if (ret)
1394 goto out;
1395
1396 if (!running)
1397 goto update_phy;
1398 }
1399
1400 ret = mt7996_mcu_rdd_background_enable(phy, chandef);
1401 if (ret)
1402 goto out;
1403
1404 update_phy:
1405 dev->rdd2_phy = chandef ? phy : NULL;
1406 if (chandef)
1407 dev->rdd2_chandef = *chandef;
1408 out:
1409 mutex_unlock(&dev->mt76.mutex);
1410
1411 return ret;
1412 }
1413
1414 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1415 static int
mt7996_net_fill_forward_path(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct net_device_path_ctx * ctx,struct net_device_path * path)1416 mt7996_net_fill_forward_path(struct ieee80211_hw *hw,
1417 struct ieee80211_vif *vif,
1418 struct ieee80211_sta *sta,
1419 struct net_device_path_ctx *ctx,
1420 struct net_device_path *path)
1421 {
1422 struct mt7996_vif *mvif = (struct mt7996_vif *)vif->drv_priv;
1423 struct mt7996_sta *msta = (struct mt7996_sta *)sta->drv_priv;
1424 struct mt7996_dev *dev = mt7996_hw_dev(hw);
1425 struct mt7996_phy *phy = mt7996_hw_phy(hw);
1426 struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1427
1428 if (phy != &dev->phy && phy->mt76->band_idx == MT_BAND2)
1429 wed = &dev->mt76.mmio.wed_hif2;
1430
1431 if (!mtk_wed_device_active(wed))
1432 return -ENODEV;
1433
1434 if (msta->wcid.idx > MT7996_WTBL_STA)
1435 return -EIO;
1436
1437 path->type = DEV_PATH_MTK_WDMA;
1438 path->dev = ctx->dev;
1439 path->mtk_wdma.wdma_idx = wed->wdma_idx;
1440 path->mtk_wdma.bss = mvif->mt76.idx;
1441 path->mtk_wdma.queue = 0;
1442 path->mtk_wdma.wcid = msta->wcid.idx;
1443
1444 path->mtk_wdma.amsdu = mtk_wed_is_amsdu_supported(wed);
1445 ctx->dev = NULL;
1446
1447 return 0;
1448 }
1449
1450 #endif
1451
1452 const struct ieee80211_ops mt7996_ops = {
1453 .tx = mt7996_tx,
1454 .start = mt7996_start,
1455 .stop = mt7996_stop,
1456 .add_interface = mt7996_add_interface,
1457 .remove_interface = mt7996_remove_interface,
1458 .config = mt7996_config,
1459 .conf_tx = mt7996_conf_tx,
1460 .configure_filter = mt7996_configure_filter,
1461 .bss_info_changed = mt7996_bss_info_changed,
1462 .sta_add = mt7996_sta_add,
1463 .sta_remove = mt7996_sta_remove,
1464 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1465 .sta_rc_update = mt7996_sta_rc_update,
1466 .set_key = mt7996_set_key,
1467 .ampdu_action = mt7996_ampdu_action,
1468 .set_rts_threshold = mt7996_set_rts_threshold,
1469 .wake_tx_queue = mt76_wake_tx_queue,
1470 .sw_scan_start = mt76_sw_scan,
1471 .sw_scan_complete = mt76_sw_scan_complete,
1472 .release_buffered_frames = mt76_release_buffered_frames,
1473 .get_txpower = mt76_get_txpower,
1474 .channel_switch_beacon = mt7996_channel_switch_beacon,
1475 .get_stats = mt7996_get_stats,
1476 .get_et_sset_count = mt7996_get_et_sset_count,
1477 .get_et_stats = mt7996_get_et_stats,
1478 .get_et_strings = mt7996_get_et_strings,
1479 .get_tsf = mt7996_get_tsf,
1480 .set_tsf = mt7996_set_tsf,
1481 .offset_tsf = mt7996_offset_tsf,
1482 .get_survey = mt76_get_survey,
1483 .get_antenna = mt76_get_antenna,
1484 .set_antenna = mt7996_set_antenna,
1485 .set_bitrate_mask = mt7996_set_bitrate_mask,
1486 .set_coverage_class = mt7996_set_coverage_class,
1487 .sta_statistics = mt7996_sta_statistics,
1488 .sta_set_4addr = mt7996_sta_set_4addr,
1489 .sta_set_decap_offload = mt7996_sta_set_decap_offload,
1490 .add_twt_setup = mt7996_mac_add_twt_setup,
1491 .twt_teardown_request = mt7996_twt_teardown_request,
1492 #ifdef CONFIG_MAC80211_DEBUGFS
1493 .sta_add_debugfs = mt7996_sta_add_debugfs,
1494 #endif
1495 .set_radar_background = mt7996_set_radar_background,
1496 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1497 .net_fill_forward_path = mt7996_net_fill_forward_path,
1498 .net_setup_tc = mt76_net_setup_tc,
1499 #endif
1500 };
1501