xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/main.c (revision bd1e3e7b693c17a04e7d2bd9119daa482b7c7720)
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 			break;
132 		}
133 
134 		he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
135 		he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
136 
137 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
138 		if (he_cap_elem->phy_cap_info[6] &
139 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
140 			mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
141 		} else {
142 			he_cap_elem->phy_cap_info[9] |=
143 				IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
144 		}
145 
146 		if (band == NL80211_BAND_6GHZ) {
147 			struct ieee80211_supported_band *sband =
148 				&phy->mt76->sband_5g.sband;
149 			struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
150 			struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
151 			u32 exp;
152 			u16 cap;
153 
154 			cap = u16_encode_bits(ht_cap->ampdu_density,
155 					IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
156 			exp = u32_get_bits(vht_cap->cap,
157 				IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
158 			cap |= u16_encode_bits(exp,
159 					IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
160 			exp = u32_get_bits(vht_cap->cap,
161 					   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
162 			cap |= u16_encode_bits(exp,
163 					IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
164 			if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
165 				cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
166 			if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
167 				cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
168 
169 			data->he_6ghz_capa.capa = cpu_to_le16(cap);
170 		}
171 		idx++;
172 	}
173 
174 	return idx;
175 }
176 
177 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
178 {
179 	struct ieee80211_sband_iftype_data *data;
180 	struct ieee80211_supported_band *band;
181 	int n;
182 
183 	if (phy->mt76->cap.has_2ghz) {
184 		data = phy->iftype[NL80211_BAND_2GHZ];
185 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
186 
187 		band = &phy->mt76->sband_2g.sband;
188 		band->iftype_data = data;
189 		band->n_iftype_data = n;
190 	}
191 
192 	if (phy->mt76->cap.has_5ghz) {
193 		data = phy->iftype[NL80211_BAND_5GHZ];
194 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
195 
196 		band = &phy->mt76->sband_5g.sband;
197 		band->iftype_data = data;
198 		band->n_iftype_data = n;
199 
200 		if (phy->mt76->cap.has_6ghz) {
201 			data = phy->iftype[NL80211_BAND_6GHZ];
202 			n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data);
203 
204 			band = &phy->mt76->sband_6g.sband;
205 			band->iftype_data = data;
206 			band->n_iftype_data = n;
207 		}
208 	}
209 }
210 
211 int __mt7921_start(struct mt7921_phy *phy)
212 {
213 	struct mt76_phy *mphy = phy->mt76;
214 	int err;
215 
216 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
217 	if (err)
218 		return err;
219 
220 	err = mt76_connac_mcu_set_channel_domain(mphy);
221 	if (err)
222 		return err;
223 
224 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
225 	if (err)
226 		return err;
227 
228 	err = mt76_connac_mcu_set_rate_txpower(phy->mt76);
229 	if (err)
230 		return err;
231 
232 	mt7921_mac_reset_counters(phy);
233 	set_bit(MT76_STATE_RUNNING, &mphy->state);
234 
235 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
236 				     MT7921_WATCHDOG_TIME);
237 
238 	return 0;
239 }
240 
241 static int mt7921_start(struct ieee80211_hw *hw)
242 {
243 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
244 	int err;
245 
246 	mt7921_mutex_acquire(phy->dev);
247 	err = __mt7921_start(phy);
248 	mt7921_mutex_release(phy->dev);
249 
250 	return err;
251 }
252 
253 static void mt7921_stop(struct ieee80211_hw *hw)
254 {
255 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
256 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
257 
258 	cancel_delayed_work_sync(&phy->mt76->mac_work);
259 
260 	cancel_delayed_work_sync(&dev->pm.ps_work);
261 	cancel_work_sync(&dev->pm.wake_work);
262 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
263 
264 	mt7921_mutex_acquire(dev);
265 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
266 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
267 	mt7921_mutex_release(dev);
268 }
269 
270 static int mt7921_add_interface(struct ieee80211_hw *hw,
271 				struct ieee80211_vif *vif)
272 {
273 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
274 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
275 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
276 	struct mt76_txq *mtxq;
277 	int idx, ret = 0;
278 
279 	mt7921_mutex_acquire(dev);
280 
281 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
282 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
283 		ret = -ENOSPC;
284 		goto out;
285 	}
286 
287 	mvif->mt76.omac_idx = mvif->mt76.idx;
288 	mvif->phy = phy;
289 	mvif->mt76.band_idx = 0;
290 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
291 
292 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
293 					  true);
294 	if (ret)
295 		goto out;
296 
297 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
298 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
299 
300 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
301 
302 	INIT_LIST_HEAD(&mvif->sta.poll_list);
303 	mvif->sta.wcid.idx = idx;
304 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
305 	mvif->sta.wcid.hw_key_idx = -1;
306 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
307 	mt76_packet_id_init(&mvif->sta.wcid);
308 
309 	mt7921_mac_wtbl_update(dev, idx,
310 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
311 
312 	ewma_rssi_init(&mvif->rssi);
313 
314 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
315 	if (vif->txq) {
316 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
317 		mtxq->wcid = &mvif->sta.wcid;
318 	}
319 
320 	if (vif->type != NL80211_IFTYPE_AP &&
321 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
322 		vif->offload_flags = 0;
323 
324 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
325 
326 out:
327 	mt7921_mutex_release(dev);
328 
329 	return ret;
330 }
331 
332 static void mt7921_remove_interface(struct ieee80211_hw *hw,
333 				    struct ieee80211_vif *vif)
334 {
335 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
336 	struct mt7921_sta *msta = &mvif->sta;
337 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
338 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
339 	int idx = msta->wcid.idx;
340 
341 	mt7921_mutex_acquire(dev);
342 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
343 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
344 
345 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
346 
347 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
348 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
349 	mt7921_mutex_release(dev);
350 
351 	spin_lock_bh(&dev->sta_poll_lock);
352 	if (!list_empty(&msta->poll_list))
353 		list_del_init(&msta->poll_list);
354 	spin_unlock_bh(&dev->sta_poll_lock);
355 
356 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
357 }
358 
359 static int mt7921_set_channel(struct mt7921_phy *phy)
360 {
361 	struct mt7921_dev *dev = phy->dev;
362 	int ret;
363 
364 	cancel_delayed_work_sync(&phy->mt76->mac_work);
365 
366 	mt7921_mutex_acquire(dev);
367 	set_bit(MT76_RESET, &phy->mt76->state);
368 
369 	mt76_set_channel(phy->mt76);
370 
371 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
372 	if (ret)
373 		goto out;
374 
375 	mt7921_mac_set_timing(phy);
376 
377 	mt7921_mac_reset_counters(phy);
378 	phy->noise = 0;
379 
380 out:
381 	clear_bit(MT76_RESET, &phy->mt76->state);
382 	mt7921_mutex_release(dev);
383 
384 	mt76_worker_schedule(&dev->mt76.tx_worker);
385 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
386 				     MT7921_WATCHDOG_TIME);
387 
388 	return ret;
389 }
390 
391 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
392 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
393 			  struct ieee80211_key_conf *key)
394 {
395 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
396 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
397 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
398 				  &mvif->sta;
399 	struct mt76_wcid *wcid = &msta->wcid;
400 	u8 *wcid_keyidx = &wcid->hw_key_idx;
401 	int idx = key->keyidx, err = 0;
402 
403 	/* The hardware does not support per-STA RX GTK, fallback
404 	 * to software mode for these.
405 	 */
406 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
407 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
408 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
409 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
410 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
411 		return -EOPNOTSUPP;
412 
413 	/* fall back to sw encryption for unsupported ciphers */
414 	switch (key->cipher) {
415 	case WLAN_CIPHER_SUITE_AES_CMAC:
416 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
417 		wcid_keyidx = &wcid->hw_key_idx2;
418 		break;
419 	case WLAN_CIPHER_SUITE_WEP40:
420 	case WLAN_CIPHER_SUITE_WEP104:
421 		if (!mvif->wep_sta)
422 			return -EOPNOTSUPP;
423 		break;
424 	case WLAN_CIPHER_SUITE_TKIP:
425 	case WLAN_CIPHER_SUITE_CCMP:
426 	case WLAN_CIPHER_SUITE_CCMP_256:
427 	case WLAN_CIPHER_SUITE_GCMP:
428 	case WLAN_CIPHER_SUITE_GCMP_256:
429 	case WLAN_CIPHER_SUITE_SMS4:
430 		break;
431 	default:
432 		return -EOPNOTSUPP;
433 	}
434 
435 	mt7921_mutex_acquire(dev);
436 
437 	if (cmd == SET_KEY)
438 		*wcid_keyidx = idx;
439 	else if (idx == *wcid_keyidx)
440 		*wcid_keyidx = -1;
441 	else
442 		goto out;
443 
444 	mt76_wcid_key_setup(&dev->mt76, wcid,
445 			    cmd == SET_KEY ? key : NULL);
446 
447 	err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
448 	if (err)
449 		goto out;
450 
451 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
452 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
453 		err = mt7921_mcu_add_key(dev, vif, mvif->wep_sta, key, cmd);
454 out:
455 	mt7921_mutex_release(dev);
456 
457 	return err;
458 }
459 
460 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
461 {
462 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
463 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
464 	int ret;
465 
466 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
467 		ieee80211_stop_queues(hw);
468 		ret = mt7921_set_channel(phy);
469 		if (ret)
470 			return ret;
471 		ieee80211_wake_queues(hw);
472 	}
473 
474 	mt7921_mutex_acquire(dev);
475 
476 	if (changed & IEEE80211_CONF_CHANGE_POWER)
477 		mt76_connac_mcu_set_rate_txpower(phy->mt76);
478 
479 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
480 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
481 
482 		if (!enabled)
483 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
484 		else
485 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
486 
487 		mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
488 			       enabled);
489 		mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
490 	}
491 
492 	mt7921_mutex_release(dev);
493 
494 	return 0;
495 }
496 
497 static int
498 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
499 	       const struct ieee80211_tx_queue_params *params)
500 {
501 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
502 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
503 
504 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
505 	queue = mt7921_lmac_mapping(dev, queue);
506 	mvif->queue_params[queue] = *params;
507 
508 	return 0;
509 }
510 
511 static void mt7921_configure_filter(struct ieee80211_hw *hw,
512 				    unsigned int changed_flags,
513 				    unsigned int *total_flags,
514 				    u64 multicast)
515 {
516 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
517 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
518 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
519 			MT_WF_RFCR1_DROP_BF_POLL |
520 			MT_WF_RFCR1_DROP_BA |
521 			MT_WF_RFCR1_DROP_CFEND |
522 			MT_WF_RFCR1_DROP_CFACK;
523 	u32 flags = 0;
524 
525 #define MT76_FILTER(_flag, _hw) do {					\
526 		flags |= *total_flags & FIF_##_flag;			\
527 		phy->rxfilter &= ~(_hw);				\
528 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
529 	} while (0)
530 
531 	mt7921_mutex_acquire(dev);
532 
533 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
534 			   MT_WF_RFCR_DROP_OTHER_BEACON |
535 			   MT_WF_RFCR_DROP_FRAME_REPORT |
536 			   MT_WF_RFCR_DROP_PROBEREQ |
537 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
538 			   MT_WF_RFCR_DROP_MCAST |
539 			   MT_WF_RFCR_DROP_BCAST |
540 			   MT_WF_RFCR_DROP_DUPLICATE |
541 			   MT_WF_RFCR_DROP_A2_BSSID |
542 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
543 			   MT_WF_RFCR_DROP_STBC_MULTI);
544 
545 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
546 			       MT_WF_RFCR_DROP_A3_MAC |
547 			       MT_WF_RFCR_DROP_A3_BSSID);
548 
549 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
550 
551 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
552 			     MT_WF_RFCR_DROP_RTS |
553 			     MT_WF_RFCR_DROP_CTL_RSV |
554 			     MT_WF_RFCR_DROP_NDPA);
555 
556 	*total_flags = flags;
557 	mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
558 
559 	if (*total_flags & FIF_CONTROL)
560 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
561 	else
562 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
563 
564 	mt7921_mutex_release(dev);
565 }
566 
567 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
568 				    struct ieee80211_vif *vif,
569 				    struct ieee80211_bss_conf *info,
570 				    u32 changed)
571 {
572 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
573 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
574 
575 	mt7921_mutex_acquire(dev);
576 
577 	if (changed & BSS_CHANGED_ERP_SLOT) {
578 		int slottime = info->use_short_slot ? 9 : 20;
579 
580 		if (slottime != phy->slottime) {
581 			phy->slottime = slottime;
582 			mt7921_mac_set_timing(phy);
583 		}
584 	}
585 
586 	/* ensure that enable txcmd_mode after bss_info */
587 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
588 		mt7921_mcu_set_tx(dev, vif);
589 
590 	if (changed & BSS_CHANGED_PS)
591 		mt7921_mcu_uni_bss_ps(dev, vif);
592 
593 	if (changed & BSS_CHANGED_ASSOC) {
594 		mt7921_mcu_sta_update(dev, NULL, vif, true,
595 				      MT76_STA_INFO_STATE_ASSOC);
596 		if (dev->pm.enable)
597 			mt7921_mcu_set_beacon_filter(dev, vif, info->assoc);
598 	}
599 
600 	if (changed & BSS_CHANGED_ARP_FILTER) {
601 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
602 
603 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
604 						  info);
605 	}
606 
607 	mt7921_mutex_release(dev);
608 }
609 
610 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
611 		       struct ieee80211_sta *sta)
612 {
613 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
614 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
615 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
616 	int ret, idx;
617 
618 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
619 	if (idx < 0)
620 		return -ENOSPC;
621 
622 	INIT_LIST_HEAD(&msta->poll_list);
623 	msta->vif = mvif;
624 	msta->wcid.sta = 1;
625 	msta->wcid.idx = idx;
626 	msta->wcid.ext_phy = mvif->mt76.band_idx;
627 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
628 	msta->last_txs = jiffies;
629 
630 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
631 	if (ret)
632 		return ret;
633 
634 	if (vif->type == NL80211_IFTYPE_STATION)
635 		mvif->wep_sta = msta;
636 
637 	mt7921_mac_wtbl_update(dev, idx,
638 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
639 
640 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
641 				    MT76_STA_INFO_STATE_NONE);
642 	if (ret)
643 		return ret;
644 
645 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
646 
647 	return 0;
648 }
649 
650 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
651 			  struct ieee80211_sta *sta)
652 {
653 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
654 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
655 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
656 
657 	mt7921_mutex_acquire(dev);
658 
659 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
660 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
661 					    true);
662 
663 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
664 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
665 
666 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
667 
668 	mt7921_mutex_release(dev);
669 }
670 
671 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
672 			   struct ieee80211_sta *sta)
673 {
674 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
675 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
676 
677 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
678 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
679 
680 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
681 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
682 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
683 
684 	if (vif->type == NL80211_IFTYPE_STATION) {
685 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
686 
687 		mvif->wep_sta = NULL;
688 		ewma_rssi_init(&mvif->rssi);
689 		if (!sta->tdls)
690 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
691 						    &mvif->sta.wcid, false);
692 	}
693 
694 	spin_lock_bh(&dev->sta_poll_lock);
695 	if (!list_empty(&msta->poll_list))
696 		list_del_init(&msta->poll_list);
697 	spin_unlock_bh(&dev->sta_poll_lock);
698 
699 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
700 }
701 
702 void mt7921_tx_worker(struct mt76_worker *w)
703 {
704 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
705 					      mt76.tx_worker);
706 
707 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
708 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
709 		return;
710 	}
711 
712 	mt76_txq_schedule_all(&dev->mphy);
713 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
714 }
715 
716 static void mt7921_tx(struct ieee80211_hw *hw,
717 		      struct ieee80211_tx_control *control,
718 		      struct sk_buff *skb)
719 {
720 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
721 	struct mt76_phy *mphy = hw->priv;
722 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
723 	struct ieee80211_vif *vif = info->control.vif;
724 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
725 	int qid;
726 
727 	if (control->sta) {
728 		struct mt7921_sta *sta;
729 
730 		sta = (struct mt7921_sta *)control->sta->drv_priv;
731 		wcid = &sta->wcid;
732 	}
733 
734 	if (vif && !control->sta) {
735 		struct mt7921_vif *mvif;
736 
737 		mvif = (struct mt7921_vif *)vif->drv_priv;
738 		wcid = &mvif->sta.wcid;
739 	}
740 
741 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
742 		mt76_tx(mphy, control->sta, wcid, skb);
743 		mt76_connac_pm_unref(mphy, &dev->pm);
744 		return;
745 	}
746 
747 	qid = skb_get_queue_mapping(skb);
748 	if (qid >= MT_TXQ_PSD) {
749 		qid = IEEE80211_AC_BE;
750 		skb_set_queue_mapping(skb, qid);
751 	}
752 
753 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
754 }
755 
756 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
757 {
758 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
759 
760 	mt7921_mutex_acquire(dev);
761 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
762 	mt7921_mutex_release(dev);
763 
764 	return 0;
765 }
766 
767 static int
768 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
769 		    struct ieee80211_ampdu_params *params)
770 {
771 	enum ieee80211_ampdu_mlme_action action = params->action;
772 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
773 	struct ieee80211_sta *sta = params->sta;
774 	struct ieee80211_txq *txq = sta->txq[params->tid];
775 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
776 	u16 tid = params->tid;
777 	u16 ssn = params->ssn;
778 	struct mt76_txq *mtxq;
779 	int ret = 0;
780 
781 	if (!txq)
782 		return -EINVAL;
783 
784 	mtxq = (struct mt76_txq *)txq->drv_priv;
785 
786 	mt7921_mutex_acquire(dev);
787 	switch (action) {
788 	case IEEE80211_AMPDU_RX_START:
789 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
790 				   params->buf_size);
791 		mt7921_mcu_uni_rx_ba(dev, params, true);
792 		break;
793 	case IEEE80211_AMPDU_RX_STOP:
794 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
795 		mt7921_mcu_uni_rx_ba(dev, params, false);
796 		break;
797 	case IEEE80211_AMPDU_TX_OPERATIONAL:
798 		mtxq->aggr = true;
799 		mtxq->send_bar = false;
800 		mt7921_mcu_uni_tx_ba(dev, params, true);
801 		break;
802 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
803 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
804 		mtxq->aggr = false;
805 		clear_bit(tid, &msta->ampdu_state);
806 		mt7921_mcu_uni_tx_ba(dev, params, false);
807 		break;
808 	case IEEE80211_AMPDU_TX_START:
809 		set_bit(tid, &msta->ampdu_state);
810 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
811 		break;
812 	case IEEE80211_AMPDU_TX_STOP_CONT:
813 		mtxq->aggr = false;
814 		clear_bit(tid, &msta->ampdu_state);
815 		mt7921_mcu_uni_tx_ba(dev, params, false);
816 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
817 		break;
818 	}
819 	mt7921_mutex_release(dev);
820 
821 	return ret;
822 }
823 
824 static int mt7921_sta_state(struct ieee80211_hw *hw,
825 			    struct ieee80211_vif *vif,
826 			    struct ieee80211_sta *sta,
827 			    enum ieee80211_sta_state old_state,
828 			    enum ieee80211_sta_state new_state)
829 {
830 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
831 
832 	if (dev->pm.ds_enable) {
833 		mt7921_mutex_acquire(dev);
834 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
835 		mt7921_mutex_release(dev);
836 	}
837 
838 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
839 }
840 
841 static int
842 mt7921_get_stats(struct ieee80211_hw *hw,
843 		 struct ieee80211_low_level_stats *stats)
844 {
845 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
846 	struct mib_stats *mib = &phy->mib;
847 
848 	mt7921_mutex_acquire(phy->dev);
849 
850 	stats->dot11RTSSuccessCount = mib->rts_cnt;
851 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
852 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
853 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
854 
855 	memset(mib, 0, sizeof(*mib));
856 
857 	mt7921_mutex_release(phy->dev);
858 
859 	return 0;
860 }
861 
862 static u64
863 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
864 {
865 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
866 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
867 	u8 omac_idx = mvif->mt76.omac_idx;
868 	union {
869 		u64 t64;
870 		u32 t32[2];
871 	} tsf;
872 	u16 n;
873 
874 	mt7921_mutex_acquire(dev);
875 
876 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
877 	/* TSF software read */
878 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
879 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
880 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
881 
882 	mt7921_mutex_release(dev);
883 
884 	return tsf.t64;
885 }
886 
887 static void
888 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
889 	       u64 timestamp)
890 {
891 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
892 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
893 	u8 omac_idx = mvif->mt76.omac_idx;
894 	union {
895 		u64 t64;
896 		u32 t32[2];
897 	} tsf = { .t64 = timestamp, };
898 	u16 n;
899 
900 	mt7921_mutex_acquire(dev);
901 
902 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
903 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
904 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
905 	/* TSF software overwrite */
906 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
907 
908 	mt7921_mutex_release(dev);
909 }
910 
911 static void
912 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
913 {
914 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
915 	struct mt7921_dev *dev = phy->dev;
916 
917 	mt7921_mutex_acquire(dev);
918 	phy->coverage_class = max_t(s16, coverage_class, 0);
919 	mt7921_mac_set_timing(phy);
920 	mt7921_mutex_release(dev);
921 }
922 
923 void mt7921_scan_work(struct work_struct *work)
924 {
925 	struct mt7921_phy *phy;
926 
927 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
928 						scan_work.work);
929 
930 	while (true) {
931 		struct mt7921_mcu_rxd *rxd;
932 		struct sk_buff *skb;
933 
934 		spin_lock_bh(&phy->dev->mt76.lock);
935 		skb = __skb_dequeue(&phy->scan_event_list);
936 		spin_unlock_bh(&phy->dev->mt76.lock);
937 
938 		if (!skb)
939 			break;
940 
941 		rxd = (struct mt7921_mcu_rxd *)skb->data;
942 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
943 			ieee80211_sched_scan_results(phy->mt76->hw);
944 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
945 					      &phy->mt76->state)) {
946 			struct cfg80211_scan_info info = {
947 				.aborted = false,
948 			};
949 
950 			ieee80211_scan_completed(phy->mt76->hw, &info);
951 		}
952 		dev_kfree_skb(skb);
953 	}
954 }
955 
956 static int
957 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
958 	       struct ieee80211_scan_request *req)
959 {
960 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
961 	struct mt76_phy *mphy = hw->priv;
962 	int err;
963 
964 	mt7921_mutex_acquire(dev);
965 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
966 	mt7921_mutex_release(dev);
967 
968 	return err;
969 }
970 
971 static void
972 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
973 {
974 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
975 	struct mt76_phy *mphy = hw->priv;
976 
977 	mt7921_mutex_acquire(dev);
978 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
979 	mt7921_mutex_release(dev);
980 }
981 
982 static int
983 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
984 			struct cfg80211_sched_scan_request *req,
985 			struct ieee80211_scan_ies *ies)
986 {
987 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
988 	struct mt76_phy *mphy = hw->priv;
989 	int err;
990 
991 	mt7921_mutex_acquire(dev);
992 
993 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
994 	if (err < 0)
995 		goto out;
996 
997 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
998 out:
999 	mt7921_mutex_release(dev);
1000 
1001 	return err;
1002 }
1003 
1004 static int
1005 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1006 {
1007 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1008 	struct mt76_phy *mphy = hw->priv;
1009 	int err;
1010 
1011 	mt7921_mutex_acquire(dev);
1012 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1013 	mt7921_mutex_release(dev);
1014 
1015 	return err;
1016 }
1017 
1018 static int
1019 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1020 {
1021 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1022 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1023 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1024 
1025 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1026 		return -EINVAL;
1027 
1028 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1029 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1030 
1031 	mt7921_mutex_acquire(dev);
1032 
1033 	phy->mt76->antenna_mask = tx_ant;
1034 	phy->mt76->chainmask = tx_ant;
1035 
1036 	mt76_set_stream_caps(phy->mt76, true);
1037 	mt7921_set_stream_he_caps(phy);
1038 
1039 	mt7921_mutex_release(dev);
1040 
1041 	return 0;
1042 }
1043 
1044 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1045 				  struct ieee80211_vif *vif,
1046 				  struct ieee80211_sta *sta,
1047 				  struct station_info *sinfo)
1048 {
1049 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1050 	struct rate_info *txrate = &msta->wcid.rate;
1051 
1052 	if (!txrate->legacy && !txrate->flags)
1053 		return;
1054 
1055 	if (txrate->legacy) {
1056 		sinfo->txrate.legacy = txrate->legacy;
1057 	} else {
1058 		sinfo->txrate.mcs = txrate->mcs;
1059 		sinfo->txrate.nss = txrate->nss;
1060 		sinfo->txrate.bw = txrate->bw;
1061 		sinfo->txrate.he_gi = txrate->he_gi;
1062 		sinfo->txrate.he_dcm = txrate->he_dcm;
1063 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1064 	}
1065 	sinfo->txrate.flags = txrate->flags;
1066 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1067 }
1068 
1069 #ifdef CONFIG_PM
1070 static int mt7921_suspend(struct ieee80211_hw *hw,
1071 			  struct cfg80211_wowlan *wowlan)
1072 {
1073 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1074 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1075 	int err;
1076 
1077 	cancel_delayed_work_sync(&phy->scan_work);
1078 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1079 
1080 	cancel_delayed_work_sync(&dev->pm.ps_work);
1081 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1082 
1083 	mt7921_mutex_acquire(dev);
1084 
1085 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1086 
1087 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1088 	ieee80211_iterate_active_interfaces(hw,
1089 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1090 					    mt76_connac_mcu_set_suspend_iter,
1091 					    &dev->mphy);
1092 
1093 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1094 
1095 	mt7921_mutex_release(dev);
1096 
1097 	return err;
1098 }
1099 
1100 static int mt7921_resume(struct ieee80211_hw *hw)
1101 {
1102 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1103 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1104 	int err;
1105 
1106 	mt7921_mutex_acquire(dev);
1107 
1108 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1109 	if (err < 0)
1110 		goto out;
1111 
1112 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1113 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1114 	ieee80211_iterate_active_interfaces(hw,
1115 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1116 					    mt76_connac_mcu_set_suspend_iter,
1117 					    &dev->mphy);
1118 
1119 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1120 				     MT7921_WATCHDOG_TIME);
1121 out:
1122 
1123 	mt7921_mutex_release(dev);
1124 
1125 	return err;
1126 }
1127 
1128 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1129 {
1130 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1131 	struct mt76_dev *mdev = &dev->mt76;
1132 
1133 	device_set_wakeup_enable(mdev->dev, enabled);
1134 }
1135 
1136 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1137 				  struct ieee80211_vif *vif,
1138 				  struct cfg80211_gtk_rekey_data *data)
1139 {
1140 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1141 
1142 	mt7921_mutex_acquire(dev);
1143 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1144 	mt7921_mutex_release(dev);
1145 }
1146 #endif /* CONFIG_PM */
1147 
1148 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1149 			 u32 queues, bool drop)
1150 {
1151 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1152 
1153 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1154 			   HZ / 2);
1155 }
1156 
1157 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1158 					 struct ieee80211_vif *vif,
1159 					 struct ieee80211_sta *sta,
1160 					 bool enabled)
1161 {
1162 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1163 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1164 
1165 	if (enabled)
1166 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1167 	else
1168 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1169 
1170 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1171 					     MCU_UNI_CMD_STA_REC_UPDATE);
1172 }
1173 
1174 static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
1175 				const struct cfg80211_sar_specs *sar)
1176 {
1177 	const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
1178 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1179 	struct mt76_freq_range_power *data, *frp;
1180 	struct mt76_phy *mphy = hw->priv;
1181 	int err;
1182 	u32 i;
1183 
1184 	if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
1185 		return -EINVAL;
1186 
1187 	mt7921_mutex_acquire(dev);
1188 
1189 	data = mphy->frp;
1190 
1191 	for (i = 0; i < sar->num_sub_specs; i++) {
1192 		u32 index = sar->sub_specs[i].freq_range_index;
1193 		/* SAR specifies power limitaton in 0.25dbm */
1194 		s32 power = sar->sub_specs[i].power >> 1;
1195 
1196 		if (power > 127 || power < -127)
1197 			power = 127;
1198 
1199 		frp = &data[index];
1200 		frp->range = &capa->freq_ranges[index];
1201 		frp->power = power;
1202 	}
1203 
1204 	err = mt76_connac_mcu_set_rate_txpower(mphy);
1205 
1206 	mt7921_mutex_release(dev);
1207 
1208 	return err;
1209 }
1210 
1211 const struct ieee80211_ops mt7921_ops = {
1212 	.tx = mt7921_tx,
1213 	.start = mt7921_start,
1214 	.stop = mt7921_stop,
1215 	.add_interface = mt7921_add_interface,
1216 	.remove_interface = mt7921_remove_interface,
1217 	.config = mt7921_config,
1218 	.conf_tx = mt7921_conf_tx,
1219 	.configure_filter = mt7921_configure_filter,
1220 	.bss_info_changed = mt7921_bss_info_changed,
1221 	.sta_state = mt7921_sta_state,
1222 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1223 	.set_key = mt7921_set_key,
1224 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1225 	.ampdu_action = mt7921_ampdu_action,
1226 	.set_rts_threshold = mt7921_set_rts_threshold,
1227 	.wake_tx_queue = mt76_wake_tx_queue,
1228 	.release_buffered_frames = mt76_release_buffered_frames,
1229 	.get_txpower = mt76_get_txpower,
1230 	.get_stats = mt7921_get_stats,
1231 	.get_tsf = mt7921_get_tsf,
1232 	.set_tsf = mt7921_set_tsf,
1233 	.get_survey = mt76_get_survey,
1234 	.get_antenna = mt76_get_antenna,
1235 	.set_antenna = mt7921_set_antenna,
1236 	.set_coverage_class = mt7921_set_coverage_class,
1237 	.hw_scan = mt7921_hw_scan,
1238 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1239 	.sta_statistics = mt7921_sta_statistics,
1240 	.sched_scan_start = mt7921_start_sched_scan,
1241 	.sched_scan_stop = mt7921_stop_sched_scan,
1242 	CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
1243 	CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
1244 #ifdef CONFIG_PM
1245 	.suspend = mt7921_suspend,
1246 	.resume = mt7921_resume,
1247 	.set_wakeup = mt7921_set_wakeup,
1248 	.set_rekey_data = mt7921_set_rekey_data,
1249 #endif /* CONFIG_PM */
1250 	.flush = mt7921_flush,
1251 	.set_sar_specs = mt7921_set_sar_specs,
1252 };
1253