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