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