interconnect: qcom: icc-rpm: Fix peak rate calculation
[linux-2.6-microblaze.git] / drivers / net / wireless / mediatek / mt76 / mt7925 / main.c
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 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 <linux/ctype.h>
9 #include <net/ipv6.h>
10 #include "mt7925.h"
11 #include "mcu.h"
12 #include "mac.h"
13
14 static void
15 mt7925_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band,
16                     struct ieee80211_sband_iftype_data *data,
17                         enum nl80211_iftype iftype)
18 {
19         struct ieee80211_sta_he_cap *he_cap = &data->he_cap;
20         struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
21         struct ieee80211_he_mcs_nss_supp *he_mcs = &he_cap->he_mcs_nss_supp;
22         int i, nss = hweight8(phy->mt76->antenna_mask);
23         u16 mcs_map = 0;
24
25         for (i = 0; i < 8; i++) {
26                 if (i < nss)
27                         mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
28                 else
29                         mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
30         }
31
32         he_cap->has_he = true;
33
34         he_cap_elem->mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE;
35         he_cap_elem->mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
36                                        IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
37         he_cap_elem->mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
38
39         if (band == NL80211_BAND_2GHZ)
40                 he_cap_elem->phy_cap_info[0] =
41                         IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
42         else
43                 he_cap_elem->phy_cap_info[0] =
44                         IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
45                         IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
46
47         he_cap_elem->phy_cap_info[1] =
48                 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
49         he_cap_elem->phy_cap_info[2] =
50                 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
51                 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
52                 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
53                 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
54                 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
55
56         switch (i) {
57         case NL80211_IFTYPE_AP:
58                 he_cap_elem->mac_cap_info[2] |=
59                         IEEE80211_HE_MAC_CAP2_BSR;
60                 he_cap_elem->mac_cap_info[4] |=
61                         IEEE80211_HE_MAC_CAP4_BQR;
62                 he_cap_elem->mac_cap_info[5] |=
63                         IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX;
64                 he_cap_elem->phy_cap_info[3] |=
65                         IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
66                         IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
67                 he_cap_elem->phy_cap_info[6] |=
68                         IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
69                         IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
70                 he_cap_elem->phy_cap_info[9] |=
71                         IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
72                         IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
73                 break;
74         case NL80211_IFTYPE_STATION:
75                 he_cap_elem->mac_cap_info[1] |=
76                         IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
77
78                 if (band == NL80211_BAND_2GHZ)
79                         he_cap_elem->phy_cap_info[0] |=
80                                 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
81                 else
82                         he_cap_elem->phy_cap_info[0] |=
83                                 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
84
85                 he_cap_elem->phy_cap_info[1] |=
86                         IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
87                         IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
88                 he_cap_elem->phy_cap_info[3] |=
89                         IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
90                         IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
91                 he_cap_elem->phy_cap_info[4] |=
92                         IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
93                         IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4 |
94                         IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4;
95                 he_cap_elem->phy_cap_info[5] |=
96                         IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
97                         IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
98                 he_cap_elem->phy_cap_info[6] |=
99                         IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
100                         IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
101                         IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
102                         IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
103                         IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
104                 he_cap_elem->phy_cap_info[7] |=
105                         IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
106                         IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
107                 he_cap_elem->phy_cap_info[8] |=
108                         IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
109                         IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
110                         IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU |
111                         IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
112                 he_cap_elem->phy_cap_info[9] |=
113                         IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
114                         IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
115                         IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
116                         IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
117                         IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
118                         IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
119                 break;
120         default:
121                 break;
122         }
123
124         he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
125         he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
126         he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
127         he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
128
129         memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
130
131         if (he_cap_elem->phy_cap_info[6] &
132             IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
133                 mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss);
134         } else {
135                 he_cap_elem->phy_cap_info[9] |=
136                         u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
137                                        IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
138         }
139
140         if (band == NL80211_BAND_6GHZ) {
141                 u16 cap = IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS |
142                           IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
143
144                 cap |= u16_encode_bits(IEEE80211_HT_MPDU_DENSITY_0_5,
145                                        IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) |
146                        u16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K,
147                                        IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) |
148                        u16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454,
149                                        IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
150
151                 data->he_6ghz_capa.capa = cpu_to_le16(cap);
152         }
153 }
154
155 static void
156 mt7925_init_eht_caps(struct mt792x_phy *phy, enum nl80211_band band,
157                      struct ieee80211_sband_iftype_data *data,
158                      enum nl80211_iftype iftype)
159 {
160         struct ieee80211_sta_eht_cap *eht_cap = &data->eht_cap;
161         struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
162         struct ieee80211_eht_mcs_nss_supp *eht_nss = &eht_cap->eht_mcs_nss_supp;
163         enum nl80211_chan_width width = phy->mt76->chandef.width;
164         int nss = hweight8(phy->mt76->antenna_mask);
165         int sts = hweight16(phy->mt76->chainmask);
166         u8 val;
167
168         if (!phy->dev->has_eht)
169                 return;
170
171         eht_cap->has_eht = true;
172
173         eht_cap_elem->mac_cap_info[0] =
174                 IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS |
175                 IEEE80211_EHT_MAC_CAP0_OM_CONTROL;
176
177         eht_cap_elem->phy_cap_info[0] =
178                 IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI |
179                 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER |
180                 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE;
181
182         eht_cap_elem->phy_cap_info[0] |=
183                 u8_encode_bits(u8_get_bits(sts - 1, BIT(0)),
184                                IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK);
185
186         eht_cap_elem->phy_cap_info[1] =
187                 u8_encode_bits(u8_get_bits(sts - 1, GENMASK(2, 1)),
188                                IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK) |
189                 u8_encode_bits(sts - 1,
190                                IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK);
191
192         eht_cap_elem->phy_cap_info[2] =
193                 u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK) |
194                 u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK);
195
196         eht_cap_elem->phy_cap_info[3] =
197                 IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK |
198                 IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
199                 IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK |
200                 IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
201                 IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK |
202                 IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
203                 IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
204
205         eht_cap_elem->phy_cap_info[4] =
206                 u8_encode_bits(min_t(int, sts - 1, 2),
207                                IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK);
208
209         eht_cap_elem->phy_cap_info[5] =
210                 IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
211                 u8_encode_bits(IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US,
212                                IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK) |
213                 u8_encode_bits(u8_get_bits(0x11, GENMASK(1, 0)),
214                                IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK);
215
216         val = width == NL80211_CHAN_WIDTH_160 ? 0x7 :
217               width == NL80211_CHAN_WIDTH_80 ? 0x3 : 0x1;
218         eht_cap_elem->phy_cap_info[6] =
219                 u8_encode_bits(u8_get_bits(0x11, GENMASK(4, 2)),
220                                IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK) |
221                 u8_encode_bits(val, IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK);
222
223         eht_cap_elem->phy_cap_info[7] =
224                 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
225                 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
226                 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
227                 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ;
228
229         val = u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_RX) |
230               u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_TX);
231
232         eht_nss->bw._80.rx_tx_mcs9_max_nss = val;
233         eht_nss->bw._80.rx_tx_mcs11_max_nss = val;
234         eht_nss->bw._80.rx_tx_mcs13_max_nss = val;
235         eht_nss->bw._160.rx_tx_mcs9_max_nss = val;
236         eht_nss->bw._160.rx_tx_mcs11_max_nss = val;
237         eht_nss->bw._160.rx_tx_mcs13_max_nss = val;
238 }
239
240 static void
241 __mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy,
242                                 struct ieee80211_supported_band *sband,
243                                 enum nl80211_band band)
244 {
245         struct ieee80211_sband_iftype_data *data = phy->iftype[band];
246         int i, n = 0;
247
248         for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
249                 switch (i) {
250                 case NL80211_IFTYPE_STATION:
251                 case NL80211_IFTYPE_AP:
252                         break;
253                 default:
254                         continue;
255                 }
256
257                 data[n].types_mask = BIT(i);
258                 mt7925_init_he_caps(phy, band, &data[n], i);
259                 mt7925_init_eht_caps(phy, band, &data[n], i);
260
261                 n++;
262         }
263
264         _ieee80211_set_sband_iftype_data(sband, data, n);
265 }
266
267 void mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy)
268 {
269         if (phy->mt76->cap.has_2ghz)
270                 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_2g.sband,
271                                                 NL80211_BAND_2GHZ);
272
273         if (phy->mt76->cap.has_5ghz)
274                 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_5g.sband,
275                                                 NL80211_BAND_5GHZ);
276
277         if (phy->mt76->cap.has_6ghz)
278                 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_6g.sband,
279                                                 NL80211_BAND_6GHZ);
280 }
281
282 int __mt7925_start(struct mt792x_phy *phy)
283 {
284         struct mt76_phy *mphy = phy->mt76;
285         int err;
286
287         err = mt7925_mcu_set_channel_domain(mphy);
288         if (err)
289                 return err;
290
291         err = mt7925_mcu_set_rts_thresh(phy, 0x92b);
292         if (err)
293                 return err;
294
295         err = mt7925_set_tx_sar_pwr(mphy->hw, NULL);
296         if (err)
297                 return err;
298
299         mt792x_mac_reset_counters(phy);
300         set_bit(MT76_STATE_RUNNING, &mphy->state);
301
302         ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
303                                      MT792x_WATCHDOG_TIME);
304
305         return 0;
306 }
307 EXPORT_SYMBOL_GPL(__mt7925_start);
308
309 static int mt7925_start(struct ieee80211_hw *hw)
310 {
311         struct mt792x_phy *phy = mt792x_hw_phy(hw);
312         int err;
313
314         mt792x_mutex_acquire(phy->dev);
315         err = __mt7925_start(phy);
316         mt792x_mutex_release(phy->dev);
317
318         return err;
319 }
320
321 static int
322 mt7925_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
323 {
324         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
325         struct mt792x_dev *dev = mt792x_hw_dev(hw);
326         struct mt792x_phy *phy = mt792x_hw_phy(hw);
327         struct mt76_txq *mtxq;
328         int idx, ret = 0;
329
330         mt792x_mutex_acquire(dev);
331
332         mvif->mt76.idx = __ffs64(~dev->mt76.vif_mask);
333         if (mvif->mt76.idx >= MT792x_MAX_INTERFACES) {
334                 ret = -ENOSPC;
335                 goto out;
336         }
337
338         mvif->mt76.omac_idx = mvif->mt76.idx;
339         mvif->phy = phy;
340         mvif->mt76.band_idx = 0;
341         mvif->mt76.wmm_idx = mvif->mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
342
343         if (phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ)
344                 mvif->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL + 4;
345         else
346                 mvif->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL;
347
348         ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
349                                           true);
350         if (ret)
351                 goto out;
352
353         dev->mt76.vif_mask |= BIT_ULL(mvif->mt76.idx);
354         phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
355
356         idx = MT792x_WTBL_RESERVED - mvif->mt76.idx;
357
358         INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
359         mvif->sta.wcid.idx = idx;
360         mvif->sta.wcid.phy_idx = mvif->mt76.band_idx;
361         mvif->sta.wcid.hw_key_idx = -1;
362         mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
363         mt76_wcid_init(&mvif->sta.wcid);
364
365         mt7925_mac_wtbl_update(dev, idx,
366                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
367
368         ewma_rssi_init(&mvif->rssi);
369
370         rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
371         if (vif->txq) {
372                 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
373                 mtxq->wcid = idx;
374         }
375
376         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
377 out:
378         mt792x_mutex_release(dev);
379
380         return ret;
381 }
382
383 static void mt7925_roc_iter(void *priv, u8 *mac,
384                             struct ieee80211_vif *vif)
385 {
386         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
387         struct mt792x_phy *phy = priv;
388
389         mt7925_mcu_abort_roc(phy, mvif, phy->roc_token_id);
390 }
391
392 void mt7925_roc_work(struct work_struct *work)
393 {
394         struct mt792x_phy *phy;
395
396         phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
397                                                 roc_work);
398
399         if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
400                 return;
401
402         mt792x_mutex_acquire(phy->dev);
403         ieee80211_iterate_active_interfaces(phy->mt76->hw,
404                                             IEEE80211_IFACE_ITER_RESUME_ALL,
405                                             mt7925_roc_iter, phy);
406         mt792x_mutex_release(phy->dev);
407         ieee80211_remain_on_channel_expired(phy->mt76->hw);
408 }
409
410 static int mt7925_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif)
411 {
412         int err = 0;
413
414         del_timer_sync(&phy->roc_timer);
415         cancel_work_sync(&phy->roc_work);
416
417         mt792x_mutex_acquire(phy->dev);
418         if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
419                 err = mt7925_mcu_abort_roc(phy, vif, phy->roc_token_id);
420         mt792x_mutex_release(phy->dev);
421
422         return err;
423 }
424
425 static int mt7925_set_roc(struct mt792x_phy *phy,
426                           struct mt792x_vif *vif,
427                           struct ieee80211_channel *chan,
428                           int duration,
429                           enum mt7925_roc_req type)
430 {
431         int err;
432
433         if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
434                 return -EBUSY;
435
436         phy->roc_grant = false;
437
438         err = mt7925_mcu_set_roc(phy, vif, chan, duration, type,
439                                  ++phy->roc_token_id);
440         if (err < 0) {
441                 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
442                 goto out;
443         }
444
445         if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) {
446                 mt7925_mcu_abort_roc(phy, vif, phy->roc_token_id);
447                 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
448                 err = -ETIMEDOUT;
449         }
450
451 out:
452         return err;
453 }
454
455 static int mt7925_remain_on_channel(struct ieee80211_hw *hw,
456                                     struct ieee80211_vif *vif,
457                                     struct ieee80211_channel *chan,
458                                     int duration,
459                                     enum ieee80211_roc_type type)
460 {
461         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
462         struct mt792x_phy *phy = mt792x_hw_phy(hw);
463         int err;
464
465         mt792x_mutex_acquire(phy->dev);
466         err = mt7925_set_roc(phy, mvif, chan, duration, MT7925_ROC_REQ_ROC);
467         mt792x_mutex_release(phy->dev);
468
469         return err;
470 }
471
472 static int mt7925_cancel_remain_on_channel(struct ieee80211_hw *hw,
473                                            struct ieee80211_vif *vif)
474 {
475         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
476         struct mt792x_phy *phy = mt792x_hw_phy(hw);
477
478         return mt7925_abort_roc(phy, mvif);
479 }
480
481 static int mt7925_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
482                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
483                           struct ieee80211_key_conf *key)
484 {
485         struct mt792x_dev *dev = mt792x_hw_dev(hw);
486         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
487         struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv :
488                                   &mvif->sta;
489         struct mt76_wcid *wcid = &msta->wcid;
490         u8 *wcid_keyidx = &wcid->hw_key_idx;
491         int idx = key->keyidx, err = 0;
492
493         /* The hardware does not support per-STA RX GTK, fallback
494          * to software mode for these.
495          */
496         if ((vif->type == NL80211_IFTYPE_ADHOC ||
497              vif->type == NL80211_IFTYPE_MESH_POINT) &&
498             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
499              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
500             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
501                 return -EOPNOTSUPP;
502
503         /* fall back to sw encryption for unsupported ciphers */
504         switch (key->cipher) {
505         case WLAN_CIPHER_SUITE_AES_CMAC:
506                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
507                 wcid_keyidx = &wcid->hw_key_idx2;
508                 break;
509         case WLAN_CIPHER_SUITE_WEP40:
510         case WLAN_CIPHER_SUITE_WEP104:
511                 if (!mvif->wep_sta)
512                         return -EOPNOTSUPP;
513                 break;
514         case WLAN_CIPHER_SUITE_TKIP:
515         case WLAN_CIPHER_SUITE_CCMP:
516         case WLAN_CIPHER_SUITE_CCMP_256:
517         case WLAN_CIPHER_SUITE_GCMP:
518         case WLAN_CIPHER_SUITE_GCMP_256:
519         case WLAN_CIPHER_SUITE_SMS4:
520                 break;
521         default:
522                 return -EOPNOTSUPP;
523         }
524
525         mt792x_mutex_acquire(dev);
526
527         if (cmd == SET_KEY && !mvif->mt76.cipher) {
528                 struct mt792x_phy *phy = mt792x_hw_phy(hw);
529
530                 mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
531                 mt7925_mcu_add_bss_info(phy, mvif->mt76.ctx, vif, sta, true);
532         }
533
534         if (cmd == SET_KEY)
535                 *wcid_keyidx = idx;
536         else if (idx == *wcid_keyidx)
537                 *wcid_keyidx = -1;
538         else
539                 goto out;
540
541         mt76_wcid_key_setup(&dev->mt76, wcid,
542                             cmd == SET_KEY ? key : NULL);
543
544         err = mt7925_mcu_add_key(&dev->mt76, vif, &msta->bip,
545                                  key, MCU_UNI_CMD(STA_REC_UPDATE),
546                                  &msta->wcid, cmd);
547
548         if (err)
549                 goto out;
550
551         if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
552             key->cipher == WLAN_CIPHER_SUITE_WEP40)
553                 err = mt7925_mcu_add_key(&dev->mt76, vif, &mvif->wep_sta->bip,
554                                          key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE),
555                                          &mvif->wep_sta->wcid, cmd);
556
557 out:
558         mt792x_mutex_release(dev);
559
560         return err;
561 }
562
563 static void
564 mt7925_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
565 {
566         struct mt792x_dev *dev = priv;
567         struct ieee80211_hw *hw = mt76_hw(dev);
568         bool pm_enable = dev->pm.enable;
569         int err;
570
571         err = mt7925_mcu_set_beacon_filter(dev, vif, pm_enable);
572         if (err < 0)
573                 return;
574
575         if (pm_enable) {
576                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
577                 ieee80211_hw_set(hw, CONNECTION_MONITOR);
578         } else {
579                 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
580                 __clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags);
581         }
582 }
583
584 static void
585 mt7925_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
586 {
587         struct mt792x_dev *dev = priv;
588         struct ieee80211_hw *hw = mt76_hw(dev);
589         struct mt76_connac_pm *pm = &dev->pm;
590         bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
591
592         mt7925_mcu_set_sniffer(dev, vif, monitor);
593         pm->enable = pm->enable_user && !monitor;
594         pm->ds_enable = pm->ds_enable_user && !monitor;
595
596         mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
597
598         if (monitor)
599                 mt7925_mcu_set_beacon_filter(dev, vif, false);
600 }
601
602 void mt7925_set_runtime_pm(struct mt792x_dev *dev)
603 {
604         struct ieee80211_hw *hw = mt76_hw(dev);
605         struct mt76_connac_pm *pm = &dev->pm;
606         bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
607
608         pm->enable = pm->enable_user && !monitor;
609         ieee80211_iterate_active_interfaces(hw,
610                                             IEEE80211_IFACE_ITER_RESUME_ALL,
611                                             mt7925_pm_interface_iter, dev);
612         pm->ds_enable = pm->ds_enable_user && !monitor;
613         mt7925_mcu_set_deep_sleep(dev, pm->ds_enable);
614 }
615
616 static int mt7925_config(struct ieee80211_hw *hw, u32 changed)
617 {
618         struct mt792x_dev *dev = mt792x_hw_dev(hw);
619         int ret = 0;
620
621         mt792x_mutex_acquire(dev);
622
623         if (changed & IEEE80211_CONF_CHANGE_POWER) {
624                 ret = mt7925_set_tx_sar_pwr(hw, NULL);
625                 if (ret)
626                         goto out;
627         }
628
629         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
630                 ieee80211_iterate_active_interfaces(hw,
631                                                     IEEE80211_IFACE_ITER_RESUME_ALL,
632                                                     mt7925_sniffer_interface_iter, dev);
633         }
634
635 out:
636         mt792x_mutex_release(dev);
637
638         return ret;
639 }
640
641 static void mt7925_configure_filter(struct ieee80211_hw *hw,
642                                     unsigned int changed_flags,
643                                     unsigned int *total_flags,
644                                     u64 multicast)
645 {
646 #define MT7925_FILTER_FCSFAIL    BIT(2)
647 #define MT7925_FILTER_CONTROL    BIT(5)
648 #define MT7925_FILTER_OTHER_BSS  BIT(6)
649 #define MT7925_FILTER_ENABLE     BIT(31)
650         struct mt792x_dev *dev = mt792x_hw_dev(hw);
651         u32 flags = MT7925_FILTER_ENABLE;
652
653 #define MT7925_FILTER(_fif, _type) do {                 \
654                 if (*total_flags & (_fif))              \
655                         flags |= MT7925_FILTER_##_type; \
656         } while (0)
657
658         MT7925_FILTER(FIF_FCSFAIL, FCSFAIL);
659         MT7925_FILTER(FIF_CONTROL, CONTROL);
660         MT7925_FILTER(FIF_OTHER_BSS, OTHER_BSS);
661
662         mt792x_mutex_acquire(dev);
663         mt7925_mcu_set_rxfilter(dev, flags, 0, 0);
664         mt792x_mutex_release(dev);
665
666         *total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL);
667 }
668
669 static u8
670 mt7925_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
671                        bool beacon, bool mcast)
672 {
673         struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
674         struct mt76_phy *mphy = hw->priv;
675         u16 rate;
676         u8 i, idx, ht;
677
678         rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast);
679         ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM;
680
681         if (beacon && ht) {
682                 struct mt792x_dev *dev = mt792x_hw_dev(hw);
683
684                 /* must odd index */
685                 idx = MT7925_BEACON_RATES_TBL + 2 * (mvif->idx % 20);
686                 mt7925_mac_set_fixed_rate_table(dev, idx, rate);
687                 return idx;
688         }
689
690         idx = FIELD_GET(MT_TX_RATE_IDX, rate);
691         for (i = 0; i < ARRAY_SIZE(mt76_rates); i++)
692                 if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx)
693                         return MT792x_BASIC_RATES_TBL + i;
694
695         return mvif->basic_rates_idx;
696 }
697
698 static void mt7925_bss_info_changed(struct ieee80211_hw *hw,
699                                     struct ieee80211_vif *vif,
700                                     struct ieee80211_bss_conf *info,
701                                     u64 changed)
702 {
703         struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
704         struct mt792x_phy *phy = mt792x_hw_phy(hw);
705         struct mt792x_dev *dev = mt792x_hw_dev(hw);
706
707         mt792x_mutex_acquire(dev);
708
709         if (changed & BSS_CHANGED_ERP_SLOT) {
710                 int slottime = info->use_short_slot ? 9 : 20;
711
712                 if (slottime != phy->slottime) {
713                         phy->slottime = slottime;
714                         mt792x_mac_set_timeing(phy);
715                 }
716         }
717
718         if (changed & BSS_CHANGED_MCAST_RATE)
719                 mvif->mcast_rates_idx =
720                                 mt7925_get_rates_table(hw, vif, false, true);
721
722         if (changed & BSS_CHANGED_BASIC_RATES)
723                 mvif->basic_rates_idx =
724                                 mt7925_get_rates_table(hw, vif, false, false);
725
726         if (changed & (BSS_CHANGED_BEACON |
727                        BSS_CHANGED_BEACON_ENABLED)) {
728                 mvif->beacon_rates_idx =
729                                 mt7925_get_rates_table(hw, vif, true, false);
730
731                 mt7925_mcu_uni_add_beacon_offload(dev, hw, vif,
732                                                   info->enable_beacon);
733         }
734
735         /* ensure that enable txcmd_mode after bss_info */
736         if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
737                 mt7925_mcu_set_tx(dev, vif);
738
739         if (changed & BSS_CHANGED_PS)
740                 mt7925_mcu_uni_bss_ps(dev, vif);
741
742         if (changed & BSS_CHANGED_ASSOC) {
743                 mt7925_mcu_sta_update(dev, NULL, vif, true,
744                                       MT76_STA_INFO_STATE_ASSOC);
745                 mt7925_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
746         }
747
748         if (changed & BSS_CHANGED_ARP_FILTER) {
749                 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
750
751                 mt7925_mcu_update_arp_filter(&dev->mt76, &mvif->mt76, info);
752         }
753
754         mt792x_mutex_release(dev);
755 }
756
757 int mt7925_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
758                        struct ieee80211_sta *sta)
759 {
760         struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
761         struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
762         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
763         int ret, idx;
764
765         idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1);
766         if (idx < 0)
767                 return -ENOSPC;
768
769         INIT_LIST_HEAD(&msta->wcid.poll_list);
770         msta->vif = mvif;
771         msta->wcid.sta = 1;
772         msta->wcid.idx = idx;
773         msta->wcid.phy_idx = mvif->mt76.band_idx;
774         msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
775         msta->last_txs = jiffies;
776
777         ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
778         if (ret)
779                 return ret;
780
781         if (vif->type == NL80211_IFTYPE_STATION)
782                 mvif->wep_sta = msta;
783
784         mt7925_mac_wtbl_update(dev, idx,
785                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
786
787         /* should update bss info before STA add */
788         if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
789                 mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
790                                         false);
791
792         ret = mt7925_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(mt7925_mac_sta_add);
802
803 void mt7925_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
804                           struct ieee80211_sta *sta)
805 {
806         struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
807         struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
808         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
809
810         mt792x_mutex_acquire(dev);
811
812         if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
813                 mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
814                                         true);
815
816         ewma_avg_signal_init(&msta->avg_ack_signal);
817
818         mt7925_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         mt7925_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
823
824         mt792x_mutex_release(dev);
825 }
826 EXPORT_SYMBOL_GPL(mt7925_mac_sta_assoc);
827
828 void mt7925_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
829                            struct ieee80211_sta *sta)
830 {
831         struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76);
832         struct mt792x_sta *msta = (struct mt792x_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         mt7925_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
838         mt7925_mac_wtbl_update(dev, msta->wcid.idx,
839                                MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
840
841         if (vif->type == NL80211_IFTYPE_STATION) {
842                 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
843
844                 mvif->wep_sta = NULL;
845                 ewma_rssi_init(&mvif->rssi);
846                 if (!sta->tdls)
847                         mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, sta,
848                                                 false);
849         }
850
851         spin_lock_bh(&mdev->sta_poll_lock);
852         if (!list_empty(&msta->wcid.poll_list))
853                 list_del_init(&msta->wcid.poll_list);
854         spin_unlock_bh(&mdev->sta_poll_lock);
855
856         mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
857 }
858 EXPORT_SYMBOL_GPL(mt7925_mac_sta_remove);
859
860 static int mt7925_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
861 {
862         struct mt792x_dev *dev = mt792x_hw_dev(hw);
863
864         mt792x_mutex_acquire(dev);
865         mt7925_mcu_set_rts_thresh(&dev->phy, val);
866         mt792x_mutex_release(dev);
867
868         return 0;
869 }
870
871 static int
872 mt7925_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
873                     struct ieee80211_ampdu_params *params)
874 {
875         enum ieee80211_ampdu_mlme_action action = params->action;
876         struct mt792x_dev *dev = mt792x_hw_dev(hw);
877         struct ieee80211_sta *sta = params->sta;
878         struct ieee80211_txq *txq = sta->txq[params->tid];
879         struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
880         u16 tid = params->tid;
881         u16 ssn = params->ssn;
882         struct mt76_txq *mtxq;
883         int ret = 0;
884
885         if (!txq)
886                 return -EINVAL;
887
888         mtxq = (struct mt76_txq *)txq->drv_priv;
889
890         mt792x_mutex_acquire(dev);
891         switch (action) {
892         case IEEE80211_AMPDU_RX_START:
893                 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
894                                    params->buf_size);
895                 mt7925_mcu_uni_rx_ba(dev, params, true);
896                 break;
897         case IEEE80211_AMPDU_RX_STOP:
898                 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
899                 mt7925_mcu_uni_rx_ba(dev, params, false);
900                 break;
901         case IEEE80211_AMPDU_TX_OPERATIONAL:
902                 mtxq->aggr = true;
903                 mtxq->send_bar = false;
904                 mt7925_mcu_uni_tx_ba(dev, params, true);
905                 break;
906         case IEEE80211_AMPDU_TX_STOP_FLUSH:
907         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
908                 mtxq->aggr = false;
909                 clear_bit(tid, &msta->wcid.ampdu_state);
910                 mt7925_mcu_uni_tx_ba(dev, params, false);
911                 break;
912         case IEEE80211_AMPDU_TX_START:
913                 set_bit(tid, &msta->wcid.ampdu_state);
914                 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
915                 break;
916         case IEEE80211_AMPDU_TX_STOP_CONT:
917                 mtxq->aggr = false;
918                 clear_bit(tid, &msta->wcid.ampdu_state);
919                 mt7925_mcu_uni_tx_ba(dev, params, false);
920                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
921                 break;
922         }
923         mt792x_mutex_release(dev);
924
925         return ret;
926 }
927
928 static bool is_valid_alpha2(const char *alpha2)
929 {
930         if (!alpha2)
931                 return false;
932
933         if (alpha2[0] == '0' && alpha2[1] == '0')
934                 return true;
935
936         if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
937                 return true;
938
939         return false;
940 }
941
942 void mt7925_scan_work(struct work_struct *work)
943 {
944         struct mt792x_phy *phy;
945
946         phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
947                                                 scan_work.work);
948
949         while (true) {
950                 struct mt76_dev *mdev = &phy->dev->mt76;
951                 struct sk_buff *skb;
952                 struct tlv *tlv;
953                 int tlv_len;
954
955                 spin_lock_bh(&phy->dev->mt76.lock);
956                 skb = __skb_dequeue(&phy->scan_event_list);
957                 spin_unlock_bh(&phy->dev->mt76.lock);
958
959                 if (!skb)
960                         break;
961
962                 skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
963                 tlv = (struct tlv *)skb->data;
964                 tlv_len = skb->len;
965
966                 while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
967                         struct mt7925_mcu_scan_chinfo_event *evt;
968
969                         switch (le16_to_cpu(tlv->tag)) {
970                         case UNI_EVENT_SCAN_DONE_BASIC:
971                                 if (test_and_clear_bit(MT76_HW_SCANNING, &phy->mt76->state)) {
972                                         struct cfg80211_scan_info info = {
973                                                 .aborted = false,
974                                         };
975                                         ieee80211_scan_completed(phy->mt76->hw, &info);
976                                 }
977                                 break;
978                         case UNI_EVENT_SCAN_DONE_CHNLINFO:
979                                 evt = (struct mt7925_mcu_scan_chinfo_event *)tlv->data;
980
981                                 if (!is_valid_alpha2(evt->alpha2))
982                                         break;
983
984                                 if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0')
985                                         break;
986
987                                 mt7925_mcu_set_clc(phy->dev, evt->alpha2, ENVIRON_INDOOR);
988
989                                 break;
990                         case UNI_EVENT_SCAN_DONE_NLO:
991                                 ieee80211_sched_scan_results(phy->mt76->hw);
992                                 break;
993                         default:
994                                 break;
995                         }
996
997                         tlv_len -= le16_to_cpu(tlv->len);
998                         tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
999                 }
1000
1001                 dev_kfree_skb(skb);
1002         }
1003 }
1004
1005 static int
1006 mt7925_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1007                struct ieee80211_scan_request *req)
1008 {
1009         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1010         struct mt76_phy *mphy = hw->priv;
1011         int err;
1012
1013         mt792x_mutex_acquire(dev);
1014         err = mt7925_mcu_hw_scan(mphy, vif, req);
1015         mt792x_mutex_release(dev);
1016
1017         return err;
1018 }
1019
1020 static void
1021 mt7925_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1022 {
1023         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1024         struct mt76_phy *mphy = hw->priv;
1025
1026         mt792x_mutex_acquire(dev);
1027         mt7925_mcu_cancel_hw_scan(mphy, vif);
1028         mt792x_mutex_release(dev);
1029 }
1030
1031 static int
1032 mt7925_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1033                         struct cfg80211_sched_scan_request *req,
1034                         struct ieee80211_scan_ies *ies)
1035 {
1036         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1037         struct mt76_phy *mphy = hw->priv;
1038         int err;
1039
1040         mt792x_mutex_acquire(dev);
1041
1042         err = mt7925_mcu_sched_scan_req(mphy, vif, req);
1043         if (err < 0)
1044                 goto out;
1045
1046         err = mt7925_mcu_sched_scan_enable(mphy, vif, true);
1047 out:
1048         mt792x_mutex_release(dev);
1049
1050         return err;
1051 }
1052
1053 static int
1054 mt7925_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1055 {
1056         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1057         struct mt76_phy *mphy = hw->priv;
1058         int err;
1059
1060         mt792x_mutex_acquire(dev);
1061         err = mt7925_mcu_sched_scan_enable(mphy, vif, false);
1062         mt792x_mutex_release(dev);
1063
1064         return err;
1065 }
1066
1067 static int
1068 mt7925_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1069 {
1070         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1071         struct mt792x_phy *phy = mt792x_hw_phy(hw);
1072         int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1073
1074         if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1075                 return -EINVAL;
1076
1077         if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1078                 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1079
1080         mt792x_mutex_acquire(dev);
1081
1082         phy->mt76->antenna_mask = tx_ant;
1083         phy->mt76->chainmask = tx_ant;
1084
1085         mt76_set_stream_caps(phy->mt76, true);
1086         mt7925_set_stream_he_eht_caps(phy);
1087
1088         /* TODO: update bmc_wtbl spe_idx when antenna changes */
1089         mt792x_mutex_release(dev);
1090
1091         return 0;
1092 }
1093
1094 #ifdef CONFIG_PM
1095 static int mt7925_suspend(struct ieee80211_hw *hw,
1096                           struct cfg80211_wowlan *wowlan)
1097 {
1098         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1099         struct mt792x_phy *phy = mt792x_hw_phy(hw);
1100
1101         cancel_delayed_work_sync(&phy->scan_work);
1102         cancel_delayed_work_sync(&phy->mt76->mac_work);
1103
1104         cancel_delayed_work_sync(&dev->pm.ps_work);
1105         mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1106
1107         mt792x_mutex_acquire(dev);
1108
1109         clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1110         ieee80211_iterate_active_interfaces(hw,
1111                                             IEEE80211_IFACE_ITER_RESUME_ALL,
1112                                             mt7925_mcu_set_suspend_iter,
1113                                             &dev->mphy);
1114
1115         mt792x_mutex_release(dev);
1116
1117         return 0;
1118 }
1119
1120 static int mt7925_resume(struct ieee80211_hw *hw)
1121 {
1122         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1123         struct mt792x_phy *phy = mt792x_hw_phy(hw);
1124
1125         mt792x_mutex_acquire(dev);
1126
1127         set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1128         ieee80211_iterate_active_interfaces(hw,
1129                                             IEEE80211_IFACE_ITER_RESUME_ALL,
1130                                             mt7925_mcu_set_suspend_iter,
1131                                             &dev->mphy);
1132
1133         ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1134                                      MT792x_WATCHDOG_TIME);
1135
1136         mt792x_mutex_release(dev);
1137
1138         return 0;
1139 }
1140
1141 static void mt7925_set_rekey_data(struct ieee80211_hw *hw,
1142                                   struct ieee80211_vif *vif,
1143                                   struct cfg80211_gtk_rekey_data *data)
1144 {
1145         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1146
1147         mt792x_mutex_acquire(dev);
1148         mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1149         mt792x_mutex_release(dev);
1150 }
1151 #endif /* CONFIG_PM */
1152
1153 static void mt7925_sta_set_decap_offload(struct ieee80211_hw *hw,
1154                                          struct ieee80211_vif *vif,
1155                                          struct ieee80211_sta *sta,
1156                                          bool enabled)
1157 {
1158         struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1159         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1160
1161         mt792x_mutex_acquire(dev);
1162
1163         if (enabled)
1164                 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1165         else
1166                 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1167
1168         mt7925_mcu_wtbl_update_hdr_trans(dev, vif, sta);
1169
1170         mt792x_mutex_release(dev);
1171 }
1172
1173 #if IS_ENABLED(CONFIG_IPV6)
1174 static void mt7925_ipv6_addr_change(struct ieee80211_hw *hw,
1175                                     struct ieee80211_vif *vif,
1176                                     struct inet6_dev *idev)
1177 {
1178         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1179         struct mt792x_dev *dev = mvif->phy->dev;
1180         struct inet6_ifaddr *ifa;
1181         struct sk_buff *skb;
1182         u8 idx = 0;
1183
1184         struct {
1185                 struct {
1186                         u8 bss_idx;
1187                         u8 pad[3];
1188                 } __packed hdr;
1189                 struct mt7925_arpns_tlv arpns;
1190                 struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1191         } req_hdr = {
1192                 .hdr = {
1193                         .bss_idx = mvif->mt76.idx,
1194                 },
1195                 .arpns = {
1196                         .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1197                         .len = cpu_to_le16(sizeof(req_hdr) - 4),
1198                         .enable = true,
1199                 },
1200         };
1201
1202         read_lock_bh(&idev->lock);
1203         list_for_each_entry(ifa, &idev->addr_list, if_list) {
1204                 if (ifa->flags & IFA_F_TENTATIVE)
1205                         continue;
1206                 req_hdr.ns_addrs[idx] = ifa->addr;
1207                 if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1208                         break;
1209         }
1210         read_unlock_bh(&idev->lock);
1211
1212         if (!idx)
1213                 return;
1214
1215         req_hdr.arpns.ips_num = idx;
1216
1217         skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr),
1218                                    0, GFP_ATOMIC);
1219         if (!skb)
1220                 return;
1221
1222         skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1223
1224         skb_queue_tail(&dev->ipv6_ns_list, skb);
1225
1226         ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1227 }
1228 #endif
1229
1230 int mt7925_set_tx_sar_pwr(struct ieee80211_hw *hw,
1231                           const struct cfg80211_sar_specs *sar)
1232 {
1233         struct mt76_phy *mphy = hw->priv;
1234
1235         if (sar) {
1236                 int err = mt76_init_sar_power(hw, sar);
1237
1238                 if (err)
1239                         return err;
1240         }
1241         mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1242
1243         return mt7925_mcu_set_rate_txpower(mphy);
1244 }
1245
1246 static int mt7925_set_sar_specs(struct ieee80211_hw *hw,
1247                                 const struct cfg80211_sar_specs *sar)
1248 {
1249         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1250         int err;
1251
1252         mt792x_mutex_acquire(dev);
1253         err = mt7925_mcu_set_clc(dev, dev->mt76.alpha2,
1254                                  dev->country_ie_env);
1255         if (err < 0)
1256                 goto out;
1257
1258         err = mt7925_set_tx_sar_pwr(hw, sar);
1259 out:
1260         mt792x_mutex_release(dev);
1261
1262         return err;
1263 }
1264
1265 static void
1266 mt7925_channel_switch_beacon(struct ieee80211_hw *hw,
1267                              struct ieee80211_vif *vif,
1268                              struct cfg80211_chan_def *chandef)
1269 {
1270         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1271
1272         mt792x_mutex_acquire(dev);
1273         mt7925_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1274         mt792x_mutex_release(dev);
1275 }
1276
1277 static int
1278 mt7925_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1279                 struct ieee80211_bss_conf *link_conf)
1280 {
1281         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1282         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1283         int err;
1284
1285         mt792x_mutex_acquire(dev);
1286
1287         err = mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, NULL,
1288                                       true);
1289         if (err)
1290                 goto out;
1291
1292         err = mt7925_mcu_set_bss_pm(dev, vif, true);
1293         if (err)
1294                 goto out;
1295
1296         err = mt7925_mcu_sta_update(dev, NULL, vif, true,
1297                                     MT76_STA_INFO_STATE_NONE);
1298 out:
1299         mt792x_mutex_release(dev);
1300
1301         return err;
1302 }
1303
1304 static void
1305 mt7925_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1306                struct ieee80211_bss_conf *link_conf)
1307 {
1308         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1309         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1310         int err;
1311
1312         mt792x_mutex_acquire(dev);
1313
1314         err = mt7925_mcu_set_bss_pm(dev, vif, false);
1315         if (err)
1316                 goto out;
1317
1318         mt7925_mcu_add_bss_info(&dev->phy, mvif->mt76.ctx, vif, NULL,
1319                                 false);
1320
1321 out:
1322         mt792x_mutex_release(dev);
1323 }
1324
1325 static int
1326 mt7925_add_chanctx(struct ieee80211_hw *hw,
1327                    struct ieee80211_chanctx_conf *ctx)
1328 {
1329         return 0;
1330 }
1331
1332 static void
1333 mt7925_remove_chanctx(struct ieee80211_hw *hw,
1334                       struct ieee80211_chanctx_conf *ctx)
1335 {
1336 }
1337
1338 static void mt7925_ctx_iter(void *priv, u8 *mac,
1339                             struct ieee80211_vif *vif)
1340 {
1341         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1342         struct ieee80211_chanctx_conf *ctx = priv;
1343
1344         if (ctx != mvif->mt76.ctx)
1345                 return;
1346
1347         if (vif->type == NL80211_IFTYPE_MONITOR) {
1348                 mt7925_mcu_set_sniffer(mvif->phy->dev, vif, true);
1349                 mt7925_mcu_config_sniffer(mvif, ctx);
1350         } else {
1351                 mt7925_mcu_set_chctx(mvif->phy->mt76, &mvif->mt76, ctx);
1352         }
1353 }
1354
1355 static void
1356 mt7925_change_chanctx(struct ieee80211_hw *hw,
1357                       struct ieee80211_chanctx_conf *ctx,
1358                       u32 changed)
1359 {
1360         struct mt792x_phy *phy = mt792x_hw_phy(hw);
1361
1362         mt792x_mutex_acquire(phy->dev);
1363         ieee80211_iterate_active_interfaces(phy->mt76->hw,
1364                                             IEEE80211_IFACE_ITER_ACTIVE,
1365                                             mt7925_ctx_iter, ctx);
1366         mt792x_mutex_release(phy->dev);
1367 }
1368
1369 static void mt7925_mgd_prepare_tx(struct ieee80211_hw *hw,
1370                                   struct ieee80211_vif *vif,
1371                                   struct ieee80211_prep_tx_info *info)
1372 {
1373         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1374         struct mt792x_dev *dev = mt792x_hw_dev(hw);
1375         u16 duration = info->duration ? info->duration :
1376                        jiffies_to_msecs(HZ);
1377
1378         mt792x_mutex_acquire(dev);
1379         mt7925_set_roc(mvif->phy, mvif, mvif->mt76.ctx->def.chan, duration,
1380                        MT7925_ROC_REQ_JOIN);
1381         mt792x_mutex_release(dev);
1382 }
1383
1384 static void mt7925_mgd_complete_tx(struct ieee80211_hw *hw,
1385                                    struct ieee80211_vif *vif,
1386                                    struct ieee80211_prep_tx_info *info)
1387 {
1388         struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1389
1390         mt7925_abort_roc(mvif->phy, mvif);
1391 }
1392
1393 const struct ieee80211_ops mt7925_ops = {
1394         .tx = mt792x_tx,
1395         .start = mt7925_start,
1396         .stop = mt792x_stop,
1397         .add_interface = mt7925_add_interface,
1398         .remove_interface = mt792x_remove_interface,
1399         .config = mt7925_config,
1400         .conf_tx = mt792x_conf_tx,
1401         .configure_filter = mt7925_configure_filter,
1402         .bss_info_changed = mt7925_bss_info_changed,
1403         .start_ap = mt7925_start_ap,
1404         .stop_ap = mt7925_stop_ap,
1405         .sta_state = mt76_sta_state,
1406         .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1407         .set_key = mt7925_set_key,
1408         .sta_set_decap_offload = mt7925_sta_set_decap_offload,
1409 #if IS_ENABLED(CONFIG_IPV6)
1410         .ipv6_addr_change = mt7925_ipv6_addr_change,
1411 #endif /* CONFIG_IPV6 */
1412         .ampdu_action = mt7925_ampdu_action,
1413         .set_rts_threshold = mt7925_set_rts_threshold,
1414         .wake_tx_queue = mt76_wake_tx_queue,
1415         .release_buffered_frames = mt76_release_buffered_frames,
1416         .channel_switch_beacon = mt7925_channel_switch_beacon,
1417         .get_txpower = mt76_get_txpower,
1418         .get_stats = mt792x_get_stats,
1419         .get_et_sset_count = mt792x_get_et_sset_count,
1420         .get_et_strings = mt792x_get_et_strings,
1421         .get_et_stats = mt792x_get_et_stats,
1422         .get_tsf = mt792x_get_tsf,
1423         .set_tsf = mt792x_set_tsf,
1424         .get_survey = mt76_get_survey,
1425         .get_antenna = mt76_get_antenna,
1426         .set_antenna = mt7925_set_antenna,
1427         .set_coverage_class = mt792x_set_coverage_class,
1428         .hw_scan = mt7925_hw_scan,
1429         .cancel_hw_scan = mt7925_cancel_hw_scan,
1430         .sta_statistics = mt792x_sta_statistics,
1431         .sched_scan_start = mt7925_start_sched_scan,
1432         .sched_scan_stop = mt7925_stop_sched_scan,
1433 #ifdef CONFIG_PM
1434         .suspend = mt7925_suspend,
1435         .resume = mt7925_resume,
1436         .set_wakeup = mt792x_set_wakeup,
1437         .set_rekey_data = mt7925_set_rekey_data,
1438 #endif /* CONFIG_PM */
1439         .flush = mt792x_flush,
1440         .set_sar_specs = mt7925_set_sar_specs,
1441         .remain_on_channel = mt7925_remain_on_channel,
1442         .cancel_remain_on_channel = mt7925_cancel_remain_on_channel,
1443         .add_chanctx = mt7925_add_chanctx,
1444         .remove_chanctx = mt7925_remove_chanctx,
1445         .change_chanctx = mt7925_change_chanctx,
1446         .assign_vif_chanctx = mt792x_assign_vif_chanctx,
1447         .unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1448         .mgd_prepare_tx = mt7925_mgd_prepare_tx,
1449         .mgd_complete_tx = mt7925_mgd_complete_tx,
1450 };
1451 EXPORT_SYMBOL_GPL(mt7925_ops);
1452
1453 MODULE_AUTHOR("Deren Wu <deren.wu@mediatek.com>");
1454 MODULE_LICENSE("Dual BSD/GPL");