Linux-libre 5.7.6-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / mediatek / mt76 / mt7615 / main.c
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Roy Luo <royluo@google.com>
5  *         Ryder Lee <ryder.lee@mediatek.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  */
8
9 #include <linux/etherdevice.h>
10 #include <linux/platform_device.h>
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include "mt7615.h"
14 #include "mcu.h"
15
16 static bool mt7615_dev_running(struct mt7615_dev *dev)
17 {
18         struct mt7615_phy *phy;
19
20         if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
21                 return true;
22
23         phy = mt7615_ext_phy(dev);
24
25         return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
26 }
27
28 static int mt7615_start(struct ieee80211_hw *hw)
29 {
30         struct mt7615_dev *dev = mt7615_hw_dev(hw);
31         struct mt7615_phy *phy = mt7615_hw_phy(hw);
32         bool running;
33
34         if (!mt7615_wait_for_mcu_init(dev))
35                 return -EIO;
36
37         mutex_lock(&dev->mt76.mutex);
38
39         running = mt7615_dev_running(dev);
40
41         if (!running) {
42                 mt7615_mcu_set_pm(dev, 0, 0);
43                 mt7615_mcu_set_mac_enable(dev, 0, true);
44                 mt7615_mac_enable_nf(dev, 0);
45         }
46
47         if (phy != &dev->phy) {
48                 mt7615_mcu_set_pm(dev, 1, 0);
49                 mt7615_mcu_set_mac_enable(dev, 1, true);
50                 mt7615_mac_enable_nf(dev, 1);
51         }
52
53         mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
54
55         set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
56
57         if (running)
58                 goto out;
59
60         mt7615_mac_reset_counters(dev);
61
62         ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
63                                      MT7615_WATCHDOG_TIME);
64
65 out:
66         mutex_unlock(&dev->mt76.mutex);
67
68         return 0;
69 }
70
71 static void mt7615_stop(struct ieee80211_hw *hw)
72 {
73         struct mt7615_dev *dev = mt7615_hw_dev(hw);
74         struct mt7615_phy *phy = mt7615_hw_phy(hw);
75
76         mutex_lock(&dev->mt76.mutex);
77
78         clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
79
80         if (phy != &dev->phy) {
81                 mt7615_mcu_set_pm(dev, 1, 1);
82                 mt7615_mcu_set_mac_enable(dev, 1, false);
83         }
84
85         if (!mt7615_dev_running(dev)) {
86                 cancel_delayed_work_sync(&dev->mt76.mac_work);
87
88                 mt7615_mcu_set_pm(dev, 0, 1);
89                 mt7615_mcu_set_mac_enable(dev, 0, false);
90         }
91
92         mutex_unlock(&dev->mt76.mutex);
93 }
94
95 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
96 {
97         int i;
98
99         switch (type) {
100         case NL80211_IFTYPE_MONITOR:
101         case NL80211_IFTYPE_AP:
102         case NL80211_IFTYPE_MESH_POINT:
103         case NL80211_IFTYPE_ADHOC:
104                 /* ap use hw bssid 0 and ext bssid */
105                 if (~mask & BIT(HW_BSSID_0))
106                         return HW_BSSID_0;
107
108                 for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
109                         if (~mask & BIT(i))
110                                 return i;
111
112                 break;
113         case NL80211_IFTYPE_STATION:
114                 /* sta use hw bssid other than 0 */
115                 for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
116                         if (~mask & BIT(i))
117                                 return i;
118
119                 break;
120         default:
121                 WARN_ON(1);
122                 break;
123         }
124
125         return -1;
126 }
127
128 static int mt7615_add_interface(struct ieee80211_hw *hw,
129                                 struct ieee80211_vif *vif)
130 {
131         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
132         struct mt7615_dev *dev = mt7615_hw_dev(hw);
133         struct mt7615_phy *phy = mt7615_hw_phy(hw);
134         struct mt76_txq *mtxq;
135         bool ext_phy = phy != &dev->phy;
136         int idx, ret = 0;
137
138         mutex_lock(&dev->mt76.mutex);
139
140         mvif->idx = ffs(~dev->vif_mask) - 1;
141         if (mvif->idx >= MT7615_MAX_INTERFACES) {
142                 ret = -ENOSPC;
143                 goto out;
144         }
145
146         idx = get_omac_idx(vif->type, dev->omac_mask);
147         if (idx < 0) {
148                 ret = -ENOSPC;
149                 goto out;
150         }
151         mvif->omac_idx = idx;
152
153         mvif->band_idx = ext_phy;
154         if (mt7615_ext_phy(dev))
155                 mvif->wmm_idx = ext_phy * (MT7615_MAX_WMM_SETS / 2) +
156                                 mvif->idx % (MT7615_MAX_WMM_SETS / 2);
157         else
158                 mvif->wmm_idx = mvif->idx % MT7615_MAX_WMM_SETS;
159
160         ret = mt7615_mcu_add_dev_info(dev, vif, true);
161         if (ret)
162                 goto out;
163
164         dev->vif_mask |= BIT(mvif->idx);
165         dev->omac_mask |= BIT(mvif->omac_idx);
166         phy->omac_mask |= BIT(mvif->omac_idx);
167
168         mt7615_mcu_set_dbdc(dev);
169
170         idx = MT7615_WTBL_RESERVED - mvif->idx;
171
172         INIT_LIST_HEAD(&mvif->sta.poll_list);
173         mvif->sta.wcid.idx = idx;
174         mvif->sta.wcid.ext_phy = mvif->band_idx;
175         mvif->sta.wcid.hw_key_idx = -1;
176         mt7615_mac_wtbl_update(dev, idx,
177                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
178
179         rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
180         if (vif->txq) {
181                 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
182                 mtxq->wcid = &mvif->sta.wcid;
183                 mt76_txq_init(&dev->mt76, vif->txq);
184         }
185
186 out:
187         mutex_unlock(&dev->mt76.mutex);
188
189         return ret;
190 }
191
192 static void mt7615_remove_interface(struct ieee80211_hw *hw,
193                                     struct ieee80211_vif *vif)
194 {
195         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
196         struct mt7615_sta *msta = &mvif->sta;
197         struct mt7615_dev *dev = mt7615_hw_dev(hw);
198         struct mt7615_phy *phy = mt7615_hw_phy(hw);
199         int idx = msta->wcid.idx;
200
201         /* TODO: disable beacon for the bss */
202
203         mt7615_mcu_add_dev_info(dev, vif, false);
204
205         rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
206         if (vif->txq)
207                 mt76_txq_remove(&dev->mt76, vif->txq);
208
209         mutex_lock(&dev->mt76.mutex);
210         dev->vif_mask &= ~BIT(mvif->idx);
211         dev->omac_mask &= ~BIT(mvif->omac_idx);
212         phy->omac_mask &= ~BIT(mvif->omac_idx);
213         mutex_unlock(&dev->mt76.mutex);
214
215         spin_lock_bh(&dev->sta_poll_lock);
216         if (!list_empty(&msta->poll_list))
217                 list_del_init(&msta->poll_list);
218         spin_unlock_bh(&dev->sta_poll_lock);
219 }
220
221 static void mt7615_init_dfs_state(struct mt7615_phy *phy)
222 {
223         struct mt76_phy *mphy = phy->mt76;
224         struct ieee80211_hw *hw = mphy->hw;
225         struct cfg80211_chan_def *chandef = &hw->conf.chandef;
226
227         if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
228                 return;
229
230         if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
231                 return;
232
233         if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
234             mphy->chandef.width == chandef->width)
235                 return;
236
237         phy->dfs_state = -1;
238 }
239
240 static int mt7615_set_channel(struct mt7615_phy *phy)
241 {
242         struct mt7615_dev *dev = phy->dev;
243         bool ext_phy = phy != &dev->phy;
244         int ret;
245
246         cancel_delayed_work_sync(&dev->mt76.mac_work);
247
248         mutex_lock(&dev->mt76.mutex);
249         set_bit(MT76_RESET, &phy->mt76->state);
250
251         mt7615_init_dfs_state(phy);
252         mt76_set_channel(phy->mt76);
253
254         ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
255         if (ret)
256                 goto out;
257
258         mt7615_mac_set_timing(phy);
259         ret = mt7615_dfs_init_radar_detector(phy);
260         mt7615_mac_cca_stats_reset(phy);
261         mt7615_mcu_set_sku_en(phy, true);
262
263         mt7615_mac_reset_counters(dev);
264         phy->noise = 0;
265         phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
266
267 out:
268         clear_bit(MT76_RESET, &phy->mt76->state);
269         mutex_unlock(&dev->mt76.mutex);
270
271         mt76_txq_schedule_all(phy->mt76);
272         ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
273                                      MT7615_WATCHDOG_TIME);
274         return ret;
275 }
276
277 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
278                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
279                           struct ieee80211_key_conf *key)
280 {
281         struct mt7615_dev *dev = mt7615_hw_dev(hw);
282         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
283         struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
284                                   &mvif->sta;
285         struct mt76_wcid *wcid = &msta->wcid;
286         int idx = key->keyidx;
287
288         /* The hardware does not support per-STA RX GTK, fallback
289          * to software mode for these.
290          */
291         if ((vif->type == NL80211_IFTYPE_ADHOC ||
292              vif->type == NL80211_IFTYPE_MESH_POINT) &&
293             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
294              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
295             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
296                 return -EOPNOTSUPP;
297
298         /* fall back to sw encryption for unsupported ciphers */
299         switch (key->cipher) {
300         case WLAN_CIPHER_SUITE_AES_CMAC:
301                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
302                 break;
303         case WLAN_CIPHER_SUITE_WEP40:
304         case WLAN_CIPHER_SUITE_WEP104:
305         case WLAN_CIPHER_SUITE_TKIP:
306         case WLAN_CIPHER_SUITE_CCMP:
307         case WLAN_CIPHER_SUITE_CCMP_256:
308         case WLAN_CIPHER_SUITE_GCMP:
309         case WLAN_CIPHER_SUITE_GCMP_256:
310         case WLAN_CIPHER_SUITE_SMS4:
311                 break;
312         default:
313                 return -EOPNOTSUPP;
314         }
315
316         if (cmd == SET_KEY) {
317                 key->hw_key_idx = wcid->idx;
318                 wcid->hw_key_idx = idx;
319         } else if (idx == wcid->hw_key_idx) {
320                 wcid->hw_key_idx = -1;
321         }
322         mt76_wcid_key_setup(&dev->mt76, wcid,
323                             cmd == SET_KEY ? key : NULL);
324
325         return mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
326 }
327
328 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
329 {
330         struct mt7615_dev *dev = mt7615_hw_dev(hw);
331         struct mt7615_phy *phy = mt7615_hw_phy(hw);
332         bool band = phy != &dev->phy;
333         int ret = 0;
334
335         if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
336                        IEEE80211_CONF_CHANGE_POWER)) {
337                 ieee80211_stop_queues(hw);
338                 ret = mt7615_set_channel(phy);
339                 ieee80211_wake_queues(hw);
340         }
341
342         mutex_lock(&dev->mt76.mutex);
343
344         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
345                 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
346                         phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
347                 else
348                         phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
349
350                 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
351         }
352
353         mutex_unlock(&dev->mt76.mutex);
354
355         return ret;
356 }
357
358 static int
359 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
360                const struct ieee80211_tx_queue_params *params)
361 {
362         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
363         struct mt7615_dev *dev = mt7615_hw_dev(hw);
364
365         queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
366
367         return mt7615_mcu_set_wmm(dev, queue, params);
368 }
369
370 static void mt7615_configure_filter(struct ieee80211_hw *hw,
371                                     unsigned int changed_flags,
372                                     unsigned int *total_flags,
373                                     u64 multicast)
374 {
375         struct mt7615_dev *dev = mt7615_hw_dev(hw);
376         struct mt7615_phy *phy = mt7615_hw_phy(hw);
377         bool band = phy != &dev->phy;
378
379         u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
380                         MT_WF_RFCR1_DROP_BF_POLL |
381                         MT_WF_RFCR1_DROP_BA |
382                         MT_WF_RFCR1_DROP_CFEND |
383                         MT_WF_RFCR1_DROP_CFACK;
384         u32 flags = 0;
385
386 #define MT76_FILTER(_flag, _hw) do { \
387                 flags |= *total_flags & FIF_##_flag;                    \
388                 phy->rxfilter &= ~(_hw);                                \
389                 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);        \
390         } while (0)
391
392         phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
393                            MT_WF_RFCR_DROP_OTHER_BEACON |
394                            MT_WF_RFCR_DROP_FRAME_REPORT |
395                            MT_WF_RFCR_DROP_PROBEREQ |
396                            MT_WF_RFCR_DROP_MCAST_FILTERED |
397                            MT_WF_RFCR_DROP_MCAST |
398                            MT_WF_RFCR_DROP_BCAST |
399                            MT_WF_RFCR_DROP_DUPLICATE |
400                            MT_WF_RFCR_DROP_A2_BSSID |
401                            MT_WF_RFCR_DROP_UNWANTED_CTL |
402                            MT_WF_RFCR_DROP_STBC_MULTI);
403
404         MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
405                                MT_WF_RFCR_DROP_A3_MAC |
406                                MT_WF_RFCR_DROP_A3_BSSID);
407
408         MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
409
410         MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
411                              MT_WF_RFCR_DROP_RTS |
412                              MT_WF_RFCR_DROP_CTL_RSV |
413                              MT_WF_RFCR_DROP_NDPA);
414
415         *total_flags = flags;
416         mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
417
418         if (*total_flags & FIF_CONTROL)
419                 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
420         else
421                 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
422 }
423
424 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
425                                     struct ieee80211_vif *vif,
426                                     struct ieee80211_bss_conf *info,
427                                     u32 changed)
428 {
429         struct mt7615_dev *dev = mt7615_hw_dev(hw);
430
431         mutex_lock(&dev->mt76.mutex);
432
433         if (changed & BSS_CHANGED_ASSOC)
434                 mt7615_mcu_add_bss_info(dev, vif, info->assoc);
435
436         if (changed & BSS_CHANGED_ERP_SLOT) {
437                 int slottime = info->use_short_slot ? 9 : 20;
438                 struct mt7615_phy *phy = mt7615_hw_phy(hw);
439
440                 if (slottime != phy->slottime) {
441                         phy->slottime = slottime;
442                         mt7615_mac_set_timing(phy);
443                 }
444         }
445
446         if (changed & BSS_CHANGED_BEACON_ENABLED) {
447                 mt7615_mcu_add_bss_info(dev, vif, info->enable_beacon);
448                 mt7615_mcu_sta_add(dev, vif, NULL, info->enable_beacon);
449         }
450
451         if (changed & (BSS_CHANGED_BEACON |
452                        BSS_CHANGED_BEACON_ENABLED))
453                 mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
454
455         mutex_unlock(&dev->mt76.mutex);
456 }
457
458 static void
459 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
460                              struct ieee80211_vif *vif,
461                              struct cfg80211_chan_def *chandef)
462 {
463         struct mt7615_dev *dev = mt7615_hw_dev(hw);
464
465         mutex_lock(&dev->mt76.mutex);
466         mt7615_mcu_add_beacon(dev, hw, vif, true);
467         mutex_unlock(&dev->mt76.mutex);
468 }
469
470 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
471                        struct ieee80211_sta *sta)
472 {
473         struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
474         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
475         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
476         int idx;
477
478         idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
479         if (idx < 0)
480                 return -ENOSPC;
481
482         INIT_LIST_HEAD(&msta->poll_list);
483         msta->vif = mvif;
484         msta->wcid.sta = 1;
485         msta->wcid.idx = idx;
486         msta->wcid.ext_phy = mvif->band_idx;
487
488         mt7615_mac_wtbl_update(dev, idx,
489                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
490
491         mt7615_mcu_sta_add(dev, vif, sta, true);
492
493         return 0;
494 }
495
496 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
497                            struct ieee80211_sta *sta)
498 {
499         struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
500         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
501
502         mt7615_mcu_sta_add(dev, vif, sta, false);
503         mt7615_mac_wtbl_update(dev, msta->wcid.idx,
504                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
505
506         spin_lock_bh(&dev->sta_poll_lock);
507         if (!list_empty(&msta->poll_list))
508                 list_del_init(&msta->poll_list);
509         spin_unlock_bh(&dev->sta_poll_lock);
510 }
511
512 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
513                                        struct ieee80211_vif *vif,
514                                        struct ieee80211_sta *sta)
515 {
516         struct mt7615_dev *dev = mt7615_hw_dev(hw);
517         struct mt7615_phy *phy = mt7615_hw_phy(hw);
518         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
519         struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
520         int i;
521
522         spin_lock_bh(&dev->mt76.lock);
523         for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
524                 msta->rates[i].idx = sta_rates->rate[i].idx;
525                 msta->rates[i].count = sta_rates->rate[i].count;
526                 msta->rates[i].flags = sta_rates->rate[i].flags;
527
528                 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
529                         break;
530         }
531         msta->n_rates = i;
532         mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
533         msta->rate_probe = false;
534         spin_unlock_bh(&dev->mt76.lock);
535 }
536
537 static void mt7615_tx(struct ieee80211_hw *hw,
538                       struct ieee80211_tx_control *control,
539                       struct sk_buff *skb)
540 {
541         struct mt7615_dev *dev = mt7615_hw_dev(hw);
542         struct mt76_phy *mphy = hw->priv;
543         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
544         struct ieee80211_vif *vif = info->control.vif;
545         struct mt76_wcid *wcid = &dev->mt76.global_wcid;
546
547         if (control->sta) {
548                 struct mt7615_sta *sta;
549
550                 sta = (struct mt7615_sta *)control->sta->drv_priv;
551                 wcid = &sta->wcid;
552         }
553
554         if (vif && !control->sta) {
555                 struct mt7615_vif *mvif;
556
557                 mvif = (struct mt7615_vif *)vif->drv_priv;
558                 wcid = &mvif->sta.wcid;
559         }
560
561         mt76_tx(mphy, control->sta, wcid, skb);
562 }
563
564 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
565 {
566         struct mt7615_dev *dev = mt7615_hw_dev(hw);
567         struct mt7615_phy *phy = mt7615_hw_phy(hw);
568
569         mutex_lock(&dev->mt76.mutex);
570         mt7615_mcu_set_rts_thresh(phy, val);
571         mutex_unlock(&dev->mt76.mutex);
572
573         return 0;
574 }
575
576 static int
577 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
578                     struct ieee80211_ampdu_params *params)
579 {
580         enum ieee80211_ampdu_mlme_action action = params->action;
581         struct mt7615_dev *dev = mt7615_hw_dev(hw);
582         struct ieee80211_sta *sta = params->sta;
583         struct ieee80211_txq *txq = sta->txq[params->tid];
584         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
585         u16 tid = params->tid;
586         u16 ssn = params->ssn;
587         struct mt76_txq *mtxq;
588         int ret = 0;
589
590         if (!txq)
591                 return -EINVAL;
592
593         mtxq = (struct mt76_txq *)txq->drv_priv;
594
595         mutex_lock(&dev->mt76.mutex);
596         switch (action) {
597         case IEEE80211_AMPDU_RX_START:
598                 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
599                                    params->buf_size);
600                 mt7615_mcu_add_rx_ba(dev, params, true);
601                 break;
602         case IEEE80211_AMPDU_RX_STOP:
603                 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
604                 mt7615_mcu_add_rx_ba(dev, params, false);
605                 break;
606         case IEEE80211_AMPDU_TX_OPERATIONAL:
607                 mtxq->aggr = true;
608                 mtxq->send_bar = false;
609                 mt7615_mcu_add_tx_ba(dev, params, true);
610                 break;
611         case IEEE80211_AMPDU_TX_STOP_FLUSH:
612         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
613                 mtxq->aggr = false;
614                 mt7615_mcu_add_tx_ba(dev, params, false);
615                 break;
616         case IEEE80211_AMPDU_TX_START:
617                 mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
618                 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
619                 break;
620         case IEEE80211_AMPDU_TX_STOP_CONT:
621                 mtxq->aggr = false;
622                 mt7615_mcu_add_tx_ba(dev, params, false);
623                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
624                 break;
625         }
626         mutex_unlock(&dev->mt76.mutex);
627
628         return ret;
629 }
630
631 static int
632 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
633                struct ieee80211_sta *sta)
634 {
635     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
636                           IEEE80211_STA_NONE);
637 }
638
639 static int
640 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
641                   struct ieee80211_sta *sta)
642 {
643     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
644                           IEEE80211_STA_NOTEXIST);
645 }
646
647 static int
648 mt7615_get_stats(struct ieee80211_hw *hw,
649                  struct ieee80211_low_level_stats *stats)
650 {
651         struct mt7615_phy *phy = mt7615_hw_phy(hw);
652         struct mib_stats *mib = &phy->mib;
653
654         stats->dot11RTSSuccessCount = mib->rts_cnt;
655         stats->dot11RTSFailureCount = mib->rts_retries_cnt;
656         stats->dot11FCSErrorCount = mib->fcs_err_cnt;
657         stats->dot11ACKFailureCount = mib->ack_fail_cnt;
658
659         return 0;
660 }
661
662 static u64
663 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
664 {
665         struct mt7615_dev *dev = mt7615_hw_dev(hw);
666         union {
667                 u64 t64;
668                 u32 t32[2];
669         } tsf;
670
671         mutex_lock(&dev->mt76.mutex);
672
673         mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_MODE); /* TSF read */
674         tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
675         tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
676
677         mutex_unlock(&dev->mt76.mutex);
678
679         return tsf.t64;
680 }
681
682 static void
683 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
684 {
685         struct mt7615_phy *phy = mt7615_hw_phy(hw);
686
687         phy->coverage_class = max_t(s16, coverage_class, 0);
688         mt7615_mac_set_timing(phy);
689 }
690
691 static int
692 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
693 {
694         struct mt7615_dev *dev = mt7615_hw_dev(hw);
695         struct mt7615_phy *phy = mt7615_hw_phy(hw);
696         int max_nss = hweight8(hw->wiphy->available_antennas_tx);
697         bool ext_phy = phy != &dev->phy;
698
699         if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
700                 return -EINVAL;
701
702         if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
703                 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
704
705         mutex_lock(&dev->mt76.mutex);
706
707         phy->mt76->antenna_mask = tx_ant;
708         if (ext_phy) {
709                 if (dev->chainmask == 0xf)
710                         tx_ant <<= 2;
711                 else
712                         tx_ant <<= 1;
713         }
714         phy->chainmask = tx_ant;
715
716         mt76_set_stream_caps(&dev->mt76, true);
717
718         mutex_unlock(&dev->mt76.mutex);
719
720         return 0;
721 }
722
723 const struct ieee80211_ops mt7615_ops = {
724         .tx = mt7615_tx,
725         .start = mt7615_start,
726         .stop = mt7615_stop,
727         .add_interface = mt7615_add_interface,
728         .remove_interface = mt7615_remove_interface,
729         .config = mt7615_config,
730         .conf_tx = mt7615_conf_tx,
731         .configure_filter = mt7615_configure_filter,
732         .bss_info_changed = mt7615_bss_info_changed,
733         .sta_add = mt7615_sta_add,
734         .sta_remove = mt7615_sta_remove,
735         .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
736         .set_key = mt7615_set_key,
737         .ampdu_action = mt7615_ampdu_action,
738         .set_rts_threshold = mt7615_set_rts_threshold,
739         .wake_tx_queue = mt76_wake_tx_queue,
740         .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
741         .sw_scan_start = mt76_sw_scan,
742         .sw_scan_complete = mt76_sw_scan_complete,
743         .release_buffered_frames = mt76_release_buffered_frames,
744         .get_txpower = mt76_get_txpower,
745         .channel_switch_beacon = mt7615_channel_switch_beacon,
746         .get_stats = mt7615_get_stats,
747         .get_tsf = mt7615_get_tsf,
748         .get_survey = mt76_get_survey,
749         .get_antenna = mt76_get_antenna,
750         .set_antenna = mt7615_set_antenna,
751         .set_coverage_class = mt7615_set_coverage_class,
752 };
753
754 static int __init mt7615_init(void)
755 {
756         int ret;
757
758         ret = pci_register_driver(&mt7615_pci_driver);
759         if (ret)
760                 return ret;
761
762         if (IS_ENABLED(CONFIG_MT7622_WMAC)) {
763                 ret = platform_driver_register(&mt7622_wmac_driver);
764                 if (ret)
765                         pci_unregister_driver(&mt7615_pci_driver);
766         }
767
768         return ret;
769 }
770
771 static void __exit mt7615_exit(void)
772 {
773         if (IS_ENABLED(CONFIG_MT7622_WMAC))
774                 platform_driver_unregister(&mt7622_wmac_driver);
775         pci_unregister_driver(&mt7615_pci_driver);
776 }
777
778 module_init(mt7615_init);
779 module_exit(mt7615_exit);
780 MODULE_LICENSE("Dual BSD/GPL");