Debian Codegraph @

drivers/net/wireless/mediatek/mt76/mt7921/main.c

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