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