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