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