1 // 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 "mt7915.h"
9 #include "mcu.h"
10
mt7915_dev_running(struct mt7915_dev * dev)11 static bool mt7915_dev_running(struct mt7915_dev *dev)
12 {
13 struct mt7915_phy *phy;
14
15 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 return true;
17
18 phy = mt7915_ext_phy(dev);
19
20 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
21 }
22
mt7915_run(struct ieee80211_hw * hw)23 int mt7915_run(struct ieee80211_hw *hw)
24 {
25 struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 bool running;
28 int ret;
29
30 running = mt7915_dev_running(dev);
31
32 if (!running) {
33 ret = mt76_connac_mcu_set_pm(&dev->mt76,
34 dev->phy.mt76->band_idx, 0);
35 if (ret)
36 goto out;
37
38 ret = mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx,
39 true, true);
40 if (ret)
41 goto out;
42
43 mt7915_mac_enable_nf(dev, dev->phy.mt76->band_idx);
44 }
45
46 if (phy != &dev->phy) {
47 ret = mt76_connac_mcu_set_pm(&dev->mt76,
48 phy->mt76->band_idx, 0);
49 if (ret)
50 goto out;
51
52 ret = mt7915_mcu_set_mac(dev, phy->mt76->band_idx,
53 true, true);
54 if (ret)
55 goto out;
56
57 mt7915_mac_enable_nf(dev, phy->mt76->band_idx);
58 }
59
60 ret = mt7915_mcu_set_thermal_throttling(phy,
61 MT7915_THERMAL_THROTTLE_MAX);
62
63 if (ret)
64 goto out;
65
66 ret = mt7915_mcu_set_thermal_protect(phy);
67
68 if (ret)
69 goto out;
70
71 ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
72 phy->mt76->band_idx);
73 if (ret)
74 goto out;
75
76 ret = mt7915_mcu_set_sku_en(phy, true);
77 if (ret)
78 goto out;
79
80 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
81 if (ret)
82 goto out;
83
84 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
85
86 if (!mt76_testmode_enabled(phy->mt76))
87 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
88 MT7915_WATCHDOG_TIME);
89
90 if (!running)
91 mt7915_mac_reset_counters(phy);
92
93 out:
94 return ret;
95 }
96
mt7915_start(struct ieee80211_hw * hw)97 static int mt7915_start(struct ieee80211_hw *hw)
98 {
99 struct mt7915_dev *dev = mt7915_hw_dev(hw);
100 int ret;
101
102 flush_work(&dev->init_work);
103
104 mutex_lock(&dev->mt76.mutex);
105 ret = mt7915_run(hw);
106 mutex_unlock(&dev->mt76.mutex);
107
108 return ret;
109 }
110
mt7915_stop(struct ieee80211_hw * hw,bool suspend)111 static void mt7915_stop(struct ieee80211_hw *hw, bool suspend)
112 {
113 struct mt7915_dev *dev = mt7915_hw_dev(hw);
114 struct mt7915_phy *phy = mt7915_hw_phy(hw);
115
116 cancel_delayed_work_sync(&phy->mt76->mac_work);
117
118 mutex_lock(&dev->mt76.mutex);
119
120 mt76_testmode_reset(phy->mt76, true);
121
122 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
123
124 if (phy != &dev->phy) {
125 mt76_connac_mcu_set_pm(&dev->mt76, phy->mt76->band_idx, 1);
126 mt7915_mcu_set_mac(dev, phy->mt76->band_idx, false, false);
127 }
128
129 if (!mt7915_dev_running(dev)) {
130 mt76_connac_mcu_set_pm(&dev->mt76, dev->phy.mt76->band_idx, 1);
131 mt7915_mcu_set_mac(dev, dev->phy.mt76->band_idx, false, false);
132 }
133
134 mutex_unlock(&dev->mt76.mutex);
135 }
136
get_free_idx(u32 mask,u8 start,u8 end)137 static inline int get_free_idx(u32 mask, u8 start, u8 end)
138 {
139 return ffs(~mask & GENMASK(end, start));
140 }
141
get_omac_idx(enum nl80211_iftype type,u64 mask)142 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
143 {
144 int i;
145
146 switch (type) {
147 case NL80211_IFTYPE_MESH_POINT:
148 case NL80211_IFTYPE_ADHOC:
149 case NL80211_IFTYPE_STATION:
150 /* prefer hw bssid slot 1-3 */
151 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
152 if (i)
153 return i - 1;
154
155 if (type != NL80211_IFTYPE_STATION)
156 break;
157
158 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
159 if (i)
160 return i - 1;
161
162 if (~mask & BIT(HW_BSSID_0))
163 return HW_BSSID_0;
164
165 break;
166 case NL80211_IFTYPE_MONITOR:
167 case NL80211_IFTYPE_AP:
168 /* ap uses hw bssid 0 and ext bssid */
169 if (~mask & BIT(HW_BSSID_0))
170 return HW_BSSID_0;
171
172 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
173 if (i)
174 return i - 1;
175
176 break;
177 default:
178 WARN_ON(1);
179 break;
180 }
181
182 return -1;
183 }
184
mt7915_init_bitrate_mask(struct ieee80211_vif * vif)185 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
186 {
187 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
188 int i;
189
190 for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
191 mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
192 mvif->bitrate_mask.control[i].he_gi = 0xff;
193 mvif->bitrate_mask.control[i].he_ltf = 0xff;
194 mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
195 memset(mvif->bitrate_mask.control[i].ht_mcs, 0xff,
196 sizeof(mvif->bitrate_mask.control[i].ht_mcs));
197 memset(mvif->bitrate_mask.control[i].vht_mcs, 0xff,
198 sizeof(mvif->bitrate_mask.control[i].vht_mcs));
199 memset(mvif->bitrate_mask.control[i].he_mcs, 0xff,
200 sizeof(mvif->bitrate_mask.control[i].he_mcs));
201 }
202 }
203
mt7915_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)204 static int mt7915_add_interface(struct ieee80211_hw *hw,
205 struct ieee80211_vif *vif)
206 {
207 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
208 struct mt7915_dev *dev = mt7915_hw_dev(hw);
209 struct mt7915_phy *phy = mt7915_hw_phy(hw);
210 struct mt76_txq *mtxq;
211 bool ext_phy = phy != &dev->phy;
212 int idx, ret = 0;
213
214 mutex_lock(&dev->mt76.mutex);
215
216 mt76_testmode_reset(phy->mt76, true);
217
218 if (vif->type == NL80211_IFTYPE_MONITOR &&
219 is_zero_ether_addr(vif->addr))
220 phy->monitor_vif = vif;
221
222 mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
223 if (mvif->mt76.idx >= (MT7915_MAX_INTERFACES << dev->dbdc_support)) {
224 ret = -ENOSPC;
225 goto out;
226 }
227
228 idx = get_omac_idx(vif->type, phy->omac_mask);
229 if (idx < 0) {
230 ret = -ENOSPC;
231 goto out;
232 }
233 mvif->mt76.omac_idx = idx;
234 mvif->phy = phy;
235 mvif->mt76.band_idx = phy->mt76->band_idx;
236
237 mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
238 if (ext_phy)
239 mvif->mt76.wmm_idx += 2;
240
241 ret = mt7915_mcu_add_dev_info(phy, vif, true);
242 if (ret)
243 goto out;
244
245 dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
246 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
247
248 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, mt7915_wtbl_size(dev));
249 if (idx < 0) {
250 ret = -ENOSPC;
251 goto out;
252 }
253
254 INIT_LIST_HEAD(&mvif->sta.rc_list);
255 INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
256 mvif->sta.wcid.idx = idx;
257 mvif->sta.wcid.phy_idx = ext_phy;
258 mvif->sta.wcid.hw_key_idx = -1;
259 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
260 mt76_wcid_init(&mvif->sta.wcid);
261
262 mt7915_mac_wtbl_update(dev, idx,
263 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
264
265 if (vif->txq) {
266 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
267 mtxq->wcid = idx;
268 }
269
270 if (vif->type != NL80211_IFTYPE_AP &&
271 (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
272 vif->offload_flags = 0;
273 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
274
275 mt7915_init_bitrate_mask(vif);
276 memset(&mvif->cap, -1, sizeof(mvif->cap));
277
278 mt7915_mcu_add_bss_info(phy, vif, true);
279 mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, true);
280 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
281
282 out:
283 mutex_unlock(&dev->mt76.mutex);
284
285 return ret;
286 }
287
mt7915_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)288 static void mt7915_remove_interface(struct ieee80211_hw *hw,
289 struct ieee80211_vif *vif)
290 {
291 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
292 struct mt7915_sta *msta = &mvif->sta;
293 struct mt7915_dev *dev = mt7915_hw_dev(hw);
294 struct mt7915_phy *phy = mt7915_hw_phy(hw);
295 int idx = msta->wcid.idx;
296
297 mt7915_mcu_add_bss_info(phy, vif, false);
298 mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
299 mt76_wcid_mask_clear(dev->mt76.wcid_mask, mvif->sta.wcid.idx);
300
301 mutex_lock(&dev->mt76.mutex);
302 mt76_testmode_reset(phy->mt76, true);
303 mutex_unlock(&dev->mt76.mutex);
304
305 if (vif == phy->monitor_vif)
306 phy->monitor_vif = NULL;
307
308 mt7915_mcu_add_dev_info(phy, vif, false);
309
310 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
311
312 mutex_lock(&dev->mt76.mutex);
313 dev->mt76.vif_mask &= ~BIT_ULL(mvif->mt76.idx);
314 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
315 mutex_unlock(&dev->mt76.mutex);
316
317 spin_lock_bh(&dev->mt76.sta_poll_lock);
318 if (!list_empty(&msta->wcid.poll_list))
319 list_del_init(&msta->wcid.poll_list);
320 spin_unlock_bh(&dev->mt76.sta_poll_lock);
321
322 mt76_wcid_cleanup(&dev->mt76, &msta->wcid);
323 }
324
mt7915_set_channel(struct mt76_phy * mphy)325 int mt7915_set_channel(struct mt76_phy *mphy)
326 {
327 struct mt7915_phy *phy = mphy->priv;
328 struct mt7915_dev *dev = phy->dev;
329 int ret;
330
331 if (dev->cal) {
332 ret = mt7915_mcu_apply_tx_dpd(phy);
333 if (ret)
334 goto out;
335 }
336
337 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
338 if (ret)
339 goto out;
340
341 mt7915_mac_set_timing(phy);
342 ret = mt7915_dfs_init_radar_detector(phy);
343 mt7915_mac_cca_stats_reset(phy);
344
345 mt7915_mac_reset_counters(phy);
346 phy->noise = 0;
347
348 out:
349 if (!mt76_testmode_enabled(phy->mt76))
350 ieee80211_queue_delayed_work(phy->mt76->hw,
351 &phy->mt76->mac_work,
352 MT7915_WATCHDOG_TIME);
353
354 return ret;
355 }
356
mt7915_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)357 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
358 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
359 struct ieee80211_key_conf *key)
360 {
361 struct mt7915_dev *dev = mt7915_hw_dev(hw);
362 struct mt7915_phy *phy = mt7915_hw_phy(hw);
363 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
364 struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
365 &mvif->sta;
366 struct mt76_wcid *wcid = &msta->wcid;
367 u8 *wcid_keyidx = &wcid->hw_key_idx;
368 int idx = key->keyidx;
369 int err = 0;
370
371 if (sta && !wcid->sta)
372 return -EOPNOTSUPP;
373
374 /* The hardware does not support per-STA RX GTK, fallback
375 * to software mode for these.
376 */
377 if ((vif->type == NL80211_IFTYPE_ADHOC ||
378 vif->type == NL80211_IFTYPE_MESH_POINT) &&
379 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
380 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
381 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
382 return -EOPNOTSUPP;
383
384 /* fall back to sw encryption for unsupported ciphers */
385 switch (key->cipher) {
386 case WLAN_CIPHER_SUITE_AES_CMAC:
387 wcid_keyidx = &wcid->hw_key_idx2;
388 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
389 break;
390 case WLAN_CIPHER_SUITE_TKIP:
391 case WLAN_CIPHER_SUITE_CCMP:
392 case WLAN_CIPHER_SUITE_CCMP_256:
393 case WLAN_CIPHER_SUITE_GCMP:
394 case WLAN_CIPHER_SUITE_GCMP_256:
395 case WLAN_CIPHER_SUITE_SMS4:
396 break;
397 case WLAN_CIPHER_SUITE_WEP40:
398 case WLAN_CIPHER_SUITE_WEP104:
399 default:
400 return -EOPNOTSUPP;
401 }
402
403 mutex_lock(&dev->mt76.mutex);
404
405 if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
406 mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
407 mt7915_mcu_add_bss_info(phy, vif, true);
408 }
409
410 if (cmd == SET_KEY) {
411 *wcid_keyidx = idx;
412 } else {
413 if (idx == *wcid_keyidx)
414 *wcid_keyidx = -1;
415 goto out;
416 }
417
418 mt76_wcid_key_setup(&dev->mt76, wcid, key);
419 err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
420 key, MCU_EXT_CMD(STA_REC_UPDATE),
421 &msta->wcid, cmd);
422 out:
423 mutex_unlock(&dev->mt76.mutex);
424
425 return err;
426 }
427
mt7915_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)428 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
429 const struct cfg80211_sar_specs *sar)
430 {
431 struct mt7915_phy *phy = mt7915_hw_phy(hw);
432 struct mt7915_dev *dev = mt7915_hw_dev(hw);
433 int err = -EINVAL;
434
435 mutex_lock(&dev->mt76.mutex);
436 if (!cfg80211_chandef_valid(&phy->mt76->chandef))
437 goto out;
438
439 err = mt76_init_sar_power(hw, sar);
440 if (err)
441 goto out;
442
443 err = mt7915_mcu_set_txpower_sku(phy);
444 out:
445 mutex_unlock(&dev->mt76.mutex);
446
447 return err;
448 }
449
mt7915_config(struct ieee80211_hw * hw,u32 changed)450 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
451 {
452 struct mt7915_dev *dev = mt7915_hw_dev(hw);
453 struct mt7915_phy *phy = mt7915_hw_phy(hw);
454 int ret;
455
456 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
457 #ifdef CONFIG_NL80211_TESTMODE
458 if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
459 mutex_lock(&dev->mt76.mutex);
460 mt76_testmode_reset(phy->mt76, false);
461 mutex_unlock(&dev->mt76.mutex);
462 }
463 #endif
464 ret = mt76_update_channel(phy->mt76);
465 if (ret)
466 return ret;
467 }
468
469 if (changed & (IEEE80211_CONF_CHANGE_POWER |
470 IEEE80211_CONF_CHANGE_CHANNEL)) {
471 ret = mt7915_mcu_set_txpower_sku(phy);
472 if (ret)
473 return ret;
474 }
475
476 mutex_lock(&dev->mt76.mutex);
477
478 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
479 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
480 bool band = phy->mt76->band_idx;
481 u32 rxfilter = phy->rxfilter;
482
483 if (!enabled) {
484 rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
485 dev->monitor_mask &= ~BIT(band);
486 } else {
487 rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
488 dev->monitor_mask |= BIT(band);
489 }
490
491 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
492 enabled);
493 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_MDP_DCR0_RX_HDR_TRANS_EN,
494 !dev->monitor_mask);
495 mt76_testmode_reset(phy->mt76, true);
496 mt76_wr(dev, MT_WF_RFCR(band), rxfilter);
497 }
498
499 mutex_unlock(&dev->mt76.mutex);
500
501 return 0;
502 }
503
504 static int
mt7915_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)505 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
506 unsigned int link_id, u16 queue,
507 const struct ieee80211_tx_queue_params *params)
508 {
509 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
510
511 /* no need to update right away, we'll get BSS_CHANGED_QOS */
512 queue = mt76_connac_lmac_mapping(queue);
513 mvif->queue_params[queue] = *params;
514
515 return 0;
516 }
517
mt7915_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)518 static void mt7915_configure_filter(struct ieee80211_hw *hw,
519 unsigned int changed_flags,
520 unsigned int *total_flags,
521 u64 multicast)
522 {
523 struct mt7915_dev *dev = mt7915_hw_dev(hw);
524 struct mt7915_phy *phy = mt7915_hw_phy(hw);
525 bool band = phy->mt76->band_idx;
526 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
527 MT_WF_RFCR1_DROP_BF_POLL |
528 MT_WF_RFCR1_DROP_BA |
529 MT_WF_RFCR1_DROP_CFEND |
530 MT_WF_RFCR1_DROP_CFACK;
531 u32 rxfilter;
532 u32 flags = 0;
533
534 #define MT76_FILTER(_flag, _hw) do { \
535 flags |= *total_flags & FIF_##_flag; \
536 phy->rxfilter &= ~(_hw); \
537 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
538 } while (0)
539
540 mutex_lock(&dev->mt76.mutex);
541
542 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
543 MT_WF_RFCR_DROP_OTHER_BEACON |
544 MT_WF_RFCR_DROP_FRAME_REPORT |
545 MT_WF_RFCR_DROP_PROBEREQ |
546 MT_WF_RFCR_DROP_MCAST_FILTERED |
547 MT_WF_RFCR_DROP_MCAST |
548 MT_WF_RFCR_DROP_BCAST |
549 MT_WF_RFCR_DROP_DUPLICATE |
550 MT_WF_RFCR_DROP_A2_BSSID |
551 MT_WF_RFCR_DROP_UNWANTED_CTL |
552 MT_WF_RFCR_DROP_STBC_MULTI);
553
554 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
555 MT_WF_RFCR_DROP_A3_MAC |
556 MT_WF_RFCR_DROP_A3_BSSID);
557
558 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
559
560 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
561 MT_WF_RFCR_DROP_RTS |
562 MT_WF_RFCR_DROP_CTL_RSV);
563
564 *total_flags = flags;
565 rxfilter = phy->rxfilter;
566 if (hw->conf.flags & IEEE80211_CONF_MONITOR)
567 rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
568 else
569 rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
570 mt76_wr(dev, MT_WF_RFCR(band), rxfilter);
571
572 if (*total_flags & FIF_CONTROL)
573 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
574 else
575 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
576
577 mutex_unlock(&dev->mt76.mutex);
578 }
579
580 static void
mt7915_update_bss_color(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_he_bss_color * bss_color)581 mt7915_update_bss_color(struct ieee80211_hw *hw,
582 struct ieee80211_vif *vif,
583 struct cfg80211_he_bss_color *bss_color)
584 {
585 struct mt7915_dev *dev = mt7915_hw_dev(hw);
586
587 switch (vif->type) {
588 case NL80211_IFTYPE_AP: {
589 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
590
591 if (mvif->mt76.omac_idx > HW_BSSID_MAX)
592 return;
593 fallthrough;
594 }
595 case NL80211_IFTYPE_STATION:
596 mt7915_mcu_update_bss_color(dev, vif, bss_color);
597 break;
598 default:
599 break;
600 }
601 }
602
mt7915_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)603 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
604 struct ieee80211_vif *vif,
605 struct ieee80211_bss_conf *info,
606 u64 changed)
607 {
608 struct mt7915_phy *phy = mt7915_hw_phy(hw);
609 struct mt7915_dev *dev = mt7915_hw_dev(hw);
610 int set_bss_info = -1, set_sta = -1;
611
612 mutex_lock(&dev->mt76.mutex);
613
614 /*
615 * station mode uses BSSID to map the wlan entry to a peer,
616 * and then peer references bss_info_rfch to set bandwidth cap.
617 */
618 if (changed & BSS_CHANGED_BSSID &&
619 vif->type == NL80211_IFTYPE_STATION)
620 set_bss_info = set_sta = !is_zero_ether_addr(info->bssid);
621 if (changed & BSS_CHANGED_ASSOC)
622 set_bss_info = vif->cfg.assoc;
623 if (changed & BSS_CHANGED_BEACON_ENABLED &&
624 info->enable_beacon &&
625 vif->type != NL80211_IFTYPE_AP)
626 set_bss_info = set_sta = 1;
627
628 if (set_bss_info == 1)
629 mt7915_mcu_add_bss_info(phy, vif, true);
630 if (set_sta == 1)
631 mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, false);
632
633 if (changed & BSS_CHANGED_ERP_CTS_PROT)
634 mt7915_mac_enable_rtscts(dev, vif, info->use_cts_prot);
635
636 if (changed & BSS_CHANGED_ERP_SLOT) {
637 int slottime = info->use_short_slot ? 9 : 20;
638
639 if (slottime != phy->slottime) {
640 phy->slottime = slottime;
641 mt7915_mac_set_timing(phy);
642 }
643 }
644
645 /* ensure that enable txcmd_mode after bss_info */
646 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
647 mt7915_mcu_set_tx(dev, vif);
648
649 if (changed & BSS_CHANGED_HE_OBSS_PD)
650 mt7915_mcu_add_obss_spr(phy, vif, &info->he_obss_pd);
651
652 if (changed & BSS_CHANGED_HE_BSS_COLOR)
653 mt7915_update_bss_color(hw, vif, &info->he_bss_color);
654
655 if (changed & (BSS_CHANGED_BEACON |
656 BSS_CHANGED_BEACON_ENABLED))
657 mt7915_mcu_add_beacon(hw, vif, info->enable_beacon, changed);
658
659 if (changed & (BSS_CHANGED_UNSOL_BCAST_PROBE_RESP |
660 BSS_CHANGED_FILS_DISCOVERY))
661 mt7915_mcu_add_inband_discov(dev, vif, changed);
662
663 if (set_bss_info == 0)
664 mt7915_mcu_add_bss_info(phy, vif, false);
665 if (set_sta == 0)
666 mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
667
668 mutex_unlock(&dev->mt76.mutex);
669 }
670
671 static void
mt7915_vif_check_caps(struct mt7915_phy * phy,struct ieee80211_vif * vif)672 mt7915_vif_check_caps(struct mt7915_phy *phy, struct ieee80211_vif *vif)
673 {
674 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
675 struct mt7915_vif_cap *vc = &mvif->cap;
676
677 vc->ht_ldpc = vif->bss_conf.ht_ldpc;
678 vc->vht_ldpc = vif->bss_conf.vht_ldpc;
679 vc->vht_su_ebfer = vif->bss_conf.vht_su_beamformer;
680 vc->vht_su_ebfee = vif->bss_conf.vht_su_beamformee;
681 vc->vht_mu_ebfer = vif->bss_conf.vht_mu_beamformer;
682 vc->vht_mu_ebfee = vif->bss_conf.vht_mu_beamformee;
683 vc->he_ldpc = vif->bss_conf.he_ldpc;
684 vc->he_su_ebfer = vif->bss_conf.he_su_beamformer;
685 vc->he_su_ebfee = vif->bss_conf.he_su_beamformee;
686 vc->he_mu_ebfer = vif->bss_conf.he_mu_beamformer;
687 }
688
689 static int
mt7915_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)690 mt7915_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
691 struct ieee80211_bss_conf *link_conf)
692 {
693 struct mt7915_phy *phy = mt7915_hw_phy(hw);
694 struct mt7915_dev *dev = mt7915_hw_dev(hw);
695 int err;
696
697 mutex_lock(&dev->mt76.mutex);
698
699 mt7915_vif_check_caps(phy, vif);
700
701 err = mt7915_mcu_add_bss_info(phy, vif, true);
702 if (err)
703 goto out;
704 err = mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_PORT_SECURE, false);
705 out:
706 mutex_unlock(&dev->mt76.mutex);
707
708 return err;
709 }
710
711 static void
mt7915_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)712 mt7915_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
713 struct ieee80211_bss_conf *link_conf)
714 {
715 struct mt7915_dev *dev = mt7915_hw_dev(hw);
716
717 mutex_lock(&dev->mt76.mutex);
718 mt7915_mcu_add_sta(dev, vif, NULL, CONN_STATE_DISCONNECT, false);
719 mutex_unlock(&dev->mt76.mutex);
720 }
721
722 static void
mt7915_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)723 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
724 struct ieee80211_vif *vif,
725 struct cfg80211_chan_def *chandef)
726 {
727 struct mt7915_dev *dev = mt7915_hw_dev(hw);
728
729 mutex_lock(&dev->mt76.mutex);
730 mt7915_mcu_add_beacon(hw, vif, true, BSS_CHANGED_BEACON);
731 mutex_unlock(&dev->mt76.mutex);
732 }
733
mt7915_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)734 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
735 struct ieee80211_sta *sta)
736 {
737 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
738 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
739 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
740 bool ext_phy = mvif->phy != &dev->phy;
741 int idx;
742
743 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
744 if (idx < 0)
745 return -ENOSPC;
746
747 INIT_LIST_HEAD(&msta->rc_list);
748 INIT_LIST_HEAD(&msta->wcid.poll_list);
749 msta->vif = mvif;
750 msta->wcid.sta_disabled = 1;
751 msta->wcid.idx = idx;
752 msta->wcid.phy_idx = ext_phy;
753 msta->jiffies = jiffies;
754
755 ewma_avg_signal_init(&msta->avg_ack_signal);
756
757 mt7915_mac_wtbl_update(dev, idx,
758 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
759 mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_DISCONNECT, true);
760
761 return 0;
762 }
763
mt7915_mac_sta_event(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum mt76_sta_event ev)764 int mt7915_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif,
765 struct ieee80211_sta *sta, enum mt76_sta_event ev)
766 {
767 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
768 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
769 int i, ret;
770 u32 addr;
771
772 switch (ev) {
773 case MT76_STA_EVENT_ASSOC:
774 ret = mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_CONNECT, true);
775 if (ret)
776 return ret;
777
778 addr = mt7915_mac_wtbl_lmac_addr(dev, msta->wcid.idx, 30);
779 mt76_rmw_field(dev, addr, GENMASK(7, 0), 0xa0);
780
781 ret = mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
782 if (ret)
783 return ret;
784
785 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
786 msta->wcid.sta = 1;
787 msta->wcid.sta_disabled = 0;
788
789 return 0;
790
791 case MT76_STA_EVENT_AUTHORIZE:
792 return mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_PORT_SECURE, false);
793
794 case MT76_STA_EVENT_DISASSOC:
795 for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
796 mt7915_mac_twt_teardown_flow(dev, msta, i);
797
798 mt7915_mcu_add_sta(dev, vif, sta, CONN_STATE_DISCONNECT, false);
799 msta->wcid.sta_disabled = 1;
800 msta->wcid.sta = 0;
801 return 0;
802 }
803
804 return 0;
805 }
806
mt7915_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)807 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
808 struct ieee80211_sta *sta)
809 {
810 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
811 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
812
813 mt7915_mac_wtbl_update(dev, msta->wcid.idx,
814 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
815
816 spin_lock_bh(&mdev->sta_poll_lock);
817 if (!list_empty(&msta->wcid.poll_list))
818 list_del_init(&msta->wcid.poll_list);
819 if (!list_empty(&msta->rc_list))
820 list_del_init(&msta->rc_list);
821 spin_unlock_bh(&mdev->sta_poll_lock);
822 }
823
mt7915_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)824 static void mt7915_tx(struct ieee80211_hw *hw,
825 struct ieee80211_tx_control *control,
826 struct sk_buff *skb)
827 {
828 struct mt7915_dev *dev = mt7915_hw_dev(hw);
829 struct mt76_phy *mphy = hw->priv;
830 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
831 struct ieee80211_vif *vif = info->control.vif;
832 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
833
834 if (control->sta) {
835 struct mt7915_sta *sta;
836
837 sta = (struct mt7915_sta *)control->sta->drv_priv;
838 wcid = &sta->wcid;
839 }
840
841 if (vif && !control->sta) {
842 struct mt7915_vif *mvif;
843
844 mvif = (struct mt7915_vif *)vif->drv_priv;
845 wcid = &mvif->sta.wcid;
846 }
847
848 mt76_tx(mphy, control->sta, wcid, skb);
849 }
850
mt7915_set_rts_threshold(struct ieee80211_hw * hw,u32 val)851 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
852 {
853 struct mt7915_dev *dev = mt7915_hw_dev(hw);
854 struct mt7915_phy *phy = mt7915_hw_phy(hw);
855 int ret;
856
857 mutex_lock(&dev->mt76.mutex);
858 ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val,
859 phy->mt76->band_idx);
860 mutex_unlock(&dev->mt76.mutex);
861
862 return ret;
863 }
864
865 static int
mt7915_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)866 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
867 struct ieee80211_ampdu_params *params)
868 {
869 enum ieee80211_ampdu_mlme_action action = params->action;
870 struct mt7915_dev *dev = mt7915_hw_dev(hw);
871 struct ieee80211_sta *sta = params->sta;
872 struct ieee80211_txq *txq = sta->txq[params->tid];
873 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
874 u16 tid = params->tid;
875 u16 ssn = params->ssn;
876 struct mt76_txq *mtxq;
877 int ret = 0;
878
879 if (!txq)
880 return -EINVAL;
881
882 mtxq = (struct mt76_txq *)txq->drv_priv;
883
884 mutex_lock(&dev->mt76.mutex);
885 switch (action) {
886 case IEEE80211_AMPDU_RX_START:
887 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
888 params->buf_size);
889 ret = mt7915_mcu_add_rx_ba(dev, params, true);
890 break;
891 case IEEE80211_AMPDU_RX_STOP:
892 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
893 ret = mt7915_mcu_add_rx_ba(dev, params, false);
894 break;
895 case IEEE80211_AMPDU_TX_OPERATIONAL:
896 mtxq->aggr = true;
897 mtxq->send_bar = false;
898 ret = mt7915_mcu_add_tx_ba(dev, params, true);
899 break;
900 case IEEE80211_AMPDU_TX_STOP_FLUSH:
901 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
902 mtxq->aggr = false;
903 clear_bit(tid, &msta->wcid.ampdu_state);
904 ret = mt7915_mcu_add_tx_ba(dev, params, false);
905 break;
906 case IEEE80211_AMPDU_TX_START:
907 set_bit(tid, &msta->wcid.ampdu_state);
908 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
909 break;
910 case IEEE80211_AMPDU_TX_STOP_CONT:
911 mtxq->aggr = false;
912 clear_bit(tid, &msta->wcid.ampdu_state);
913 ret = mt7915_mcu_add_tx_ba(dev, params, false);
914 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
915 break;
916 }
917 mutex_unlock(&dev->mt76.mutex);
918
919 return ret;
920 }
921
922 static int
mt7915_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)923 mt7915_get_stats(struct ieee80211_hw *hw,
924 struct ieee80211_low_level_stats *stats)
925 {
926 struct mt7915_phy *phy = mt7915_hw_phy(hw);
927 struct mt7915_dev *dev = mt7915_hw_dev(hw);
928 struct mt76_mib_stats *mib = &phy->mib;
929
930 mutex_lock(&dev->mt76.mutex);
931
932 stats->dot11RTSSuccessCount = mib->rts_cnt;
933 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
934 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
935 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
936
937 mutex_unlock(&dev->mt76.mutex);
938
939 return 0;
940 }
941
__mt7915_get_tsf(struct ieee80211_hw * hw,struct mt7915_vif * mvif)942 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
943 {
944 struct mt7915_dev *dev = mt7915_hw_dev(hw);
945 struct mt7915_phy *phy = mt7915_hw_phy(hw);
946 bool band = phy->mt76->band_idx;
947 union {
948 u64 t64;
949 u32 t32[2];
950 } tsf;
951 u16 n;
952
953 lockdep_assert_held(&dev->mt76.mutex);
954
955 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
956 : mvif->mt76.omac_idx;
957 /* TSF software read */
958 if (is_mt7915(&dev->mt76))
959 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
960 MT_LPON_TCR_SW_READ);
961 else
962 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
963 MT_LPON_TCR_SW_READ);
964 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
965 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
966
967 return tsf.t64;
968 }
969
970 static u64
mt7915_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)971 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
972 {
973 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
974 struct mt7915_dev *dev = mt7915_hw_dev(hw);
975 u64 ret;
976
977 mutex_lock(&dev->mt76.mutex);
978 ret = __mt7915_get_tsf(hw, mvif);
979 mutex_unlock(&dev->mt76.mutex);
980
981 return ret;
982 }
983
984 static void
mt7915_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)985 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
986 u64 timestamp)
987 {
988 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
989 struct mt7915_dev *dev = mt7915_hw_dev(hw);
990 struct mt7915_phy *phy = mt7915_hw_phy(hw);
991 bool band = phy->mt76->band_idx;
992 union {
993 u64 t64;
994 u32 t32[2];
995 } tsf = { .t64 = timestamp, };
996 u16 n;
997
998 mutex_lock(&dev->mt76.mutex);
999
1000 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1001 : mvif->mt76.omac_idx;
1002 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
1003 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
1004 /* TSF software overwrite */
1005 if (is_mt7915(&dev->mt76))
1006 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1007 MT_LPON_TCR_SW_WRITE);
1008 else
1009 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1010 MT_LPON_TCR_SW_WRITE);
1011
1012 mutex_unlock(&dev->mt76.mutex);
1013 }
1014
1015 static void
mt7915_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)1016 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1017 s64 timestamp)
1018 {
1019 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1020 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1021 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1022 bool band = phy->mt76->band_idx;
1023 union {
1024 u64 t64;
1025 u32 t32[2];
1026 } tsf = { .t64 = timestamp, };
1027 u16 n;
1028
1029 mutex_lock(&dev->mt76.mutex);
1030
1031 n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
1032 : mvif->mt76.omac_idx;
1033 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
1034 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
1035 /* TSF software adjust*/
1036 if (is_mt7915(&dev->mt76))
1037 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
1038 MT_LPON_TCR_SW_ADJUST);
1039 else
1040 mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
1041 MT_LPON_TCR_SW_ADJUST);
1042
1043 mutex_unlock(&dev->mt76.mutex);
1044 }
1045
1046 static void
mt7915_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)1047 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1048 {
1049 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1050 struct mt7915_dev *dev = phy->dev;
1051
1052 mutex_lock(&dev->mt76.mutex);
1053 phy->coverage_class = max_t(s16, coverage_class, 0);
1054 mt7915_mac_set_timing(phy);
1055 mutex_unlock(&dev->mt76.mutex);
1056 }
1057
1058 static int
mt7915_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)1059 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1060 {
1061 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1062 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1063 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1064 u8 chainshift = dev->chainshift;
1065 u8 band = phy->mt76->band_idx;
1066
1067 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1068 return -EINVAL;
1069
1070 mutex_lock(&dev->mt76.mutex);
1071
1072 phy->mt76->antenna_mask = tx_ant;
1073
1074 /* handle a variant of mt7916/mt7981 which has 3T3R but nss2 on 5 GHz band */
1075 if ((is_mt7916(&dev->mt76) || is_mt7981(&dev->mt76)) &&
1076 band && hweight8(tx_ant) == max_nss)
1077 phy->mt76->chainmask = (dev->chainmask >> chainshift) << chainshift;
1078 else
1079 phy->mt76->chainmask = tx_ant << (chainshift * band);
1080
1081 mt76_set_stream_caps(phy->mt76, true);
1082 mt7915_set_stream_vht_txbf_caps(phy);
1083 mt7915_set_stream_he_caps(phy);
1084
1085 mutex_unlock(&dev->mt76.mutex);
1086
1087 return 0;
1088 }
1089
mt7915_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)1090 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
1091 struct ieee80211_vif *vif,
1092 struct ieee80211_sta *sta,
1093 struct station_info *sinfo)
1094 {
1095 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1096 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1097 struct rate_info *txrate = &msta->wcid.rate;
1098 struct rate_info rxrate = {};
1099
1100 if (!mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
1101 sinfo->rxrate = rxrate;
1102 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
1103 }
1104
1105 if (txrate->legacy || txrate->flags) {
1106 if (txrate->legacy) {
1107 sinfo->txrate.legacy = txrate->legacy;
1108 } else {
1109 sinfo->txrate.mcs = txrate->mcs;
1110 sinfo->txrate.nss = txrate->nss;
1111 sinfo->txrate.bw = txrate->bw;
1112 sinfo->txrate.he_gi = txrate->he_gi;
1113 sinfo->txrate.he_dcm = txrate->he_dcm;
1114 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1115 }
1116 sinfo->txrate.flags = txrate->flags;
1117 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1118 }
1119
1120 /* offloading flows bypass networking stack, so driver counts and
1121 * reports sta statistics via NL80211_STA_INFO when WED is active.
1122 */
1123 if (mtk_wed_device_active(&phy->dev->mt76.mmio.wed)) {
1124 sinfo->tx_bytes = msta->wcid.stats.tx_bytes;
1125 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
1126
1127 if (!mt7915_mcu_wed_wa_tx_stats(phy->dev, msta->wcid.idx)) {
1128 sinfo->tx_packets = msta->wcid.stats.tx_packets;
1129 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1130 }
1131
1132 if (mtk_wed_get_rx_capa(&phy->dev->mt76.mmio.wed)) {
1133 sinfo->rx_bytes = msta->wcid.stats.rx_bytes;
1134 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
1135
1136 sinfo->rx_packets = msta->wcid.stats.rx_packets;
1137 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1138 }
1139 }
1140
1141 sinfo->tx_failed = msta->wcid.stats.tx_failed;
1142 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1143
1144 sinfo->tx_retries = msta->wcid.stats.tx_retries;
1145 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
1146
1147 sinfo->ack_signal = (s8)msta->ack_signal;
1148 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
1149
1150 sinfo->avg_ack_signal = -(s8)ewma_avg_signal_read(&msta->avg_ack_signal);
1151 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
1152 }
1153
mt7915_sta_rc_work(void * data,struct ieee80211_sta * sta)1154 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1155 {
1156 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1157 struct mt7915_dev *dev = msta->vif->phy->dev;
1158 u32 *changed = data;
1159
1160 spin_lock_bh(&dev->mt76.sta_poll_lock);
1161 msta->changed |= *changed;
1162 if (list_empty(&msta->rc_list))
1163 list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1164 spin_unlock_bh(&dev->mt76.sta_poll_lock);
1165 }
1166
mt7915_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)1167 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1168 struct ieee80211_vif *vif,
1169 struct ieee80211_sta *sta,
1170 u32 changed)
1171 {
1172 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1173 struct mt7915_dev *dev = phy->dev;
1174 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1175
1176 if (!msta->wcid.sta)
1177 return;
1178
1179 mt7915_sta_rc_work(&changed, sta);
1180 ieee80211_queue_work(hw, &dev->rc_work);
1181 }
1182
1183 static int
mt7915_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)1184 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1185 const struct cfg80211_bitrate_mask *mask)
1186 {
1187 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1188 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1189 struct mt7915_dev *dev = phy->dev;
1190 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1191
1192 mvif->bitrate_mask = *mask;
1193
1194 /* if multiple rates across different preambles are given we can
1195 * reconfigure this info with all peers using sta_rec command with
1196 * the below exception cases.
1197 * - single rate : if a rate is passed along with different preambles,
1198 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1199 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1200 * then multiple MCS setting (MCS 4,5,6) is not supported.
1201 */
1202 ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1203 ieee80211_queue_work(hw, &dev->rc_work);
1204
1205 return 0;
1206 }
1207
mt7915_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1208 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1209 struct ieee80211_vif *vif,
1210 struct ieee80211_sta *sta,
1211 bool enabled)
1212 {
1213 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1214 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1215
1216 if (enabled)
1217 set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1218 else
1219 clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1220
1221 if (!msta->wcid.sta)
1222 return;
1223
1224 mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1225 }
1226
mt7915_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1227 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1228 struct ieee80211_vif *vif,
1229 struct ieee80211_sta *sta,
1230 bool enabled)
1231 {
1232 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1233 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1234
1235 if (enabled)
1236 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1237 else
1238 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1239
1240 if (!msta->wcid.sta)
1241 return;
1242
1243 mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1244 }
1245
mt7915_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1246 static int mt7915_sta_set_txpwr(struct ieee80211_hw *hw,
1247 struct ieee80211_vif *vif,
1248 struct ieee80211_sta *sta)
1249 {
1250 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1251 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1252 s16 txpower = sta->deflink.txpwr.power;
1253 int ret;
1254
1255 if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC)
1256 txpower = 0;
1257
1258 mutex_lock(&dev->mt76.mutex);
1259
1260 /* NOTE: temporarily use 0 as minimum limit, which is a
1261 * global setting and will be applied to all stations.
1262 */
1263 ret = mt7915_mcu_set_txpower_frame_min(phy, 0);
1264 if (ret)
1265 goto out;
1266
1267 /* This only applies to data frames while pushing traffic,
1268 * whereas the management frames or other packets that are
1269 * using fixed rate can be configured via TxD.
1270 */
1271 ret = mt7915_mcu_set_txpower_frame(phy, vif, sta, txpower);
1272
1273 out:
1274 mutex_unlock(&dev->mt76.mutex);
1275
1276 return ret;
1277 }
1278
1279 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1280 "tx_ampdu_cnt",
1281 "tx_stop_q_empty_cnt",
1282 "tx_mpdu_attempts",
1283 "tx_mpdu_success",
1284 "tx_rwp_fail_cnt",
1285 "tx_rwp_need_cnt",
1286 "tx_pkt_ebf_cnt",
1287 "tx_pkt_ibf_cnt",
1288 "tx_ampdu_len:0-1",
1289 "tx_ampdu_len:2-10",
1290 "tx_ampdu_len:11-19",
1291 "tx_ampdu_len:20-28",
1292 "tx_ampdu_len:29-37",
1293 "tx_ampdu_len:38-46",
1294 "tx_ampdu_len:47-55",
1295 "tx_ampdu_len:56-79",
1296 "tx_ampdu_len:80-103",
1297 "tx_ampdu_len:104-127",
1298 "tx_ampdu_len:128-151",
1299 "tx_ampdu_len:152-175",
1300 "tx_ampdu_len:176-199",
1301 "tx_ampdu_len:200-223",
1302 "tx_ampdu_len:224-247",
1303 "ba_miss_count",
1304 "tx_beamformer_ppdu_iBF",
1305 "tx_beamformer_ppdu_eBF",
1306 "tx_beamformer_rx_feedback_all",
1307 "tx_beamformer_rx_feedback_he",
1308 "tx_beamformer_rx_feedback_vht",
1309 "tx_beamformer_rx_feedback_ht",
1310 "tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1311 "tx_beamformer_rx_feedback_nc",
1312 "tx_beamformer_rx_feedback_nr",
1313 "tx_beamformee_ok_feedback_pkts",
1314 "tx_beamformee_feedback_trig",
1315 "tx_mu_beamforming",
1316 "tx_mu_mpdu",
1317 "tx_mu_successful_mpdu",
1318 "tx_su_successful_mpdu",
1319 "tx_msdu_pack_1",
1320 "tx_msdu_pack_2",
1321 "tx_msdu_pack_3",
1322 "tx_msdu_pack_4",
1323 "tx_msdu_pack_5",
1324 "tx_msdu_pack_6",
1325 "tx_msdu_pack_7",
1326 "tx_msdu_pack_8",
1327
1328 /* rx counters */
1329 "rx_fifo_full_cnt",
1330 "rx_mpdu_cnt",
1331 "channel_idle_cnt",
1332 "primary_cca_busy_time",
1333 "secondary_cca_busy_time",
1334 "primary_energy_detect_time",
1335 "cck_mdrdy_time",
1336 "ofdm_mdrdy_time",
1337 "green_mdrdy_time",
1338 "rx_vector_mismatch_cnt",
1339 "rx_delimiter_fail_cnt",
1340 "rx_mrdy_cnt",
1341 "rx_len_mismatch_cnt",
1342 "rx_ampdu_cnt",
1343 "rx_ampdu_bytes_cnt",
1344 "rx_ampdu_valid_subframe_cnt",
1345 "rx_ampdu_valid_subframe_b_cnt",
1346 "rx_pfdrop_cnt",
1347 "rx_vec_queue_overflow_drop_cnt",
1348 "rx_ba_cnt",
1349
1350 /* muru mu-mimo and ofdma related stats */
1351 "dl_cck_cnt",
1352 "dl_ofdm_cnt",
1353 "dl_htmix_cnt",
1354 "dl_htgf_cnt",
1355 "dl_vht_su_cnt",
1356 "dl_vht_2mu_cnt",
1357 "dl_vht_3mu_cnt",
1358 "dl_vht_4mu_cnt",
1359 "dl_he_su_cnt",
1360 "dl_he_ext_su_cnt",
1361 "dl_he_2ru_cnt",
1362 "dl_he_2mu_cnt",
1363 "dl_he_3ru_cnt",
1364 "dl_he_3mu_cnt",
1365 "dl_he_4ru_cnt",
1366 "dl_he_4mu_cnt",
1367 "dl_he_5to8ru_cnt",
1368 "dl_he_9to16ru_cnt",
1369 "dl_he_gtr16ru_cnt",
1370
1371 "ul_hetrig_su_cnt",
1372 "ul_hetrig_2ru_cnt",
1373 "ul_hetrig_3ru_cnt",
1374 "ul_hetrig_4ru_cnt",
1375 "ul_hetrig_5to8ru_cnt",
1376 "ul_hetrig_9to16ru_cnt",
1377 "ul_hetrig_gtr16ru_cnt",
1378 "ul_hetrig_2mu_cnt",
1379 "ul_hetrig_3mu_cnt",
1380 "ul_hetrig_4mu_cnt",
1381
1382 /* per vif counters */
1383 "v_tx_mode_cck",
1384 "v_tx_mode_ofdm",
1385 "v_tx_mode_ht",
1386 "v_tx_mode_ht_gf",
1387 "v_tx_mode_vht",
1388 "v_tx_mode_he_su",
1389 "v_tx_mode_he_ext_su",
1390 "v_tx_mode_he_tb",
1391 "v_tx_mode_he_mu",
1392 "v_tx_bw_20",
1393 "v_tx_bw_40",
1394 "v_tx_bw_80",
1395 "v_tx_bw_160",
1396 "v_tx_mcs_0",
1397 "v_tx_mcs_1",
1398 "v_tx_mcs_2",
1399 "v_tx_mcs_3",
1400 "v_tx_mcs_4",
1401 "v_tx_mcs_5",
1402 "v_tx_mcs_6",
1403 "v_tx_mcs_7",
1404 "v_tx_mcs_8",
1405 "v_tx_mcs_9",
1406 "v_tx_mcs_10",
1407 "v_tx_mcs_11",
1408 "v_tx_nss_1",
1409 "v_tx_nss_2",
1410 "v_tx_nss_3",
1411 "v_tx_nss_4",
1412 };
1413
1414 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1415
1416 /* Ethtool related API */
1417 static
mt7915_get_et_strings(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 sset,u8 * data)1418 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1419 struct ieee80211_vif *vif,
1420 u32 sset, u8 *data)
1421 {
1422 if (sset != ETH_SS_STATS)
1423 return;
1424
1425 memcpy(data, mt7915_gstrings_stats, sizeof(mt7915_gstrings_stats));
1426 data += sizeof(mt7915_gstrings_stats);
1427 page_pool_ethtool_stats_get_strings(data);
1428 }
1429
1430 static
mt7915_get_et_sset_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int sset)1431 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1432 struct ieee80211_vif *vif, int sset)
1433 {
1434 if (sset != ETH_SS_STATS)
1435 return 0;
1436
1437 return MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1438 }
1439
mt7915_ethtool_worker(void * wi_data,struct ieee80211_sta * sta)1440 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1441 {
1442 struct mt76_ethtool_worker_info *wi = wi_data;
1443 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1444
1445 if (msta->vif->mt76.idx != wi->idx)
1446 return;
1447
1448 mt76_ethtool_worker(wi, &msta->wcid.stats, false);
1449 }
1450
1451 static
mt7915_get_et_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ethtool_stats * stats,u64 * data)1452 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1453 struct ieee80211_vif *vif,
1454 struct ethtool_stats *stats, u64 *data)
1455 {
1456 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1457 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1458 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1459 struct mt76_mib_stats *mib = &phy->mib;
1460 struct mt76_ethtool_worker_info wi = {
1461 .data = data,
1462 .idx = mvif->mt76.idx,
1463 };
1464 /* See mt7915_ampdu_stat_read_phy, etc */
1465 int i, ei = 0, stats_size;
1466
1467 mutex_lock(&dev->mt76.mutex);
1468
1469 mt7915_mac_update_stats(phy);
1470
1471 data[ei++] = mib->tx_ampdu_cnt;
1472 data[ei++] = mib->tx_stop_q_empty_cnt;
1473 data[ei++] = mib->tx_mpdu_attempts_cnt;
1474 data[ei++] = mib->tx_mpdu_success_cnt;
1475 data[ei++] = mib->tx_rwp_fail_cnt;
1476 data[ei++] = mib->tx_rwp_need_cnt;
1477 data[ei++] = mib->tx_pkt_ebf_cnt;
1478 data[ei++] = mib->tx_pkt_ibf_cnt;
1479
1480 /* Tx ampdu stat */
1481 for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1482 data[ei++] = phy->mt76->aggr_stats[i];
1483
1484 data[ei++] = phy->mib.ba_miss_cnt;
1485
1486 /* Tx Beamformer monitor */
1487 data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1488 data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1489
1490 /* Tx Beamformer Rx feedback monitor */
1491 data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1492 data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1493 data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1494 data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1495
1496 data[ei++] = mib->tx_bf_rx_fb_bw;
1497 data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1498 data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1499
1500 /* Tx Beamformee Rx NDPA & Tx feedback report */
1501 data[ei++] = mib->tx_bf_fb_cpl_cnt;
1502 data[ei++] = mib->tx_bf_fb_trig_cnt;
1503
1504 /* Tx SU & MU counters */
1505 data[ei++] = mib->tx_bf_cnt;
1506 data[ei++] = mib->tx_mu_mpdu_cnt;
1507 data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1508 data[ei++] = mib->tx_su_acked_mpdu_cnt;
1509
1510 /* Tx amsdu info (pack-count histogram) */
1511 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1512 data[ei++] = mib->tx_amsdu[i];
1513
1514 /* rx counters */
1515 data[ei++] = mib->rx_fifo_full_cnt;
1516 data[ei++] = mib->rx_mpdu_cnt;
1517 data[ei++] = mib->channel_idle_cnt;
1518 data[ei++] = mib->primary_cca_busy_time;
1519 data[ei++] = mib->secondary_cca_busy_time;
1520 data[ei++] = mib->primary_energy_detect_time;
1521 data[ei++] = mib->cck_mdrdy_time;
1522 data[ei++] = mib->ofdm_mdrdy_time;
1523 data[ei++] = mib->green_mdrdy_time;
1524 data[ei++] = mib->rx_vector_mismatch_cnt;
1525 data[ei++] = mib->rx_delimiter_fail_cnt;
1526 data[ei++] = mib->rx_mrdy_cnt;
1527 data[ei++] = mib->rx_len_mismatch_cnt;
1528 data[ei++] = mib->rx_ampdu_cnt;
1529 data[ei++] = mib->rx_ampdu_bytes_cnt;
1530 data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1531 data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1532 data[ei++] = mib->rx_pfdrop_cnt;
1533 data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1534 data[ei++] = mib->rx_ba_cnt;
1535
1536 data[ei++] = mib->dl_cck_cnt;
1537 data[ei++] = mib->dl_ofdm_cnt;
1538 data[ei++] = mib->dl_htmix_cnt;
1539 data[ei++] = mib->dl_htgf_cnt;
1540 data[ei++] = mib->dl_vht_su_cnt;
1541 data[ei++] = mib->dl_vht_2mu_cnt;
1542 data[ei++] = mib->dl_vht_3mu_cnt;
1543 data[ei++] = mib->dl_vht_4mu_cnt;
1544 data[ei++] = mib->dl_he_su_cnt;
1545 data[ei++] = mib->dl_he_ext_su_cnt;
1546 data[ei++] = mib->dl_he_2ru_cnt;
1547 data[ei++] = mib->dl_he_2mu_cnt;
1548 data[ei++] = mib->dl_he_3ru_cnt;
1549 data[ei++] = mib->dl_he_3mu_cnt;
1550 data[ei++] = mib->dl_he_4ru_cnt;
1551 data[ei++] = mib->dl_he_4mu_cnt;
1552 data[ei++] = mib->dl_he_5to8ru_cnt;
1553 data[ei++] = mib->dl_he_9to16ru_cnt;
1554 data[ei++] = mib->dl_he_gtr16ru_cnt;
1555
1556 data[ei++] = mib->ul_hetrig_su_cnt;
1557 data[ei++] = mib->ul_hetrig_2ru_cnt;
1558 data[ei++] = mib->ul_hetrig_3ru_cnt;
1559 data[ei++] = mib->ul_hetrig_4ru_cnt;
1560 data[ei++] = mib->ul_hetrig_5to8ru_cnt;
1561 data[ei++] = mib->ul_hetrig_9to16ru_cnt;
1562 data[ei++] = mib->ul_hetrig_gtr16ru_cnt;
1563 data[ei++] = mib->ul_hetrig_2mu_cnt;
1564 data[ei++] = mib->ul_hetrig_3mu_cnt;
1565 data[ei++] = mib->ul_hetrig_4mu_cnt;
1566
1567 /* Add values for all stations owned by this vif */
1568 wi.initial_stat_idx = ei;
1569 ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1570
1571 mutex_unlock(&dev->mt76.mutex);
1572
1573 if (wi.sta_count == 0)
1574 return;
1575
1576 ei += wi.worker_stat_count;
1577
1578 mt76_ethtool_page_pool_stats(&dev->mt76, &data[ei], &ei);
1579
1580 stats_size = MT7915_SSTATS_LEN + page_pool_ethtool_stats_get_count();
1581 if (ei != stats_size)
1582 dev_err(dev->mt76.dev, "ei: %d size: %d", ei, stats_size);
1583 }
1584
1585 static void
mt7915_twt_teardown_request(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u8 flowid)1586 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1587 struct ieee80211_sta *sta,
1588 u8 flowid)
1589 {
1590 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1591 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1592
1593 mutex_lock(&dev->mt76.mutex);
1594 mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1595 mutex_unlock(&dev->mt76.mutex);
1596 }
1597
1598 static int
mt7915_set_frag_threshold(struct ieee80211_hw * hw,u32 val)1599 mt7915_set_frag_threshold(struct ieee80211_hw *hw, u32 val)
1600 {
1601 return 0;
1602 }
1603
1604 static int
mt7915_set_radar_background(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef)1605 mt7915_set_radar_background(struct ieee80211_hw *hw,
1606 struct cfg80211_chan_def *chandef)
1607 {
1608 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1609 struct mt7915_dev *dev = phy->dev;
1610 int ret = -EINVAL;
1611 bool running;
1612
1613 mutex_lock(&dev->mt76.mutex);
1614
1615 if (dev->mt76.region == NL80211_DFS_UNSET)
1616 goto out;
1617
1618 if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1619 /* rdd2 is already locked */
1620 ret = -EBUSY;
1621 goto out;
1622 }
1623
1624 /* rdd2 already configured on a radar channel */
1625 running = dev->rdd2_phy &&
1626 cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1627 !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1628
1629 if (!chandef || running ||
1630 !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1631 ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1632 if (ret)
1633 goto out;
1634
1635 if (!running)
1636 goto update_phy;
1637 }
1638
1639 ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1640 if (ret)
1641 goto out;
1642
1643 update_phy:
1644 dev->rdd2_phy = chandef ? phy : NULL;
1645 if (chandef)
1646 dev->rdd2_chandef = *chandef;
1647 out:
1648 mutex_unlock(&dev->mt76.mutex);
1649
1650 return ret;
1651 }
1652
1653 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1654 static int
mt7915_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)1655 mt7915_net_fill_forward_path(struct ieee80211_hw *hw,
1656 struct ieee80211_vif *vif,
1657 struct ieee80211_sta *sta,
1658 struct net_device_path_ctx *ctx,
1659 struct net_device_path *path)
1660 {
1661 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1662 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1663 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1664 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1665 struct mtk_wed_device *wed = &dev->mt76.mmio.wed;
1666
1667 if (!mtk_wed_device_active(wed))
1668 return -ENODEV;
1669
1670 if (msta->wcid.idx > 0xff)
1671 return -EIO;
1672
1673 path->type = DEV_PATH_MTK_WDMA;
1674 path->dev = ctx->dev;
1675 path->mtk_wdma.wdma_idx = wed->wdma_idx;
1676 path->mtk_wdma.bss = mvif->mt76.idx;
1677 path->mtk_wdma.wcid = is_mt7915(&dev->mt76) ? msta->wcid.idx : 0x3ff;
1678 path->mtk_wdma.queue = phy != &dev->phy;
1679
1680 ctx->dev = NULL;
1681
1682 return 0;
1683 }
1684 #endif
1685
1686 static void
mt7915_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)1687 mt7915_reconfig_complete(struct ieee80211_hw *hw,
1688 enum ieee80211_reconfig_type reconfig_type)
1689 {
1690 struct mt7915_phy *phy = mt7915_hw_phy(hw);
1691
1692 ieee80211_wake_queues(hw);
1693 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1694 MT7915_WATCHDOG_TIME);
1695 }
1696
1697 const struct ieee80211_ops mt7915_ops = {
1698 .add_chanctx = ieee80211_emulate_add_chanctx,
1699 .remove_chanctx = ieee80211_emulate_remove_chanctx,
1700 .change_chanctx = ieee80211_emulate_change_chanctx,
1701 .switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
1702 .tx = mt7915_tx,
1703 .start = mt7915_start,
1704 .stop = mt7915_stop,
1705 .add_interface = mt7915_add_interface,
1706 .remove_interface = mt7915_remove_interface,
1707 .config = mt7915_config,
1708 .conf_tx = mt7915_conf_tx,
1709 .configure_filter = mt7915_configure_filter,
1710 .bss_info_changed = mt7915_bss_info_changed,
1711 .start_ap = mt7915_start_ap,
1712 .stop_ap = mt7915_stop_ap,
1713 .sta_state = mt76_sta_state,
1714 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1715 .sta_rc_update = mt7915_sta_rc_update,
1716 .set_key = mt7915_set_key,
1717 .ampdu_action = mt7915_ampdu_action,
1718 .set_rts_threshold = mt7915_set_rts_threshold,
1719 .wake_tx_queue = mt76_wake_tx_queue,
1720 .sw_scan_start = mt76_sw_scan,
1721 .sw_scan_complete = mt76_sw_scan_complete,
1722 .release_buffered_frames = mt76_release_buffered_frames,
1723 .get_txpower = mt76_get_txpower,
1724 .set_sar_specs = mt7915_set_sar_specs,
1725 .channel_switch_beacon = mt7915_channel_switch_beacon,
1726 .get_stats = mt7915_get_stats,
1727 .get_et_sset_count = mt7915_get_et_sset_count,
1728 .get_et_stats = mt7915_get_et_stats,
1729 .get_et_strings = mt7915_get_et_strings,
1730 .get_tsf = mt7915_get_tsf,
1731 .set_tsf = mt7915_set_tsf,
1732 .offset_tsf = mt7915_offset_tsf,
1733 .get_survey = mt76_get_survey,
1734 .get_antenna = mt76_get_antenna,
1735 .set_antenna = mt7915_set_antenna,
1736 .set_bitrate_mask = mt7915_set_bitrate_mask,
1737 .set_coverage_class = mt7915_set_coverage_class,
1738 .sta_statistics = mt7915_sta_statistics,
1739 .sta_set_txpwr = mt7915_sta_set_txpwr,
1740 .sta_set_4addr = mt7915_sta_set_4addr,
1741 .sta_set_decap_offload = mt7915_sta_set_decap_offload,
1742 .add_twt_setup = mt7915_mac_add_twt_setup,
1743 .twt_teardown_request = mt7915_twt_teardown_request,
1744 .set_frag_threshold = mt7915_set_frag_threshold,
1745 CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1746 CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1747 #ifdef CONFIG_MAC80211_DEBUGFS
1748 .sta_add_debugfs = mt7915_sta_add_debugfs,
1749 #endif
1750 .set_radar_background = mt7915_set_radar_background,
1751 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
1752 .net_fill_forward_path = mt7915_net_fill_forward_path,
1753 .net_setup_tc = mt76_wed_net_setup_tc,
1754 #endif
1755 .reconfig_complete = mt7915_reconfig_complete,
1756 };
1757