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