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