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