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