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