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