xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
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,int radio_idx,u32 changed)627 static int mt7921_config(struct ieee80211_hw *hw, int radio_idx, 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,int radio_idx,u32 val)910 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
911 				    u32 val)
912 {
913 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
914 
915 	mt792x_mutex_acquire(dev);
916 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
917 	mt792x_mutex_release(dev);
918 
919 	return 0;
920 }
921 
922 static int
mt7921_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)923 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
924 		    struct ieee80211_ampdu_params *params)
925 {
926 	enum ieee80211_ampdu_mlme_action action = params->action;
927 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
928 	struct ieee80211_sta *sta = params->sta;
929 	struct ieee80211_txq *txq = sta->txq[params->tid];
930 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
931 	u16 tid = params->tid;
932 	u16 ssn = params->ssn;
933 	struct mt76_txq *mtxq;
934 	int ret = 0;
935 
936 	if (!txq)
937 		return -EINVAL;
938 
939 	mtxq = (struct mt76_txq *)txq->drv_priv;
940 
941 	mt792x_mutex_acquire(dev);
942 	switch (action) {
943 	case IEEE80211_AMPDU_RX_START:
944 		mt76_rx_aggr_start(&dev->mt76, &msta->deflink.wcid, tid, ssn,
945 				   params->buf_size);
946 		mt7921_mcu_uni_rx_ba(dev, params, true);
947 		break;
948 	case IEEE80211_AMPDU_RX_STOP:
949 		mt76_rx_aggr_stop(&dev->mt76, &msta->deflink.wcid, tid);
950 		mt7921_mcu_uni_rx_ba(dev, params, false);
951 		break;
952 	case IEEE80211_AMPDU_TX_OPERATIONAL:
953 		mtxq->aggr = true;
954 		mtxq->send_bar = false;
955 		mt7921_mcu_uni_tx_ba(dev, params, true);
956 		break;
957 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
958 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
959 		mtxq->aggr = false;
960 		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
961 		mt7921_mcu_uni_tx_ba(dev, params, false);
962 		break;
963 	case IEEE80211_AMPDU_TX_START:
964 		set_bit(tid, &msta->deflink.wcid.ampdu_state);
965 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
966 		break;
967 	case IEEE80211_AMPDU_TX_STOP_CONT:
968 		mtxq->aggr = false;
969 		clear_bit(tid, &msta->deflink.wcid.ampdu_state);
970 		mt7921_mcu_uni_tx_ba(dev, params, false);
971 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
972 		break;
973 	}
974 	mt792x_mutex_release(dev);
975 
976 	return ret;
977 }
978 
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)979 static int mt7921_sta_state(struct ieee80211_hw *hw,
980 			    struct ieee80211_vif *vif,
981 			    struct ieee80211_sta *sta,
982 			    enum ieee80211_sta_state old_state,
983 			    enum ieee80211_sta_state new_state)
984 {
985 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
986 
987 	if (dev->pm.ds_enable) {
988 		mt792x_mutex_acquire(dev);
989 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
990 		mt792x_mutex_release(dev);
991 	}
992 
993 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
994 }
995 
mt7921_scan_work(struct work_struct * work)996 void mt7921_scan_work(struct work_struct *work)
997 {
998 	struct mt792x_phy *phy;
999 
1000 	phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy,
1001 						scan_work.work);
1002 
1003 	while (true) {
1004 		struct mt76_connac2_mcu_rxd *rxd;
1005 		struct sk_buff *skb;
1006 
1007 		spin_lock_bh(&phy->dev->mt76.lock);
1008 		skb = __skb_dequeue(&phy->scan_event_list);
1009 		spin_unlock_bh(&phy->dev->mt76.lock);
1010 
1011 		if (!skb)
1012 			break;
1013 
1014 		rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
1015 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1016 			ieee80211_sched_scan_results(phy->mt76->hw);
1017 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1018 					      &phy->mt76->state)) {
1019 			struct cfg80211_scan_info info = {
1020 				.aborted = false,
1021 			};
1022 
1023 			ieee80211_scan_completed(phy->mt76->hw, &info);
1024 		}
1025 		dev_kfree_skb(skb);
1026 	}
1027 }
1028 
1029 static int
mt7921_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)1030 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1031 	       struct ieee80211_scan_request *req)
1032 {
1033 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1034 	struct mt76_phy *mphy = hw->priv;
1035 	int err;
1036 
1037 	mt792x_mutex_acquire(dev);
1038 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1039 	mt792x_mutex_release(dev);
1040 
1041 	return err;
1042 }
1043 
1044 static void
mt7921_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1045 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1046 {
1047 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1048 	struct mt76_phy *mphy = hw->priv;
1049 
1050 	mt792x_mutex_acquire(dev);
1051 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1052 	mt792x_mutex_release(dev);
1053 }
1054 
1055 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)1056 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1057 			struct cfg80211_sched_scan_request *req,
1058 			struct ieee80211_scan_ies *ies)
1059 {
1060 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1061 	struct mt76_phy *mphy = hw->priv;
1062 	int err;
1063 
1064 	mt792x_mutex_acquire(dev);
1065 
1066 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1067 	if (err < 0)
1068 		goto out;
1069 
1070 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1071 out:
1072 	mt792x_mutex_release(dev);
1073 
1074 	return err;
1075 }
1076 
1077 static int
mt7921_stop_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1078 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1079 {
1080 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1081 	struct mt76_phy *mphy = hw->priv;
1082 	int err;
1083 
1084 	mt792x_mutex_acquire(dev);
1085 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1086 	mt792x_mutex_release(dev);
1087 
1088 	return err;
1089 }
1090 
1091 static int
mt7921_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)1092 mt7921_set_antenna(struct ieee80211_hw *hw, int radio_idx,
1093 		   u32 tx_ant, u32 rx_ant)
1094 {
1095 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1096 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1097 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1098 
1099 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1100 		return -EINVAL;
1101 
1102 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1103 		return -EINVAL;
1104 
1105 	mt792x_mutex_acquire(dev);
1106 
1107 	phy->mt76->antenna_mask = tx_ant;
1108 	phy->mt76->chainmask = tx_ant;
1109 
1110 	mt76_set_stream_caps(phy->mt76, true);
1111 	mt7921_set_stream_he_caps(phy);
1112 
1113 	mt792x_mutex_release(dev);
1114 
1115 	return 0;
1116 }
1117 
1118 #ifdef CONFIG_PM
mt7921_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1119 static int mt7921_suspend(struct ieee80211_hw *hw,
1120 			  struct cfg80211_wowlan *wowlan)
1121 {
1122 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1123 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1124 
1125 	cancel_delayed_work_sync(&phy->scan_work);
1126 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1127 
1128 	cancel_delayed_work_sync(&dev->pm.ps_work);
1129 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1130 
1131 	mt792x_mutex_acquire(dev);
1132 
1133 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1134 	ieee80211_iterate_active_interfaces(hw,
1135 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1136 					    mt7921_mcu_set_suspend_iter,
1137 					    &dev->mphy);
1138 
1139 	mt792x_mutex_release(dev);
1140 
1141 	return 0;
1142 }
1143 
mt7921_resume(struct ieee80211_hw * hw)1144 static int mt7921_resume(struct ieee80211_hw *hw)
1145 {
1146 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1147 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1148 
1149 	mt792x_mutex_acquire(dev);
1150 
1151 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1152 	ieee80211_iterate_active_interfaces(hw,
1153 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1154 					    mt76_connac_mcu_set_suspend_iter,
1155 					    &dev->mphy);
1156 
1157 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1158 				     MT792x_WATCHDOG_TIME);
1159 
1160 	mt792x_mutex_release(dev);
1161 
1162 	return 0;
1163 }
1164 
mt7921_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)1165 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1166 				  struct ieee80211_vif *vif,
1167 				  struct cfg80211_gtk_rekey_data *data)
1168 {
1169 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1170 
1171 	mt792x_mutex_acquire(dev);
1172 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1173 	mt792x_mutex_release(dev);
1174 }
1175 #endif /* CONFIG_PM */
1176 
mt7921_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1177 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1178 					 struct ieee80211_vif *vif,
1179 					 struct ieee80211_sta *sta,
1180 					 bool enabled)
1181 {
1182 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1183 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1184 
1185 	if (!msta->deflink.wcid.sta)
1186 		return;
1187 
1188 	mt792x_mutex_acquire(dev);
1189 
1190 	if (enabled)
1191 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags);
1192 	else
1193 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags);
1194 
1195 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->deflink.wcid,
1196 					     MCU_UNI_CMD(STA_REC_UPDATE));
1197 
1198 	mt792x_mutex_release(dev);
1199 }
1200 
1201 #if IS_ENABLED(CONFIG_IPV6)
mt7921_ipv6_addr_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)1202 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw,
1203 				    struct ieee80211_vif *vif,
1204 				    struct inet6_dev *idev)
1205 {
1206 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1207 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1208 	struct inet6_ifaddr *ifa;
1209 	struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN];
1210 	struct sk_buff *skb;
1211 	u8 i, idx = 0;
1212 
1213 	struct {
1214 		struct {
1215 			u8 bss_idx;
1216 			u8 pad[3];
1217 		} __packed hdr;
1218 		struct mt76_connac_arpns_tlv arpns;
1219 	} req_hdr = {
1220 		.hdr = {
1221 			.bss_idx = mvif->bss_conf.mt76.idx,
1222 		},
1223 		.arpns = {
1224 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND),
1225 			.mode = 2,  /* update */
1226 			.option = 1, /* update only */
1227 		},
1228 	};
1229 
1230 	read_lock_bh(&idev->lock);
1231 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1232 		if (ifa->flags & IFA_F_TENTATIVE)
1233 			continue;
1234 		ns_addrs[idx] = ifa->addr;
1235 		if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN)
1236 			break;
1237 	}
1238 	read_unlock_bh(&idev->lock);
1239 
1240 	if (!idx)
1241 		return;
1242 
1243 	req_hdr.arpns.ips_num = idx;
1244 	req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)
1245 					+ idx * sizeof(struct in6_addr));
1246 	skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr,
1247 			sizeof(req_hdr) + idx * sizeof(struct in6_addr),
1248 			sizeof(req_hdr), GFP_ATOMIC);
1249 	if (!skb)
1250 		return;
1251 
1252 	for (i = 0; i < idx; i++)
1253 		skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr));
1254 
1255 	skb_queue_tail(&dev->ipv6_ns_list, skb);
1256 
1257 	ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work);
1258 }
1259 #endif
1260 
mt7921_set_tx_sar_pwr(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)1261 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
1262 			  const struct cfg80211_sar_specs *sar)
1263 {
1264 	struct mt76_phy *mphy = hw->priv;
1265 
1266 	if (sar) {
1267 		int err = mt76_init_sar_power(hw, sar);
1268 
1269 		if (err)
1270 			return err;
1271 	}
1272 	mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar);
1273 
1274 	return mt76_connac_mcu_set_rate_txpower(mphy);
1275 }
1276 
mt7921_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)1277 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1278 				const struct cfg80211_sar_specs *sar)
1279 {
1280 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1281 	int err;
1282 
1283 	mt792x_mutex_acquire(dev);
1284 	err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2,
1285 				 dev->country_ie_env);
1286 	if (err < 0)
1287 		goto out;
1288 
1289 	err = mt7921_set_tx_sar_pwr(hw, sar);
1290 out:
1291 	mt792x_mutex_release(dev);
1292 
1293 	return err;
1294 }
1295 
1296 static void
mt7921_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)1297 mt7921_channel_switch_beacon(struct ieee80211_hw *hw,
1298 			     struct ieee80211_vif *vif,
1299 			     struct cfg80211_chan_def *chandef)
1300 {
1301 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1302 
1303 	mt792x_mutex_acquire(dev);
1304 	mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true);
1305 	mt792x_mutex_release(dev);
1306 }
1307 
1308 static int
mt7921_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1309 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1310 		struct ieee80211_bss_conf *link_conf)
1311 {
1312 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1313 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1314 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1315 	int err;
1316 
1317 	mt792x_mutex_acquire(dev);
1318 
1319 	err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid,
1320 					  true, mvif->bss_conf.mt76.ctx);
1321 	if (err)
1322 		goto out;
1323 
1324 	err = mt7921_mcu_set_bss_pm(dev, vif, true);
1325 	if (err)
1326 		goto out;
1327 
1328 	err = mt7921_mcu_sta_update(dev, NULL, vif, true,
1329 				    MT76_STA_INFO_STATE_NONE);
1330 out:
1331 	mt792x_mutex_release(dev);
1332 
1333 	return err;
1334 }
1335 
1336 static void
mt7921_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1337 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1338 	       struct ieee80211_bss_conf *link_conf)
1339 {
1340 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1341 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1342 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1343 	int err;
1344 
1345 	mt792x_mutex_acquire(dev);
1346 
1347 	err = mt7921_mcu_set_bss_pm(dev, vif, false);
1348 	if (err)
1349 		goto out;
1350 
1351 	mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid, false,
1352 				    mvif->bss_conf.mt76.ctx);
1353 
1354 out:
1355 	mt792x_mutex_release(dev);
1356 }
1357 
1358 static int
mt7921_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)1359 mt7921_add_chanctx(struct ieee80211_hw *hw,
1360 		   struct ieee80211_chanctx_conf *ctx)
1361 {
1362 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1363 
1364 	dev->new_ctx = ctx;
1365 	return 0;
1366 }
1367 
1368 static void
mt7921_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)1369 mt7921_remove_chanctx(struct ieee80211_hw *hw,
1370 		      struct ieee80211_chanctx_conf *ctx)
1371 {
1372 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1373 
1374 	if (dev->new_ctx == ctx)
1375 		dev->new_ctx = NULL;
1376 }
1377 
1378 static void
mt7921_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)1379 mt7921_change_chanctx(struct ieee80211_hw *hw,
1380 		      struct ieee80211_chanctx_conf *ctx,
1381 		      u32 changed)
1382 {
1383 	struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv;
1384 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1385 	struct ieee80211_vif *vif;
1386 	struct mt792x_vif *mvif;
1387 
1388 	if (!mctx->bss_conf)
1389 		return;
1390 
1391 	mvif = container_of(mctx->bss_conf, struct mt792x_vif, bss_conf);
1392 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1393 
1394 	mt792x_mutex_acquire(phy->dev);
1395 	if (vif->type == NL80211_IFTYPE_MONITOR)
1396 		mt7921_mcu_config_sniffer(mvif, ctx);
1397 	else
1398 		mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76, ctx);
1399 	mt792x_mutex_release(phy->dev);
1400 }
1401 
mt7921_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)1402 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw,
1403 				  struct ieee80211_vif *vif,
1404 				  struct ieee80211_prep_tx_info *info)
1405 {
1406 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1407 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1408 	u16 duration = info->duration ? info->duration :
1409 		       jiffies_to_msecs(HZ);
1410 
1411 	mt792x_mutex_acquire(dev);
1412 	mt7921_set_roc(mvif->phy, mvif, mvif->bss_conf.mt76.ctx->def.chan, duration,
1413 		       MT7921_ROC_REQ_JOIN);
1414 	mt792x_mutex_release(dev);
1415 }
1416 
mt7921_mgd_complete_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)1417 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw,
1418 				   struct ieee80211_vif *vif,
1419 				   struct ieee80211_prep_tx_info *info)
1420 {
1421 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1422 
1423 	mt7921_abort_roc(mvif->phy, mvif);
1424 }
1425 
mt7921_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)1426 static int mt7921_switch_vif_chanctx(struct ieee80211_hw *hw,
1427 				     struct ieee80211_vif_chanctx_switch *vifs,
1428 				     int n_vifs,
1429 				     enum ieee80211_chanctx_switch_mode mode)
1430 {
1431 	return mt792x_assign_vif_chanctx(hw, vifs->vif, vifs->link_conf,
1432 					 vifs->new_ctx);
1433 }
1434 
mt7921_csa_work(struct work_struct * work)1435 void mt7921_csa_work(struct work_struct *work)
1436 {
1437 	struct mt792x_vif *mvif;
1438 	struct mt792x_dev *dev;
1439 	struct ieee80211_vif *vif;
1440 	int ret;
1441 
1442 	mvif = (struct mt792x_vif *)container_of(work, struct mt792x_vif,
1443 						csa_work);
1444 	dev = mvif->phy->dev;
1445 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1446 
1447 	mt792x_mutex_acquire(dev);
1448 	ret = mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76,
1449 					    dev->new_ctx);
1450 	mt792x_mutex_release(dev);
1451 
1452 	ieee80211_chswitch_done(vif, !ret, 0);
1453 }
1454 
mt7921_pre_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)1455 static int mt7921_pre_channel_switch(struct ieee80211_hw *hw,
1456 				     struct ieee80211_vif *vif,
1457 				     struct ieee80211_channel_switch *chsw)
1458 {
1459 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1460 		return -EOPNOTSUPP;
1461 
1462 	/* Avoid beacon loss due to the CAC(Channel Availability Check) time
1463 	 * of the AP.
1464 	 */
1465 	if (!cfg80211_chandef_usable(hw->wiphy, &chsw->chandef,
1466 				     IEEE80211_CHAN_RADAR))
1467 		return -EOPNOTSUPP;
1468 
1469 	return 0;
1470 }
1471 
mt7921_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)1472 static void mt7921_channel_switch(struct ieee80211_hw *hw,
1473 				  struct ieee80211_vif *vif,
1474 				  struct ieee80211_channel_switch *chsw)
1475 {
1476 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1477 	u16 beacon_interval = vif->bss_conf.beacon_int;
1478 
1479 	mvif->csa_timer.expires = TU_TO_EXP_TIME(beacon_interval * chsw->count);
1480 	add_timer(&mvif->csa_timer);
1481 }
1482 
mt7921_abort_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1483 static void mt7921_abort_channel_switch(struct ieee80211_hw *hw,
1484 					struct ieee80211_vif *vif,
1485 					struct ieee80211_bss_conf *link_conf)
1486 {
1487 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1488 
1489 	timer_delete_sync(&mvif->csa_timer);
1490 	cancel_work_sync(&mvif->csa_work);
1491 }
1492 
mt7921_channel_switch_rx_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)1493 static void mt7921_channel_switch_rx_beacon(struct ieee80211_hw *hw,
1494 					    struct ieee80211_vif *vif,
1495 					    struct ieee80211_channel_switch *chsw)
1496 {
1497 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
1498 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1499 	u16 beacon_interval = vif->bss_conf.beacon_int;
1500 
1501 	if (cfg80211_chandef_identical(&chsw->chandef,
1502 				       &dev->new_ctx->def) &&
1503 				       chsw->count) {
1504 		mod_timer(&mvif->csa_timer,
1505 			  TU_TO_EXP_TIME(beacon_interval * chsw->count));
1506 	}
1507 }
1508 
mt7921_rfkill_poll(struct ieee80211_hw * hw)1509 static void mt7921_rfkill_poll(struct ieee80211_hw *hw)
1510 {
1511 	struct mt792x_phy *phy = mt792x_hw_phy(hw);
1512 	int ret = 0;
1513 
1514 	mt792x_mutex_acquire(phy->dev);
1515 	ret = mt7921_mcu_wf_rf_pin_ctrl(phy, WF_RF_PIN_POLL);
1516 	mt792x_mutex_release(phy->dev);
1517 
1518 	wiphy_rfkill_set_hw_state(hw->wiphy, ret ? false : true);
1519 }
1520 
1521 const struct ieee80211_ops mt7921_ops = {
1522 	.tx = mt792x_tx,
1523 	.start = mt7921_start,
1524 	.stop = mt7921_stop,
1525 	.add_interface = mt7921_add_interface,
1526 	.remove_interface = mt792x_remove_interface,
1527 	.config = mt7921_config,
1528 	.conf_tx = mt792x_conf_tx,
1529 	.configure_filter = mt7921_configure_filter,
1530 	.bss_info_changed = mt7921_bss_info_changed,
1531 	.start_ap = mt7921_start_ap,
1532 	.stop_ap = mt7921_stop_ap,
1533 	.sta_state = mt7921_sta_state,
1534 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1535 	.set_key = mt7921_set_key,
1536 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1537 #if IS_ENABLED(CONFIG_IPV6)
1538 	.ipv6_addr_change = mt7921_ipv6_addr_change,
1539 #endif /* CONFIG_IPV6 */
1540 	.ampdu_action = mt7921_ampdu_action,
1541 	.set_rts_threshold = mt7921_set_rts_threshold,
1542 	.wake_tx_queue = mt76_wake_tx_queue,
1543 	.release_buffered_frames = mt76_release_buffered_frames,
1544 	.channel_switch_beacon = mt7921_channel_switch_beacon,
1545 	.get_txpower = mt76_get_txpower,
1546 	.get_stats = mt792x_get_stats,
1547 	.get_et_sset_count = mt792x_get_et_sset_count,
1548 	.get_et_strings = mt792x_get_et_strings,
1549 	.get_et_stats = mt792x_get_et_stats,
1550 	.get_tsf = mt792x_get_tsf,
1551 	.set_tsf = mt792x_set_tsf,
1552 	.get_survey = mt76_get_survey,
1553 	.get_antenna = mt76_get_antenna,
1554 	.set_antenna = mt7921_set_antenna,
1555 	.set_coverage_class = mt792x_set_coverage_class,
1556 	.hw_scan = mt7921_hw_scan,
1557 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1558 	.sta_statistics = mt792x_sta_statistics,
1559 	.sched_scan_start = mt7921_start_sched_scan,
1560 	.sched_scan_stop = mt7921_stop_sched_scan,
1561 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1562 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1563 #ifdef CONFIG_PM
1564 	.suspend = mt7921_suspend,
1565 	.resume = mt7921_resume,
1566 	.set_wakeup = mt792x_set_wakeup,
1567 	.set_rekey_data = mt7921_set_rekey_data,
1568 #endif /* CONFIG_PM */
1569 	.flush = mt792x_flush,
1570 	.set_sar_specs = mt7921_set_sar_specs,
1571 	.rfkill_poll = mt7921_rfkill_poll,
1572 	.remain_on_channel = mt7921_remain_on_channel,
1573 	.cancel_remain_on_channel = mt7921_cancel_remain_on_channel,
1574 	.add_chanctx = mt7921_add_chanctx,
1575 	.remove_chanctx = mt7921_remove_chanctx,
1576 	.change_chanctx = mt7921_change_chanctx,
1577 	.assign_vif_chanctx = mt792x_assign_vif_chanctx,
1578 	.unassign_vif_chanctx = mt792x_unassign_vif_chanctx,
1579 	.mgd_prepare_tx = mt7921_mgd_prepare_tx,
1580 	.mgd_complete_tx = mt7921_mgd_complete_tx,
1581 	.switch_vif_chanctx = mt7921_switch_vif_chanctx,
1582 	.pre_channel_switch = mt7921_pre_channel_switch,
1583 	.channel_switch = mt7921_channel_switch,
1584 	.abort_channel_switch = mt7921_abort_channel_switch,
1585 	.channel_switch_rx_beacon = mt7921_channel_switch_rx_beacon,
1586 };
1587 EXPORT_SYMBOL_GPL(mt7921_ops);
1588 
1589 MODULE_DESCRIPTION("MediaTek MT7921 core driver");
1590 MODULE_LICENSE("Dual BSD/GPL");
1591 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1592