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 if (band == NL80211_BAND_5GHZ)
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 if (band == NL80211_BAND_5GHZ)
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 		idx++;
146 	}
147 
148 	return idx;
149 }
150 
151 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
152 {
153 	struct ieee80211_sband_iftype_data *data;
154 	struct ieee80211_supported_band *band;
155 	int n;
156 
157 	if (phy->mt76->cap.has_2ghz) {
158 		data = phy->iftype[NL80211_BAND_2GHZ];
159 		n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
160 
161 		band = &phy->mt76->sband_2g.sband;
162 		band->iftype_data = data;
163 		band->n_iftype_data = n;
164 	}
165 
166 	if (phy->mt76->cap.has_5ghz) {
167 		data = phy->iftype[NL80211_BAND_5GHZ];
168 		n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
169 
170 		band = &phy->mt76->sband_5g.sband;
171 		band->iftype_data = data;
172 		band->n_iftype_data = n;
173 	}
174 }
175 
176 int __mt7921_start(struct mt7921_phy *phy)
177 {
178 	struct mt76_phy *mphy = phy->mt76;
179 	int err;
180 
181 	err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
182 	if (err)
183 		return err;
184 
185 	err = mt76_connac_mcu_set_channel_domain(mphy);
186 	if (err)
187 		return err;
188 
189 	err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
190 	if (err)
191 		return err;
192 
193 	err = mt76_connac_mcu_set_rate_txpower(phy->mt76);
194 	if (err)
195 		return err;
196 
197 	mt7921_mac_reset_counters(phy);
198 	set_bit(MT76_STATE_RUNNING, &mphy->state);
199 
200 	ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
201 				     MT7921_WATCHDOG_TIME);
202 
203 	return 0;
204 }
205 
206 static int mt7921_start(struct ieee80211_hw *hw)
207 {
208 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
209 	int err;
210 
211 	mt7921_mutex_acquire(phy->dev);
212 	err = __mt7921_start(phy);
213 	mt7921_mutex_release(phy->dev);
214 
215 	return err;
216 }
217 
218 static void mt7921_stop(struct ieee80211_hw *hw)
219 {
220 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
221 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
222 
223 	cancel_delayed_work_sync(&phy->mt76->mac_work);
224 
225 	cancel_delayed_work_sync(&dev->pm.ps_work);
226 	cancel_work_sync(&dev->pm.wake_work);
227 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
228 
229 	mt7921_mutex_acquire(dev);
230 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
231 	mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
232 	mt7921_mutex_release(dev);
233 }
234 
235 static int mt7921_add_interface(struct ieee80211_hw *hw,
236 				struct ieee80211_vif *vif)
237 {
238 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
239 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
240 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
241 	struct mt76_txq *mtxq;
242 	int idx, ret = 0;
243 
244 	mt7921_mutex_acquire(dev);
245 
246 	if (vif->type == NL80211_IFTYPE_MONITOR &&
247 	    is_zero_ether_addr(vif->addr))
248 		phy->monitor_vif = vif;
249 
250 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
251 	if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
252 		ret = -ENOSPC;
253 		goto out;
254 	}
255 
256 	mvif->mt76.omac_idx = mvif->mt76.idx;
257 	mvif->phy = phy;
258 	mvif->mt76.band_idx = 0;
259 	mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
260 
261 	ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
262 					  true);
263 	if (ret)
264 		goto out;
265 
266 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
267 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
268 
269 	idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
270 
271 	INIT_LIST_HEAD(&mvif->sta.stats_list);
272 	INIT_LIST_HEAD(&mvif->sta.poll_list);
273 	mvif->sta.wcid.idx = idx;
274 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
275 	mvif->sta.wcid.hw_key_idx = -1;
276 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
277 	mt7921_mac_wtbl_update(dev, idx,
278 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
279 
280 	ewma_rssi_init(&mvif->rssi);
281 
282 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
283 	if (vif->txq) {
284 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
285 		mtxq->wcid = &mvif->sta.wcid;
286 	}
287 
288 	if (vif->type != NL80211_IFTYPE_AP &&
289 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
290 		vif->offload_flags = 0;
291 
292 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
293 
294 out:
295 	mt7921_mutex_release(dev);
296 
297 	return ret;
298 }
299 
300 static void mt7921_remove_interface(struct ieee80211_hw *hw,
301 				    struct ieee80211_vif *vif)
302 {
303 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
304 	struct mt7921_sta *msta = &mvif->sta;
305 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
306 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
307 	int idx = msta->wcid.idx;
308 
309 	if (vif == phy->monitor_vif)
310 		phy->monitor_vif = NULL;
311 
312 	mt7921_mutex_acquire(dev);
313 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
314 	mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
315 
316 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
317 
318 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
319 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
320 	mt7921_mutex_release(dev);
321 
322 	spin_lock_bh(&dev->sta_poll_lock);
323 	if (!list_empty(&msta->poll_list))
324 		list_del_init(&msta->poll_list);
325 	spin_unlock_bh(&dev->sta_poll_lock);
326 }
327 
328 static int mt7921_set_channel(struct mt7921_phy *phy)
329 {
330 	struct mt7921_dev *dev = phy->dev;
331 	int ret;
332 
333 	cancel_delayed_work_sync(&phy->mt76->mac_work);
334 
335 	mt7921_mutex_acquire(dev);
336 	set_bit(MT76_RESET, &phy->mt76->state);
337 
338 	mt76_set_channel(phy->mt76);
339 
340 	ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
341 	if (ret)
342 		goto out;
343 
344 	mt7921_mac_set_timing(phy);
345 
346 	mt7921_mac_reset_counters(phy);
347 	phy->noise = 0;
348 
349 out:
350 	clear_bit(MT76_RESET, &phy->mt76->state);
351 	mt7921_mutex_release(dev);
352 
353 	mt76_worker_schedule(&dev->mt76.tx_worker);
354 	ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
355 				     MT7921_WATCHDOG_TIME);
356 
357 	return ret;
358 }
359 
360 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
361 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
362 			  struct ieee80211_key_conf *key)
363 {
364 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
365 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
366 	struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
367 				  &mvif->sta;
368 	struct mt76_wcid *wcid = &msta->wcid;
369 	u8 *wcid_keyidx = &wcid->hw_key_idx;
370 	int idx = key->keyidx, err = 0;
371 
372 	/* The hardware does not support per-STA RX GTK, fallback
373 	 * to software mode for these.
374 	 */
375 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
376 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
377 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
378 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
379 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
380 		return -EOPNOTSUPP;
381 
382 	/* fall back to sw encryption for unsupported ciphers */
383 	switch (key->cipher) {
384 	case WLAN_CIPHER_SUITE_AES_CMAC:
385 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
386 		wcid_keyidx = &wcid->hw_key_idx2;
387 		break;
388 	case WLAN_CIPHER_SUITE_WEP40:
389 	case WLAN_CIPHER_SUITE_WEP104:
390 		if (!mvif->wep_sta)
391 			return -EOPNOTSUPP;
392 	case WLAN_CIPHER_SUITE_TKIP:
393 	case WLAN_CIPHER_SUITE_CCMP:
394 	case WLAN_CIPHER_SUITE_CCMP_256:
395 	case WLAN_CIPHER_SUITE_GCMP:
396 	case WLAN_CIPHER_SUITE_GCMP_256:
397 	case WLAN_CIPHER_SUITE_SMS4:
398 		break;
399 	default:
400 		return -EOPNOTSUPP;
401 	}
402 
403 	mt7921_mutex_acquire(dev);
404 
405 	if (cmd == SET_KEY)
406 		*wcid_keyidx = idx;
407 	else if (idx == *wcid_keyidx)
408 		*wcid_keyidx = -1;
409 	else
410 		goto out;
411 
412 	mt76_wcid_key_setup(&dev->mt76, wcid,
413 			    cmd == SET_KEY ? key : NULL);
414 
415 	err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
416 	if (err)
417 		goto out;
418 
419 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
420 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
421 		err = mt7921_mcu_add_key(dev, vif, mvif->wep_sta, key, cmd);
422 out:
423 	mt7921_mutex_release(dev);
424 
425 	return err;
426 }
427 
428 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
429 {
430 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
431 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
432 	int ret;
433 
434 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
435 		ieee80211_stop_queues(hw);
436 		ret = mt7921_set_channel(phy);
437 		if (ret)
438 			return ret;
439 		ieee80211_wake_queues(hw);
440 	}
441 
442 	mt7921_mutex_acquire(dev);
443 
444 	if (changed & IEEE80211_CONF_CHANGE_POWER)
445 		mt76_connac_mcu_set_rate_txpower(phy->mt76);
446 
447 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
448 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
449 
450 		if (!enabled)
451 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
452 		else
453 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
454 
455 		mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
456 			       enabled);
457 		mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
458 	}
459 
460 	mt7921_mutex_release(dev);
461 
462 	return 0;
463 }
464 
465 static int
466 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
467 	       const struct ieee80211_tx_queue_params *params)
468 {
469 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
470 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
471 
472 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
473 	queue = mt7921_lmac_mapping(dev, queue);
474 	mvif->queue_params[queue] = *params;
475 
476 	return 0;
477 }
478 
479 static void mt7921_configure_filter(struct ieee80211_hw *hw,
480 				    unsigned int changed_flags,
481 				    unsigned int *total_flags,
482 				    u64 multicast)
483 {
484 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
485 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
486 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
487 			MT_WF_RFCR1_DROP_BF_POLL |
488 			MT_WF_RFCR1_DROP_BA |
489 			MT_WF_RFCR1_DROP_CFEND |
490 			MT_WF_RFCR1_DROP_CFACK;
491 	u32 flags = 0;
492 
493 #define MT76_FILTER(_flag, _hw) do {					\
494 		flags |= *total_flags & FIF_##_flag;			\
495 		phy->rxfilter &= ~(_hw);				\
496 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
497 	} while (0)
498 
499 	mt7921_mutex_acquire(dev);
500 
501 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
502 			   MT_WF_RFCR_DROP_OTHER_BEACON |
503 			   MT_WF_RFCR_DROP_FRAME_REPORT |
504 			   MT_WF_RFCR_DROP_PROBEREQ |
505 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
506 			   MT_WF_RFCR_DROP_MCAST |
507 			   MT_WF_RFCR_DROP_BCAST |
508 			   MT_WF_RFCR_DROP_DUPLICATE |
509 			   MT_WF_RFCR_DROP_A2_BSSID |
510 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
511 			   MT_WF_RFCR_DROP_STBC_MULTI);
512 
513 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
514 			       MT_WF_RFCR_DROP_A3_MAC |
515 			       MT_WF_RFCR_DROP_A3_BSSID);
516 
517 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
518 
519 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
520 			     MT_WF_RFCR_DROP_RTS |
521 			     MT_WF_RFCR_DROP_CTL_RSV |
522 			     MT_WF_RFCR_DROP_NDPA);
523 
524 	*total_flags = flags;
525 	mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
526 
527 	if (*total_flags & FIF_CONTROL)
528 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
529 	else
530 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
531 
532 	mt7921_mutex_release(dev);
533 }
534 
535 static int
536 mt7921_bss_bcnft_apply(struct mt7921_dev *dev, struct ieee80211_vif *vif,
537 		       bool assoc)
538 {
539 	int ret;
540 
541 	if (!dev->pm.enable)
542 		return 0;
543 
544 	if (assoc) {
545 		ret = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
546 		if (ret)
547 			return ret;
548 
549 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
550 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
551 
552 		return 0;
553 	}
554 
555 	ret = mt7921_mcu_set_bss_pm(dev, vif, false);
556 	if (ret)
557 		return ret;
558 
559 	vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
560 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
561 
562 	return 0;
563 }
564 
565 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
566 				    struct ieee80211_vif *vif,
567 				    struct ieee80211_bss_conf *info,
568 				    u32 changed)
569 {
570 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
571 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
572 
573 	mt7921_mutex_acquire(dev);
574 
575 	if (changed & BSS_CHANGED_ERP_SLOT) {
576 		int slottime = info->use_short_slot ? 9 : 20;
577 
578 		if (slottime != phy->slottime) {
579 			phy->slottime = slottime;
580 			mt7921_mac_set_timing(phy);
581 		}
582 	}
583 
584 	/* ensure that enable txcmd_mode after bss_info */
585 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
586 		mt7921_mcu_set_tx(dev, vif);
587 
588 	if (changed & BSS_CHANGED_PS)
589 		mt7921_mcu_uni_bss_ps(dev, vif);
590 
591 	if (changed & BSS_CHANGED_ASSOC) {
592 		mt7921_mcu_sta_update(dev, NULL, vif, true,
593 				      MT76_STA_INFO_STATE_ASSOC);
594 		mt7921_bss_bcnft_apply(dev, vif, info->assoc);
595 	}
596 
597 	if (changed & BSS_CHANGED_ARP_FILTER) {
598 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
599 
600 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
601 						  info);
602 	}
603 
604 	mt7921_mutex_release(dev);
605 }
606 
607 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
608 		       struct ieee80211_sta *sta)
609 {
610 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
611 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
612 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
613 	int ret, idx;
614 
615 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
616 	if (idx < 0)
617 		return -ENOSPC;
618 
619 	INIT_LIST_HEAD(&msta->stats_list);
620 	INIT_LIST_HEAD(&msta->poll_list);
621 	msta->vif = mvif;
622 	msta->wcid.sta = 1;
623 	msta->wcid.idx = idx;
624 	msta->wcid.ext_phy = mvif->mt76.band_idx;
625 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
626 	msta->stats.jiffies = jiffies;
627 
628 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
629 	if (ret)
630 		return ret;
631 
632 	if (vif->type == NL80211_IFTYPE_STATION)
633 		mvif->wep_sta = msta;
634 
635 	mt7921_mac_wtbl_update(dev, idx,
636 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
637 
638 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
639 				    MT76_STA_INFO_STATE_NONE);
640 	if (ret)
641 		return ret;
642 
643 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
644 
645 	return 0;
646 }
647 
648 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
649 			  struct ieee80211_sta *sta)
650 {
651 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
652 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
653 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
654 
655 	mt7921_mutex_acquire(dev);
656 
657 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
658 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
659 					    true);
660 
661 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
662 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
663 
664 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
665 
666 	mt7921_mutex_release(dev);
667 }
668 
669 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
670 			   struct ieee80211_sta *sta)
671 {
672 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
673 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
674 
675 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
676 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
677 
678 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
679 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
680 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
681 
682 	if (vif->type == NL80211_IFTYPE_STATION) {
683 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
684 
685 		mvif->wep_sta = NULL;
686 		ewma_rssi_init(&mvif->rssi);
687 		if (!sta->tdls)
688 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
689 						    &mvif->sta.wcid, false);
690 	}
691 
692 	spin_lock_bh(&dev->sta_poll_lock);
693 	if (!list_empty(&msta->poll_list))
694 		list_del_init(&msta->poll_list);
695 	if (!list_empty(&msta->stats_list))
696 		list_del_init(&msta->stats_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 mt7921_sta_stats *stats = &msta->stats;
1051 
1052 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
1053 		return;
1054 
1055 	if (stats->tx_rate.legacy) {
1056 		sinfo->txrate.legacy = stats->tx_rate.legacy;
1057 	} else {
1058 		sinfo->txrate.mcs = stats->tx_rate.mcs;
1059 		sinfo->txrate.nss = stats->tx_rate.nss;
1060 		sinfo->txrate.bw = stats->tx_rate.bw;
1061 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
1062 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
1063 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
1064 	}
1065 	sinfo->txrate.flags = stats->tx_rate.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 const struct ieee80211_ops mt7921_ops = {
1175 	.tx = mt7921_tx,
1176 	.start = mt7921_start,
1177 	.stop = mt7921_stop,
1178 	.add_interface = mt7921_add_interface,
1179 	.remove_interface = mt7921_remove_interface,
1180 	.config = mt7921_config,
1181 	.conf_tx = mt7921_conf_tx,
1182 	.configure_filter = mt7921_configure_filter,
1183 	.bss_info_changed = mt7921_bss_info_changed,
1184 	.sta_state = mt7921_sta_state,
1185 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1186 	.set_key = mt7921_set_key,
1187 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1188 	.ampdu_action = mt7921_ampdu_action,
1189 	.set_rts_threshold = mt7921_set_rts_threshold,
1190 	.wake_tx_queue = mt76_wake_tx_queue,
1191 	.release_buffered_frames = mt76_release_buffered_frames,
1192 	.get_txpower = mt76_get_txpower,
1193 	.get_stats = mt7921_get_stats,
1194 	.get_tsf = mt7921_get_tsf,
1195 	.set_tsf = mt7921_set_tsf,
1196 	.get_survey = mt76_get_survey,
1197 	.get_antenna = mt76_get_antenna,
1198 	.set_antenna = mt7921_set_antenna,
1199 	.set_coverage_class = mt7921_set_coverage_class,
1200 	.hw_scan = mt7921_hw_scan,
1201 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1202 	.sta_statistics = mt7921_sta_statistics,
1203 	.sched_scan_start = mt7921_start_sched_scan,
1204 	.sched_scan_stop = mt7921_stop_sched_scan,
1205 #ifdef CONFIG_PM
1206 	.suspend = mt7921_suspend,
1207 	.resume = mt7921_resume,
1208 	.set_wakeup = mt7921_set_wakeup,
1209 	.set_rekey_data = mt7921_set_rekey_data,
1210 #endif /* CONFIG_PM */
1211 	.flush = mt7921_flush,
1212 };
1213