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 		break;
393 	case WLAN_CIPHER_SUITE_TKIP:
394 	case WLAN_CIPHER_SUITE_CCMP:
395 	case WLAN_CIPHER_SUITE_CCMP_256:
396 	case WLAN_CIPHER_SUITE_GCMP:
397 	case WLAN_CIPHER_SUITE_GCMP_256:
398 	case WLAN_CIPHER_SUITE_SMS4:
399 		break;
400 	default:
401 		return -EOPNOTSUPP;
402 	}
403 
404 	mt7921_mutex_acquire(dev);
405 
406 	if (cmd == SET_KEY)
407 		*wcid_keyidx = idx;
408 	else if (idx == *wcid_keyidx)
409 		*wcid_keyidx = -1;
410 	else
411 		goto out;
412 
413 	mt76_wcid_key_setup(&dev->mt76, wcid,
414 			    cmd == SET_KEY ? key : NULL);
415 
416 	err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
417 	if (err)
418 		goto out;
419 
420 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104 ||
421 	    key->cipher == WLAN_CIPHER_SUITE_WEP40)
422 		err = mt7921_mcu_add_key(dev, vif, mvif->wep_sta, key, cmd);
423 out:
424 	mt7921_mutex_release(dev);
425 
426 	return err;
427 }
428 
429 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
430 {
431 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
432 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
433 	int ret;
434 
435 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
436 		ieee80211_stop_queues(hw);
437 		ret = mt7921_set_channel(phy);
438 		if (ret)
439 			return ret;
440 		ieee80211_wake_queues(hw);
441 	}
442 
443 	mt7921_mutex_acquire(dev);
444 
445 	if (changed & IEEE80211_CONF_CHANGE_POWER)
446 		mt76_connac_mcu_set_rate_txpower(phy->mt76);
447 
448 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
449 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
450 
451 		if (!enabled)
452 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
453 		else
454 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
455 
456 		mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
457 			       enabled);
458 		mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
459 	}
460 
461 	mt7921_mutex_release(dev);
462 
463 	return 0;
464 }
465 
466 static int
467 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
468 	       const struct ieee80211_tx_queue_params *params)
469 {
470 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
471 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
472 
473 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
474 	queue = mt7921_lmac_mapping(dev, queue);
475 	mvif->queue_params[queue] = *params;
476 
477 	return 0;
478 }
479 
480 static void mt7921_configure_filter(struct ieee80211_hw *hw,
481 				    unsigned int changed_flags,
482 				    unsigned int *total_flags,
483 				    u64 multicast)
484 {
485 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
486 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
487 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
488 			MT_WF_RFCR1_DROP_BF_POLL |
489 			MT_WF_RFCR1_DROP_BA |
490 			MT_WF_RFCR1_DROP_CFEND |
491 			MT_WF_RFCR1_DROP_CFACK;
492 	u32 flags = 0;
493 
494 #define MT76_FILTER(_flag, _hw) do {					\
495 		flags |= *total_flags & FIF_##_flag;			\
496 		phy->rxfilter &= ~(_hw);				\
497 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
498 	} while (0)
499 
500 	mt7921_mutex_acquire(dev);
501 
502 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
503 			   MT_WF_RFCR_DROP_OTHER_BEACON |
504 			   MT_WF_RFCR_DROP_FRAME_REPORT |
505 			   MT_WF_RFCR_DROP_PROBEREQ |
506 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
507 			   MT_WF_RFCR_DROP_MCAST |
508 			   MT_WF_RFCR_DROP_BCAST |
509 			   MT_WF_RFCR_DROP_DUPLICATE |
510 			   MT_WF_RFCR_DROP_A2_BSSID |
511 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
512 			   MT_WF_RFCR_DROP_STBC_MULTI);
513 
514 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
515 			       MT_WF_RFCR_DROP_A3_MAC |
516 			       MT_WF_RFCR_DROP_A3_BSSID);
517 
518 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
519 
520 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
521 			     MT_WF_RFCR_DROP_RTS |
522 			     MT_WF_RFCR_DROP_CTL_RSV |
523 			     MT_WF_RFCR_DROP_NDPA);
524 
525 	*total_flags = flags;
526 	mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
527 
528 	if (*total_flags & FIF_CONTROL)
529 		mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
530 	else
531 		mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
532 
533 	mt7921_mutex_release(dev);
534 }
535 
536 static int
537 mt7921_bss_bcnft_apply(struct mt7921_dev *dev, struct ieee80211_vif *vif,
538 		       bool assoc)
539 {
540 	int ret;
541 
542 	if (!dev->pm.enable)
543 		return 0;
544 
545 	if (assoc) {
546 		ret = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
547 		if (ret)
548 			return ret;
549 
550 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
551 		mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
552 
553 		return 0;
554 	}
555 
556 	ret = mt7921_mcu_set_bss_pm(dev, vif, false);
557 	if (ret)
558 		return ret;
559 
560 	vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
561 	mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
562 
563 	return 0;
564 }
565 
566 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
567 				    struct ieee80211_vif *vif,
568 				    struct ieee80211_bss_conf *info,
569 				    u32 changed)
570 {
571 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
572 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
573 
574 	mt7921_mutex_acquire(dev);
575 
576 	if (changed & BSS_CHANGED_ERP_SLOT) {
577 		int slottime = info->use_short_slot ? 9 : 20;
578 
579 		if (slottime != phy->slottime) {
580 			phy->slottime = slottime;
581 			mt7921_mac_set_timing(phy);
582 		}
583 	}
584 
585 	/* ensure that enable txcmd_mode after bss_info */
586 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
587 		mt7921_mcu_set_tx(dev, vif);
588 
589 	if (changed & BSS_CHANGED_PS)
590 		mt7921_mcu_uni_bss_ps(dev, vif);
591 
592 	if (changed & BSS_CHANGED_ASSOC) {
593 		mt7921_mcu_sta_update(dev, NULL, vif, true,
594 				      MT76_STA_INFO_STATE_ASSOC);
595 		mt7921_bss_bcnft_apply(dev, vif, info->assoc);
596 	}
597 
598 	if (changed & BSS_CHANGED_ARP_FILTER) {
599 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
600 
601 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
602 						  info);
603 	}
604 
605 	mt7921_mutex_release(dev);
606 }
607 
608 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
609 		       struct ieee80211_sta *sta)
610 {
611 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
612 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
613 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
614 	int ret, idx;
615 
616 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
617 	if (idx < 0)
618 		return -ENOSPC;
619 
620 	INIT_LIST_HEAD(&msta->stats_list);
621 	INIT_LIST_HEAD(&msta->poll_list);
622 	msta->vif = mvif;
623 	msta->wcid.sta = 1;
624 	msta->wcid.idx = idx;
625 	msta->wcid.ext_phy = mvif->mt76.band_idx;
626 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
627 	msta->stats.jiffies = jiffies;
628 
629 	ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
630 	if (ret)
631 		return ret;
632 
633 	if (vif->type == NL80211_IFTYPE_STATION)
634 		mvif->wep_sta = msta;
635 
636 	mt7921_mac_wtbl_update(dev, idx,
637 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
638 
639 	ret = mt7921_mcu_sta_update(dev, sta, vif, true,
640 				    MT76_STA_INFO_STATE_NONE);
641 	if (ret)
642 		return ret;
643 
644 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
645 
646 	return 0;
647 }
648 
649 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
650 			  struct ieee80211_sta *sta)
651 {
652 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
653 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
654 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
655 
656 	mt7921_mutex_acquire(dev);
657 
658 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
659 		mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
660 					    true);
661 
662 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
663 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
664 
665 	mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC);
666 
667 	mt7921_mutex_release(dev);
668 }
669 
670 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
671 			   struct ieee80211_sta *sta)
672 {
673 	struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
674 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
675 
676 	mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
677 	mt76_connac_pm_wake(&dev->mphy, &dev->pm);
678 
679 	mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE);
680 	mt7921_mac_wtbl_update(dev, msta->wcid.idx,
681 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
682 
683 	if (vif->type == NL80211_IFTYPE_STATION) {
684 		struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
685 
686 		mvif->wep_sta = NULL;
687 		ewma_rssi_init(&mvif->rssi);
688 		if (!sta->tdls)
689 			mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
690 						    &mvif->sta.wcid, false);
691 	}
692 
693 	spin_lock_bh(&dev->sta_poll_lock);
694 	if (!list_empty(&msta->poll_list))
695 		list_del_init(&msta->poll_list);
696 	if (!list_empty(&msta->stats_list))
697 		list_del_init(&msta->stats_list);
698 	spin_unlock_bh(&dev->sta_poll_lock);
699 
700 	mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
701 }
702 
703 void mt7921_tx_worker(struct mt76_worker *w)
704 {
705 	struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
706 					      mt76.tx_worker);
707 
708 	if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
709 		queue_work(dev->mt76.wq, &dev->pm.wake_work);
710 		return;
711 	}
712 
713 	mt76_txq_schedule_all(&dev->mphy);
714 	mt76_connac_pm_unref(&dev->mphy, &dev->pm);
715 }
716 
717 static void mt7921_tx(struct ieee80211_hw *hw,
718 		      struct ieee80211_tx_control *control,
719 		      struct sk_buff *skb)
720 {
721 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
722 	struct mt76_phy *mphy = hw->priv;
723 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
724 	struct ieee80211_vif *vif = info->control.vif;
725 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
726 	int qid;
727 
728 	if (control->sta) {
729 		struct mt7921_sta *sta;
730 
731 		sta = (struct mt7921_sta *)control->sta->drv_priv;
732 		wcid = &sta->wcid;
733 	}
734 
735 	if (vif && !control->sta) {
736 		struct mt7921_vif *mvif;
737 
738 		mvif = (struct mt7921_vif *)vif->drv_priv;
739 		wcid = &mvif->sta.wcid;
740 	}
741 
742 	if (mt76_connac_pm_ref(mphy, &dev->pm)) {
743 		mt76_tx(mphy, control->sta, wcid, skb);
744 		mt76_connac_pm_unref(mphy, &dev->pm);
745 		return;
746 	}
747 
748 	qid = skb_get_queue_mapping(skb);
749 	if (qid >= MT_TXQ_PSD) {
750 		qid = IEEE80211_AC_BE;
751 		skb_set_queue_mapping(skb, qid);
752 	}
753 
754 	mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
755 }
756 
757 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
758 {
759 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
760 
761 	mt7921_mutex_acquire(dev);
762 	mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
763 	mt7921_mutex_release(dev);
764 
765 	return 0;
766 }
767 
768 static int
769 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
770 		    struct ieee80211_ampdu_params *params)
771 {
772 	enum ieee80211_ampdu_mlme_action action = params->action;
773 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
774 	struct ieee80211_sta *sta = params->sta;
775 	struct ieee80211_txq *txq = sta->txq[params->tid];
776 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
777 	u16 tid = params->tid;
778 	u16 ssn = params->ssn;
779 	struct mt76_txq *mtxq;
780 	int ret = 0;
781 
782 	if (!txq)
783 		return -EINVAL;
784 
785 	mtxq = (struct mt76_txq *)txq->drv_priv;
786 
787 	mt7921_mutex_acquire(dev);
788 	switch (action) {
789 	case IEEE80211_AMPDU_RX_START:
790 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
791 				   params->buf_size);
792 		mt7921_mcu_uni_rx_ba(dev, params, true);
793 		break;
794 	case IEEE80211_AMPDU_RX_STOP:
795 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
796 		mt7921_mcu_uni_rx_ba(dev, params, false);
797 		break;
798 	case IEEE80211_AMPDU_TX_OPERATIONAL:
799 		mtxq->aggr = true;
800 		mtxq->send_bar = false;
801 		mt7921_mcu_uni_tx_ba(dev, params, true);
802 		break;
803 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
804 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
805 		mtxq->aggr = false;
806 		clear_bit(tid, &msta->ampdu_state);
807 		mt7921_mcu_uni_tx_ba(dev, params, false);
808 		break;
809 	case IEEE80211_AMPDU_TX_START:
810 		set_bit(tid, &msta->ampdu_state);
811 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
812 		break;
813 	case IEEE80211_AMPDU_TX_STOP_CONT:
814 		mtxq->aggr = false;
815 		clear_bit(tid, &msta->ampdu_state);
816 		mt7921_mcu_uni_tx_ba(dev, params, false);
817 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
818 		break;
819 	}
820 	mt7921_mutex_release(dev);
821 
822 	return ret;
823 }
824 
825 static int mt7921_sta_state(struct ieee80211_hw *hw,
826 			    struct ieee80211_vif *vif,
827 			    struct ieee80211_sta *sta,
828 			    enum ieee80211_sta_state old_state,
829 			    enum ieee80211_sta_state new_state)
830 {
831 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
832 
833 	if (dev->pm.ds_enable) {
834 		mt7921_mutex_acquire(dev);
835 		mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state);
836 		mt7921_mutex_release(dev);
837 	}
838 
839 	return mt76_sta_state(hw, vif, sta, old_state, new_state);
840 }
841 
842 static int
843 mt7921_get_stats(struct ieee80211_hw *hw,
844 		 struct ieee80211_low_level_stats *stats)
845 {
846 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
847 	struct mib_stats *mib = &phy->mib;
848 
849 	mt7921_mutex_acquire(phy->dev);
850 
851 	stats->dot11RTSSuccessCount = mib->rts_cnt;
852 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
853 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
854 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
855 
856 	memset(mib, 0, sizeof(*mib));
857 
858 	mt7921_mutex_release(phy->dev);
859 
860 	return 0;
861 }
862 
863 static u64
864 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
865 {
866 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
867 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
868 	u8 omac_idx = mvif->mt76.omac_idx;
869 	union {
870 		u64 t64;
871 		u32 t32[2];
872 	} tsf;
873 	u16 n;
874 
875 	mt7921_mutex_acquire(dev);
876 
877 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
878 	/* TSF software read */
879 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
880 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
881 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
882 
883 	mt7921_mutex_release(dev);
884 
885 	return tsf.t64;
886 }
887 
888 static void
889 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
890 	       u64 timestamp)
891 {
892 	struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
893 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
894 	u8 omac_idx = mvif->mt76.omac_idx;
895 	union {
896 		u64 t64;
897 		u32 t32[2];
898 	} tsf = { .t64 = timestamp, };
899 	u16 n;
900 
901 	mt7921_mutex_acquire(dev);
902 
903 	n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
904 	mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
905 	mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
906 	/* TSF software overwrite */
907 	mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
908 
909 	mt7921_mutex_release(dev);
910 }
911 
912 static void
913 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
914 {
915 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
916 	struct mt7921_dev *dev = phy->dev;
917 
918 	mt7921_mutex_acquire(dev);
919 	phy->coverage_class = max_t(s16, coverage_class, 0);
920 	mt7921_mac_set_timing(phy);
921 	mt7921_mutex_release(dev);
922 }
923 
924 void mt7921_scan_work(struct work_struct *work)
925 {
926 	struct mt7921_phy *phy;
927 
928 	phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
929 						scan_work.work);
930 
931 	while (true) {
932 		struct mt7921_mcu_rxd *rxd;
933 		struct sk_buff *skb;
934 
935 		spin_lock_bh(&phy->dev->mt76.lock);
936 		skb = __skb_dequeue(&phy->scan_event_list);
937 		spin_unlock_bh(&phy->dev->mt76.lock);
938 
939 		if (!skb)
940 			break;
941 
942 		rxd = (struct mt7921_mcu_rxd *)skb->data;
943 		if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
944 			ieee80211_sched_scan_results(phy->mt76->hw);
945 		} else if (test_and_clear_bit(MT76_HW_SCANNING,
946 					      &phy->mt76->state)) {
947 			struct cfg80211_scan_info info = {
948 				.aborted = false,
949 			};
950 
951 			ieee80211_scan_completed(phy->mt76->hw, &info);
952 		}
953 		dev_kfree_skb(skb);
954 	}
955 }
956 
957 static int
958 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
959 	       struct ieee80211_scan_request *req)
960 {
961 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
962 	struct mt76_phy *mphy = hw->priv;
963 	int err;
964 
965 	mt7921_mutex_acquire(dev);
966 	err = mt76_connac_mcu_hw_scan(mphy, vif, req);
967 	mt7921_mutex_release(dev);
968 
969 	return err;
970 }
971 
972 static void
973 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
974 {
975 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
976 	struct mt76_phy *mphy = hw->priv;
977 
978 	mt7921_mutex_acquire(dev);
979 	mt76_connac_mcu_cancel_hw_scan(mphy, vif);
980 	mt7921_mutex_release(dev);
981 }
982 
983 static int
984 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
985 			struct cfg80211_sched_scan_request *req,
986 			struct ieee80211_scan_ies *ies)
987 {
988 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
989 	struct mt76_phy *mphy = hw->priv;
990 	int err;
991 
992 	mt7921_mutex_acquire(dev);
993 
994 	err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
995 	if (err < 0)
996 		goto out;
997 
998 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
999 out:
1000 	mt7921_mutex_release(dev);
1001 
1002 	return err;
1003 }
1004 
1005 static int
1006 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1007 {
1008 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1009 	struct mt76_phy *mphy = hw->priv;
1010 	int err;
1011 
1012 	mt7921_mutex_acquire(dev);
1013 	err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1014 	mt7921_mutex_release(dev);
1015 
1016 	return err;
1017 }
1018 
1019 static int
1020 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1021 {
1022 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1023 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1024 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1025 
1026 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1027 		return -EINVAL;
1028 
1029 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1030 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1031 
1032 	mt7921_mutex_acquire(dev);
1033 
1034 	phy->mt76->antenna_mask = tx_ant;
1035 	phy->mt76->chainmask = tx_ant;
1036 
1037 	mt76_set_stream_caps(phy->mt76, true);
1038 	mt7921_set_stream_he_caps(phy);
1039 
1040 	mt7921_mutex_release(dev);
1041 
1042 	return 0;
1043 }
1044 
1045 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1046 				  struct ieee80211_vif *vif,
1047 				  struct ieee80211_sta *sta,
1048 				  struct station_info *sinfo)
1049 {
1050 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1051 	struct mt7921_sta_stats *stats = &msta->stats;
1052 
1053 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
1054 		return;
1055 
1056 	if (stats->tx_rate.legacy) {
1057 		sinfo->txrate.legacy = stats->tx_rate.legacy;
1058 	} else {
1059 		sinfo->txrate.mcs = stats->tx_rate.mcs;
1060 		sinfo->txrate.nss = stats->tx_rate.nss;
1061 		sinfo->txrate.bw = stats->tx_rate.bw;
1062 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
1063 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
1064 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
1065 	}
1066 	sinfo->txrate.flags = stats->tx_rate.flags;
1067 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1068 }
1069 
1070 #ifdef CONFIG_PM
1071 static int mt7921_suspend(struct ieee80211_hw *hw,
1072 			  struct cfg80211_wowlan *wowlan)
1073 {
1074 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1075 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1076 	int err;
1077 
1078 	cancel_delayed_work_sync(&phy->scan_work);
1079 	cancel_delayed_work_sync(&phy->mt76->mac_work);
1080 
1081 	cancel_delayed_work_sync(&dev->pm.ps_work);
1082 	mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1083 
1084 	mt7921_mutex_acquire(dev);
1085 
1086 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1087 
1088 	set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1089 	ieee80211_iterate_active_interfaces(hw,
1090 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1091 					    mt76_connac_mcu_set_suspend_iter,
1092 					    &dev->mphy);
1093 
1094 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1095 
1096 	mt7921_mutex_release(dev);
1097 
1098 	return err;
1099 }
1100 
1101 static int mt7921_resume(struct ieee80211_hw *hw)
1102 {
1103 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1104 	struct mt7921_phy *phy = mt7921_hw_phy(hw);
1105 	int err;
1106 
1107 	mt7921_mutex_acquire(dev);
1108 
1109 	err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1110 	if (err < 0)
1111 		goto out;
1112 
1113 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1114 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1115 	ieee80211_iterate_active_interfaces(hw,
1116 					    IEEE80211_IFACE_ITER_RESUME_ALL,
1117 					    mt76_connac_mcu_set_suspend_iter,
1118 					    &dev->mphy);
1119 
1120 	ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1121 				     MT7921_WATCHDOG_TIME);
1122 out:
1123 
1124 	mt7921_mutex_release(dev);
1125 
1126 	return err;
1127 }
1128 
1129 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1130 {
1131 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1132 	struct mt76_dev *mdev = &dev->mt76;
1133 
1134 	device_set_wakeup_enable(mdev->dev, enabled);
1135 }
1136 
1137 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1138 				  struct ieee80211_vif *vif,
1139 				  struct cfg80211_gtk_rekey_data *data)
1140 {
1141 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1142 
1143 	mt7921_mutex_acquire(dev);
1144 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1145 	mt7921_mutex_release(dev);
1146 }
1147 #endif /* CONFIG_PM */
1148 
1149 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1150 			 u32 queues, bool drop)
1151 {
1152 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1153 
1154 	wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1155 			   HZ / 2);
1156 }
1157 
1158 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
1159 					 struct ieee80211_vif *vif,
1160 					 struct ieee80211_sta *sta,
1161 					 bool enabled)
1162 {
1163 	struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1164 	struct mt7921_dev *dev = mt7921_hw_dev(hw);
1165 
1166 	if (enabled)
1167 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1168 	else
1169 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1170 
1171 	mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid,
1172 					     MCU_UNI_CMD_STA_REC_UPDATE);
1173 }
1174 
1175 const struct ieee80211_ops mt7921_ops = {
1176 	.tx = mt7921_tx,
1177 	.start = mt7921_start,
1178 	.stop = mt7921_stop,
1179 	.add_interface = mt7921_add_interface,
1180 	.remove_interface = mt7921_remove_interface,
1181 	.config = mt7921_config,
1182 	.conf_tx = mt7921_conf_tx,
1183 	.configure_filter = mt7921_configure_filter,
1184 	.bss_info_changed = mt7921_bss_info_changed,
1185 	.sta_state = mt7921_sta_state,
1186 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1187 	.set_key = mt7921_set_key,
1188 	.sta_set_decap_offload = mt7921_sta_set_decap_offload,
1189 	.ampdu_action = mt7921_ampdu_action,
1190 	.set_rts_threshold = mt7921_set_rts_threshold,
1191 	.wake_tx_queue = mt76_wake_tx_queue,
1192 	.release_buffered_frames = mt76_release_buffered_frames,
1193 	.get_txpower = mt76_get_txpower,
1194 	.get_stats = mt7921_get_stats,
1195 	.get_tsf = mt7921_get_tsf,
1196 	.set_tsf = mt7921_set_tsf,
1197 	.get_survey = mt76_get_survey,
1198 	.get_antenna = mt76_get_antenna,
1199 	.set_antenna = mt7921_set_antenna,
1200 	.set_coverage_class = mt7921_set_coverage_class,
1201 	.hw_scan = mt7921_hw_scan,
1202 	.cancel_hw_scan = mt7921_cancel_hw_scan,
1203 	.sta_statistics = mt7921_sta_statistics,
1204 	.sched_scan_start = mt7921_start_sched_scan,
1205 	.sched_scan_stop = mt7921_stop_sched_scan,
1206 #ifdef CONFIG_PM
1207 	.suspend = mt7921_suspend,
1208 	.resume = mt7921_resume,
1209 	.set_wakeup = mt7921_set_wakeup,
1210 	.set_rekey_data = mt7921_set_rekey_data,
1211 #endif /* CONFIG_PM */
1212 	.flush = mt7921_flush,
1213 };
1214