OSDN Git Service

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