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 "mt7921.h"
9 #include "mcu.h"
10 
11 static void
12 mt7921_gen_ppe_thresh(u8 *he_ppet, int nss)
13 {
14 	u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
15 	u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
16 
17 	he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
18 		     FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
19 				ru_bit_mask);
20 
21 	ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
22 		    nss * hweight8(ru_bit_mask) * 2;
23 	ppet_size = DIV_ROUND_UP(ppet_bits, 8);
24 
25 	for (i = 0; i < ppet_size - 1; i++)
26 		he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
27 
28 	he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
29 			 (0xff >> (8 - (ppet_bits - 1) % 8));
30 }
31 
32 static int
33 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
34 		    struct ieee80211_sband_iftype_data *data)
35 {
36 	int i, idx = 0;
37 	int nss = hweight8(phy->mt76->chainmask);
38 	u16 mcs_map = 0;
39 
40 	for (i = 0; i < 8; i++) {
41 		if (i < nss)
42 			mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
43 		else
44 			mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
45 	}
46 
47 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
48 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
49 		struct ieee80211_he_cap_elem *he_cap_elem =
50 				&he_cap->he_cap_elem;
51 		struct ieee80211_he_mcs_nss_supp *he_mcs =
52 				&he_cap->he_mcs_nss_supp;
53 
54 		switch (i) {
55 		case NL80211_IFTYPE_STATION:
56 			break;
57 		default:
58 			continue;
59 		}
60 
61 		data[idx].types_mask = BIT(i);
62 		he_cap->has_he = true;
63 
64 		he_cap_elem->mac_cap_info[0] =
65 			IEEE80211_HE_MAC_CAP0_HTC_HE;
66 		he_cap_elem->mac_cap_info[3] =
67 			IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
68 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
69 		he_cap_elem->mac_cap_info[4] =
70 			IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
71 
72 		if (band == NL80211_BAND_2GHZ)
73 			he_cap_elem->phy_cap_info[0] =
74 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
75 		else
76 			he_cap_elem->phy_cap_info[0] =
77 				IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
78 
79 		he_cap_elem->phy_cap_info[1] =
80 			IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
81 		he_cap_elem->phy_cap_info[2] =
82 			IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US |
83 			IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
84 			IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ |
85 			IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
86 			IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
87 
88 		switch (i) {
89 		case NL80211_IFTYPE_STATION:
90 			he_cap_elem->mac_cap_info[1] |=
91 				IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
92 
93 			if (band == NL80211_BAND_2GHZ)
94 				he_cap_elem->phy_cap_info[0] |=
95 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
96 			else
97 				he_cap_elem->phy_cap_info[0] |=
98 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
99 
100 			he_cap_elem->phy_cap_info[1] |=
101 				IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
102 				IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
103 			he_cap_elem->phy_cap_info[3] |=
104 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
105 				IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
106 			he_cap_elem->phy_cap_info[4] |=
107 				IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE |
108 				IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4;
109 			he_cap_elem->phy_cap_info[5] |=
110 				IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK |
111 				IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
112 			he_cap_elem->phy_cap_info[6] |=
113 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU |
114 				IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
115 				IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
116 				IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
117 				IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
118 			he_cap_elem->phy_cap_info[7] |=
119 				IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
120 				IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
121 			he_cap_elem->phy_cap_info[8] |=
122 				IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
123 				IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
124 			he_cap_elem->phy_cap_info[9] |=
125 				IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
126 				IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
127 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
128 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
129 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
130 				IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
131 
132 			if (is_mt7922(phy->mt76->dev)) {
133 				he_cap_elem->phy_cap_info[0] |=
134 					IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
135 				he_cap_elem->phy_cap_info[8] |=
136 					IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
137 					IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
138 			}
139 			break;
140 		}
141 
142 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
143 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
144 		if (is_mt7922(phy->mt76->dev)) {
145 			he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map);
146 			he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map);
147 		}
148 
149 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
150 		if (he_cap_elem->phy_cap_info[6] &
151 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
152 			mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
153 		} else {
154 			he_cap_elem->phy_cap_info[9] |=
155 				u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US,
156 					       IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
157 		}
158 
159 		if (band == NL80211_BAND_6GHZ) {
160 			struct ieee80211_supported_band *sband =
161 				&phy->mt76->sband_5g.sband;
162 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
163 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
164 			u32 exp;
165 			u16 cap;
166 
167 			cap = u16_encode_bits(ht_cap->ampdu_density,
168 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
169 			exp = u32_get_bits(vht_cap->cap,
170 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
171 			cap |= u16_encode_bits(exp,
172 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
173 			exp = u32_get_bits(vht_cap->cap,
174 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
175 			cap |= u16_encode_bits(exp,
176 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
177 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
178 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
179 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
180 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
181 
182 			data[idx].he_6ghz_capa.capa = cpu_to_le16(cap);
183 		}
184 		idx++;
185 	}
186 
187 	return idx;
188 }
189 
190 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
191 {
192 	struct ieee80211_sband_iftype_data *data;
193 	struct ieee80211_supported_band *band;
194 	int n;
195 
196 	if (phy->mt76->cap.has_2ghz) {
197 		data = phy->iftype[NL80211_BAND_2GHZ];
198 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
199 
200 		band = &phy->mt76->sband_2g.sband;
201 		band->iftype_data = data;
202 		band->n_iftype_data = n;
203 	}
204 
205 	if (phy->mt76->cap.has_5ghz) {
206 		data = phy->iftype[NL80211_BAND_5GHZ];
207 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
208 
209 		band = &phy->mt76->sband_5g.sband;
210 		band->iftype_data = data;
211 		band->n_iftype_data = n;
212 
213 		if (phy->mt76->cap.has_6ghz) {
214 			data = phy->iftype[NL80211_BAND_6GHZ];
215 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
216 
217 			band = &phy->mt76->sband_6g.sband;
218 			band->iftype_data = data;
219 			band->n_iftype_data = n;
220 		}
221 	}
222 }
223 
224 int __mt7921_start(struct mt7921_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 = mt76_connac_mcu_set_rate_txpower(phy->mt76);
242 	if (err)
243 		return err;
244 
245 	mt7921_mac_reset_counters(phy);
246 	set_bit(MT76_STATE_RUNNING, &mphy->state);
247 
248 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
249 				     MT7921_WATCHDOG_TIME);
250 
251 	return 0;
252 }
253 EXPORT_SYMBOL_GPL(__mt7921_start);
254 
255 static int mt7921_start(struct ieee80211_hw *hw)
256 {
257 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
258 	int err;
259 
260 	mt7921_mutex_acquire(phy->dev);
261 	err = __mt7921_start(phy);
262 	mt7921_mutex_release(phy->dev);
263 
264 	return err;
265 }
266 
267 static void mt7921_stop(struct ieee80211_hw *hw)
268 {
269 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
270 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
271 
272 	cancel_delayed_work_sync(&phy->mt76->mac_work);
273 
274 	cancel_delayed_work_sync(&dev->pm.ps_work);
275 	cancel_work_sync(&dev->pm.wake_work);
276 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
277 
278 	mt7921_mutex_acquire(dev);
279 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
280 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
281 	mt7921_mutex_release(dev);
282 }
283 
284 static int mt7921_add_interface(struct ieee80211_hw *hw,
285 				struct ieee80211_vif *vif)
286 {
287 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
288 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
289 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
290 	struct mt76_txq *mtxq;
291 	int idx, ret = 0;
292 
293 	mt7921_mutex_acquire(dev);
294 
295 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
296 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
297 		ret = -ENOSPC;
298 		goto out;
299 	}
300 
301 	mvif->mt76.omac_idx = mvif->mt76.idx;
302 	mvif->phy = phy;
303 	mvif->mt76.band_idx = 0;
304 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
305 
306 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
307 					  true);
308 	if (ret)
309 		goto out;
310 
311 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
312 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
313 
314 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
315 
316 	INIT_LIST_HEAD(&mvif->sta.poll_list);
317 	mvif->sta.wcid.idx = idx;
318 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
319 	mvif->sta.wcid.hw_key_idx = -1;
320 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
321 	mt76_packet_id_init(&mvif->sta.wcid);
322 
323 	mt7921_mac_wtbl_update(dev, idx,
324 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
325 
326 	ewma_rssi_init(&mvif->rssi);
327 
328 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
329 	if (vif->txq) {
330 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
331 		mtxq->wcid = &mvif->sta.wcid;
332 	}
333 
334 out:
335 	mt7921_mutex_release(dev);
336 
337 	return ret;
338 }
339 
340 static void mt7921_remove_interface(struct ieee80211_hw *hw,
341 				    struct ieee80211_vif *vif)
342 {
343 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
344 	struct mt7921_sta *msta = &mvif->sta;
345 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
346 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
347 	int idx = msta->wcid.idx;
348 
349 	mt7921_mutex_acquire(dev);
350 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
351 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
352 
353 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
354 
355 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
356 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
357 	mt7921_mutex_release(dev);
358 
359 	spin_lock_bh(&dev->sta_poll_lock);
360 	if (!list_empty(&msta->poll_list))
361 		list_del_init(&msta->poll_list);
362 	spin_unlock_bh(&dev->sta_poll_lock);
363 
364 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
365 }
366 
367 static int mt7921_set_channel(struct mt7921_phy *phy)
368 {
369 	struct mt7921_dev *dev = phy->dev;
370 	int ret;
371 
372 	cancel_delayed_work_sync(&phy->mt76->mac_work);
373 
374 	mt7921_mutex_acquire(dev);
375 	set_bit(MT76_RESET, &phy->mt76->state);
376 
377 	mt76_set_channel(phy->mt76);
378 
379 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
380 	if (ret)
381 		goto out;
382 
383 	mt7921_mac_set_timing(phy);
384 
385 	mt7921_mac_reset_counters(phy);
386 	phy->noise = 0;
387 
388 out:
389 	clear_bit(MT76_RESET, &phy->mt76->state);
390 	mt7921_mutex_release(dev);
391 
392 	mt76_worker_schedule(&dev->mt76.tx_worker);
393 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
394 				     MT7921_WATCHDOG_TIME);
395 
396 	return ret;
397 }
398 
399 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
400 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
401 			  struct ieee80211_key_conf *key)
402 {
403 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
404 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
405 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
406 				  &mvif->sta;
407 	struct mt76_wcid *wcid = &msta->wcid;
408 	u8 *wcid_keyidx = &wcid->hw_key_idx;
409 	int idx = key->keyidx, err = 0;
410 
411 	/* The hardware does not support per-STA RX GTK, fallback
412 	 * to software mode for these.
413 	 */
414 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
415 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
416 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
417 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
418 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
419 		return -EOPNOTSUPP;
420 
421 	/* fall back to sw encryption for unsupported ciphers */
422 	switch (key->cipher) {
423 	case WLAN_CIPHER_SUITE_AES_CMAC:
424 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
425 		wcid_keyidx = &wcid->hw_key_idx2;
426 		break;
427 	case WLAN_CIPHER_SUITE_WEP40:
428 	case WLAN_CIPHER_SUITE_WEP104:
429 		if (!mvif->wep_sta)
430 			return -EOPNOTSUPP;
431 		break;
432 	case WLAN_CIPHER_SUITE_TKIP:
433 	case WLAN_CIPHER_SUITE_CCMP:
434 	case WLAN_CIPHER_SUITE_CCMP_256:
435 	case WLAN_CIPHER_SUITE_GCMP:
436 	case WLAN_CIPHER_SUITE_GCMP_256:
437 	case WLAN_CIPHER_SUITE_SMS4:
438 		break;
439 	default:
440 		return -EOPNOTSUPP;
441 	}
442 
443 	mt7921_mutex_acquire(dev);
444 
445 	if (cmd == SET_KEY)
446 		*wcid_keyidx = idx;
447 	else if (idx == *wcid_keyidx)
448 		*wcid_keyidx = -1;
449 	else
450 		goto out;
451 
452 	mt76_wcid_key_setup(&dev->mt76, wcid,
453 			    cmd == SET_KEY ? key : NULL);
454 
455 	err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
456 	if (err)
457 		goto out;
458 
459 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
460 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
461 		err = mt7921_mcu_add_key(dev, vif, mvif->wep_sta, key, cmd);
462 out:
463 	mt7921_mutex_release(dev);
464 
465 	return err;
466 }
467 
468 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
469 {
470 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
471 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
472 	int ret = 0;
473 
474 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
475 		ieee80211_stop_queues(hw);
476 		ret = mt7921_set_channel(phy);
477 		if (ret)
478 			return ret;
479 		ieee80211_wake_queues(hw);
480 	}
481 
482 	mt7921_mutex_acquire(dev);
483 
484 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
485 		ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
486 		if (ret)
487 			goto out;
488 	}
489 
490 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
491 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
492 
493 		if (!enabled)
494 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
495 		else
496 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
497 
498 		mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
499 			       enabled);
500 		mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
501 	}
502 
503 out:
504 	mt7921_mutex_release(dev);
505 
506 	return ret;
507 }
508 
509 static int
510 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
511 	       const struct ieee80211_tx_queue_params *params)
512 {
513 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
514 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
515 
516 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
517 	queue = mt7921_lmac_mapping(dev, queue);
518 	mvif->queue_params[queue] = *params;
519 
520 	return 0;
521 }
522 
523 static void mt7921_configure_filter(struct ieee80211_hw *hw,
524 				    unsigned int changed_flags,
525 				    unsigned int *total_flags,
526 				    u64 multicast)
527 {
528 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
529 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
530 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
531 			MT_WF_RFCR1_DROP_BF_POLL |
532 			MT_WF_RFCR1_DROP_BA |
533 			MT_WF_RFCR1_DROP_CFEND |
534 			MT_WF_RFCR1_DROP_CFACK;
535 	u32 flags = 0;
536 
537 #define MT76_FILTER(_flag, _hw) do {					\
538 		flags |= *total_flags & FIF_##_flag;			\
539 		phy->rxfilter &= ~(_hw);				\
540 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
541 	} while (0)
542 
543 	mt7921_mutex_acquire(dev);
544 
545 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
546 			   MT_WF_RFCR_DROP_OTHER_BEACON |
547 			   MT_WF_RFCR_DROP_FRAME_REPORT |
548 			   MT_WF_RFCR_DROP_PROBEREQ |
549 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
550 			   MT_WF_RFCR_DROP_MCAST |
551 			   MT_WF_RFCR_DROP_BCAST |
552 			   MT_WF_RFCR_DROP_DUPLICATE |
553 			   MT_WF_RFCR_DROP_A2_BSSID |
554 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
555 			   MT_WF_RFCR_DROP_STBC_MULTI);
556 
557 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
558 			       MT_WF_RFCR_DROP_A3_MAC |
559 			       MT_WF_RFCR_DROP_A3_BSSID);
560 
561 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
562 
563 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
564 			     MT_WF_RFCR_DROP_RTS |
565 			     MT_WF_RFCR_DROP_CTL_RSV |
566 			     MT_WF_RFCR_DROP_NDPA);
567 
568 	*total_flags = flags;
569 	mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
570 
571 	if (*total_flags & FIF_CONTROL)
572 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
573 	else
574 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
575 
576 	mt7921_mutex_release(dev);
577 }
578 
579 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
580 				    struct ieee80211_vif *vif,
581 				    struct ieee80211_bss_conf *info,
582 				    u32 changed)
583 {
584 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
585 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
586 
587 	mt7921_mutex_acquire(dev);
588 
589 	if (changed & BSS_CHANGED_ERP_SLOT) {
590 		int slottime = info->use_short_slot ? 9 : 20;
591 
592 		if (slottime != phy->slottime) {
593 			phy->slottime = slottime;
594 			mt7921_mac_set_timing(phy);
595 		}
596 	}
597 
598 	/* ensure that enable txcmd_mode after bss_info */
599 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
600 		mt7921_mcu_set_tx(dev, vif);
601 
602 	if (changed & BSS_CHANGED_PS)
603 		mt7921_mcu_uni_bss_ps(dev, vif);
604 
605 	if (changed & BSS_CHANGED_ASSOC) {
606 		mt7921_mcu_sta_update(dev, NULL, vif, true,
607 				      MT76_STA_INFO_STATE_ASSOC);
608 		if (dev->pm.enable)
609 			mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
610 	}
611 
612 	if (changed & BSS_CHANGED_ARP_FILTER) {
613 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
614 
615 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
616 						  info);
617 	}
618 
619 	mt7921_mutex_release(dev);
620 }
621 
622 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
623 		       struct ieee80211_sta *sta)
624 {
625 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
626 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
627 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
628 	int ret, idx;
629 
630 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
631 	if (idx < 0)
632 		return -ENOSPC;
633 
634 	INIT_LIST_HEAD(&msta->poll_list);
635 	msta->vif = mvif;
636 	msta->wcid.sta = 1;
637 	msta->wcid.idx = idx;
638 	msta->wcid.ext_phy = mvif->mt76.band_idx;
639 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
640 	msta->last_txs = jiffies;
641 
642 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
643 	if (ret)
644 		return ret;
645 
646 	if (vif->type == NL80211_IFTYPE_STATION)
647 		mvif->wep_sta = msta;
648 
649 	mt7921_mac_wtbl_update(dev, idx,
650 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
651 
652 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
653 				    MT76_STA_INFO_STATE_NONE);
654 	if (ret)
655 		return ret;
656 
657 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
658 
659 	return 0;
660 }
661 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add);
662 
663 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
664 			  struct ieee80211_sta *sta)
665 {
666 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
667 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
668 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
669 
670 	mt7921_mutex_acquire(dev);
671 
672 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
673 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
674 					    true);
675 
676 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
677 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
678 
679 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
680 
681 	mt7921_mutex_release(dev);
682 }
683 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc);
684 
685 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
686 			   struct ieee80211_sta *sta)
687 {
688 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
689 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
690 
691 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
692 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
693 
694 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
695 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
696 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
697 
698 	if (vif->type == NL80211_IFTYPE_STATION) {
699 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
700 
701 		mvif->wep_sta = NULL;
702 		ewma_rssi_init(&mvif->rssi);
703 		if (!sta->tdls)
704 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
705 						    &mvif->sta.wcid, false);
706 	}
707 
708 	spin_lock_bh(&dev->sta_poll_lock);
709 	if (!list_empty(&msta->poll_list))
710 		list_del_init(&msta->poll_list);
711 	spin_unlock_bh(&dev->sta_poll_lock);
712 
713 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
714 }
715 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove);
716 
717 void mt7921_tx_worker(struct mt76_worker *w)
718 {
719 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
720 					      mt76.tx_worker);
721 
722 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
723 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
724 		return;
725 	}
726 
727 	mt76_txq_schedule_all(&dev->mphy);
728 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
729 }
730 
731 static void mt7921_tx(struct ieee80211_hw *hw,
732 		      struct ieee80211_tx_control *control,
733 		      struct sk_buff *skb)
734 {
735 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
736 	struct mt76_phy *mphy = hw->priv;
737 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
738 	struct ieee80211_vif *vif = info->control.vif;
739 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
740 	int qid;
741 
742 	if (control->sta) {
743 		struct mt7921_sta *sta;
744 
745 		sta = (struct mt7921_sta *)control->sta->drv_priv;
746 		wcid = &sta->wcid;
747 	}
748 
749 	if (vif && !control->sta) {
750 		struct mt7921_vif *mvif;
751 
752 		mvif = (struct mt7921_vif *)vif->drv_priv;
753 		wcid = &mvif->sta.wcid;
754 	}
755 
756 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
757 		mt76_tx(mphy, control->sta, wcid, skb);
758 		mt76_connac_pm_unref(mphy, &dev->pm);
759 		return;
760 	}
761 
762 	qid = skb_get_queue_mapping(skb);
763 	if (qid >= MT_TXQ_PSD) {
764 		qid = IEEE80211_AC_BE;
765 		skb_set_queue_mapping(skb, qid);
766 	}
767 
768 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
769 }
770 
771 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
772 {
773 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
774 
775 	mt7921_mutex_acquire(dev);
776 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
777 	mt7921_mutex_release(dev);
778 
779 	return 0;
780 }
781 
782 static int
783 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
784 		    struct ieee80211_ampdu_params *params)
785 {
786 	enum ieee80211_ampdu_mlme_action action = params->action;
787 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
788 	struct ieee80211_sta *sta = params->sta;
789 	struct ieee80211_txq *txq = sta->txq[params->tid];
790 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
791 	u16 tid = params->tid;
792 	u16 ssn = params->ssn;
793 	struct mt76_txq *mtxq;
794 	int ret = 0;
795 
796 	if (!txq)
797 		return -EINVAL;
798 
799 	mtxq = (struct mt76_txq *)txq->drv_priv;
800 
801 	mt7921_mutex_acquire(dev);
802 	switch (action) {
803 	case IEEE80211_AMPDU_RX_START:
804 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
805 				   params->buf_size);
806 		mt7921_mcu_uni_rx_ba(dev, params, true);
807 		break;
808 	case IEEE80211_AMPDU_RX_STOP:
809 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
810 		mt7921_mcu_uni_rx_ba(dev, params, false);
811 		break;
812 	case IEEE80211_AMPDU_TX_OPERATIONAL:
813 		mtxq->aggr = true;
814 		mtxq->send_bar = false;
815 		mt7921_mcu_uni_tx_ba(dev, params, true);
816 		break;
817 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
818 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
819 		mtxq->aggr = false;
820 		clear_bit(tid, &msta->ampdu_state);
821 		mt7921_mcu_uni_tx_ba(dev, params, false);
822 		break;
823 	case IEEE80211_AMPDU_TX_START:
824 		set_bit(tid, &msta->ampdu_state);
825 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
826 		break;
827 	case IEEE80211_AMPDU_TX_STOP_CONT:
828 		mtxq->aggr = false;
829 		clear_bit(tid, &msta->ampdu_state);
830 		mt7921_mcu_uni_tx_ba(dev, params, false);
831 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
832 		break;
833 	}
834 	mt7921_mutex_release(dev);
835 
836 	return ret;
837 }
838 
839 static int mt7921_sta_state(struct ieee80211_hw *hw,
840 			    struct ieee80211_vif *vif,
841 			    struct ieee80211_sta *sta,
842 			    enum ieee80211_sta_state old_state,
843 			    enum ieee80211_sta_state new_state)
844 {
845 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
846 
847 	if (dev->pm.ds_enable) {
848 		mt7921_mutex_acquire(dev);
849 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
850 		mt7921_mutex_release(dev);
851 	}
852 
853 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
854 }
855 
856 static int
857 mt7921_get_stats(struct ieee80211_hw *hw,
858 		 struct ieee80211_low_level_stats *stats)
859 {
860 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
861 	struct mib_stats *mib = &phy->mib;
862 
863 	mt7921_mutex_acquire(phy->dev);
864 
865 	stats->dot11RTSSuccessCount = mib->rts_cnt;
866 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
867 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
868 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
869 
870 	mt7921_mutex_release(phy->dev);
871 
872 	return 0;
873 }
874 
875 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = {
876 	/* tx counters */
877 	"tx_ampdu_cnt",
878 	"tx_mpdu_attempts",
879 	"tx_mpdu_success",
880 	"tx_pkt_ebf_cnt",
881 	"tx_pkt_ibf_cnt",
882 	"tx_ampdu_len:0-1",
883 	"tx_ampdu_len:2-10",
884 	"tx_ampdu_len:11-19",
885 	"tx_ampdu_len:20-28",
886 	"tx_ampdu_len:29-37",
887 	"tx_ampdu_len:38-46",
888 	"tx_ampdu_len:47-55",
889 	"tx_ampdu_len:56-79",
890 	"tx_ampdu_len:80-103",
891 	"tx_ampdu_len:104-127",
892 	"tx_ampdu_len:128-151",
893 	"tx_ampdu_len:152-175",
894 	"tx_ampdu_len:176-199",
895 	"tx_ampdu_len:200-223",
896 	"tx_ampdu_len:224-247",
897 	"ba_miss_count",
898 	"tx_beamformer_ppdu_iBF",
899 	"tx_beamformer_ppdu_eBF",
900 	"tx_beamformer_rx_feedback_all",
901 	"tx_beamformer_rx_feedback_he",
902 	"tx_beamformer_rx_feedback_vht",
903 	"tx_beamformer_rx_feedback_ht",
904 	"tx_msdu_pack_1",
905 	"tx_msdu_pack_2",
906 	"tx_msdu_pack_3",
907 	"tx_msdu_pack_4",
908 	"tx_msdu_pack_5",
909 	"tx_msdu_pack_6",
910 	"tx_msdu_pack_7",
911 	"tx_msdu_pack_8",
912 	/* rx counters */
913 	"rx_mpdu_cnt",
914 	"rx_ampdu_cnt",
915 	"rx_ampdu_bytes_cnt",
916 	"rx_ba_cnt",
917 	/* per vif counters */
918 	"v_tx_mode_cck",
919 	"v_tx_mode_ofdm",
920 	"v_tx_mode_ht",
921 	"v_tx_mode_ht_gf",
922 	"v_tx_mode_vht",
923 	"v_tx_mode_he_su",
924 	"v_tx_mode_he_ext_su",
925 	"v_tx_mode_he_tb",
926 	"v_tx_mode_he_mu",
927 	"v_tx_bw_20",
928 	"v_tx_bw_40",
929 	"v_tx_bw_80",
930 	"v_tx_bw_160",
931 	"v_tx_mcs_0",
932 	"v_tx_mcs_1",
933 	"v_tx_mcs_2",
934 	"v_tx_mcs_3",
935 	"v_tx_mcs_4",
936 	"v_tx_mcs_5",
937 	"v_tx_mcs_6",
938 	"v_tx_mcs_7",
939 	"v_tx_mcs_8",
940 	"v_tx_mcs_9",
941 	"v_tx_mcs_10",
942 	"v_tx_mcs_11",
943 };
944 
945 static void
946 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
947 		      u32 sset, u8 *data)
948 {
949 	if (sset != ETH_SS_STATS)
950 		return;
951 
952 	memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats));
953 }
954 
955 static int
956 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
957 			 int sset)
958 {
959 	return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0;
960 }
961 
962 static void
963 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
964 {
965 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
966 	struct mt76_ethtool_worker_info *wi = wi_data;
967 
968 	if (msta->vif->mt76.idx != wi->idx)
969 		return;
970 
971 	mt76_ethtool_worker(wi, &msta->stats);
972 }
973 
974 static
975 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
976 			 struct ethtool_stats *stats, u64 *data)
977 {
978 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
979 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
980 	struct mt7921_dev *dev = phy->dev;
981 	struct mib_stats *mib = &phy->mib;
982 	struct mt76_ethtool_worker_info wi = {
983 		.data = data,
984 		.idx = mvif->mt76.idx,
985 	};
986 	int i, ei = 0;
987 
988 	mt7921_mutex_acquire(dev);
989 
990 	mt7921_mac_update_mib_stats(phy);
991 
992 	data[ei++] = mib->tx_ampdu_cnt;
993 	data[ei++] = mib->tx_mpdu_attempts_cnt;
994 	data[ei++] = mib->tx_mpdu_success_cnt;
995 	data[ei++] = mib->tx_pkt_ebf_cnt;
996 	data[ei++] = mib->tx_pkt_ibf_cnt;
997 
998 	/* Tx ampdu stat */
999 	for (i = 0; i < 15; i++)
1000 		data[ei++] = dev->mt76.aggr_stats[i];
1001 
1002 	data[ei++] = phy->mib.ba_miss_cnt;
1003 
1004 	/* Tx Beamformer monitor */
1005 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1006 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1007 
1008 	/* Tx Beamformer Rx feedback monitor */
1009 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1010 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1011 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1012 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1013 
1014 	/* Tx amsdu info (pack-count histogram) */
1015 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1016 		data[ei++] = mib->tx_amsdu[i];
1017 
1018 	/* rx counters */
1019 	data[ei++] = mib->rx_mpdu_cnt;
1020 	data[ei++] = mib->rx_ampdu_cnt;
1021 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1022 	data[ei++] = mib->rx_ba_cnt;
1023 
1024 	/* Add values for all stations owned by this vif */
1025 	wi.initial_stat_idx = ei;
1026 	ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi);
1027 
1028 	mt7921_mutex_release(dev);
1029 
1030 	if (!wi.sta_count)
1031 		return;
1032 
1033 	ei += wi.worker_stat_count;
1034 	if (ei != ARRAY_SIZE(mt7921_gstrings_stats))
1035 		dev_err(dev->mt76.dev, "ei: %d  SSTATS_LEN: %zu",
1036 			ei, ARRAY_SIZE(mt7921_gstrings_stats));
1037 }
1038 
1039 static u64
1040 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1041 {
1042 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1043 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1044 	u8 omac_idx = mvif->mt76.omac_idx;
1045 	union {
1046 		u64 t64;
1047 		u32 t32[2];
1048 	} tsf;
1049 	u16 n;
1050 
1051 	mt7921_mutex_acquire(dev);
1052 
1053 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1054 	/* TSF software read */
1055 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
1056 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
1057 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
1058 
1059 	mt7921_mutex_release(dev);
1060 
1061 	return tsf.t64;
1062 }
1063 
1064 static void
1065 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1066 	       u64 timestamp)
1067 {
1068 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
1069 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1070 	u8 omac_idx = mvif->mt76.omac_idx;
1071 	union {
1072 		u64 t64;
1073 		u32 t32[2];
1074 	} tsf = { .t64 = timestamp, };
1075 	u16 n;
1076 
1077 	mt7921_mutex_acquire(dev);
1078 
1079 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
1080 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
1081 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
1082 	/* TSF software overwrite */
1083 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
1084 
1085 	mt7921_mutex_release(dev);
1086 }
1087 
1088 static void
1089 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
1090 {
1091 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1092 	struct mt7921_dev *dev = phy->dev;
1093 
1094 	mt7921_mutex_acquire(dev);
1095 	phy->coverage_class = max_t(s16, coverage_class, 0);
1096 	mt7921_mac_set_timing(phy);
1097 	mt7921_mutex_release(dev);
1098 }
1099 
1100 void mt7921_scan_work(struct work_struct *work)
1101 {
1102 	struct mt7921_phy *phy;
1103 
1104 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
1105 						scan_work.work);
1106 
1107 	while (true) {
1108 		struct mt7921_mcu_rxd *rxd;
1109 		struct sk_buff *skb;
1110 
1111 		spin_lock_bh(&phy->dev->mt76.lock);
1112 		skb = __skb_dequeue(&phy->scan_event_list);
1113 		spin_unlock_bh(&phy->dev->mt76.lock);
1114 
1115 		if (!skb)
1116 			break;
1117 
1118 		rxd = (struct mt7921_mcu_rxd *)skb->data;
1119 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1120 			ieee80211_sched_scan_results(phy->mt76->hw);
1121 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
1122 					      &phy->mt76->state)) {
1123 			struct cfg80211_scan_info info = {
1124 				.aborted = false,
1125 			};
1126 
1127 			ieee80211_scan_completed(phy->mt76->hw, &info);
1128 		}
1129 		dev_kfree_skb(skb);
1130 	}
1131 }
1132 
1133 static int
1134 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1135 	       struct ieee80211_scan_request *req)
1136 {
1137 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1138 	struct mt76_phy *mphy = hw->priv;
1139 	int err;
1140 
1141 	mt7921_mutex_acquire(dev);
1142 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1143 	mt7921_mutex_release(dev);
1144 
1145 	return err;
1146 }
1147 
1148 static void
1149 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1150 {
1151 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1152 	struct mt76_phy *mphy = hw->priv;
1153 
1154 	mt7921_mutex_acquire(dev);
1155 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1156 	mt7921_mutex_release(dev);
1157 }
1158 
1159 static int
1160 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1161 			struct cfg80211_sched_scan_request *req,
1162 			struct ieee80211_scan_ies *ies)
1163 {
1164 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1165 	struct mt76_phy *mphy = hw->priv;
1166 	int err;
1167 
1168 	mt7921_mutex_acquire(dev);
1169 
1170 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1171 	if (err < 0)
1172 		goto out;
1173 
1174 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1175 out:
1176 	mt7921_mutex_release(dev);
1177 
1178 	return err;
1179 }
1180 
1181 static int
1182 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1183 {
1184 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1185 	struct mt76_phy *mphy = hw->priv;
1186 	int err;
1187 
1188 	mt7921_mutex_acquire(dev);
1189 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1190 	mt7921_mutex_release(dev);
1191 
1192 	return err;
1193 }
1194 
1195 static int
1196 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1197 {
1198 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1199 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1200 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1201 
1202 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1203 		return -EINVAL;
1204 
1205 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1206 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1207 
1208 	mt7921_mutex_acquire(dev);
1209 
1210 	phy->mt76->antenna_mask = tx_ant;
1211 	phy->mt76->chainmask = tx_ant;
1212 
1213 	mt76_set_stream_caps(phy->mt76, true);
1214 	mt7921_set_stream_he_caps(phy);
1215 
1216 	mt7921_mutex_release(dev);
1217 
1218 	return 0;
1219 }
1220 
1221 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1222 				  struct ieee80211_vif *vif,
1223 				  struct ieee80211_sta *sta,
1224 				  struct station_info *sinfo)
1225 {
1226 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1227 	struct rate_info *txrate = &msta->wcid.rate;
1228 
1229 	if (!txrate->legacy && !txrate->flags)
1230 		return;
1231 
1232 	if (txrate->legacy) {
1233 		sinfo->txrate.legacy = txrate->legacy;
1234 	} else {
1235 		sinfo->txrate.mcs = txrate->mcs;
1236 		sinfo->txrate.nss = txrate->nss;
1237 		sinfo->txrate.bw = txrate->bw;
1238 		sinfo->txrate.he_gi = txrate->he_gi;
1239 		sinfo->txrate.he_dcm = txrate->he_dcm;
1240 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1241 	}
1242 	sinfo->txrate.flags = txrate->flags;
1243 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1244 }
1245 
1246 #ifdef CONFIG_PM
1247 static int mt7921_suspend(struct ieee80211_hw *hw,
1248 			  struct cfg80211_wowlan *wowlan)
1249 {
1250 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1251 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1252 
1253 	cancel_delayed_work_sync(&phy->scan_work);
1254 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1255 
1256 	cancel_delayed_work_sync(&dev->pm.ps_work);
1257 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1258 
1259 	mt7921_mutex_acquire(dev);
1260 
1261 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1262 	ieee80211_iterate_active_interfaces(hw,
1263 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1264 					    mt76_connac_mcu_set_suspend_iter,
1265 					    &dev->mphy);
1266 
1267 	mt7921_mutex_release(dev);
1268 
1269 	return 0;
1270 }
1271 
1272 static int mt7921_resume(struct ieee80211_hw *hw)
1273 {
1274 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1275 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1276 
1277 	mt7921_mutex_acquire(dev);
1278 
1279 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1280 	ieee80211_iterate_active_interfaces(hw,
1281 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1282 					    mt76_connac_mcu_set_suspend_iter,
1283 					    &dev->mphy);
1284 
1285 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1286 				     MT7921_WATCHDOG_TIME);
1287 
1288 	mt7921_mutex_release(dev);
1289 
1290 	return 0;
1291 }
1292 
1293 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1294 {
1295 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1296 	struct mt76_dev *mdev = &dev->mt76;
1297 
1298 	device_set_wakeup_enable(mdev->dev, enabled);
1299 }
1300 
1301 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1302 				  struct ieee80211_vif *vif,
1303 				  struct cfg80211_gtk_rekey_data *data)
1304 {
1305 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1306 
1307 	mt7921_mutex_acquire(dev);
1308 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1309 	mt7921_mutex_release(dev);
1310 }
1311 #endif /* CONFIG_PM */
1312 
1313 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1314 			 u32 queues, bool drop)
1315 {
1316 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1317 
1318 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1319 			   HZ / 2);
1320 }
1321 
1322 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1323 					 struct ieee80211_vif *vif,
1324 					 struct ieee80211_sta *sta,
1325 					 bool enabled)
1326 {
1327 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1328 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1329 
1330 	if (enabled)
1331 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1332 	else
1333 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1334 
1335 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1336 					     MCU_UNI_CMD(STA_REC_UPDATE));
1337 }
1338 
1339 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1340 				const struct cfg80211_sar_specs *sar)
1341 {
1342 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1343 	struct mt76_phy *mphy = hw->priv;
1344 	int err;
1345 
1346 	mt7921_mutex_acquire(dev);
1347 	err = mt76_init_sar_power(hw, sar);
1348 	if (err)
1349 		goto out;
1350 
1351 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1352 out:
1353 	mt7921_mutex_release(dev);
1354 
1355 	return err;
1356 }
1357 
1358 const struct ieee80211_ops mt7921_ops = {
1359 	.tx = mt7921_tx,
1360 	.start = mt7921_start,
1361 	.stop = mt7921_stop,
1362 	.add_interface = mt7921_add_interface,
1363 	.remove_interface = mt7921_remove_interface,
1364 	.config = mt7921_config,
1365 	.conf_tx = mt7921_conf_tx,
1366 	.configure_filter = mt7921_configure_filter,
1367 	.bss_info_changed = mt7921_bss_info_changed,
1368 	.sta_state = mt7921_sta_state,
1369 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1370 	.set_key = mt7921_set_key,
1371 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1372 	.ampdu_action = mt7921_ampdu_action,
1373 	.set_rts_threshold = mt7921_set_rts_threshold,
1374 	.wake_tx_queue = mt76_wake_tx_queue,
1375 	.release_buffered_frames = mt76_release_buffered_frames,
1376 	.get_txpower = mt76_get_txpower,
1377 	.get_stats = mt7921_get_stats,
1378 	.get_et_sset_count = mt7921_get_et_sset_count,
1379 	.get_et_strings = mt7921_get_et_strings,
1380 	.get_et_stats = mt7921_get_et_stats,
1381 	.get_tsf = mt7921_get_tsf,
1382 	.set_tsf = mt7921_set_tsf,
1383 	.get_survey = mt76_get_survey,
1384 	.get_antenna = mt76_get_antenna,
1385 	.set_antenna = mt7921_set_antenna,
1386 	.set_coverage_class = mt7921_set_coverage_class,
1387 	.hw_scan = mt7921_hw_scan,
1388 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1389 	.sta_statistics = mt7921_sta_statistics,
1390 	.sched_scan_start = mt7921_start_sched_scan,
1391 	.sched_scan_stop = mt7921_stop_sched_scan,
1392 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1393 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1394 #ifdef CONFIG_PM
1395 	.suspend = mt7921_suspend,
1396 	.resume = mt7921_resume,
1397 	.set_wakeup = mt7921_set_wakeup,
1398 	.set_rekey_data = mt7921_set_rekey_data,
1399 #endif /* CONFIG_PM */
1400 	.flush = mt7921_flush,
1401 	.set_sar_specs = mt7921_set_sar_specs,
1402 };
1403 EXPORT_SYMBOL_GPL(mt7921_ops);
1404 
1405 MODULE_LICENSE("Dual BSD/GPL");
1406 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1407